blob: cb8a3544896fb613578f2a5bdf1b096ea0bde205 [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;
574 while (*s >= ' '
575#ifndef EBCDIC
Bram Moolenaar30613902019-12-01 22:11:18 +0100576 && *s < DEL // EBCDIC: all chars above space are normal
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577#endif
Bram Moolenaarebefac62005-12-28 22:39:57 +0000578 && (len < 0 || s - str < len))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 ++s;
580
Bram Moolenaar30613902019-12-01 22:11:18 +0100581 // Don't put '0' or '^' as last character, just in case a CTRL-D is
582 // typed next.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 if (*s == NUL && (s[-1] == '0' || s[-1] == '^'))
584 --s;
585 if (s > start)
586 add_buff(&redobuff, start, (long)(s - start));
587
Bram Moolenaarebefac62005-12-28 22:39:57 +0000588 if (*s == NUL || (len >= 0 && s - str >= len))
589 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590
Bram Moolenaar30613902019-12-01 22:11:18 +0100591 // Handle a special or multibyte character.
Bram Moolenaarebefac62005-12-28 22:39:57 +0000592 if (has_mbyte)
Bram Moolenaar30613902019-12-01 22:11:18 +0100593 // Handle composing chars separately.
Bram Moolenaarebefac62005-12-28 22:39:57 +0000594 c = mb_cptr2char_adv(&s);
595 else
Bram Moolenaarebefac62005-12-28 22:39:57 +0000596 c = *s++;
597 if (c < ' ' || c == DEL || (*s == NUL && (c == '0' || c == '^')))
598 add_char_buff(&redobuff, Ctrl_V);
599
Bram Moolenaar30613902019-12-01 22:11:18 +0100600 // CTRL-V '0' must be inserted as CTRL-V 048 (EBCDIC: xf0)
Bram Moolenaarebefac62005-12-28 22:39:57 +0000601 if (*s == NUL && c == '0')
602#ifdef EBCDIC
603 add_buff(&redobuff, (char_u *)"xf0", 3L);
604#else
605 add_buff(&redobuff, (char_u *)"048", 3L);
606#endif
607 else
608 add_char_buff(&redobuff, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 }
610}
611
612/*
613 * Append a character to the redo buffer.
614 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
615 */
616 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100617AppendCharToRedobuff(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618{
619 if (!block_redo)
620 add_char_buff(&redobuff, c);
621}
622
623/*
624 * Append a number to the redo buffer.
625 */
626 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100627AppendNumberToRedobuff(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628{
629 if (!block_redo)
630 add_num_buff(&redobuff, n);
631}
632
633/*
634 * Append string "s" to the stuff buffer.
635 * CSI and K_SPECIAL must already have been escaped.
636 */
637 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100638stuffReadbuff(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100640 add_buff(&readbuf1, s, -1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641}
642
Bram Moolenaar4f5ce332014-07-30 16:00:58 +0200643/*
644 * Append string "s" to the redo stuff buffer.
645 * CSI and K_SPECIAL must already have been escaped.
646 */
647 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100648stuffRedoReadbuff(char_u *s)
Bram Moolenaar4f5ce332014-07-30 16:00:58 +0200649{
650 add_buff(&readbuf2, s, -1L);
651}
652
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200653 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100654stuffReadbuffLen(char_u *s, long len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100656 add_buff(&readbuf1, s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657}
658
659#if defined(FEAT_EVAL) || defined(PROTO)
660/*
661 * Stuff "s" into the stuff buffer, leaving special key codes unmodified and
662 * escaping other K_SPECIAL and CSI bytes.
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200663 * Change CR, LF and ESC into a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 */
665 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100666stuffReadbuffSpec(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667{
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200668 int c;
669
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 while (*s != NUL)
671 {
672 if (*s == K_SPECIAL && s[1] != NUL && s[2] != NUL)
673 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100674 // Insert special key literally.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 stuffReadbuffLen(s, 3L);
676 s += 3;
677 }
678 else
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200679 {
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200680 c = mb_ptr2char_adv(&s);
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200681 if (c == CAR || c == NL || c == ESC)
682 c = ' ';
683 stuffcharReadbuff(c);
684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 }
686}
687#endif
688
689/*
690 * Append a character to the stuff buffer.
691 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
692 */
693 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100694stuffcharReadbuff(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100696 add_char_buff(&readbuf1, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697}
698
699/*
700 * Append a number to the stuff buffer.
701 */
702 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100703stuffnumReadbuff(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100705 add_num_buff(&readbuf1, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706}
707
708/*
Bram Moolenaar11abd092020-05-01 14:26:37 +0200709 * Stuff a string into the typeahead buffer, such that edit() will insert it
710 * literally ("literally" TRUE) or interpret is as typed characters.
711 */
712 void
713stuffescaped(char_u *arg, int literally)
714{
715 int c;
716 char_u *start;
717
718 while (*arg != NUL)
719 {
720 // Stuff a sequence of normal ASCII characters, that's fast. Also
721 // stuff K_SPECIAL to get the effect of a special key when "literally"
722 // is TRUE.
723 start = arg;
724 while ((*arg >= ' '
725#ifndef EBCDIC
726 && *arg < DEL // EBCDIC: chars above space are normal
727#endif
728 )
729 || (*arg == K_SPECIAL && !literally))
730 ++arg;
731 if (arg > start)
732 stuffReadbuffLen(start, (long)(arg - start));
733
734 // stuff a single special character
735 if (*arg != NUL)
736 {
737 if (has_mbyte)
738 c = mb_cptr2char_adv(&arg);
739 else
740 c = *arg++;
741 if (literally && ((c < ' ' && c != TAB) || c == DEL))
742 stuffcharReadbuff(Ctrl_V);
743 stuffcharReadbuff(c);
744 }
745 }
746}
747
748/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 * Read a character from the redo buffer. Translates K_SPECIAL, CSI and
750 * multibyte characters.
751 * The redo buffer is left as it is.
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100752 * If init is TRUE, prepare for redo, return FAIL if nothing to redo, OK
753 * otherwise.
754 * If old is TRUE, use old_redobuff instead of redobuff.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 */
756 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100757read_redo(int init, int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100759 static buffblock_T *bp;
760 static char_u *p;
761 int c;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100762 int n;
763 char_u buf[MB_MAXBYTES + 1];
764 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765
766 if (init)
767 {
768 if (old_redo)
Bram Moolenaar285e3352018-04-18 23:01:13 +0200769 bp = old_redobuff.bh_first.b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 else
Bram Moolenaar285e3352018-04-18 23:01:13 +0200771 bp = redobuff.bh_first.b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 if (bp == NULL)
773 return FAIL;
774 p = bp->b_str;
775 return OK;
776 }
777 if ((c = *p) != NUL)
778 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100779 // Reverse the conversion done by add_char_buff()
780 // For a multi-byte character get all the bytes and return the
781 // converted character.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 if (has_mbyte && (c != K_SPECIAL || p[1] == KS_SPECIAL))
783 n = MB_BYTE2LEN_CHECK(c);
784 else
785 n = 1;
786 for (i = 0; ; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100788 if (c == K_SPECIAL) // special key or escaped K_SPECIAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 {
790 c = TO_SPECIAL(p[1], p[2]);
791 p += 2;
792 }
793#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +0100794 if (c == CSI) // escaped CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 p += 2;
796#endif
797 if (*++p == NUL && bp->b_next != NULL)
798 {
799 bp = bp->b_next;
800 p = bp->b_str;
801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 buf[i] = c;
Bram Moolenaar30613902019-12-01 22:11:18 +0100803 if (i == n - 1) // last byte of a character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 {
805 if (n != 1)
806 c = (*mb_ptr2char)(buf);
807 break;
808 }
809 c = *p;
Bram Moolenaar30613902019-12-01 22:11:18 +0100810 if (c == NUL) // cannot happen?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 }
813 }
814
815 return c;
816}
817
818/*
819 * Copy the rest of the redo buffer into the stuff buffer (in a slow way).
820 * If old_redo is TRUE, use old_redobuff instead of redobuff.
821 * The escaped K_SPECIAL and CSI are copied without translation.
822 */
823 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100824copy_redo(int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825{
826 int c;
827
828 while ((c = read_redo(FALSE, old_redo)) != NUL)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100829 add_char_buff(&readbuf2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830}
831
832/*
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100833 * Stuff the redo buffer into readbuf2.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 * Insert the redo count into the command.
835 * If "old_redo" is TRUE, the last but one command is repeated
836 * instead of the last command (inserting text). This is used for
837 * CTRL-O <.> in insert mode
838 *
839 * return FAIL for failure, OK otherwise
840 */
841 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100842start_redo(long count, int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843{
844 int c;
845
Bram Moolenaar30613902019-12-01 22:11:18 +0100846 // init the pointers; return if nothing to redo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 if (read_redo(TRUE, old_redo) == FAIL)
848 return FAIL;
849
850 c = read_redo(FALSE, old_redo);
851
Bram Moolenaar30613902019-12-01 22:11:18 +0100852 // copy the buffer name, if present
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 if (c == '"')
854 {
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100855 add_buff(&readbuf2, (char_u *)"\"", 1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 c = read_redo(FALSE, old_redo);
857
Bram Moolenaar30613902019-12-01 22:11:18 +0100858 // if a numbered buffer is used, increment the number
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 if (c >= '1' && c < '9')
860 ++c;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100861 add_char_buff(&readbuf2, c);
Bram Moolenaar833093b2018-05-23 21:53:52 +0200862
Bram Moolenaar30613902019-12-01 22:11:18 +0100863 // the expression register should be re-evaluated
Bram Moolenaar833093b2018-05-23 21:53:52 +0200864 if (c == '=')
865 {
866 add_char_buff(&readbuf2, CAR);
867 cmd_silent = TRUE;
868 }
869
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 c = read_redo(FALSE, old_redo);
871 }
872
Bram Moolenaar30613902019-12-01 22:11:18 +0100873 if (c == 'v') // redo Visual
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 {
875 VIsual = curwin->w_cursor;
876 VIsual_active = TRUE;
877 VIsual_select = FALSE;
878 VIsual_reselect = TRUE;
879 redo_VIsual_busy = TRUE;
880 c = read_redo(FALSE, old_redo);
881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882
Bram Moolenaar30613902019-12-01 22:11:18 +0100883 // try to enter the count (in place of a previous count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 if (count)
885 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100886 while (VIM_ISDIGIT(c)) // skip "old" count
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 c = read_redo(FALSE, old_redo);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100888 add_num_buff(&readbuf2, count);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 }
890
Bram Moolenaar30613902019-12-01 22:11:18 +0100891 // copy from the redo buffer into the stuff buffer
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100892 add_char_buff(&readbuf2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 copy_redo(old_redo);
894 return OK;
895}
896
897/*
898 * Repeat the last insert (R, o, O, a, A, i or I command) by stuffing
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100899 * the redo buffer into readbuf2.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 * return FAIL for failure, OK otherwise
901 */
902 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100903start_redo_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904{
905 int c;
906
907 if (read_redo(TRUE, FALSE) == FAIL)
908 return FAIL;
909 start_stuff();
910
Bram Moolenaar30613902019-12-01 22:11:18 +0100911 // skip the count and the command character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 while ((c = read_redo(FALSE, FALSE)) != NUL)
913 {
914 if (vim_strchr((char_u *)"AaIiRrOo", c) != NULL)
915 {
916 if (c == 'O' || c == 'o')
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100917 add_buff(&readbuf2, NL_STR, -1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 break;
919 }
920 }
921
Bram Moolenaar30613902019-12-01 22:11:18 +0100922 // copy the typed text from the redo buffer into the stuff buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 copy_redo(FALSE);
924 block_redo = TRUE;
925 return OK;
926}
927
928 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100929stop_redo_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930{
931 block_redo = FALSE;
932}
933
934/*
935 * Initialize typebuf.tb_buf to point to typebuf_init.
936 * alloc() cannot be used here: In out-of-memory situations it would
937 * be impossible to type anything.
938 */
939 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100940init_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941{
942 if (typebuf.tb_buf == NULL)
943 {
944 typebuf.tb_buf = typebuf_init;
945 typebuf.tb_noremap = noremapbuf_init;
946 typebuf.tb_buflen = TYPELEN_INIT;
947 typebuf.tb_len = 0;
Bram Moolenaard34f9b12017-04-08 18:41:13 +0200948 typebuf.tb_off = MAXMAPLEN + 4;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 typebuf.tb_change_cnt = 1;
950 }
951}
952
953/*
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200954 * Returns TRUE when keys cannot be remapped.
955 */
956 int
957noremap_keys(void)
958{
959 return KeyNoremap & (RM_NONE|RM_SCRIPT);
960}
961
962/*
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100963 * Insert a string in position 'offset' in the typeahead buffer (for "@r"
964 * and ":normal" command, vgetorpeek() and check_termcode()).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 *
966 * If noremap is REMAP_YES, new string can be mapped again.
967 * If noremap is REMAP_NONE, new string cannot be mapped again.
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000968 * If noremap is REMAP_SKIP, first char of new string cannot be mapped again,
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000969 * but abbreviations are allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 * If noremap is REMAP_SCRIPT, new string cannot be mapped again, except for
971 * script-local mappings.
972 * If noremap is > 0, that many characters of the new string cannot be mapped.
973 *
974 * If nottyped is TRUE, the string does not return KeyTyped (don't use when
975 * offset is non-zero!).
976 *
977 * If silent is TRUE, cmd_silent is set when the characters are obtained.
978 *
979 * return FAIL for failure, OK otherwise
980 */
981 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100982ins_typebuf(
983 char_u *str,
984 int noremap,
985 int offset,
986 int nottyped,
987 int silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988{
989 char_u *s1, *s2;
990 int newlen;
991 int addlen;
992 int i;
993 int newoff;
994 int val;
995 int nrm;
996
997 init_typebuf();
998 if (++typebuf.tb_change_cnt == 0)
999 typebuf.tb_change_cnt = 1;
Bram Moolenaard103ee72019-09-18 21:15:31 +02001000 state_no_longer_safe("ins_typebuf()");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001
1002 addlen = (int)STRLEN(str);
Bram Moolenaar4e86cba2007-05-06 11:56:32 +00001003
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 if (offset == 0 && addlen <= typebuf.tb_off)
1005 {
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001006 /*
1007 * Easy case: there is room in front of typebuf.tb_buf[typebuf.tb_off]
1008 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 typebuf.tb_off -= addlen;
1010 mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen);
1011 }
Bram Moolenaard34f9b12017-04-08 18:41:13 +02001012 else if (typebuf.tb_len == 0 && typebuf.tb_buflen
1013 >= addlen + 3 * (MAXMAPLEN + 4))
1014 {
1015 /*
1016 * Buffer is empty and string fits in the existing buffer.
1017 * Leave some space before and after, if possible.
1018 */
1019 typebuf.tb_off = (typebuf.tb_buflen - addlen - 3 * (MAXMAPLEN + 4)) / 2;
1020 mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen);
1021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 else
1023 {
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001024 int extra;
1025
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001026 /*
1027 * Need to allocate a new buffer.
Bram Moolenaard34f9b12017-04-08 18:41:13 +02001028 * In typebuf.tb_buf there must always be room for 3 * (MAXMAPLEN + 4)
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001029 * characters. We add some extra room to avoid having to allocate too
1030 * often.
1031 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 newoff = MAXMAPLEN + 4;
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001033 extra = addlen + newoff + 4 * (MAXMAPLEN + 4);
1034 if (typebuf.tb_len > 2147483647 - extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 {
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001036 // string is getting too long for a 32 bit int
Bram Moolenaar12f3c1b2021-12-05 21:46:34 +00001037 emsg(_(e_command_too_complex)); // also calls flush_buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 setcursor();
1039 return FAIL;
1040 }
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001041 newlen = typebuf.tb_len + extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 s1 = alloc(newlen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001043 if (s1 == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 return FAIL;
1045 s2 = alloc(newlen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001046 if (s2 == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 {
1048 vim_free(s1);
1049 return FAIL;
1050 }
1051 typebuf.tb_buflen = newlen;
1052
Bram Moolenaar30613902019-12-01 22:11:18 +01001053 // copy the old chars, before the insertion point
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 mch_memmove(s1 + newoff, typebuf.tb_buf + typebuf.tb_off,
1055 (size_t)offset);
Bram Moolenaar30613902019-12-01 22:11:18 +01001056 // copy the new chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 mch_memmove(s1 + newoff + offset, str, (size_t)addlen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001058 // copy the old chars, after the insertion point, including the NUL at
1059 // the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 mch_memmove(s1 + newoff + offset + addlen,
1061 typebuf.tb_buf + typebuf.tb_off + offset,
1062 (size_t)(typebuf.tb_len - offset + 1));
1063 if (typebuf.tb_buf != typebuf_init)
1064 vim_free(typebuf.tb_buf);
1065 typebuf.tb_buf = s1;
1066
1067 mch_memmove(s2 + newoff, typebuf.tb_noremap + typebuf.tb_off,
1068 (size_t)offset);
1069 mch_memmove(s2 + newoff + offset + addlen,
1070 typebuf.tb_noremap + typebuf.tb_off + offset,
1071 (size_t)(typebuf.tb_len - offset));
1072 if (typebuf.tb_noremap != noremapbuf_init)
1073 vim_free(typebuf.tb_noremap);
1074 typebuf.tb_noremap = s2;
1075
1076 typebuf.tb_off = newoff;
1077 }
1078 typebuf.tb_len += addlen;
1079
Bram Moolenaar30613902019-12-01 22:11:18 +01001080 // If noremap == REMAP_SCRIPT: do remap script-local mappings.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 if (noremap == REMAP_SCRIPT)
1082 val = RM_SCRIPT;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001083 else if (noremap == REMAP_SKIP)
1084 val = RM_ABBR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 else
1086 val = RM_NONE;
1087
1088 /*
1089 * Adjust typebuf.tb_noremap[] for the new characters:
1090 * If noremap == REMAP_NONE or REMAP_SCRIPT: new characters are
1091 * (sometimes) not remappable
1092 * If noremap == REMAP_YES: all the new characters are mappable
1093 * If noremap > 0: "noremap" characters are not remappable, the rest
1094 * mappable
1095 */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001096 if (noremap == REMAP_SKIP)
1097 nrm = 1;
1098 else if (noremap < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 nrm = addlen;
1100 else
1101 nrm = noremap;
1102 for (i = 0; i < addlen; ++i)
1103 typebuf.tb_noremap[typebuf.tb_off + i + offset] =
1104 (--nrm >= 0) ? val : RM_YES;
1105
Bram Moolenaar30613902019-12-01 22:11:18 +01001106 // tb_maplen and tb_silent only remember the length of mapped and/or
1107 // silent mappings at the start of the buffer, assuming that a mapped
1108 // sequence doesn't result in typed characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 if (nottyped || typebuf.tb_maplen > offset)
1110 typebuf.tb_maplen += addlen;
1111 if (silent || typebuf.tb_silent > offset)
1112 {
1113 typebuf.tb_silent += addlen;
1114 cmd_silent = TRUE;
1115 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001116 if (typebuf.tb_no_abbr_cnt && offset == 0) // and not used for abbrev.s
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 typebuf.tb_no_abbr_cnt += addlen;
1118
1119 return OK;
1120}
1121
1122/*
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001123 * Put character "c" back into the typeahead buffer.
1124 * Can be used for a character obtained by vgetc() that needs to be put back.
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00001125 * Uses cmd_silent, KeyTyped and KeyNoremap to restore the flags belonging to
1126 * the char.
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001127 * Returns the length of what was inserted.
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001128 */
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001129 int
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001130ins_char_typebuf(int c, int modifier)
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001131{
zeertzjq6cac7702022-01-04 18:01:21 +00001132 char_u buf[MB_MAXBYTES * 3 + 4];
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001133 int len = 0;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001134
1135 if (modifier != 0)
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001136 {
1137 buf[0] = K_SPECIAL;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001138 buf[1] = KS_MODIFIER;
1139 buf[2] = modifier;
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001140 buf[3] = NUL;
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001141 len = 3;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001142 }
1143 if (IS_SPECIAL(c))
1144 {
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001145 buf[len] = K_SPECIAL;
1146 buf[len + 1] = K_SECOND(c);
1147 buf[len + 2] = K_THIRD(c);
1148 buf[len + 3] = NUL;
1149 len += 3;
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001150 }
1151 else
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001152 {
zeertzjq6cac7702022-01-04 18:01:21 +00001153 char_u *p = buf + len;
1154 int char_len = (*mb_char2bytes)(c, p);
1155#ifdef FEAT_GUI
1156 int save_gui_in_use = gui.in_use;
1157
1158 gui.in_use = FALSE;
1159#endif
1160 // if the character contains CSI or K_SPECIAL bytes they need escaping
1161 len += fix_input_buffer(p, char_len);
1162#ifdef FEAT_GUI
1163 gui.in_use = save_gui_in_use;
1164#endif
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001165 }
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00001166 (void)ins_typebuf(buf, KeyNoremap, 0, !KeyTyped, cmd_silent);
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001167 return len;
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001168}
1169
1170/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 * Return TRUE if the typeahead buffer was changed (while waiting for a
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001172 * character to arrive). Happens when a message was received from a client or
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001173 * from feedkeys().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 * But check in a more generic way to avoid trouble: When "typebuf.tb_buf"
1175 * changed it was reallocated and the old pointer can no longer be used.
1176 * Or "typebuf.tb_off" may have been changed and we would overwrite characters
1177 * that was just added.
1178 */
1179 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001180typebuf_changed(
Bram Moolenaar30613902019-12-01 22:11:18 +01001181 int tb_change_cnt) // old value of typebuf.tb_change_cnt
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182{
1183 return (tb_change_cnt != 0 && (typebuf.tb_change_cnt != tb_change_cnt
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001184#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
1185 || typebuf_was_filled
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186#endif
1187 ));
1188}
1189
1190/*
1191 * Return TRUE if there are no characters in the typeahead buffer that have
1192 * not been typed (result from a mapping or come from ":normal").
1193 */
1194 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001195typebuf_typed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196{
1197 return typebuf.tb_maplen == 0;
1198}
1199
1200/*
1201 * Return the number of characters that are mapped (or not typed).
1202 */
1203 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001204typebuf_maplen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205{
1206 return typebuf.tb_maplen;
1207}
1208
1209/*
1210 * remove "len" characters from typebuf.tb_buf[typebuf.tb_off + offset]
1211 */
1212 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001213del_typebuf(int len, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214{
1215 int i;
1216
1217 if (len == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01001218 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219
1220 typebuf.tb_len -= len;
1221
1222 /*
1223 * Easy case: Just increase typebuf.tb_off.
1224 */
1225 if (offset == 0 && typebuf.tb_buflen - (typebuf.tb_off + len)
1226 >= 3 * MAXMAPLEN + 3)
1227 typebuf.tb_off += len;
1228 /*
1229 * Have to move the characters in typebuf.tb_buf[] and typebuf.tb_noremap[]
1230 */
1231 else
1232 {
1233 i = typebuf.tb_off + offset;
1234 /*
1235 * Leave some extra room at the end to avoid reallocation.
1236 */
1237 if (typebuf.tb_off > MAXMAPLEN)
1238 {
1239 mch_memmove(typebuf.tb_buf + MAXMAPLEN,
1240 typebuf.tb_buf + typebuf.tb_off, (size_t)offset);
1241 mch_memmove(typebuf.tb_noremap + MAXMAPLEN,
1242 typebuf.tb_noremap + typebuf.tb_off, (size_t)offset);
1243 typebuf.tb_off = MAXMAPLEN;
1244 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001245 // adjust typebuf.tb_buf (include the NUL at the end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset,
1247 typebuf.tb_buf + i + len,
1248 (size_t)(typebuf.tb_len - offset + 1));
Bram Moolenaar30613902019-12-01 22:11:18 +01001249 // adjust typebuf.tb_noremap[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 mch_memmove(typebuf.tb_noremap + typebuf.tb_off + offset,
1251 typebuf.tb_noremap + i + len,
1252 (size_t)(typebuf.tb_len - offset));
1253 }
1254
Bram Moolenaar30613902019-12-01 22:11:18 +01001255 if (typebuf.tb_maplen > offset) // adjust tb_maplen
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 {
1257 if (typebuf.tb_maplen < offset + len)
1258 typebuf.tb_maplen = offset;
1259 else
1260 typebuf.tb_maplen -= len;
1261 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001262 if (typebuf.tb_silent > offset) // adjust tb_silent
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 {
1264 if (typebuf.tb_silent < offset + len)
1265 typebuf.tb_silent = offset;
1266 else
1267 typebuf.tb_silent -= len;
1268 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001269 if (typebuf.tb_no_abbr_cnt > offset) // adjust tb_no_abbr_cnt
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 {
1271 if (typebuf.tb_no_abbr_cnt < offset + len)
1272 typebuf.tb_no_abbr_cnt = offset;
1273 else
1274 typebuf.tb_no_abbr_cnt -= len;
1275 }
1276
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001277#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar30613902019-12-01 22:11:18 +01001278 // Reset the flag that text received from a client or from feedkeys()
1279 // was inserted in the typeahead buffer.
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001280 typebuf_was_filled = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281#endif
1282 if (++typebuf.tb_change_cnt == 0)
1283 typebuf.tb_change_cnt = 1;
1284}
1285
1286/*
1287 * Write typed characters to script file.
1288 * If recording is on put the character in the recordbuffer.
1289 */
1290 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001291gotchars(char_u *chars, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292{
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001293 char_u *s = chars;
1294 int i;
1295 static char_u buf[4];
1296 static int buflen = 0;
1297 int todo = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298
Bram Moolenaar4e86cba2007-05-06 11:56:32 +00001299 while (todo--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 {
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001301 buf[buflen++] = *s++;
1302
1303 // When receiving a special key sequence, store it until we have all
1304 // the bytes and we can decide what to do with it.
1305 if (buflen == 1 && buf[0] == K_SPECIAL)
1306 continue;
1307 if (buflen == 2)
1308 continue;
1309 if (buflen == 3 && buf[1] == KS_EXTRA
1310 && (buf[2] == KE_FOCUSGAINED || buf[2] == KE_FOCUSLOST))
1311 {
1312 // Drop K_FOCUSGAINED and K_FOCUSLOST, they are not useful in a
1313 // recording.
1314 buflen = 0;
1315 continue;
1316 }
1317
Bram Moolenaar30613902019-12-01 22:11:18 +01001318 // Handle one byte at a time; no translation to be done.
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001319 for (i = 0; i < buflen; ++i)
1320 updatescript(buf[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001322 if (reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 {
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001324 buf[buflen] = NUL;
1325 add_buff(&recordbuff, buf, (long)buflen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001326 // remember how many chars were last recorded
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001327 last_recorded_len += buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 }
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001329 buflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 }
1331 may_sync_undo();
1332
1333#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01001334 // output "debug mode" message next time in debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 debug_did_msg = FALSE;
1336#endif
1337
Bram Moolenaar30613902019-12-01 22:11:18 +01001338 // Since characters have been typed, consider the following to be in
1339 // another mapping. Search string will be kept in history.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 ++maptick;
1341}
1342
1343/*
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001344 * Undo the last gotchars() for "len" bytes. To be used when putting a typed
1345 * character back into the typeahead buffer, thus gotchars() will be called
1346 * again.
1347 * Only affects recorded characters.
1348 */
1349 void
1350ungetchars(int len)
1351{
1352 if (reg_recording != 0)
1353 {
1354 delete_buff_tail(&recordbuff, len);
1355 last_recorded_len -= len;
1356 }
1357}
1358
1359/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 * Sync undo. Called when typed characters are obtained from the typeahead
1361 * buffer, or when a menu is used.
1362 * Do not sync:
1363 * - In Insert mode, unless cursor key has been used.
1364 * - While reading a script file.
1365 * - When no_u_sync is non-zero.
1366 */
1367 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001368may_sync_undo(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369{
1370 if ((!(State & (INSERT + CMDLINE)) || arrow_used)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001371 && scriptin[curscript] == NULL)
1372 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373}
1374
1375/*
1376 * Make "typebuf" empty and allocate new buffers.
1377 * Returns FAIL when out of memory.
1378 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001379 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001380alloc_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381{
1382 typebuf.tb_buf = alloc(TYPELEN_INIT);
1383 typebuf.tb_noremap = alloc(TYPELEN_INIT);
1384 if (typebuf.tb_buf == NULL || typebuf.tb_noremap == NULL)
1385 {
1386 free_typebuf();
1387 return FAIL;
1388 }
1389 typebuf.tb_buflen = TYPELEN_INIT;
Bram Moolenaar30613902019-12-01 22:11:18 +01001390 typebuf.tb_off = MAXMAPLEN + 4; // can insert without realloc
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 typebuf.tb_len = 0;
1392 typebuf.tb_maplen = 0;
1393 typebuf.tb_silent = 0;
1394 typebuf.tb_no_abbr_cnt = 0;
1395 if (++typebuf.tb_change_cnt == 0)
1396 typebuf.tb_change_cnt = 1;
Bram Moolenaare49b4bb2020-03-11 13:01:40 +01001397#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
1398 typebuf_was_filled = FALSE;
1399#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 return OK;
1401}
1402
1403/*
1404 * Free the buffers of "typebuf".
1405 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001406 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001407free_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408{
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001409 if (typebuf.tb_buf == typebuf_init)
Bram Moolenaar95f09602016-11-10 20:01:45 +01001410 internal_error("Free typebuf 1");
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001411 else
Bram Moolenaar0f1c6702019-09-28 15:24:00 +02001412 VIM_CLEAR(typebuf.tb_buf);
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00001413 if (typebuf.tb_noremap == noremapbuf_init)
Bram Moolenaar95f09602016-11-10 20:01:45 +01001414 internal_error("Free typebuf 2");
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001415 else
Bram Moolenaar0f1c6702019-09-28 15:24:00 +02001416 VIM_CLEAR(typebuf.tb_noremap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417}
1418
1419/*
1420 * When doing ":so! file", the current typeahead needs to be saved, and
1421 * restored when "file" has been read completely.
1422 */
1423static typebuf_T saved_typebuf[NSCRIPT];
1424
1425 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001426save_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427{
1428 init_typebuf();
1429 saved_typebuf[curscript] = typebuf;
Bram Moolenaar30613902019-12-01 22:11:18 +01001430 // If out of memory: restore typebuf and close file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 if (alloc_typebuf() == FAIL)
1432 {
1433 closescript();
1434 return FAIL;
1435 }
1436 return OK;
1437}
1438
Bram Moolenaar30613902019-12-01 22:11:18 +01001439static int old_char = -1; // character put back by vungetc()
1440static int old_mod_mask; // mod_mask for ungotten character
1441static int old_mouse_row; // mouse_row related to old_char
1442static int old_mouse_col; // mouse_col related to old_char
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001443
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444/*
1445 * Save all three kinds of typeahead, so that the user must type at a prompt.
1446 */
1447 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001448save_typeahead(tasave_T *tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449{
1450 tp->save_typebuf = typebuf;
1451 tp->typebuf_valid = (alloc_typebuf() == OK);
1452 if (!tp->typebuf_valid)
1453 typebuf = tp->save_typebuf;
1454
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001455 tp->old_char = old_char;
1456 tp->old_mod_mask = old_mod_mask;
1457 old_char = -1;
1458
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001459 tp->save_readbuf1 = readbuf1;
Bram Moolenaar285e3352018-04-18 23:01:13 +02001460 readbuf1.bh_first.b_next = NULL;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001461 tp->save_readbuf2 = readbuf2;
Bram Moolenaar285e3352018-04-18 23:01:13 +02001462 readbuf2.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463# ifdef USE_INPUT_BUF
1464 tp->save_inputbuf = get_input_buf();
1465# endif
1466}
1467
1468/*
1469 * Restore the typeahead to what it was before calling save_typeahead().
1470 * The allocated memory is freed, can only be called once!
Bram Moolenaarc41badb2021-06-07 22:04:52 +02001471 * When "overwrite" is FALSE input typed later is kept.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 */
1473 void
Bram Moolenaarc41badb2021-06-07 22:04:52 +02001474restore_typeahead(tasave_T *tp, int overwrite UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475{
1476 if (tp->typebuf_valid)
1477 {
1478 free_typebuf();
1479 typebuf = tp->save_typebuf;
1480 }
1481
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001482 old_char = tp->old_char;
1483 old_mod_mask = tp->old_mod_mask;
1484
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001485 free_buff(&readbuf1);
1486 readbuf1 = tp->save_readbuf1;
1487 free_buff(&readbuf2);
1488 readbuf2 = tp->save_readbuf2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489# ifdef USE_INPUT_BUF
Bram Moolenaarc41badb2021-06-07 22:04:52 +02001490 set_input_buf(tp->save_inputbuf, overwrite);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491# endif
1492}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493
1494/*
1495 * Open a new script file for the ":source!" command.
1496 */
1497 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001498openscript(
1499 char_u *name,
Bram Moolenaar30613902019-12-01 22:11:18 +01001500 int directly) // when TRUE execute directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501{
1502 if (curscript + 1 == NSCRIPT)
1503 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001504 emsg(_(e_scripts_nested_too_deep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 return;
1506 }
Bram Moolenaar53575522019-05-22 22:38:25 +02001507
1508 // Disallow sourcing a file in the sandbox, the commands would be executed
1509 // later, possibly outside of the sandbox.
1510 if (check_secure())
1511 return;
1512
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001513#ifdef FEAT_EVAL
1514 if (ignore_script)
Bram Moolenaar30613902019-12-01 22:11:18 +01001515 // Not reading from script, also don't open one. Warning message?
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001516 return;
1517#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518
Bram Moolenaar30613902019-12-01 22:11:18 +01001519 if (scriptin[curscript] != NULL) // already reading script
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 ++curscript;
Bram Moolenaar30613902019-12-01 22:11:18 +01001521 // use NameBuff for expanded name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 expand_env(name, NameBuff, MAXPATHL);
1523 if ((scriptin[curscript] = mch_fopen((char *)NameBuff, READBIN)) == NULL)
1524 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001525 semsg(_(e_cant_open_file_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 if (curscript)
1527 --curscript;
1528 return;
1529 }
1530 if (save_typebuf() == FAIL)
1531 return;
1532
1533 /*
1534 * Execute the commands from the file right now when using ":source!"
1535 * after ":global" or ":argdo" or in a loop. Also when another command
1536 * follows. This means the display won't be updated. Don't do this
1537 * always, "make test" would fail.
1538 */
1539 if (directly)
1540 {
1541 oparg_T oa;
1542 int oldcurscript;
1543 int save_State = State;
1544 int save_restart_edit = restart_edit;
1545 int save_insertmode = p_im;
1546 int save_finish_op = finish_op;
1547 int save_msg_scroll = msg_scroll;
1548
1549 State = NORMAL;
Bram Moolenaar30613902019-12-01 22:11:18 +01001550 msg_scroll = FALSE; // no msg scrolling in Normal mode
1551 restart_edit = 0; // don't go to Insert mode
1552 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 clear_oparg(&oa);
1554 finish_op = FALSE;
1555
1556 oldcurscript = curscript;
1557 do
1558 {
Bram Moolenaar386b43e2019-05-19 21:57:11 +02001559 update_topline_cursor(); // update cursor position and topline
1560 normal_cmd(&oa, FALSE); // execute one command
Bram Moolenaarae97b942020-07-09 19:16:35 +02001561 (void)vpeekc(); // check for end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 }
1563 while (scriptin[oldcurscript] != NULL);
1564
1565 State = save_State;
1566 msg_scroll = save_msg_scroll;
1567 restart_edit = save_restart_edit;
1568 p_im = save_insertmode;
1569 finish_op = save_finish_op;
1570 }
1571}
1572
1573/*
1574 * Close the currently active input script.
1575 */
1576 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001577closescript(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578{
1579 free_typebuf();
1580 typebuf = saved_typebuf[curscript];
1581
1582 fclose(scriptin[curscript]);
1583 scriptin[curscript] = NULL;
1584 if (curscript > 0)
1585 --curscript;
1586}
1587
Bram Moolenaarea408852005-06-25 22:49:46 +00001588#if defined(EXITFREE) || defined(PROTO)
1589 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001590close_all_scripts(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00001591{
1592 while (scriptin[0] != NULL)
1593 closescript();
1594}
1595#endif
1596
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597/*
1598 * Return TRUE when reading keys from a script file.
1599 */
1600 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001601using_script(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602{
1603 return scriptin[curscript] != NULL;
1604}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605
1606/*
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001607 * This function is called just before doing a blocking wait. Thus after
1608 * waiting 'updatetime' for a character to arrive.
1609 */
1610 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001611before_blocking(void)
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001612{
1613 updatescript(0);
1614#ifdef FEAT_EVAL
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001615 if (may_garbage_collect)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001616 garbage_collect(FALSE);
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001617#endif
1618}
1619
1620/*
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001621 * updatescript() is called when a character can be written into the script file
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 * or when we have waited some time for a character (c == 0)
1623 *
1624 * All the changed memfiles are synced if c == 0 or when the number of typed
1625 * characters reaches 'updatecount' and 'updatecount' is non-zero.
1626 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001627 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001628updatescript(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629{
1630 static int count = 0;
1631
1632 if (c && scriptout)
1633 putc(c, scriptout);
1634 if (c == 0 || (p_uc > 0 && ++count >= p_uc))
1635 {
1636 ml_sync_all(c == 0, TRUE);
1637 count = 0;
1638 }
1639}
1640
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641/*
Bram Moolenaar975a8802020-06-06 22:36:24 +02001642 * Convert "c" plus "modifiers" to merge the effect of modifyOtherKeys into the
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001643 * character.
1644 */
1645 int
Bram Moolenaar975a8802020-06-06 22:36:24 +02001646merge_modifyOtherKeys(int c_arg, int *modifiers)
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001647{
1648 int c = c_arg;
1649
Bram Moolenaar975a8802020-06-06 22:36:24 +02001650 if (*modifiers & MOD_MASK_CTRL)
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001651 {
1652 if ((c >= '`' && c <= 0x7f) || (c >= '@' && c <= '_'))
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001653 c &= 0x1f;
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001654 else if (c == '6')
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001655 // CTRL-6 is equivalent to CTRL-^
1656 c = 0x1e;
Bram Moolenaarf4ae6b22020-05-30 19:52:46 +02001657#ifdef FEAT_GUI_GTK
1658 // These mappings look arbitrary at the first glance, but in fact
1659 // resemble quite exactly the behaviour of the GTK+ 1.2 GUI on my
1660 // machine. The only difference is BS vs. DEL for CTRL-8 (makes
1661 // more sense and is consistent with usual terminal behaviour).
1662 else if (c == '2')
1663 c = NUL;
1664 else if (c >= '3' && c <= '7')
1665 c = c ^ 0x28;
1666 else if (c == '8')
1667 c = BS;
1668 else if (c == '?')
1669 c = DEL;
1670#endif
1671 if (c != c_arg)
Bram Moolenaar975a8802020-06-06 22:36:24 +02001672 *modifiers &= ~MOD_MASK_CTRL;
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001673 }
Bram Moolenaar975a8802020-06-06 22:36:24 +02001674 if ((*modifiers & (MOD_MASK_META | MOD_MASK_ALT))
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001675 && c >= 0 && c <= 127)
1676 {
1677 c += 0x80;
Bram Moolenaar975a8802020-06-06 22:36:24 +02001678 *modifiers &= ~(MOD_MASK_META|MOD_MASK_ALT);
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001679 }
1680 return c;
1681}
1682
1683/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 * Get the next input character.
1685 * Can return a special key or a multi-byte character.
1686 * Can return NUL when called recursively, use safe_vgetc() if that's not
1687 * wanted.
1688 * This translates escaped K_SPECIAL and CSI bytes to a K_SPECIAL or CSI byte.
1689 * Collects the bytes of a multibyte character into the whole character.
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00001690 * Returns the modifiers in the global "mod_mask".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 */
1692 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001693vgetc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694{
1695 int c, c2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001697 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001700#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01001701 // Do garbage collection when garbagecollect() was called previously and
1702 // we are now at the toplevel.
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001703 if (may_garbage_collect && want_garbage_collect)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001704 garbage_collect(FALSE);
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001705#endif
1706
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 /*
1708 * If a character was put back with vungetc, it was already processed.
1709 * Return it directly.
1710 */
1711 if (old_char != -1)
1712 {
1713 c = old_char;
1714 old_char = -1;
1715 mod_mask = old_mod_mask;
Bram Moolenaarb8978712013-03-16 21:42:16 +01001716 mouse_row = old_mouse_row;
1717 mouse_col = old_mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001719 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 {
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001721 mod_mask = 0;
1722 vgetc_mod_mask = 0;
1723 vgetc_char = 0;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001724 last_recorded_len = 0;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001725
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001726 for (;;) // this is done twice if there are modifiers
1727 {
1728 int did_inc = FALSE;
Bram Moolenaar2455c4e2015-09-15 18:29:39 +02001729
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001730 if (mod_mask
Bram Moolenaar39719052017-09-05 22:20:46 +02001731#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001732 || im_is_preediting()
Bram Moolenaar39719052017-09-05 22:20:46 +02001733#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001734#if defined(FEAT_PROP_POPUP)
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001735 || popup_no_mapping()
1736#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001737 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 {
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001739 // no mapping after modifier has been read
1740 ++no_mapping;
1741 ++allow_keys;
1742 did_inc = TRUE; // mod_mask may change value
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 }
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001744 c = vgetorpeek(TRUE);
1745 if (did_inc)
1746 {
1747 --no_mapping;
1748 --allow_keys;
1749 }
1750
1751 // Get two extra bytes for special keys
1752 if (c == K_SPECIAL
1753#ifdef FEAT_GUI
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001754 || (c == CSI)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001755#endif
1756 )
1757 {
1758 int save_allow_keys = allow_keys;
1759
1760 ++no_mapping;
1761 allow_keys = 0; // make sure BS is not found
1762 c2 = vgetorpeek(TRUE); // no mapping for these chars
1763 c = vgetorpeek(TRUE);
1764 --no_mapping;
1765 allow_keys = save_allow_keys;
1766 if (c2 == KS_MODIFIER)
1767 {
1768 mod_mask = c;
1769 continue;
1770 }
1771 c = TO_SPECIAL(c2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772
Bram Moolenaar4f974752019-02-17 17:44:42 +01001773#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001774 // Handle K_TEAROFF here, the caller of vgetc() doesn't need to
1775 // know that a menu was torn off
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001776 if (
1777# ifdef VIMDLL
1778 gui.in_use &&
1779# endif
1780 c == K_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 {
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001782 char_u name[200];
1783 int i;
1784
1785 // get menu path, it ends with a <CR>
1786 for (i = 0; (c = vgetorpeek(TRUE)) != '\r'; )
1787 {
1788 name[i] = c;
1789 if (i < 199)
1790 ++i;
1791 }
1792 name[i] = NUL;
1793 gui_make_tearoff(name);
1794 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796#endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001797#if defined(FEAT_GUI) && defined(FEAT_GUI_GTK) && defined(FEAT_MENU)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001798 // GTK: <F10> normally selects the menu, but it's passed until
1799 // here to allow mapping it. Intercept and invoke the GTK
1800 // behavior if it's not mapped.
1801 if (c == K_F10 && gui.menubar != NULL)
1802 {
1803 gtk_menu_shell_select_first(
1804 GTK_MENU_SHELL(gui.menubar), FALSE);
1805 continue;
1806 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001807#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808#ifdef FEAT_GUI
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001809 // Handle focus event here, so that the caller doesn't need to
1810 // know about it. Return K_IGNORE so that we loop once (needed
1811 // if 'lazyredraw' is set).
1812 if (c == K_FOCUSGAINED || c == K_FOCUSLOST)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001813 {
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001814 ui_focus_change(c == K_FOCUSGAINED);
1815 c = K_IGNORE;
Bram Moolenaara9dd2d32019-04-29 21:58:41 +02001816 }
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001817
1818 // Translate K_CSI to CSI. The special key is only used to
1819 // avoid it being recognized as the start of a special key.
1820 if (c == K_CSI)
1821 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001823 }
1824 // a keypad or special function key was not mapped, use it like
1825 // its ASCII equivalent
1826 switch (c)
1827 {
1828 case K_KPLUS: c = '+'; break;
1829 case K_KMINUS: c = '-'; break;
1830 case K_KDIVIDE: c = '/'; break;
1831 case K_KMULTIPLY: c = '*'; break;
1832 case K_KENTER: c = CAR; break;
1833 case K_KPOINT:
Bram Moolenaar4f974752019-02-17 17:44:42 +01001834#ifdef MSWIN
Bram Moolenaar8e85db02018-07-27 23:16:51 +02001835 // Can be either '.' or a ',',
1836 // depending on the type of keypad.
1837 c = MapVirtualKey(VK_DECIMAL, 2); break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001838#else
Bram Moolenaar8e85db02018-07-27 23:16:51 +02001839 c = '.'; break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001840#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001841 case K_K0: c = '0'; break;
1842 case K_K1: c = '1'; break;
1843 case K_K2: c = '2'; break;
1844 case K_K3: c = '3'; break;
1845 case K_K4: c = '4'; break;
1846 case K_K5: c = '5'; break;
1847 case K_K6: c = '6'; break;
1848 case K_K7: c = '7'; break;
1849 case K_K8: c = '8'; break;
1850 case K_K9: c = '9'; break;
Bram Moolenaara88d9682005-03-25 21:45:43 +00001851
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001852 case K_XHOME:
1853 case K_ZHOME: if (mod_mask == MOD_MASK_SHIFT)
Bram Moolenaara88d9682005-03-25 21:45:43 +00001854 {
1855 c = K_S_HOME;
1856 mod_mask = 0;
1857 }
1858 else if (mod_mask == MOD_MASK_CTRL)
1859 {
1860 c = K_C_HOME;
1861 mod_mask = 0;
1862 }
1863 else
1864 c = K_HOME;
1865 break;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001866 case K_XEND:
1867 case K_ZEND: if (mod_mask == MOD_MASK_SHIFT)
Bram Moolenaara88d9682005-03-25 21:45:43 +00001868 {
1869 c = K_S_END;
1870 mod_mask = 0;
1871 }
1872 else if (mod_mask == MOD_MASK_CTRL)
1873 {
1874 c = K_C_END;
1875 mod_mask = 0;
1876 }
1877 else
1878 c = K_END;
1879 break;
1880
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001881 case K_XUP: c = K_UP; break;
1882 case K_XDOWN: c = K_DOWN; break;
1883 case K_XLEFT: c = K_LEFT; break;
1884 case K_XRIGHT: c = K_RIGHT; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001887 // For a multi-byte character get all the bytes and return the
1888 // converted character.
1889 // Note: This will loop until enough bytes are received!
1890 if (has_mbyte && (n = MB_BYTE2LEN_CHECK(c)) > 1)
1891 {
1892 ++no_mapping;
1893 buf[0] = c;
1894 for (i = 1; i < n; ++i)
1895 {
1896 buf[i] = vgetorpeek(TRUE);
1897 if (buf[i] == K_SPECIAL
1898#ifdef FEAT_GUI
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001899 || (buf[i] == CSI)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001900#endif
1901 )
1902 {
1903 // Must be a K_SPECIAL - KS_SPECIAL - KE_FILLER
1904 // sequence, which represents a K_SPECIAL (0x80),
1905 // or a CSI - KS_EXTRA - KE_CSI sequence, which
1906 // represents a CSI (0x9B),
1907 // or a K_SPECIAL - KS_EXTRA - KE_CSI, which is CSI
1908 // too.
1909 c = vgetorpeek(TRUE);
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01001910 if (vgetorpeek(TRUE) == KE_CSI && c == KS_EXTRA)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001911 buf[i] = CSI;
1912 }
1913 }
1914 --no_mapping;
1915 c = (*mb_ptr2char)(buf);
1916 }
1917
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02001918 if (vgetc_char == 0)
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001919 {
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001920 vgetc_mod_mask = mod_mask;
1921 vgetc_char = c;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001922 }
Bram Moolenaar00eab7f2019-10-10 21:49:28 +02001923
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001924 break;
1925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001927
1928#ifdef FEAT_EVAL
1929 /*
1930 * In the main loop "may_garbage_collect" can be set to do garbage
1931 * collection in the first next vgetc(). It's disabled after that to
1932 * avoid internally used Lists and Dicts to be freed.
1933 */
1934 may_garbage_collect = FALSE;
1935#endif
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001936
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001937#ifdef FEAT_BEVAL_TERM
Bram Moolenaarc2f50542019-07-05 23:24:56 +02001938 if (c != K_MOUSEMOVE && c != K_IGNORE && c != K_CURSORHOLD)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001939 {
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001940 // Don't trigger 'balloonexpr' unless only the mouse was moved.
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001941 bevalexpr_due_set = FALSE;
1942 ui_remove_balloon();
1943 }
1944#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001945#ifdef FEAT_PROP_POPUP
Bram Moolenaar4934ad02020-09-28 22:29:58 +02001946 // Only filter keys that do not come from ":normal". Keys from feedkeys()
1947 // are filtered.
1948 if ((!ex_normal_busy || in_feedkeys) && popup_do_filter(c))
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02001949 {
1950 if (c == Ctrl_C)
1951 got_int = FALSE; // avoid looping
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001952 c = K_IGNORE;
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02001953 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001954#endif
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001955
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001956 // Need to process the character before we know it's safe to do something
1957 // else.
1958 if (c != K_IGNORE)
Bram Moolenaard103ee72019-09-18 21:15:31 +02001959 state_no_longer_safe("key typed");
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001960
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001961 return c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962}
1963
1964/*
1965 * Like vgetc(), but never return a NUL when called recursively, get a key
1966 * directly from the user (ignoring typeahead).
1967 */
1968 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001969safe_vgetc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970{
1971 int c;
1972
1973 c = vgetc();
1974 if (c == NUL)
1975 c = get_keystroke();
1976 return c;
1977}
1978
1979/*
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001980 * Like safe_vgetc(), but loop to handle K_IGNORE.
1981 * Also ignore scrollbar events.
1982 */
1983 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001984plain_vgetc(void)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001985{
1986 int c;
1987
1988 do
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001989 c = safe_vgetc();
Bram Moolenaar3a006592021-05-05 19:58:17 +02001990 while (c == K_IGNORE
1991 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
1992 || c == K_MOUSEMOVE);
Bram Moolenaarec2da362017-01-21 20:04:22 +01001993
1994 if (c == K_PS)
Bram Moolenaar30613902019-12-01 22:11:18 +01001995 // Only handle the first pasted character. Drop the rest, since we
1996 // don't know what to do with it.
Bram Moolenaarec2da362017-01-21 20:04:22 +01001997 c = bracketed_paste(PASTE_ONE_CHAR, FALSE, NULL);
1998
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001999 return c;
2000}
2001
2002/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 * Check if a character is available, such that vgetc() will not block.
2004 * If the next character is a special character or multi-byte, the returned
2005 * character is not valid!.
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02002006 * Returns NUL if no character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 */
2008 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002009vpeekc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010{
2011 if (old_char != -1)
2012 return old_char;
2013 return vgetorpeek(FALSE);
2014}
2015
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01002016#if defined(FEAT_TERMRESPONSE) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017/*
2018 * Like vpeekc(), but don't allow mapping. Do allow checking for terminal
2019 * codes.
2020 */
2021 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002022vpeekc_nomap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023{
2024 int c;
2025
2026 ++no_mapping;
2027 ++allow_keys;
2028 c = vpeekc();
2029 --no_mapping;
2030 --allow_keys;
2031 return c;
2032}
2033#endif
2034
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035/*
2036 * Check if any character is available, also half an escape sequence.
2037 * Trick: when no typeahead found, but there is something in the typeahead
2038 * buffer, it must be an ESC that is recognized as the start of a key code.
2039 */
2040 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002041vpeekc_any(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042{
2043 int c;
2044
2045 c = vpeekc();
2046 if (c == NUL && typebuf.tb_len > 0)
2047 c = ESC;
2048 return c;
2049}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050
2051/*
2052 * Call vpeekc() without causing anything to be mapped.
2053 * Return TRUE if a character is available, FALSE otherwise.
2054 */
2055 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002056char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057{
2058 int retval;
2059
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01002060#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01002061 // When test_override("char_avail", 1) was called pretend there is no
2062 // typeahead.
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01002063 if (disable_char_avail_for_testing)
2064 return FALSE;
2065#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 ++no_mapping;
2067 retval = vpeekc();
2068 --no_mapping;
2069 return (retval != NUL);
2070}
2071
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002072#if defined(FEAT_EVAL) || defined(PROTO)
2073/*
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002074 * "getchar()" and "getcharstr()" functions
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002075 */
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002076 static void
2077getchar_common(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002078{
2079 varnumber_T n;
2080 int error = FALSE;
2081
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002082 if (in_vim9script() && check_for_opt_bool_arg(argvars, 0) == FAIL)
2083 return;
2084
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002085#ifdef MESSAGE_QUEUE
2086 // vpeekc() used to check for messages, but that caused problems, invoking
2087 // a callback where it was not expected. Some plugins use getchar(1) in a
2088 // loop to await a message, therefore make sure we check for messages here.
2089 parse_queued_messages();
2090#endif
2091
Bram Moolenaar30613902019-12-01 22:11:18 +01002092 // Position the cursor. Needed after a message that ends in a space.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002093 windgoto(msg_row, msg_col);
2094
2095 ++no_mapping;
2096 ++allow_keys;
2097 for (;;)
2098 {
2099 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar30613902019-12-01 22:11:18 +01002100 // getchar(): blocking wait.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002101 n = plain_vgetc();
Bram Moolenaarc08cc722020-09-05 17:51:23 +02002102 else if (tv_get_bool_chk(&argvars[0], &error))
Bram Moolenaar30613902019-12-01 22:11:18 +01002103 // getchar(1): only check if char avail
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002104 n = vpeekc_any();
2105 else if (error || vpeekc_any() == NUL)
Bram Moolenaar30613902019-12-01 22:11:18 +01002106 // illegal argument or getchar(0) and no char avail: return zero
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002107 n = 0;
2108 else
Bram Moolenaar15183b42020-09-05 19:59:39 +02002109 // getchar(0) and char avail() != NUL: get a character.
2110 // Note that vpeekc_any() returns K_SPECIAL for K_IGNORE.
2111 n = safe_vgetc();
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002112
Bram Moolenaar15183b42020-09-05 19:59:39 +02002113 if (n == K_IGNORE || n == K_MOUSEMOVE
2114 || n == K_VER_SCROLLBAR || n == K_HOR_SCROLLBAR)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002115 continue;
2116 break;
2117 }
2118 --no_mapping;
2119 --allow_keys;
2120
2121 set_vim_var_nr(VV_MOUSE_WIN, 0);
2122 set_vim_var_nr(VV_MOUSE_WINID, 0);
2123 set_vim_var_nr(VV_MOUSE_LNUM, 0);
2124 set_vim_var_nr(VV_MOUSE_COL, 0);
2125
2126 rettv->vval.v_number = n;
2127 if (IS_SPECIAL(n) || mod_mask != 0)
2128 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002129 char_u temp[10]; // modifier: 3, mbyte-char: 6, NUL: 1
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002130 int i = 0;
2131
Bram Moolenaar30613902019-12-01 22:11:18 +01002132 // Turn a special key into three bytes, plus modifier.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002133 if (mod_mask != 0)
2134 {
2135 temp[i++] = K_SPECIAL;
2136 temp[i++] = KS_MODIFIER;
2137 temp[i++] = mod_mask;
2138 }
2139 if (IS_SPECIAL(n))
2140 {
2141 temp[i++] = K_SPECIAL;
2142 temp[i++] = K_SECOND(n);
2143 temp[i++] = K_THIRD(n);
2144 }
2145 else if (has_mbyte)
2146 i += (*mb_char2bytes)(n, temp + i);
2147 else
2148 temp[i++] = n;
2149 temp[i++] = NUL;
2150 rettv->v_type = VAR_STRING;
2151 rettv->vval.v_string = vim_strsave(temp);
2152
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002153 if (is_mouse_key(n))
2154 {
2155 int row = mouse_row;
2156 int col = mouse_col;
2157 win_T *win;
2158 linenr_T lnum;
2159 win_T *wp;
2160 int winnr = 1;
2161
2162 if (row >= 0 && col >= 0)
2163 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002164 // Find the window at the mouse coordinates and compute the
2165 // text position.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002166 win = mouse_find_win(&row, &col, FIND_POPUP);
2167 if (win == NULL)
2168 return;
2169 (void)mouse_comp_pos(win, &row, &col, &lnum, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002170#ifdef FEAT_PROP_POPUP
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002171 if (WIN_IS_POPUP(win))
2172 winnr = 0;
2173 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002174#endif
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002175 for (wp = firstwin; wp != win && wp != NULL;
2176 wp = wp->w_next)
2177 ++winnr;
2178 set_vim_var_nr(VV_MOUSE_WIN, winnr);
2179 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
2180 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
2181 set_vim_var_nr(VV_MOUSE_COL, col + 1);
2182 }
2183 }
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002184 }
2185}
2186
2187/*
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002188 * "getchar()" function
2189 */
2190 void
2191f_getchar(typval_T *argvars, typval_T *rettv)
2192{
2193 getchar_common(argvars, rettv);
2194}
2195
2196/*
2197 * "getcharstr()" function
2198 */
2199 void
2200f_getcharstr(typval_T *argvars, typval_T *rettv)
2201{
2202 getchar_common(argvars, rettv);
2203
2204 if (rettv->v_type == VAR_NUMBER)
2205 {
2206 char_u temp[7]; // mbyte-char: 6, NUL: 1
2207 varnumber_T n = rettv->vval.v_number;
2208 int i = 0;
2209
2210 if (n != 0)
2211 {
2212 if (has_mbyte)
2213 i += (*mb_char2bytes)(n, temp + i);
2214 else
2215 temp[i++] = n;
2216 }
2217 temp[i++] = NUL;
2218 rettv->v_type = VAR_STRING;
2219 rettv->vval.v_string = vim_strsave(temp);
2220 }
2221}
2222
2223/*
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002224 * "getcharmod()" function
2225 */
2226 void
2227f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
2228{
2229 rettv->vval.v_number = mod_mask;
2230}
2231#endif // FEAT_EVAL
2232
2233#if defined(MESSAGE_QUEUE) || defined(PROTO)
2234# define MAX_REPEAT_PARSE 8
2235
2236/*
2237 * Process messages that have been queued for netbeans or clientserver.
2238 * Also check if any jobs have ended.
h-east965d2ed2021-10-04 21:51:57 +01002239 * These functions can call arbitrary Vim script and should only be called when
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002240 * it is safe to do so.
2241 */
2242 void
2243parse_queued_messages(void)
2244{
Bram Moolenaar1cac7092019-10-22 21:54:31 +02002245 int old_curwin_id;
2246 int old_curbuf_fnum;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002247 int i;
2248 int save_may_garbage_collect = may_garbage_collect;
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002249 static int entered = 0;
Bram Moolenaard103ee72019-09-18 21:15:31 +02002250 int was_safe = get_was_safe_state();
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002251
2252 // Do not handle messages while redrawing, because it may cause buffers to
2253 // change or be wiped while they are being redrawn.
Bram Moolenaar4778b4d2020-11-04 11:03:12 +01002254 // Also bail out when parsing messages was explicitly disabled.
2255 if (updating_screen || dont_parse_messages)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002256 return;
2257
Bram Moolenaar1cac7092019-10-22 21:54:31 +02002258 // If memory allocation fails during startup we'll exit but curbuf or
2259 // curwin could be NULL.
2260 if (curbuf == NULL || curwin == NULL)
2261 return;
2262
2263 old_curbuf_fnum = curbuf->b_fnum;
2264 old_curwin_id = curwin->w_id;
2265
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002266 ++entered;
2267
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002268 // may_garbage_collect is set in main_loop() to do garbage collection when
2269 // blocking to wait on a character. We don't want that while parsing
2270 // messages, a callback may invoke vgetc() while lists and dicts are in use
2271 // in the call stack.
2272 may_garbage_collect = FALSE;
2273
2274 // Loop when a job ended, but don't keep looping forever.
2275 for (i = 0; i < MAX_REPEAT_PARSE; ++i)
2276 {
2277 // For Win32 mch_breakcheck() does not check for input, do it here.
Bram Moolenaar80a8d382020-05-03 22:57:32 +02002278# if (defined(MSWIN) || defined(__HAIKU__)) && defined(FEAT_JOB_CHANNEL)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002279 channel_handle_events(FALSE);
2280# endif
2281
2282# ifdef FEAT_NETBEANS_INTG
2283 // Process the queued netbeans messages.
2284 netbeans_parse_messages();
2285# endif
2286# ifdef FEAT_JOB_CHANNEL
2287 // Write any buffer lines still to be written.
2288 channel_write_any_lines();
2289
2290 // Process the messages queued on channels.
2291 channel_parse_messages();
2292# endif
2293# if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
2294 // Process the queued clientserver messages.
2295 server_parse_messages();
2296# endif
2297# ifdef FEAT_JOB_CHANNEL
2298 // Check if any jobs have ended. If so, repeat the above to handle
2299 // changes, e.g. stdin may have been closed.
2300 if (job_check_ended())
2301 continue;
2302# endif
2303# ifdef FEAT_TERMINAL
2304 free_unused_terminals();
2305# endif
2306# ifdef FEAT_SOUND_CANBERRA
2307 if (has_sound_callback_in_queue())
2308 invoke_sound_callback();
2309# endif
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002310#ifdef SIGUSR1
2311 if (got_sigusr1)
2312 {
2313 apply_autocmds(EVENT_SIGUSR1, NULL, NULL, FALSE, curbuf);
2314 got_sigusr1 = FALSE;
2315 }
2316#endif
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002317 break;
2318 }
2319
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002320 // When not nested we'll go back to waiting for a typed character. If it
2321 // was safe before then this triggers a SafeStateAgain autocommand event.
Bram Moolenaard103ee72019-09-18 21:15:31 +02002322 if (entered == 1 && was_safe)
Bram Moolenaar37d18072019-09-17 20:28:38 +02002323 may_trigger_safestateagain();
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002324
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002325 may_garbage_collect = save_may_garbage_collect;
2326
2327 // If the current window or buffer changed we need to bail out of the
2328 // waiting loop. E.g. when a job exit callback closes the terminal window.
2329 if (curwin->w_id != old_curwin_id || curbuf->b_fnum != old_curbuf_fnum)
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02002330 ins_char_typebuf(K_IGNORE, 0);
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002331
2332 --entered;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002333}
2334#endif
2335
2336
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002337typedef enum {
2338 map_result_fail, // failed, break loop
2339 map_result_get, // get a character from typeahead
2340 map_result_retry, // try to map again
2341 map_result_nomatch // no matching mapping, get char
2342} map_result_T;
2343
2344/*
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002345 * Check if the bytes at the start of the typeahead buffer are a character used
2346 * in CTRL-X mode. This includes the form with a CTRL modifier.
2347 */
2348 static int
2349at_ctrl_x_key(void)
2350{
2351 char_u *p = typebuf.tb_buf + typebuf.tb_off;
2352 int c = *p;
2353
2354 if (typebuf.tb_len > 3
2355 && c == K_SPECIAL
2356 && p[1] == KS_MODIFIER
2357 && (p[2] & MOD_MASK_CTRL))
2358 c = p[3] & 0x1f;
2359 return vim_is_ctrl_x_key(c);
2360}
2361
2362/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002363 * Check if typebuf.tb_buf[] contains a modifier plus key that can be changed
Bram Moolenaar975a8802020-06-06 22:36:24 +02002364 * into just a key, apply that.
2365 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
2366 * + "max_offset"].
2367 * Return the length of the replaced bytes, zero if nothing changed.
2368 */
2369 static int
2370check_simplify_modifier(int max_offset)
2371{
2372 int offset;
2373 char_u *tp;
2374
2375 for (offset = 0; offset < max_offset; ++offset)
2376 {
2377 if (offset + 3 >= typebuf.tb_len)
2378 break;
2379 tp = typebuf.tb_buf + typebuf.tb_off + offset;
2380 if (tp[0] == K_SPECIAL && tp[1] == KS_MODIFIER)
2381 {
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002382 // A modifier was not used for a mapping, apply it to ASCII keys.
2383 // Shift would already have been applied.
Bram Moolenaar975a8802020-06-06 22:36:24 +02002384 int modifier = tp[2];
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002385 int c = tp[3];
2386 int new_c = merge_modifyOtherKeys(c, &modifier);
Bram Moolenaar975a8802020-06-06 22:36:24 +02002387
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002388 if (new_c != c)
Bram Moolenaar975a8802020-06-06 22:36:24 +02002389 {
2390 char_u new_string[MB_MAXBYTES];
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002391 int len;
Bram Moolenaar975a8802020-06-06 22:36:24 +02002392
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002393 if (offset == 0)
2394 {
2395 // At the start: remember the character and mod_mask before
2396 // merging, in some cases, e.g. at the hit-return prompt,
2397 // they are put back in the typeahead buffer.
2398 vgetc_char = c;
2399 vgetc_mod_mask = tp[2];
2400 }
2401 len = mb_char2bytes(new_c, new_string);
2402 if (modifier == 0)
2403 {
2404 if (put_string_in_typebuf(offset, 4, new_string, len,
Bram Moolenaar975a8802020-06-06 22:36:24 +02002405 NULL, 0, 0) == FAIL)
2406 return -1;
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002407 }
2408 else
2409 {
2410 tp[2] = modifier;
2411 if (put_string_in_typebuf(offset + 3, 1, new_string, len,
2412 NULL, 0, 0) == FAIL)
2413 return -1;
2414 }
Bram Moolenaar975a8802020-06-06 22:36:24 +02002415 return len;
2416 }
2417 }
2418 }
2419 return 0;
2420}
2421
2422/*
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002423 * Handle mappings in the typeahead buffer.
2424 * - When something was mapped, return map_result_retry for recursive mappings.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002425 * - When nothing mapped and typeahead has a character: return map_result_get.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002426 * - When there is no match yet, return map_result_nomatch, need to get more
2427 * typeahead.
2428 */
2429 static int
2430handle_mapping(
2431 int *keylenp,
2432 int *timedout,
2433 int *mapdepth)
2434{
2435 mapblock_T *mp = NULL;
2436 mapblock_T *mp2;
2437 mapblock_T *mp_match;
2438 int mp_match_len = 0;
2439 int max_mlen = 0;
2440 int tb_c1;
2441 int mlen;
2442#ifdef FEAT_LANGMAP
2443 int nolmaplen;
2444#endif
2445 int keylen = *keylenp;
2446 int i;
2447 int local_State = get_real_state();
2448
2449 /*
2450 * Check for a mappable key sequence.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002451 * Walk through one maphash[] list until we find an entry that matches.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002452 *
2453 * Don't look for mappings if:
2454 * - no_mapping set: mapping disabled (e.g. for CTRL-V)
2455 * - maphash_valid not set: no mappings present.
2456 * - typebuf.tb_buf[typebuf.tb_off] should not be remapped
2457 * - in insert or cmdline mode and 'paste' option set
Bram Moolenaareda35f72019-08-03 14:59:44 +02002458 * - waiting for "hit return to continue" and CR or SPACE typed
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002459 * - waiting for a char with --more--
2460 * - in Ctrl-X mode, and we get a valid char for that mode
2461 */
2462 tb_c1 = typebuf.tb_buf[typebuf.tb_off];
2463 if (no_mapping == 0 && is_maphash_valid()
2464 && (no_zero_mapping == 0 || tb_c1 != '0')
2465 && (typebuf.tb_maplen == 0
2466 || (p_remap
2467 && (typebuf.tb_noremap[typebuf.tb_off]
2468 & (RM_NONE|RM_ABBR)) == 0))
2469 && !(p_paste && (State & (INSERT + CMDLINE)))
2470 && !(State == HITRETURN && (tb_c1 == CAR || tb_c1 == ' '))
2471 && State != ASKMORE
2472 && State != CONFIRM
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002473 && !((ctrl_x_mode_not_default() && at_ctrl_x_key())
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002474 || (compl_status_local()
Bram Moolenaare2c453d2019-08-21 14:37:09 +02002475 && (tb_c1 == Ctrl_N || tb_c1 == Ctrl_P))))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002476 {
Bram Moolenaarc9983702020-05-28 21:03:53 +02002477#ifdef FEAT_GUI
2478 if (gui.in_use && tb_c1 == CSI && typebuf.tb_len >= 2
2479 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_MODIFIER)
2480 {
2481 // The GUI code sends CSI KS_MODIFIER {flags}, but mappings expect
2482 // K_SPECIAL KS_MODIFIER {flags}.
2483 tb_c1 = K_SPECIAL;
2484 }
2485#endif
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002486#ifdef FEAT_LANGMAP
2487 if (tb_c1 == K_SPECIAL)
2488 nolmaplen = 2;
2489 else
2490 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002491 LANGMAP_ADJUST(tb_c1, (State & (CMDLINE | INSERT)) == 0
2492 && get_real_state() != SELECTMODE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002493 nolmaplen = 0;
2494 }
2495#endif
2496 // First try buffer-local mappings.
2497 mp = get_buf_maphash_list(local_State, tb_c1);
2498 mp2 = get_maphash_list(local_State, tb_c1);
2499 if (mp == NULL)
2500 {
2501 // There are no buffer-local mappings.
2502 mp = mp2;
2503 mp2 = NULL;
2504 }
Bram Moolenaareda35f72019-08-03 14:59:44 +02002505
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002506 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002507 * Loop until a partly matching mapping is found or all (local)
2508 * mappings have been checked.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002509 * The longest full match is remembered in "mp_match".
Bram Moolenaareda35f72019-08-03 14:59:44 +02002510 * A full match is only accepted if there is no partly match, so "aa"
2511 * and "aaa" can both be mapped.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002512 */
2513 mp_match = NULL;
2514 mp_match_len = 0;
2515 for ( ; mp != NULL;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002516 mp->m_next == NULL ? (mp = mp2, mp2 = NULL) : (mp = mp->m_next))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002517 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002518 // Only consider an entry if the first character matches and it is
2519 // for the current state.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002520 // Skip ":lmap" mappings if keys were mapped.
2521 if (mp->m_keys[0] == tb_c1
2522 && (mp->m_mode & local_State)
Bram Moolenaar46cd43b2020-06-04 22:22:11 +02002523 && !(mp->m_simplified && seenModifyOtherKeys
2524 && typebuf.tb_maplen == 0)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002525 && ((mp->m_mode & LANGMAP) == 0 || typebuf.tb_maplen == 0))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002526 {
2527#ifdef FEAT_LANGMAP
2528 int nomap = nolmaplen;
2529 int c2;
2530#endif
2531 // find the match length of this mapping
2532 for (mlen = 1; mlen < typebuf.tb_len; ++mlen)
2533 {
2534#ifdef FEAT_LANGMAP
2535 c2 = typebuf.tb_buf[typebuf.tb_off + mlen];
2536 if (nomap > 0)
2537 --nomap;
2538 else if (c2 == K_SPECIAL)
2539 nomap = 2;
2540 else
2541 LANGMAP_ADJUST(c2, TRUE);
2542 if (mp->m_keys[mlen] != c2)
2543#else
2544 if (mp->m_keys[mlen] !=
Bram Moolenaarc9983702020-05-28 21:03:53 +02002545 typebuf.tb_buf[typebuf.tb_off + mlen])
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002546#endif
2547 break;
2548 }
2549
Bram Moolenaareda35f72019-08-03 14:59:44 +02002550 // Don't allow mapping the first byte(s) of a multi-byte char.
2551 // Happens when mapping <M-a> and then changing 'encoding'.
2552 // Beware that 0x80 is escaped.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002553 {
2554 char_u *p1 = mp->m_keys;
2555 char_u *p2 = mb_unescape(&p1);
2556
2557 if (has_mbyte && p2 != NULL
Bram Moolenaar1614a142019-10-06 22:00:13 +02002558 && MB_BYTE2LEN(tb_c1) > mb_ptr2len(p2))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002559 mlen = 0;
2560 }
2561
2562 // Check an entry whether it matches.
2563 // - Full match: mlen == keylen
2564 // - Partly match: mlen == typebuf.tb_len
2565 keylen = mp->m_keylen;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002566 if (mlen == keylen || (mlen == typebuf.tb_len
2567 && typebuf.tb_len < keylen))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002568 {
2569 char_u *s;
2570 int n;
2571
Bram Moolenaareda35f72019-08-03 14:59:44 +02002572 // If only script-local mappings are allowed, check if the
2573 // mapping starts with K_SNR.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002574 s = typebuf.tb_noremap + typebuf.tb_off;
2575 if (*s == RM_SCRIPT
2576 && (mp->m_keys[0] != K_SPECIAL
2577 || mp->m_keys[1] != KS_EXTRA
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01002578 || mp->m_keys[2] != KE_SNR))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002579 continue;
2580
Bram Moolenaareda35f72019-08-03 14:59:44 +02002581 // If one of the typed keys cannot be remapped, skip the
2582 // entry.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002583 for (n = mlen; --n >= 0; )
2584 if (*s++ & (RM_NONE|RM_ABBR))
2585 break;
2586 if (n >= 0)
2587 continue;
2588
2589 if (keylen > typebuf.tb_len)
2590 {
2591 if (!*timedout && !(mp_match != NULL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002592 && mp_match->m_nowait))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002593 {
2594 // break at a partly match
2595 keylen = KEYLEN_PART_MAP;
2596 break;
2597 }
2598 }
2599 else if (keylen > mp_match_len)
2600 {
2601 // found a longer match
2602 mp_match = mp;
2603 mp_match_len = keylen;
2604 }
2605 }
2606 else
Bram Moolenaareda35f72019-08-03 14:59:44 +02002607 // No match; may have to check for termcode at next
2608 // character.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002609 if (max_mlen < mlen)
2610 max_mlen = mlen;
2611 }
2612 }
2613
Bram Moolenaareda35f72019-08-03 14:59:44 +02002614 // If no partly match found, use the longest full match.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002615 if (keylen != KEYLEN_PART_MAP)
2616 {
2617 mp = mp_match;
2618 keylen = mp_match_len;
2619 }
2620 }
2621
2622 /*
2623 * Check for match with 'pastetoggle'
2624 */
2625 if (*p_pt != NUL && mp == NULL && (State & (INSERT|NORMAL)))
2626 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002627 for (mlen = 0; mlen < typebuf.tb_len && p_pt[mlen]; ++mlen)
2628 if (p_pt[mlen] != typebuf.tb_buf[typebuf.tb_off + mlen])
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002629 break;
2630 if (p_pt[mlen] == NUL) // match
2631 {
2632 // write chars to script file(s)
2633 if (mlen > typebuf.tb_maplen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002634 gotchars(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_maplen,
2635 mlen - typebuf.tb_maplen);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002636
2637 del_typebuf(mlen, 0); // remove the chars
Bram Moolenaareda35f72019-08-03 14:59:44 +02002638 set_option_value((char_u *)"paste", (long)!p_paste, NULL, 0);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002639 if (!(State & INSERT))
2640 {
2641 msg_col = 0;
2642 msg_row = Rows - 1;
2643 msg_clr_eos(); // clear ruler
2644 }
2645 status_redraw_all();
2646 redraw_statuslines();
2647 showmode();
2648 setcursor();
2649 *keylenp = keylen;
2650 return map_result_retry;
2651 }
2652 // Need more chars for partly match.
2653 if (mlen == typebuf.tb_len)
2654 keylen = KEYLEN_PART_KEY;
2655 else if (max_mlen < mlen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002656 // no match, may have to check for termcode at next character
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002657 max_mlen = mlen + 1;
2658 }
2659
Bram Moolenaareda35f72019-08-03 14:59:44 +02002660 if ((mp == NULL || max_mlen >= mp_match_len) && keylen != KEYLEN_PART_MAP)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002661 {
2662 int save_keylen = keylen;
2663
2664 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002665 * When no matching mapping found or found a non-matching mapping that
2666 * matches at least what the matching mapping matched:
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002667 * Check if we have a terminal code, when:
Bram Moolenaareda35f72019-08-03 14:59:44 +02002668 * - mapping is allowed,
2669 * - keys have not been mapped,
2670 * - and not an ESC sequence, not in insert mode or p_ek is on,
2671 * - and when not timed out,
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002672 */
2673 if ((no_mapping == 0 || allow_keys != 0)
2674 && (typebuf.tb_maplen == 0
2675 || (p_remap && typebuf.tb_noremap[
Bram Moolenaareda35f72019-08-03 14:59:44 +02002676 typebuf.tb_off] == RM_YES))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002677 && !*timedout)
2678 {
Bram Moolenaar0684e362020-12-03 19:54:42 +01002679 keylen = check_termcode(max_mlen + 1, NULL, 0, NULL);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002680
Bram Moolenaar0684e362020-12-03 19:54:42 +01002681 // If no termcode matched but 'pastetoggle' matched partially
2682 // it's like an incomplete key sequence.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002683 if (keylen == 0 && save_keylen == KEYLEN_PART_KEY)
2684 keylen = KEYLEN_PART_KEY;
2685
Bram Moolenaar975a8802020-06-06 22:36:24 +02002686 // If no termcode matched, try to include the modifier into the
zeertzjqa3f83fe2021-11-22 12:47:39 +00002687 // key. This is for when modifyOtherKeys is working.
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002688 if (keylen == 0 && !no_reduce_keys)
Bram Moolenaar975a8802020-06-06 22:36:24 +02002689 keylen = check_simplify_modifier(max_mlen + 1);
2690
Bram Moolenaareda35f72019-08-03 14:59:44 +02002691 // When getting a partial match, but the last characters were not
2692 // typed, don't wait for a typed character to complete the
2693 // termcode. This helps a lot when a ":normal" command ends in an
2694 // ESC.
2695 if (keylen < 0 && typebuf.tb_len == typebuf.tb_maplen)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002696 keylen = 0;
2697 }
2698 else
2699 keylen = 0;
2700 if (keylen == 0) // no matching terminal code
2701 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002702#ifdef AMIGA
2703 // check for window bounds report
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002704 if (typebuf.tb_maplen == 0 && (typebuf.tb_buf[
Bram Moolenaareda35f72019-08-03 14:59:44 +02002705 typebuf.tb_off] & 0xff) == CSI)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002706 {
2707 char_u *s;
2708
2709 for (s = typebuf.tb_buf + typebuf.tb_off + 1;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002710 s < typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len
2711 && (VIM_ISDIGIT(*s) || *s == ';' || *s == ' ');
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002712 ++s)
2713 ;
2714 if (*s == 'r' || *s == '|') // found one
2715 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002716 del_typebuf(
2717 (int)(s + 1 - (typebuf.tb_buf + typebuf.tb_off)), 0);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002718 // get size and redraw screen
2719 shell_resized();
2720 *keylenp = keylen;
2721 return map_result_retry;
2722 }
2723 if (*s == NUL) // need more characters
2724 keylen = KEYLEN_PART_KEY;
2725 }
2726 if (keylen >= 0)
2727#endif
Bram Moolenaareda35f72019-08-03 14:59:44 +02002728 // When there was a matching mapping and no termcode could be
2729 // replaced after another one, use that mapping (loop around).
2730 // If there was no mapping at all use the character from the
2731 // typeahead buffer right here.
2732 if (mp == NULL)
2733 {
2734 *keylenp = keylen;
Bram Moolenaar52797ba2021-12-16 14:45:13 +00002735 return map_result_get; // get character from typeahead
Bram Moolenaareda35f72019-08-03 14:59:44 +02002736 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002737 }
2738
2739 if (keylen > 0) // full matching terminal code
2740 {
2741#if defined(FEAT_GUI) && defined(FEAT_MENU)
2742 if (typebuf.tb_len >= 2
Bram Moolenaareda35f72019-08-03 14:59:44 +02002743 && typebuf.tb_buf[typebuf.tb_off] == K_SPECIAL
2744 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_MENU)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002745 {
2746 int idx;
2747
Bram Moolenaareda35f72019-08-03 14:59:44 +02002748 // Using a menu may cause a break in undo! It's like using
2749 // gotchars(), but without recording or writing to a script
2750 // file.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002751 may_sync_undo();
2752 del_typebuf(3, 0);
2753 idx = get_menu_index(current_menu, local_State);
2754 if (idx != MENU_INDEX_INVALID)
2755 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002756 // In Select mode and a Visual mode menu is used: Switch
2757 // to Visual mode temporarily. Append K_SELECT to switch
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002758 // back to Select mode.
2759 if (VIsual_active && VIsual_select
Bram Moolenaareda35f72019-08-03 14:59:44 +02002760 && (current_menu->modes & VISUAL))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002761 {
2762 VIsual_select = FALSE;
2763 (void)ins_typebuf(K_SELECT_STRING,
Bram Moolenaareda35f72019-08-03 14:59:44 +02002764 REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002765 }
2766 ins_typebuf(current_menu->strings[idx],
2767 current_menu->noremap[idx],
Bram Moolenaareda35f72019-08-03 14:59:44 +02002768 0, TRUE, current_menu->silent[idx]);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002769 }
2770 }
2771#endif // FEAT_GUI && FEAT_MENU
2772 *keylenp = keylen;
2773 return map_result_retry; // try mapping again
2774 }
2775
Bram Moolenaareda35f72019-08-03 14:59:44 +02002776 // Partial match: get some more characters. When a matching mapping
2777 // was found use that one.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002778 if (mp == NULL || keylen < 0)
2779 keylen = KEYLEN_PART_KEY;
2780 else
2781 keylen = mp_match_len;
2782 }
2783
2784 /*
2785 * complete match
2786 */
2787 if (keylen >= 0 && keylen <= typebuf.tb_len)
2788 {
2789 char_u *map_str;
2790
2791#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002792 int save_m_expr;
2793 int save_m_noremap;
2794 int save_m_silent;
2795 char_u *save_m_keys;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002796#else
2797# define save_m_noremap mp->m_noremap
2798# define save_m_silent mp->m_silent
2799#endif
2800
2801 // write chars to script file(s)
2802 if (keylen > typebuf.tb_maplen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002803 gotchars(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_maplen,
2804 keylen - typebuf.tb_maplen);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002805
2806 cmd_silent = (typebuf.tb_silent > 0);
2807 del_typebuf(keylen, 0); // remove the mapped keys
2808
2809 /*
2810 * Put the replacement string in front of mapstr.
2811 * The depth check catches ":map x y" and ":map y x".
2812 */
2813 if (++*mapdepth >= p_mmd)
2814 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00002815 emsg(_(e_recursive_mapping));
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002816 if (State & CMDLINE)
2817 redrawcmdline();
2818 else
2819 setcursor();
2820 flush_buffers(FLUSH_MINIMAL);
Bram Moolenaar30613902019-12-01 22:11:18 +01002821 *mapdepth = 0; // for next one
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002822 *keylenp = keylen;
2823 return map_result_fail;
2824 }
2825
2826 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002827 * In Select mode and a Visual mode mapping is used: Switch to Visual
2828 * mode temporarily. Append K_SELECT to switch back to Select mode.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002829 */
Bram Moolenaareda35f72019-08-03 14:59:44 +02002830 if (VIsual_active && VIsual_select && (mp->m_mode & VISUAL))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002831 {
2832 VIsual_select = FALSE;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002833 (void)ins_typebuf(K_SELECT_STRING, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002834 }
2835
2836#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002837 // Copy the values from *mp that are used, because evaluating the
2838 // expression may invoke a function that redefines the mapping, thereby
2839 // making *mp invalid.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002840 save_m_expr = mp->m_expr;
2841 save_m_noremap = mp->m_noremap;
2842 save_m_silent = mp->m_silent;
2843 save_m_keys = NULL; // only saved when needed
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002844
2845 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002846 * Handle ":map <expr>": evaluate the {rhs} as an expression. Also
2847 * save and restore the command line for "normal :".
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002848 */
2849 if (mp->m_expr)
2850 {
2851 int save_vgetc_busy = vgetc_busy;
2852 int save_may_garbage_collect = may_garbage_collect;
Bram Moolenaar4ebe0e62019-11-22 20:55:40 +01002853 int was_screen_col = screen_cur_col;
2854 int was_screen_row = screen_cur_row;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002855
2856 vgetc_busy = 0;
2857 may_garbage_collect = FALSE;
2858
2859 save_m_keys = vim_strsave(mp->m_keys);
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002860 map_str = eval_map_expr(mp, NUL);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002861
Bram Moolenaar4ebe0e62019-11-22 20:55:40 +01002862 // The mapping may do anything, but we expect it to take care of
2863 // redrawing. Do put the cursor back where it was.
2864 windgoto(was_screen_row, was_screen_col);
2865 out_flush();
2866
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002867 vgetc_busy = save_vgetc_busy;
2868 may_garbage_collect = save_may_garbage_collect;
2869 }
2870 else
2871#endif
2872 map_str = mp->m_str;
2873
2874 /*
2875 * Insert the 'to' part in the typebuf.tb_buf.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002876 * If 'from' field is the same as the start of the 'to' field, don't
2877 * remap the first character (but do allow abbreviations).
2878 * If m_noremap is set, don't remap the whole 'to' part.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002879 */
2880 if (map_str == NULL)
2881 i = FAIL;
2882 else
2883 {
2884 int noremap;
2885
2886 if (save_m_noremap != REMAP_YES)
2887 noremap = save_m_noremap;
2888 else if (
2889#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002890 STRNCMP(map_str, save_m_keys != NULL ? save_m_keys : mp->m_keys,
2891 (size_t)keylen)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002892#else
2893 STRNCMP(map_str, mp->m_keys, (size_t)keylen)
2894#endif
2895 != 0)
2896 noremap = REMAP_YES;
2897 else
2898 noremap = REMAP_SKIP;
2899 i = ins_typebuf(map_str, noremap,
2900 0, TRUE, cmd_silent || save_m_silent);
2901#ifdef FEAT_EVAL
2902 if (save_m_expr)
2903 vim_free(map_str);
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002904 last_used_map = mp;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002905#endif
2906 }
2907#ifdef FEAT_EVAL
2908 vim_free(save_m_keys);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002909#endif
2910 *keylenp = keylen;
2911 if (i == FAIL)
2912 return map_result_fail;
2913 return map_result_retry;
2914 }
2915
2916 *keylenp = keylen;
2917 return map_result_nomatch;
2918}
2919
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002920/*
2921 * unget one character (can only be done once!)
2922 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002924vungetc(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925{
2926 old_char = c;
2927 old_mod_mask = mod_mask;
Bram Moolenaarb8978712013-03-16 21:42:16 +01002928 old_mouse_row = mouse_row;
2929 old_mouse_col = mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930}
2931
2932/*
Bram Moolenaar32526b32019-01-19 17:43:09 +01002933 * Get a byte:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 * 1. from the stuffbuffer
2935 * This is used for abbreviated commands like "D" -> "d$".
2936 * Also used to redo a command for ".".
2937 * 2. from the typeahead buffer
2938 * Stores text obtained previously but not used yet.
2939 * Also stores the result of mappings.
2940 * Also used for the ":normal" command.
2941 * 3. from the user
2942 * This may do a blocking wait if "advance" is TRUE.
2943 *
2944 * if "advance" is TRUE (vgetc()):
Bram Moolenaard29459b2016-08-26 22:29:11 +02002945 * Really get the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 * KeyTyped is set to TRUE in the case the user typed the key.
2947 * KeyStuffed is TRUE if the character comes from the stuff buffer.
2948 * if "advance" is FALSE (vpeekc()):
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02002949 * Just look whether there is a character available.
2950 * Return NUL if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 *
2952 * When "no_mapping" is zero, checks for mappings in the current mode.
2953 * Only returns one byte (of a multi-byte character).
2954 * K_SPECIAL and CSI may be escaped, need to get two more bytes then.
2955 */
2956 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002957vgetorpeek(int advance)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958{
2959 int c, c1;
Bram Moolenaar30613902019-12-01 22:11:18 +01002960 int timedout = FALSE; // waited for more than 1 second
2961 // for mapping to complete
2962 int mapdepth = 0; // check for recursive mapping
2963 int mode_deleted = FALSE; // set when mode has been deleted
Bram Moolenaar4e427192006-03-10 21:34:27 +00002964#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 int new_wcol, new_wrow;
2966#endif
2967#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01002968 int shape_changed = FALSE; // adjusted cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969#endif
2970 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 int old_wcol, old_wrow;
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00002972 int wait_tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973
2974 /*
2975 * This function doesn't work very well when called recursively. This may
2976 * happen though, because of:
2977 * 1. The call to add_to_showcmd(). char_avail() is then used to check if
2978 * there is a character available, which calls this function. In that
2979 * case we must return NUL, to indicate no character is available.
2980 * 2. A GUI callback function writes to the screen, causing a
2981 * wait_return().
2982 * Using ":normal" can also do this, but it saves the typeahead buffer,
2983 * thus it should be OK. But don't get a key from the user then.
2984 */
Bram Moolenaare2c38102016-01-31 14:55:40 +01002985 if (vgetc_busy > 0 && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 return NUL;
2987
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002988 ++vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989
2990 if (advance)
2991 KeyStuffed = FALSE;
2992
2993 init_typebuf();
2994 start_stuff();
2995 if (advance && typebuf.tb_maplen == 0)
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02002996 reg_executing = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997 do
2998 {
2999/*
3000 * get a character: 1. from the stuffbuffer
3001 */
3002 if (typeahead_char != 0)
3003 {
3004 c = typeahead_char;
3005 if (advance)
3006 typeahead_char = 0;
3007 }
3008 else
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01003009 c = read_readbuffers(advance);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 if (c != NUL && !got_int)
3011 {
3012 if (advance)
3013 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003014 // KeyTyped = FALSE; When the command that stuffed something
3015 // was typed, behave like the stuffed command was typed.
3016 // needed for CTRL-W CTRL-] to open a fold, for example.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 KeyStuffed = TRUE;
3018 }
3019 if (typebuf.tb_no_abbr_cnt == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003020 typebuf.tb_no_abbr_cnt = 1; // no abbreviations now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 }
3022 else
3023 {
3024 /*
3025 * Loop until we either find a matching mapped key, or we
3026 * are sure that it is not a mapped key.
3027 * If a mapped key sequence is found we go back to the start to
3028 * try re-mapping.
3029 */
3030 for (;;)
3031 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003032 long wait_time;
3033 int keylen = 0;
Bram Moolenaareda35f72019-08-03 14:59:44 +02003034#ifdef FEAT_CMDL_INFO
3035 int showcmd_idx;
3036#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 /*
3038 * ui_breakcheck() is slow, don't use it too often when
3039 * inside a mapping. But call it each time for typed
3040 * characters.
3041 */
3042 if (typebuf.tb_maplen)
3043 line_breakcheck();
3044 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003045 ui_breakcheck(); // check for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 if (got_int)
3047 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003048 // flush all input
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003049 c = inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 0L);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003050
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 /*
3052 * If inchar() returns TRUE (script file was active) or we
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003053 * are inside a mapping, get out of Insert mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 * Otherwise we behave like having gotten a CTRL-C.
3055 * As a result typing CTRL-C in insert mode will
3056 * really insert a CTRL-C.
3057 */
3058 if ((c || typebuf.tb_maplen)
3059 && (State & (INSERT + CMDLINE)))
3060 c = ESC;
3061 else
3062 c = Ctrl_C;
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02003063 flush_buffers(FLUSH_INPUT); // flush all typeahead
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064
Bram Moolenaard9dfd572006-10-03 13:36:13 +00003065 if (advance)
3066 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003067 // Also record this character, it might be needed to
3068 // get out of Insert mode.
Bram Moolenaard9dfd572006-10-03 13:36:13 +00003069 *typebuf.tb_buf = c;
3070 gotchars(typebuf.tb_buf, 1);
3071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 cmd_silent = FALSE;
3073
3074 break;
3075 }
3076 else if (typebuf.tb_len > 0)
3077 {
3078 /*
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003079 * Check for a mapping in "typebuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 */
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003081 map_result_T result = handle_mapping(
3082 &keylen, &timedout, &mapdepth);
3083
3084 if (result == map_result_retry)
3085 // try mapping again
3086 continue;
3087 if (result == map_result_fail)
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003088 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003089 // failed, use the outer loop
3090 c = -1;
3091 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003093 if (result == map_result_get)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095/*
3096 * get a character: 2. from the typeahead buffer
3097 */
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003098 c = typebuf.tb_buf[typebuf.tb_off];
Bram Moolenaar30613902019-12-01 22:11:18 +01003099 if (advance) // remove chars from tb_buf
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003100 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003101 cmd_silent = (typebuf.tb_silent > 0);
3102 if (typebuf.tb_maplen > 0)
3103 KeyTyped = FALSE;
3104 else
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003105 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003106 KeyTyped = TRUE;
Bram Moolenaar30613902019-12-01 22:11:18 +01003107 // write char to script file(s)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003108 gotchars(typebuf.tb_buf
3109 + typebuf.tb_off, 1);
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003110 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003111 KeyNoremap = typebuf.tb_noremap[
3112 typebuf.tb_off];
3113 del_typebuf(1, 0);
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003114 }
Bram Moolenaar52797ba2021-12-16 14:45:13 +00003115 break; // got character, break the for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 }
3117
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003118 // not enough characters, get more
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 }
3120
3121/*
3122 * get a character: 3. from the user - handle <Esc> in Insert mode
3123 */
3124 /*
Bram Moolenaarf085f422017-06-07 20:39:47 +02003125 * Special case: if we get an <ESC> in insert mode and there
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 * are no more characters at once, we pretend to go out of
3127 * insert mode. This prevents the one second delay after
3128 * typing an <ESC>. If we get something after all, we may
3129 * have to redisplay the mode. That the cursor is in the wrong
3130 * place does not matter.
3131 */
3132 c = 0;
3133#ifdef FEAT_CMDL_INFO
3134 new_wcol = curwin->w_wcol;
3135 new_wrow = curwin->w_wrow;
3136#endif
3137 if ( advance
3138 && typebuf.tb_len == 1
3139 && typebuf.tb_buf[typebuf.tb_off] == ESC
3140 && !no_mapping
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 && ex_normal_busy == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 && typebuf.tb_maplen == 0
3143 && (State & INSERT)
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003144 && (p_timeout
3145 || (keylen == KEYLEN_PART_KEY && p_ttimeout))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 && (c = inchar(typebuf.tb_buf + typebuf.tb_off
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003147 + typebuf.tb_len, 3, 25L)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 {
3149 colnr_T col = 0, vcol;
3150 char_u *ptr;
3151
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003152 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 {
3154 unshowmode(TRUE);
3155 mode_deleted = TRUE;
3156 }
3157#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003158 // may show a different cursor shape
Bram Moolenaarf085f422017-06-07 20:39:47 +02003159 if (gui.in_use && State != NORMAL && !cmd_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 {
3161 int save_State;
3162
3163 save_State = State;
3164 State = NORMAL;
3165 gui_update_cursor(TRUE, FALSE);
3166 State = save_State;
3167 shape_changed = TRUE;
3168 }
3169#endif
3170 validate_cursor();
3171 old_wcol = curwin->w_wcol;
3172 old_wrow = curwin->w_wrow;
3173
Bram Moolenaar30613902019-12-01 22:11:18 +01003174 // move cursor left, if possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 if (curwin->w_cursor.col != 0)
3176 {
3177 if (curwin->w_wcol > 0)
3178 {
3179 if (did_ai)
3180 {
3181 /*
3182 * We are expecting to truncate the trailing
3183 * white-space, so find the last non-white
3184 * character -- webb
3185 */
3186 col = vcol = curwin->w_wcol = 0;
3187 ptr = ml_get_curline();
3188 while (col < curwin->w_cursor.col)
3189 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01003190 if (!VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 curwin->w_wcol = vcol;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003192 vcol += lbr_chartabsize(ptr, ptr + col,
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01003193 vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003195 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 ++col;
3198 }
3199 curwin->w_wrow = curwin->w_cline_row
Bram Moolenaar02631462017-09-22 15:20:32 +02003200 + curwin->w_wcol / curwin->w_width;
3201 curwin->w_wcol %= curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 curwin->w_wcol += curwin_col_off();
Bram Moolenaar30613902019-12-01 22:11:18 +01003203 col = 0; // no correction needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 }
3205 else
3206 {
3207 --curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 col = curwin->w_cursor.col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 }
3210 }
3211 else if (curwin->w_p_wrap && curwin->w_wrow)
3212 {
3213 --curwin->w_wrow;
Bram Moolenaar02631462017-09-22 15:20:32 +02003214 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 col = curwin->w_cursor.col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 if (has_mbyte && col > 0 && curwin->w_wcol > 0)
3218 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003219 // Correct when the cursor is on the right halve
3220 // of a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 ptr = ml_get_curline();
3222 col -= (*mb_head_off)(ptr, ptr + col);
3223 if ((*mb_ptr2cells)(ptr + col) > 1)
3224 --curwin->w_wcol;
3225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 }
3227 setcursor();
3228 out_flush();
3229#ifdef FEAT_CMDL_INFO
3230 new_wcol = curwin->w_wcol;
3231 new_wrow = curwin->w_wrow;
3232#endif
3233 curwin->w_wcol = old_wcol;
3234 curwin->w_wrow = old_wrow;
3235 }
3236 if (c < 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003237 continue; // end of input script reached
Bram Moolenaar20c38922014-07-23 20:41:14 +02003238
Bram Moolenaar30613902019-12-01 22:11:18 +01003239 // Allow mapping for just typed characters. When we get here c
3240 // is the number of extra bytes and typebuf.tb_len is 1.
Bram Moolenaar20c38922014-07-23 20:41:14 +02003241 for (n = 1; n <= c; ++n)
3242 typebuf.tb_noremap[typebuf.tb_off + n] = RM_YES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 typebuf.tb_len += c;
3244
Bram Moolenaar30613902019-12-01 22:11:18 +01003245 // buffer full, don't map
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 if (typebuf.tb_len >= typebuf.tb_maplen + MAXMAPLEN)
3247 {
3248 timedout = TRUE;
3249 continue;
3250 }
3251
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 if (ex_normal_busy > 0)
3253 {
Bram Moolenaare2c38102016-01-31 14:55:40 +01003254#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 static int tc = 0;
Bram Moolenaare2c38102016-01-31 14:55:40 +01003256#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257
Bram Moolenaar30613902019-12-01 22:11:18 +01003258 // No typeahead left and inside ":normal". Must return
3259 // something to avoid getting stuck. When an incomplete
3260 // mapping is present, behave like it timed out.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 if (typebuf.tb_len > 0)
3262 {
3263 timedout = TRUE;
3264 continue;
3265 }
Bram Moolenaar189832b2020-09-23 12:29:11 +02003266
Bram Moolenaar30613902019-12-01 22:11:18 +01003267 // When 'insertmode' is set, ESC just beeps in Insert
3268 // mode. Use CTRL-L to make edit() return.
3269 // For the command line only CTRL-C always breaks it.
3270 // For the cmdline window: Alternate between ESC and
3271 // CTRL-C: ESC for most situations and CTRL-C to close the
3272 // cmdline window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 if (p_im && (State & INSERT))
3274 c = Ctrl_L;
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003275#ifdef FEAT_TERMINAL
3276 else if (terminal_is_active())
3277 c = K_CANCEL;
3278#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 else if ((State & CMDLINE)
Bram Moolenaare2c38102016-01-31 14:55:40 +01003280#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 || (cmdwin_type > 0 && tc == ESC)
Bram Moolenaare2c38102016-01-31 14:55:40 +01003282#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 )
3284 c = Ctrl_C;
3285 else
3286 c = ESC;
Bram Moolenaare2c38102016-01-31 14:55:40 +01003287#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 tc = c;
Bram Moolenaare2c38102016-01-31 14:55:40 +01003289#endif
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01003290 // return from main_loop()
3291 if (pending_exmode_active)
3292 exmode_active = EXMODE_NORMAL;
3293
Bram Moolenaarb37a65e2022-01-01 12:42:56 +00003294 // no chars to block abbreviation for
3295 typebuf.tb_no_abbr_cnt = 0;
3296
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 break;
3298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299
3300/*
3301 * get a character: 3. from the user - update display
3302 */
Bram Moolenaar30613902019-12-01 22:11:18 +01003303 // In insert mode a screen update is skipped when characters
3304 // are still available. But when those available characters
3305 // are part of a mapping, and we are going to do a blocking
3306 // wait here. Need to update the screen to display the
3307 // changed text so far. Also for when 'lazyredraw' is set and
3308 // redrawing was postponed because there was something in the
3309 // input buffer (e.g., termresponse).
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01003310 if (((State & INSERT) != 0 || p_lz) && (State & CMDLINE) == 0
3311 && advance && must_redraw != 0 && !need_wait_return)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 {
3313 update_screen(0);
Bram Moolenaar30613902019-12-01 22:11:18 +01003314 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 }
3316
3317 /*
3318 * If we have a partial match (and are going to wait for more
3319 * input from the user), show the partially matched characters
3320 * to the user with showcmd.
3321 */
3322#ifdef FEAT_CMDL_INFO
Bram Moolenaareda35f72019-08-03 14:59:44 +02003323 showcmd_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324#endif
3325 c1 = 0;
3326 if (typebuf.tb_len > 0 && advance && !exmode_active)
3327 {
3328 if (((State & (NORMAL | INSERT)) || State == LANGMAP)
3329 && State != HITRETURN)
3330 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003331 // this looks nice when typing a dead character map
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 if (State & INSERT
3333 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
3334 + typebuf.tb_len - 1) == 1)
3335 {
3336 edit_putchar(typebuf.tb_buf[typebuf.tb_off
3337 + typebuf.tb_len - 1], FALSE);
Bram Moolenaar30613902019-12-01 22:11:18 +01003338 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 c1 = 1;
3340 }
3341#ifdef FEAT_CMDL_INFO
Bram Moolenaar30613902019-12-01 22:11:18 +01003342 // need to use the col and row from above here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 old_wcol = curwin->w_wcol;
3344 old_wrow = curwin->w_wrow;
3345 curwin->w_wcol = new_wcol;
3346 curwin->w_wrow = new_wrow;
3347 push_showcmd();
3348 if (typebuf.tb_len > SHOWCMD_COLS)
Bram Moolenaareda35f72019-08-03 14:59:44 +02003349 showcmd_idx = typebuf.tb_len - SHOWCMD_COLS;
3350 while (showcmd_idx < typebuf.tb_len)
3351 (void)add_to_showcmd(
3352 typebuf.tb_buf[typebuf.tb_off + showcmd_idx++]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 curwin->w_wcol = old_wcol;
3354 curwin->w_wrow = old_wrow;
3355#endif
3356 }
3357
Bram Moolenaar30613902019-12-01 22:11:18 +01003358 // this looks nice when typing a dead character map
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 if ((State & CMDLINE)
3360#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3361 && cmdline_star == 0
3362#endif
3363 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
3364 + typebuf.tb_len - 1) == 1)
3365 {
3366 putcmdline(typebuf.tb_buf[typebuf.tb_off
3367 + typebuf.tb_len - 1], FALSE);
3368 c1 = 1;
3369 }
3370 }
3371
3372/*
3373 * get a character: 3. from the user - get it
3374 */
Bram Moolenaar83f4cbd2018-06-12 21:35:40 +02003375 if (typebuf.tb_len == 0)
3376 // timedout may have been set while waiting for a mapping
3377 // that has a <Nop> RHS.
3378 timedout = FALSE;
3379
Bram Moolenaar652de232019-04-04 20:13:09 +02003380 if (advance)
3381 {
3382 if (typebuf.tb_len == 0
3383 || !(p_timeout
3384 || (p_ttimeout && keylen == KEYLEN_PART_KEY)))
3385 // blocking wait
3386 wait_time = -1L;
3387 else if (keylen == KEYLEN_PART_KEY && p_ttm >= 0)
3388 wait_time = p_ttm;
3389 else
3390 wait_time = p_tm;
3391 }
3392 else
3393 wait_time = 0;
3394
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003395 wait_tb_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 c = inchar(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len,
3397 typebuf.tb_buflen - typebuf.tb_off - typebuf.tb_len - 1,
Bram Moolenaar652de232019-04-04 20:13:09 +02003398 wait_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399
3400#ifdef FEAT_CMDL_INFO
Bram Moolenaareda35f72019-08-03 14:59:44 +02003401 if (showcmd_idx != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 pop_showcmd();
3403#endif
3404 if (c1 == 1)
3405 {
3406 if (State & INSERT)
3407 edit_unputchar();
3408 if (State & CMDLINE)
3409 unputcmdline();
Bram Moolenaarbc256d92012-06-06 12:06:15 +02003410 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003411 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 }
3413
3414 if (c < 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003415 continue; // end of input script reached
3416 if (c == NUL) // no character available
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 {
3418 if (!advance)
3419 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01003420 if (wait_tb_len > 0) // timed out
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 {
3422 timedout = TRUE;
3423 continue;
3424 }
3425 }
3426 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003427 { // allow mapping for just typed characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 while (typebuf.tb_buf[typebuf.tb_off
3429 + typebuf.tb_len] != NUL)
3430 typebuf.tb_noremap[typebuf.tb_off
3431 + typebuf.tb_len++] = RM_YES;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003432#ifdef HAVE_INPUT_METHOD
Bram Moolenaar30613902019-12-01 22:11:18 +01003433 // Get IM status right after getting keys, not after the
3434 // timeout for a mapping (focus may be lost by then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 vgetc_im_active = im_get_status();
3436#endif
3437 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003438 } // for (;;)
3439 } // if (!character from stuffbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440
Bram Moolenaar30613902019-12-01 22:11:18 +01003441 // if advance is FALSE don't loop on NULs
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003442 } while ((c < 0 && c != K_CANCEL) || (advance && c == NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443
3444 /*
3445 * The "INSERT" message is taken care of here:
3446 * if we return an ESC to exit insert mode, the message is deleted
3447 * if we don't return an ESC but deleted the message before, redisplay it
3448 */
Bram Moolenaar09df3122006-01-23 22:23:09 +00003449 if (advance && p_smd && msg_silent == 0 && (State & INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003451 if (c == ESC && !mode_deleted && !no_mapping && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 {
3453 if (typebuf.tb_len && !KeyTyped)
Bram Moolenaar30613902019-12-01 22:11:18 +01003454 redraw_cmdline = TRUE; // delete mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 else
3456 unshowmode(FALSE);
3457 }
3458 else if (c != ESC && mode_deleted)
3459 {
3460 if (typebuf.tb_len && !KeyTyped)
Bram Moolenaar30613902019-12-01 22:11:18 +01003461 redraw_cmdline = TRUE; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 else
3463 showmode();
3464 }
3465 }
3466#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003467 // may unshow different cursor shape
Bram Moolenaarf085f422017-06-07 20:39:47 +02003468 if (gui.in_use && shape_changed)
3469 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470#endif
Bram Moolenaar6edbbd82019-03-10 09:41:51 +01003471 if (timedout && c == ESC)
3472 {
3473 char_u nop_buf[3];
3474
3475 // When recording there will be no timeout. Add a <Nop> after the ESC
3476 // to avoid that it forms a key code with following characters.
3477 nop_buf[0] = K_SPECIAL;
3478 nop_buf[1] = KS_EXTRA;
3479 nop_buf[2] = KE_NOP;
3480 gotchars(nop_buf, 3);
3481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003483 --vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484
3485 return c;
3486}
3487
3488/*
3489 * inchar() - get one character from
3490 * 1. a scriptfile
3491 * 2. the keyboard
3492 *
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003493 * As many characters as we can get (up to 'maxlen') are put in "buf" and
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 * NUL terminated (buffer length must be 'maxlen' + 1).
3495 * Minimum for "maxlen" is 3!!!!
3496 *
3497 * "tb_change_cnt" is the value of typebuf.tb_change_cnt if "buf" points into
3498 * it. When typebuf.tb_change_cnt changes (e.g., when a message is received
3499 * from a remote client) "buf" can no longer be used. "tb_change_cnt" is 0
3500 * otherwise.
3501 *
3502 * If we got an interrupt all input is read until none is available.
3503 *
3504 * If wait_time == 0 there is no waiting for the char.
3505 * If wait_time == n we wait for n msec for a character to arrive.
3506 * If wait_time == -1 we wait forever for a character to arrive.
3507 *
3508 * Return the number of obtained characters.
3509 * Return -1 when end of input script reached.
3510 */
Bram Moolenaarcda77642016-06-04 13:32:35 +02003511 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003512inchar(
3513 char_u *buf,
3514 int maxlen,
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003515 long wait_time) // milliseconds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516{
Bram Moolenaar30613902019-12-01 22:11:18 +01003517 int len = 0; // init for GCC
3518 int retesc = FALSE; // return ESC with gotint
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 int script_char;
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003520 int tb_change_cnt = typebuf.tb_change_cnt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521
Bram Moolenaar30613902019-12-01 22:11:18 +01003522 if (wait_time == -1L || wait_time > 100L) // flush output before waiting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 {
3524 cursor_on();
Bram Moolenaara338adc2018-01-31 20:51:47 +01003525 out_flush_cursor(FALSE, FALSE);
3526#if defined(FEAT_GUI) && defined(FEAT_MOUSESHAPE)
3527 if (gui.in_use && postponed_mouseshape)
3528 update_mouseshape(-1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529#endif
3530 }
3531
3532 /*
3533 * Don't reset these when at the hit-return prompt, otherwise a endless
3534 * recursive loop may result (write error in swapfile, hit-return, timeout
3535 * on char wait, flush swapfile, write error....).
3536 */
3537 if (State != HITRETURN)
3538 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003539 did_outofmem_msg = FALSE; // display out of memory message (again)
3540 did_swapwrite_msg = FALSE; // display swap file write error again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003542 undo_off = FALSE; // restart undo now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543
3544 /*
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00003545 * Get a character from a script file if there is one.
3546 * If interrupted: Stop reading script files, close them all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 */
3548 script_char = -1;
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00003549 while (scriptin[curscript] != NULL && script_char < 0
3550#ifdef FEAT_EVAL
3551 && !ignore_script
3552#endif
3553 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02003555#ifdef MESSAGE_QUEUE
3556 parse_queued_messages();
Bram Moolenaarf2330482008-06-24 20:19:36 +00003557#endif
3558
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 if (got_int || (script_char = getc(scriptin[curscript])) < 0)
3560 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003561 // Reached EOF.
3562 // Careful: closescript() frees typebuf.tb_buf[] and buf[] may
3563 // point inside typebuf.tb_buf[]. Don't use buf[] after this!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 closescript();
3565 /*
3566 * When reading script file is interrupted, return an ESC to get
3567 * back to normal mode.
3568 * Otherwise return -1, because typebuf.tb_buf[] has changed.
3569 */
3570 if (got_int)
3571 retesc = TRUE;
3572 else
3573 return -1;
3574 }
3575 else
3576 {
3577 buf[0] = script_char;
3578 len = 1;
3579 }
3580 }
3581
Bram Moolenaar30613902019-12-01 22:11:18 +01003582 if (script_char < 0) // did not get a character from script
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 {
3584 /*
3585 * If we got an interrupt, skip all previously typed characters and
3586 * return TRUE if quit reading script file.
3587 * Stop reading typeahead when a single CTRL-C was read,
3588 * fill_input_buf() returns this when not able to read from stdin.
3589 * Don't use buf[] here, closescript() may have freed typebuf.tb_buf[]
3590 * and buf may be pointing inside typebuf.tb_buf[].
3591 */
3592 if (got_int)
3593 {
3594#define DUM_LEN MAXMAPLEN * 3 + 3
3595 char_u dum[DUM_LEN + 1];
3596
3597 for (;;)
3598 {
3599 len = ui_inchar(dum, DUM_LEN, 0L, 0);
3600 if (len == 0 || (len == 1 && dum[0] == 3))
3601 break;
3602 }
3603 return retesc;
3604 }
3605
3606 /*
3607 * Always flush the output characters when getting input characters
Bram Moolenaarcb574f42019-01-25 22:29:57 +01003608 * from the user and not just peeking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 */
Bram Moolenaarcb574f42019-01-25 22:29:57 +01003610 if (wait_time == -1L || wait_time > 10L)
3611 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612
3613 /*
3614 * Fill up to a third of the buffer, because each character may be
3615 * tripled below.
3616 */
3617 len = ui_inchar(buf, maxlen / 3, wait_time, tb_change_cnt);
3618 }
3619
Bram Moolenaar30613902019-12-01 22:11:18 +01003620 // If the typebuf was changed further down, it is like nothing was added by
3621 // this call.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 if (typebuf_changed(tb_change_cnt))
3623 return 0;
3624
Bram Moolenaar30613902019-12-01 22:11:18 +01003625 // Note the change in the typeahead buffer, this matters for when
3626 // vgetorpeek() is called recursively, e.g. using getchar(1) in a timer
3627 // function.
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003628 if (len > 0 && ++typebuf.tb_change_cnt == 0)
3629 typebuf.tb_change_cnt = 1;
3630
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003631 return fix_input_buffer(buf, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632}
3633
3634/*
3635 * Fix typed characters for use by vgetc() and check_termcode().
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003636 * "buf[]" must have room to triple the number of bytes!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 * Returns the new length.
3638 */
3639 int
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003640fix_input_buffer(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641{
3642 int i;
3643 char_u *p = buf;
3644
3645 /*
3646 * Two characters are special: NUL and K_SPECIAL.
3647 * When compiled With the GUI CSI is also special.
3648 * Replace NUL by K_SPECIAL KS_ZERO KE_FILLER
3649 * Replace K_SPECIAL by K_SPECIAL KS_SPECIAL KE_FILLER
3650 * Replace CSI by K_SPECIAL KS_EXTRA KE_CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 */
3652 for (i = len; --i >= 0; ++p)
3653 {
3654#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003655 // When the GUI is used any character can come after a CSI, don't
3656 // escape it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 if (gui.in_use && p[0] == CSI && i >= 2)
3658 {
3659 p += 2;
3660 i -= 2;
3661 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003662 // When the GUI is not used CSI needs to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 else if (!gui.in_use && p[0] == CSI)
3664 {
3665 mch_memmove(p + 3, p + 1, (size_t)i);
3666 *p++ = K_SPECIAL;
3667 *p++ = KS_EXTRA;
3668 *p = (int)KE_CSI;
3669 len += 2;
3670 }
3671 else
3672#endif
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003673 if (p[0] == NUL || (p[0] == K_SPECIAL
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003674 // timeout may generate K_CURSORHOLD
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003675 && (i < 2 || p[1] != KS_EXTRA || p[2] != (int)KE_CURSORHOLD)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003676#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003677 // Win32 console passes modifiers
3678 && (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003679# ifdef VIMDLL
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003680 gui.in_use ||
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003681# endif
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003682 (i < 2 || p[1] != KS_MODIFIER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683#endif
3684 ))
3685 {
3686 mch_memmove(p + 3, p + 1, (size_t)i);
3687 p[2] = K_THIRD(p[0]);
3688 p[1] = K_SECOND(p[0]);
3689 p[0] = K_SPECIAL;
3690 p += 2;
3691 len += 2;
3692 }
3693 }
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003694 *p = NUL; // add trailing NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 return len;
3696}
3697
3698#if defined(USE_INPUT_BUF) || defined(PROTO)
3699/*
3700 * Return TRUE when bytes are in the input buffer or in the typeahead buffer.
3701 * Normally the input buffer would be sufficient, but the server_to_input_buf()
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003702 * or feedkeys() may insert characters in the typeahead buffer while we are
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003703 * waiting for input to arrive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 */
3705 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003706input_available(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707{
3708 return (!vim_is_input_buf_empty()
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003709# if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
3710 || typebuf_was_filled
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711# endif
3712 );
3713}
3714#endif
Bram Moolenaar957cf672020-11-12 14:21:06 +01003715
3716/*
3717 * Function passed to do_cmdline() to get the command after a <Cmd> key from
3718 * typeahead.
3719 */
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003720 static char_u *
Bram Moolenaar957cf672020-11-12 14:21:06 +01003721getcmdkeycmd(
3722 int promptc UNUSED,
3723 void *cookie UNUSED,
3724 int indent UNUSED,
3725 getline_opt_T do_concat UNUSED)
3726{
3727 garray_T line_ga;
3728 int c1 = -1;
3729 int c2;
3730 int cmod = 0;
3731 int aborted = FALSE;
3732
3733 ga_init2(&line_ga, 1, 32);
3734
3735 // no mapping for these characters
3736 no_mapping++;
3737
3738 got_int = FALSE;
3739 while (c1 != NUL && !aborted)
3740 {
Bram Moolenaarca359cb2020-11-15 20:49:41 +01003741 if (ga_grow(&line_ga, 32) != OK)
3742 {
3743 aborted = TRUE;
3744 break;
3745 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003746
3747 if (vgetorpeek(FALSE) == NUL)
3748 {
3749 // incomplete <Cmd> is an error, because there is not much the user
3750 // could do in this state.
3751 emsg(_(e_cmd_mapping_must_end_with_cr));
3752 aborted = TRUE;
3753 break;
3754 }
3755
3756 // Get one character at a time.
3757 c1 = vgetorpeek(TRUE);
3758
3759 // Get two extra bytes for special keys
3760 if (c1 == K_SPECIAL)
3761 {
3762 c1 = vgetorpeek(TRUE);
3763 c2 = vgetorpeek(TRUE);
3764 if (c1 == KS_MODIFIER)
3765 {
3766 cmod = c2;
3767 continue;
3768 }
3769 c1 = TO_SPECIAL(c1, c2);
3770 }
Bram Moolenaar4a441202020-11-28 14:43:26 +01003771 if (c1 == Ctrl_V)
3772 {
3773 // CTRL-V is followed by octal, hex or other characters, reverses
3774 // what AppendToRedobuffLit() does.
3775 no_reduce_keys = TRUE; // don't merge modifyOtherKeys
Bram Moolenaar0684e362020-12-03 19:54:42 +01003776 c1 = get_literal(TRUE);
Bram Moolenaar4a441202020-11-28 14:43:26 +01003777 no_reduce_keys = FALSE;
3778 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003779
3780 if (got_int)
3781 aborted = TRUE;
3782 else if (c1 == '\r' || c1 == '\n')
3783 c1 = NUL; // end the line
3784 else if (c1 == ESC)
3785 aborted = TRUE;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003786 else if (c1 == K_COMMAND || c1 == K_SCRIPT_COMMAND)
Bram Moolenaar957cf672020-11-12 14:21:06 +01003787 {
3788 // give a nicer error message for this special case
3789 emsg(_(e_cmd_mapping_must_end_with_cr_before_second_cmd));
3790 aborted = TRUE;
3791 }
3792 else if (IS_SPECIAL(c1))
3793 {
3794 if (c1 == K_SNR)
Bram Moolenaarc77534c2020-11-18 11:34:37 +01003795 ga_concat(&line_ga, (char_u *)"<SNR>");
Bram Moolenaar957cf672020-11-12 14:21:06 +01003796 else
3797 {
3798 semsg(e_cmd_maping_must_not_include_str_key,
3799 get_special_key_name(c1, cmod));
3800 aborted = TRUE;
3801 }
3802 }
3803 else
Bram Moolenaara9725222022-01-16 13:30:33 +00003804 ga_append(&line_ga, c1);
Bram Moolenaar957cf672020-11-12 14:21:06 +01003805
3806 cmod = 0;
3807 }
3808
3809 no_mapping--;
3810
3811 if (aborted)
3812 ga_clear(&line_ga);
3813
3814 return (char_u *)line_ga.ga_data;
3815}
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003816
3817 int
Bram Moolenaard4e2f502022-01-15 18:48:32 +00003818do_cmdkey_command(int key UNUSED, int flags)
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003819{
3820 int res;
3821#ifdef FEAT_EVAL
Bram Moolenaara9725222022-01-16 13:30:33 +00003822 sctx_T save_current_sctx = {-1, 0, 0, 0};
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003823
3824 if (key == K_SCRIPT_COMMAND && last_used_map != NULL)
3825 {
3826 save_current_sctx = current_sctx;
3827 current_sctx = last_used_map->m_script_ctx;
3828 }
3829#endif
3830
3831 res = do_cmdline(NULL, getcmdkeycmd, NULL, flags);
3832
3833#ifdef FEAT_EVAL
Bram Moolenaara9725222022-01-16 13:30:33 +00003834 if (save_current_sctx.sc_sid >= 0)
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003835 current_sctx = save_current_sctx;
3836#endif
3837
3838 return res;
3839}
3840
3841#if defined(FEAT_EVAL) || defined(PROTO)
3842 void
Bram Moolenaarf61c89d2022-01-19 22:51:48 +00003843reset_last_used_map(mapblock_T *mp)
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003844{
Bram Moolenaarf61c89d2022-01-19 22:51:48 +00003845 if (last_used_map == mp)
3846 last_used_map = NULL;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003847}
3848#endif