blob: b81f64a65c0aeeae7440a0bd69ad3e270575ccc3 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
zeertzjqa3f83fe2021-11-22 12:47:39 +000011 * getchar.c: Code related to getting a character from the user or a script
12 * file, manipulations with redo buffer and stuff buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013 */
14
15#include "vim.h"
16
17/*
18 * These buffers are used for storing:
19 * - stuffed characters: A command that is translated into another command.
20 * - redo characters: will redo the last change.
Bram Moolenaarf6f95d92009-11-11 15:23:37 +000021 * - recorded characters: for the "q" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022 *
23 * The bytes are stored like in the typeahead buffer:
24 * - K_SPECIAL introduces a special key (two more bytes follow). A literal
25 * K_SPECIAL is stored as K_SPECIAL KS_SPECIAL KE_FILLER.
26 * - CSI introduces a GUI termcap code (also when gui.in_use is FALSE,
27 * otherwise switching the GUI on would make mappings invalid).
28 * A literal CSI is stored as CSI KS_EXTRA KE_CSI.
29 * These translations are also done on multi-byte characters!
30 *
31 * Escaping CSI bytes is done by the system-specific input functions, called
32 * by ui_inchar().
33 * Escaping K_SPECIAL is done by inchar().
34 * Un-escaping is done by vgetc().
35 */
36
Bram Moolenaar30613902019-12-01 22:11:18 +010037#define MINIMAL_SIZE 20 // minimal size for b_str
Bram Moolenaar071d4272004-06-13 20:20:40 +000038
Bram Moolenaar285e3352018-04-18 23:01:13 +020039static buffheader_T redobuff = {{NULL, {NUL}}, NULL, 0, 0};
40static buffheader_T old_redobuff = {{NULL, {NUL}}, NULL, 0, 0};
41static buffheader_T recordbuff = {{NULL, {NUL}}, NULL, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +000042
Bram Moolenaar30613902019-12-01 22:11:18 +010043static int typeahead_char = 0; // typeahead char that's not flushed
Bram Moolenaar071d4272004-06-13 20:20:40 +000044
45/*
46 * when block_redo is TRUE redo buffer will not be changed
47 * used by edit() to repeat insertions and 'V' command for redoing
48 */
49static int block_redo = FALSE;
50
Bram Moolenaar459fd782019-10-13 16:43:39 +020051static int KeyNoremap = 0; // remapping flags
Bram Moolenaar071d4272004-06-13 20:20:40 +000052
53/*
Bram Moolenaarfd89d7e2016-06-04 20:25:05 +020054 * Variables used by vgetorpeek() and flush_buffers().
Bram Moolenaar071d4272004-06-13 20:20:40 +000055 *
56 * typebuf.tb_buf[] contains all characters that are not consumed yet.
57 * typebuf.tb_buf[typebuf.tb_off] is the first valid character.
58 * typebuf.tb_buf[typebuf.tb_off + typebuf.tb_len - 1] is the last valid char.
59 * typebuf.tb_buf[typebuf.tb_off + typebuf.tb_len] must be NUL.
60 * The head of the buffer may contain the result of mappings, abbreviations
61 * and @a commands. The length of this part is typebuf.tb_maplen.
62 * typebuf.tb_silent is the part where <silent> applies.
63 * After the head are characters that come from the terminal.
64 * typebuf.tb_no_abbr_cnt is the number of characters in typebuf.tb_buf that
65 * should not be considered for abbreviations.
66 * Some parts of typebuf.tb_buf may not be mapped. These parts are remembered
67 * in typebuf.tb_noremap[], which is the same length as typebuf.tb_buf and
68 * contains RM_NONE for the characters that are not to be remapped.
69 * typebuf.tb_noremap[typebuf.tb_off] is the first valid flag.
70 * (typebuf has been put in globals.h, because check_termcode() needs it).
71 */
Bram Moolenaar30613902019-12-01 22:11:18 +010072#define RM_YES 0 // tb_noremap: remap
73#define RM_NONE 1 // tb_noremap: don't remap
74#define RM_SCRIPT 2 // tb_noremap: remap local script mappings
75#define RM_ABBR 4 // tb_noremap: don't remap, do abbrev.
Bram Moolenaar071d4272004-06-13 20:20:40 +000076
Bram Moolenaar30613902019-12-01 22:11:18 +010077// typebuf.tb_buf has three parts: room in front (for result of mappings), the
78// middle for typeahead and room for new characters (which needs to be 3 *
Dominique Pelleaf4a61a2021-12-27 17:21:41 +000079// MAXMAPLEN for the Amiga).
Bram Moolenaar071d4272004-06-13 20:20:40 +000080#define TYPELEN_INIT (5 * (MAXMAPLEN + 3))
Bram Moolenaar30613902019-12-01 22:11:18 +010081static char_u typebuf_init[TYPELEN_INIT]; // initial typebuf.tb_buf
82static char_u noremapbuf_init[TYPELEN_INIT]; // initial typebuf.tb_noremap
Bram Moolenaar071d4272004-06-13 20:20:40 +000083
Bram Moolenaar30613902019-12-01 22:11:18 +010084static int last_recorded_len = 0; // number of last recorded chars
Bram Moolenaar071d4272004-06-13 20:20:40 +000085
Bram Moolenaare32c3c42022-01-15 18:26:04 +000086#ifdef FEAT_EVAL
87mapblock_T *last_used_map = NULL;
88#endif
89
Bram Moolenaard25c16e2016-01-29 22:13:30 +010090static int read_readbuf(buffheader_T *buf, int advance);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010091static void init_typebuf(void);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010092static void may_sync_undo(void);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020093static void free_typebuf(void);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010094static void closescript(void);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020095static void updatescript(int c);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010096static int vgetorpeek(int);
Bram Moolenaar0f0f2302017-08-30 18:52:56 +020097static int inchar(char_u *buf, int maxlen, long wait_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +000098
99/*
100 * Free and clear a buffer.
101 */
Bram Moolenaarbdff0122020-04-05 18:56:05 +0200102 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100103free_buff(buffheader_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100105 buffblock_T *p, *np;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106
Bram Moolenaar285e3352018-04-18 23:01:13 +0200107 for (p = buf->bh_first.b_next; p != NULL; p = np)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108 {
109 np = p->b_next;
110 vim_free(p);
111 }
Bram Moolenaar285e3352018-04-18 23:01:13 +0200112 buf->bh_first.b_next = NULL;
Bram Moolenaar166788c2022-01-27 21:56:40 +0000113 buf->bh_curr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114}
115
116/*
117 * Return the contents of a buffer as a single string.
118 * K_SPECIAL and CSI in the returned string are escaped.
119 */
120 static char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100121get_buffcont(
122 buffheader_T *buffer,
Bram Moolenaar30613902019-12-01 22:11:18 +0100123 int dozero) // count == zero is not an error
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124{
125 long_u count = 0;
126 char_u *p = NULL;
127 char_u *p2;
128 char_u *str;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200129 buffblock_T *bp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130
Bram Moolenaar30613902019-12-01 22:11:18 +0100131 // compute the total length of the string
Bram Moolenaar285e3352018-04-18 23:01:13 +0200132 for (bp = buffer->bh_first.b_next; bp != NULL; bp = bp->b_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133 count += (long_u)STRLEN(bp->b_str);
134
Bram Moolenaar18a4ba22019-05-24 19:39:03 +0200135 if ((count || dozero) && (p = alloc(count + 1)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136 {
137 p2 = p;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200138 for (bp = buffer->bh_first.b_next; bp != NULL; bp = bp->b_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139 for (str = bp->b_str; *str; )
140 *p2++ = *str++;
141 *p2 = NUL;
142 }
143 return (p);
144}
145
146/*
147 * Return the contents of the record buffer as a single string
148 * and clear the record buffer.
149 * K_SPECIAL and CSI in the returned string are escaped.
150 */
151 char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100152get_recorded(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153{
154 char_u *p;
155 size_t len;
156
157 p = get_buffcont(&recordbuff, TRUE);
158 free_buff(&recordbuff);
159
160 /*
161 * Remove the characters that were added the last time, these must be the
162 * (possibly mapped) characters that stopped the recording.
163 */
164 len = STRLEN(p);
165 if ((int)len >= last_recorded_len)
166 {
167 len -= last_recorded_len;
168 p[len] = NUL;
169 }
170
171 /*
172 * When stopping recording from Insert mode with CTRL-O q, also remove the
173 * CTRL-O.
174 */
175 if (len > 0 && restart_edit != 0 && p[len - 1] == Ctrl_O)
176 p[len - 1] = NUL;
177
178 return (p);
179}
180
181/*
182 * Return the contents of the redo buffer as a single string.
183 * K_SPECIAL and CSI in the returned string are escaped.
184 */
185 char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100186get_inserted(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187{
Bram Moolenaarf2330482008-06-24 20:19:36 +0000188 return get_buffcont(&redobuff, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189}
190
191/*
Bram Moolenaar4e86cba2007-05-06 11:56:32 +0000192 * Add string "s" after the current block of buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193 * K_SPECIAL and CSI should have been escaped already.
194 */
195 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100196add_buff(
197 buffheader_T *buf,
198 char_u *s,
Bram Moolenaar30613902019-12-01 22:11:18 +0100199 long slen) // length of "s" or -1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100201 buffblock_T *p;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200202 long_u len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203
204 if (slen < 0)
205 slen = (long)STRLEN(s);
Bram Moolenaar30613902019-12-01 22:11:18 +0100206 if (slen == 0) // don't add empty strings
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207 return;
208
Bram Moolenaar30613902019-12-01 22:11:18 +0100209 if (buf->bh_first.b_next == NULL) // first add to list
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210 {
211 buf->bh_space = 0;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200212 buf->bh_curr = &(buf->bh_first);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213 }
Bram Moolenaar30613902019-12-01 22:11:18 +0100214 else if (buf->bh_curr == NULL) // buffer has already been read
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215 {
Bram Moolenaar6d057012021-12-31 18:49:43 +0000216 iemsg(_(e_add_to_internal_buffer_that_was_already_read_from));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217 return;
218 }
219 else if (buf->bh_index != 0)
Bram Moolenaar285e3352018-04-18 23:01:13 +0200220 mch_memmove(buf->bh_first.b_next->b_str,
221 buf->bh_first.b_next->b_str + buf->bh_index,
222 STRLEN(buf->bh_first.b_next->b_str + buf->bh_index) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223 buf->bh_index = 0;
224
225 if (buf->bh_space >= (int)slen)
226 {
227 len = (long_u)STRLEN(buf->bh_curr->b_str);
Bram Moolenaarb6356332005-07-18 21:40:44 +0000228 vim_strncpy(buf->bh_curr->b_str + len, s, (size_t)slen);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229 buf->bh_space -= slen;
230 }
231 else
232 {
233 if (slen < MINIMAL_SIZE)
234 len = MINIMAL_SIZE;
235 else
236 len = slen;
Bram Moolenaar47ed5532019-08-08 20:49:14 +0200237 p = alloc(offsetof(buffblock_T, b_str) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238 if (p == NULL)
Bram Moolenaar30613902019-12-01 22:11:18 +0100239 return; // no space, just forget it
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000240 buf->bh_space = (int)(len - slen);
Bram Moolenaarb6356332005-07-18 21:40:44 +0000241 vim_strncpy(p->b_str, s, (size_t)slen);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242
Bram Moolenaar285e3352018-04-18 23:01:13 +0200243 p->b_next = buf->bh_curr->b_next;
244 buf->bh_curr->b_next = p;
245 buf->bh_curr = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247}
248
249/*
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000250 * Delete "slen" bytes from the end of "buf".
251 * Only works when it was just added.
252 */
253 static void
254delete_buff_tail(buffheader_T *buf, int slen)
255{
Bram Moolenaara4bc2dd2022-01-27 19:27:16 +0000256 int len;
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000257
Bram Moolenaar37cf4132022-01-27 20:47:03 +0000258 if (buf->bh_curr == NULL)
Bram Moolenaara4bc2dd2022-01-27 19:27:16 +0000259 return; // nothing to delete
260 len = (int)STRLEN(buf->bh_curr->b_str);
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000261 if (len >= slen)
262 {
263 buf->bh_curr->b_str[len - slen] = NUL;
264 buf->bh_space += slen;
265 }
266}
267
268/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269 * Add number "n" to buffer "buf".
270 */
271 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100272add_num_buff(buffheader_T *buf, long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273{
274 char_u number[32];
275
276 sprintf((char *)number, "%ld", n);
277 add_buff(buf, number, -1L);
278}
279
280/*
281 * Add character 'c' to buffer "buf".
282 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
283 */
284 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100285add_char_buff(buffheader_T *buf, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287 char_u bytes[MB_MAXBYTES + 1];
288 int len;
289 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290 char_u temp[4];
291
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292 if (IS_SPECIAL(c))
293 len = 1;
294 else
295 len = (*mb_char2bytes)(c, bytes);
296 for (i = 0; i < len; ++i)
297 {
298 if (!IS_SPECIAL(c))
299 c = bytes[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000300
301 if (IS_SPECIAL(c) || c == K_SPECIAL || c == NUL)
302 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100303 // translate special key code into three byte sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304 temp[0] = K_SPECIAL;
305 temp[1] = K_SECOND(c);
306 temp[2] = K_THIRD(c);
307 temp[3] = NUL;
308 }
309#ifdef FEAT_GUI
310 else if (c == CSI)
311 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100312 // Translate a CSI to a CSI - KS_EXTRA - KE_CSI sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313 temp[0] = CSI;
314 temp[1] = KS_EXTRA;
315 temp[2] = (int)KE_CSI;
316 temp[3] = NUL;
317 }
318#endif
319 else
320 {
321 temp[0] = c;
322 temp[1] = NUL;
323 }
324 add_buff(buf, temp, -1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326}
327
Bram Moolenaar30613902019-12-01 22:11:18 +0100328// First read ahead buffer. Used for translated commands.
Bram Moolenaar285e3352018-04-18 23:01:13 +0200329static buffheader_T readbuf1 = {{NULL, {NUL}}, NULL, 0, 0};
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100330
Bram Moolenaar30613902019-12-01 22:11:18 +0100331// Second read ahead buffer. Used for redo.
Bram Moolenaar285e3352018-04-18 23:01:13 +0200332static buffheader_T readbuf2 = {{NULL, {NUL}}, NULL, 0, 0};
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100333
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334/*
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100335 * Get one byte from the read buffers. Use readbuf1 one first, use readbuf2
336 * if that one is empty.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337 * If advance == TRUE go to the next char.
338 * No translation is done K_SPECIAL and CSI are escaped.
339 */
340 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100341read_readbuffers(int advance)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100343 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100345 c = read_readbuf(&readbuf1, advance);
346 if (c == NUL)
347 c = read_readbuf(&readbuf2, advance);
348 return c;
349}
350
351 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100352read_readbuf(buffheader_T *buf, int advance)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100353{
354 char_u c;
355 buffblock_T *curr;
356
Bram Moolenaar30613902019-12-01 22:11:18 +0100357 if (buf->bh_first.b_next == NULL) // buffer is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 return NUL;
359
Bram Moolenaar285e3352018-04-18 23:01:13 +0200360 curr = buf->bh_first.b_next;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100361 c = curr->b_str[buf->bh_index];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362
363 if (advance)
364 {
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100365 if (curr->b_str[++buf->bh_index] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366 {
Bram Moolenaar285e3352018-04-18 23:01:13 +0200367 buf->bh_first.b_next = curr->b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368 vim_free(curr);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100369 buf->bh_index = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370 }
371 }
372 return c;
373}
374
375/*
Bram Moolenaar06811f32014-02-15 16:17:07 +0100376 * Prepare the read buffers for reading (if they contain something).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377 */
378 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100379start_stuff(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380{
Bram Moolenaar285e3352018-04-18 23:01:13 +0200381 if (readbuf1.bh_first.b_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382 {
Bram Moolenaar285e3352018-04-18 23:01:13 +0200383 readbuf1.bh_curr = &(readbuf1.bh_first);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100384 readbuf1.bh_space = 0;
385 }
Bram Moolenaar285e3352018-04-18 23:01:13 +0200386 if (readbuf2.bh_first.b_next != NULL)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100387 {
Bram Moolenaar285e3352018-04-18 23:01:13 +0200388 readbuf2.bh_curr = &(readbuf2.bh_first);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100389 readbuf2.bh_space = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390 }
391}
392
393/*
394 * Return TRUE if the stuff buffer is empty.
395 */
396 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100397stuff_empty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398{
Bram Moolenaar285e3352018-04-18 23:01:13 +0200399 return (readbuf1.bh_first.b_next == NULL
400 && readbuf2.bh_first.b_next == NULL);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100401}
402
Bram Moolenaar113e1072019-01-20 15:30:40 +0100403#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100404/*
405 * Return TRUE if readbuf1 is empty. There may still be redo characters in
406 * redbuf2.
407 */
408 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100409readbuf1_empty(void)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100410{
Bram Moolenaar285e3352018-04-18 23:01:13 +0200411 return (readbuf1.bh_first.b_next == NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100413#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414
415/*
416 * Set a typeahead character that won't be flushed.
417 */
418 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100419typeahead_noflush(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420{
421 typeahead_char = c;
422}
423
424/*
425 * Remove the contents of the stuff buffer and the mapped characters in the
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100426 * typeahead buffer (used in case of an error). If "flush_typeahead" is true,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427 * flush all typeahead characters (used when interrupted by a CTRL-C).
428 */
429 void
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200430flush_buffers(flush_buffers_T flush_typeahead)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431{
432 init_typebuf();
433
434 start_stuff();
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100435 while (read_readbuffers(TRUE) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436 ;
437
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200438 if (flush_typeahead == FLUSH_MINIMAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000439 {
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200440 // remove mapped characters at the start only
441 typebuf.tb_off += typebuf.tb_maplen;
442 typebuf.tb_len -= typebuf.tb_maplen;
Bram Moolenaare49b4bb2020-03-11 13:01:40 +0100443#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
444 if (typebuf.tb_len == 0)
445 typebuf_was_filled = FALSE;
446#endif
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200447 }
448 else
449 {
450 // remove typeahead
451 if (flush_typeahead == FLUSH_INPUT)
452 // We have to get all characters, because we may delete the first
453 // part of an escape sequence. In an xterm we get one char at a
454 // time and we have to get them all.
455 while (inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 10L) != 0)
456 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457 typebuf.tb_off = MAXMAPLEN;
458 typebuf.tb_len = 0;
Bram Moolenaar4eb65312017-06-24 18:49:00 +0200459#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar30613902019-12-01 22:11:18 +0100460 // Reset the flag that text received from a client or from feedkeys()
461 // was inserted in the typeahead buffer.
Bram Moolenaar4eb65312017-06-24 18:49:00 +0200462 typebuf_was_filled = FALSE;
463#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 typebuf.tb_maplen = 0;
466 typebuf.tb_silent = 0;
467 cmd_silent = FALSE;
468 typebuf.tb_no_abbr_cnt = 0;
Bram Moolenaarb8d732e2020-08-05 22:07:26 +0200469 if (++typebuf.tb_change_cnt == 0)
470 typebuf.tb_change_cnt = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471}
472
473/*
474 * The previous contents of the redo buffer is kept in old_redobuffer.
475 * This is used for the CTRL-O <.> command in insert mode.
476 */
477 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100478ResetRedobuff(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479{
480 if (!block_redo)
481 {
482 free_buff(&old_redobuff);
483 old_redobuff = redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200484 redobuff.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485 }
486}
487
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100488/*
489 * Discard the contents of the redo buffer and restore the previous redo
490 * buffer.
491 */
492 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100493CancelRedo(void)
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100494{
495 if (!block_redo)
496 {
497 free_buff(&redobuff);
498 redobuff = old_redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200499 old_redobuff.bh_first.b_next = NULL;
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100500 start_stuff();
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100501 while (read_readbuffers(TRUE) != NUL)
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100502 ;
503 }
504}
505
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506/*
507 * Save redobuff and old_redobuff to save_redobuff and save_old_redobuff.
508 * Used before executing autocommands and user functions.
509 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 void
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200511saveRedobuff(save_redo_T *save_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512{
513 char_u *s;
514
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200515 save_redo->sr_redobuff = redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200516 redobuff.bh_first.b_next = NULL;
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200517 save_redo->sr_old_redobuff = old_redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200518 old_redobuff.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519
Bram Moolenaar30613902019-12-01 22:11:18 +0100520 // Make a copy, so that ":normal ." in a function works.
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200521 s = get_buffcont(&save_redo->sr_redobuff, FALSE);
522 if (s != NULL)
523 {
524 add_buff(&redobuff, s, -1L);
525 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 }
527}
528
529/*
530 * Restore redobuff and old_redobuff from save_redobuff and save_old_redobuff.
531 * Used after executing autocommands and user functions.
532 */
533 void
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200534restoreRedobuff(save_redo_T *save_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535{
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200536 free_buff(&redobuff);
537 redobuff = save_redo->sr_redobuff;
538 free_buff(&old_redobuff);
539 old_redobuff = save_redo->sr_old_redobuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541
542/*
543 * Append "s" to the redo buffer.
544 * K_SPECIAL and CSI should already have been escaped.
545 */
546 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100547AppendToRedobuff(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548{
549 if (!block_redo)
550 add_buff(&redobuff, s, -1L);
551}
552
553/*
554 * Append to Redo buffer literally, escaping special characters with CTRL-V.
555 * K_SPECIAL and CSI are escaped as well.
556 */
557 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100558AppendToRedobuffLit(
559 char_u *str,
Bram Moolenaar30613902019-12-01 22:11:18 +0100560 int len) // length of "str" or -1 for up to the NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561{
Bram Moolenaarebefac62005-12-28 22:39:57 +0000562 char_u *s = str;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 int c;
564 char_u *start;
565
566 if (block_redo)
567 return;
568
Bram Moolenaarebefac62005-12-28 22:39:57 +0000569 while (len < 0 ? *s != NUL : s - str < len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100571 // Put a string of normal characters in the redo buffer (that's
572 // faster).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 start = s;
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000574 while (*s >= ' ' && *s < DEL && (len < 0 || s - str < len))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 ++s;
576
Bram Moolenaar30613902019-12-01 22:11:18 +0100577 // Don't put '0' or '^' as last character, just in case a CTRL-D is
578 // typed next.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 if (*s == NUL && (s[-1] == '0' || s[-1] == '^'))
580 --s;
581 if (s > start)
582 add_buff(&redobuff, start, (long)(s - start));
583
Bram Moolenaarebefac62005-12-28 22:39:57 +0000584 if (*s == NUL || (len >= 0 && s - str >= len))
585 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586
Bram Moolenaar30613902019-12-01 22:11:18 +0100587 // Handle a special or multibyte character.
Bram Moolenaarebefac62005-12-28 22:39:57 +0000588 if (has_mbyte)
Bram Moolenaar30613902019-12-01 22:11:18 +0100589 // Handle composing chars separately.
Bram Moolenaarebefac62005-12-28 22:39:57 +0000590 c = mb_cptr2char_adv(&s);
591 else
Bram Moolenaarebefac62005-12-28 22:39:57 +0000592 c = *s++;
593 if (c < ' ' || c == DEL || (*s == NUL && (c == '0' || c == '^')))
594 add_char_buff(&redobuff, Ctrl_V);
595
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000596 // CTRL-V '0' must be inserted as CTRL-V 048
Bram Moolenaarebefac62005-12-28 22:39:57 +0000597 if (*s == NUL && c == '0')
Bram Moolenaarebefac62005-12-28 22:39:57 +0000598 add_buff(&redobuff, (char_u *)"048", 3L);
Bram Moolenaarebefac62005-12-28 22:39:57 +0000599 else
600 add_char_buff(&redobuff, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 }
602}
603
604/*
605 * Append a character to the redo buffer.
606 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
607 */
608 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100609AppendCharToRedobuff(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610{
611 if (!block_redo)
612 add_char_buff(&redobuff, c);
613}
614
615/*
616 * Append a number to the redo buffer.
617 */
618 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100619AppendNumberToRedobuff(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620{
621 if (!block_redo)
622 add_num_buff(&redobuff, n);
623}
624
625/*
626 * Append string "s" to the stuff buffer.
627 * CSI and K_SPECIAL must already have been escaped.
628 */
629 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100630stuffReadbuff(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100632 add_buff(&readbuf1, s, -1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633}
634
Bram Moolenaar4f5ce332014-07-30 16:00:58 +0200635/*
636 * Append string "s" to the redo stuff buffer.
637 * CSI and K_SPECIAL must already have been escaped.
638 */
639 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100640stuffRedoReadbuff(char_u *s)
Bram Moolenaar4f5ce332014-07-30 16:00:58 +0200641{
642 add_buff(&readbuf2, s, -1L);
643}
644
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200645 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100646stuffReadbuffLen(char_u *s, long len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100648 add_buff(&readbuf1, s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649}
650
651#if defined(FEAT_EVAL) || defined(PROTO)
652/*
653 * Stuff "s" into the stuff buffer, leaving special key codes unmodified and
654 * escaping other K_SPECIAL and CSI bytes.
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200655 * Change CR, LF and ESC into a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 */
657 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100658stuffReadbuffSpec(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659{
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200660 int c;
661
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 while (*s != NUL)
663 {
664 if (*s == K_SPECIAL && s[1] != NUL && s[2] != NUL)
665 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100666 // Insert special key literally.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 stuffReadbuffLen(s, 3L);
668 s += 3;
669 }
670 else
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200671 {
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200672 c = mb_ptr2char_adv(&s);
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200673 if (c == CAR || c == NL || c == ESC)
674 c = ' ';
675 stuffcharReadbuff(c);
676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 }
678}
679#endif
680
681/*
682 * Append a character to the stuff buffer.
683 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
684 */
685 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100686stuffcharReadbuff(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100688 add_char_buff(&readbuf1, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689}
690
691/*
692 * Append a number to the stuff buffer.
693 */
694 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100695stuffnumReadbuff(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100697 add_num_buff(&readbuf1, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698}
699
700/*
Bram Moolenaar11abd092020-05-01 14:26:37 +0200701 * Stuff a string into the typeahead buffer, such that edit() will insert it
702 * literally ("literally" TRUE) or interpret is as typed characters.
703 */
704 void
705stuffescaped(char_u *arg, int literally)
706{
707 int c;
708 char_u *start;
709
710 while (*arg != NUL)
711 {
712 // Stuff a sequence of normal ASCII characters, that's fast. Also
713 // stuff K_SPECIAL to get the effect of a special key when "literally"
714 // is TRUE.
715 start = arg;
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000716 while ((*arg >= ' ' && *arg < DEL)
Bram Moolenaar11abd092020-05-01 14:26:37 +0200717 || (*arg == K_SPECIAL && !literally))
718 ++arg;
719 if (arg > start)
720 stuffReadbuffLen(start, (long)(arg - start));
721
722 // stuff a single special character
723 if (*arg != NUL)
724 {
725 if (has_mbyte)
726 c = mb_cptr2char_adv(&arg);
727 else
728 c = *arg++;
729 if (literally && ((c < ' ' && c != TAB) || c == DEL))
730 stuffcharReadbuff(Ctrl_V);
731 stuffcharReadbuff(c);
732 }
733 }
734}
735
736/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 * Read a character from the redo buffer. Translates K_SPECIAL, CSI and
738 * multibyte characters.
739 * The redo buffer is left as it is.
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100740 * If init is TRUE, prepare for redo, return FAIL if nothing to redo, OK
741 * otherwise.
742 * If old is TRUE, use old_redobuff instead of redobuff.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 */
744 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100745read_redo(int init, int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100747 static buffblock_T *bp;
748 static char_u *p;
749 int c;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100750 int n;
751 char_u buf[MB_MAXBYTES + 1];
752 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753
754 if (init)
755 {
756 if (old_redo)
Bram Moolenaar285e3352018-04-18 23:01:13 +0200757 bp = old_redobuff.bh_first.b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 else
Bram Moolenaar285e3352018-04-18 23:01:13 +0200759 bp = redobuff.bh_first.b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 if (bp == NULL)
761 return FAIL;
762 p = bp->b_str;
763 return OK;
764 }
765 if ((c = *p) != NUL)
766 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100767 // Reverse the conversion done by add_char_buff()
768 // For a multi-byte character get all the bytes and return the
769 // converted character.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 if (has_mbyte && (c != K_SPECIAL || p[1] == KS_SPECIAL))
771 n = MB_BYTE2LEN_CHECK(c);
772 else
773 n = 1;
774 for (i = 0; ; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100776 if (c == K_SPECIAL) // special key or escaped K_SPECIAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 {
778 c = TO_SPECIAL(p[1], p[2]);
779 p += 2;
780 }
781#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +0100782 if (c == CSI) // escaped CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 p += 2;
784#endif
785 if (*++p == NUL && bp->b_next != NULL)
786 {
787 bp = bp->b_next;
788 p = bp->b_str;
789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 buf[i] = c;
Bram Moolenaar30613902019-12-01 22:11:18 +0100791 if (i == n - 1) // last byte of a character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 {
793 if (n != 1)
794 c = (*mb_ptr2char)(buf);
795 break;
796 }
797 c = *p;
Bram Moolenaar30613902019-12-01 22:11:18 +0100798 if (c == NUL) // cannot happen?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 }
801 }
802
803 return c;
804}
805
806/*
807 * Copy the rest of the redo buffer into the stuff buffer (in a slow way).
808 * If old_redo is TRUE, use old_redobuff instead of redobuff.
809 * The escaped K_SPECIAL and CSI are copied without translation.
810 */
811 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100812copy_redo(int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813{
814 int c;
815
816 while ((c = read_redo(FALSE, old_redo)) != NUL)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100817 add_char_buff(&readbuf2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818}
819
820/*
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100821 * Stuff the redo buffer into readbuf2.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 * Insert the redo count into the command.
823 * If "old_redo" is TRUE, the last but one command is repeated
824 * instead of the last command (inserting text). This is used for
825 * CTRL-O <.> in insert mode
826 *
827 * return FAIL for failure, OK otherwise
828 */
829 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100830start_redo(long count, int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831{
832 int c;
833
Bram Moolenaar30613902019-12-01 22:11:18 +0100834 // init the pointers; return if nothing to redo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 if (read_redo(TRUE, old_redo) == FAIL)
836 return FAIL;
837
838 c = read_redo(FALSE, old_redo);
839
Bram Moolenaar30613902019-12-01 22:11:18 +0100840 // copy the buffer name, if present
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 if (c == '"')
842 {
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100843 add_buff(&readbuf2, (char_u *)"\"", 1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 c = read_redo(FALSE, old_redo);
845
Bram Moolenaar30613902019-12-01 22:11:18 +0100846 // if a numbered buffer is used, increment the number
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 if (c >= '1' && c < '9')
848 ++c;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100849 add_char_buff(&readbuf2, c);
Bram Moolenaar833093b2018-05-23 21:53:52 +0200850
Bram Moolenaar30613902019-12-01 22:11:18 +0100851 // the expression register should be re-evaluated
Bram Moolenaar833093b2018-05-23 21:53:52 +0200852 if (c == '=')
853 {
854 add_char_buff(&readbuf2, CAR);
855 cmd_silent = TRUE;
856 }
857
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 c = read_redo(FALSE, old_redo);
859 }
860
Bram Moolenaar30613902019-12-01 22:11:18 +0100861 if (c == 'v') // redo Visual
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 {
863 VIsual = curwin->w_cursor;
864 VIsual_active = TRUE;
865 VIsual_select = FALSE;
866 VIsual_reselect = TRUE;
867 redo_VIsual_busy = TRUE;
868 c = read_redo(FALSE, old_redo);
869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870
Bram Moolenaar30613902019-12-01 22:11:18 +0100871 // try to enter the count (in place of a previous count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 if (count)
873 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100874 while (VIM_ISDIGIT(c)) // skip "old" count
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 c = read_redo(FALSE, old_redo);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100876 add_num_buff(&readbuf2, count);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 }
878
Bram Moolenaar30613902019-12-01 22:11:18 +0100879 // copy from the redo buffer into the stuff buffer
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100880 add_char_buff(&readbuf2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 copy_redo(old_redo);
882 return OK;
883}
884
885/*
886 * Repeat the last insert (R, o, O, a, A, i or I command) by stuffing
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100887 * the redo buffer into readbuf2.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 * return FAIL for failure, OK otherwise
889 */
890 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100891start_redo_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892{
893 int c;
894
895 if (read_redo(TRUE, FALSE) == FAIL)
896 return FAIL;
897 start_stuff();
898
Bram Moolenaar30613902019-12-01 22:11:18 +0100899 // skip the count and the command character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 while ((c = read_redo(FALSE, FALSE)) != NUL)
901 {
902 if (vim_strchr((char_u *)"AaIiRrOo", c) != NULL)
903 {
904 if (c == 'O' || c == 'o')
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100905 add_buff(&readbuf2, NL_STR, -1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 break;
907 }
908 }
909
Bram Moolenaar30613902019-12-01 22:11:18 +0100910 // copy the typed text from the redo buffer into the stuff buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 copy_redo(FALSE);
912 block_redo = TRUE;
913 return OK;
914}
915
916 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100917stop_redo_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918{
919 block_redo = FALSE;
920}
921
922/*
923 * Initialize typebuf.tb_buf to point to typebuf_init.
924 * alloc() cannot be used here: In out-of-memory situations it would
925 * be impossible to type anything.
926 */
927 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100928init_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929{
930 if (typebuf.tb_buf == NULL)
931 {
932 typebuf.tb_buf = typebuf_init;
933 typebuf.tb_noremap = noremapbuf_init;
934 typebuf.tb_buflen = TYPELEN_INIT;
935 typebuf.tb_len = 0;
Bram Moolenaard34f9b12017-04-08 18:41:13 +0200936 typebuf.tb_off = MAXMAPLEN + 4;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 typebuf.tb_change_cnt = 1;
938 }
939}
940
941/*
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200942 * Returns TRUE when keys cannot be remapped.
943 */
944 int
945noremap_keys(void)
946{
947 return KeyNoremap & (RM_NONE|RM_SCRIPT);
948}
949
950/*
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100951 * Insert a string in position 'offset' in the typeahead buffer (for "@r"
952 * and ":normal" command, vgetorpeek() and check_termcode()).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 *
954 * If noremap is REMAP_YES, new string can be mapped again.
955 * If noremap is REMAP_NONE, new string cannot be mapped again.
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000956 * If noremap is REMAP_SKIP, first char of new string cannot be mapped again,
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000957 * but abbreviations are allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 * If noremap is REMAP_SCRIPT, new string cannot be mapped again, except for
959 * script-local mappings.
960 * If noremap is > 0, that many characters of the new string cannot be mapped.
961 *
962 * If nottyped is TRUE, the string does not return KeyTyped (don't use when
963 * offset is non-zero!).
964 *
965 * If silent is TRUE, cmd_silent is set when the characters are obtained.
966 *
967 * return FAIL for failure, OK otherwise
968 */
969 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100970ins_typebuf(
971 char_u *str,
972 int noremap,
973 int offset,
974 int nottyped,
975 int silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976{
977 char_u *s1, *s2;
978 int newlen;
979 int addlen;
980 int i;
981 int newoff;
982 int val;
983 int nrm;
984
985 init_typebuf();
986 if (++typebuf.tb_change_cnt == 0)
987 typebuf.tb_change_cnt = 1;
Bram Moolenaard103ee72019-09-18 21:15:31 +0200988 state_no_longer_safe("ins_typebuf()");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989
990 addlen = (int)STRLEN(str);
Bram Moolenaar4e86cba2007-05-06 11:56:32 +0000991
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 if (offset == 0 && addlen <= typebuf.tb_off)
993 {
Bram Moolenaarcaa55b62017-01-10 13:51:09 +0100994 /*
995 * Easy case: there is room in front of typebuf.tb_buf[typebuf.tb_off]
996 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 typebuf.tb_off -= addlen;
998 mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen);
999 }
Bram Moolenaard34f9b12017-04-08 18:41:13 +02001000 else if (typebuf.tb_len == 0 && typebuf.tb_buflen
1001 >= addlen + 3 * (MAXMAPLEN + 4))
1002 {
1003 /*
1004 * Buffer is empty and string fits in the existing buffer.
1005 * Leave some space before and after, if possible.
1006 */
1007 typebuf.tb_off = (typebuf.tb_buflen - addlen - 3 * (MAXMAPLEN + 4)) / 2;
1008 mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen);
1009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 else
1011 {
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001012 int extra;
1013
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001014 /*
1015 * Need to allocate a new buffer.
Bram Moolenaard34f9b12017-04-08 18:41:13 +02001016 * In typebuf.tb_buf there must always be room for 3 * (MAXMAPLEN + 4)
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001017 * characters. We add some extra room to avoid having to allocate too
1018 * often.
1019 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 newoff = MAXMAPLEN + 4;
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001021 extra = addlen + newoff + 4 * (MAXMAPLEN + 4);
1022 if (typebuf.tb_len > 2147483647 - extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 {
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001024 // string is getting too long for a 32 bit int
Bram Moolenaar12f3c1b2021-12-05 21:46:34 +00001025 emsg(_(e_command_too_complex)); // also calls flush_buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 setcursor();
1027 return FAIL;
1028 }
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001029 newlen = typebuf.tb_len + extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 s1 = alloc(newlen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001031 if (s1 == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 return FAIL;
1033 s2 = alloc(newlen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001034 if (s2 == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 {
1036 vim_free(s1);
1037 return FAIL;
1038 }
1039 typebuf.tb_buflen = newlen;
1040
Bram Moolenaar30613902019-12-01 22:11:18 +01001041 // copy the old chars, before the insertion point
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 mch_memmove(s1 + newoff, typebuf.tb_buf + typebuf.tb_off,
1043 (size_t)offset);
Bram Moolenaar30613902019-12-01 22:11:18 +01001044 // copy the new chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 mch_memmove(s1 + newoff + offset, str, (size_t)addlen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001046 // copy the old chars, after the insertion point, including the NUL at
1047 // the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 mch_memmove(s1 + newoff + offset + addlen,
1049 typebuf.tb_buf + typebuf.tb_off + offset,
1050 (size_t)(typebuf.tb_len - offset + 1));
1051 if (typebuf.tb_buf != typebuf_init)
1052 vim_free(typebuf.tb_buf);
1053 typebuf.tb_buf = s1;
1054
1055 mch_memmove(s2 + newoff, typebuf.tb_noremap + typebuf.tb_off,
1056 (size_t)offset);
1057 mch_memmove(s2 + newoff + offset + addlen,
1058 typebuf.tb_noremap + typebuf.tb_off + offset,
1059 (size_t)(typebuf.tb_len - offset));
1060 if (typebuf.tb_noremap != noremapbuf_init)
1061 vim_free(typebuf.tb_noremap);
1062 typebuf.tb_noremap = s2;
1063
1064 typebuf.tb_off = newoff;
1065 }
1066 typebuf.tb_len += addlen;
1067
Bram Moolenaar30613902019-12-01 22:11:18 +01001068 // If noremap == REMAP_SCRIPT: do remap script-local mappings.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 if (noremap == REMAP_SCRIPT)
1070 val = RM_SCRIPT;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001071 else if (noremap == REMAP_SKIP)
1072 val = RM_ABBR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 else
1074 val = RM_NONE;
1075
1076 /*
1077 * Adjust typebuf.tb_noremap[] for the new characters:
1078 * If noremap == REMAP_NONE or REMAP_SCRIPT: new characters are
1079 * (sometimes) not remappable
1080 * If noremap == REMAP_YES: all the new characters are mappable
1081 * If noremap > 0: "noremap" characters are not remappable, the rest
1082 * mappable
1083 */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001084 if (noremap == REMAP_SKIP)
1085 nrm = 1;
1086 else if (noremap < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 nrm = addlen;
1088 else
1089 nrm = noremap;
1090 for (i = 0; i < addlen; ++i)
1091 typebuf.tb_noremap[typebuf.tb_off + i + offset] =
1092 (--nrm >= 0) ? val : RM_YES;
1093
Bram Moolenaar30613902019-12-01 22:11:18 +01001094 // tb_maplen and tb_silent only remember the length of mapped and/or
1095 // silent mappings at the start of the buffer, assuming that a mapped
1096 // sequence doesn't result in typed characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 if (nottyped || typebuf.tb_maplen > offset)
1098 typebuf.tb_maplen += addlen;
1099 if (silent || typebuf.tb_silent > offset)
1100 {
1101 typebuf.tb_silent += addlen;
1102 cmd_silent = TRUE;
1103 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001104 if (typebuf.tb_no_abbr_cnt && offset == 0) // and not used for abbrev.s
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 typebuf.tb_no_abbr_cnt += addlen;
1106
1107 return OK;
1108}
1109
1110/*
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001111 * Put character "c" back into the typeahead buffer.
1112 * Can be used for a character obtained by vgetc() that needs to be put back.
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00001113 * Uses cmd_silent, KeyTyped and KeyNoremap to restore the flags belonging to
1114 * the char.
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001115 * Returns the length of what was inserted.
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001116 */
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001117 int
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001118ins_char_typebuf(int c, int modifier)
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001119{
zeertzjq6cac7702022-01-04 18:01:21 +00001120 char_u buf[MB_MAXBYTES * 3 + 4];
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001121 int len = 0;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001122
1123 if (modifier != 0)
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001124 {
1125 buf[0] = K_SPECIAL;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001126 buf[1] = KS_MODIFIER;
1127 buf[2] = modifier;
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001128 buf[3] = NUL;
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001129 len = 3;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001130 }
1131 if (IS_SPECIAL(c))
1132 {
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001133 buf[len] = K_SPECIAL;
1134 buf[len + 1] = K_SECOND(c);
1135 buf[len + 2] = K_THIRD(c);
1136 buf[len + 3] = NUL;
1137 len += 3;
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001138 }
1139 else
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001140 {
zeertzjq646bb722022-02-16 17:51:47 +00001141 char_u *end = add_char2buf(c, buf + len);
1142 *end = NUL;
1143 len = end - buf;
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001144 }
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00001145 (void)ins_typebuf(buf, KeyNoremap, 0, !KeyTyped, cmd_silent);
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001146 return len;
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001147}
1148
1149/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 * Return TRUE if the typeahead buffer was changed (while waiting for a
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001151 * character to arrive). Happens when a message was received from a client or
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001152 * from feedkeys().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 * But check in a more generic way to avoid trouble: When "typebuf.tb_buf"
1154 * changed it was reallocated and the old pointer can no longer be used.
1155 * Or "typebuf.tb_off" may have been changed and we would overwrite characters
1156 * that was just added.
1157 */
1158 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001159typebuf_changed(
Bram Moolenaar30613902019-12-01 22:11:18 +01001160 int tb_change_cnt) // old value of typebuf.tb_change_cnt
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161{
1162 return (tb_change_cnt != 0 && (typebuf.tb_change_cnt != tb_change_cnt
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001163#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
1164 || typebuf_was_filled
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165#endif
1166 ));
1167}
1168
1169/*
1170 * Return TRUE if there are no characters in the typeahead buffer that have
1171 * not been typed (result from a mapping or come from ":normal").
1172 */
1173 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001174typebuf_typed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175{
1176 return typebuf.tb_maplen == 0;
1177}
1178
1179/*
1180 * Return the number of characters that are mapped (or not typed).
1181 */
1182 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001183typebuf_maplen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184{
1185 return typebuf.tb_maplen;
1186}
1187
1188/*
1189 * remove "len" characters from typebuf.tb_buf[typebuf.tb_off + offset]
1190 */
1191 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001192del_typebuf(int len, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193{
1194 int i;
1195
1196 if (len == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01001197 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198
1199 typebuf.tb_len -= len;
1200
1201 /*
1202 * Easy case: Just increase typebuf.tb_off.
1203 */
1204 if (offset == 0 && typebuf.tb_buflen - (typebuf.tb_off + len)
1205 >= 3 * MAXMAPLEN + 3)
1206 typebuf.tb_off += len;
1207 /*
1208 * Have to move the characters in typebuf.tb_buf[] and typebuf.tb_noremap[]
1209 */
1210 else
1211 {
1212 i = typebuf.tb_off + offset;
1213 /*
1214 * Leave some extra room at the end to avoid reallocation.
1215 */
1216 if (typebuf.tb_off > MAXMAPLEN)
1217 {
1218 mch_memmove(typebuf.tb_buf + MAXMAPLEN,
1219 typebuf.tb_buf + typebuf.tb_off, (size_t)offset);
1220 mch_memmove(typebuf.tb_noremap + MAXMAPLEN,
1221 typebuf.tb_noremap + typebuf.tb_off, (size_t)offset);
1222 typebuf.tb_off = MAXMAPLEN;
1223 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001224 // adjust typebuf.tb_buf (include the NUL at the end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset,
1226 typebuf.tb_buf + i + len,
1227 (size_t)(typebuf.tb_len - offset + 1));
Bram Moolenaar30613902019-12-01 22:11:18 +01001228 // adjust typebuf.tb_noremap[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 mch_memmove(typebuf.tb_noremap + typebuf.tb_off + offset,
1230 typebuf.tb_noremap + i + len,
1231 (size_t)(typebuf.tb_len - offset));
1232 }
1233
Bram Moolenaar30613902019-12-01 22:11:18 +01001234 if (typebuf.tb_maplen > offset) // adjust tb_maplen
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 {
1236 if (typebuf.tb_maplen < offset + len)
1237 typebuf.tb_maplen = offset;
1238 else
1239 typebuf.tb_maplen -= len;
1240 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001241 if (typebuf.tb_silent > offset) // adjust tb_silent
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 {
1243 if (typebuf.tb_silent < offset + len)
1244 typebuf.tb_silent = offset;
1245 else
1246 typebuf.tb_silent -= len;
1247 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001248 if (typebuf.tb_no_abbr_cnt > offset) // adjust tb_no_abbr_cnt
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 {
1250 if (typebuf.tb_no_abbr_cnt < offset + len)
1251 typebuf.tb_no_abbr_cnt = offset;
1252 else
1253 typebuf.tb_no_abbr_cnt -= len;
1254 }
1255
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001256#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar30613902019-12-01 22:11:18 +01001257 // Reset the flag that text received from a client or from feedkeys()
1258 // was inserted in the typeahead buffer.
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001259 typebuf_was_filled = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260#endif
1261 if (++typebuf.tb_change_cnt == 0)
1262 typebuf.tb_change_cnt = 1;
1263}
1264
1265/*
1266 * Write typed characters to script file.
1267 * If recording is on put the character in the recordbuffer.
1268 */
1269 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001270gotchars(char_u *chars, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271{
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001272 char_u *s = chars;
1273 int i;
1274 static char_u buf[4];
1275 static int buflen = 0;
1276 int todo = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277
Bram Moolenaar4e86cba2007-05-06 11:56:32 +00001278 while (todo--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 {
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001280 buf[buflen++] = *s++;
1281
1282 // When receiving a special key sequence, store it until we have all
1283 // the bytes and we can decide what to do with it.
1284 if (buflen == 1 && buf[0] == K_SPECIAL)
1285 continue;
1286 if (buflen == 2)
1287 continue;
1288 if (buflen == 3 && buf[1] == KS_EXTRA
1289 && (buf[2] == KE_FOCUSGAINED || buf[2] == KE_FOCUSLOST))
1290 {
1291 // Drop K_FOCUSGAINED and K_FOCUSLOST, they are not useful in a
1292 // recording.
1293 buflen = 0;
1294 continue;
1295 }
1296
Bram Moolenaar30613902019-12-01 22:11:18 +01001297 // Handle one byte at a time; no translation to be done.
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001298 for (i = 0; i < buflen; ++i)
1299 updatescript(buf[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001301 if (reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 {
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001303 buf[buflen] = NUL;
1304 add_buff(&recordbuff, buf, (long)buflen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001305 // remember how many chars were last recorded
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001306 last_recorded_len += buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 }
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001308 buflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 }
1310 may_sync_undo();
1311
1312#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01001313 // output "debug mode" message next time in debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 debug_did_msg = FALSE;
1315#endif
1316
Bram Moolenaar30613902019-12-01 22:11:18 +01001317 // Since characters have been typed, consider the following to be in
1318 // another mapping. Search string will be kept in history.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 ++maptick;
1320}
1321
1322/*
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001323 * Undo the last gotchars() for "len" bytes. To be used when putting a typed
1324 * character back into the typeahead buffer, thus gotchars() will be called
1325 * again.
1326 * Only affects recorded characters.
1327 */
1328 void
1329ungetchars(int len)
1330{
1331 if (reg_recording != 0)
1332 {
1333 delete_buff_tail(&recordbuff, len);
1334 last_recorded_len -= len;
1335 }
1336}
1337
1338/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 * Sync undo. Called when typed characters are obtained from the typeahead
1340 * buffer, or when a menu is used.
1341 * Do not sync:
1342 * - In Insert mode, unless cursor key has been used.
1343 * - While reading a script file.
1344 * - When no_u_sync is non-zero.
1345 */
1346 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001347may_sync_undo(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348{
1349 if ((!(State & (INSERT + CMDLINE)) || arrow_used)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001350 && scriptin[curscript] == NULL)
1351 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352}
1353
1354/*
1355 * Make "typebuf" empty and allocate new buffers.
1356 * Returns FAIL when out of memory.
1357 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001358 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001359alloc_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360{
1361 typebuf.tb_buf = alloc(TYPELEN_INIT);
1362 typebuf.tb_noremap = alloc(TYPELEN_INIT);
1363 if (typebuf.tb_buf == NULL || typebuf.tb_noremap == NULL)
1364 {
1365 free_typebuf();
1366 return FAIL;
1367 }
1368 typebuf.tb_buflen = TYPELEN_INIT;
Bram Moolenaar30613902019-12-01 22:11:18 +01001369 typebuf.tb_off = MAXMAPLEN + 4; // can insert without realloc
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 typebuf.tb_len = 0;
1371 typebuf.tb_maplen = 0;
1372 typebuf.tb_silent = 0;
1373 typebuf.tb_no_abbr_cnt = 0;
1374 if (++typebuf.tb_change_cnt == 0)
1375 typebuf.tb_change_cnt = 1;
Bram Moolenaare49b4bb2020-03-11 13:01:40 +01001376#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
1377 typebuf_was_filled = FALSE;
1378#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 return OK;
1380}
1381
1382/*
1383 * Free the buffers of "typebuf".
1384 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001385 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001386free_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387{
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001388 if (typebuf.tb_buf == typebuf_init)
Bram Moolenaar95f09602016-11-10 20:01:45 +01001389 internal_error("Free typebuf 1");
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001390 else
Bram Moolenaar0f1c6702019-09-28 15:24:00 +02001391 VIM_CLEAR(typebuf.tb_buf);
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00001392 if (typebuf.tb_noremap == noremapbuf_init)
Bram Moolenaar95f09602016-11-10 20:01:45 +01001393 internal_error("Free typebuf 2");
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001394 else
Bram Moolenaar0f1c6702019-09-28 15:24:00 +02001395 VIM_CLEAR(typebuf.tb_noremap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396}
1397
1398/*
1399 * When doing ":so! file", the current typeahead needs to be saved, and
1400 * restored when "file" has been read completely.
1401 */
1402static typebuf_T saved_typebuf[NSCRIPT];
1403
1404 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001405save_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406{
1407 init_typebuf();
1408 saved_typebuf[curscript] = typebuf;
Bram Moolenaar30613902019-12-01 22:11:18 +01001409 // If out of memory: restore typebuf and close file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 if (alloc_typebuf() == FAIL)
1411 {
1412 closescript();
1413 return FAIL;
1414 }
1415 return OK;
1416}
1417
Bram Moolenaar30613902019-12-01 22:11:18 +01001418static int old_char = -1; // character put back by vungetc()
1419static int old_mod_mask; // mod_mask for ungotten character
1420static int old_mouse_row; // mouse_row related to old_char
1421static int old_mouse_col; // mouse_col related to old_char
zeertzjq0f68e6c2022-04-05 13:17:01 +01001422static int old_KeyStuffed; // whether old_char was stuffed
1423
1424static int can_get_old_char()
1425{
1426 // If the old character was not stuffed and characters have been added to
1427 // the stuff buffer, need to first get the stuffed characters instead.
1428 return old_char != -1 && (old_KeyStuffed || stuff_empty());
1429}
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001430
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431/*
1432 * Save all three kinds of typeahead, so that the user must type at a prompt.
1433 */
1434 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001435save_typeahead(tasave_T *tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436{
1437 tp->save_typebuf = typebuf;
1438 tp->typebuf_valid = (alloc_typebuf() == OK);
1439 if (!tp->typebuf_valid)
1440 typebuf = tp->save_typebuf;
1441
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001442 tp->old_char = old_char;
1443 tp->old_mod_mask = old_mod_mask;
1444 old_char = -1;
1445
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001446 tp->save_readbuf1 = readbuf1;
Bram Moolenaar285e3352018-04-18 23:01:13 +02001447 readbuf1.bh_first.b_next = NULL;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001448 tp->save_readbuf2 = readbuf2;
Bram Moolenaar285e3352018-04-18 23:01:13 +02001449 readbuf2.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450# ifdef USE_INPUT_BUF
1451 tp->save_inputbuf = get_input_buf();
1452# endif
1453}
1454
1455/*
1456 * Restore the typeahead to what it was before calling save_typeahead().
1457 * The allocated memory is freed, can only be called once!
Bram Moolenaarc41badb2021-06-07 22:04:52 +02001458 * When "overwrite" is FALSE input typed later is kept.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 */
1460 void
Bram Moolenaarc41badb2021-06-07 22:04:52 +02001461restore_typeahead(tasave_T *tp, int overwrite UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462{
1463 if (tp->typebuf_valid)
1464 {
1465 free_typebuf();
1466 typebuf = tp->save_typebuf;
1467 }
1468
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001469 old_char = tp->old_char;
1470 old_mod_mask = tp->old_mod_mask;
1471
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001472 free_buff(&readbuf1);
1473 readbuf1 = tp->save_readbuf1;
1474 free_buff(&readbuf2);
1475 readbuf2 = tp->save_readbuf2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476# ifdef USE_INPUT_BUF
Bram Moolenaarc41badb2021-06-07 22:04:52 +02001477 set_input_buf(tp->save_inputbuf, overwrite);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478# endif
1479}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480
1481/*
1482 * Open a new script file for the ":source!" command.
1483 */
1484 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001485openscript(
1486 char_u *name,
Bram Moolenaar30613902019-12-01 22:11:18 +01001487 int directly) // when TRUE execute directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488{
1489 if (curscript + 1 == NSCRIPT)
1490 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001491 emsg(_(e_scripts_nested_too_deep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 return;
1493 }
Bram Moolenaar53575522019-05-22 22:38:25 +02001494
1495 // Disallow sourcing a file in the sandbox, the commands would be executed
1496 // later, possibly outside of the sandbox.
1497 if (check_secure())
1498 return;
1499
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001500#ifdef FEAT_EVAL
1501 if (ignore_script)
Bram Moolenaar30613902019-12-01 22:11:18 +01001502 // Not reading from script, also don't open one. Warning message?
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001503 return;
1504#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505
Bram Moolenaar30613902019-12-01 22:11:18 +01001506 if (scriptin[curscript] != NULL) // already reading script
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 ++curscript;
Bram Moolenaar30613902019-12-01 22:11:18 +01001508 // use NameBuff for expanded name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 expand_env(name, NameBuff, MAXPATHL);
1510 if ((scriptin[curscript] = mch_fopen((char *)NameBuff, READBIN)) == NULL)
1511 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001512 semsg(_(e_cant_open_file_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 if (curscript)
1514 --curscript;
1515 return;
1516 }
1517 if (save_typebuf() == FAIL)
1518 return;
1519
1520 /*
1521 * Execute the commands from the file right now when using ":source!"
1522 * after ":global" or ":argdo" or in a loop. Also when another command
1523 * follows. This means the display won't be updated. Don't do this
1524 * always, "make test" would fail.
1525 */
1526 if (directly)
1527 {
1528 oparg_T oa;
1529 int oldcurscript;
1530 int save_State = State;
1531 int save_restart_edit = restart_edit;
1532 int save_insertmode = p_im;
1533 int save_finish_op = finish_op;
1534 int save_msg_scroll = msg_scroll;
1535
1536 State = NORMAL;
Bram Moolenaar30613902019-12-01 22:11:18 +01001537 msg_scroll = FALSE; // no msg scrolling in Normal mode
1538 restart_edit = 0; // don't go to Insert mode
1539 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 clear_oparg(&oa);
1541 finish_op = FALSE;
1542
1543 oldcurscript = curscript;
1544 do
1545 {
Bram Moolenaar386b43e2019-05-19 21:57:11 +02001546 update_topline_cursor(); // update cursor position and topline
1547 normal_cmd(&oa, FALSE); // execute one command
Bram Moolenaarae97b942020-07-09 19:16:35 +02001548 (void)vpeekc(); // check for end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 }
1550 while (scriptin[oldcurscript] != NULL);
1551
1552 State = save_State;
1553 msg_scroll = save_msg_scroll;
1554 restart_edit = save_restart_edit;
1555 p_im = save_insertmode;
1556 finish_op = save_finish_op;
1557 }
1558}
1559
1560/*
1561 * Close the currently active input script.
1562 */
1563 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001564closescript(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565{
1566 free_typebuf();
1567 typebuf = saved_typebuf[curscript];
1568
1569 fclose(scriptin[curscript]);
1570 scriptin[curscript] = NULL;
1571 if (curscript > 0)
1572 --curscript;
1573}
1574
Bram Moolenaarea408852005-06-25 22:49:46 +00001575#if defined(EXITFREE) || defined(PROTO)
1576 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001577close_all_scripts(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00001578{
1579 while (scriptin[0] != NULL)
1580 closescript();
1581}
1582#endif
1583
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584/*
1585 * Return TRUE when reading keys from a script file.
1586 */
1587 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001588using_script(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589{
1590 return scriptin[curscript] != NULL;
1591}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592
1593/*
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001594 * This function is called just before doing a blocking wait. Thus after
1595 * waiting 'updatetime' for a character to arrive.
1596 */
1597 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001598before_blocking(void)
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001599{
1600 updatescript(0);
1601#ifdef FEAT_EVAL
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001602 if (may_garbage_collect)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001603 garbage_collect(FALSE);
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001604#endif
1605}
1606
1607/*
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001608 * updatescript() is called when a character can be written into the script file
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 * or when we have waited some time for a character (c == 0)
1610 *
1611 * All the changed memfiles are synced if c == 0 or when the number of typed
1612 * characters reaches 'updatecount' and 'updatecount' is non-zero.
1613 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001614 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001615updatescript(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616{
1617 static int count = 0;
1618
1619 if (c && scriptout)
1620 putc(c, scriptout);
1621 if (c == 0 || (p_uc > 0 && ++count >= p_uc))
1622 {
1623 ml_sync_all(c == 0, TRUE);
1624 count = 0;
1625 }
1626}
1627
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628/*
Bram Moolenaar975a8802020-06-06 22:36:24 +02001629 * Convert "c" plus "modifiers" to merge the effect of modifyOtherKeys into the
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001630 * character.
1631 */
1632 int
Bram Moolenaar975a8802020-06-06 22:36:24 +02001633merge_modifyOtherKeys(int c_arg, int *modifiers)
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001634{
1635 int c = c_arg;
1636
Bram Moolenaar975a8802020-06-06 22:36:24 +02001637 if (*modifiers & MOD_MASK_CTRL)
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001638 {
1639 if ((c >= '`' && c <= 0x7f) || (c >= '@' && c <= '_'))
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001640 c &= 0x1f;
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001641 else if (c == '6')
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001642 // CTRL-6 is equivalent to CTRL-^
1643 c = 0x1e;
Bram Moolenaarf4ae6b22020-05-30 19:52:46 +02001644#ifdef FEAT_GUI_GTK
1645 // These mappings look arbitrary at the first glance, but in fact
1646 // resemble quite exactly the behaviour of the GTK+ 1.2 GUI on my
1647 // machine. The only difference is BS vs. DEL for CTRL-8 (makes
1648 // more sense and is consistent with usual terminal behaviour).
1649 else if (c == '2')
1650 c = NUL;
1651 else if (c >= '3' && c <= '7')
1652 c = c ^ 0x28;
1653 else if (c == '8')
1654 c = BS;
1655 else if (c == '?')
1656 c = DEL;
1657#endif
1658 if (c != c_arg)
Bram Moolenaar975a8802020-06-06 22:36:24 +02001659 *modifiers &= ~MOD_MASK_CTRL;
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001660 }
Bram Moolenaar975a8802020-06-06 22:36:24 +02001661 if ((*modifiers & (MOD_MASK_META | MOD_MASK_ALT))
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001662 && c >= 0 && c <= 127)
1663 {
1664 c += 0x80;
Bram Moolenaar975a8802020-06-06 22:36:24 +02001665 *modifiers &= ~(MOD_MASK_META|MOD_MASK_ALT);
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001666 }
1667 return c;
1668}
1669
1670/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 * Get the next input character.
1672 * Can return a special key or a multi-byte character.
1673 * Can return NUL when called recursively, use safe_vgetc() if that's not
1674 * wanted.
1675 * This translates escaped K_SPECIAL and CSI bytes to a K_SPECIAL or CSI byte.
1676 * Collects the bytes of a multibyte character into the whole character.
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00001677 * Returns the modifiers in the global "mod_mask".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 */
1679 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001680vgetc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681{
1682 int c, c2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001684 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001687#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01001688 // Do garbage collection when garbagecollect() was called previously and
1689 // we are now at the toplevel.
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001690 if (may_garbage_collect && want_garbage_collect)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001691 garbage_collect(FALSE);
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001692#endif
1693
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 /*
1695 * If a character was put back with vungetc, it was already processed.
1696 * Return it directly.
1697 */
zeertzjq0f68e6c2022-04-05 13:17:01 +01001698 if (can_get_old_char())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 {
1700 c = old_char;
1701 old_char = -1;
1702 mod_mask = old_mod_mask;
Bram Moolenaarb8978712013-03-16 21:42:16 +01001703 mouse_row = old_mouse_row;
1704 mouse_col = old_mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001706 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 {
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001708 mod_mask = 0;
1709 vgetc_mod_mask = 0;
1710 vgetc_char = 0;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001711 last_recorded_len = 0;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001712
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001713 for (;;) // this is done twice if there are modifiers
1714 {
1715 int did_inc = FALSE;
Bram Moolenaar2455c4e2015-09-15 18:29:39 +02001716
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001717 if (mod_mask
Bram Moolenaar39719052017-09-05 22:20:46 +02001718#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001719 || im_is_preediting()
Bram Moolenaar39719052017-09-05 22:20:46 +02001720#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001721#if defined(FEAT_PROP_POPUP)
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001722 || popup_no_mapping()
1723#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001724 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 {
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001726 // no mapping after modifier has been read
1727 ++no_mapping;
1728 ++allow_keys;
1729 did_inc = TRUE; // mod_mask may change value
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 }
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01001731 c = vgetorpeek(TRUE);
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001732 if (did_inc)
1733 {
1734 --no_mapping;
1735 --allow_keys;
1736 }
1737
1738 // Get two extra bytes for special keys
1739 if (c == K_SPECIAL
1740#ifdef FEAT_GUI
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001741 || (c == CSI)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001742#endif
1743 )
1744 {
1745 int save_allow_keys = allow_keys;
1746
1747 ++no_mapping;
1748 allow_keys = 0; // make sure BS is not found
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01001749 c2 = vgetorpeek(TRUE); // no mapping for these chars
1750 c = vgetorpeek(TRUE);
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001751 --no_mapping;
1752 allow_keys = save_allow_keys;
1753 if (c2 == KS_MODIFIER)
1754 {
1755 mod_mask = c;
1756 continue;
1757 }
1758 c = TO_SPECIAL(c2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759
Bram Moolenaar4f974752019-02-17 17:44:42 +01001760#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001761 // Handle K_TEAROFF here, the caller of vgetc() doesn't need to
1762 // know that a menu was torn off
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001763 if (
1764# ifdef VIMDLL
1765 gui.in_use &&
1766# endif
1767 c == K_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 {
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001769 char_u name[200];
K.Takata54119102022-02-03 13:33:03 +00001770 int j;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001771
1772 // get menu path, it ends with a <CR>
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01001773 for (j = 0; (c = vgetorpeek(TRUE)) != '\r'; )
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001774 {
K.Takata54119102022-02-03 13:33:03 +00001775 name[j] = c;
1776 if (j < 199)
1777 ++j;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001778 }
K.Takata54119102022-02-03 13:33:03 +00001779 name[j] = NUL;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001780 gui_make_tearoff(name);
1781 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783#endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001784#if defined(FEAT_GUI) && defined(FEAT_GUI_GTK) && defined(FEAT_MENU)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001785 // GTK: <F10> normally selects the menu, but it's passed until
1786 // here to allow mapping it. Intercept and invoke the GTK
1787 // behavior if it's not mapped.
1788 if (c == K_F10 && gui.menubar != NULL)
1789 {
1790 gtk_menu_shell_select_first(
1791 GTK_MENU_SHELL(gui.menubar), FALSE);
1792 continue;
1793 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001794#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795#ifdef FEAT_GUI
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001796 // Handle focus event here, so that the caller doesn't need to
1797 // know about it. Return K_IGNORE so that we loop once (needed
1798 // if 'lazyredraw' is set).
1799 if (c == K_FOCUSGAINED || c == K_FOCUSLOST)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001800 {
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001801 ui_focus_change(c == K_FOCUSGAINED);
1802 c = K_IGNORE;
Bram Moolenaara9dd2d32019-04-29 21:58:41 +02001803 }
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001804
1805 // Translate K_CSI to CSI. The special key is only used to
1806 // avoid it being recognized as the start of a special key.
1807 if (c == K_CSI)
1808 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001810 }
1811 // a keypad or special function key was not mapped, use it like
1812 // its ASCII equivalent
1813 switch (c)
1814 {
1815 case K_KPLUS: c = '+'; break;
1816 case K_KMINUS: c = '-'; break;
1817 case K_KDIVIDE: c = '/'; break;
1818 case K_KMULTIPLY: c = '*'; break;
1819 case K_KENTER: c = CAR; break;
1820 case K_KPOINT:
Bram Moolenaar4f974752019-02-17 17:44:42 +01001821#ifdef MSWIN
Bram Moolenaar8e85db02018-07-27 23:16:51 +02001822 // Can be either '.' or a ',',
1823 // depending on the type of keypad.
1824 c = MapVirtualKey(VK_DECIMAL, 2); break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001825#else
Bram Moolenaar8e85db02018-07-27 23:16:51 +02001826 c = '.'; break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001827#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001828 case K_K0: c = '0'; break;
1829 case K_K1: c = '1'; break;
1830 case K_K2: c = '2'; break;
1831 case K_K3: c = '3'; break;
1832 case K_K4: c = '4'; break;
1833 case K_K5: c = '5'; break;
1834 case K_K6: c = '6'; break;
1835 case K_K7: c = '7'; break;
1836 case K_K8: c = '8'; break;
1837 case K_K9: c = '9'; break;
Bram Moolenaara88d9682005-03-25 21:45:43 +00001838
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001839 case K_XHOME:
1840 case K_ZHOME: if (mod_mask == MOD_MASK_SHIFT)
Bram Moolenaara88d9682005-03-25 21:45:43 +00001841 {
1842 c = K_S_HOME;
1843 mod_mask = 0;
1844 }
1845 else if (mod_mask == MOD_MASK_CTRL)
1846 {
1847 c = K_C_HOME;
1848 mod_mask = 0;
1849 }
1850 else
1851 c = K_HOME;
1852 break;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001853 case K_XEND:
1854 case K_ZEND: if (mod_mask == MOD_MASK_SHIFT)
Bram Moolenaara88d9682005-03-25 21:45:43 +00001855 {
1856 c = K_S_END;
1857 mod_mask = 0;
1858 }
1859 else if (mod_mask == MOD_MASK_CTRL)
1860 {
1861 c = K_C_END;
1862 mod_mask = 0;
1863 }
1864 else
1865 c = K_END;
1866 break;
1867
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001868 case K_XUP: c = K_UP; break;
1869 case K_XDOWN: c = K_DOWN; break;
1870 case K_XLEFT: c = K_LEFT; break;
1871 case K_XRIGHT: c = K_RIGHT; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001874 // For a multi-byte character get all the bytes and return the
1875 // converted character.
1876 // Note: This will loop until enough bytes are received!
1877 if (has_mbyte && (n = MB_BYTE2LEN_CHECK(c)) > 1)
1878 {
1879 ++no_mapping;
1880 buf[0] = c;
1881 for (i = 1; i < n; ++i)
1882 {
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01001883 buf[i] = vgetorpeek(TRUE);
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001884 if (buf[i] == K_SPECIAL
1885#ifdef FEAT_GUI
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001886 || (buf[i] == CSI)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001887#endif
1888 )
1889 {
1890 // Must be a K_SPECIAL - KS_SPECIAL - KE_FILLER
1891 // sequence, which represents a K_SPECIAL (0x80),
1892 // or a CSI - KS_EXTRA - KE_CSI sequence, which
1893 // represents a CSI (0x9B),
1894 // or a K_SPECIAL - KS_EXTRA - KE_CSI, which is CSI
1895 // too.
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01001896 c = vgetorpeek(TRUE);
1897 if (vgetorpeek(TRUE) == KE_CSI && c == KS_EXTRA)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001898 buf[i] = CSI;
1899 }
1900 }
1901 --no_mapping;
1902 c = (*mb_ptr2char)(buf);
1903 }
1904
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02001905 if (vgetc_char == 0)
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001906 {
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001907 vgetc_mod_mask = mod_mask;
1908 vgetc_char = c;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001909 }
Bram Moolenaar00eab7f2019-10-10 21:49:28 +02001910
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001911 break;
1912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001914
1915#ifdef FEAT_EVAL
1916 /*
1917 * In the main loop "may_garbage_collect" can be set to do garbage
1918 * collection in the first next vgetc(). It's disabled after that to
1919 * avoid internally used Lists and Dicts to be freed.
1920 */
1921 may_garbage_collect = FALSE;
1922#endif
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001923
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001924#ifdef FEAT_BEVAL_TERM
Bram Moolenaarc2f50542019-07-05 23:24:56 +02001925 if (c != K_MOUSEMOVE && c != K_IGNORE && c != K_CURSORHOLD)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001926 {
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001927 // Don't trigger 'balloonexpr' unless only the mouse was moved.
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001928 bevalexpr_due_set = FALSE;
1929 ui_remove_balloon();
1930 }
1931#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001932#ifdef FEAT_PROP_POPUP
Bram Moolenaar4934ad02020-09-28 22:29:58 +02001933 // Only filter keys that do not come from ":normal". Keys from feedkeys()
1934 // are filtered.
1935 if ((!ex_normal_busy || in_feedkeys) && popup_do_filter(c))
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02001936 {
1937 if (c == Ctrl_C)
1938 got_int = FALSE; // avoid looping
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001939 c = K_IGNORE;
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02001940 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001941#endif
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001942
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001943 // Need to process the character before we know it's safe to do something
1944 // else.
1945 if (c != K_IGNORE)
Bram Moolenaard103ee72019-09-18 21:15:31 +02001946 state_no_longer_safe("key typed");
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001947
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001948 return c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949}
1950
1951/*
1952 * Like vgetc(), but never return a NUL when called recursively, get a key
1953 * directly from the user (ignoring typeahead).
1954 */
1955 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001956safe_vgetc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957{
1958 int c;
1959
1960 c = vgetc();
1961 if (c == NUL)
1962 c = get_keystroke();
1963 return c;
1964}
1965
1966/*
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001967 * Like safe_vgetc(), but loop to handle K_IGNORE.
1968 * Also ignore scrollbar events.
1969 */
1970 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001971plain_vgetc(void)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001972{
1973 int c;
1974
1975 do
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001976 c = safe_vgetc();
Bram Moolenaar3a006592021-05-05 19:58:17 +02001977 while (c == K_IGNORE
1978 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
1979 || c == K_MOUSEMOVE);
Bram Moolenaarec2da362017-01-21 20:04:22 +01001980
1981 if (c == K_PS)
Bram Moolenaar30613902019-12-01 22:11:18 +01001982 // Only handle the first pasted character. Drop the rest, since we
1983 // don't know what to do with it.
Bram Moolenaarec2da362017-01-21 20:04:22 +01001984 c = bracketed_paste(PASTE_ONE_CHAR, FALSE, NULL);
1985
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001986 return c;
1987}
1988
1989/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 * Check if a character is available, such that vgetc() will not block.
1991 * If the next character is a special character or multi-byte, the returned
1992 * character is not valid!.
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02001993 * Returns NUL if no character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 */
1995 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001996vpeekc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997{
zeertzjq0f68e6c2022-04-05 13:17:01 +01001998 if (can_get_old_char())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 return old_char;
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01002000 return vgetorpeek(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001}
2002
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01002003#if defined(FEAT_TERMRESPONSE) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004/*
2005 * Like vpeekc(), but don't allow mapping. Do allow checking for terminal
2006 * codes.
2007 */
2008 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002009vpeekc_nomap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010{
2011 int c;
2012
2013 ++no_mapping;
2014 ++allow_keys;
2015 c = vpeekc();
2016 --no_mapping;
2017 --allow_keys;
2018 return c;
2019}
2020#endif
2021
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022/*
2023 * Check if any character is available, also half an escape sequence.
2024 * Trick: when no typeahead found, but there is something in the typeahead
2025 * buffer, it must be an ESC that is recognized as the start of a key code.
2026 */
2027 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002028vpeekc_any(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029{
2030 int c;
2031
2032 c = vpeekc();
2033 if (c == NUL && typebuf.tb_len > 0)
2034 c = ESC;
2035 return c;
2036}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037
2038/*
2039 * Call vpeekc() without causing anything to be mapped.
2040 * Return TRUE if a character is available, FALSE otherwise.
2041 */
2042 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002043char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044{
2045 int retval;
2046
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01002047#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01002048 // When test_override("char_avail", 1) was called pretend there is no
2049 // typeahead.
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01002050 if (disable_char_avail_for_testing)
2051 return FALSE;
2052#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 ++no_mapping;
2054 retval = vpeekc();
2055 --no_mapping;
2056 return (retval != NUL);
2057}
2058
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002059#if defined(FEAT_EVAL) || defined(PROTO)
2060/*
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002061 * "getchar()" and "getcharstr()" functions
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002062 */
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002063 static void
2064getchar_common(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002065{
2066 varnumber_T n;
2067 int error = FALSE;
2068
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002069 if (in_vim9script() && check_for_opt_bool_arg(argvars, 0) == FAIL)
2070 return;
2071
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002072#ifdef MESSAGE_QUEUE
2073 // vpeekc() used to check for messages, but that caused problems, invoking
2074 // a callback where it was not expected. Some plugins use getchar(1) in a
2075 // loop to await a message, therefore make sure we check for messages here.
2076 parse_queued_messages();
2077#endif
2078
Bram Moolenaar30613902019-12-01 22:11:18 +01002079 // Position the cursor. Needed after a message that ends in a space.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002080 windgoto(msg_row, msg_col);
2081
2082 ++no_mapping;
2083 ++allow_keys;
2084 for (;;)
2085 {
2086 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar30613902019-12-01 22:11:18 +01002087 // getchar(): blocking wait.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002088 n = plain_vgetc();
Bram Moolenaarc08cc722020-09-05 17:51:23 +02002089 else if (tv_get_bool_chk(&argvars[0], &error))
Bram Moolenaar30613902019-12-01 22:11:18 +01002090 // getchar(1): only check if char avail
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002091 n = vpeekc_any();
2092 else if (error || vpeekc_any() == NUL)
Bram Moolenaar30613902019-12-01 22:11:18 +01002093 // illegal argument or getchar(0) and no char avail: return zero
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002094 n = 0;
2095 else
Bram Moolenaar15183b42020-09-05 19:59:39 +02002096 // getchar(0) and char avail() != NUL: get a character.
2097 // Note that vpeekc_any() returns K_SPECIAL for K_IGNORE.
2098 n = safe_vgetc();
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002099
Bram Moolenaar15183b42020-09-05 19:59:39 +02002100 if (n == K_IGNORE || n == K_MOUSEMOVE
2101 || n == K_VER_SCROLLBAR || n == K_HOR_SCROLLBAR)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002102 continue;
2103 break;
2104 }
2105 --no_mapping;
2106 --allow_keys;
2107
2108 set_vim_var_nr(VV_MOUSE_WIN, 0);
2109 set_vim_var_nr(VV_MOUSE_WINID, 0);
2110 set_vim_var_nr(VV_MOUSE_LNUM, 0);
2111 set_vim_var_nr(VV_MOUSE_COL, 0);
2112
2113 rettv->vval.v_number = n;
zeertzjqad6c45f2022-02-20 19:05:10 +00002114 if (n != 0 && (IS_SPECIAL(n) || mod_mask != 0))
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002115 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002116 char_u temp[10]; // modifier: 3, mbyte-char: 6, NUL: 1
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002117 int i = 0;
2118
Bram Moolenaar30613902019-12-01 22:11:18 +01002119 // Turn a special key into three bytes, plus modifier.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002120 if (mod_mask != 0)
2121 {
2122 temp[i++] = K_SPECIAL;
2123 temp[i++] = KS_MODIFIER;
2124 temp[i++] = mod_mask;
2125 }
2126 if (IS_SPECIAL(n))
2127 {
2128 temp[i++] = K_SPECIAL;
2129 temp[i++] = K_SECOND(n);
2130 temp[i++] = K_THIRD(n);
2131 }
2132 else if (has_mbyte)
2133 i += (*mb_char2bytes)(n, temp + i);
2134 else
2135 temp[i++] = n;
2136 temp[i++] = NUL;
2137 rettv->v_type = VAR_STRING;
2138 rettv->vval.v_string = vim_strsave(temp);
2139
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002140 if (is_mouse_key(n))
2141 {
2142 int row = mouse_row;
2143 int col = mouse_col;
2144 win_T *win;
2145 linenr_T lnum;
2146 win_T *wp;
2147 int winnr = 1;
2148
2149 if (row >= 0 && col >= 0)
2150 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002151 // Find the window at the mouse coordinates and compute the
2152 // text position.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002153 win = mouse_find_win(&row, &col, FIND_POPUP);
2154 if (win == NULL)
2155 return;
2156 (void)mouse_comp_pos(win, &row, &col, &lnum, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002157#ifdef FEAT_PROP_POPUP
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002158 if (WIN_IS_POPUP(win))
2159 winnr = 0;
2160 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002161#endif
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002162 for (wp = firstwin; wp != win && wp != NULL;
2163 wp = wp->w_next)
2164 ++winnr;
2165 set_vim_var_nr(VV_MOUSE_WIN, winnr);
2166 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
2167 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
2168 set_vim_var_nr(VV_MOUSE_COL, col + 1);
2169 }
2170 }
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002171 }
2172}
2173
2174/*
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002175 * "getchar()" function
2176 */
2177 void
2178f_getchar(typval_T *argvars, typval_T *rettv)
2179{
2180 getchar_common(argvars, rettv);
2181}
2182
2183/*
2184 * "getcharstr()" function
2185 */
2186 void
2187f_getcharstr(typval_T *argvars, typval_T *rettv)
2188{
2189 getchar_common(argvars, rettv);
2190
2191 if (rettv->v_type == VAR_NUMBER)
2192 {
2193 char_u temp[7]; // mbyte-char: 6, NUL: 1
2194 varnumber_T n = rettv->vval.v_number;
2195 int i = 0;
2196
2197 if (n != 0)
2198 {
2199 if (has_mbyte)
2200 i += (*mb_char2bytes)(n, temp + i);
2201 else
2202 temp[i++] = n;
2203 }
2204 temp[i++] = NUL;
2205 rettv->v_type = VAR_STRING;
2206 rettv->vval.v_string = vim_strsave(temp);
2207 }
2208}
2209
2210/*
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002211 * "getcharmod()" function
2212 */
2213 void
2214f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
2215{
2216 rettv->vval.v_number = mod_mask;
2217}
2218#endif // FEAT_EVAL
2219
2220#if defined(MESSAGE_QUEUE) || defined(PROTO)
2221# define MAX_REPEAT_PARSE 8
2222
2223/*
2224 * Process messages that have been queued for netbeans or clientserver.
2225 * Also check if any jobs have ended.
h-east965d2ed2021-10-04 21:51:57 +01002226 * These functions can call arbitrary Vim script and should only be called when
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002227 * it is safe to do so.
2228 */
2229 void
2230parse_queued_messages(void)
2231{
Bram Moolenaar1cac7092019-10-22 21:54:31 +02002232 int old_curwin_id;
2233 int old_curbuf_fnum;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002234 int i;
2235 int save_may_garbage_collect = may_garbage_collect;
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002236 static int entered = 0;
Bram Moolenaard103ee72019-09-18 21:15:31 +02002237 int was_safe = get_was_safe_state();
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002238
2239 // Do not handle messages while redrawing, because it may cause buffers to
2240 // change or be wiped while they are being redrawn.
Bram Moolenaar4778b4d2020-11-04 11:03:12 +01002241 // Also bail out when parsing messages was explicitly disabled.
2242 if (updating_screen || dont_parse_messages)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002243 return;
2244
Bram Moolenaar1cac7092019-10-22 21:54:31 +02002245 // If memory allocation fails during startup we'll exit but curbuf or
2246 // curwin could be NULL.
2247 if (curbuf == NULL || curwin == NULL)
2248 return;
2249
2250 old_curbuf_fnum = curbuf->b_fnum;
2251 old_curwin_id = curwin->w_id;
2252
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002253 ++entered;
2254
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002255 // may_garbage_collect is set in main_loop() to do garbage collection when
2256 // blocking to wait on a character. We don't want that while parsing
2257 // messages, a callback may invoke vgetc() while lists and dicts are in use
2258 // in the call stack.
2259 may_garbage_collect = FALSE;
2260
2261 // Loop when a job ended, but don't keep looping forever.
2262 for (i = 0; i < MAX_REPEAT_PARSE; ++i)
2263 {
2264 // For Win32 mch_breakcheck() does not check for input, do it here.
Bram Moolenaar80a8d382020-05-03 22:57:32 +02002265# if (defined(MSWIN) || defined(__HAIKU__)) && defined(FEAT_JOB_CHANNEL)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002266 channel_handle_events(FALSE);
2267# endif
2268
2269# ifdef FEAT_NETBEANS_INTG
2270 // Process the queued netbeans messages.
2271 netbeans_parse_messages();
2272# endif
2273# ifdef FEAT_JOB_CHANNEL
2274 // Write any buffer lines still to be written.
2275 channel_write_any_lines();
2276
2277 // Process the messages queued on channels.
2278 channel_parse_messages();
2279# endif
2280# if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
2281 // Process the queued clientserver messages.
2282 server_parse_messages();
2283# endif
2284# ifdef FEAT_JOB_CHANNEL
2285 // Check if any jobs have ended. If so, repeat the above to handle
2286 // changes, e.g. stdin may have been closed.
2287 if (job_check_ended())
2288 continue;
2289# endif
2290# ifdef FEAT_TERMINAL
2291 free_unused_terminals();
2292# endif
2293# ifdef FEAT_SOUND_CANBERRA
2294 if (has_sound_callback_in_queue())
2295 invoke_sound_callback();
2296# endif
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002297#ifdef SIGUSR1
2298 if (got_sigusr1)
2299 {
2300 apply_autocmds(EVENT_SIGUSR1, NULL, NULL, FALSE, curbuf);
2301 got_sigusr1 = FALSE;
2302 }
2303#endif
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002304 break;
2305 }
2306
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002307 // When not nested we'll go back to waiting for a typed character. If it
2308 // was safe before then this triggers a SafeStateAgain autocommand event.
Bram Moolenaard103ee72019-09-18 21:15:31 +02002309 if (entered == 1 && was_safe)
Bram Moolenaar37d18072019-09-17 20:28:38 +02002310 may_trigger_safestateagain();
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002311
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002312 may_garbage_collect = save_may_garbage_collect;
2313
2314 // If the current window or buffer changed we need to bail out of the
2315 // waiting loop. E.g. when a job exit callback closes the terminal window.
2316 if (curwin->w_id != old_curwin_id || curbuf->b_fnum != old_curbuf_fnum)
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02002317 ins_char_typebuf(K_IGNORE, 0);
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002318
2319 --entered;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002320}
2321#endif
2322
2323
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002324typedef enum {
2325 map_result_fail, // failed, break loop
2326 map_result_get, // get a character from typeahead
2327 map_result_retry, // try to map again
2328 map_result_nomatch // no matching mapping, get char
2329} map_result_T;
2330
2331/*
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002332 * Check if the bytes at the start of the typeahead buffer are a character used
2333 * in CTRL-X mode. This includes the form with a CTRL modifier.
2334 */
2335 static int
2336at_ctrl_x_key(void)
2337{
2338 char_u *p = typebuf.tb_buf + typebuf.tb_off;
2339 int c = *p;
2340
2341 if (typebuf.tb_len > 3
Bram Moolenaard979d642022-03-04 14:51:06 +00002342 && (c == K_SPECIAL || c == CSI) // CSI is used by the GUI
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002343 && p[1] == KS_MODIFIER
2344 && (p[2] & MOD_MASK_CTRL))
2345 c = p[3] & 0x1f;
2346 return vim_is_ctrl_x_key(c);
2347}
2348
2349/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002350 * Check if typebuf.tb_buf[] contains a modifier plus key that can be changed
Bram Moolenaar975a8802020-06-06 22:36:24 +02002351 * into just a key, apply that.
2352 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
2353 * + "max_offset"].
2354 * Return the length of the replaced bytes, zero if nothing changed.
2355 */
2356 static int
2357check_simplify_modifier(int max_offset)
2358{
2359 int offset;
2360 char_u *tp;
2361
2362 for (offset = 0; offset < max_offset; ++offset)
2363 {
2364 if (offset + 3 >= typebuf.tb_len)
2365 break;
2366 tp = typebuf.tb_buf + typebuf.tb_off + offset;
2367 if (tp[0] == K_SPECIAL && tp[1] == KS_MODIFIER)
2368 {
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002369 // A modifier was not used for a mapping, apply it to ASCII keys.
2370 // Shift would already have been applied.
Bram Moolenaar975a8802020-06-06 22:36:24 +02002371 int modifier = tp[2];
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002372 int c = tp[3];
2373 int new_c = merge_modifyOtherKeys(c, &modifier);
Bram Moolenaar975a8802020-06-06 22:36:24 +02002374
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002375 if (new_c != c)
Bram Moolenaar975a8802020-06-06 22:36:24 +02002376 {
2377 char_u new_string[MB_MAXBYTES];
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002378 int len;
Bram Moolenaar975a8802020-06-06 22:36:24 +02002379
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002380 if (offset == 0)
2381 {
2382 // At the start: remember the character and mod_mask before
2383 // merging, in some cases, e.g. at the hit-return prompt,
2384 // they are put back in the typeahead buffer.
2385 vgetc_char = c;
2386 vgetc_mod_mask = tp[2];
2387 }
2388 len = mb_char2bytes(new_c, new_string);
2389 if (modifier == 0)
2390 {
2391 if (put_string_in_typebuf(offset, 4, new_string, len,
Bram Moolenaar975a8802020-06-06 22:36:24 +02002392 NULL, 0, 0) == FAIL)
2393 return -1;
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002394 }
2395 else
2396 {
2397 tp[2] = modifier;
2398 if (put_string_in_typebuf(offset + 3, 1, new_string, len,
2399 NULL, 0, 0) == FAIL)
2400 return -1;
2401 }
Bram Moolenaar975a8802020-06-06 22:36:24 +02002402 return len;
2403 }
2404 }
2405 }
2406 return 0;
2407}
2408
2409/*
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002410 * Handle mappings in the typeahead buffer.
2411 * - When something was mapped, return map_result_retry for recursive mappings.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002412 * - When nothing mapped and typeahead has a character: return map_result_get.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002413 * - When there is no match yet, return map_result_nomatch, need to get more
2414 * typeahead.
2415 */
2416 static int
2417handle_mapping(
2418 int *keylenp,
2419 int *timedout,
2420 int *mapdepth)
2421{
2422 mapblock_T *mp = NULL;
2423 mapblock_T *mp2;
2424 mapblock_T *mp_match;
2425 int mp_match_len = 0;
2426 int max_mlen = 0;
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002427 int want_termcode = 0; // 1 if termcode expected after max_mlen
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002428 int tb_c1;
2429 int mlen;
2430#ifdef FEAT_LANGMAP
2431 int nolmaplen;
2432#endif
2433 int keylen = *keylenp;
2434 int i;
2435 int local_State = get_real_state();
Bram Moolenaar1fc34222022-03-03 13:56:24 +00002436 int is_plug_map = FALSE;
2437
2438 // If typehead starts with <Plug> then remap, even for a "noremap" mapping.
2439 if (typebuf.tb_buf[typebuf.tb_off] == K_SPECIAL
2440 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_EXTRA
2441 && typebuf.tb_buf[typebuf.tb_off + 2] == KE_PLUG)
2442 is_plug_map = TRUE;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002443
2444 /*
2445 * Check for a mappable key sequence.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002446 * Walk through one maphash[] list until we find an entry that matches.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002447 *
2448 * Don't look for mappings if:
2449 * - no_mapping set: mapping disabled (e.g. for CTRL-V)
2450 * - maphash_valid not set: no mappings present.
2451 * - typebuf.tb_buf[typebuf.tb_off] should not be remapped
2452 * - in insert or cmdline mode and 'paste' option set
Bram Moolenaareda35f72019-08-03 14:59:44 +02002453 * - waiting for "hit return to continue" and CR or SPACE typed
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002454 * - waiting for a char with --more--
2455 * - in Ctrl-X mode, and we get a valid char for that mode
2456 */
2457 tb_c1 = typebuf.tb_buf[typebuf.tb_off];
2458 if (no_mapping == 0 && is_maphash_valid()
2459 && (no_zero_mapping == 0 || tb_c1 != '0')
Bram Moolenaar1fc34222022-03-03 13:56:24 +00002460 && (typebuf.tb_maplen == 0 || is_plug_map
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002461 || (p_remap
2462 && (typebuf.tb_noremap[typebuf.tb_off]
2463 & (RM_NONE|RM_ABBR)) == 0))
2464 && !(p_paste && (State & (INSERT + CMDLINE)))
2465 && !(State == HITRETURN && (tb_c1 == CAR || tb_c1 == ' '))
2466 && State != ASKMORE
2467 && State != CONFIRM
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002468 && !((ctrl_x_mode_not_default() && at_ctrl_x_key())
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002469 || (compl_status_local()
Bram Moolenaare2c453d2019-08-21 14:37:09 +02002470 && (tb_c1 == Ctrl_N || tb_c1 == Ctrl_P))))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002471 {
Bram Moolenaarc9983702020-05-28 21:03:53 +02002472#ifdef FEAT_GUI
2473 if (gui.in_use && tb_c1 == CSI && typebuf.tb_len >= 2
2474 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_MODIFIER)
2475 {
2476 // The GUI code sends CSI KS_MODIFIER {flags}, but mappings expect
2477 // K_SPECIAL KS_MODIFIER {flags}.
2478 tb_c1 = K_SPECIAL;
2479 }
2480#endif
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002481#ifdef FEAT_LANGMAP
2482 if (tb_c1 == K_SPECIAL)
2483 nolmaplen = 2;
2484 else
2485 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002486 LANGMAP_ADJUST(tb_c1, (State & (CMDLINE | INSERT)) == 0
2487 && get_real_state() != SELECTMODE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002488 nolmaplen = 0;
2489 }
2490#endif
2491 // First try buffer-local mappings.
2492 mp = get_buf_maphash_list(local_State, tb_c1);
2493 mp2 = get_maphash_list(local_State, tb_c1);
2494 if (mp == NULL)
2495 {
2496 // There are no buffer-local mappings.
2497 mp = mp2;
2498 mp2 = NULL;
2499 }
Bram Moolenaareda35f72019-08-03 14:59:44 +02002500
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002501 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002502 * Loop until a partly matching mapping is found or all (local)
2503 * mappings have been checked.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002504 * The longest full match is remembered in "mp_match".
Bram Moolenaareda35f72019-08-03 14:59:44 +02002505 * A full match is only accepted if there is no partly match, so "aa"
2506 * and "aaa" can both be mapped.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002507 */
2508 mp_match = NULL;
2509 mp_match_len = 0;
2510 for ( ; mp != NULL;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002511 mp->m_next == NULL ? (mp = mp2, mp2 = NULL) : (mp = mp->m_next))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002512 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002513 // Only consider an entry if the first character matches and it is
2514 // for the current state.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002515 // Skip ":lmap" mappings if keys were mapped.
2516 if (mp->m_keys[0] == tb_c1
2517 && (mp->m_mode & local_State)
Bram Moolenaar46cd43b2020-06-04 22:22:11 +02002518 && !(mp->m_simplified && seenModifyOtherKeys
2519 && typebuf.tb_maplen == 0)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002520 && ((mp->m_mode & LANGMAP) == 0 || typebuf.tb_maplen == 0))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002521 {
2522#ifdef FEAT_LANGMAP
2523 int nomap = nolmaplen;
2524 int c2;
2525#endif
2526 // find the match length of this mapping
2527 for (mlen = 1; mlen < typebuf.tb_len; ++mlen)
2528 {
2529#ifdef FEAT_LANGMAP
2530 c2 = typebuf.tb_buf[typebuf.tb_off + mlen];
2531 if (nomap > 0)
2532 --nomap;
2533 else if (c2 == K_SPECIAL)
2534 nomap = 2;
2535 else
2536 LANGMAP_ADJUST(c2, TRUE);
2537 if (mp->m_keys[mlen] != c2)
2538#else
2539 if (mp->m_keys[mlen] !=
Bram Moolenaarc9983702020-05-28 21:03:53 +02002540 typebuf.tb_buf[typebuf.tb_off + mlen])
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002541#endif
2542 break;
2543 }
2544
Bram Moolenaareda35f72019-08-03 14:59:44 +02002545 // Don't allow mapping the first byte(s) of a multi-byte char.
2546 // Happens when mapping <M-a> and then changing 'encoding'.
2547 // Beware that 0x80 is escaped.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002548 {
2549 char_u *p1 = mp->m_keys;
2550 char_u *p2 = mb_unescape(&p1);
2551
2552 if (has_mbyte && p2 != NULL
Bram Moolenaar1614a142019-10-06 22:00:13 +02002553 && MB_BYTE2LEN(tb_c1) > mb_ptr2len(p2))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002554 mlen = 0;
2555 }
2556
2557 // Check an entry whether it matches.
2558 // - Full match: mlen == keylen
2559 // - Partly match: mlen == typebuf.tb_len
2560 keylen = mp->m_keylen;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002561 if (mlen == keylen || (mlen == typebuf.tb_len
2562 && typebuf.tb_len < keylen))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002563 {
2564 char_u *s;
2565 int n;
2566
Bram Moolenaareda35f72019-08-03 14:59:44 +02002567 // If only script-local mappings are allowed, check if the
2568 // mapping starts with K_SNR.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002569 s = typebuf.tb_noremap + typebuf.tb_off;
2570 if (*s == RM_SCRIPT
2571 && (mp->m_keys[0] != K_SPECIAL
2572 || mp->m_keys[1] != KS_EXTRA
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01002573 || mp->m_keys[2] != KE_SNR))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002574 continue;
2575
Bram Moolenaareda35f72019-08-03 14:59:44 +02002576 // If one of the typed keys cannot be remapped, skip the
2577 // entry.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002578 for (n = mlen; --n >= 0; )
2579 if (*s++ & (RM_NONE|RM_ABBR))
2580 break;
Bram Moolenaar1fc34222022-03-03 13:56:24 +00002581 if (!is_plug_map && n >= 0)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002582 continue;
2583
2584 if (keylen > typebuf.tb_len)
2585 {
2586 if (!*timedout && !(mp_match != NULL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002587 && mp_match->m_nowait))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002588 {
2589 // break at a partly match
2590 keylen = KEYLEN_PART_MAP;
2591 break;
2592 }
2593 }
2594 else if (keylen > mp_match_len)
2595 {
2596 // found a longer match
2597 mp_match = mp;
2598 mp_match_len = keylen;
2599 }
2600 }
2601 else
Bram Moolenaareda35f72019-08-03 14:59:44 +02002602 // No match; may have to check for termcode at next
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002603 // character. If the first character that didn't match is
2604 // K_SPECIAL then check for a termcode. This isn't perfect
2605 // but should work in most cases.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002606 if (max_mlen < mlen)
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002607 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002608 max_mlen = mlen;
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002609 want_termcode = mp->m_keys[mlen] == K_SPECIAL;
2610 }
2611 else if (max_mlen == mlen && mp->m_keys[mlen] == K_SPECIAL)
2612 want_termcode = 1;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002613 }
2614 }
2615
Bram Moolenaareda35f72019-08-03 14:59:44 +02002616 // If no partly match found, use the longest full match.
Bram Moolenaar196c3852022-03-04 19:22:36 +00002617 if (keylen != KEYLEN_PART_MAP && mp_match != NULL)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002618 {
2619 mp = mp_match;
2620 keylen = mp_match_len;
2621 }
2622 }
2623
2624 /*
2625 * Check for match with 'pastetoggle'
2626 */
2627 if (*p_pt != NUL && mp == NULL && (State & (INSERT|NORMAL)))
2628 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002629 for (mlen = 0; mlen < typebuf.tb_len && p_pt[mlen]; ++mlen)
2630 if (p_pt[mlen] != typebuf.tb_buf[typebuf.tb_off + mlen])
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002631 break;
2632 if (p_pt[mlen] == NUL) // match
2633 {
2634 // write chars to script file(s)
2635 if (mlen > typebuf.tb_maplen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002636 gotchars(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_maplen,
2637 mlen - typebuf.tb_maplen);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002638
2639 del_typebuf(mlen, 0); // remove the chars
Bram Moolenaareda35f72019-08-03 14:59:44 +02002640 set_option_value((char_u *)"paste", (long)!p_paste, NULL, 0);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002641 if (!(State & INSERT))
2642 {
2643 msg_col = 0;
2644 msg_row = Rows - 1;
2645 msg_clr_eos(); // clear ruler
2646 }
2647 status_redraw_all();
2648 redraw_statuslines();
2649 showmode();
2650 setcursor();
2651 *keylenp = keylen;
2652 return map_result_retry;
2653 }
2654 // Need more chars for partly match.
2655 if (mlen == typebuf.tb_len)
2656 keylen = KEYLEN_PART_KEY;
2657 else if (max_mlen < mlen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002658 // no match, may have to check for termcode at next character
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002659 max_mlen = mlen + 1;
2660 }
2661
Bram Moolenaarbbf84e22022-03-12 13:48:39 +00002662 // May check for a terminal code when there is no mapping or only a partial
2663 // mapping. Also check if there is a full mapping with <Esc>, unless timed
2664 // out, since that is nearly always a partial match with a terminal code.
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002665 if ((mp == NULL || max_mlen + want_termcode > mp_match_len
Bram Moolenaarbbf84e22022-03-12 13:48:39 +00002666 || (mp_match_len == 1 && *mp->m_keys == ESC && !*timedout))
2667 && keylen != KEYLEN_PART_MAP)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002668 {
2669 int save_keylen = keylen;
2670
2671 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002672 * When no matching mapping found or found a non-matching mapping that
2673 * matches at least what the matching mapping matched:
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002674 * Check if we have a terminal code, when:
Bram Moolenaareda35f72019-08-03 14:59:44 +02002675 * - mapping is allowed,
2676 * - keys have not been mapped,
2677 * - and not an ESC sequence, not in insert mode or p_ek is on,
2678 * - and when not timed out,
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002679 */
2680 if ((no_mapping == 0 || allow_keys != 0)
2681 && (typebuf.tb_maplen == 0
2682 || (p_remap && typebuf.tb_noremap[
Bram Moolenaareda35f72019-08-03 14:59:44 +02002683 typebuf.tb_off] == RM_YES))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002684 && !*timedout)
2685 {
Bram Moolenaar0684e362020-12-03 19:54:42 +01002686 keylen = check_termcode(max_mlen + 1, NULL, 0, NULL);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002687
Bram Moolenaar0684e362020-12-03 19:54:42 +01002688 // If no termcode matched but 'pastetoggle' matched partially
2689 // it's like an incomplete key sequence.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002690 if (keylen == 0 && save_keylen == KEYLEN_PART_KEY)
2691 keylen = KEYLEN_PART_KEY;
2692
Bram Moolenaar975a8802020-06-06 22:36:24 +02002693 // If no termcode matched, try to include the modifier into the
zeertzjqa3f83fe2021-11-22 12:47:39 +00002694 // key. This is for when modifyOtherKeys is working.
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002695 if (keylen == 0 && !no_reduce_keys)
Bram Moolenaar975a8802020-06-06 22:36:24 +02002696 keylen = check_simplify_modifier(max_mlen + 1);
2697
Bram Moolenaareda35f72019-08-03 14:59:44 +02002698 // When getting a partial match, but the last characters were not
2699 // typed, don't wait for a typed character to complete the
2700 // termcode. This helps a lot when a ":normal" command ends in an
2701 // ESC.
2702 if (keylen < 0 && typebuf.tb_len == typebuf.tb_maplen)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002703 keylen = 0;
2704 }
2705 else
2706 keylen = 0;
2707 if (keylen == 0) // no matching terminal code
2708 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002709#ifdef AMIGA
2710 // check for window bounds report
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002711 if (typebuf.tb_maplen == 0 && (typebuf.tb_buf[
Bram Moolenaareda35f72019-08-03 14:59:44 +02002712 typebuf.tb_off] & 0xff) == CSI)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002713 {
2714 char_u *s;
2715
2716 for (s = typebuf.tb_buf + typebuf.tb_off + 1;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002717 s < typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len
2718 && (VIM_ISDIGIT(*s) || *s == ';' || *s == ' ');
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002719 ++s)
2720 ;
2721 if (*s == 'r' || *s == '|') // found one
2722 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002723 del_typebuf(
2724 (int)(s + 1 - (typebuf.tb_buf + typebuf.tb_off)), 0);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002725 // get size and redraw screen
2726 shell_resized();
2727 *keylenp = keylen;
2728 return map_result_retry;
2729 }
2730 if (*s == NUL) // need more characters
2731 keylen = KEYLEN_PART_KEY;
2732 }
2733 if (keylen >= 0)
2734#endif
Bram Moolenaareda35f72019-08-03 14:59:44 +02002735 // When there was a matching mapping and no termcode could be
2736 // replaced after another one, use that mapping (loop around).
2737 // If there was no mapping at all use the character from the
2738 // typeahead buffer right here.
2739 if (mp == NULL)
2740 {
2741 *keylenp = keylen;
Bram Moolenaar52797ba2021-12-16 14:45:13 +00002742 return map_result_get; // get character from typeahead
Bram Moolenaareda35f72019-08-03 14:59:44 +02002743 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002744 }
2745
2746 if (keylen > 0) // full matching terminal code
2747 {
2748#if defined(FEAT_GUI) && defined(FEAT_MENU)
2749 if (typebuf.tb_len >= 2
Bram Moolenaareda35f72019-08-03 14:59:44 +02002750 && typebuf.tb_buf[typebuf.tb_off] == K_SPECIAL
2751 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_MENU)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002752 {
2753 int idx;
2754
Bram Moolenaareda35f72019-08-03 14:59:44 +02002755 // Using a menu may cause a break in undo! It's like using
2756 // gotchars(), but without recording or writing to a script
2757 // file.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002758 may_sync_undo();
2759 del_typebuf(3, 0);
2760 idx = get_menu_index(current_menu, local_State);
2761 if (idx != MENU_INDEX_INVALID)
2762 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002763 // In Select mode and a Visual mode menu is used: Switch
2764 // to Visual mode temporarily. Append K_SELECT to switch
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002765 // back to Select mode.
2766 if (VIsual_active && VIsual_select
Bram Moolenaareda35f72019-08-03 14:59:44 +02002767 && (current_menu->modes & VISUAL))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002768 {
2769 VIsual_select = FALSE;
2770 (void)ins_typebuf(K_SELECT_STRING,
Bram Moolenaareda35f72019-08-03 14:59:44 +02002771 REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002772 }
2773 ins_typebuf(current_menu->strings[idx],
2774 current_menu->noremap[idx],
Bram Moolenaareda35f72019-08-03 14:59:44 +02002775 0, TRUE, current_menu->silent[idx]);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002776 }
2777 }
2778#endif // FEAT_GUI && FEAT_MENU
2779 *keylenp = keylen;
2780 return map_result_retry; // try mapping again
2781 }
2782
Bram Moolenaareda35f72019-08-03 14:59:44 +02002783 // Partial match: get some more characters. When a matching mapping
2784 // was found use that one.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002785 if (mp == NULL || keylen < 0)
2786 keylen = KEYLEN_PART_KEY;
2787 else
2788 keylen = mp_match_len;
2789 }
2790
2791 /*
2792 * complete match
2793 */
2794 if (keylen >= 0 && keylen <= typebuf.tb_len)
2795 {
2796 char_u *map_str;
2797
2798#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002799 int save_m_expr;
2800 int save_m_noremap;
2801 int save_m_silent;
2802 char_u *save_m_keys;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002803#else
2804# define save_m_noremap mp->m_noremap
2805# define save_m_silent mp->m_silent
2806#endif
2807
2808 // write chars to script file(s)
2809 if (keylen > typebuf.tb_maplen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002810 gotchars(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_maplen,
2811 keylen - typebuf.tb_maplen);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002812
2813 cmd_silent = (typebuf.tb_silent > 0);
2814 del_typebuf(keylen, 0); // remove the mapped keys
2815
2816 /*
2817 * Put the replacement string in front of mapstr.
2818 * The depth check catches ":map x y" and ":map y x".
2819 */
2820 if (++*mapdepth >= p_mmd)
2821 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00002822 emsg(_(e_recursive_mapping));
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002823 if (State & CMDLINE)
2824 redrawcmdline();
2825 else
2826 setcursor();
2827 flush_buffers(FLUSH_MINIMAL);
Bram Moolenaar30613902019-12-01 22:11:18 +01002828 *mapdepth = 0; // for next one
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002829 *keylenp = keylen;
2830 return map_result_fail;
2831 }
2832
2833 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002834 * In Select mode and a Visual mode mapping is used: Switch to Visual
2835 * mode temporarily. Append K_SELECT to switch back to Select mode.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002836 */
Bram Moolenaareda35f72019-08-03 14:59:44 +02002837 if (VIsual_active && VIsual_select && (mp->m_mode & VISUAL))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002838 {
2839 VIsual_select = FALSE;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002840 (void)ins_typebuf(K_SELECT_STRING, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002841 }
2842
2843#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002844 // Copy the values from *mp that are used, because evaluating the
2845 // expression may invoke a function that redefines the mapping, thereby
2846 // making *mp invalid.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002847 save_m_expr = mp->m_expr;
2848 save_m_noremap = mp->m_noremap;
2849 save_m_silent = mp->m_silent;
2850 save_m_keys = NULL; // only saved when needed
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002851
2852 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002853 * Handle ":map <expr>": evaluate the {rhs} as an expression. Also
2854 * save and restore the command line for "normal :".
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002855 */
2856 if (mp->m_expr)
2857 {
2858 int save_vgetc_busy = vgetc_busy;
2859 int save_may_garbage_collect = may_garbage_collect;
Bram Moolenaar4ebe0e62019-11-22 20:55:40 +01002860 int was_screen_col = screen_cur_col;
2861 int was_screen_row = screen_cur_row;
Bram Moolenaar74a0a5b2022-02-10 14:07:41 +00002862 int prev_did_emsg = did_emsg;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002863
2864 vgetc_busy = 0;
2865 may_garbage_collect = FALSE;
2866
2867 save_m_keys = vim_strsave(mp->m_keys);
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002868 map_str = eval_map_expr(mp, NUL);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002869
Bram Moolenaar4ebe0e62019-11-22 20:55:40 +01002870 // The mapping may do anything, but we expect it to take care of
2871 // redrawing. Do put the cursor back where it was.
2872 windgoto(was_screen_row, was_screen_col);
2873 out_flush();
2874
Bram Moolenaar74a0a5b2022-02-10 14:07:41 +00002875 // If an error was displayed and the expression returns an empty
2876 // string, generate a <Nop> to allow for a redraw.
2877 if (prev_did_emsg != did_emsg
2878 && (map_str == NULL || *map_str == NUL))
2879 {
2880 char_u buf[4];
2881
2882 vim_free(map_str);
2883 buf[0] = K_SPECIAL;
2884 buf[1] = KS_EXTRA;
2885 buf[2] = KE_IGNORE;
2886 buf[3] = NUL;
2887 map_str = vim_strsave(buf);
2888 if (State & CMDLINE)
2889 {
2890 // redraw the command below the error
2891 msg_didout = TRUE;
2892 if (msg_row < cmdline_row)
2893 msg_row = cmdline_row;
2894 redrawcmd();
2895 }
2896 }
2897
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002898 vgetc_busy = save_vgetc_busy;
2899 may_garbage_collect = save_may_garbage_collect;
2900 }
2901 else
2902#endif
2903 map_str = mp->m_str;
2904
2905 /*
2906 * Insert the 'to' part in the typebuf.tb_buf.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002907 * If 'from' field is the same as the start of the 'to' field, don't
2908 * remap the first character (but do allow abbreviations).
2909 * If m_noremap is set, don't remap the whole 'to' part.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002910 */
2911 if (map_str == NULL)
2912 i = FAIL;
2913 else
2914 {
2915 int noremap;
2916
2917 if (save_m_noremap != REMAP_YES)
2918 noremap = save_m_noremap;
2919 else if (
2920#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002921 STRNCMP(map_str, save_m_keys != NULL ? save_m_keys : mp->m_keys,
2922 (size_t)keylen)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002923#else
2924 STRNCMP(map_str, mp->m_keys, (size_t)keylen)
2925#endif
2926 != 0)
2927 noremap = REMAP_YES;
2928 else
2929 noremap = REMAP_SKIP;
2930 i = ins_typebuf(map_str, noremap,
2931 0, TRUE, cmd_silent || save_m_silent);
2932#ifdef FEAT_EVAL
2933 if (save_m_expr)
2934 vim_free(map_str);
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002935 last_used_map = mp;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002936#endif
2937 }
2938#ifdef FEAT_EVAL
2939 vim_free(save_m_keys);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002940#endif
2941 *keylenp = keylen;
2942 if (i == FAIL)
2943 return map_result_fail;
2944 return map_result_retry;
2945 }
2946
2947 *keylenp = keylen;
2948 return map_result_nomatch;
2949}
2950
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002951/*
2952 * unget one character (can only be done once!)
zeertzjq0f68e6c2022-04-05 13:17:01 +01002953 * If the character was stuffed, vgetc() will get it next time it was called.
2954 * Otherwise vgetc() will only get it when the stuff buffer is empty.
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002955 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002957vungetc(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958{
2959 old_char = c;
2960 old_mod_mask = mod_mask;
Bram Moolenaarb8978712013-03-16 21:42:16 +01002961 old_mouse_row = mouse_row;
2962 old_mouse_col = mouse_col;
zeertzjq0f68e6c2022-04-05 13:17:01 +01002963 old_KeyStuffed = KeyStuffed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964}
2965
2966/*
Bram Moolenaar32526b32019-01-19 17:43:09 +01002967 * Get a byte:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 * 1. from the stuffbuffer
2969 * This is used for abbreviated commands like "D" -> "d$".
2970 * Also used to redo a command for ".".
2971 * 2. from the typeahead buffer
2972 * Stores text obtained previously but not used yet.
2973 * Also stores the result of mappings.
2974 * Also used for the ":normal" command.
2975 * 3. from the user
2976 * This may do a blocking wait if "advance" is TRUE.
2977 *
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01002978 * if "advance" is TRUE (vgetc()):
Bram Moolenaard29459b2016-08-26 22:29:11 +02002979 * Really get the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 * KeyTyped is set to TRUE in the case the user typed the key.
2981 * KeyStuffed is TRUE if the character comes from the stuff buffer.
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01002982 * if "advance" is FALSE (vpeekc()):
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02002983 * Just look whether there is a character available.
2984 * Return NUL if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 *
2986 * When "no_mapping" is zero, checks for mappings in the current mode.
2987 * Only returns one byte (of a multi-byte character).
2988 * K_SPECIAL and CSI may be escaped, need to get two more bytes then.
2989 */
2990 static int
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01002991vgetorpeek(int advance)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992{
2993 int c, c1;
Bram Moolenaar30613902019-12-01 22:11:18 +01002994 int timedout = FALSE; // waited for more than 1 second
2995 // for mapping to complete
2996 int mapdepth = 0; // check for recursive mapping
2997 int mode_deleted = FALSE; // set when mode has been deleted
Bram Moolenaar4e427192006-03-10 21:34:27 +00002998#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 int new_wcol, new_wrow;
3000#endif
3001#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003002 int shape_changed = FALSE; // adjusted cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003#endif
3004 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 int old_wcol, old_wrow;
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003006 int wait_tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007
3008 /*
3009 * This function doesn't work very well when called recursively. This may
3010 * happen though, because of:
3011 * 1. The call to add_to_showcmd(). char_avail() is then used to check if
3012 * there is a character available, which calls this function. In that
3013 * case we must return NUL, to indicate no character is available.
3014 * 2. A GUI callback function writes to the screen, causing a
3015 * wait_return().
3016 * Using ":normal" can also do this, but it saves the typeahead buffer,
3017 * thus it should be OK. But don't get a key from the user then.
3018 */
Bram Moolenaare2c38102016-01-31 14:55:40 +01003019 if (vgetc_busy > 0 && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 return NUL;
3021
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003022 ++vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023
3024 if (advance)
3025 KeyStuffed = FALSE;
3026
3027 init_typebuf();
3028 start_stuff();
3029 if (advance && typebuf.tb_maplen == 0)
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02003030 reg_executing = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 do
3032 {
3033/*
3034 * get a character: 1. from the stuffbuffer
3035 */
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003036 if (typeahead_char != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 {
3038 c = typeahead_char;
3039 if (advance)
3040 typeahead_char = 0;
3041 }
3042 else
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01003043 c = read_readbuffers(advance);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 if (c != NUL && !got_int)
3045 {
3046 if (advance)
3047 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003048 // KeyTyped = FALSE; When the command that stuffed something
3049 // was typed, behave like the stuffed command was typed.
3050 // needed for CTRL-W CTRL-] to open a fold, for example.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 KeyStuffed = TRUE;
3052 }
3053 if (typebuf.tb_no_abbr_cnt == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003054 typebuf.tb_no_abbr_cnt = 1; // no abbreviations now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 }
3056 else
3057 {
3058 /*
3059 * Loop until we either find a matching mapped key, or we
3060 * are sure that it is not a mapped key.
3061 * If a mapped key sequence is found we go back to the start to
3062 * try re-mapping.
3063 */
3064 for (;;)
3065 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003066 long wait_time;
3067 int keylen = 0;
Bram Moolenaareda35f72019-08-03 14:59:44 +02003068#ifdef FEAT_CMDL_INFO
3069 int showcmd_idx;
3070#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 /*
3072 * ui_breakcheck() is slow, don't use it too often when
3073 * inside a mapping. But call it each time for typed
3074 * characters.
3075 */
3076 if (typebuf.tb_maplen)
3077 line_breakcheck();
3078 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003079 ui_breakcheck(); // check for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 if (got_int)
3081 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003082 // flush all input
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003083 c = inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 0L);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003084
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 /*
3086 * If inchar() returns TRUE (script file was active) or we
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003087 * are inside a mapping, get out of Insert mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 * Otherwise we behave like having gotten a CTRL-C.
3089 * As a result typing CTRL-C in insert mode will
3090 * really insert a CTRL-C.
3091 */
3092 if ((c || typebuf.tb_maplen)
3093 && (State & (INSERT + CMDLINE)))
3094 c = ESC;
3095 else
3096 c = Ctrl_C;
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02003097 flush_buffers(FLUSH_INPUT); // flush all typeahead
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098
Bram Moolenaard9dfd572006-10-03 13:36:13 +00003099 if (advance)
3100 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003101 // Also record this character, it might be needed to
3102 // get out of Insert mode.
Bram Moolenaard9dfd572006-10-03 13:36:13 +00003103 *typebuf.tb_buf = c;
3104 gotchars(typebuf.tb_buf, 1);
3105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 cmd_silent = FALSE;
3107
3108 break;
3109 }
3110 else if (typebuf.tb_len > 0)
3111 {
3112 /*
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003113 * Check for a mapping in "typebuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 */
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003115 map_result_T result = handle_mapping(
3116 &keylen, &timedout, &mapdepth);
3117
3118 if (result == map_result_retry)
3119 // try mapping again
3120 continue;
3121 if (result == map_result_fail)
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003122 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003123 // failed, use the outer loop
3124 c = -1;
3125 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003127 if (result == map_result_get)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129/*
3130 * get a character: 2. from the typeahead buffer
3131 */
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003132 c = typebuf.tb_buf[typebuf.tb_off];
Bram Moolenaar30613902019-12-01 22:11:18 +01003133 if (advance) // remove chars from tb_buf
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003134 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003135 cmd_silent = (typebuf.tb_silent > 0);
3136 if (typebuf.tb_maplen > 0)
3137 KeyTyped = FALSE;
3138 else
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003139 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003140 KeyTyped = TRUE;
Bram Moolenaar30613902019-12-01 22:11:18 +01003141 // write char to script file(s)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003142 gotchars(typebuf.tb_buf
3143 + typebuf.tb_off, 1);
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003144 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003145 KeyNoremap = typebuf.tb_noremap[
3146 typebuf.tb_off];
3147 del_typebuf(1, 0);
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003148 }
Bram Moolenaar52797ba2021-12-16 14:45:13 +00003149 break; // got character, break the for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 }
3151
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003152 // not enough characters, get more
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 }
3154
3155/*
3156 * get a character: 3. from the user - handle <Esc> in Insert mode
3157 */
3158 /*
Bram Moolenaarf085f422017-06-07 20:39:47 +02003159 * Special case: if we get an <ESC> in insert mode and there
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 * are no more characters at once, we pretend to go out of
3161 * insert mode. This prevents the one second delay after
3162 * typing an <ESC>. If we get something after all, we may
3163 * have to redisplay the mode. That the cursor is in the wrong
3164 * place does not matter.
3165 */
3166 c = 0;
3167#ifdef FEAT_CMDL_INFO
3168 new_wcol = curwin->w_wcol;
3169 new_wrow = curwin->w_wrow;
3170#endif
3171 if ( advance
3172 && typebuf.tb_len == 1
3173 && typebuf.tb_buf[typebuf.tb_off] == ESC
3174 && !no_mapping
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 && ex_normal_busy == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 && typebuf.tb_maplen == 0
3177 && (State & INSERT)
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003178 && (p_timeout
3179 || (keylen == KEYLEN_PART_KEY && p_ttimeout))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 && (c = inchar(typebuf.tb_buf + typebuf.tb_off
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003181 + typebuf.tb_len, 3, 25L)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 {
3183 colnr_T col = 0, vcol;
3184 char_u *ptr;
3185
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003186 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 {
3188 unshowmode(TRUE);
3189 mode_deleted = TRUE;
3190 }
3191#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003192 // may show a different cursor shape
Bram Moolenaarf085f422017-06-07 20:39:47 +02003193 if (gui.in_use && State != NORMAL && !cmd_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 {
3195 int save_State;
3196
3197 save_State = State;
3198 State = NORMAL;
3199 gui_update_cursor(TRUE, FALSE);
3200 State = save_State;
3201 shape_changed = TRUE;
3202 }
3203#endif
3204 validate_cursor();
3205 old_wcol = curwin->w_wcol;
3206 old_wrow = curwin->w_wrow;
3207
Bram Moolenaar30613902019-12-01 22:11:18 +01003208 // move cursor left, if possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 if (curwin->w_cursor.col != 0)
3210 {
3211 if (curwin->w_wcol > 0)
3212 {
3213 if (did_ai)
3214 {
3215 /*
3216 * We are expecting to truncate the trailing
3217 * white-space, so find the last non-white
3218 * character -- webb
3219 */
3220 col = vcol = curwin->w_wcol = 0;
3221 ptr = ml_get_curline();
3222 while (col < curwin->w_cursor.col)
3223 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01003224 if (!VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 curwin->w_wcol = vcol;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003226 vcol += lbr_chartabsize(ptr, ptr + col,
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01003227 vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003229 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 ++col;
3232 }
3233 curwin->w_wrow = curwin->w_cline_row
Bram Moolenaar02631462017-09-22 15:20:32 +02003234 + curwin->w_wcol / curwin->w_width;
3235 curwin->w_wcol %= curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 curwin->w_wcol += curwin_col_off();
Bram Moolenaar30613902019-12-01 22:11:18 +01003237 col = 0; // no correction needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 }
3239 else
3240 {
3241 --curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 col = curwin->w_cursor.col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 }
3244 }
3245 else if (curwin->w_p_wrap && curwin->w_wrow)
3246 {
3247 --curwin->w_wrow;
Bram Moolenaar02631462017-09-22 15:20:32 +02003248 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 col = curwin->w_cursor.col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 if (has_mbyte && col > 0 && curwin->w_wcol > 0)
3252 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003253 // Correct when the cursor is on the right halve
3254 // of a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 ptr = ml_get_curline();
3256 col -= (*mb_head_off)(ptr, ptr + col);
3257 if ((*mb_ptr2cells)(ptr + col) > 1)
3258 --curwin->w_wcol;
3259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 }
3261 setcursor();
3262 out_flush();
3263#ifdef FEAT_CMDL_INFO
3264 new_wcol = curwin->w_wcol;
3265 new_wrow = curwin->w_wrow;
3266#endif
3267 curwin->w_wcol = old_wcol;
3268 curwin->w_wrow = old_wrow;
3269 }
3270 if (c < 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003271 continue; // end of input script reached
Bram Moolenaar20c38922014-07-23 20:41:14 +02003272
Bram Moolenaar30613902019-12-01 22:11:18 +01003273 // Allow mapping for just typed characters. When we get here c
3274 // is the number of extra bytes and typebuf.tb_len is 1.
Bram Moolenaar20c38922014-07-23 20:41:14 +02003275 for (n = 1; n <= c; ++n)
3276 typebuf.tb_noremap[typebuf.tb_off + n] = RM_YES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 typebuf.tb_len += c;
3278
Bram Moolenaar30613902019-12-01 22:11:18 +01003279 // buffer full, don't map
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 if (typebuf.tb_len >= typebuf.tb_maplen + MAXMAPLEN)
3281 {
3282 timedout = TRUE;
3283 continue;
3284 }
3285
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 if (ex_normal_busy > 0)
3287 {
Bram Moolenaare2c38102016-01-31 14:55:40 +01003288#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 static int tc = 0;
Bram Moolenaare2c38102016-01-31 14:55:40 +01003290#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291
Bram Moolenaar30613902019-12-01 22:11:18 +01003292 // No typeahead left and inside ":normal". Must return
3293 // something to avoid getting stuck. When an incomplete
3294 // mapping is present, behave like it timed out.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 if (typebuf.tb_len > 0)
3296 {
3297 timedout = TRUE;
3298 continue;
3299 }
Bram Moolenaar189832b2020-09-23 12:29:11 +02003300
Bram Moolenaar30613902019-12-01 22:11:18 +01003301 // When 'insertmode' is set, ESC just beeps in Insert
3302 // mode. Use CTRL-L to make edit() return.
3303 // For the command line only CTRL-C always breaks it.
3304 // For the cmdline window: Alternate between ESC and
3305 // CTRL-C: ESC for most situations and CTRL-C to close the
3306 // cmdline window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 if (p_im && (State & INSERT))
3308 c = Ctrl_L;
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003309#ifdef FEAT_TERMINAL
3310 else if (terminal_is_active())
3311 c = K_CANCEL;
3312#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 else if ((State & CMDLINE)
Bram Moolenaare2c38102016-01-31 14:55:40 +01003314#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 || (cmdwin_type > 0 && tc == ESC)
Bram Moolenaare2c38102016-01-31 14:55:40 +01003316#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 )
3318 c = Ctrl_C;
3319 else
3320 c = ESC;
Bram Moolenaare2c38102016-01-31 14:55:40 +01003321#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 tc = c;
Bram Moolenaare2c38102016-01-31 14:55:40 +01003323#endif
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01003324 // return from main_loop()
3325 if (pending_exmode_active)
3326 exmode_active = EXMODE_NORMAL;
3327
Bram Moolenaarb37a65e2022-01-01 12:42:56 +00003328 // no chars to block abbreviation for
3329 typebuf.tb_no_abbr_cnt = 0;
3330
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 break;
3332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333
3334/*
3335 * get a character: 3. from the user - update display
3336 */
Bram Moolenaar30613902019-12-01 22:11:18 +01003337 // In insert mode a screen update is skipped when characters
3338 // are still available. But when those available characters
3339 // are part of a mapping, and we are going to do a blocking
3340 // wait here. Need to update the screen to display the
3341 // changed text so far. Also for when 'lazyredraw' is set and
3342 // redrawing was postponed because there was something in the
3343 // input buffer (e.g., termresponse).
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01003344 if (((State & INSERT) != 0 || p_lz) && (State & CMDLINE) == 0
3345 && advance && must_redraw != 0 && !need_wait_return)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 {
3347 update_screen(0);
Bram Moolenaar30613902019-12-01 22:11:18 +01003348 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 }
3350
3351 /*
3352 * If we have a partial match (and are going to wait for more
3353 * input from the user), show the partially matched characters
3354 * to the user with showcmd.
3355 */
3356#ifdef FEAT_CMDL_INFO
Bram Moolenaareda35f72019-08-03 14:59:44 +02003357 showcmd_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358#endif
3359 c1 = 0;
3360 if (typebuf.tb_len > 0 && advance && !exmode_active)
3361 {
3362 if (((State & (NORMAL | INSERT)) || State == LANGMAP)
3363 && State != HITRETURN)
3364 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003365 // this looks nice when typing a dead character map
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 if (State & INSERT
3367 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
3368 + typebuf.tb_len - 1) == 1)
3369 {
3370 edit_putchar(typebuf.tb_buf[typebuf.tb_off
3371 + typebuf.tb_len - 1], FALSE);
Bram Moolenaar30613902019-12-01 22:11:18 +01003372 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 c1 = 1;
3374 }
3375#ifdef FEAT_CMDL_INFO
Bram Moolenaar30613902019-12-01 22:11:18 +01003376 // need to use the col and row from above here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 old_wcol = curwin->w_wcol;
3378 old_wrow = curwin->w_wrow;
3379 curwin->w_wcol = new_wcol;
3380 curwin->w_wrow = new_wrow;
3381 push_showcmd();
3382 if (typebuf.tb_len > SHOWCMD_COLS)
Bram Moolenaareda35f72019-08-03 14:59:44 +02003383 showcmd_idx = typebuf.tb_len - SHOWCMD_COLS;
3384 while (showcmd_idx < typebuf.tb_len)
3385 (void)add_to_showcmd(
3386 typebuf.tb_buf[typebuf.tb_off + showcmd_idx++]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 curwin->w_wcol = old_wcol;
3388 curwin->w_wrow = old_wrow;
3389#endif
3390 }
3391
Bram Moolenaar30613902019-12-01 22:11:18 +01003392 // this looks nice when typing a dead character map
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 if ((State & CMDLINE)
3394#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3395 && cmdline_star == 0
3396#endif
3397 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
3398 + typebuf.tb_len - 1) == 1)
3399 {
3400 putcmdline(typebuf.tb_buf[typebuf.tb_off
3401 + typebuf.tb_len - 1], FALSE);
3402 c1 = 1;
3403 }
3404 }
3405
3406/*
3407 * get a character: 3. from the user - get it
3408 */
Bram Moolenaar83f4cbd2018-06-12 21:35:40 +02003409 if (typebuf.tb_len == 0)
3410 // timedout may have been set while waiting for a mapping
3411 // that has a <Nop> RHS.
3412 timedout = FALSE;
3413
Bram Moolenaar652de232019-04-04 20:13:09 +02003414 if (advance)
3415 {
3416 if (typebuf.tb_len == 0
3417 || !(p_timeout
3418 || (p_ttimeout && keylen == KEYLEN_PART_KEY)))
3419 // blocking wait
3420 wait_time = -1L;
3421 else if (keylen == KEYLEN_PART_KEY && p_ttm >= 0)
3422 wait_time = p_ttm;
3423 else
3424 wait_time = p_tm;
3425 }
3426 else
3427 wait_time = 0;
3428
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003429 wait_tb_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 c = inchar(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len,
3431 typebuf.tb_buflen - typebuf.tb_off - typebuf.tb_len - 1,
Bram Moolenaar652de232019-04-04 20:13:09 +02003432 wait_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433
3434#ifdef FEAT_CMDL_INFO
Bram Moolenaareda35f72019-08-03 14:59:44 +02003435 if (showcmd_idx != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 pop_showcmd();
3437#endif
3438 if (c1 == 1)
3439 {
3440 if (State & INSERT)
3441 edit_unputchar();
3442 if (State & CMDLINE)
3443 unputcmdline();
Bram Moolenaarbc256d92012-06-06 12:06:15 +02003444 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003445 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 }
3447
3448 if (c < 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003449 continue; // end of input script reached
3450 if (c == NUL) // no character available
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 {
3452 if (!advance)
3453 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01003454 if (wait_tb_len > 0) // timed out
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 {
3456 timedout = TRUE;
3457 continue;
3458 }
3459 }
3460 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003461 { // allow mapping for just typed characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 while (typebuf.tb_buf[typebuf.tb_off
3463 + typebuf.tb_len] != NUL)
3464 typebuf.tb_noremap[typebuf.tb_off
3465 + typebuf.tb_len++] = RM_YES;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003466#ifdef HAVE_INPUT_METHOD
Bram Moolenaar30613902019-12-01 22:11:18 +01003467 // Get IM status right after getting keys, not after the
3468 // timeout for a mapping (focus may be lost by then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 vgetc_im_active = im_get_status();
3470#endif
3471 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003472 } // for (;;)
3473 } // if (!character from stuffbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474
Bram Moolenaar30613902019-12-01 22:11:18 +01003475 // if advance is FALSE don't loop on NULs
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003476 } while ((c < 0 && c != K_CANCEL) || (advance && c == NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477
3478 /*
3479 * The "INSERT" message is taken care of here:
3480 * if we return an ESC to exit insert mode, the message is deleted
3481 * if we don't return an ESC but deleted the message before, redisplay it
3482 */
Bram Moolenaar09df3122006-01-23 22:23:09 +00003483 if (advance && p_smd && msg_silent == 0 && (State & INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003485 if (c == ESC && !mode_deleted && !no_mapping && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 {
3487 if (typebuf.tb_len && !KeyTyped)
Bram Moolenaar30613902019-12-01 22:11:18 +01003488 redraw_cmdline = TRUE; // delete mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 else
3490 unshowmode(FALSE);
3491 }
3492 else if (c != ESC && mode_deleted)
3493 {
3494 if (typebuf.tb_len && !KeyTyped)
Bram Moolenaar30613902019-12-01 22:11:18 +01003495 redraw_cmdline = TRUE; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 else
3497 showmode();
3498 }
3499 }
3500#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003501 // may unshow different cursor shape
Bram Moolenaarf085f422017-06-07 20:39:47 +02003502 if (gui.in_use && shape_changed)
3503 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504#endif
Bram Moolenaar6edbbd82019-03-10 09:41:51 +01003505 if (timedout && c == ESC)
3506 {
3507 char_u nop_buf[3];
3508
3509 // When recording there will be no timeout. Add a <Nop> after the ESC
3510 // to avoid that it forms a key code with following characters.
3511 nop_buf[0] = K_SPECIAL;
3512 nop_buf[1] = KS_EXTRA;
3513 nop_buf[2] = KE_NOP;
3514 gotchars(nop_buf, 3);
3515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003517 --vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518
3519 return c;
3520}
3521
3522/*
3523 * inchar() - get one character from
3524 * 1. a scriptfile
3525 * 2. the keyboard
3526 *
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003527 * As many characters as we can get (up to 'maxlen') are put in "buf" and
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 * NUL terminated (buffer length must be 'maxlen' + 1).
3529 * Minimum for "maxlen" is 3!!!!
3530 *
3531 * "tb_change_cnt" is the value of typebuf.tb_change_cnt if "buf" points into
3532 * it. When typebuf.tb_change_cnt changes (e.g., when a message is received
3533 * from a remote client) "buf" can no longer be used. "tb_change_cnt" is 0
3534 * otherwise.
3535 *
3536 * If we got an interrupt all input is read until none is available.
3537 *
3538 * If wait_time == 0 there is no waiting for the char.
3539 * If wait_time == n we wait for n msec for a character to arrive.
3540 * If wait_time == -1 we wait forever for a character to arrive.
3541 *
3542 * Return the number of obtained characters.
3543 * Return -1 when end of input script reached.
3544 */
Bram Moolenaarcda77642016-06-04 13:32:35 +02003545 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003546inchar(
3547 char_u *buf,
3548 int maxlen,
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003549 long wait_time) // milliseconds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550{
Bram Moolenaar30613902019-12-01 22:11:18 +01003551 int len = 0; // init for GCC
3552 int retesc = FALSE; // return ESC with gotint
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 int script_char;
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003554 int tb_change_cnt = typebuf.tb_change_cnt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555
Bram Moolenaar30613902019-12-01 22:11:18 +01003556 if (wait_time == -1L || wait_time > 100L) // flush output before waiting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 {
3558 cursor_on();
Bram Moolenaara338adc2018-01-31 20:51:47 +01003559 out_flush_cursor(FALSE, FALSE);
3560#if defined(FEAT_GUI) && defined(FEAT_MOUSESHAPE)
3561 if (gui.in_use && postponed_mouseshape)
3562 update_mouseshape(-1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563#endif
3564 }
3565
3566 /*
3567 * Don't reset these when at the hit-return prompt, otherwise a endless
3568 * recursive loop may result (write error in swapfile, hit-return, timeout
3569 * on char wait, flush swapfile, write error....).
3570 */
3571 if (State != HITRETURN)
3572 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003573 did_outofmem_msg = FALSE; // display out of memory message (again)
3574 did_swapwrite_msg = FALSE; // display swap file write error again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003576 undo_off = FALSE; // restart undo now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577
3578 /*
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00003579 * Get a character from a script file if there is one.
3580 * If interrupted: Stop reading script files, close them all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 */
3582 script_char = -1;
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00003583 while (scriptin[curscript] != NULL && script_char < 0
3584#ifdef FEAT_EVAL
3585 && !ignore_script
3586#endif
3587 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02003589#ifdef MESSAGE_QUEUE
3590 parse_queued_messages();
Bram Moolenaarf2330482008-06-24 20:19:36 +00003591#endif
3592
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 if (got_int || (script_char = getc(scriptin[curscript])) < 0)
3594 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003595 // Reached EOF.
3596 // Careful: closescript() frees typebuf.tb_buf[] and buf[] may
3597 // point inside typebuf.tb_buf[]. Don't use buf[] after this!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 closescript();
3599 /*
3600 * When reading script file is interrupted, return an ESC to get
3601 * back to normal mode.
3602 * Otherwise return -1, because typebuf.tb_buf[] has changed.
3603 */
3604 if (got_int)
3605 retesc = TRUE;
3606 else
3607 return -1;
3608 }
3609 else
3610 {
3611 buf[0] = script_char;
3612 len = 1;
3613 }
3614 }
3615
Bram Moolenaar30613902019-12-01 22:11:18 +01003616 if (script_char < 0) // did not get a character from script
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 {
3618 /*
3619 * If we got an interrupt, skip all previously typed characters and
3620 * return TRUE if quit reading script file.
3621 * Stop reading typeahead when a single CTRL-C was read,
3622 * fill_input_buf() returns this when not able to read from stdin.
3623 * Don't use buf[] here, closescript() may have freed typebuf.tb_buf[]
3624 * and buf may be pointing inside typebuf.tb_buf[].
3625 */
3626 if (got_int)
3627 {
kylo252ae6f1d82022-02-16 19:24:07 +00003628#define DUM_LEN (MAXMAPLEN * 3 + 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 char_u dum[DUM_LEN + 1];
3630
3631 for (;;)
3632 {
3633 len = ui_inchar(dum, DUM_LEN, 0L, 0);
3634 if (len == 0 || (len == 1 && dum[0] == 3))
3635 break;
3636 }
3637 return retesc;
3638 }
3639
3640 /*
3641 * Always flush the output characters when getting input characters
Bram Moolenaarcb574f42019-01-25 22:29:57 +01003642 * from the user and not just peeking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 */
Bram Moolenaarcb574f42019-01-25 22:29:57 +01003644 if (wait_time == -1L || wait_time > 10L)
3645 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646
3647 /*
3648 * Fill up to a third of the buffer, because each character may be
3649 * tripled below.
3650 */
3651 len = ui_inchar(buf, maxlen / 3, wait_time, tb_change_cnt);
3652 }
3653
Bram Moolenaar30613902019-12-01 22:11:18 +01003654 // If the typebuf was changed further down, it is like nothing was added by
3655 // this call.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 if (typebuf_changed(tb_change_cnt))
3657 return 0;
3658
Bram Moolenaar30613902019-12-01 22:11:18 +01003659 // Note the change in the typeahead buffer, this matters for when
3660 // vgetorpeek() is called recursively, e.g. using getchar(1) in a timer
3661 // function.
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003662 if (len > 0 && ++typebuf.tb_change_cnt == 0)
3663 typebuf.tb_change_cnt = 1;
3664
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003665 return fix_input_buffer(buf, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666}
3667
3668/*
3669 * Fix typed characters for use by vgetc() and check_termcode().
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003670 * "buf[]" must have room to triple the number of bytes!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 * Returns the new length.
3672 */
3673 int
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003674fix_input_buffer(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675{
3676 int i;
3677 char_u *p = buf;
3678
3679 /*
3680 * Two characters are special: NUL and K_SPECIAL.
3681 * When compiled With the GUI CSI is also special.
3682 * Replace NUL by K_SPECIAL KS_ZERO KE_FILLER
3683 * Replace K_SPECIAL by K_SPECIAL KS_SPECIAL KE_FILLER
3684 * Replace CSI by K_SPECIAL KS_EXTRA KE_CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 */
3686 for (i = len; --i >= 0; ++p)
3687 {
3688#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003689 // When the GUI is used any character can come after a CSI, don't
3690 // escape it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 if (gui.in_use && p[0] == CSI && i >= 2)
3692 {
3693 p += 2;
3694 i -= 2;
3695 }
zeertzjq646bb722022-02-16 17:51:47 +00003696# ifndef MSWIN
3697 // When not on MS-Windows and the GUI is not used CSI needs to be
3698 // escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 else if (!gui.in_use && p[0] == CSI)
3700 {
3701 mch_memmove(p + 3, p + 1, (size_t)i);
3702 *p++ = K_SPECIAL;
3703 *p++ = KS_EXTRA;
3704 *p = (int)KE_CSI;
3705 len += 2;
3706 }
zeertzjq646bb722022-02-16 17:51:47 +00003707# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 else
3709#endif
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003710 if (p[0] == NUL || (p[0] == K_SPECIAL
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003711 // timeout may generate K_CURSORHOLD
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003712 && (i < 2 || p[1] != KS_EXTRA || p[2] != (int)KE_CURSORHOLD)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003713#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003714 // Win32 console passes modifiers
3715 && (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003716# ifdef VIMDLL
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003717 gui.in_use ||
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003718# endif
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003719 (i < 2 || p[1] != KS_MODIFIER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720#endif
3721 ))
3722 {
3723 mch_memmove(p + 3, p + 1, (size_t)i);
3724 p[2] = K_THIRD(p[0]);
3725 p[1] = K_SECOND(p[0]);
3726 p[0] = K_SPECIAL;
3727 p += 2;
3728 len += 2;
3729 }
3730 }
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003731 *p = NUL; // add trailing NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 return len;
3733}
3734
3735#if defined(USE_INPUT_BUF) || defined(PROTO)
3736/*
3737 * Return TRUE when bytes are in the input buffer or in the typeahead buffer.
3738 * Normally the input buffer would be sufficient, but the server_to_input_buf()
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003739 * or feedkeys() may insert characters in the typeahead buffer while we are
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003740 * waiting for input to arrive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 */
3742 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003743input_available(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744{
3745 return (!vim_is_input_buf_empty()
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003746# if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
3747 || typebuf_was_filled
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748# endif
3749 );
3750}
3751#endif
Bram Moolenaar957cf672020-11-12 14:21:06 +01003752
3753/*
3754 * Function passed to do_cmdline() to get the command after a <Cmd> key from
3755 * typeahead.
3756 */
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003757 static char_u *
Bram Moolenaar957cf672020-11-12 14:21:06 +01003758getcmdkeycmd(
3759 int promptc UNUSED,
3760 void *cookie UNUSED,
3761 int indent UNUSED,
3762 getline_opt_T do_concat UNUSED)
3763{
3764 garray_T line_ga;
3765 int c1 = -1;
3766 int c2;
3767 int cmod = 0;
3768 int aborted = FALSE;
3769
3770 ga_init2(&line_ga, 1, 32);
3771
3772 // no mapping for these characters
3773 no_mapping++;
3774
3775 got_int = FALSE;
3776 while (c1 != NUL && !aborted)
3777 {
Bram Moolenaarca359cb2020-11-15 20:49:41 +01003778 if (ga_grow(&line_ga, 32) != OK)
3779 {
3780 aborted = TRUE;
3781 break;
3782 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003783
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003784 if (vgetorpeek(FALSE) == NUL)
Bram Moolenaar957cf672020-11-12 14:21:06 +01003785 {
3786 // incomplete <Cmd> is an error, because there is not much the user
3787 // could do in this state.
3788 emsg(_(e_cmd_mapping_must_end_with_cr));
3789 aborted = TRUE;
3790 break;
3791 }
3792
3793 // Get one character at a time.
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003794 c1 = vgetorpeek(TRUE);
3795
Bram Moolenaar957cf672020-11-12 14:21:06 +01003796 // Get two extra bytes for special keys
3797 if (c1 == K_SPECIAL)
3798 {
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003799 c1 = vgetorpeek(TRUE);
3800 c2 = vgetorpeek(TRUE);
Bram Moolenaar957cf672020-11-12 14:21:06 +01003801 if (c1 == KS_MODIFIER)
3802 {
3803 cmod = c2;
3804 continue;
3805 }
3806 c1 = TO_SPECIAL(c1, c2);
3807 }
Bram Moolenaar4a441202020-11-28 14:43:26 +01003808 if (c1 == Ctrl_V)
3809 {
3810 // CTRL-V is followed by octal, hex or other characters, reverses
3811 // what AppendToRedobuffLit() does.
3812 no_reduce_keys = TRUE; // don't merge modifyOtherKeys
Bram Moolenaar0684e362020-12-03 19:54:42 +01003813 c1 = get_literal(TRUE);
Bram Moolenaar4a441202020-11-28 14:43:26 +01003814 no_reduce_keys = FALSE;
3815 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003816
3817 if (got_int)
3818 aborted = TRUE;
3819 else if (c1 == '\r' || c1 == '\n')
3820 c1 = NUL; // end the line
3821 else if (c1 == ESC)
3822 aborted = TRUE;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003823 else if (c1 == K_COMMAND || c1 == K_SCRIPT_COMMAND)
Bram Moolenaar957cf672020-11-12 14:21:06 +01003824 {
3825 // give a nicer error message for this special case
3826 emsg(_(e_cmd_mapping_must_end_with_cr_before_second_cmd));
3827 aborted = TRUE;
3828 }
3829 else if (IS_SPECIAL(c1))
3830 {
3831 if (c1 == K_SNR)
Bram Moolenaarc77534c2020-11-18 11:34:37 +01003832 ga_concat(&line_ga, (char_u *)"<SNR>");
Bram Moolenaar957cf672020-11-12 14:21:06 +01003833 else
3834 {
3835 semsg(e_cmd_maping_must_not_include_str_key,
3836 get_special_key_name(c1, cmod));
3837 aborted = TRUE;
3838 }
3839 }
3840 else
Bram Moolenaara9725222022-01-16 13:30:33 +00003841 ga_append(&line_ga, c1);
Bram Moolenaar957cf672020-11-12 14:21:06 +01003842
3843 cmod = 0;
3844 }
3845
3846 no_mapping--;
3847
3848 if (aborted)
3849 ga_clear(&line_ga);
3850
3851 return (char_u *)line_ga.ga_data;
3852}
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003853
3854 int
Bram Moolenaard4e2f502022-01-15 18:48:32 +00003855do_cmdkey_command(int key UNUSED, int flags)
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003856{
3857 int res;
3858#ifdef FEAT_EVAL
Bram Moolenaara9725222022-01-16 13:30:33 +00003859 sctx_T save_current_sctx = {-1, 0, 0, 0};
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003860
3861 if (key == K_SCRIPT_COMMAND && last_used_map != NULL)
3862 {
3863 save_current_sctx = current_sctx;
3864 current_sctx = last_used_map->m_script_ctx;
3865 }
3866#endif
3867
3868 res = do_cmdline(NULL, getcmdkeycmd, NULL, flags);
3869
3870#ifdef FEAT_EVAL
Bram Moolenaara9725222022-01-16 13:30:33 +00003871 if (save_current_sctx.sc_sid >= 0)
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003872 current_sctx = save_current_sctx;
3873#endif
3874
3875 return res;
3876}
3877
3878#if defined(FEAT_EVAL) || defined(PROTO)
3879 void
Bram Moolenaarf61c89d2022-01-19 22:51:48 +00003880reset_last_used_map(mapblock_T *mp)
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003881{
Bram Moolenaarf61c89d2022-01-19 22:51:48 +00003882 if (last_used_map == mp)
3883 last_used_map = NULL;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003884}
3885#endif