blob: 08c1a955781600cff414e032178ef3d237c545dd [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 Moolenaard25c16e2016-01-29 22:13:30 +010086static int read_readbuf(buffheader_T *buf, int advance);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010087static void init_typebuf(void);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010088static void may_sync_undo(void);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020089static void free_typebuf(void);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010090static void closescript(void);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020091static void updatescript(int c);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010092static int vgetorpeek(int);
Bram Moolenaar0f0f2302017-08-30 18:52:56 +020093static int inchar(char_u *buf, int maxlen, long wait_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +000094
95/*
96 * Free and clear a buffer.
97 */
Bram Moolenaarbdff0122020-04-05 18:56:05 +020098 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +010099free_buff(buffheader_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100101 buffblock_T *p, *np;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102
Bram Moolenaar285e3352018-04-18 23:01:13 +0200103 for (p = buf->bh_first.b_next; p != NULL; p = np)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104 {
105 np = p->b_next;
106 vim_free(p);
107 }
Bram Moolenaar285e3352018-04-18 23:01:13 +0200108 buf->bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000109}
110
111/*
112 * Return the contents of a buffer as a single string.
113 * K_SPECIAL and CSI in the returned string are escaped.
114 */
115 static char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100116get_buffcont(
117 buffheader_T *buffer,
Bram Moolenaar30613902019-12-01 22:11:18 +0100118 int dozero) // count == zero is not an error
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119{
120 long_u count = 0;
121 char_u *p = NULL;
122 char_u *p2;
123 char_u *str;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200124 buffblock_T *bp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125
Bram Moolenaar30613902019-12-01 22:11:18 +0100126 // compute the total length of the string
Bram Moolenaar285e3352018-04-18 23:01:13 +0200127 for (bp = buffer->bh_first.b_next; bp != NULL; bp = bp->b_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128 count += (long_u)STRLEN(bp->b_str);
129
Bram Moolenaar18a4ba22019-05-24 19:39:03 +0200130 if ((count || dozero) && (p = alloc(count + 1)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131 {
132 p2 = p;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200133 for (bp = buffer->bh_first.b_next; bp != NULL; bp = bp->b_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134 for (str = bp->b_str; *str; )
135 *p2++ = *str++;
136 *p2 = NUL;
137 }
138 return (p);
139}
140
141/*
142 * Return the contents of the record buffer as a single string
143 * and clear the record buffer.
144 * K_SPECIAL and CSI in the returned string are escaped.
145 */
146 char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100147get_recorded(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148{
149 char_u *p;
150 size_t len;
151
152 p = get_buffcont(&recordbuff, TRUE);
153 free_buff(&recordbuff);
154
155 /*
156 * Remove the characters that were added the last time, these must be the
157 * (possibly mapped) characters that stopped the recording.
158 */
159 len = STRLEN(p);
160 if ((int)len >= last_recorded_len)
161 {
162 len -= last_recorded_len;
163 p[len] = NUL;
164 }
165
166 /*
167 * When stopping recording from Insert mode with CTRL-O q, also remove the
168 * CTRL-O.
169 */
170 if (len > 0 && restart_edit != 0 && p[len - 1] == Ctrl_O)
171 p[len - 1] = NUL;
172
173 return (p);
174}
175
176/*
177 * Return the contents of the redo buffer as a single string.
178 * K_SPECIAL and CSI in the returned string are escaped.
179 */
180 char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100181get_inserted(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182{
Bram Moolenaarf2330482008-06-24 20:19:36 +0000183 return get_buffcont(&redobuff, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184}
185
186/*
Bram Moolenaar4e86cba2007-05-06 11:56:32 +0000187 * Add string "s" after the current block of buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188 * K_SPECIAL and CSI should have been escaped already.
189 */
190 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100191add_buff(
192 buffheader_T *buf,
193 char_u *s,
Bram Moolenaar30613902019-12-01 22:11:18 +0100194 long slen) // length of "s" or -1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100196 buffblock_T *p;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200197 long_u len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198
199 if (slen < 0)
200 slen = (long)STRLEN(s);
Bram Moolenaar30613902019-12-01 22:11:18 +0100201 if (slen == 0) // don't add empty strings
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202 return;
203
Bram Moolenaar30613902019-12-01 22:11:18 +0100204 if (buf->bh_first.b_next == NULL) // first add to list
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205 {
206 buf->bh_space = 0;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200207 buf->bh_curr = &(buf->bh_first);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208 }
Bram Moolenaar30613902019-12-01 22:11:18 +0100209 else if (buf->bh_curr == NULL) // buffer has already been read
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210 {
Bram Moolenaar6d057012021-12-31 18:49:43 +0000211 iemsg(_(e_add_to_internal_buffer_that_was_already_read_from));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212 return;
213 }
214 else if (buf->bh_index != 0)
Bram Moolenaar285e3352018-04-18 23:01:13 +0200215 mch_memmove(buf->bh_first.b_next->b_str,
216 buf->bh_first.b_next->b_str + buf->bh_index,
217 STRLEN(buf->bh_first.b_next->b_str + buf->bh_index) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218 buf->bh_index = 0;
219
220 if (buf->bh_space >= (int)slen)
221 {
222 len = (long_u)STRLEN(buf->bh_curr->b_str);
Bram Moolenaarb6356332005-07-18 21:40:44 +0000223 vim_strncpy(buf->bh_curr->b_str + len, s, (size_t)slen);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224 buf->bh_space -= slen;
225 }
226 else
227 {
228 if (slen < MINIMAL_SIZE)
229 len = MINIMAL_SIZE;
230 else
231 len = slen;
Bram Moolenaar47ed5532019-08-08 20:49:14 +0200232 p = alloc(offsetof(buffblock_T, b_str) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233 if (p == NULL)
Bram Moolenaar30613902019-12-01 22:11:18 +0100234 return; // no space, just forget it
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000235 buf->bh_space = (int)(len - slen);
Bram Moolenaarb6356332005-07-18 21:40:44 +0000236 vim_strncpy(p->b_str, s, (size_t)slen);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237
Bram Moolenaar285e3352018-04-18 23:01:13 +0200238 p->b_next = buf->bh_curr->b_next;
239 buf->bh_curr->b_next = p;
240 buf->bh_curr = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242}
243
244/*
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000245 * Delete "slen" bytes from the end of "buf".
246 * Only works when it was just added.
247 */
248 static void
249delete_buff_tail(buffheader_T *buf, int slen)
250{
251 int len = (int)STRLEN(buf->bh_curr->b_str);
252
253 if (len >= slen)
254 {
255 buf->bh_curr->b_str[len - slen] = NUL;
256 buf->bh_space += slen;
257 }
258}
259
260/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261 * Add number "n" to buffer "buf".
262 */
263 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100264add_num_buff(buffheader_T *buf, long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265{
266 char_u number[32];
267
268 sprintf((char *)number, "%ld", n);
269 add_buff(buf, number, -1L);
270}
271
272/*
273 * Add character 'c' to buffer "buf".
274 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
275 */
276 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100277add_char_buff(buffheader_T *buf, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279 char_u bytes[MB_MAXBYTES + 1];
280 int len;
281 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282 char_u temp[4];
283
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284 if (IS_SPECIAL(c))
285 len = 1;
286 else
287 len = (*mb_char2bytes)(c, bytes);
288 for (i = 0; i < len; ++i)
289 {
290 if (!IS_SPECIAL(c))
291 c = bytes[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292
293 if (IS_SPECIAL(c) || c == K_SPECIAL || c == NUL)
294 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100295 // translate special key code into three byte sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296 temp[0] = K_SPECIAL;
297 temp[1] = K_SECOND(c);
298 temp[2] = K_THIRD(c);
299 temp[3] = NUL;
300 }
301#ifdef FEAT_GUI
302 else if (c == CSI)
303 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100304 // Translate a CSI to a CSI - KS_EXTRA - KE_CSI sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305 temp[0] = CSI;
306 temp[1] = KS_EXTRA;
307 temp[2] = (int)KE_CSI;
308 temp[3] = NUL;
309 }
310#endif
311 else
312 {
313 temp[0] = c;
314 temp[1] = NUL;
315 }
316 add_buff(buf, temp, -1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318}
319
Bram Moolenaar30613902019-12-01 22:11:18 +0100320// First read ahead buffer. Used for translated commands.
Bram Moolenaar285e3352018-04-18 23:01:13 +0200321static buffheader_T readbuf1 = {{NULL, {NUL}}, NULL, 0, 0};
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100322
Bram Moolenaar30613902019-12-01 22:11:18 +0100323// Second read ahead buffer. Used for redo.
Bram Moolenaar285e3352018-04-18 23:01:13 +0200324static buffheader_T readbuf2 = {{NULL, {NUL}}, NULL, 0, 0};
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100325
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326/*
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100327 * Get one byte from the read buffers. Use readbuf1 one first, use readbuf2
328 * if that one is empty.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329 * If advance == TRUE go to the next char.
330 * No translation is done K_SPECIAL and CSI are escaped.
331 */
332 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100333read_readbuffers(int advance)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100335 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100337 c = read_readbuf(&readbuf1, advance);
338 if (c == NUL)
339 c = read_readbuf(&readbuf2, advance);
340 return c;
341}
342
343 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100344read_readbuf(buffheader_T *buf, int advance)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100345{
346 char_u c;
347 buffblock_T *curr;
348
Bram Moolenaar30613902019-12-01 22:11:18 +0100349 if (buf->bh_first.b_next == NULL) // buffer is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 return NUL;
351
Bram Moolenaar285e3352018-04-18 23:01:13 +0200352 curr = buf->bh_first.b_next;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100353 c = curr->b_str[buf->bh_index];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354
355 if (advance)
356 {
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100357 if (curr->b_str[++buf->bh_index] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 {
Bram Moolenaar285e3352018-04-18 23:01:13 +0200359 buf->bh_first.b_next = curr->b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360 vim_free(curr);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100361 buf->bh_index = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362 }
363 }
364 return c;
365}
366
367/*
Bram Moolenaar06811f32014-02-15 16:17:07 +0100368 * Prepare the read buffers for reading (if they contain something).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369 */
370 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100371start_stuff(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372{
Bram Moolenaar285e3352018-04-18 23:01:13 +0200373 if (readbuf1.bh_first.b_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 {
Bram Moolenaar285e3352018-04-18 23:01:13 +0200375 readbuf1.bh_curr = &(readbuf1.bh_first);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100376 readbuf1.bh_space = 0;
377 }
Bram Moolenaar285e3352018-04-18 23:01:13 +0200378 if (readbuf2.bh_first.b_next != NULL)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100379 {
Bram Moolenaar285e3352018-04-18 23:01:13 +0200380 readbuf2.bh_curr = &(readbuf2.bh_first);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100381 readbuf2.bh_space = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382 }
383}
384
385/*
386 * Return TRUE if the stuff buffer is empty.
387 */
388 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100389stuff_empty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390{
Bram Moolenaar285e3352018-04-18 23:01:13 +0200391 return (readbuf1.bh_first.b_next == NULL
392 && readbuf2.bh_first.b_next == NULL);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100393}
394
Bram Moolenaar113e1072019-01-20 15:30:40 +0100395#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100396/*
397 * Return TRUE if readbuf1 is empty. There may still be redo characters in
398 * redbuf2.
399 */
400 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100401readbuf1_empty(void)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100402{
Bram Moolenaar285e3352018-04-18 23:01:13 +0200403 return (readbuf1.bh_first.b_next == NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100405#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406
407/*
408 * Set a typeahead character that won't be flushed.
409 */
410 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100411typeahead_noflush(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412{
413 typeahead_char = c;
414}
415
416/*
417 * Remove the contents of the stuff buffer and the mapped characters in the
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100418 * typeahead buffer (used in case of an error). If "flush_typeahead" is true,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419 * flush all typeahead characters (used when interrupted by a CTRL-C).
420 */
421 void
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200422flush_buffers(flush_buffers_T flush_typeahead)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423{
424 init_typebuf();
425
426 start_stuff();
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100427 while (read_readbuffers(TRUE) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428 ;
429
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200430 if (flush_typeahead == FLUSH_MINIMAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431 {
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200432 // remove mapped characters at the start only
433 typebuf.tb_off += typebuf.tb_maplen;
434 typebuf.tb_len -= typebuf.tb_maplen;
Bram Moolenaare49b4bb2020-03-11 13:01:40 +0100435#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
436 if (typebuf.tb_len == 0)
437 typebuf_was_filled = FALSE;
438#endif
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200439 }
440 else
441 {
442 // remove typeahead
443 if (flush_typeahead == FLUSH_INPUT)
444 // We have to get all characters, because we may delete the first
445 // part of an escape sequence. In an xterm we get one char at a
446 // time and we have to get them all.
447 while (inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 10L) != 0)
448 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449 typebuf.tb_off = MAXMAPLEN;
450 typebuf.tb_len = 0;
Bram Moolenaar4eb65312017-06-24 18:49:00 +0200451#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar30613902019-12-01 22:11:18 +0100452 // Reset the flag that text received from a client or from feedkeys()
453 // was inserted in the typeahead buffer.
Bram Moolenaar4eb65312017-06-24 18:49:00 +0200454 typebuf_was_filled = FALSE;
455#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457 typebuf.tb_maplen = 0;
458 typebuf.tb_silent = 0;
459 cmd_silent = FALSE;
460 typebuf.tb_no_abbr_cnt = 0;
Bram Moolenaarb8d732e2020-08-05 22:07:26 +0200461 if (++typebuf.tb_change_cnt == 0)
462 typebuf.tb_change_cnt = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463}
464
465/*
466 * The previous contents of the redo buffer is kept in old_redobuffer.
467 * This is used for the CTRL-O <.> command in insert mode.
468 */
469 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100470ResetRedobuff(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471{
472 if (!block_redo)
473 {
474 free_buff(&old_redobuff);
475 old_redobuff = redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200476 redobuff.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477 }
478}
479
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100480/*
481 * Discard the contents of the redo buffer and restore the previous redo
482 * buffer.
483 */
484 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100485CancelRedo(void)
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100486{
487 if (!block_redo)
488 {
489 free_buff(&redobuff);
490 redobuff = old_redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200491 old_redobuff.bh_first.b_next = NULL;
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100492 start_stuff();
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100493 while (read_readbuffers(TRUE) != NUL)
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100494 ;
495 }
496}
497
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498/*
499 * Save redobuff and old_redobuff to save_redobuff and save_old_redobuff.
500 * Used before executing autocommands and user functions.
501 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 void
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200503saveRedobuff(save_redo_T *save_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504{
505 char_u *s;
506
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200507 save_redo->sr_redobuff = redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200508 redobuff.bh_first.b_next = NULL;
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200509 save_redo->sr_old_redobuff = old_redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200510 old_redobuff.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511
Bram Moolenaar30613902019-12-01 22:11:18 +0100512 // Make a copy, so that ":normal ." in a function works.
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200513 s = get_buffcont(&save_redo->sr_redobuff, FALSE);
514 if (s != NULL)
515 {
516 add_buff(&redobuff, s, -1L);
517 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 }
519}
520
521/*
522 * Restore redobuff and old_redobuff from save_redobuff and save_old_redobuff.
523 * Used after executing autocommands and user functions.
524 */
525 void
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200526restoreRedobuff(save_redo_T *save_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527{
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200528 free_buff(&redobuff);
529 redobuff = save_redo->sr_redobuff;
530 free_buff(&old_redobuff);
531 old_redobuff = save_redo->sr_old_redobuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533
534/*
535 * Append "s" to the redo buffer.
536 * K_SPECIAL and CSI should already have been escaped.
537 */
538 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100539AppendToRedobuff(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540{
541 if (!block_redo)
542 add_buff(&redobuff, s, -1L);
543}
544
545/*
546 * Append to Redo buffer literally, escaping special characters with CTRL-V.
547 * K_SPECIAL and CSI are escaped as well.
548 */
549 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100550AppendToRedobuffLit(
551 char_u *str,
Bram Moolenaar30613902019-12-01 22:11:18 +0100552 int len) // length of "str" or -1 for up to the NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553{
Bram Moolenaarebefac62005-12-28 22:39:57 +0000554 char_u *s = str;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 int c;
556 char_u *start;
557
558 if (block_redo)
559 return;
560
Bram Moolenaarebefac62005-12-28 22:39:57 +0000561 while (len < 0 ? *s != NUL : s - str < len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100563 // Put a string of normal characters in the redo buffer (that's
564 // faster).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 start = s;
566 while (*s >= ' '
567#ifndef EBCDIC
Bram Moolenaar30613902019-12-01 22:11:18 +0100568 && *s < DEL // EBCDIC: all chars above space are normal
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569#endif
Bram Moolenaarebefac62005-12-28 22:39:57 +0000570 && (len < 0 || s - str < len))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 ++s;
572
Bram Moolenaar30613902019-12-01 22:11:18 +0100573 // Don't put '0' or '^' as last character, just in case a CTRL-D is
574 // typed next.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 if (*s == NUL && (s[-1] == '0' || s[-1] == '^'))
576 --s;
577 if (s > start)
578 add_buff(&redobuff, start, (long)(s - start));
579
Bram Moolenaarebefac62005-12-28 22:39:57 +0000580 if (*s == NUL || (len >= 0 && s - str >= len))
581 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582
Bram Moolenaar30613902019-12-01 22:11:18 +0100583 // Handle a special or multibyte character.
Bram Moolenaarebefac62005-12-28 22:39:57 +0000584 if (has_mbyte)
Bram Moolenaar30613902019-12-01 22:11:18 +0100585 // Handle composing chars separately.
Bram Moolenaarebefac62005-12-28 22:39:57 +0000586 c = mb_cptr2char_adv(&s);
587 else
Bram Moolenaarebefac62005-12-28 22:39:57 +0000588 c = *s++;
589 if (c < ' ' || c == DEL || (*s == NUL && (c == '0' || c == '^')))
590 add_char_buff(&redobuff, Ctrl_V);
591
Bram Moolenaar30613902019-12-01 22:11:18 +0100592 // CTRL-V '0' must be inserted as CTRL-V 048 (EBCDIC: xf0)
Bram Moolenaarebefac62005-12-28 22:39:57 +0000593 if (*s == NUL && c == '0')
594#ifdef EBCDIC
595 add_buff(&redobuff, (char_u *)"xf0", 3L);
596#else
597 add_buff(&redobuff, (char_u *)"048", 3L);
598#endif
599 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;
716 while ((*arg >= ' '
717#ifndef EBCDIC
718 && *arg < DEL // EBCDIC: chars above space are normal
719#endif
720 )
721 || (*arg == K_SPECIAL && !literally))
722 ++arg;
723 if (arg > start)
724 stuffReadbuffLen(start, (long)(arg - start));
725
726 // stuff a single special character
727 if (*arg != NUL)
728 {
729 if (has_mbyte)
730 c = mb_cptr2char_adv(&arg);
731 else
732 c = *arg++;
733 if (literally && ((c < ' ' && c != TAB) || c == DEL))
734 stuffcharReadbuff(Ctrl_V);
735 stuffcharReadbuff(c);
736 }
737 }
738}
739
740/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 * Read a character from the redo buffer. Translates K_SPECIAL, CSI and
742 * multibyte characters.
743 * The redo buffer is left as it is.
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100744 * If init is TRUE, prepare for redo, return FAIL if nothing to redo, OK
745 * otherwise.
746 * If old is TRUE, use old_redobuff instead of redobuff.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 */
748 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100749read_redo(int init, int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100751 static buffblock_T *bp;
752 static char_u *p;
753 int c;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100754 int n;
755 char_u buf[MB_MAXBYTES + 1];
756 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757
758 if (init)
759 {
760 if (old_redo)
Bram Moolenaar285e3352018-04-18 23:01:13 +0200761 bp = old_redobuff.bh_first.b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 else
Bram Moolenaar285e3352018-04-18 23:01:13 +0200763 bp = redobuff.bh_first.b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 if (bp == NULL)
765 return FAIL;
766 p = bp->b_str;
767 return OK;
768 }
769 if ((c = *p) != NUL)
770 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100771 // Reverse the conversion done by add_char_buff()
772 // For a multi-byte character get all the bytes and return the
773 // converted character.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 if (has_mbyte && (c != K_SPECIAL || p[1] == KS_SPECIAL))
775 n = MB_BYTE2LEN_CHECK(c);
776 else
777 n = 1;
778 for (i = 0; ; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100780 if (c == K_SPECIAL) // special key or escaped K_SPECIAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 {
782 c = TO_SPECIAL(p[1], p[2]);
783 p += 2;
784 }
785#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +0100786 if (c == CSI) // escaped CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 p += 2;
788#endif
789 if (*++p == NUL && bp->b_next != NULL)
790 {
791 bp = bp->b_next;
792 p = bp->b_str;
793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 buf[i] = c;
Bram Moolenaar30613902019-12-01 22:11:18 +0100795 if (i == n - 1) // last byte of a character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 {
797 if (n != 1)
798 c = (*mb_ptr2char)(buf);
799 break;
800 }
801 c = *p;
Bram Moolenaar30613902019-12-01 22:11:18 +0100802 if (c == NUL) // cannot happen?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 }
805 }
806
807 return c;
808}
809
810/*
811 * Copy the rest of the redo buffer into the stuff buffer (in a slow way).
812 * If old_redo is TRUE, use old_redobuff instead of redobuff.
813 * The escaped K_SPECIAL and CSI are copied without translation.
814 */
815 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100816copy_redo(int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817{
818 int c;
819
820 while ((c = read_redo(FALSE, old_redo)) != NUL)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100821 add_char_buff(&readbuf2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822}
823
824/*
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100825 * Stuff the redo buffer into readbuf2.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 * Insert the redo count into the command.
827 * If "old_redo" is TRUE, the last but one command is repeated
828 * instead of the last command (inserting text). This is used for
829 * CTRL-O <.> in insert mode
830 *
831 * return FAIL for failure, OK otherwise
832 */
833 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100834start_redo(long count, int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835{
836 int c;
837
Bram Moolenaar30613902019-12-01 22:11:18 +0100838 // init the pointers; return if nothing to redo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 if (read_redo(TRUE, old_redo) == FAIL)
840 return FAIL;
841
842 c = read_redo(FALSE, old_redo);
843
Bram Moolenaar30613902019-12-01 22:11:18 +0100844 // copy the buffer name, if present
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 if (c == '"')
846 {
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100847 add_buff(&readbuf2, (char_u *)"\"", 1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 c = read_redo(FALSE, old_redo);
849
Bram Moolenaar30613902019-12-01 22:11:18 +0100850 // if a numbered buffer is used, increment the number
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 if (c >= '1' && c < '9')
852 ++c;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100853 add_char_buff(&readbuf2, c);
Bram Moolenaar833093b2018-05-23 21:53:52 +0200854
Bram Moolenaar30613902019-12-01 22:11:18 +0100855 // the expression register should be re-evaluated
Bram Moolenaar833093b2018-05-23 21:53:52 +0200856 if (c == '=')
857 {
858 add_char_buff(&readbuf2, CAR);
859 cmd_silent = TRUE;
860 }
861
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 c = read_redo(FALSE, old_redo);
863 }
864
Bram Moolenaar30613902019-12-01 22:11:18 +0100865 if (c == 'v') // redo Visual
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 {
867 VIsual = curwin->w_cursor;
868 VIsual_active = TRUE;
869 VIsual_select = FALSE;
870 VIsual_reselect = TRUE;
871 redo_VIsual_busy = TRUE;
872 c = read_redo(FALSE, old_redo);
873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874
Bram Moolenaar30613902019-12-01 22:11:18 +0100875 // try to enter the count (in place of a previous count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 if (count)
877 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100878 while (VIM_ISDIGIT(c)) // skip "old" count
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 c = read_redo(FALSE, old_redo);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100880 add_num_buff(&readbuf2, count);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 }
882
Bram Moolenaar30613902019-12-01 22:11:18 +0100883 // copy from the redo buffer into the stuff buffer
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100884 add_char_buff(&readbuf2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 copy_redo(old_redo);
886 return OK;
887}
888
889/*
890 * Repeat the last insert (R, o, O, a, A, i or I command) by stuffing
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100891 * the redo buffer into readbuf2.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 * return FAIL for failure, OK otherwise
893 */
894 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100895start_redo_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896{
897 int c;
898
899 if (read_redo(TRUE, FALSE) == FAIL)
900 return FAIL;
901 start_stuff();
902
Bram Moolenaar30613902019-12-01 22:11:18 +0100903 // skip the count and the command character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 while ((c = read_redo(FALSE, FALSE)) != NUL)
905 {
906 if (vim_strchr((char_u *)"AaIiRrOo", c) != NULL)
907 {
908 if (c == 'O' || c == 'o')
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100909 add_buff(&readbuf2, NL_STR, -1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 break;
911 }
912 }
913
Bram Moolenaar30613902019-12-01 22:11:18 +0100914 // copy the typed text from the redo buffer into the stuff buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 copy_redo(FALSE);
916 block_redo = TRUE;
917 return OK;
918}
919
920 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100921stop_redo_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922{
923 block_redo = FALSE;
924}
925
926/*
927 * Initialize typebuf.tb_buf to point to typebuf_init.
928 * alloc() cannot be used here: In out-of-memory situations it would
929 * be impossible to type anything.
930 */
931 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100932init_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933{
934 if (typebuf.tb_buf == NULL)
935 {
936 typebuf.tb_buf = typebuf_init;
937 typebuf.tb_noremap = noremapbuf_init;
938 typebuf.tb_buflen = TYPELEN_INIT;
939 typebuf.tb_len = 0;
Bram Moolenaard34f9b12017-04-08 18:41:13 +0200940 typebuf.tb_off = MAXMAPLEN + 4;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 typebuf.tb_change_cnt = 1;
942 }
943}
944
945/*
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200946 * Returns TRUE when keys cannot be remapped.
947 */
948 int
949noremap_keys(void)
950{
951 return KeyNoremap & (RM_NONE|RM_SCRIPT);
952}
953
954/*
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100955 * Insert a string in position 'offset' in the typeahead buffer (for "@r"
956 * and ":normal" command, vgetorpeek() and check_termcode()).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 *
958 * If noremap is REMAP_YES, new string can be mapped again.
959 * If noremap is REMAP_NONE, new string cannot be mapped again.
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000960 * If noremap is REMAP_SKIP, first char of new string cannot be mapped again,
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000961 * but abbreviations are allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 * If noremap is REMAP_SCRIPT, new string cannot be mapped again, except for
963 * script-local mappings.
964 * If noremap is > 0, that many characters of the new string cannot be mapped.
965 *
966 * If nottyped is TRUE, the string does not return KeyTyped (don't use when
967 * offset is non-zero!).
968 *
969 * If silent is TRUE, cmd_silent is set when the characters are obtained.
970 *
971 * return FAIL for failure, OK otherwise
972 */
973 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100974ins_typebuf(
975 char_u *str,
976 int noremap,
977 int offset,
978 int nottyped,
979 int silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980{
981 char_u *s1, *s2;
982 int newlen;
983 int addlen;
984 int i;
985 int newoff;
986 int val;
987 int nrm;
988
989 init_typebuf();
990 if (++typebuf.tb_change_cnt == 0)
991 typebuf.tb_change_cnt = 1;
Bram Moolenaard103ee72019-09-18 21:15:31 +0200992 state_no_longer_safe("ins_typebuf()");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993
994 addlen = (int)STRLEN(str);
Bram Moolenaar4e86cba2007-05-06 11:56:32 +0000995
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 if (offset == 0 && addlen <= typebuf.tb_off)
997 {
Bram Moolenaarcaa55b62017-01-10 13:51:09 +0100998 /*
999 * Easy case: there is room in front of typebuf.tb_buf[typebuf.tb_off]
1000 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 typebuf.tb_off -= addlen;
1002 mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen);
1003 }
Bram Moolenaard34f9b12017-04-08 18:41:13 +02001004 else if (typebuf.tb_len == 0 && typebuf.tb_buflen
1005 >= addlen + 3 * (MAXMAPLEN + 4))
1006 {
1007 /*
1008 * Buffer is empty and string fits in the existing buffer.
1009 * Leave some space before and after, if possible.
1010 */
1011 typebuf.tb_off = (typebuf.tb_buflen - addlen - 3 * (MAXMAPLEN + 4)) / 2;
1012 mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen);
1013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 else
1015 {
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001016 int extra;
1017
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001018 /*
1019 * Need to allocate a new buffer.
Bram Moolenaard34f9b12017-04-08 18:41:13 +02001020 * In typebuf.tb_buf there must always be room for 3 * (MAXMAPLEN + 4)
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001021 * characters. We add some extra room to avoid having to allocate too
1022 * often.
1023 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 newoff = MAXMAPLEN + 4;
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001025 extra = addlen + newoff + 4 * (MAXMAPLEN + 4);
1026 if (typebuf.tb_len > 2147483647 - extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 {
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001028 // string is getting too long for a 32 bit int
Bram Moolenaar12f3c1b2021-12-05 21:46:34 +00001029 emsg(_(e_command_too_complex)); // also calls flush_buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 setcursor();
1031 return FAIL;
1032 }
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001033 newlen = typebuf.tb_len + extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 s1 = alloc(newlen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001035 if (s1 == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 return FAIL;
1037 s2 = alloc(newlen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001038 if (s2 == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 {
1040 vim_free(s1);
1041 return FAIL;
1042 }
1043 typebuf.tb_buflen = newlen;
1044
Bram Moolenaar30613902019-12-01 22:11:18 +01001045 // copy the old chars, before the insertion point
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 mch_memmove(s1 + newoff, typebuf.tb_buf + typebuf.tb_off,
1047 (size_t)offset);
Bram Moolenaar30613902019-12-01 22:11:18 +01001048 // copy the new chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 mch_memmove(s1 + newoff + offset, str, (size_t)addlen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001050 // copy the old chars, after the insertion point, including the NUL at
1051 // the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 mch_memmove(s1 + newoff + offset + addlen,
1053 typebuf.tb_buf + typebuf.tb_off + offset,
1054 (size_t)(typebuf.tb_len - offset + 1));
1055 if (typebuf.tb_buf != typebuf_init)
1056 vim_free(typebuf.tb_buf);
1057 typebuf.tb_buf = s1;
1058
1059 mch_memmove(s2 + newoff, typebuf.tb_noremap + typebuf.tb_off,
1060 (size_t)offset);
1061 mch_memmove(s2 + newoff + offset + addlen,
1062 typebuf.tb_noremap + typebuf.tb_off + offset,
1063 (size_t)(typebuf.tb_len - offset));
1064 if (typebuf.tb_noremap != noremapbuf_init)
1065 vim_free(typebuf.tb_noremap);
1066 typebuf.tb_noremap = s2;
1067
1068 typebuf.tb_off = newoff;
1069 }
1070 typebuf.tb_len += addlen;
1071
Bram Moolenaar30613902019-12-01 22:11:18 +01001072 // If noremap == REMAP_SCRIPT: do remap script-local mappings.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 if (noremap == REMAP_SCRIPT)
1074 val = RM_SCRIPT;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001075 else if (noremap == REMAP_SKIP)
1076 val = RM_ABBR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 else
1078 val = RM_NONE;
1079
1080 /*
1081 * Adjust typebuf.tb_noremap[] for the new characters:
1082 * If noremap == REMAP_NONE or REMAP_SCRIPT: new characters are
1083 * (sometimes) not remappable
1084 * If noremap == REMAP_YES: all the new characters are mappable
1085 * If noremap > 0: "noremap" characters are not remappable, the rest
1086 * mappable
1087 */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001088 if (noremap == REMAP_SKIP)
1089 nrm = 1;
1090 else if (noremap < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 nrm = addlen;
1092 else
1093 nrm = noremap;
1094 for (i = 0; i < addlen; ++i)
1095 typebuf.tb_noremap[typebuf.tb_off + i + offset] =
1096 (--nrm >= 0) ? val : RM_YES;
1097
Bram Moolenaar30613902019-12-01 22:11:18 +01001098 // tb_maplen and tb_silent only remember the length of mapped and/or
1099 // silent mappings at the start of the buffer, assuming that a mapped
1100 // sequence doesn't result in typed characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 if (nottyped || typebuf.tb_maplen > offset)
1102 typebuf.tb_maplen += addlen;
1103 if (silent || typebuf.tb_silent > offset)
1104 {
1105 typebuf.tb_silent += addlen;
1106 cmd_silent = TRUE;
1107 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001108 if (typebuf.tb_no_abbr_cnt && offset == 0) // and not used for abbrev.s
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 typebuf.tb_no_abbr_cnt += addlen;
1110
1111 return OK;
1112}
1113
1114/*
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001115 * Put character "c" back into the typeahead buffer.
1116 * Can be used for a character obtained by vgetc() that needs to be put back.
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00001117 * Uses cmd_silent, KeyTyped and KeyNoremap to restore the flags belonging to
1118 * the char.
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001119 * Returns the length of what was inserted.
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001120 */
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001121 int
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001122ins_char_typebuf(int c, int modifier)
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001123{
zeertzjq6cac7702022-01-04 18:01:21 +00001124 char_u buf[MB_MAXBYTES * 3 + 4];
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001125 int len = 0;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001126
1127 if (modifier != 0)
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001128 {
1129 buf[0] = K_SPECIAL;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001130 buf[1] = KS_MODIFIER;
1131 buf[2] = modifier;
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001132 buf[3] = NUL;
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001133 len = 3;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001134 }
1135 if (IS_SPECIAL(c))
1136 {
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001137 buf[len] = K_SPECIAL;
1138 buf[len + 1] = K_SECOND(c);
1139 buf[len + 2] = K_THIRD(c);
1140 buf[len + 3] = NUL;
1141 len += 3;
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001142 }
1143 else
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001144 {
zeertzjq6cac7702022-01-04 18:01:21 +00001145 char_u *p = buf + len;
1146 int char_len = (*mb_char2bytes)(c, p);
1147#ifdef FEAT_GUI
1148 int save_gui_in_use = gui.in_use;
1149
1150 gui.in_use = FALSE;
1151#endif
1152 // if the character contains CSI or K_SPECIAL bytes they need escaping
1153 len += fix_input_buffer(p, char_len);
1154#ifdef FEAT_GUI
1155 gui.in_use = save_gui_in_use;
1156#endif
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001157 }
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00001158 (void)ins_typebuf(buf, KeyNoremap, 0, !KeyTyped, cmd_silent);
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001159 return len;
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001160}
1161
1162/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 * Return TRUE if the typeahead buffer was changed (while waiting for a
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001164 * character to arrive). Happens when a message was received from a client or
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001165 * from feedkeys().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 * But check in a more generic way to avoid trouble: When "typebuf.tb_buf"
1167 * changed it was reallocated and the old pointer can no longer be used.
1168 * Or "typebuf.tb_off" may have been changed and we would overwrite characters
1169 * that was just added.
1170 */
1171 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001172typebuf_changed(
Bram Moolenaar30613902019-12-01 22:11:18 +01001173 int tb_change_cnt) // old value of typebuf.tb_change_cnt
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174{
1175 return (tb_change_cnt != 0 && (typebuf.tb_change_cnt != tb_change_cnt
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001176#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
1177 || typebuf_was_filled
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178#endif
1179 ));
1180}
1181
1182/*
1183 * Return TRUE if there are no characters in the typeahead buffer that have
1184 * not been typed (result from a mapping or come from ":normal").
1185 */
1186 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001187typebuf_typed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188{
1189 return typebuf.tb_maplen == 0;
1190}
1191
1192/*
1193 * Return the number of characters that are mapped (or not typed).
1194 */
1195 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001196typebuf_maplen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197{
1198 return typebuf.tb_maplen;
1199}
1200
1201/*
1202 * remove "len" characters from typebuf.tb_buf[typebuf.tb_off + offset]
1203 */
1204 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001205del_typebuf(int len, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206{
1207 int i;
1208
1209 if (len == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01001210 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211
1212 typebuf.tb_len -= len;
1213
1214 /*
1215 * Easy case: Just increase typebuf.tb_off.
1216 */
1217 if (offset == 0 && typebuf.tb_buflen - (typebuf.tb_off + len)
1218 >= 3 * MAXMAPLEN + 3)
1219 typebuf.tb_off += len;
1220 /*
1221 * Have to move the characters in typebuf.tb_buf[] and typebuf.tb_noremap[]
1222 */
1223 else
1224 {
1225 i = typebuf.tb_off + offset;
1226 /*
1227 * Leave some extra room at the end to avoid reallocation.
1228 */
1229 if (typebuf.tb_off > MAXMAPLEN)
1230 {
1231 mch_memmove(typebuf.tb_buf + MAXMAPLEN,
1232 typebuf.tb_buf + typebuf.tb_off, (size_t)offset);
1233 mch_memmove(typebuf.tb_noremap + MAXMAPLEN,
1234 typebuf.tb_noremap + typebuf.tb_off, (size_t)offset);
1235 typebuf.tb_off = MAXMAPLEN;
1236 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001237 // adjust typebuf.tb_buf (include the NUL at the end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset,
1239 typebuf.tb_buf + i + len,
1240 (size_t)(typebuf.tb_len - offset + 1));
Bram Moolenaar30613902019-12-01 22:11:18 +01001241 // adjust typebuf.tb_noremap[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 mch_memmove(typebuf.tb_noremap + typebuf.tb_off + offset,
1243 typebuf.tb_noremap + i + len,
1244 (size_t)(typebuf.tb_len - offset));
1245 }
1246
Bram Moolenaar30613902019-12-01 22:11:18 +01001247 if (typebuf.tb_maplen > offset) // adjust tb_maplen
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 {
1249 if (typebuf.tb_maplen < offset + len)
1250 typebuf.tb_maplen = offset;
1251 else
1252 typebuf.tb_maplen -= len;
1253 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001254 if (typebuf.tb_silent > offset) // adjust tb_silent
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 {
1256 if (typebuf.tb_silent < offset + len)
1257 typebuf.tb_silent = offset;
1258 else
1259 typebuf.tb_silent -= len;
1260 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001261 if (typebuf.tb_no_abbr_cnt > offset) // adjust tb_no_abbr_cnt
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 {
1263 if (typebuf.tb_no_abbr_cnt < offset + len)
1264 typebuf.tb_no_abbr_cnt = offset;
1265 else
1266 typebuf.tb_no_abbr_cnt -= len;
1267 }
1268
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001269#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar30613902019-12-01 22:11:18 +01001270 // Reset the flag that text received from a client or from feedkeys()
1271 // was inserted in the typeahead buffer.
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001272 typebuf_was_filled = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273#endif
1274 if (++typebuf.tb_change_cnt == 0)
1275 typebuf.tb_change_cnt = 1;
1276}
1277
1278/*
1279 * Write typed characters to script file.
1280 * If recording is on put the character in the recordbuffer.
1281 */
1282 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001283gotchars(char_u *chars, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284{
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001285 char_u *s = chars;
1286 int i;
1287 static char_u buf[4];
1288 static int buflen = 0;
1289 int todo = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290
Bram Moolenaar4e86cba2007-05-06 11:56:32 +00001291 while (todo--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 {
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001293 buf[buflen++] = *s++;
1294
1295 // When receiving a special key sequence, store it until we have all
1296 // the bytes and we can decide what to do with it.
1297 if (buflen == 1 && buf[0] == K_SPECIAL)
1298 continue;
1299 if (buflen == 2)
1300 continue;
1301 if (buflen == 3 && buf[1] == KS_EXTRA
1302 && (buf[2] == KE_FOCUSGAINED || buf[2] == KE_FOCUSLOST))
1303 {
1304 // Drop K_FOCUSGAINED and K_FOCUSLOST, they are not useful in a
1305 // recording.
1306 buflen = 0;
1307 continue;
1308 }
1309
Bram Moolenaar30613902019-12-01 22:11:18 +01001310 // Handle one byte at a time; no translation to be done.
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001311 for (i = 0; i < buflen; ++i)
1312 updatescript(buf[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001314 if (reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 {
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001316 buf[buflen] = NUL;
1317 add_buff(&recordbuff, buf, (long)buflen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001318 // remember how many chars were last recorded
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001319 last_recorded_len += buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 }
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001321 buflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 }
1323 may_sync_undo();
1324
1325#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01001326 // output "debug mode" message next time in debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 debug_did_msg = FALSE;
1328#endif
1329
Bram Moolenaar30613902019-12-01 22:11:18 +01001330 // Since characters have been typed, consider the following to be in
1331 // another mapping. Search string will be kept in history.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 ++maptick;
1333}
1334
1335/*
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001336 * Undo the last gotchars() for "len" bytes. To be used when putting a typed
1337 * character back into the typeahead buffer, thus gotchars() will be called
1338 * again.
1339 * Only affects recorded characters.
1340 */
1341 void
1342ungetchars(int len)
1343{
1344 if (reg_recording != 0)
1345 {
1346 delete_buff_tail(&recordbuff, len);
1347 last_recorded_len -= len;
1348 }
1349}
1350
1351/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 * Sync undo. Called when typed characters are obtained from the typeahead
1353 * buffer, or when a menu is used.
1354 * Do not sync:
1355 * - In Insert mode, unless cursor key has been used.
1356 * - While reading a script file.
1357 * - When no_u_sync is non-zero.
1358 */
1359 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001360may_sync_undo(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361{
1362 if ((!(State & (INSERT + CMDLINE)) || arrow_used)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001363 && scriptin[curscript] == NULL)
1364 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365}
1366
1367/*
1368 * Make "typebuf" empty and allocate new buffers.
1369 * Returns FAIL when out of memory.
1370 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001371 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001372alloc_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373{
1374 typebuf.tb_buf = alloc(TYPELEN_INIT);
1375 typebuf.tb_noremap = alloc(TYPELEN_INIT);
1376 if (typebuf.tb_buf == NULL || typebuf.tb_noremap == NULL)
1377 {
1378 free_typebuf();
1379 return FAIL;
1380 }
1381 typebuf.tb_buflen = TYPELEN_INIT;
Bram Moolenaar30613902019-12-01 22:11:18 +01001382 typebuf.tb_off = MAXMAPLEN + 4; // can insert without realloc
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 typebuf.tb_len = 0;
1384 typebuf.tb_maplen = 0;
1385 typebuf.tb_silent = 0;
1386 typebuf.tb_no_abbr_cnt = 0;
1387 if (++typebuf.tb_change_cnt == 0)
1388 typebuf.tb_change_cnt = 1;
Bram Moolenaare49b4bb2020-03-11 13:01:40 +01001389#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
1390 typebuf_was_filled = FALSE;
1391#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 return OK;
1393}
1394
1395/*
1396 * Free the buffers of "typebuf".
1397 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001398 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001399free_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400{
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001401 if (typebuf.tb_buf == typebuf_init)
Bram Moolenaar95f09602016-11-10 20:01:45 +01001402 internal_error("Free typebuf 1");
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001403 else
Bram Moolenaar0f1c6702019-09-28 15:24:00 +02001404 VIM_CLEAR(typebuf.tb_buf);
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00001405 if (typebuf.tb_noremap == noremapbuf_init)
Bram Moolenaar95f09602016-11-10 20:01:45 +01001406 internal_error("Free typebuf 2");
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001407 else
Bram Moolenaar0f1c6702019-09-28 15:24:00 +02001408 VIM_CLEAR(typebuf.tb_noremap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409}
1410
1411/*
1412 * When doing ":so! file", the current typeahead needs to be saved, and
1413 * restored when "file" has been read completely.
1414 */
1415static typebuf_T saved_typebuf[NSCRIPT];
1416
1417 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001418save_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419{
1420 init_typebuf();
1421 saved_typebuf[curscript] = typebuf;
Bram Moolenaar30613902019-12-01 22:11:18 +01001422 // If out of memory: restore typebuf and close file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 if (alloc_typebuf() == FAIL)
1424 {
1425 closescript();
1426 return FAIL;
1427 }
1428 return OK;
1429}
1430
Bram Moolenaar30613902019-12-01 22:11:18 +01001431static int old_char = -1; // character put back by vungetc()
1432static int old_mod_mask; // mod_mask for ungotten character
1433static int old_mouse_row; // mouse_row related to old_char
1434static int old_mouse_col; // mouse_col related to old_char
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001435
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436/*
1437 * Save all three kinds of typeahead, so that the user must type at a prompt.
1438 */
1439 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001440save_typeahead(tasave_T *tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441{
1442 tp->save_typebuf = typebuf;
1443 tp->typebuf_valid = (alloc_typebuf() == OK);
1444 if (!tp->typebuf_valid)
1445 typebuf = tp->save_typebuf;
1446
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001447 tp->old_char = old_char;
1448 tp->old_mod_mask = old_mod_mask;
1449 old_char = -1;
1450
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001451 tp->save_readbuf1 = readbuf1;
Bram Moolenaar285e3352018-04-18 23:01:13 +02001452 readbuf1.bh_first.b_next = NULL;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001453 tp->save_readbuf2 = readbuf2;
Bram Moolenaar285e3352018-04-18 23:01:13 +02001454 readbuf2.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455# ifdef USE_INPUT_BUF
1456 tp->save_inputbuf = get_input_buf();
1457# endif
1458}
1459
1460/*
1461 * Restore the typeahead to what it was before calling save_typeahead().
1462 * The allocated memory is freed, can only be called once!
Bram Moolenaarc41badb2021-06-07 22:04:52 +02001463 * When "overwrite" is FALSE input typed later is kept.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 */
1465 void
Bram Moolenaarc41badb2021-06-07 22:04:52 +02001466restore_typeahead(tasave_T *tp, int overwrite UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467{
1468 if (tp->typebuf_valid)
1469 {
1470 free_typebuf();
1471 typebuf = tp->save_typebuf;
1472 }
1473
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001474 old_char = tp->old_char;
1475 old_mod_mask = tp->old_mod_mask;
1476
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001477 free_buff(&readbuf1);
1478 readbuf1 = tp->save_readbuf1;
1479 free_buff(&readbuf2);
1480 readbuf2 = tp->save_readbuf2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481# ifdef USE_INPUT_BUF
Bram Moolenaarc41badb2021-06-07 22:04:52 +02001482 set_input_buf(tp->save_inputbuf, overwrite);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483# endif
1484}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485
1486/*
1487 * Open a new script file for the ":source!" command.
1488 */
1489 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001490openscript(
1491 char_u *name,
Bram Moolenaar30613902019-12-01 22:11:18 +01001492 int directly) // when TRUE execute directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493{
1494 if (curscript + 1 == NSCRIPT)
1495 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001496 emsg(_(e_scripts_nested_too_deep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 return;
1498 }
Bram Moolenaar53575522019-05-22 22:38:25 +02001499
1500 // Disallow sourcing a file in the sandbox, the commands would be executed
1501 // later, possibly outside of the sandbox.
1502 if (check_secure())
1503 return;
1504
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001505#ifdef FEAT_EVAL
1506 if (ignore_script)
Bram Moolenaar30613902019-12-01 22:11:18 +01001507 // Not reading from script, also don't open one. Warning message?
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001508 return;
1509#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510
Bram Moolenaar30613902019-12-01 22:11:18 +01001511 if (scriptin[curscript] != NULL) // already reading script
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 ++curscript;
Bram Moolenaar30613902019-12-01 22:11:18 +01001513 // use NameBuff for expanded name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 expand_env(name, NameBuff, MAXPATHL);
1515 if ((scriptin[curscript] = mch_fopen((char *)NameBuff, READBIN)) == NULL)
1516 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001517 semsg(_(e_cant_open_file_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 if (curscript)
1519 --curscript;
1520 return;
1521 }
1522 if (save_typebuf() == FAIL)
1523 return;
1524
1525 /*
1526 * Execute the commands from the file right now when using ":source!"
1527 * after ":global" or ":argdo" or in a loop. Also when another command
1528 * follows. This means the display won't be updated. Don't do this
1529 * always, "make test" would fail.
1530 */
1531 if (directly)
1532 {
1533 oparg_T oa;
1534 int oldcurscript;
1535 int save_State = State;
1536 int save_restart_edit = restart_edit;
1537 int save_insertmode = p_im;
1538 int save_finish_op = finish_op;
1539 int save_msg_scroll = msg_scroll;
1540
1541 State = NORMAL;
Bram Moolenaar30613902019-12-01 22:11:18 +01001542 msg_scroll = FALSE; // no msg scrolling in Normal mode
1543 restart_edit = 0; // don't go to Insert mode
1544 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 clear_oparg(&oa);
1546 finish_op = FALSE;
1547
1548 oldcurscript = curscript;
1549 do
1550 {
Bram Moolenaar386b43e2019-05-19 21:57:11 +02001551 update_topline_cursor(); // update cursor position and topline
1552 normal_cmd(&oa, FALSE); // execute one command
Bram Moolenaarae97b942020-07-09 19:16:35 +02001553 (void)vpeekc(); // check for end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 }
1555 while (scriptin[oldcurscript] != NULL);
1556
1557 State = save_State;
1558 msg_scroll = save_msg_scroll;
1559 restart_edit = save_restart_edit;
1560 p_im = save_insertmode;
1561 finish_op = save_finish_op;
1562 }
1563}
1564
1565/*
1566 * Close the currently active input script.
1567 */
1568 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001569closescript(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570{
1571 free_typebuf();
1572 typebuf = saved_typebuf[curscript];
1573
1574 fclose(scriptin[curscript]);
1575 scriptin[curscript] = NULL;
1576 if (curscript > 0)
1577 --curscript;
1578}
1579
Bram Moolenaarea408852005-06-25 22:49:46 +00001580#if defined(EXITFREE) || defined(PROTO)
1581 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001582close_all_scripts(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00001583{
1584 while (scriptin[0] != NULL)
1585 closescript();
1586}
1587#endif
1588
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589/*
1590 * Return TRUE when reading keys from a script file.
1591 */
1592 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001593using_script(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594{
1595 return scriptin[curscript] != NULL;
1596}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597
1598/*
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001599 * This function is called just before doing a blocking wait. Thus after
1600 * waiting 'updatetime' for a character to arrive.
1601 */
1602 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001603before_blocking(void)
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001604{
1605 updatescript(0);
1606#ifdef FEAT_EVAL
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001607 if (may_garbage_collect)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001608 garbage_collect(FALSE);
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001609#endif
1610}
1611
1612/*
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001613 * updatescript() is called when a character can be written into the script file
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 * or when we have waited some time for a character (c == 0)
1615 *
1616 * All the changed memfiles are synced if c == 0 or when the number of typed
1617 * characters reaches 'updatecount' and 'updatecount' is non-zero.
1618 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001619 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001620updatescript(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621{
1622 static int count = 0;
1623
1624 if (c && scriptout)
1625 putc(c, scriptout);
1626 if (c == 0 || (p_uc > 0 && ++count >= p_uc))
1627 {
1628 ml_sync_all(c == 0, TRUE);
1629 count = 0;
1630 }
1631}
1632
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633/*
Bram Moolenaar975a8802020-06-06 22:36:24 +02001634 * Convert "c" plus "modifiers" to merge the effect of modifyOtherKeys into the
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001635 * character.
1636 */
1637 int
Bram Moolenaar975a8802020-06-06 22:36:24 +02001638merge_modifyOtherKeys(int c_arg, int *modifiers)
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001639{
1640 int c = c_arg;
1641
Bram Moolenaar975a8802020-06-06 22:36:24 +02001642 if (*modifiers & MOD_MASK_CTRL)
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001643 {
1644 if ((c >= '`' && c <= 0x7f) || (c >= '@' && c <= '_'))
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001645 c &= 0x1f;
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001646 else if (c == '6')
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001647 // CTRL-6 is equivalent to CTRL-^
1648 c = 0x1e;
Bram Moolenaarf4ae6b22020-05-30 19:52:46 +02001649#ifdef FEAT_GUI_GTK
1650 // These mappings look arbitrary at the first glance, but in fact
1651 // resemble quite exactly the behaviour of the GTK+ 1.2 GUI on my
1652 // machine. The only difference is BS vs. DEL for CTRL-8 (makes
1653 // more sense and is consistent with usual terminal behaviour).
1654 else if (c == '2')
1655 c = NUL;
1656 else if (c >= '3' && c <= '7')
1657 c = c ^ 0x28;
1658 else if (c == '8')
1659 c = BS;
1660 else if (c == '?')
1661 c = DEL;
1662#endif
1663 if (c != c_arg)
Bram Moolenaar975a8802020-06-06 22:36:24 +02001664 *modifiers &= ~MOD_MASK_CTRL;
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001665 }
Bram Moolenaar975a8802020-06-06 22:36:24 +02001666 if ((*modifiers & (MOD_MASK_META | MOD_MASK_ALT))
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001667 && c >= 0 && c <= 127)
1668 {
1669 c += 0x80;
Bram Moolenaar975a8802020-06-06 22:36:24 +02001670 *modifiers &= ~(MOD_MASK_META|MOD_MASK_ALT);
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001671 }
1672 return c;
1673}
1674
1675/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 * Get the next input character.
1677 * Can return a special key or a multi-byte character.
1678 * Can return NUL when called recursively, use safe_vgetc() if that's not
1679 * wanted.
1680 * This translates escaped K_SPECIAL and CSI bytes to a K_SPECIAL or CSI byte.
1681 * Collects the bytes of a multibyte character into the whole character.
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00001682 * Returns the modifiers in the global "mod_mask".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 */
1684 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001685vgetc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686{
1687 int c, c2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001689 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001692#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01001693 // Do garbage collection when garbagecollect() was called previously and
1694 // we are now at the toplevel.
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001695 if (may_garbage_collect && want_garbage_collect)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001696 garbage_collect(FALSE);
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001697#endif
1698
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 /*
1700 * If a character was put back with vungetc, it was already processed.
1701 * Return it directly.
1702 */
1703 if (old_char != -1)
1704 {
1705 c = old_char;
1706 old_char = -1;
1707 mod_mask = old_mod_mask;
Bram Moolenaarb8978712013-03-16 21:42:16 +01001708 mouse_row = old_mouse_row;
1709 mouse_col = old_mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001711 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 {
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001713 mod_mask = 0;
1714 vgetc_mod_mask = 0;
1715 vgetc_char = 0;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001716 last_recorded_len = 0;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001717
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001718 for (;;) // this is done twice if there are modifiers
1719 {
1720 int did_inc = FALSE;
Bram Moolenaar2455c4e2015-09-15 18:29:39 +02001721
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001722 if (mod_mask
Bram Moolenaar39719052017-09-05 22:20:46 +02001723#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001724 || im_is_preediting()
Bram Moolenaar39719052017-09-05 22:20:46 +02001725#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001726#if defined(FEAT_PROP_POPUP)
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001727 || popup_no_mapping()
1728#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001729 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 {
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001731 // no mapping after modifier has been read
1732 ++no_mapping;
1733 ++allow_keys;
1734 did_inc = TRUE; // mod_mask may change value
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 }
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001736 c = vgetorpeek(TRUE);
1737 if (did_inc)
1738 {
1739 --no_mapping;
1740 --allow_keys;
1741 }
1742
1743 // Get two extra bytes for special keys
1744 if (c == K_SPECIAL
1745#ifdef FEAT_GUI
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001746 || (c == CSI)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001747#endif
1748 )
1749 {
1750 int save_allow_keys = allow_keys;
1751
1752 ++no_mapping;
1753 allow_keys = 0; // make sure BS is not found
1754 c2 = vgetorpeek(TRUE); // no mapping for these chars
1755 c = vgetorpeek(TRUE);
1756 --no_mapping;
1757 allow_keys = save_allow_keys;
1758 if (c2 == KS_MODIFIER)
1759 {
1760 mod_mask = c;
1761 continue;
1762 }
1763 c = TO_SPECIAL(c2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764
Bram Moolenaar4f974752019-02-17 17:44:42 +01001765#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001766 // Handle K_TEAROFF here, the caller of vgetc() doesn't need to
1767 // know that a menu was torn off
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001768 if (
1769# ifdef VIMDLL
1770 gui.in_use &&
1771# endif
1772 c == K_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 {
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001774 char_u name[200];
1775 int i;
1776
1777 // get menu path, it ends with a <CR>
1778 for (i = 0; (c = vgetorpeek(TRUE)) != '\r'; )
1779 {
1780 name[i] = c;
1781 if (i < 199)
1782 ++i;
1783 }
1784 name[i] = NUL;
1785 gui_make_tearoff(name);
1786 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788#endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001789#if defined(FEAT_GUI) && defined(FEAT_GUI_GTK) && defined(FEAT_MENU)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001790 // GTK: <F10> normally selects the menu, but it's passed until
1791 // here to allow mapping it. Intercept and invoke the GTK
1792 // behavior if it's not mapped.
1793 if (c == K_F10 && gui.menubar != NULL)
1794 {
1795 gtk_menu_shell_select_first(
1796 GTK_MENU_SHELL(gui.menubar), FALSE);
1797 continue;
1798 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001799#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800#ifdef FEAT_GUI
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001801 // Handle focus event here, so that the caller doesn't need to
1802 // know about it. Return K_IGNORE so that we loop once (needed
1803 // if 'lazyredraw' is set).
1804 if (c == K_FOCUSGAINED || c == K_FOCUSLOST)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001805 {
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001806 ui_focus_change(c == K_FOCUSGAINED);
1807 c = K_IGNORE;
Bram Moolenaara9dd2d32019-04-29 21:58:41 +02001808 }
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001809
1810 // Translate K_CSI to CSI. The special key is only used to
1811 // avoid it being recognized as the start of a special key.
1812 if (c == K_CSI)
1813 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001815 }
1816 // a keypad or special function key was not mapped, use it like
1817 // its ASCII equivalent
1818 switch (c)
1819 {
1820 case K_KPLUS: c = '+'; break;
1821 case K_KMINUS: c = '-'; break;
1822 case K_KDIVIDE: c = '/'; break;
1823 case K_KMULTIPLY: c = '*'; break;
1824 case K_KENTER: c = CAR; break;
1825 case K_KPOINT:
Bram Moolenaar4f974752019-02-17 17:44:42 +01001826#ifdef MSWIN
Bram Moolenaar8e85db02018-07-27 23:16:51 +02001827 // Can be either '.' or a ',',
1828 // depending on the type of keypad.
1829 c = MapVirtualKey(VK_DECIMAL, 2); break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001830#else
Bram Moolenaar8e85db02018-07-27 23:16:51 +02001831 c = '.'; break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001832#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001833 case K_K0: c = '0'; break;
1834 case K_K1: c = '1'; break;
1835 case K_K2: c = '2'; break;
1836 case K_K3: c = '3'; break;
1837 case K_K4: c = '4'; break;
1838 case K_K5: c = '5'; break;
1839 case K_K6: c = '6'; break;
1840 case K_K7: c = '7'; break;
1841 case K_K8: c = '8'; break;
1842 case K_K9: c = '9'; break;
Bram Moolenaara88d9682005-03-25 21:45:43 +00001843
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001844 case K_XHOME:
1845 case K_ZHOME: if (mod_mask == MOD_MASK_SHIFT)
Bram Moolenaara88d9682005-03-25 21:45:43 +00001846 {
1847 c = K_S_HOME;
1848 mod_mask = 0;
1849 }
1850 else if (mod_mask == MOD_MASK_CTRL)
1851 {
1852 c = K_C_HOME;
1853 mod_mask = 0;
1854 }
1855 else
1856 c = K_HOME;
1857 break;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001858 case K_XEND:
1859 case K_ZEND: if (mod_mask == MOD_MASK_SHIFT)
Bram Moolenaara88d9682005-03-25 21:45:43 +00001860 {
1861 c = K_S_END;
1862 mod_mask = 0;
1863 }
1864 else if (mod_mask == MOD_MASK_CTRL)
1865 {
1866 c = K_C_END;
1867 mod_mask = 0;
1868 }
1869 else
1870 c = K_END;
1871 break;
1872
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001873 case K_XUP: c = K_UP; break;
1874 case K_XDOWN: c = K_DOWN; break;
1875 case K_XLEFT: c = K_LEFT; break;
1876 case K_XRIGHT: c = K_RIGHT; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001879 // For a multi-byte character get all the bytes and return the
1880 // converted character.
1881 // Note: This will loop until enough bytes are received!
1882 if (has_mbyte && (n = MB_BYTE2LEN_CHECK(c)) > 1)
1883 {
1884 ++no_mapping;
1885 buf[0] = c;
1886 for (i = 1; i < n; ++i)
1887 {
1888 buf[i] = vgetorpeek(TRUE);
1889 if (buf[i] == K_SPECIAL
1890#ifdef FEAT_GUI
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001891 || (buf[i] == CSI)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001892#endif
1893 )
1894 {
1895 // Must be a K_SPECIAL - KS_SPECIAL - KE_FILLER
1896 // sequence, which represents a K_SPECIAL (0x80),
1897 // or a CSI - KS_EXTRA - KE_CSI sequence, which
1898 // represents a CSI (0x9B),
1899 // or a K_SPECIAL - KS_EXTRA - KE_CSI, which is CSI
1900 // too.
1901 c = vgetorpeek(TRUE);
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01001902 if (vgetorpeek(TRUE) == KE_CSI && c == KS_EXTRA)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001903 buf[i] = CSI;
1904 }
1905 }
1906 --no_mapping;
1907 c = (*mb_ptr2char)(buf);
1908 }
1909
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02001910 if (vgetc_char == 0)
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001911 {
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001912 vgetc_mod_mask = mod_mask;
1913 vgetc_char = c;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001914 }
Bram Moolenaar00eab7f2019-10-10 21:49:28 +02001915
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001916 break;
1917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001919
1920#ifdef FEAT_EVAL
1921 /*
1922 * In the main loop "may_garbage_collect" can be set to do garbage
1923 * collection in the first next vgetc(). It's disabled after that to
1924 * avoid internally used Lists and Dicts to be freed.
1925 */
1926 may_garbage_collect = FALSE;
1927#endif
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001928
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001929#ifdef FEAT_BEVAL_TERM
Bram Moolenaarc2f50542019-07-05 23:24:56 +02001930 if (c != K_MOUSEMOVE && c != K_IGNORE && c != K_CURSORHOLD)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001931 {
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001932 // Don't trigger 'balloonexpr' unless only the mouse was moved.
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001933 bevalexpr_due_set = FALSE;
1934 ui_remove_balloon();
1935 }
1936#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001937#ifdef FEAT_PROP_POPUP
Bram Moolenaar4934ad02020-09-28 22:29:58 +02001938 // Only filter keys that do not come from ":normal". Keys from feedkeys()
1939 // are filtered.
1940 if ((!ex_normal_busy || in_feedkeys) && popup_do_filter(c))
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02001941 {
1942 if (c == Ctrl_C)
1943 got_int = FALSE; // avoid looping
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001944 c = K_IGNORE;
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02001945 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001946#endif
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001947
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001948 // Need to process the character before we know it's safe to do something
1949 // else.
1950 if (c != K_IGNORE)
Bram Moolenaard103ee72019-09-18 21:15:31 +02001951 state_no_longer_safe("key typed");
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001952
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001953 return c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954}
1955
1956/*
1957 * Like vgetc(), but never return a NUL when called recursively, get a key
1958 * directly from the user (ignoring typeahead).
1959 */
1960 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001961safe_vgetc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962{
1963 int c;
1964
1965 c = vgetc();
1966 if (c == NUL)
1967 c = get_keystroke();
1968 return c;
1969}
1970
1971/*
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001972 * Like safe_vgetc(), but loop to handle K_IGNORE.
1973 * Also ignore scrollbar events.
1974 */
1975 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001976plain_vgetc(void)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001977{
1978 int c;
1979
1980 do
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001981 c = safe_vgetc();
Bram Moolenaar3a006592021-05-05 19:58:17 +02001982 while (c == K_IGNORE
1983 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
1984 || c == K_MOUSEMOVE);
Bram Moolenaarec2da362017-01-21 20:04:22 +01001985
1986 if (c == K_PS)
Bram Moolenaar30613902019-12-01 22:11:18 +01001987 // Only handle the first pasted character. Drop the rest, since we
1988 // don't know what to do with it.
Bram Moolenaarec2da362017-01-21 20:04:22 +01001989 c = bracketed_paste(PASTE_ONE_CHAR, FALSE, NULL);
1990
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001991 return c;
1992}
1993
1994/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 * Check if a character is available, such that vgetc() will not block.
1996 * If the next character is a special character or multi-byte, the returned
1997 * character is not valid!.
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02001998 * Returns NUL if no character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 */
2000 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002001vpeekc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002{
2003 if (old_char != -1)
2004 return old_char;
2005 return vgetorpeek(FALSE);
2006}
2007
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01002008#if defined(FEAT_TERMRESPONSE) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009/*
2010 * Like vpeekc(), but don't allow mapping. Do allow checking for terminal
2011 * codes.
2012 */
2013 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002014vpeekc_nomap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015{
2016 int c;
2017
2018 ++no_mapping;
2019 ++allow_keys;
2020 c = vpeekc();
2021 --no_mapping;
2022 --allow_keys;
2023 return c;
2024}
2025#endif
2026
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027/*
2028 * Check if any character is available, also half an escape sequence.
2029 * Trick: when no typeahead found, but there is something in the typeahead
2030 * buffer, it must be an ESC that is recognized as the start of a key code.
2031 */
2032 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002033vpeekc_any(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034{
2035 int c;
2036
2037 c = vpeekc();
2038 if (c == NUL && typebuf.tb_len > 0)
2039 c = ESC;
2040 return c;
2041}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042
2043/*
2044 * Call vpeekc() without causing anything to be mapped.
2045 * Return TRUE if a character is available, FALSE otherwise.
2046 */
2047 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002048char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049{
2050 int retval;
2051
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01002052#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01002053 // When test_override("char_avail", 1) was called pretend there is no
2054 // typeahead.
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01002055 if (disable_char_avail_for_testing)
2056 return FALSE;
2057#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 ++no_mapping;
2059 retval = vpeekc();
2060 --no_mapping;
2061 return (retval != NUL);
2062}
2063
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002064#if defined(FEAT_EVAL) || defined(PROTO)
2065/*
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002066 * "getchar()" and "getcharstr()" functions
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002067 */
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002068 static void
2069getchar_common(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002070{
2071 varnumber_T n;
2072 int error = FALSE;
2073
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002074 if (in_vim9script() && check_for_opt_bool_arg(argvars, 0) == FAIL)
2075 return;
2076
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002077#ifdef MESSAGE_QUEUE
2078 // vpeekc() used to check for messages, but that caused problems, invoking
2079 // a callback where it was not expected. Some plugins use getchar(1) in a
2080 // loop to await a message, therefore make sure we check for messages here.
2081 parse_queued_messages();
2082#endif
2083
Bram Moolenaar30613902019-12-01 22:11:18 +01002084 // Position the cursor. Needed after a message that ends in a space.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002085 windgoto(msg_row, msg_col);
2086
2087 ++no_mapping;
2088 ++allow_keys;
2089 for (;;)
2090 {
2091 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar30613902019-12-01 22:11:18 +01002092 // getchar(): blocking wait.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002093 n = plain_vgetc();
Bram Moolenaarc08cc722020-09-05 17:51:23 +02002094 else if (tv_get_bool_chk(&argvars[0], &error))
Bram Moolenaar30613902019-12-01 22:11:18 +01002095 // getchar(1): only check if char avail
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002096 n = vpeekc_any();
2097 else if (error || vpeekc_any() == NUL)
Bram Moolenaar30613902019-12-01 22:11:18 +01002098 // illegal argument or getchar(0) and no char avail: return zero
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002099 n = 0;
2100 else
Bram Moolenaar15183b42020-09-05 19:59:39 +02002101 // getchar(0) and char avail() != NUL: get a character.
2102 // Note that vpeekc_any() returns K_SPECIAL for K_IGNORE.
2103 n = safe_vgetc();
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002104
Bram Moolenaar15183b42020-09-05 19:59:39 +02002105 if (n == K_IGNORE || n == K_MOUSEMOVE
2106 || n == K_VER_SCROLLBAR || n == K_HOR_SCROLLBAR)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002107 continue;
2108 break;
2109 }
2110 --no_mapping;
2111 --allow_keys;
2112
2113 set_vim_var_nr(VV_MOUSE_WIN, 0);
2114 set_vim_var_nr(VV_MOUSE_WINID, 0);
2115 set_vim_var_nr(VV_MOUSE_LNUM, 0);
2116 set_vim_var_nr(VV_MOUSE_COL, 0);
2117
2118 rettv->vval.v_number = n;
2119 if (IS_SPECIAL(n) || mod_mask != 0)
2120 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002121 char_u temp[10]; // modifier: 3, mbyte-char: 6, NUL: 1
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002122 int i = 0;
2123
Bram Moolenaar30613902019-12-01 22:11:18 +01002124 // Turn a special key into three bytes, plus modifier.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002125 if (mod_mask != 0)
2126 {
2127 temp[i++] = K_SPECIAL;
2128 temp[i++] = KS_MODIFIER;
2129 temp[i++] = mod_mask;
2130 }
2131 if (IS_SPECIAL(n))
2132 {
2133 temp[i++] = K_SPECIAL;
2134 temp[i++] = K_SECOND(n);
2135 temp[i++] = K_THIRD(n);
2136 }
2137 else if (has_mbyte)
2138 i += (*mb_char2bytes)(n, temp + i);
2139 else
2140 temp[i++] = n;
2141 temp[i++] = NUL;
2142 rettv->v_type = VAR_STRING;
2143 rettv->vval.v_string = vim_strsave(temp);
2144
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002145 if (is_mouse_key(n))
2146 {
2147 int row = mouse_row;
2148 int col = mouse_col;
2149 win_T *win;
2150 linenr_T lnum;
2151 win_T *wp;
2152 int winnr = 1;
2153
2154 if (row >= 0 && col >= 0)
2155 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002156 // Find the window at the mouse coordinates and compute the
2157 // text position.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002158 win = mouse_find_win(&row, &col, FIND_POPUP);
2159 if (win == NULL)
2160 return;
2161 (void)mouse_comp_pos(win, &row, &col, &lnum, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002162#ifdef FEAT_PROP_POPUP
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002163 if (WIN_IS_POPUP(win))
2164 winnr = 0;
2165 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002166#endif
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002167 for (wp = firstwin; wp != win && wp != NULL;
2168 wp = wp->w_next)
2169 ++winnr;
2170 set_vim_var_nr(VV_MOUSE_WIN, winnr);
2171 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
2172 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
2173 set_vim_var_nr(VV_MOUSE_COL, col + 1);
2174 }
2175 }
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002176 }
2177}
2178
2179/*
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002180 * "getchar()" function
2181 */
2182 void
2183f_getchar(typval_T *argvars, typval_T *rettv)
2184{
2185 getchar_common(argvars, rettv);
2186}
2187
2188/*
2189 * "getcharstr()" function
2190 */
2191 void
2192f_getcharstr(typval_T *argvars, typval_T *rettv)
2193{
2194 getchar_common(argvars, rettv);
2195
2196 if (rettv->v_type == VAR_NUMBER)
2197 {
2198 char_u temp[7]; // mbyte-char: 6, NUL: 1
2199 varnumber_T n = rettv->vval.v_number;
2200 int i = 0;
2201
2202 if (n != 0)
2203 {
2204 if (has_mbyte)
2205 i += (*mb_char2bytes)(n, temp + i);
2206 else
2207 temp[i++] = n;
2208 }
2209 temp[i++] = NUL;
2210 rettv->v_type = VAR_STRING;
2211 rettv->vval.v_string = vim_strsave(temp);
2212 }
2213}
2214
2215/*
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002216 * "getcharmod()" function
2217 */
2218 void
2219f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
2220{
2221 rettv->vval.v_number = mod_mask;
2222}
2223#endif // FEAT_EVAL
2224
2225#if defined(MESSAGE_QUEUE) || defined(PROTO)
2226# define MAX_REPEAT_PARSE 8
2227
2228/*
2229 * Process messages that have been queued for netbeans or clientserver.
2230 * Also check if any jobs have ended.
h-east965d2ed2021-10-04 21:51:57 +01002231 * These functions can call arbitrary Vim script and should only be called when
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002232 * it is safe to do so.
2233 */
2234 void
2235parse_queued_messages(void)
2236{
Bram Moolenaar1cac7092019-10-22 21:54:31 +02002237 int old_curwin_id;
2238 int old_curbuf_fnum;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002239 int i;
2240 int save_may_garbage_collect = may_garbage_collect;
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002241 static int entered = 0;
Bram Moolenaard103ee72019-09-18 21:15:31 +02002242 int was_safe = get_was_safe_state();
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002243
2244 // Do not handle messages while redrawing, because it may cause buffers to
2245 // change or be wiped while they are being redrawn.
Bram Moolenaar4778b4d2020-11-04 11:03:12 +01002246 // Also bail out when parsing messages was explicitly disabled.
2247 if (updating_screen || dont_parse_messages)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002248 return;
2249
Bram Moolenaar1cac7092019-10-22 21:54:31 +02002250 // If memory allocation fails during startup we'll exit but curbuf or
2251 // curwin could be NULL.
2252 if (curbuf == NULL || curwin == NULL)
2253 return;
2254
2255 old_curbuf_fnum = curbuf->b_fnum;
2256 old_curwin_id = curwin->w_id;
2257
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002258 ++entered;
2259
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002260 // may_garbage_collect is set in main_loop() to do garbage collection when
2261 // blocking to wait on a character. We don't want that while parsing
2262 // messages, a callback may invoke vgetc() while lists and dicts are in use
2263 // in the call stack.
2264 may_garbage_collect = FALSE;
2265
2266 // Loop when a job ended, but don't keep looping forever.
2267 for (i = 0; i < MAX_REPEAT_PARSE; ++i)
2268 {
2269 // For Win32 mch_breakcheck() does not check for input, do it here.
Bram Moolenaar80a8d382020-05-03 22:57:32 +02002270# if (defined(MSWIN) || defined(__HAIKU__)) && defined(FEAT_JOB_CHANNEL)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002271 channel_handle_events(FALSE);
2272# endif
2273
2274# ifdef FEAT_NETBEANS_INTG
2275 // Process the queued netbeans messages.
2276 netbeans_parse_messages();
2277# endif
2278# ifdef FEAT_JOB_CHANNEL
2279 // Write any buffer lines still to be written.
2280 channel_write_any_lines();
2281
2282 // Process the messages queued on channels.
2283 channel_parse_messages();
2284# endif
2285# if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
2286 // Process the queued clientserver messages.
2287 server_parse_messages();
2288# endif
2289# ifdef FEAT_JOB_CHANNEL
2290 // Check if any jobs have ended. If so, repeat the above to handle
2291 // changes, e.g. stdin may have been closed.
2292 if (job_check_ended())
2293 continue;
2294# endif
2295# ifdef FEAT_TERMINAL
2296 free_unused_terminals();
2297# endif
2298# ifdef FEAT_SOUND_CANBERRA
2299 if (has_sound_callback_in_queue())
2300 invoke_sound_callback();
2301# endif
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002302#ifdef SIGUSR1
2303 if (got_sigusr1)
2304 {
2305 apply_autocmds(EVENT_SIGUSR1, NULL, NULL, FALSE, curbuf);
2306 got_sigusr1 = FALSE;
2307 }
2308#endif
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002309 break;
2310 }
2311
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002312 // When not nested we'll go back to waiting for a typed character. If it
2313 // was safe before then this triggers a SafeStateAgain autocommand event.
Bram Moolenaard103ee72019-09-18 21:15:31 +02002314 if (entered == 1 && was_safe)
Bram Moolenaar37d18072019-09-17 20:28:38 +02002315 may_trigger_safestateagain();
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002316
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002317 may_garbage_collect = save_may_garbage_collect;
2318
2319 // If the current window or buffer changed we need to bail out of the
2320 // waiting loop. E.g. when a job exit callback closes the terminal window.
2321 if (curwin->w_id != old_curwin_id || curbuf->b_fnum != old_curbuf_fnum)
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02002322 ins_char_typebuf(K_IGNORE, 0);
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002323
2324 --entered;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002325}
2326#endif
2327
2328
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002329typedef enum {
2330 map_result_fail, // failed, break loop
2331 map_result_get, // get a character from typeahead
2332 map_result_retry, // try to map again
2333 map_result_nomatch // no matching mapping, get char
2334} map_result_T;
2335
2336/*
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002337 * Check if the bytes at the start of the typeahead buffer are a character used
2338 * in CTRL-X mode. This includes the form with a CTRL modifier.
2339 */
2340 static int
2341at_ctrl_x_key(void)
2342{
2343 char_u *p = typebuf.tb_buf + typebuf.tb_off;
2344 int c = *p;
2345
2346 if (typebuf.tb_len > 3
2347 && c == K_SPECIAL
2348 && p[1] == KS_MODIFIER
2349 && (p[2] & MOD_MASK_CTRL))
2350 c = p[3] & 0x1f;
2351 return vim_is_ctrl_x_key(c);
2352}
2353
2354/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002355 * Check if typebuf.tb_buf[] contains a modifier plus key that can be changed
Bram Moolenaar975a8802020-06-06 22:36:24 +02002356 * into just a key, apply that.
2357 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
2358 * + "max_offset"].
2359 * Return the length of the replaced bytes, zero if nothing changed.
2360 */
2361 static int
2362check_simplify_modifier(int max_offset)
2363{
2364 int offset;
2365 char_u *tp;
2366
2367 for (offset = 0; offset < max_offset; ++offset)
2368 {
2369 if (offset + 3 >= typebuf.tb_len)
2370 break;
2371 tp = typebuf.tb_buf + typebuf.tb_off + offset;
2372 if (tp[0] == K_SPECIAL && tp[1] == KS_MODIFIER)
2373 {
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002374 // A modifier was not used for a mapping, apply it to ASCII keys.
2375 // Shift would already have been applied.
Bram Moolenaar975a8802020-06-06 22:36:24 +02002376 int modifier = tp[2];
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002377 int c = tp[3];
2378 int new_c = merge_modifyOtherKeys(c, &modifier);
Bram Moolenaar975a8802020-06-06 22:36:24 +02002379
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002380 if (new_c != c)
Bram Moolenaar975a8802020-06-06 22:36:24 +02002381 {
2382 char_u new_string[MB_MAXBYTES];
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002383 int len;
Bram Moolenaar975a8802020-06-06 22:36:24 +02002384
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002385 if (offset == 0)
2386 {
2387 // At the start: remember the character and mod_mask before
2388 // merging, in some cases, e.g. at the hit-return prompt,
2389 // they are put back in the typeahead buffer.
2390 vgetc_char = c;
2391 vgetc_mod_mask = tp[2];
2392 }
2393 len = mb_char2bytes(new_c, new_string);
2394 if (modifier == 0)
2395 {
2396 if (put_string_in_typebuf(offset, 4, new_string, len,
Bram Moolenaar975a8802020-06-06 22:36:24 +02002397 NULL, 0, 0) == FAIL)
2398 return -1;
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002399 }
2400 else
2401 {
2402 tp[2] = modifier;
2403 if (put_string_in_typebuf(offset + 3, 1, new_string, len,
2404 NULL, 0, 0) == FAIL)
2405 return -1;
2406 }
Bram Moolenaar975a8802020-06-06 22:36:24 +02002407 return len;
2408 }
2409 }
2410 }
2411 return 0;
2412}
2413
2414/*
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002415 * Handle mappings in the typeahead buffer.
2416 * - When something was mapped, return map_result_retry for recursive mappings.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002417 * - When nothing mapped and typeahead has a character: return map_result_get.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002418 * - When there is no match yet, return map_result_nomatch, need to get more
2419 * typeahead.
2420 */
2421 static int
2422handle_mapping(
2423 int *keylenp,
2424 int *timedout,
2425 int *mapdepth)
2426{
2427 mapblock_T *mp = NULL;
2428 mapblock_T *mp2;
2429 mapblock_T *mp_match;
2430 int mp_match_len = 0;
2431 int max_mlen = 0;
2432 int tb_c1;
2433 int mlen;
2434#ifdef FEAT_LANGMAP
2435 int nolmaplen;
2436#endif
2437 int keylen = *keylenp;
2438 int i;
2439 int local_State = get_real_state();
2440
2441 /*
2442 * Check for a mappable key sequence.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002443 * Walk through one maphash[] list until we find an entry that matches.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002444 *
2445 * Don't look for mappings if:
2446 * - no_mapping set: mapping disabled (e.g. for CTRL-V)
2447 * - maphash_valid not set: no mappings present.
2448 * - typebuf.tb_buf[typebuf.tb_off] should not be remapped
2449 * - in insert or cmdline mode and 'paste' option set
Bram Moolenaareda35f72019-08-03 14:59:44 +02002450 * - waiting for "hit return to continue" and CR or SPACE typed
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002451 * - waiting for a char with --more--
2452 * - in Ctrl-X mode, and we get a valid char for that mode
2453 */
2454 tb_c1 = typebuf.tb_buf[typebuf.tb_off];
2455 if (no_mapping == 0 && is_maphash_valid()
2456 && (no_zero_mapping == 0 || tb_c1 != '0')
2457 && (typebuf.tb_maplen == 0
2458 || (p_remap
2459 && (typebuf.tb_noremap[typebuf.tb_off]
2460 & (RM_NONE|RM_ABBR)) == 0))
2461 && !(p_paste && (State & (INSERT + CMDLINE)))
2462 && !(State == HITRETURN && (tb_c1 == CAR || tb_c1 == ' '))
2463 && State != ASKMORE
2464 && State != CONFIRM
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002465 && !((ctrl_x_mode_not_default() && at_ctrl_x_key())
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002466 || (compl_status_local()
Bram Moolenaare2c453d2019-08-21 14:37:09 +02002467 && (tb_c1 == Ctrl_N || tb_c1 == Ctrl_P))))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002468 {
Bram Moolenaarc9983702020-05-28 21:03:53 +02002469#ifdef FEAT_GUI
2470 if (gui.in_use && tb_c1 == CSI && typebuf.tb_len >= 2
2471 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_MODIFIER)
2472 {
2473 // The GUI code sends CSI KS_MODIFIER {flags}, but mappings expect
2474 // K_SPECIAL KS_MODIFIER {flags}.
2475 tb_c1 = K_SPECIAL;
2476 }
2477#endif
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002478#ifdef FEAT_LANGMAP
2479 if (tb_c1 == K_SPECIAL)
2480 nolmaplen = 2;
2481 else
2482 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002483 LANGMAP_ADJUST(tb_c1, (State & (CMDLINE | INSERT)) == 0
2484 && get_real_state() != SELECTMODE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002485 nolmaplen = 0;
2486 }
2487#endif
2488 // First try buffer-local mappings.
2489 mp = get_buf_maphash_list(local_State, tb_c1);
2490 mp2 = get_maphash_list(local_State, tb_c1);
2491 if (mp == NULL)
2492 {
2493 // There are no buffer-local mappings.
2494 mp = mp2;
2495 mp2 = NULL;
2496 }
Bram Moolenaareda35f72019-08-03 14:59:44 +02002497
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002498 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002499 * Loop until a partly matching mapping is found or all (local)
2500 * mappings have been checked.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002501 * The longest full match is remembered in "mp_match".
Bram Moolenaareda35f72019-08-03 14:59:44 +02002502 * A full match is only accepted if there is no partly match, so "aa"
2503 * and "aaa" can both be mapped.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002504 */
2505 mp_match = NULL;
2506 mp_match_len = 0;
2507 for ( ; mp != NULL;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002508 mp->m_next == NULL ? (mp = mp2, mp2 = NULL) : (mp = mp->m_next))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002509 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002510 // Only consider an entry if the first character matches and it is
2511 // for the current state.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002512 // Skip ":lmap" mappings if keys were mapped.
2513 if (mp->m_keys[0] == tb_c1
2514 && (mp->m_mode & local_State)
Bram Moolenaar46cd43b2020-06-04 22:22:11 +02002515 && !(mp->m_simplified && seenModifyOtherKeys
2516 && typebuf.tb_maplen == 0)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002517 && ((mp->m_mode & LANGMAP) == 0 || typebuf.tb_maplen == 0))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002518 {
2519#ifdef FEAT_LANGMAP
2520 int nomap = nolmaplen;
2521 int c2;
2522#endif
2523 // find the match length of this mapping
2524 for (mlen = 1; mlen < typebuf.tb_len; ++mlen)
2525 {
2526#ifdef FEAT_LANGMAP
2527 c2 = typebuf.tb_buf[typebuf.tb_off + mlen];
2528 if (nomap > 0)
2529 --nomap;
2530 else if (c2 == K_SPECIAL)
2531 nomap = 2;
2532 else
2533 LANGMAP_ADJUST(c2, TRUE);
2534 if (mp->m_keys[mlen] != c2)
2535#else
2536 if (mp->m_keys[mlen] !=
Bram Moolenaarc9983702020-05-28 21:03:53 +02002537 typebuf.tb_buf[typebuf.tb_off + mlen])
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002538#endif
2539 break;
2540 }
2541
Bram Moolenaareda35f72019-08-03 14:59:44 +02002542 // Don't allow mapping the first byte(s) of a multi-byte char.
2543 // Happens when mapping <M-a> and then changing 'encoding'.
2544 // Beware that 0x80 is escaped.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002545 {
2546 char_u *p1 = mp->m_keys;
2547 char_u *p2 = mb_unescape(&p1);
2548
2549 if (has_mbyte && p2 != NULL
Bram Moolenaar1614a142019-10-06 22:00:13 +02002550 && MB_BYTE2LEN(tb_c1) > mb_ptr2len(p2))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002551 mlen = 0;
2552 }
2553
2554 // Check an entry whether it matches.
2555 // - Full match: mlen == keylen
2556 // - Partly match: mlen == typebuf.tb_len
2557 keylen = mp->m_keylen;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002558 if (mlen == keylen || (mlen == typebuf.tb_len
2559 && typebuf.tb_len < keylen))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002560 {
2561 char_u *s;
2562 int n;
2563
Bram Moolenaareda35f72019-08-03 14:59:44 +02002564 // If only script-local mappings are allowed, check if the
2565 // mapping starts with K_SNR.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002566 s = typebuf.tb_noremap + typebuf.tb_off;
2567 if (*s == RM_SCRIPT
2568 && (mp->m_keys[0] != K_SPECIAL
2569 || mp->m_keys[1] != KS_EXTRA
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01002570 || mp->m_keys[2] != KE_SNR))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002571 continue;
2572
Bram Moolenaareda35f72019-08-03 14:59:44 +02002573 // If one of the typed keys cannot be remapped, skip the
2574 // entry.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002575 for (n = mlen; --n >= 0; )
2576 if (*s++ & (RM_NONE|RM_ABBR))
2577 break;
2578 if (n >= 0)
2579 continue;
2580
2581 if (keylen > typebuf.tb_len)
2582 {
2583 if (!*timedout && !(mp_match != NULL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002584 && mp_match->m_nowait))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002585 {
2586 // break at a partly match
2587 keylen = KEYLEN_PART_MAP;
2588 break;
2589 }
2590 }
2591 else if (keylen > mp_match_len)
2592 {
2593 // found a longer match
2594 mp_match = mp;
2595 mp_match_len = keylen;
2596 }
2597 }
2598 else
Bram Moolenaareda35f72019-08-03 14:59:44 +02002599 // No match; may have to check for termcode at next
2600 // character.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002601 if (max_mlen < mlen)
2602 max_mlen = mlen;
2603 }
2604 }
2605
Bram Moolenaareda35f72019-08-03 14:59:44 +02002606 // If no partly match found, use the longest full match.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002607 if (keylen != KEYLEN_PART_MAP)
2608 {
2609 mp = mp_match;
2610 keylen = mp_match_len;
2611 }
2612 }
2613
2614 /*
2615 * Check for match with 'pastetoggle'
2616 */
2617 if (*p_pt != NUL && mp == NULL && (State & (INSERT|NORMAL)))
2618 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002619 for (mlen = 0; mlen < typebuf.tb_len && p_pt[mlen]; ++mlen)
2620 if (p_pt[mlen] != typebuf.tb_buf[typebuf.tb_off + mlen])
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002621 break;
2622 if (p_pt[mlen] == NUL) // match
2623 {
2624 // write chars to script file(s)
2625 if (mlen > typebuf.tb_maplen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002626 gotchars(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_maplen,
2627 mlen - typebuf.tb_maplen);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002628
2629 del_typebuf(mlen, 0); // remove the chars
Bram Moolenaareda35f72019-08-03 14:59:44 +02002630 set_option_value((char_u *)"paste", (long)!p_paste, NULL, 0);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002631 if (!(State & INSERT))
2632 {
2633 msg_col = 0;
2634 msg_row = Rows - 1;
2635 msg_clr_eos(); // clear ruler
2636 }
2637 status_redraw_all();
2638 redraw_statuslines();
2639 showmode();
2640 setcursor();
2641 *keylenp = keylen;
2642 return map_result_retry;
2643 }
2644 // Need more chars for partly match.
2645 if (mlen == typebuf.tb_len)
2646 keylen = KEYLEN_PART_KEY;
2647 else if (max_mlen < mlen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002648 // no match, may have to check for termcode at next character
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002649 max_mlen = mlen + 1;
2650 }
2651
Bram Moolenaareda35f72019-08-03 14:59:44 +02002652 if ((mp == NULL || max_mlen >= mp_match_len) && keylen != KEYLEN_PART_MAP)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002653 {
2654 int save_keylen = keylen;
2655
2656 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002657 * When no matching mapping found or found a non-matching mapping that
2658 * matches at least what the matching mapping matched:
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002659 * Check if we have a terminal code, when:
Bram Moolenaareda35f72019-08-03 14:59:44 +02002660 * - mapping is allowed,
2661 * - keys have not been mapped,
2662 * - and not an ESC sequence, not in insert mode or p_ek is on,
2663 * - and when not timed out,
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002664 */
2665 if ((no_mapping == 0 || allow_keys != 0)
2666 && (typebuf.tb_maplen == 0
2667 || (p_remap && typebuf.tb_noremap[
Bram Moolenaareda35f72019-08-03 14:59:44 +02002668 typebuf.tb_off] == RM_YES))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002669 && !*timedout)
2670 {
Bram Moolenaar0684e362020-12-03 19:54:42 +01002671 keylen = check_termcode(max_mlen + 1, NULL, 0, NULL);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002672
Bram Moolenaar0684e362020-12-03 19:54:42 +01002673 // If no termcode matched but 'pastetoggle' matched partially
2674 // it's like an incomplete key sequence.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002675 if (keylen == 0 && save_keylen == KEYLEN_PART_KEY)
2676 keylen = KEYLEN_PART_KEY;
2677
Bram Moolenaar975a8802020-06-06 22:36:24 +02002678 // If no termcode matched, try to include the modifier into the
zeertzjqa3f83fe2021-11-22 12:47:39 +00002679 // key. This is for when modifyOtherKeys is working.
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002680 if (keylen == 0 && !no_reduce_keys)
Bram Moolenaar975a8802020-06-06 22:36:24 +02002681 keylen = check_simplify_modifier(max_mlen + 1);
2682
Bram Moolenaareda35f72019-08-03 14:59:44 +02002683 // When getting a partial match, but the last characters were not
2684 // typed, don't wait for a typed character to complete the
2685 // termcode. This helps a lot when a ":normal" command ends in an
2686 // ESC.
2687 if (keylen < 0 && typebuf.tb_len == typebuf.tb_maplen)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002688 keylen = 0;
2689 }
2690 else
2691 keylen = 0;
2692 if (keylen == 0) // no matching terminal code
2693 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002694#ifdef AMIGA
2695 // check for window bounds report
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002696 if (typebuf.tb_maplen == 0 && (typebuf.tb_buf[
Bram Moolenaareda35f72019-08-03 14:59:44 +02002697 typebuf.tb_off] & 0xff) == CSI)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002698 {
2699 char_u *s;
2700
2701 for (s = typebuf.tb_buf + typebuf.tb_off + 1;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002702 s < typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len
2703 && (VIM_ISDIGIT(*s) || *s == ';' || *s == ' ');
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002704 ++s)
2705 ;
2706 if (*s == 'r' || *s == '|') // found one
2707 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002708 del_typebuf(
2709 (int)(s + 1 - (typebuf.tb_buf + typebuf.tb_off)), 0);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002710 // get size and redraw screen
2711 shell_resized();
2712 *keylenp = keylen;
2713 return map_result_retry;
2714 }
2715 if (*s == NUL) // need more characters
2716 keylen = KEYLEN_PART_KEY;
2717 }
2718 if (keylen >= 0)
2719#endif
Bram Moolenaareda35f72019-08-03 14:59:44 +02002720 // When there was a matching mapping and no termcode could be
2721 // replaced after another one, use that mapping (loop around).
2722 // If there was no mapping at all use the character from the
2723 // typeahead buffer right here.
2724 if (mp == NULL)
2725 {
2726 *keylenp = keylen;
Bram Moolenaar52797ba2021-12-16 14:45:13 +00002727 return map_result_get; // get character from typeahead
Bram Moolenaareda35f72019-08-03 14:59:44 +02002728 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002729 }
2730
2731 if (keylen > 0) // full matching terminal code
2732 {
2733#if defined(FEAT_GUI) && defined(FEAT_MENU)
2734 if (typebuf.tb_len >= 2
Bram Moolenaareda35f72019-08-03 14:59:44 +02002735 && typebuf.tb_buf[typebuf.tb_off] == K_SPECIAL
2736 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_MENU)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002737 {
2738 int idx;
2739
Bram Moolenaareda35f72019-08-03 14:59:44 +02002740 // Using a menu may cause a break in undo! It's like using
2741 // gotchars(), but without recording or writing to a script
2742 // file.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002743 may_sync_undo();
2744 del_typebuf(3, 0);
2745 idx = get_menu_index(current_menu, local_State);
2746 if (idx != MENU_INDEX_INVALID)
2747 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002748 // In Select mode and a Visual mode menu is used: Switch
2749 // to Visual mode temporarily. Append K_SELECT to switch
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002750 // back to Select mode.
2751 if (VIsual_active && VIsual_select
Bram Moolenaareda35f72019-08-03 14:59:44 +02002752 && (current_menu->modes & VISUAL))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002753 {
2754 VIsual_select = FALSE;
2755 (void)ins_typebuf(K_SELECT_STRING,
Bram Moolenaareda35f72019-08-03 14:59:44 +02002756 REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002757 }
2758 ins_typebuf(current_menu->strings[idx],
2759 current_menu->noremap[idx],
Bram Moolenaareda35f72019-08-03 14:59:44 +02002760 0, TRUE, current_menu->silent[idx]);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002761 }
2762 }
2763#endif // FEAT_GUI && FEAT_MENU
2764 *keylenp = keylen;
2765 return map_result_retry; // try mapping again
2766 }
2767
Bram Moolenaareda35f72019-08-03 14:59:44 +02002768 // Partial match: get some more characters. When a matching mapping
2769 // was found use that one.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002770 if (mp == NULL || keylen < 0)
2771 keylen = KEYLEN_PART_KEY;
2772 else
2773 keylen = mp_match_len;
2774 }
2775
2776 /*
2777 * complete match
2778 */
2779 if (keylen >= 0 && keylen <= typebuf.tb_len)
2780 {
2781 char_u *map_str;
2782
2783#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002784 int save_m_expr;
2785 int save_m_noremap;
2786 int save_m_silent;
2787 char_u *save_m_keys;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002788#else
2789# define save_m_noremap mp->m_noremap
2790# define save_m_silent mp->m_silent
2791#endif
2792
2793 // write chars to script file(s)
2794 if (keylen > typebuf.tb_maplen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002795 gotchars(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_maplen,
2796 keylen - typebuf.tb_maplen);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002797
2798 cmd_silent = (typebuf.tb_silent > 0);
2799 del_typebuf(keylen, 0); // remove the mapped keys
2800
2801 /*
2802 * Put the replacement string in front of mapstr.
2803 * The depth check catches ":map x y" and ":map y x".
2804 */
2805 if (++*mapdepth >= p_mmd)
2806 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00002807 emsg(_(e_recursive_mapping));
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002808 if (State & CMDLINE)
2809 redrawcmdline();
2810 else
2811 setcursor();
2812 flush_buffers(FLUSH_MINIMAL);
Bram Moolenaar30613902019-12-01 22:11:18 +01002813 *mapdepth = 0; // for next one
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002814 *keylenp = keylen;
2815 return map_result_fail;
2816 }
2817
2818 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002819 * In Select mode and a Visual mode mapping is used: Switch to Visual
2820 * mode temporarily. Append K_SELECT to switch back to Select mode.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002821 */
Bram Moolenaareda35f72019-08-03 14:59:44 +02002822 if (VIsual_active && VIsual_select && (mp->m_mode & VISUAL))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002823 {
2824 VIsual_select = FALSE;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002825 (void)ins_typebuf(K_SELECT_STRING, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002826 }
2827
2828#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002829 // Copy the values from *mp that are used, because evaluating the
2830 // expression may invoke a function that redefines the mapping, thereby
2831 // making *mp invalid.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002832 save_m_expr = mp->m_expr;
2833 save_m_noremap = mp->m_noremap;
2834 save_m_silent = mp->m_silent;
2835 save_m_keys = NULL; // only saved when needed
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002836
2837 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002838 * Handle ":map <expr>": evaluate the {rhs} as an expression. Also
2839 * save and restore the command line for "normal :".
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002840 */
2841 if (mp->m_expr)
2842 {
2843 int save_vgetc_busy = vgetc_busy;
2844 int save_may_garbage_collect = may_garbage_collect;
Bram Moolenaar4ebe0e62019-11-22 20:55:40 +01002845 int was_screen_col = screen_cur_col;
2846 int was_screen_row = screen_cur_row;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002847
2848 vgetc_busy = 0;
2849 may_garbage_collect = FALSE;
2850
2851 save_m_keys = vim_strsave(mp->m_keys);
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002852 map_str = eval_map_expr(mp, NUL);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002853
Bram Moolenaar4ebe0e62019-11-22 20:55:40 +01002854 // The mapping may do anything, but we expect it to take care of
2855 // redrawing. Do put the cursor back where it was.
2856 windgoto(was_screen_row, was_screen_col);
2857 out_flush();
2858
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002859 vgetc_busy = save_vgetc_busy;
2860 may_garbage_collect = save_may_garbage_collect;
2861 }
2862 else
2863#endif
2864 map_str = mp->m_str;
2865
2866 /*
2867 * Insert the 'to' part in the typebuf.tb_buf.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002868 * If 'from' field is the same as the start of the 'to' field, don't
2869 * remap the first character (but do allow abbreviations).
2870 * If m_noremap is set, don't remap the whole 'to' part.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002871 */
2872 if (map_str == NULL)
2873 i = FAIL;
2874 else
2875 {
2876 int noremap;
2877
2878 if (save_m_noremap != REMAP_YES)
2879 noremap = save_m_noremap;
2880 else if (
2881#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002882 STRNCMP(map_str, save_m_keys != NULL ? save_m_keys : mp->m_keys,
2883 (size_t)keylen)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002884#else
2885 STRNCMP(map_str, mp->m_keys, (size_t)keylen)
2886#endif
2887 != 0)
2888 noremap = REMAP_YES;
2889 else
2890 noremap = REMAP_SKIP;
2891 i = ins_typebuf(map_str, noremap,
2892 0, TRUE, cmd_silent || save_m_silent);
2893#ifdef FEAT_EVAL
2894 if (save_m_expr)
2895 vim_free(map_str);
2896#endif
2897 }
2898#ifdef FEAT_EVAL
2899 vim_free(save_m_keys);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002900#endif
2901 *keylenp = keylen;
2902 if (i == FAIL)
2903 return map_result_fail;
2904 return map_result_retry;
2905 }
2906
2907 *keylenp = keylen;
2908 return map_result_nomatch;
2909}
2910
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002911/*
2912 * unget one character (can only be done once!)
2913 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002915vungetc(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916{
2917 old_char = c;
2918 old_mod_mask = mod_mask;
Bram Moolenaarb8978712013-03-16 21:42:16 +01002919 old_mouse_row = mouse_row;
2920 old_mouse_col = mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921}
2922
2923/*
Bram Moolenaar32526b32019-01-19 17:43:09 +01002924 * Get a byte:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 * 1. from the stuffbuffer
2926 * This is used for abbreviated commands like "D" -> "d$".
2927 * Also used to redo a command for ".".
2928 * 2. from the typeahead buffer
2929 * Stores text obtained previously but not used yet.
2930 * Also stores the result of mappings.
2931 * Also used for the ":normal" command.
2932 * 3. from the user
2933 * This may do a blocking wait if "advance" is TRUE.
2934 *
2935 * if "advance" is TRUE (vgetc()):
Bram Moolenaard29459b2016-08-26 22:29:11 +02002936 * Really get the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 * KeyTyped is set to TRUE in the case the user typed the key.
2938 * KeyStuffed is TRUE if the character comes from the stuff buffer.
2939 * if "advance" is FALSE (vpeekc()):
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02002940 * Just look whether there is a character available.
2941 * Return NUL if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 *
2943 * When "no_mapping" is zero, checks for mappings in the current mode.
2944 * Only returns one byte (of a multi-byte character).
2945 * K_SPECIAL and CSI may be escaped, need to get two more bytes then.
2946 */
2947 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002948vgetorpeek(int advance)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949{
2950 int c, c1;
Bram Moolenaar30613902019-12-01 22:11:18 +01002951 int timedout = FALSE; // waited for more than 1 second
2952 // for mapping to complete
2953 int mapdepth = 0; // check for recursive mapping
2954 int mode_deleted = FALSE; // set when mode has been deleted
Bram Moolenaar4e427192006-03-10 21:34:27 +00002955#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 int new_wcol, new_wrow;
2957#endif
2958#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01002959 int shape_changed = FALSE; // adjusted cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960#endif
2961 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 int old_wcol, old_wrow;
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00002963 int wait_tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964
2965 /*
2966 * This function doesn't work very well when called recursively. This may
2967 * happen though, because of:
2968 * 1. The call to add_to_showcmd(). char_avail() is then used to check if
2969 * there is a character available, which calls this function. In that
2970 * case we must return NUL, to indicate no character is available.
2971 * 2. A GUI callback function writes to the screen, causing a
2972 * wait_return().
2973 * Using ":normal" can also do this, but it saves the typeahead buffer,
2974 * thus it should be OK. But don't get a key from the user then.
2975 */
Bram Moolenaare2c38102016-01-31 14:55:40 +01002976 if (vgetc_busy > 0 && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 return NUL;
2978
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002979 ++vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980
2981 if (advance)
2982 KeyStuffed = FALSE;
2983
2984 init_typebuf();
2985 start_stuff();
2986 if (advance && typebuf.tb_maplen == 0)
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02002987 reg_executing = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 do
2989 {
2990/*
2991 * get a character: 1. from the stuffbuffer
2992 */
2993 if (typeahead_char != 0)
2994 {
2995 c = typeahead_char;
2996 if (advance)
2997 typeahead_char = 0;
2998 }
2999 else
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01003000 c = read_readbuffers(advance);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 if (c != NUL && !got_int)
3002 {
3003 if (advance)
3004 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003005 // KeyTyped = FALSE; When the command that stuffed something
3006 // was typed, behave like the stuffed command was typed.
3007 // needed for CTRL-W CTRL-] to open a fold, for example.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 KeyStuffed = TRUE;
3009 }
3010 if (typebuf.tb_no_abbr_cnt == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003011 typebuf.tb_no_abbr_cnt = 1; // no abbreviations now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 }
3013 else
3014 {
3015 /*
3016 * Loop until we either find a matching mapped key, or we
3017 * are sure that it is not a mapped key.
3018 * If a mapped key sequence is found we go back to the start to
3019 * try re-mapping.
3020 */
3021 for (;;)
3022 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003023 long wait_time;
3024 int keylen = 0;
Bram Moolenaareda35f72019-08-03 14:59:44 +02003025#ifdef FEAT_CMDL_INFO
3026 int showcmd_idx;
3027#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 /*
3029 * ui_breakcheck() is slow, don't use it too often when
3030 * inside a mapping. But call it each time for typed
3031 * characters.
3032 */
3033 if (typebuf.tb_maplen)
3034 line_breakcheck();
3035 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003036 ui_breakcheck(); // check for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 if (got_int)
3038 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003039 // flush all input
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003040 c = inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 0L);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003041
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 /*
3043 * If inchar() returns TRUE (script file was active) or we
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003044 * are inside a mapping, get out of Insert mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 * Otherwise we behave like having gotten a CTRL-C.
3046 * As a result typing CTRL-C in insert mode will
3047 * really insert a CTRL-C.
3048 */
3049 if ((c || typebuf.tb_maplen)
3050 && (State & (INSERT + CMDLINE)))
3051 c = ESC;
3052 else
3053 c = Ctrl_C;
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02003054 flush_buffers(FLUSH_INPUT); // flush all typeahead
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055
Bram Moolenaard9dfd572006-10-03 13:36:13 +00003056 if (advance)
3057 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003058 // Also record this character, it might be needed to
3059 // get out of Insert mode.
Bram Moolenaard9dfd572006-10-03 13:36:13 +00003060 *typebuf.tb_buf = c;
3061 gotchars(typebuf.tb_buf, 1);
3062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 cmd_silent = FALSE;
3064
3065 break;
3066 }
3067 else if (typebuf.tb_len > 0)
3068 {
3069 /*
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003070 * Check for a mapping in "typebuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 */
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003072 map_result_T result = handle_mapping(
3073 &keylen, &timedout, &mapdepth);
3074
3075 if (result == map_result_retry)
3076 // try mapping again
3077 continue;
3078 if (result == map_result_fail)
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003079 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003080 // failed, use the outer loop
3081 c = -1;
3082 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003084 if (result == map_result_get)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086/*
3087 * get a character: 2. from the typeahead buffer
3088 */
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003089 c = typebuf.tb_buf[typebuf.tb_off];
Bram Moolenaar30613902019-12-01 22:11:18 +01003090 if (advance) // remove chars from tb_buf
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003091 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003092 cmd_silent = (typebuf.tb_silent > 0);
3093 if (typebuf.tb_maplen > 0)
3094 KeyTyped = FALSE;
3095 else
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003096 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003097 KeyTyped = TRUE;
Bram Moolenaar30613902019-12-01 22:11:18 +01003098 // write char to script file(s)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003099 gotchars(typebuf.tb_buf
3100 + typebuf.tb_off, 1);
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003101 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003102 KeyNoremap = typebuf.tb_noremap[
3103 typebuf.tb_off];
3104 del_typebuf(1, 0);
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003105 }
Bram Moolenaar52797ba2021-12-16 14:45:13 +00003106 break; // got character, break the for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 }
3108
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003109 // not enough characters, get more
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 }
3111
3112/*
3113 * get a character: 3. from the user - handle <Esc> in Insert mode
3114 */
3115 /*
Bram Moolenaarf085f422017-06-07 20:39:47 +02003116 * Special case: if we get an <ESC> in insert mode and there
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 * are no more characters at once, we pretend to go out of
3118 * insert mode. This prevents the one second delay after
3119 * typing an <ESC>. If we get something after all, we may
3120 * have to redisplay the mode. That the cursor is in the wrong
3121 * place does not matter.
3122 */
3123 c = 0;
3124#ifdef FEAT_CMDL_INFO
3125 new_wcol = curwin->w_wcol;
3126 new_wrow = curwin->w_wrow;
3127#endif
3128 if ( advance
3129 && typebuf.tb_len == 1
3130 && typebuf.tb_buf[typebuf.tb_off] == ESC
3131 && !no_mapping
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 && ex_normal_busy == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 && typebuf.tb_maplen == 0
3134 && (State & INSERT)
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003135 && (p_timeout
3136 || (keylen == KEYLEN_PART_KEY && p_ttimeout))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 && (c = inchar(typebuf.tb_buf + typebuf.tb_off
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003138 + typebuf.tb_len, 3, 25L)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 {
3140 colnr_T col = 0, vcol;
3141 char_u *ptr;
3142
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003143 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 {
3145 unshowmode(TRUE);
3146 mode_deleted = TRUE;
3147 }
3148#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003149 // may show a different cursor shape
Bram Moolenaarf085f422017-06-07 20:39:47 +02003150 if (gui.in_use && State != NORMAL && !cmd_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 {
3152 int save_State;
3153
3154 save_State = State;
3155 State = NORMAL;
3156 gui_update_cursor(TRUE, FALSE);
3157 State = save_State;
3158 shape_changed = TRUE;
3159 }
3160#endif
3161 validate_cursor();
3162 old_wcol = curwin->w_wcol;
3163 old_wrow = curwin->w_wrow;
3164
Bram Moolenaar30613902019-12-01 22:11:18 +01003165 // move cursor left, if possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 if (curwin->w_cursor.col != 0)
3167 {
3168 if (curwin->w_wcol > 0)
3169 {
3170 if (did_ai)
3171 {
3172 /*
3173 * We are expecting to truncate the trailing
3174 * white-space, so find the last non-white
3175 * character -- webb
3176 */
3177 col = vcol = curwin->w_wcol = 0;
3178 ptr = ml_get_curline();
3179 while (col < curwin->w_cursor.col)
3180 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01003181 if (!VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 curwin->w_wcol = vcol;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003183 vcol += lbr_chartabsize(ptr, ptr + col,
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01003184 vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003186 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 ++col;
3189 }
3190 curwin->w_wrow = curwin->w_cline_row
Bram Moolenaar02631462017-09-22 15:20:32 +02003191 + curwin->w_wcol / curwin->w_width;
3192 curwin->w_wcol %= curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 curwin->w_wcol += curwin_col_off();
Bram Moolenaar30613902019-12-01 22:11:18 +01003194 col = 0; // no correction needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 }
3196 else
3197 {
3198 --curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 col = curwin->w_cursor.col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 }
3201 }
3202 else if (curwin->w_p_wrap && curwin->w_wrow)
3203 {
3204 --curwin->w_wrow;
Bram Moolenaar02631462017-09-22 15:20:32 +02003205 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 col = curwin->w_cursor.col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 if (has_mbyte && col > 0 && curwin->w_wcol > 0)
3209 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003210 // Correct when the cursor is on the right halve
3211 // of a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 ptr = ml_get_curline();
3213 col -= (*mb_head_off)(ptr, ptr + col);
3214 if ((*mb_ptr2cells)(ptr + col) > 1)
3215 --curwin->w_wcol;
3216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 }
3218 setcursor();
3219 out_flush();
3220#ifdef FEAT_CMDL_INFO
3221 new_wcol = curwin->w_wcol;
3222 new_wrow = curwin->w_wrow;
3223#endif
3224 curwin->w_wcol = old_wcol;
3225 curwin->w_wrow = old_wrow;
3226 }
3227 if (c < 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003228 continue; // end of input script reached
Bram Moolenaar20c38922014-07-23 20:41:14 +02003229
Bram Moolenaar30613902019-12-01 22:11:18 +01003230 // Allow mapping for just typed characters. When we get here c
3231 // is the number of extra bytes and typebuf.tb_len is 1.
Bram Moolenaar20c38922014-07-23 20:41:14 +02003232 for (n = 1; n <= c; ++n)
3233 typebuf.tb_noremap[typebuf.tb_off + n] = RM_YES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 typebuf.tb_len += c;
3235
Bram Moolenaar30613902019-12-01 22:11:18 +01003236 // buffer full, don't map
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 if (typebuf.tb_len >= typebuf.tb_maplen + MAXMAPLEN)
3238 {
3239 timedout = TRUE;
3240 continue;
3241 }
3242
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 if (ex_normal_busy > 0)
3244 {
Bram Moolenaare2c38102016-01-31 14:55:40 +01003245#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 static int tc = 0;
Bram Moolenaare2c38102016-01-31 14:55:40 +01003247#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248
Bram Moolenaar30613902019-12-01 22:11:18 +01003249 // No typeahead left and inside ":normal". Must return
3250 // something to avoid getting stuck. When an incomplete
3251 // mapping is present, behave like it timed out.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 if (typebuf.tb_len > 0)
3253 {
3254 timedout = TRUE;
3255 continue;
3256 }
Bram Moolenaar189832b2020-09-23 12:29:11 +02003257
Bram Moolenaar30613902019-12-01 22:11:18 +01003258 // When 'insertmode' is set, ESC just beeps in Insert
3259 // mode. Use CTRL-L to make edit() return.
3260 // For the command line only CTRL-C always breaks it.
3261 // For the cmdline window: Alternate between ESC and
3262 // CTRL-C: ESC for most situations and CTRL-C to close the
3263 // cmdline window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 if (p_im && (State & INSERT))
3265 c = Ctrl_L;
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003266#ifdef FEAT_TERMINAL
3267 else if (terminal_is_active())
3268 c = K_CANCEL;
3269#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 else if ((State & CMDLINE)
Bram Moolenaare2c38102016-01-31 14:55:40 +01003271#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 || (cmdwin_type > 0 && tc == ESC)
Bram Moolenaare2c38102016-01-31 14:55:40 +01003273#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 )
3275 c = Ctrl_C;
3276 else
3277 c = ESC;
Bram Moolenaare2c38102016-01-31 14:55:40 +01003278#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 tc = c;
Bram Moolenaare2c38102016-01-31 14:55:40 +01003280#endif
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01003281 // return from main_loop()
3282 if (pending_exmode_active)
3283 exmode_active = EXMODE_NORMAL;
3284
Bram Moolenaarb37a65e2022-01-01 12:42:56 +00003285 // no chars to block abbreviation for
3286 typebuf.tb_no_abbr_cnt = 0;
3287
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 break;
3289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290
3291/*
3292 * get a character: 3. from the user - update display
3293 */
Bram Moolenaar30613902019-12-01 22:11:18 +01003294 // In insert mode a screen update is skipped when characters
3295 // are still available. But when those available characters
3296 // are part of a mapping, and we are going to do a blocking
3297 // wait here. Need to update the screen to display the
3298 // changed text so far. Also for when 'lazyredraw' is set and
3299 // redrawing was postponed because there was something in the
3300 // input buffer (e.g., termresponse).
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01003301 if (((State & INSERT) != 0 || p_lz) && (State & CMDLINE) == 0
3302 && advance && must_redraw != 0 && !need_wait_return)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 {
3304 update_screen(0);
Bram Moolenaar30613902019-12-01 22:11:18 +01003305 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 }
3307
3308 /*
3309 * If we have a partial match (and are going to wait for more
3310 * input from the user), show the partially matched characters
3311 * to the user with showcmd.
3312 */
3313#ifdef FEAT_CMDL_INFO
Bram Moolenaareda35f72019-08-03 14:59:44 +02003314 showcmd_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315#endif
3316 c1 = 0;
3317 if (typebuf.tb_len > 0 && advance && !exmode_active)
3318 {
3319 if (((State & (NORMAL | INSERT)) || State == LANGMAP)
3320 && State != HITRETURN)
3321 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003322 // this looks nice when typing a dead character map
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 if (State & INSERT
3324 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
3325 + typebuf.tb_len - 1) == 1)
3326 {
3327 edit_putchar(typebuf.tb_buf[typebuf.tb_off
3328 + typebuf.tb_len - 1], FALSE);
Bram Moolenaar30613902019-12-01 22:11:18 +01003329 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 c1 = 1;
3331 }
3332#ifdef FEAT_CMDL_INFO
Bram Moolenaar30613902019-12-01 22:11:18 +01003333 // need to use the col and row from above here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 old_wcol = curwin->w_wcol;
3335 old_wrow = curwin->w_wrow;
3336 curwin->w_wcol = new_wcol;
3337 curwin->w_wrow = new_wrow;
3338 push_showcmd();
3339 if (typebuf.tb_len > SHOWCMD_COLS)
Bram Moolenaareda35f72019-08-03 14:59:44 +02003340 showcmd_idx = typebuf.tb_len - SHOWCMD_COLS;
3341 while (showcmd_idx < typebuf.tb_len)
3342 (void)add_to_showcmd(
3343 typebuf.tb_buf[typebuf.tb_off + showcmd_idx++]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 curwin->w_wcol = old_wcol;
3345 curwin->w_wrow = old_wrow;
3346#endif
3347 }
3348
Bram Moolenaar30613902019-12-01 22:11:18 +01003349 // this looks nice when typing a dead character map
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 if ((State & CMDLINE)
3351#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3352 && cmdline_star == 0
3353#endif
3354 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
3355 + typebuf.tb_len - 1) == 1)
3356 {
3357 putcmdline(typebuf.tb_buf[typebuf.tb_off
3358 + typebuf.tb_len - 1], FALSE);
3359 c1 = 1;
3360 }
3361 }
3362
3363/*
3364 * get a character: 3. from the user - get it
3365 */
Bram Moolenaar83f4cbd2018-06-12 21:35:40 +02003366 if (typebuf.tb_len == 0)
3367 // timedout may have been set while waiting for a mapping
3368 // that has a <Nop> RHS.
3369 timedout = FALSE;
3370
Bram Moolenaar652de232019-04-04 20:13:09 +02003371 if (advance)
3372 {
3373 if (typebuf.tb_len == 0
3374 || !(p_timeout
3375 || (p_ttimeout && keylen == KEYLEN_PART_KEY)))
3376 // blocking wait
3377 wait_time = -1L;
3378 else if (keylen == KEYLEN_PART_KEY && p_ttm >= 0)
3379 wait_time = p_ttm;
3380 else
3381 wait_time = p_tm;
3382 }
3383 else
3384 wait_time = 0;
3385
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003386 wait_tb_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 c = inchar(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len,
3388 typebuf.tb_buflen - typebuf.tb_off - typebuf.tb_len - 1,
Bram Moolenaar652de232019-04-04 20:13:09 +02003389 wait_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390
3391#ifdef FEAT_CMDL_INFO
Bram Moolenaareda35f72019-08-03 14:59:44 +02003392 if (showcmd_idx != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 pop_showcmd();
3394#endif
3395 if (c1 == 1)
3396 {
3397 if (State & INSERT)
3398 edit_unputchar();
3399 if (State & CMDLINE)
3400 unputcmdline();
Bram Moolenaarbc256d92012-06-06 12:06:15 +02003401 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003402 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 }
3404
3405 if (c < 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003406 continue; // end of input script reached
3407 if (c == NUL) // no character available
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 {
3409 if (!advance)
3410 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01003411 if (wait_tb_len > 0) // timed out
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 {
3413 timedout = TRUE;
3414 continue;
3415 }
3416 }
3417 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003418 { // allow mapping for just typed characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 while (typebuf.tb_buf[typebuf.tb_off
3420 + typebuf.tb_len] != NUL)
3421 typebuf.tb_noremap[typebuf.tb_off
3422 + typebuf.tb_len++] = RM_YES;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003423#ifdef HAVE_INPUT_METHOD
Bram Moolenaar30613902019-12-01 22:11:18 +01003424 // Get IM status right after getting keys, not after the
3425 // timeout for a mapping (focus may be lost by then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 vgetc_im_active = im_get_status();
3427#endif
3428 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003429 } // for (;;)
3430 } // if (!character from stuffbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431
Bram Moolenaar30613902019-12-01 22:11:18 +01003432 // if advance is FALSE don't loop on NULs
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003433 } while ((c < 0 && c != K_CANCEL) || (advance && c == NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434
3435 /*
3436 * The "INSERT" message is taken care of here:
3437 * if we return an ESC to exit insert mode, the message is deleted
3438 * if we don't return an ESC but deleted the message before, redisplay it
3439 */
Bram Moolenaar09df3122006-01-23 22:23:09 +00003440 if (advance && p_smd && msg_silent == 0 && (State & INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003442 if (c == ESC && !mode_deleted && !no_mapping && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 {
3444 if (typebuf.tb_len && !KeyTyped)
Bram Moolenaar30613902019-12-01 22:11:18 +01003445 redraw_cmdline = TRUE; // delete mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 else
3447 unshowmode(FALSE);
3448 }
3449 else if (c != ESC && mode_deleted)
3450 {
3451 if (typebuf.tb_len && !KeyTyped)
Bram Moolenaar30613902019-12-01 22:11:18 +01003452 redraw_cmdline = TRUE; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 else
3454 showmode();
3455 }
3456 }
3457#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003458 // may unshow different cursor shape
Bram Moolenaarf085f422017-06-07 20:39:47 +02003459 if (gui.in_use && shape_changed)
3460 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461#endif
Bram Moolenaar6edbbd82019-03-10 09:41:51 +01003462 if (timedout && c == ESC)
3463 {
3464 char_u nop_buf[3];
3465
3466 // When recording there will be no timeout. Add a <Nop> after the ESC
3467 // to avoid that it forms a key code with following characters.
3468 nop_buf[0] = K_SPECIAL;
3469 nop_buf[1] = KS_EXTRA;
3470 nop_buf[2] = KE_NOP;
3471 gotchars(nop_buf, 3);
3472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003474 --vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475
3476 return c;
3477}
3478
3479/*
3480 * inchar() - get one character from
3481 * 1. a scriptfile
3482 * 2. the keyboard
3483 *
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003484 * As many characters as we can get (up to 'maxlen') are put in "buf" and
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 * NUL terminated (buffer length must be 'maxlen' + 1).
3486 * Minimum for "maxlen" is 3!!!!
3487 *
3488 * "tb_change_cnt" is the value of typebuf.tb_change_cnt if "buf" points into
3489 * it. When typebuf.tb_change_cnt changes (e.g., when a message is received
3490 * from a remote client) "buf" can no longer be used. "tb_change_cnt" is 0
3491 * otherwise.
3492 *
3493 * If we got an interrupt all input is read until none is available.
3494 *
3495 * If wait_time == 0 there is no waiting for the char.
3496 * If wait_time == n we wait for n msec for a character to arrive.
3497 * If wait_time == -1 we wait forever for a character to arrive.
3498 *
3499 * Return the number of obtained characters.
3500 * Return -1 when end of input script reached.
3501 */
Bram Moolenaarcda77642016-06-04 13:32:35 +02003502 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003503inchar(
3504 char_u *buf,
3505 int maxlen,
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003506 long wait_time) // milliseconds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507{
Bram Moolenaar30613902019-12-01 22:11:18 +01003508 int len = 0; // init for GCC
3509 int retesc = FALSE; // return ESC with gotint
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 int script_char;
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003511 int tb_change_cnt = typebuf.tb_change_cnt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512
Bram Moolenaar30613902019-12-01 22:11:18 +01003513 if (wait_time == -1L || wait_time > 100L) // flush output before waiting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 {
3515 cursor_on();
Bram Moolenaara338adc2018-01-31 20:51:47 +01003516 out_flush_cursor(FALSE, FALSE);
3517#if defined(FEAT_GUI) && defined(FEAT_MOUSESHAPE)
3518 if (gui.in_use && postponed_mouseshape)
3519 update_mouseshape(-1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520#endif
3521 }
3522
3523 /*
3524 * Don't reset these when at the hit-return prompt, otherwise a endless
3525 * recursive loop may result (write error in swapfile, hit-return, timeout
3526 * on char wait, flush swapfile, write error....).
3527 */
3528 if (State != HITRETURN)
3529 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003530 did_outofmem_msg = FALSE; // display out of memory message (again)
3531 did_swapwrite_msg = FALSE; // display swap file write error again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003533 undo_off = FALSE; // restart undo now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534
3535 /*
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00003536 * Get a character from a script file if there is one.
3537 * If interrupted: Stop reading script files, close them all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 */
3539 script_char = -1;
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00003540 while (scriptin[curscript] != NULL && script_char < 0
3541#ifdef FEAT_EVAL
3542 && !ignore_script
3543#endif
3544 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02003546#ifdef MESSAGE_QUEUE
3547 parse_queued_messages();
Bram Moolenaarf2330482008-06-24 20:19:36 +00003548#endif
3549
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 if (got_int || (script_char = getc(scriptin[curscript])) < 0)
3551 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003552 // Reached EOF.
3553 // Careful: closescript() frees typebuf.tb_buf[] and buf[] may
3554 // point inside typebuf.tb_buf[]. Don't use buf[] after this!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 closescript();
3556 /*
3557 * When reading script file is interrupted, return an ESC to get
3558 * back to normal mode.
3559 * Otherwise return -1, because typebuf.tb_buf[] has changed.
3560 */
3561 if (got_int)
3562 retesc = TRUE;
3563 else
3564 return -1;
3565 }
3566 else
3567 {
3568 buf[0] = script_char;
3569 len = 1;
3570 }
3571 }
3572
Bram Moolenaar30613902019-12-01 22:11:18 +01003573 if (script_char < 0) // did not get a character from script
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 {
3575 /*
3576 * If we got an interrupt, skip all previously typed characters and
3577 * return TRUE if quit reading script file.
3578 * Stop reading typeahead when a single CTRL-C was read,
3579 * fill_input_buf() returns this when not able to read from stdin.
3580 * Don't use buf[] here, closescript() may have freed typebuf.tb_buf[]
3581 * and buf may be pointing inside typebuf.tb_buf[].
3582 */
3583 if (got_int)
3584 {
3585#define DUM_LEN MAXMAPLEN * 3 + 3
3586 char_u dum[DUM_LEN + 1];
3587
3588 for (;;)
3589 {
3590 len = ui_inchar(dum, DUM_LEN, 0L, 0);
3591 if (len == 0 || (len == 1 && dum[0] == 3))
3592 break;
3593 }
3594 return retesc;
3595 }
3596
3597 /*
3598 * Always flush the output characters when getting input characters
Bram Moolenaarcb574f42019-01-25 22:29:57 +01003599 * from the user and not just peeking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 */
Bram Moolenaarcb574f42019-01-25 22:29:57 +01003601 if (wait_time == -1L || wait_time > 10L)
3602 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603
3604 /*
3605 * Fill up to a third of the buffer, because each character may be
3606 * tripled below.
3607 */
3608 len = ui_inchar(buf, maxlen / 3, wait_time, tb_change_cnt);
3609 }
3610
Bram Moolenaar30613902019-12-01 22:11:18 +01003611 // If the typebuf was changed further down, it is like nothing was added by
3612 // this call.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 if (typebuf_changed(tb_change_cnt))
3614 return 0;
3615
Bram Moolenaar30613902019-12-01 22:11:18 +01003616 // Note the change in the typeahead buffer, this matters for when
3617 // vgetorpeek() is called recursively, e.g. using getchar(1) in a timer
3618 // function.
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003619 if (len > 0 && ++typebuf.tb_change_cnt == 0)
3620 typebuf.tb_change_cnt = 1;
3621
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003622 return fix_input_buffer(buf, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623}
3624
3625/*
3626 * Fix typed characters for use by vgetc() and check_termcode().
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003627 * "buf[]" must have room to triple the number of bytes!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 * Returns the new length.
3629 */
3630 int
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003631fix_input_buffer(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632{
3633 int i;
3634 char_u *p = buf;
3635
3636 /*
3637 * Two characters are special: NUL and K_SPECIAL.
3638 * When compiled With the GUI CSI is also special.
3639 * Replace NUL by K_SPECIAL KS_ZERO KE_FILLER
3640 * Replace K_SPECIAL by K_SPECIAL KS_SPECIAL KE_FILLER
3641 * Replace CSI by K_SPECIAL KS_EXTRA KE_CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 */
3643 for (i = len; --i >= 0; ++p)
3644 {
3645#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003646 // When the GUI is used any character can come after a CSI, don't
3647 // escape it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 if (gui.in_use && p[0] == CSI && i >= 2)
3649 {
3650 p += 2;
3651 i -= 2;
3652 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003653 // When the GUI is not used CSI needs to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 else if (!gui.in_use && p[0] == CSI)
3655 {
3656 mch_memmove(p + 3, p + 1, (size_t)i);
3657 *p++ = K_SPECIAL;
3658 *p++ = KS_EXTRA;
3659 *p = (int)KE_CSI;
3660 len += 2;
3661 }
3662 else
3663#endif
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003664 if (p[0] == NUL || (p[0] == K_SPECIAL
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003665 // timeout may generate K_CURSORHOLD
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003666 && (i < 2 || p[1] != KS_EXTRA || p[2] != (int)KE_CURSORHOLD)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003667#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003668 // Win32 console passes modifiers
3669 && (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003670# ifdef VIMDLL
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003671 gui.in_use ||
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003672# endif
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003673 (i < 2 || p[1] != KS_MODIFIER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674#endif
3675 ))
3676 {
3677 mch_memmove(p + 3, p + 1, (size_t)i);
3678 p[2] = K_THIRD(p[0]);
3679 p[1] = K_SECOND(p[0]);
3680 p[0] = K_SPECIAL;
3681 p += 2;
3682 len += 2;
3683 }
3684 }
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003685 *p = NUL; // add trailing NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 return len;
3687}
3688
3689#if defined(USE_INPUT_BUF) || defined(PROTO)
3690/*
3691 * Return TRUE when bytes are in the input buffer or in the typeahead buffer.
3692 * Normally the input buffer would be sufficient, but the server_to_input_buf()
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003693 * or feedkeys() may insert characters in the typeahead buffer while we are
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003694 * waiting for input to arrive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 */
3696 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003697input_available(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698{
3699 return (!vim_is_input_buf_empty()
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003700# if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
3701 || typebuf_was_filled
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702# endif
3703 );
3704}
3705#endif
Bram Moolenaar957cf672020-11-12 14:21:06 +01003706
3707/*
3708 * Function passed to do_cmdline() to get the command after a <Cmd> key from
3709 * typeahead.
3710 */
3711 char_u *
3712getcmdkeycmd(
3713 int promptc UNUSED,
3714 void *cookie UNUSED,
3715 int indent UNUSED,
3716 getline_opt_T do_concat UNUSED)
3717{
3718 garray_T line_ga;
3719 int c1 = -1;
3720 int c2;
3721 int cmod = 0;
3722 int aborted = FALSE;
3723
3724 ga_init2(&line_ga, 1, 32);
3725
3726 // no mapping for these characters
3727 no_mapping++;
3728
3729 got_int = FALSE;
3730 while (c1 != NUL && !aborted)
3731 {
Bram Moolenaarca359cb2020-11-15 20:49:41 +01003732 if (ga_grow(&line_ga, 32) != OK)
3733 {
3734 aborted = TRUE;
3735 break;
3736 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003737
3738 if (vgetorpeek(FALSE) == NUL)
3739 {
3740 // incomplete <Cmd> is an error, because there is not much the user
3741 // could do in this state.
3742 emsg(_(e_cmd_mapping_must_end_with_cr));
3743 aborted = TRUE;
3744 break;
3745 }
3746
3747 // Get one character at a time.
3748 c1 = vgetorpeek(TRUE);
3749
3750 // Get two extra bytes for special keys
3751 if (c1 == K_SPECIAL)
3752 {
3753 c1 = vgetorpeek(TRUE);
3754 c2 = vgetorpeek(TRUE);
3755 if (c1 == KS_MODIFIER)
3756 {
3757 cmod = c2;
3758 continue;
3759 }
3760 c1 = TO_SPECIAL(c1, c2);
3761 }
Bram Moolenaar4a441202020-11-28 14:43:26 +01003762 if (c1 == Ctrl_V)
3763 {
3764 // CTRL-V is followed by octal, hex or other characters, reverses
3765 // what AppendToRedobuffLit() does.
3766 no_reduce_keys = TRUE; // don't merge modifyOtherKeys
Bram Moolenaar0684e362020-12-03 19:54:42 +01003767 c1 = get_literal(TRUE);
Bram Moolenaar4a441202020-11-28 14:43:26 +01003768 no_reduce_keys = FALSE;
3769 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003770
3771 if (got_int)
3772 aborted = TRUE;
3773 else if (c1 == '\r' || c1 == '\n')
3774 c1 = NUL; // end the line
3775 else if (c1 == ESC)
3776 aborted = TRUE;
3777 else if (c1 == K_COMMAND)
3778 {
3779 // give a nicer error message for this special case
3780 emsg(_(e_cmd_mapping_must_end_with_cr_before_second_cmd));
3781 aborted = TRUE;
3782 }
3783 else if (IS_SPECIAL(c1))
3784 {
3785 if (c1 == K_SNR)
Bram Moolenaarc77534c2020-11-18 11:34:37 +01003786 ga_concat(&line_ga, (char_u *)"<SNR>");
Bram Moolenaar957cf672020-11-12 14:21:06 +01003787 else
3788 {
3789 semsg(e_cmd_maping_must_not_include_str_key,
3790 get_special_key_name(c1, cmod));
3791 aborted = TRUE;
3792 }
3793 }
3794 else
3795 ga_append(&line_ga, (char)c1);
3796
3797 cmod = 0;
3798 }
3799
3800 no_mapping--;
3801
3802 if (aborted)
3803 ga_clear(&line_ga);
3804
3805 return (char_u *)line_ga.ga_data;
3806}