blob: fa8c574d9f1ec9f6cfb81571ea4922adb2163d2b [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{
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001124 char_u buf[MB_MAXBYTES + 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 {
1145 len += (*mb_char2bytes)(c, buf + len);
1146 buf[len] = NUL;
1147 }
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00001148 (void)ins_typebuf(buf, KeyNoremap, 0, !KeyTyped, cmd_silent);
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001149 return len;
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001150}
1151
1152/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 * Return TRUE if the typeahead buffer was changed (while waiting for a
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001154 * character to arrive). Happens when a message was received from a client or
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001155 * from feedkeys().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 * But check in a more generic way to avoid trouble: When "typebuf.tb_buf"
1157 * changed it was reallocated and the old pointer can no longer be used.
1158 * Or "typebuf.tb_off" may have been changed and we would overwrite characters
1159 * that was just added.
1160 */
1161 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001162typebuf_changed(
Bram Moolenaar30613902019-12-01 22:11:18 +01001163 int tb_change_cnt) // old value of typebuf.tb_change_cnt
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164{
1165 return (tb_change_cnt != 0 && (typebuf.tb_change_cnt != tb_change_cnt
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001166#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
1167 || typebuf_was_filled
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168#endif
1169 ));
1170}
1171
1172/*
1173 * Return TRUE if there are no characters in the typeahead buffer that have
1174 * not been typed (result from a mapping or come from ":normal").
1175 */
1176 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001177typebuf_typed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178{
1179 return typebuf.tb_maplen == 0;
1180}
1181
1182/*
1183 * Return the number of characters that are mapped (or not typed).
1184 */
1185 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001186typebuf_maplen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187{
1188 return typebuf.tb_maplen;
1189}
1190
1191/*
1192 * remove "len" characters from typebuf.tb_buf[typebuf.tb_off + offset]
1193 */
1194 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001195del_typebuf(int len, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196{
1197 int i;
1198
1199 if (len == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01001200 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201
1202 typebuf.tb_len -= len;
1203
1204 /*
1205 * Easy case: Just increase typebuf.tb_off.
1206 */
1207 if (offset == 0 && typebuf.tb_buflen - (typebuf.tb_off + len)
1208 >= 3 * MAXMAPLEN + 3)
1209 typebuf.tb_off += len;
1210 /*
1211 * Have to move the characters in typebuf.tb_buf[] and typebuf.tb_noremap[]
1212 */
1213 else
1214 {
1215 i = typebuf.tb_off + offset;
1216 /*
1217 * Leave some extra room at the end to avoid reallocation.
1218 */
1219 if (typebuf.tb_off > MAXMAPLEN)
1220 {
1221 mch_memmove(typebuf.tb_buf + MAXMAPLEN,
1222 typebuf.tb_buf + typebuf.tb_off, (size_t)offset);
1223 mch_memmove(typebuf.tb_noremap + MAXMAPLEN,
1224 typebuf.tb_noremap + typebuf.tb_off, (size_t)offset);
1225 typebuf.tb_off = MAXMAPLEN;
1226 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001227 // adjust typebuf.tb_buf (include the NUL at the end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset,
1229 typebuf.tb_buf + i + len,
1230 (size_t)(typebuf.tb_len - offset + 1));
Bram Moolenaar30613902019-12-01 22:11:18 +01001231 // adjust typebuf.tb_noremap[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 mch_memmove(typebuf.tb_noremap + typebuf.tb_off + offset,
1233 typebuf.tb_noremap + i + len,
1234 (size_t)(typebuf.tb_len - offset));
1235 }
1236
Bram Moolenaar30613902019-12-01 22:11:18 +01001237 if (typebuf.tb_maplen > offset) // adjust tb_maplen
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 {
1239 if (typebuf.tb_maplen < offset + len)
1240 typebuf.tb_maplen = offset;
1241 else
1242 typebuf.tb_maplen -= len;
1243 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001244 if (typebuf.tb_silent > offset) // adjust tb_silent
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 {
1246 if (typebuf.tb_silent < offset + len)
1247 typebuf.tb_silent = offset;
1248 else
1249 typebuf.tb_silent -= len;
1250 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001251 if (typebuf.tb_no_abbr_cnt > offset) // adjust tb_no_abbr_cnt
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 {
1253 if (typebuf.tb_no_abbr_cnt < offset + len)
1254 typebuf.tb_no_abbr_cnt = offset;
1255 else
1256 typebuf.tb_no_abbr_cnt -= len;
1257 }
1258
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001259#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar30613902019-12-01 22:11:18 +01001260 // Reset the flag that text received from a client or from feedkeys()
1261 // was inserted in the typeahead buffer.
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001262 typebuf_was_filled = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263#endif
1264 if (++typebuf.tb_change_cnt == 0)
1265 typebuf.tb_change_cnt = 1;
1266}
1267
1268/*
1269 * Write typed characters to script file.
1270 * If recording is on put the character in the recordbuffer.
1271 */
1272 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001273gotchars(char_u *chars, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274{
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001275 char_u *s = chars;
1276 int i;
1277 static char_u buf[4];
1278 static int buflen = 0;
1279 int todo = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280
Bram Moolenaar4e86cba2007-05-06 11:56:32 +00001281 while (todo--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 {
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001283 buf[buflen++] = *s++;
1284
1285 // When receiving a special key sequence, store it until we have all
1286 // the bytes and we can decide what to do with it.
1287 if (buflen == 1 && buf[0] == K_SPECIAL)
1288 continue;
1289 if (buflen == 2)
1290 continue;
1291 if (buflen == 3 && buf[1] == KS_EXTRA
1292 && (buf[2] == KE_FOCUSGAINED || buf[2] == KE_FOCUSLOST))
1293 {
1294 // Drop K_FOCUSGAINED and K_FOCUSLOST, they are not useful in a
1295 // recording.
1296 buflen = 0;
1297 continue;
1298 }
1299
Bram Moolenaar30613902019-12-01 22:11:18 +01001300 // Handle one byte at a time; no translation to be done.
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001301 for (i = 0; i < buflen; ++i)
1302 updatescript(buf[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001304 if (reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 {
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001306 buf[buflen] = NUL;
1307 add_buff(&recordbuff, buf, (long)buflen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001308 // remember how many chars were last recorded
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001309 last_recorded_len += buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 }
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001311 buflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 }
1313 may_sync_undo();
1314
1315#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01001316 // output "debug mode" message next time in debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 debug_did_msg = FALSE;
1318#endif
1319
Bram Moolenaar30613902019-12-01 22:11:18 +01001320 // Since characters have been typed, consider the following to be in
1321 // another mapping. Search string will be kept in history.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 ++maptick;
1323}
1324
1325/*
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001326 * Undo the last gotchars() for "len" bytes. To be used when putting a typed
1327 * character back into the typeahead buffer, thus gotchars() will be called
1328 * again.
1329 * Only affects recorded characters.
1330 */
1331 void
1332ungetchars(int len)
1333{
1334 if (reg_recording != 0)
1335 {
1336 delete_buff_tail(&recordbuff, len);
1337 last_recorded_len -= len;
1338 }
1339}
1340
1341/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 * Sync undo. Called when typed characters are obtained from the typeahead
1343 * buffer, or when a menu is used.
1344 * Do not sync:
1345 * - In Insert mode, unless cursor key has been used.
1346 * - While reading a script file.
1347 * - When no_u_sync is non-zero.
1348 */
1349 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001350may_sync_undo(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351{
1352 if ((!(State & (INSERT + CMDLINE)) || arrow_used)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001353 && scriptin[curscript] == NULL)
1354 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355}
1356
1357/*
1358 * Make "typebuf" empty and allocate new buffers.
1359 * Returns FAIL when out of memory.
1360 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001361 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001362alloc_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363{
1364 typebuf.tb_buf = alloc(TYPELEN_INIT);
1365 typebuf.tb_noremap = alloc(TYPELEN_INIT);
1366 if (typebuf.tb_buf == NULL || typebuf.tb_noremap == NULL)
1367 {
1368 free_typebuf();
1369 return FAIL;
1370 }
1371 typebuf.tb_buflen = TYPELEN_INIT;
Bram Moolenaar30613902019-12-01 22:11:18 +01001372 typebuf.tb_off = MAXMAPLEN + 4; // can insert without realloc
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 typebuf.tb_len = 0;
1374 typebuf.tb_maplen = 0;
1375 typebuf.tb_silent = 0;
1376 typebuf.tb_no_abbr_cnt = 0;
1377 if (++typebuf.tb_change_cnt == 0)
1378 typebuf.tb_change_cnt = 1;
Bram Moolenaare49b4bb2020-03-11 13:01:40 +01001379#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
1380 typebuf_was_filled = FALSE;
1381#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 return OK;
1383}
1384
1385/*
1386 * Free the buffers of "typebuf".
1387 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001388 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001389free_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390{
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001391 if (typebuf.tb_buf == typebuf_init)
Bram Moolenaar95f09602016-11-10 20:01:45 +01001392 internal_error("Free typebuf 1");
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001393 else
Bram Moolenaar0f1c6702019-09-28 15:24:00 +02001394 VIM_CLEAR(typebuf.tb_buf);
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00001395 if (typebuf.tb_noremap == noremapbuf_init)
Bram Moolenaar95f09602016-11-10 20:01:45 +01001396 internal_error("Free typebuf 2");
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001397 else
Bram Moolenaar0f1c6702019-09-28 15:24:00 +02001398 VIM_CLEAR(typebuf.tb_noremap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399}
1400
1401/*
1402 * When doing ":so! file", the current typeahead needs to be saved, and
1403 * restored when "file" has been read completely.
1404 */
1405static typebuf_T saved_typebuf[NSCRIPT];
1406
1407 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001408save_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409{
1410 init_typebuf();
1411 saved_typebuf[curscript] = typebuf;
Bram Moolenaar30613902019-12-01 22:11:18 +01001412 // If out of memory: restore typebuf and close file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 if (alloc_typebuf() == FAIL)
1414 {
1415 closescript();
1416 return FAIL;
1417 }
1418 return OK;
1419}
1420
Bram Moolenaar30613902019-12-01 22:11:18 +01001421static int old_char = -1; // character put back by vungetc()
1422static int old_mod_mask; // mod_mask for ungotten character
1423static int old_mouse_row; // mouse_row related to old_char
1424static int old_mouse_col; // mouse_col related to old_char
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001425
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426/*
1427 * Save all three kinds of typeahead, so that the user must type at a prompt.
1428 */
1429 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001430save_typeahead(tasave_T *tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431{
1432 tp->save_typebuf = typebuf;
1433 tp->typebuf_valid = (alloc_typebuf() == OK);
1434 if (!tp->typebuf_valid)
1435 typebuf = tp->save_typebuf;
1436
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001437 tp->old_char = old_char;
1438 tp->old_mod_mask = old_mod_mask;
1439 old_char = -1;
1440
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001441 tp->save_readbuf1 = readbuf1;
Bram Moolenaar285e3352018-04-18 23:01:13 +02001442 readbuf1.bh_first.b_next = NULL;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001443 tp->save_readbuf2 = readbuf2;
Bram Moolenaar285e3352018-04-18 23:01:13 +02001444 readbuf2.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445# ifdef USE_INPUT_BUF
1446 tp->save_inputbuf = get_input_buf();
1447# endif
1448}
1449
1450/*
1451 * Restore the typeahead to what it was before calling save_typeahead().
1452 * The allocated memory is freed, can only be called once!
Bram Moolenaarc41badb2021-06-07 22:04:52 +02001453 * When "overwrite" is FALSE input typed later is kept.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 */
1455 void
Bram Moolenaarc41badb2021-06-07 22:04:52 +02001456restore_typeahead(tasave_T *tp, int overwrite UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457{
1458 if (tp->typebuf_valid)
1459 {
1460 free_typebuf();
1461 typebuf = tp->save_typebuf;
1462 }
1463
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001464 old_char = tp->old_char;
1465 old_mod_mask = tp->old_mod_mask;
1466
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001467 free_buff(&readbuf1);
1468 readbuf1 = tp->save_readbuf1;
1469 free_buff(&readbuf2);
1470 readbuf2 = tp->save_readbuf2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471# ifdef USE_INPUT_BUF
Bram Moolenaarc41badb2021-06-07 22:04:52 +02001472 set_input_buf(tp->save_inputbuf, overwrite);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473# endif
1474}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475
1476/*
1477 * Open a new script file for the ":source!" command.
1478 */
1479 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001480openscript(
1481 char_u *name,
Bram Moolenaar30613902019-12-01 22:11:18 +01001482 int directly) // when TRUE execute directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483{
1484 if (curscript + 1 == NSCRIPT)
1485 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001486 emsg(_(e_scripts_nested_too_deep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 return;
1488 }
Bram Moolenaar53575522019-05-22 22:38:25 +02001489
1490 // Disallow sourcing a file in the sandbox, the commands would be executed
1491 // later, possibly outside of the sandbox.
1492 if (check_secure())
1493 return;
1494
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001495#ifdef FEAT_EVAL
1496 if (ignore_script)
Bram Moolenaar30613902019-12-01 22:11:18 +01001497 // Not reading from script, also don't open one. Warning message?
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001498 return;
1499#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500
Bram Moolenaar30613902019-12-01 22:11:18 +01001501 if (scriptin[curscript] != NULL) // already reading script
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 ++curscript;
Bram Moolenaar30613902019-12-01 22:11:18 +01001503 // use NameBuff for expanded name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 expand_env(name, NameBuff, MAXPATHL);
1505 if ((scriptin[curscript] = mch_fopen((char *)NameBuff, READBIN)) == NULL)
1506 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001507 semsg(_(e_cant_open_file_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 if (curscript)
1509 --curscript;
1510 return;
1511 }
1512 if (save_typebuf() == FAIL)
1513 return;
1514
1515 /*
1516 * Execute the commands from the file right now when using ":source!"
1517 * after ":global" or ":argdo" or in a loop. Also when another command
1518 * follows. This means the display won't be updated. Don't do this
1519 * always, "make test" would fail.
1520 */
1521 if (directly)
1522 {
1523 oparg_T oa;
1524 int oldcurscript;
1525 int save_State = State;
1526 int save_restart_edit = restart_edit;
1527 int save_insertmode = p_im;
1528 int save_finish_op = finish_op;
1529 int save_msg_scroll = msg_scroll;
1530
1531 State = NORMAL;
Bram Moolenaar30613902019-12-01 22:11:18 +01001532 msg_scroll = FALSE; // no msg scrolling in Normal mode
1533 restart_edit = 0; // don't go to Insert mode
1534 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 clear_oparg(&oa);
1536 finish_op = FALSE;
1537
1538 oldcurscript = curscript;
1539 do
1540 {
Bram Moolenaar386b43e2019-05-19 21:57:11 +02001541 update_topline_cursor(); // update cursor position and topline
1542 normal_cmd(&oa, FALSE); // execute one command
Bram Moolenaarae97b942020-07-09 19:16:35 +02001543 (void)vpeekc(); // check for end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 }
1545 while (scriptin[oldcurscript] != NULL);
1546
1547 State = save_State;
1548 msg_scroll = save_msg_scroll;
1549 restart_edit = save_restart_edit;
1550 p_im = save_insertmode;
1551 finish_op = save_finish_op;
1552 }
1553}
1554
1555/*
1556 * Close the currently active input script.
1557 */
1558 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001559closescript(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560{
1561 free_typebuf();
1562 typebuf = saved_typebuf[curscript];
1563
1564 fclose(scriptin[curscript]);
1565 scriptin[curscript] = NULL;
1566 if (curscript > 0)
1567 --curscript;
1568}
1569
Bram Moolenaarea408852005-06-25 22:49:46 +00001570#if defined(EXITFREE) || defined(PROTO)
1571 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001572close_all_scripts(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00001573{
1574 while (scriptin[0] != NULL)
1575 closescript();
1576}
1577#endif
1578
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579/*
1580 * Return TRUE when reading keys from a script file.
1581 */
1582 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001583using_script(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584{
1585 return scriptin[curscript] != NULL;
1586}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587
1588/*
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001589 * This function is called just before doing a blocking wait. Thus after
1590 * waiting 'updatetime' for a character to arrive.
1591 */
1592 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001593before_blocking(void)
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001594{
1595 updatescript(0);
1596#ifdef FEAT_EVAL
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001597 if (may_garbage_collect)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001598 garbage_collect(FALSE);
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001599#endif
1600}
1601
1602/*
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001603 * updatescript() is called when a character can be written into the script file
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 * or when we have waited some time for a character (c == 0)
1605 *
1606 * All the changed memfiles are synced if c == 0 or when the number of typed
1607 * characters reaches 'updatecount' and 'updatecount' is non-zero.
1608 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001609 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001610updatescript(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611{
1612 static int count = 0;
1613
1614 if (c && scriptout)
1615 putc(c, scriptout);
1616 if (c == 0 || (p_uc > 0 && ++count >= p_uc))
1617 {
1618 ml_sync_all(c == 0, TRUE);
1619 count = 0;
1620 }
1621}
1622
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623/*
Bram Moolenaar975a8802020-06-06 22:36:24 +02001624 * Convert "c" plus "modifiers" to merge the effect of modifyOtherKeys into the
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001625 * character.
1626 */
1627 int
Bram Moolenaar975a8802020-06-06 22:36:24 +02001628merge_modifyOtherKeys(int c_arg, int *modifiers)
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001629{
1630 int c = c_arg;
1631
Bram Moolenaar975a8802020-06-06 22:36:24 +02001632 if (*modifiers & MOD_MASK_CTRL)
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001633 {
1634 if ((c >= '`' && c <= 0x7f) || (c >= '@' && c <= '_'))
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001635 c &= 0x1f;
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001636 else if (c == '6')
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001637 // CTRL-6 is equivalent to CTRL-^
1638 c = 0x1e;
Bram Moolenaarf4ae6b22020-05-30 19:52:46 +02001639#ifdef FEAT_GUI_GTK
1640 // These mappings look arbitrary at the first glance, but in fact
1641 // resemble quite exactly the behaviour of the GTK+ 1.2 GUI on my
1642 // machine. The only difference is BS vs. DEL for CTRL-8 (makes
1643 // more sense and is consistent with usual terminal behaviour).
1644 else if (c == '2')
1645 c = NUL;
1646 else if (c >= '3' && c <= '7')
1647 c = c ^ 0x28;
1648 else if (c == '8')
1649 c = BS;
1650 else if (c == '?')
1651 c = DEL;
1652#endif
1653 if (c != c_arg)
Bram Moolenaar975a8802020-06-06 22:36:24 +02001654 *modifiers &= ~MOD_MASK_CTRL;
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001655 }
Bram Moolenaar975a8802020-06-06 22:36:24 +02001656 if ((*modifiers & (MOD_MASK_META | MOD_MASK_ALT))
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001657 && c >= 0 && c <= 127)
1658 {
1659 c += 0x80;
Bram Moolenaar975a8802020-06-06 22:36:24 +02001660 *modifiers &= ~(MOD_MASK_META|MOD_MASK_ALT);
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001661 }
1662 return c;
1663}
1664
1665/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 * Get the next input character.
1667 * Can return a special key or a multi-byte character.
1668 * Can return NUL when called recursively, use safe_vgetc() if that's not
1669 * wanted.
1670 * This translates escaped K_SPECIAL and CSI bytes to a K_SPECIAL or CSI byte.
1671 * Collects the bytes of a multibyte character into the whole character.
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00001672 * Returns the modifiers in the global "mod_mask".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 */
1674 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001675vgetc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676{
1677 int c, c2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001679 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001682#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01001683 // Do garbage collection when garbagecollect() was called previously and
1684 // we are now at the toplevel.
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001685 if (may_garbage_collect && want_garbage_collect)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001686 garbage_collect(FALSE);
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001687#endif
1688
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 /*
1690 * If a character was put back with vungetc, it was already processed.
1691 * Return it directly.
1692 */
1693 if (old_char != -1)
1694 {
1695 c = old_char;
1696 old_char = -1;
1697 mod_mask = old_mod_mask;
Bram Moolenaarb8978712013-03-16 21:42:16 +01001698 mouse_row = old_mouse_row;
1699 mouse_col = old_mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001701 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 {
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001703 mod_mask = 0;
1704 vgetc_mod_mask = 0;
1705 vgetc_char = 0;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001706 last_recorded_len = 0;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001707
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001708 for (;;) // this is done twice if there are modifiers
1709 {
1710 int did_inc = FALSE;
Bram Moolenaar2455c4e2015-09-15 18:29:39 +02001711
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001712 if (mod_mask
Bram Moolenaar39719052017-09-05 22:20:46 +02001713#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001714 || im_is_preediting()
Bram Moolenaar39719052017-09-05 22:20:46 +02001715#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001716#if defined(FEAT_PROP_POPUP)
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001717 || popup_no_mapping()
1718#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001719 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 {
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001721 // no mapping after modifier has been read
1722 ++no_mapping;
1723 ++allow_keys;
1724 did_inc = TRUE; // mod_mask may change value
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 }
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001726 c = vgetorpeek(TRUE);
1727 if (did_inc)
1728 {
1729 --no_mapping;
1730 --allow_keys;
1731 }
1732
1733 // Get two extra bytes for special keys
1734 if (c == K_SPECIAL
1735#ifdef FEAT_GUI
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001736 || (c == CSI)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001737#endif
1738 )
1739 {
1740 int save_allow_keys = allow_keys;
1741
1742 ++no_mapping;
1743 allow_keys = 0; // make sure BS is not found
1744 c2 = vgetorpeek(TRUE); // no mapping for these chars
1745 c = vgetorpeek(TRUE);
1746 --no_mapping;
1747 allow_keys = save_allow_keys;
1748 if (c2 == KS_MODIFIER)
1749 {
1750 mod_mask = c;
1751 continue;
1752 }
1753 c = TO_SPECIAL(c2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754
Bram Moolenaar4f974752019-02-17 17:44:42 +01001755#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001756 // Handle K_TEAROFF here, the caller of vgetc() doesn't need to
1757 // know that a menu was torn off
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001758 if (
1759# ifdef VIMDLL
1760 gui.in_use &&
1761# endif
1762 c == K_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 {
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001764 char_u name[200];
1765 int i;
1766
1767 // get menu path, it ends with a <CR>
1768 for (i = 0; (c = vgetorpeek(TRUE)) != '\r'; )
1769 {
1770 name[i] = c;
1771 if (i < 199)
1772 ++i;
1773 }
1774 name[i] = NUL;
1775 gui_make_tearoff(name);
1776 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778#endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001779#if defined(FEAT_GUI) && defined(FEAT_GUI_GTK) && defined(FEAT_MENU)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001780 // GTK: <F10> normally selects the menu, but it's passed until
1781 // here to allow mapping it. Intercept and invoke the GTK
1782 // behavior if it's not mapped.
1783 if (c == K_F10 && gui.menubar != NULL)
1784 {
1785 gtk_menu_shell_select_first(
1786 GTK_MENU_SHELL(gui.menubar), FALSE);
1787 continue;
1788 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001789#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790#ifdef FEAT_GUI
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001791 // Handle focus event here, so that the caller doesn't need to
1792 // know about it. Return K_IGNORE so that we loop once (needed
1793 // if 'lazyredraw' is set).
1794 if (c == K_FOCUSGAINED || c == K_FOCUSLOST)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001795 {
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001796 ui_focus_change(c == K_FOCUSGAINED);
1797 c = K_IGNORE;
Bram Moolenaara9dd2d32019-04-29 21:58:41 +02001798 }
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001799
1800 // Translate K_CSI to CSI. The special key is only used to
1801 // avoid it being recognized as the start of a special key.
1802 if (c == K_CSI)
1803 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001805 }
1806 // a keypad or special function key was not mapped, use it like
1807 // its ASCII equivalent
1808 switch (c)
1809 {
1810 case K_KPLUS: c = '+'; break;
1811 case K_KMINUS: c = '-'; break;
1812 case K_KDIVIDE: c = '/'; break;
1813 case K_KMULTIPLY: c = '*'; break;
1814 case K_KENTER: c = CAR; break;
1815 case K_KPOINT:
Bram Moolenaar4f974752019-02-17 17:44:42 +01001816#ifdef MSWIN
Bram Moolenaar8e85db02018-07-27 23:16:51 +02001817 // Can be either '.' or a ',',
1818 // depending on the type of keypad.
1819 c = MapVirtualKey(VK_DECIMAL, 2); break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001820#else
Bram Moolenaar8e85db02018-07-27 23:16:51 +02001821 c = '.'; break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001822#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001823 case K_K0: c = '0'; break;
1824 case K_K1: c = '1'; break;
1825 case K_K2: c = '2'; break;
1826 case K_K3: c = '3'; break;
1827 case K_K4: c = '4'; break;
1828 case K_K5: c = '5'; break;
1829 case K_K6: c = '6'; break;
1830 case K_K7: c = '7'; break;
1831 case K_K8: c = '8'; break;
1832 case K_K9: c = '9'; break;
Bram Moolenaara88d9682005-03-25 21:45:43 +00001833
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001834 case K_XHOME:
1835 case K_ZHOME: if (mod_mask == MOD_MASK_SHIFT)
Bram Moolenaara88d9682005-03-25 21:45:43 +00001836 {
1837 c = K_S_HOME;
1838 mod_mask = 0;
1839 }
1840 else if (mod_mask == MOD_MASK_CTRL)
1841 {
1842 c = K_C_HOME;
1843 mod_mask = 0;
1844 }
1845 else
1846 c = K_HOME;
1847 break;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001848 case K_XEND:
1849 case K_ZEND: if (mod_mask == MOD_MASK_SHIFT)
Bram Moolenaara88d9682005-03-25 21:45:43 +00001850 {
1851 c = K_S_END;
1852 mod_mask = 0;
1853 }
1854 else if (mod_mask == MOD_MASK_CTRL)
1855 {
1856 c = K_C_END;
1857 mod_mask = 0;
1858 }
1859 else
1860 c = K_END;
1861 break;
1862
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001863 case K_XUP: c = K_UP; break;
1864 case K_XDOWN: c = K_DOWN; break;
1865 case K_XLEFT: c = K_LEFT; break;
1866 case K_XRIGHT: c = K_RIGHT; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001869 // For a multi-byte character get all the bytes and return the
1870 // converted character.
1871 // Note: This will loop until enough bytes are received!
1872 if (has_mbyte && (n = MB_BYTE2LEN_CHECK(c)) > 1)
1873 {
1874 ++no_mapping;
1875 buf[0] = c;
1876 for (i = 1; i < n; ++i)
1877 {
1878 buf[i] = vgetorpeek(TRUE);
1879 if (buf[i] == K_SPECIAL
1880#ifdef FEAT_GUI
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001881 || (buf[i] == CSI)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001882#endif
1883 )
1884 {
1885 // Must be a K_SPECIAL - KS_SPECIAL - KE_FILLER
1886 // sequence, which represents a K_SPECIAL (0x80),
1887 // or a CSI - KS_EXTRA - KE_CSI sequence, which
1888 // represents a CSI (0x9B),
1889 // or a K_SPECIAL - KS_EXTRA - KE_CSI, which is CSI
1890 // too.
1891 c = vgetorpeek(TRUE);
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01001892 if (vgetorpeek(TRUE) == KE_CSI && c == KS_EXTRA)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001893 buf[i] = CSI;
1894 }
1895 }
1896 --no_mapping;
1897 c = (*mb_ptr2char)(buf);
1898 }
1899
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02001900 if (vgetc_char == 0)
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001901 {
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001902 vgetc_mod_mask = mod_mask;
1903 vgetc_char = c;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001904 }
Bram Moolenaar00eab7f2019-10-10 21:49:28 +02001905
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001906 break;
1907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001909
1910#ifdef FEAT_EVAL
1911 /*
1912 * In the main loop "may_garbage_collect" can be set to do garbage
1913 * collection in the first next vgetc(). It's disabled after that to
1914 * avoid internally used Lists and Dicts to be freed.
1915 */
1916 may_garbage_collect = FALSE;
1917#endif
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001918
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001919#ifdef FEAT_BEVAL_TERM
Bram Moolenaarc2f50542019-07-05 23:24:56 +02001920 if (c != K_MOUSEMOVE && c != K_IGNORE && c != K_CURSORHOLD)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001921 {
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001922 // Don't trigger 'balloonexpr' unless only the mouse was moved.
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001923 bevalexpr_due_set = FALSE;
1924 ui_remove_balloon();
1925 }
1926#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001927#ifdef FEAT_PROP_POPUP
Bram Moolenaar4934ad02020-09-28 22:29:58 +02001928 // Only filter keys that do not come from ":normal". Keys from feedkeys()
1929 // are filtered.
1930 if ((!ex_normal_busy || in_feedkeys) && popup_do_filter(c))
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02001931 {
1932 if (c == Ctrl_C)
1933 got_int = FALSE; // avoid looping
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001934 c = K_IGNORE;
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02001935 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001936#endif
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001937
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001938 // Need to process the character before we know it's safe to do something
1939 // else.
1940 if (c != K_IGNORE)
Bram Moolenaard103ee72019-09-18 21:15:31 +02001941 state_no_longer_safe("key typed");
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001942
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001943 return c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944}
1945
1946/*
1947 * Like vgetc(), but never return a NUL when called recursively, get a key
1948 * directly from the user (ignoring typeahead).
1949 */
1950 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001951safe_vgetc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952{
1953 int c;
1954
1955 c = vgetc();
1956 if (c == NUL)
1957 c = get_keystroke();
1958 return c;
1959}
1960
1961/*
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001962 * Like safe_vgetc(), but loop to handle K_IGNORE.
1963 * Also ignore scrollbar events.
1964 */
1965 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001966plain_vgetc(void)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001967{
1968 int c;
1969
1970 do
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001971 c = safe_vgetc();
Bram Moolenaar3a006592021-05-05 19:58:17 +02001972 while (c == K_IGNORE
1973 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
1974 || c == K_MOUSEMOVE);
Bram Moolenaarec2da362017-01-21 20:04:22 +01001975
1976 if (c == K_PS)
Bram Moolenaar30613902019-12-01 22:11:18 +01001977 // Only handle the first pasted character. Drop the rest, since we
1978 // don't know what to do with it.
Bram Moolenaarec2da362017-01-21 20:04:22 +01001979 c = bracketed_paste(PASTE_ONE_CHAR, FALSE, NULL);
1980
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001981 return c;
1982}
1983
1984/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 * Check if a character is available, such that vgetc() will not block.
1986 * If the next character is a special character or multi-byte, the returned
1987 * character is not valid!.
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02001988 * Returns NUL if no character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 */
1990 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001991vpeekc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992{
1993 if (old_char != -1)
1994 return old_char;
1995 return vgetorpeek(FALSE);
1996}
1997
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01001998#if defined(FEAT_TERMRESPONSE) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999/*
2000 * Like vpeekc(), but don't allow mapping. Do allow checking for terminal
2001 * codes.
2002 */
2003 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002004vpeekc_nomap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005{
2006 int c;
2007
2008 ++no_mapping;
2009 ++allow_keys;
2010 c = vpeekc();
2011 --no_mapping;
2012 --allow_keys;
2013 return c;
2014}
2015#endif
2016
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017/*
2018 * Check if any character is available, also half an escape sequence.
2019 * Trick: when no typeahead found, but there is something in the typeahead
2020 * buffer, it must be an ESC that is recognized as the start of a key code.
2021 */
2022 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002023vpeekc_any(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024{
2025 int c;
2026
2027 c = vpeekc();
2028 if (c == NUL && typebuf.tb_len > 0)
2029 c = ESC;
2030 return c;
2031}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032
2033/*
2034 * Call vpeekc() without causing anything to be mapped.
2035 * Return TRUE if a character is available, FALSE otherwise.
2036 */
2037 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002038char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039{
2040 int retval;
2041
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01002042#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01002043 // When test_override("char_avail", 1) was called pretend there is no
2044 // typeahead.
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01002045 if (disable_char_avail_for_testing)
2046 return FALSE;
2047#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 ++no_mapping;
2049 retval = vpeekc();
2050 --no_mapping;
2051 return (retval != NUL);
2052}
2053
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002054#if defined(FEAT_EVAL) || defined(PROTO)
2055/*
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002056 * "getchar()" and "getcharstr()" functions
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002057 */
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002058 static void
2059getchar_common(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002060{
2061 varnumber_T n;
2062 int error = FALSE;
2063
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002064 if (in_vim9script() && check_for_opt_bool_arg(argvars, 0) == FAIL)
2065 return;
2066
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002067#ifdef MESSAGE_QUEUE
2068 // vpeekc() used to check for messages, but that caused problems, invoking
2069 // a callback where it was not expected. Some plugins use getchar(1) in a
2070 // loop to await a message, therefore make sure we check for messages here.
2071 parse_queued_messages();
2072#endif
2073
Bram Moolenaar30613902019-12-01 22:11:18 +01002074 // Position the cursor. Needed after a message that ends in a space.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002075 windgoto(msg_row, msg_col);
2076
2077 ++no_mapping;
2078 ++allow_keys;
2079 for (;;)
2080 {
2081 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar30613902019-12-01 22:11:18 +01002082 // getchar(): blocking wait.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002083 n = plain_vgetc();
Bram Moolenaarc08cc722020-09-05 17:51:23 +02002084 else if (tv_get_bool_chk(&argvars[0], &error))
Bram Moolenaar30613902019-12-01 22:11:18 +01002085 // getchar(1): only check if char avail
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002086 n = vpeekc_any();
2087 else if (error || vpeekc_any() == NUL)
Bram Moolenaar30613902019-12-01 22:11:18 +01002088 // illegal argument or getchar(0) and no char avail: return zero
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002089 n = 0;
2090 else
Bram Moolenaar15183b42020-09-05 19:59:39 +02002091 // getchar(0) and char avail() != NUL: get a character.
2092 // Note that vpeekc_any() returns K_SPECIAL for K_IGNORE.
2093 n = safe_vgetc();
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002094
Bram Moolenaar15183b42020-09-05 19:59:39 +02002095 if (n == K_IGNORE || n == K_MOUSEMOVE
2096 || n == K_VER_SCROLLBAR || n == K_HOR_SCROLLBAR)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002097 continue;
2098 break;
2099 }
2100 --no_mapping;
2101 --allow_keys;
2102
2103 set_vim_var_nr(VV_MOUSE_WIN, 0);
2104 set_vim_var_nr(VV_MOUSE_WINID, 0);
2105 set_vim_var_nr(VV_MOUSE_LNUM, 0);
2106 set_vim_var_nr(VV_MOUSE_COL, 0);
2107
2108 rettv->vval.v_number = n;
2109 if (IS_SPECIAL(n) || mod_mask != 0)
2110 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002111 char_u temp[10]; // modifier: 3, mbyte-char: 6, NUL: 1
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002112 int i = 0;
2113
Bram Moolenaar30613902019-12-01 22:11:18 +01002114 // Turn a special key into three bytes, plus modifier.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002115 if (mod_mask != 0)
2116 {
2117 temp[i++] = K_SPECIAL;
2118 temp[i++] = KS_MODIFIER;
2119 temp[i++] = mod_mask;
2120 }
2121 if (IS_SPECIAL(n))
2122 {
2123 temp[i++] = K_SPECIAL;
2124 temp[i++] = K_SECOND(n);
2125 temp[i++] = K_THIRD(n);
2126 }
2127 else if (has_mbyte)
2128 i += (*mb_char2bytes)(n, temp + i);
2129 else
2130 temp[i++] = n;
2131 temp[i++] = NUL;
2132 rettv->v_type = VAR_STRING;
2133 rettv->vval.v_string = vim_strsave(temp);
2134
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002135 if (is_mouse_key(n))
2136 {
2137 int row = mouse_row;
2138 int col = mouse_col;
2139 win_T *win;
2140 linenr_T lnum;
2141 win_T *wp;
2142 int winnr = 1;
2143
2144 if (row >= 0 && col >= 0)
2145 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002146 // Find the window at the mouse coordinates and compute the
2147 // text position.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002148 win = mouse_find_win(&row, &col, FIND_POPUP);
2149 if (win == NULL)
2150 return;
2151 (void)mouse_comp_pos(win, &row, &col, &lnum, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002152#ifdef FEAT_PROP_POPUP
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002153 if (WIN_IS_POPUP(win))
2154 winnr = 0;
2155 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002156#endif
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002157 for (wp = firstwin; wp != win && wp != NULL;
2158 wp = wp->w_next)
2159 ++winnr;
2160 set_vim_var_nr(VV_MOUSE_WIN, winnr);
2161 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
2162 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
2163 set_vim_var_nr(VV_MOUSE_COL, col + 1);
2164 }
2165 }
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002166 }
2167}
2168
2169/*
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002170 * "getchar()" function
2171 */
2172 void
2173f_getchar(typval_T *argvars, typval_T *rettv)
2174{
2175 getchar_common(argvars, rettv);
2176}
2177
2178/*
2179 * "getcharstr()" function
2180 */
2181 void
2182f_getcharstr(typval_T *argvars, typval_T *rettv)
2183{
2184 getchar_common(argvars, rettv);
2185
2186 if (rettv->v_type == VAR_NUMBER)
2187 {
2188 char_u temp[7]; // mbyte-char: 6, NUL: 1
2189 varnumber_T n = rettv->vval.v_number;
2190 int i = 0;
2191
2192 if (n != 0)
2193 {
2194 if (has_mbyte)
2195 i += (*mb_char2bytes)(n, temp + i);
2196 else
2197 temp[i++] = n;
2198 }
2199 temp[i++] = NUL;
2200 rettv->v_type = VAR_STRING;
2201 rettv->vval.v_string = vim_strsave(temp);
2202 }
2203}
2204
2205/*
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002206 * "getcharmod()" function
2207 */
2208 void
2209f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
2210{
2211 rettv->vval.v_number = mod_mask;
2212}
2213#endif // FEAT_EVAL
2214
2215#if defined(MESSAGE_QUEUE) || defined(PROTO)
2216# define MAX_REPEAT_PARSE 8
2217
2218/*
2219 * Process messages that have been queued for netbeans or clientserver.
2220 * Also check if any jobs have ended.
h-east965d2ed2021-10-04 21:51:57 +01002221 * These functions can call arbitrary Vim script and should only be called when
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002222 * it is safe to do so.
2223 */
2224 void
2225parse_queued_messages(void)
2226{
Bram Moolenaar1cac7092019-10-22 21:54:31 +02002227 int old_curwin_id;
2228 int old_curbuf_fnum;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002229 int i;
2230 int save_may_garbage_collect = may_garbage_collect;
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002231 static int entered = 0;
Bram Moolenaard103ee72019-09-18 21:15:31 +02002232 int was_safe = get_was_safe_state();
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002233
2234 // Do not handle messages while redrawing, because it may cause buffers to
2235 // change or be wiped while they are being redrawn.
Bram Moolenaar4778b4d2020-11-04 11:03:12 +01002236 // Also bail out when parsing messages was explicitly disabled.
2237 if (updating_screen || dont_parse_messages)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002238 return;
2239
Bram Moolenaar1cac7092019-10-22 21:54:31 +02002240 // If memory allocation fails during startup we'll exit but curbuf or
2241 // curwin could be NULL.
2242 if (curbuf == NULL || curwin == NULL)
2243 return;
2244
2245 old_curbuf_fnum = curbuf->b_fnum;
2246 old_curwin_id = curwin->w_id;
2247
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002248 ++entered;
2249
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002250 // may_garbage_collect is set in main_loop() to do garbage collection when
2251 // blocking to wait on a character. We don't want that while parsing
2252 // messages, a callback may invoke vgetc() while lists and dicts are in use
2253 // in the call stack.
2254 may_garbage_collect = FALSE;
2255
2256 // Loop when a job ended, but don't keep looping forever.
2257 for (i = 0; i < MAX_REPEAT_PARSE; ++i)
2258 {
2259 // For Win32 mch_breakcheck() does not check for input, do it here.
Bram Moolenaar80a8d382020-05-03 22:57:32 +02002260# if (defined(MSWIN) || defined(__HAIKU__)) && defined(FEAT_JOB_CHANNEL)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002261 channel_handle_events(FALSE);
2262# endif
2263
2264# ifdef FEAT_NETBEANS_INTG
2265 // Process the queued netbeans messages.
2266 netbeans_parse_messages();
2267# endif
2268# ifdef FEAT_JOB_CHANNEL
2269 // Write any buffer lines still to be written.
2270 channel_write_any_lines();
2271
2272 // Process the messages queued on channels.
2273 channel_parse_messages();
2274# endif
2275# if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
2276 // Process the queued clientserver messages.
2277 server_parse_messages();
2278# endif
2279# ifdef FEAT_JOB_CHANNEL
2280 // Check if any jobs have ended. If so, repeat the above to handle
2281 // changes, e.g. stdin may have been closed.
2282 if (job_check_ended())
2283 continue;
2284# endif
2285# ifdef FEAT_TERMINAL
2286 free_unused_terminals();
2287# endif
2288# ifdef FEAT_SOUND_CANBERRA
2289 if (has_sound_callback_in_queue())
2290 invoke_sound_callback();
2291# endif
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002292#ifdef SIGUSR1
2293 if (got_sigusr1)
2294 {
2295 apply_autocmds(EVENT_SIGUSR1, NULL, NULL, FALSE, curbuf);
2296 got_sigusr1 = FALSE;
2297 }
2298#endif
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002299 break;
2300 }
2301
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002302 // When not nested we'll go back to waiting for a typed character. If it
2303 // was safe before then this triggers a SafeStateAgain autocommand event.
Bram Moolenaard103ee72019-09-18 21:15:31 +02002304 if (entered == 1 && was_safe)
Bram Moolenaar37d18072019-09-17 20:28:38 +02002305 may_trigger_safestateagain();
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002306
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002307 may_garbage_collect = save_may_garbage_collect;
2308
2309 // If the current window or buffer changed we need to bail out of the
2310 // waiting loop. E.g. when a job exit callback closes the terminal window.
2311 if (curwin->w_id != old_curwin_id || curbuf->b_fnum != old_curbuf_fnum)
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02002312 ins_char_typebuf(K_IGNORE, 0);
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002313
2314 --entered;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002315}
2316#endif
2317
2318
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002319typedef enum {
2320 map_result_fail, // failed, break loop
2321 map_result_get, // get a character from typeahead
2322 map_result_retry, // try to map again
2323 map_result_nomatch // no matching mapping, get char
2324} map_result_T;
2325
2326/*
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002327 * Check if the bytes at the start of the typeahead buffer are a character used
2328 * in CTRL-X mode. This includes the form with a CTRL modifier.
2329 */
2330 static int
2331at_ctrl_x_key(void)
2332{
2333 char_u *p = typebuf.tb_buf + typebuf.tb_off;
2334 int c = *p;
2335
2336 if (typebuf.tb_len > 3
2337 && c == K_SPECIAL
2338 && p[1] == KS_MODIFIER
2339 && (p[2] & MOD_MASK_CTRL))
2340 c = p[3] & 0x1f;
2341 return vim_is_ctrl_x_key(c);
2342}
2343
2344/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002345 * Check if typebuf.tb_buf[] contains a modifier plus key that can be changed
Bram Moolenaar975a8802020-06-06 22:36:24 +02002346 * into just a key, apply that.
2347 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
2348 * + "max_offset"].
2349 * Return the length of the replaced bytes, zero if nothing changed.
2350 */
2351 static int
2352check_simplify_modifier(int max_offset)
2353{
2354 int offset;
2355 char_u *tp;
2356
2357 for (offset = 0; offset < max_offset; ++offset)
2358 {
2359 if (offset + 3 >= typebuf.tb_len)
2360 break;
2361 tp = typebuf.tb_buf + typebuf.tb_off + offset;
2362 if (tp[0] == K_SPECIAL && tp[1] == KS_MODIFIER)
2363 {
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002364 // A modifier was not used for a mapping, apply it to ASCII keys.
2365 // Shift would already have been applied.
Bram Moolenaar975a8802020-06-06 22:36:24 +02002366 int modifier = tp[2];
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002367 int c = tp[3];
2368 int new_c = merge_modifyOtherKeys(c, &modifier);
Bram Moolenaar975a8802020-06-06 22:36:24 +02002369
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002370 if (new_c != c)
Bram Moolenaar975a8802020-06-06 22:36:24 +02002371 {
2372 char_u new_string[MB_MAXBYTES];
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002373 int len;
Bram Moolenaar975a8802020-06-06 22:36:24 +02002374
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002375 if (offset == 0)
2376 {
2377 // At the start: remember the character and mod_mask before
2378 // merging, in some cases, e.g. at the hit-return prompt,
2379 // they are put back in the typeahead buffer.
2380 vgetc_char = c;
2381 vgetc_mod_mask = tp[2];
2382 }
2383 len = mb_char2bytes(new_c, new_string);
2384 if (modifier == 0)
2385 {
2386 if (put_string_in_typebuf(offset, 4, new_string, len,
Bram Moolenaar975a8802020-06-06 22:36:24 +02002387 NULL, 0, 0) == FAIL)
2388 return -1;
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002389 }
2390 else
2391 {
2392 tp[2] = modifier;
2393 if (put_string_in_typebuf(offset + 3, 1, new_string, len,
2394 NULL, 0, 0) == FAIL)
2395 return -1;
2396 }
Bram Moolenaar975a8802020-06-06 22:36:24 +02002397 return len;
2398 }
2399 }
2400 }
2401 return 0;
2402}
2403
2404/*
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002405 * Handle mappings in the typeahead buffer.
2406 * - When something was mapped, return map_result_retry for recursive mappings.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002407 * - When nothing mapped and typeahead has a character: return map_result_get.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002408 * - When there is no match yet, return map_result_nomatch, need to get more
2409 * typeahead.
2410 */
2411 static int
2412handle_mapping(
2413 int *keylenp,
2414 int *timedout,
2415 int *mapdepth)
2416{
2417 mapblock_T *mp = NULL;
2418 mapblock_T *mp2;
2419 mapblock_T *mp_match;
2420 int mp_match_len = 0;
2421 int max_mlen = 0;
2422 int tb_c1;
2423 int mlen;
2424#ifdef FEAT_LANGMAP
2425 int nolmaplen;
2426#endif
2427 int keylen = *keylenp;
2428 int i;
2429 int local_State = get_real_state();
2430
2431 /*
2432 * Check for a mappable key sequence.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002433 * Walk through one maphash[] list until we find an entry that matches.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002434 *
2435 * Don't look for mappings if:
2436 * - no_mapping set: mapping disabled (e.g. for CTRL-V)
2437 * - maphash_valid not set: no mappings present.
2438 * - typebuf.tb_buf[typebuf.tb_off] should not be remapped
2439 * - in insert or cmdline mode and 'paste' option set
Bram Moolenaareda35f72019-08-03 14:59:44 +02002440 * - waiting for "hit return to continue" and CR or SPACE typed
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002441 * - waiting for a char with --more--
2442 * - in Ctrl-X mode, and we get a valid char for that mode
2443 */
2444 tb_c1 = typebuf.tb_buf[typebuf.tb_off];
2445 if (no_mapping == 0 && is_maphash_valid()
2446 && (no_zero_mapping == 0 || tb_c1 != '0')
2447 && (typebuf.tb_maplen == 0
2448 || (p_remap
2449 && (typebuf.tb_noremap[typebuf.tb_off]
2450 & (RM_NONE|RM_ABBR)) == 0))
2451 && !(p_paste && (State & (INSERT + CMDLINE)))
2452 && !(State == HITRETURN && (tb_c1 == CAR || tb_c1 == ' '))
2453 && State != ASKMORE
2454 && State != CONFIRM
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002455 && !((ctrl_x_mode_not_default() && at_ctrl_x_key())
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002456 || (compl_status_local()
Bram Moolenaare2c453d2019-08-21 14:37:09 +02002457 && (tb_c1 == Ctrl_N || tb_c1 == Ctrl_P))))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002458 {
Bram Moolenaarc9983702020-05-28 21:03:53 +02002459#ifdef FEAT_GUI
2460 if (gui.in_use && tb_c1 == CSI && typebuf.tb_len >= 2
2461 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_MODIFIER)
2462 {
2463 // The GUI code sends CSI KS_MODIFIER {flags}, but mappings expect
2464 // K_SPECIAL KS_MODIFIER {flags}.
2465 tb_c1 = K_SPECIAL;
2466 }
2467#endif
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002468#ifdef FEAT_LANGMAP
2469 if (tb_c1 == K_SPECIAL)
2470 nolmaplen = 2;
2471 else
2472 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002473 LANGMAP_ADJUST(tb_c1, (State & (CMDLINE | INSERT)) == 0
2474 && get_real_state() != SELECTMODE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002475 nolmaplen = 0;
2476 }
2477#endif
2478 // First try buffer-local mappings.
2479 mp = get_buf_maphash_list(local_State, tb_c1);
2480 mp2 = get_maphash_list(local_State, tb_c1);
2481 if (mp == NULL)
2482 {
2483 // There are no buffer-local mappings.
2484 mp = mp2;
2485 mp2 = NULL;
2486 }
Bram Moolenaareda35f72019-08-03 14:59:44 +02002487
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002488 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002489 * Loop until a partly matching mapping is found or all (local)
2490 * mappings have been checked.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002491 * The longest full match is remembered in "mp_match".
Bram Moolenaareda35f72019-08-03 14:59:44 +02002492 * A full match is only accepted if there is no partly match, so "aa"
2493 * and "aaa" can both be mapped.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002494 */
2495 mp_match = NULL;
2496 mp_match_len = 0;
2497 for ( ; mp != NULL;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002498 mp->m_next == NULL ? (mp = mp2, mp2 = NULL) : (mp = mp->m_next))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002499 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002500 // Only consider an entry if the first character matches and it is
2501 // for the current state.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002502 // Skip ":lmap" mappings if keys were mapped.
2503 if (mp->m_keys[0] == tb_c1
2504 && (mp->m_mode & local_State)
Bram Moolenaar46cd43b2020-06-04 22:22:11 +02002505 && !(mp->m_simplified && seenModifyOtherKeys
2506 && typebuf.tb_maplen == 0)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002507 && ((mp->m_mode & LANGMAP) == 0 || typebuf.tb_maplen == 0))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002508 {
2509#ifdef FEAT_LANGMAP
2510 int nomap = nolmaplen;
2511 int c2;
2512#endif
2513 // find the match length of this mapping
2514 for (mlen = 1; mlen < typebuf.tb_len; ++mlen)
2515 {
2516#ifdef FEAT_LANGMAP
2517 c2 = typebuf.tb_buf[typebuf.tb_off + mlen];
2518 if (nomap > 0)
2519 --nomap;
2520 else if (c2 == K_SPECIAL)
2521 nomap = 2;
2522 else
2523 LANGMAP_ADJUST(c2, TRUE);
2524 if (mp->m_keys[mlen] != c2)
2525#else
2526 if (mp->m_keys[mlen] !=
Bram Moolenaarc9983702020-05-28 21:03:53 +02002527 typebuf.tb_buf[typebuf.tb_off + mlen])
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002528#endif
2529 break;
2530 }
2531
Bram Moolenaareda35f72019-08-03 14:59:44 +02002532 // Don't allow mapping the first byte(s) of a multi-byte char.
2533 // Happens when mapping <M-a> and then changing 'encoding'.
2534 // Beware that 0x80 is escaped.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002535 {
2536 char_u *p1 = mp->m_keys;
2537 char_u *p2 = mb_unescape(&p1);
2538
2539 if (has_mbyte && p2 != NULL
Bram Moolenaar1614a142019-10-06 22:00:13 +02002540 && MB_BYTE2LEN(tb_c1) > mb_ptr2len(p2))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002541 mlen = 0;
2542 }
2543
2544 // Check an entry whether it matches.
2545 // - Full match: mlen == keylen
2546 // - Partly match: mlen == typebuf.tb_len
2547 keylen = mp->m_keylen;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002548 if (mlen == keylen || (mlen == typebuf.tb_len
2549 && typebuf.tb_len < keylen))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002550 {
2551 char_u *s;
2552 int n;
2553
Bram Moolenaareda35f72019-08-03 14:59:44 +02002554 // If only script-local mappings are allowed, check if the
2555 // mapping starts with K_SNR.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002556 s = typebuf.tb_noremap + typebuf.tb_off;
2557 if (*s == RM_SCRIPT
2558 && (mp->m_keys[0] != K_SPECIAL
2559 || mp->m_keys[1] != KS_EXTRA
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01002560 || mp->m_keys[2] != KE_SNR))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002561 continue;
2562
Bram Moolenaareda35f72019-08-03 14:59:44 +02002563 // If one of the typed keys cannot be remapped, skip the
2564 // entry.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002565 for (n = mlen; --n >= 0; )
2566 if (*s++ & (RM_NONE|RM_ABBR))
2567 break;
2568 if (n >= 0)
2569 continue;
2570
2571 if (keylen > typebuf.tb_len)
2572 {
2573 if (!*timedout && !(mp_match != NULL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002574 && mp_match->m_nowait))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002575 {
2576 // break at a partly match
2577 keylen = KEYLEN_PART_MAP;
2578 break;
2579 }
2580 }
2581 else if (keylen > mp_match_len)
2582 {
2583 // found a longer match
2584 mp_match = mp;
2585 mp_match_len = keylen;
2586 }
2587 }
2588 else
Bram Moolenaareda35f72019-08-03 14:59:44 +02002589 // No match; may have to check for termcode at next
2590 // character.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002591 if (max_mlen < mlen)
2592 max_mlen = mlen;
2593 }
2594 }
2595
Bram Moolenaareda35f72019-08-03 14:59:44 +02002596 // If no partly match found, use the longest full match.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002597 if (keylen != KEYLEN_PART_MAP)
2598 {
2599 mp = mp_match;
2600 keylen = mp_match_len;
2601 }
2602 }
2603
2604 /*
2605 * Check for match with 'pastetoggle'
2606 */
2607 if (*p_pt != NUL && mp == NULL && (State & (INSERT|NORMAL)))
2608 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002609 for (mlen = 0; mlen < typebuf.tb_len && p_pt[mlen]; ++mlen)
2610 if (p_pt[mlen] != typebuf.tb_buf[typebuf.tb_off + mlen])
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002611 break;
2612 if (p_pt[mlen] == NUL) // match
2613 {
2614 // write chars to script file(s)
2615 if (mlen > typebuf.tb_maplen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002616 gotchars(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_maplen,
2617 mlen - typebuf.tb_maplen);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002618
2619 del_typebuf(mlen, 0); // remove the chars
Bram Moolenaareda35f72019-08-03 14:59:44 +02002620 set_option_value((char_u *)"paste", (long)!p_paste, NULL, 0);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002621 if (!(State & INSERT))
2622 {
2623 msg_col = 0;
2624 msg_row = Rows - 1;
2625 msg_clr_eos(); // clear ruler
2626 }
2627 status_redraw_all();
2628 redraw_statuslines();
2629 showmode();
2630 setcursor();
2631 *keylenp = keylen;
2632 return map_result_retry;
2633 }
2634 // Need more chars for partly match.
2635 if (mlen == typebuf.tb_len)
2636 keylen = KEYLEN_PART_KEY;
2637 else if (max_mlen < mlen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002638 // no match, may have to check for termcode at next character
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002639 max_mlen = mlen + 1;
2640 }
2641
Bram Moolenaareda35f72019-08-03 14:59:44 +02002642 if ((mp == NULL || max_mlen >= mp_match_len) && keylen != KEYLEN_PART_MAP)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002643 {
2644 int save_keylen = keylen;
2645
2646 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002647 * When no matching mapping found or found a non-matching mapping that
2648 * matches at least what the matching mapping matched:
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002649 * Check if we have a terminal code, when:
Bram Moolenaareda35f72019-08-03 14:59:44 +02002650 * - mapping is allowed,
2651 * - keys have not been mapped,
2652 * - and not an ESC sequence, not in insert mode or p_ek is on,
2653 * - and when not timed out,
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002654 */
2655 if ((no_mapping == 0 || allow_keys != 0)
2656 && (typebuf.tb_maplen == 0
2657 || (p_remap && typebuf.tb_noremap[
Bram Moolenaareda35f72019-08-03 14:59:44 +02002658 typebuf.tb_off] == RM_YES))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002659 && !*timedout)
2660 {
Bram Moolenaar0684e362020-12-03 19:54:42 +01002661 keylen = check_termcode(max_mlen + 1, NULL, 0, NULL);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002662
Bram Moolenaar0684e362020-12-03 19:54:42 +01002663 // If no termcode matched but 'pastetoggle' matched partially
2664 // it's like an incomplete key sequence.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002665 if (keylen == 0 && save_keylen == KEYLEN_PART_KEY)
2666 keylen = KEYLEN_PART_KEY;
2667
Bram Moolenaar975a8802020-06-06 22:36:24 +02002668 // If no termcode matched, try to include the modifier into the
zeertzjqa3f83fe2021-11-22 12:47:39 +00002669 // key. This is for when modifyOtherKeys is working.
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002670 if (keylen == 0 && !no_reduce_keys)
Bram Moolenaar975a8802020-06-06 22:36:24 +02002671 keylen = check_simplify_modifier(max_mlen + 1);
2672
Bram Moolenaareda35f72019-08-03 14:59:44 +02002673 // When getting a partial match, but the last characters were not
2674 // typed, don't wait for a typed character to complete the
2675 // termcode. This helps a lot when a ":normal" command ends in an
2676 // ESC.
2677 if (keylen < 0 && typebuf.tb_len == typebuf.tb_maplen)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002678 keylen = 0;
2679 }
2680 else
2681 keylen = 0;
2682 if (keylen == 0) // no matching terminal code
2683 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002684#ifdef AMIGA
2685 // check for window bounds report
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002686 if (typebuf.tb_maplen == 0 && (typebuf.tb_buf[
Bram Moolenaareda35f72019-08-03 14:59:44 +02002687 typebuf.tb_off] & 0xff) == CSI)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002688 {
2689 char_u *s;
2690
2691 for (s = typebuf.tb_buf + typebuf.tb_off + 1;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002692 s < typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len
2693 && (VIM_ISDIGIT(*s) || *s == ';' || *s == ' ');
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002694 ++s)
2695 ;
2696 if (*s == 'r' || *s == '|') // found one
2697 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002698 del_typebuf(
2699 (int)(s + 1 - (typebuf.tb_buf + typebuf.tb_off)), 0);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002700 // get size and redraw screen
2701 shell_resized();
2702 *keylenp = keylen;
2703 return map_result_retry;
2704 }
2705 if (*s == NUL) // need more characters
2706 keylen = KEYLEN_PART_KEY;
2707 }
2708 if (keylen >= 0)
2709#endif
Bram Moolenaareda35f72019-08-03 14:59:44 +02002710 // When there was a matching mapping and no termcode could be
2711 // replaced after another one, use that mapping (loop around).
2712 // If there was no mapping at all use the character from the
2713 // typeahead buffer right here.
2714 if (mp == NULL)
2715 {
2716 *keylenp = keylen;
Bram Moolenaar52797ba2021-12-16 14:45:13 +00002717 return map_result_get; // get character from typeahead
Bram Moolenaareda35f72019-08-03 14:59:44 +02002718 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002719 }
2720
2721 if (keylen > 0) // full matching terminal code
2722 {
2723#if defined(FEAT_GUI) && defined(FEAT_MENU)
2724 if (typebuf.tb_len >= 2
Bram Moolenaareda35f72019-08-03 14:59:44 +02002725 && typebuf.tb_buf[typebuf.tb_off] == K_SPECIAL
2726 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_MENU)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002727 {
2728 int idx;
2729
Bram Moolenaareda35f72019-08-03 14:59:44 +02002730 // Using a menu may cause a break in undo! It's like using
2731 // gotchars(), but without recording or writing to a script
2732 // file.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002733 may_sync_undo();
2734 del_typebuf(3, 0);
2735 idx = get_menu_index(current_menu, local_State);
2736 if (idx != MENU_INDEX_INVALID)
2737 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002738 // In Select mode and a Visual mode menu is used: Switch
2739 // to Visual mode temporarily. Append K_SELECT to switch
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002740 // back to Select mode.
2741 if (VIsual_active && VIsual_select
Bram Moolenaareda35f72019-08-03 14:59:44 +02002742 && (current_menu->modes & VISUAL))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002743 {
2744 VIsual_select = FALSE;
2745 (void)ins_typebuf(K_SELECT_STRING,
Bram Moolenaareda35f72019-08-03 14:59:44 +02002746 REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002747 }
2748 ins_typebuf(current_menu->strings[idx],
2749 current_menu->noremap[idx],
Bram Moolenaareda35f72019-08-03 14:59:44 +02002750 0, TRUE, current_menu->silent[idx]);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002751 }
2752 }
2753#endif // FEAT_GUI && FEAT_MENU
2754 *keylenp = keylen;
2755 return map_result_retry; // try mapping again
2756 }
2757
Bram Moolenaareda35f72019-08-03 14:59:44 +02002758 // Partial match: get some more characters. When a matching mapping
2759 // was found use that one.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002760 if (mp == NULL || keylen < 0)
2761 keylen = KEYLEN_PART_KEY;
2762 else
2763 keylen = mp_match_len;
2764 }
2765
2766 /*
2767 * complete match
2768 */
2769 if (keylen >= 0 && keylen <= typebuf.tb_len)
2770 {
2771 char_u *map_str;
2772
2773#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002774 int save_m_expr;
2775 int save_m_noremap;
2776 int save_m_silent;
2777 char_u *save_m_keys;
2778 char_u *save_m_str;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002779#else
2780# define save_m_noremap mp->m_noremap
2781# define save_m_silent mp->m_silent
2782#endif
2783
2784 // write chars to script file(s)
2785 if (keylen > typebuf.tb_maplen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002786 gotchars(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_maplen,
2787 keylen - typebuf.tb_maplen);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002788
2789 cmd_silent = (typebuf.tb_silent > 0);
2790 del_typebuf(keylen, 0); // remove the mapped keys
2791
2792 /*
2793 * Put the replacement string in front of mapstr.
2794 * The depth check catches ":map x y" and ":map y x".
2795 */
2796 if (++*mapdepth >= p_mmd)
2797 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00002798 emsg(_(e_recursive_mapping));
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002799 if (State & CMDLINE)
2800 redrawcmdline();
2801 else
2802 setcursor();
2803 flush_buffers(FLUSH_MINIMAL);
Bram Moolenaar30613902019-12-01 22:11:18 +01002804 *mapdepth = 0; // for next one
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002805 *keylenp = keylen;
2806 return map_result_fail;
2807 }
2808
2809 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002810 * In Select mode and a Visual mode mapping is used: Switch to Visual
2811 * mode temporarily. Append K_SELECT to switch back to Select mode.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002812 */
Bram Moolenaareda35f72019-08-03 14:59:44 +02002813 if (VIsual_active && VIsual_select && (mp->m_mode & VISUAL))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002814 {
2815 VIsual_select = FALSE;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002816 (void)ins_typebuf(K_SELECT_STRING, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002817 }
2818
2819#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002820 // Copy the values from *mp that are used, because evaluating the
2821 // expression may invoke a function that redefines the mapping, thereby
2822 // making *mp invalid.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002823 save_m_expr = mp->m_expr;
2824 save_m_noremap = mp->m_noremap;
2825 save_m_silent = mp->m_silent;
2826 save_m_keys = NULL; // only saved when needed
2827 save_m_str = NULL; // only saved when needed
2828
2829 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002830 * Handle ":map <expr>": evaluate the {rhs} as an expression. Also
2831 * save and restore the command line for "normal :".
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002832 */
2833 if (mp->m_expr)
2834 {
2835 int save_vgetc_busy = vgetc_busy;
2836 int save_may_garbage_collect = may_garbage_collect;
Bram Moolenaar4ebe0e62019-11-22 20:55:40 +01002837 int was_screen_col = screen_cur_col;
2838 int was_screen_row = screen_cur_row;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002839
2840 vgetc_busy = 0;
2841 may_garbage_collect = FALSE;
2842
2843 save_m_keys = vim_strsave(mp->m_keys);
2844 save_m_str = vim_strsave(mp->m_str);
2845 map_str = eval_map_expr(save_m_str, NUL);
2846
Bram Moolenaar4ebe0e62019-11-22 20:55:40 +01002847 // The mapping may do anything, but we expect it to take care of
2848 // redrawing. Do put the cursor back where it was.
2849 windgoto(was_screen_row, was_screen_col);
2850 out_flush();
2851
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002852 vgetc_busy = save_vgetc_busy;
2853 may_garbage_collect = save_may_garbage_collect;
2854 }
2855 else
2856#endif
2857 map_str = mp->m_str;
2858
2859 /*
2860 * Insert the 'to' part in the typebuf.tb_buf.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002861 * If 'from' field is the same as the start of the 'to' field, don't
2862 * remap the first character (but do allow abbreviations).
2863 * If m_noremap is set, don't remap the whole 'to' part.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002864 */
2865 if (map_str == NULL)
2866 i = FAIL;
2867 else
2868 {
2869 int noremap;
2870
2871 if (save_m_noremap != REMAP_YES)
2872 noremap = save_m_noremap;
2873 else if (
2874#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002875 STRNCMP(map_str, save_m_keys != NULL ? save_m_keys : mp->m_keys,
2876 (size_t)keylen)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002877#else
2878 STRNCMP(map_str, mp->m_keys, (size_t)keylen)
2879#endif
2880 != 0)
2881 noremap = REMAP_YES;
2882 else
2883 noremap = REMAP_SKIP;
2884 i = ins_typebuf(map_str, noremap,
2885 0, TRUE, cmd_silent || save_m_silent);
2886#ifdef FEAT_EVAL
2887 if (save_m_expr)
2888 vim_free(map_str);
2889#endif
2890 }
2891#ifdef FEAT_EVAL
2892 vim_free(save_m_keys);
2893 vim_free(save_m_str);
2894#endif
2895 *keylenp = keylen;
2896 if (i == FAIL)
2897 return map_result_fail;
2898 return map_result_retry;
2899 }
2900
2901 *keylenp = keylen;
2902 return map_result_nomatch;
2903}
2904
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002905/*
2906 * unget one character (can only be done once!)
2907 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002909vungetc(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910{
2911 old_char = c;
2912 old_mod_mask = mod_mask;
Bram Moolenaarb8978712013-03-16 21:42:16 +01002913 old_mouse_row = mouse_row;
2914 old_mouse_col = mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915}
2916
2917/*
Bram Moolenaar32526b32019-01-19 17:43:09 +01002918 * Get a byte:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 * 1. from the stuffbuffer
2920 * This is used for abbreviated commands like "D" -> "d$".
2921 * Also used to redo a command for ".".
2922 * 2. from the typeahead buffer
2923 * Stores text obtained previously but not used yet.
2924 * Also stores the result of mappings.
2925 * Also used for the ":normal" command.
2926 * 3. from the user
2927 * This may do a blocking wait if "advance" is TRUE.
2928 *
2929 * if "advance" is TRUE (vgetc()):
Bram Moolenaard29459b2016-08-26 22:29:11 +02002930 * Really get the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 * KeyTyped is set to TRUE in the case the user typed the key.
2932 * KeyStuffed is TRUE if the character comes from the stuff buffer.
2933 * if "advance" is FALSE (vpeekc()):
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02002934 * Just look whether there is a character available.
2935 * Return NUL if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 *
2937 * When "no_mapping" is zero, checks for mappings in the current mode.
2938 * Only returns one byte (of a multi-byte character).
2939 * K_SPECIAL and CSI may be escaped, need to get two more bytes then.
2940 */
2941 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002942vgetorpeek(int advance)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943{
2944 int c, c1;
Bram Moolenaar30613902019-12-01 22:11:18 +01002945 int timedout = FALSE; // waited for more than 1 second
2946 // for mapping to complete
2947 int mapdepth = 0; // check for recursive mapping
2948 int mode_deleted = FALSE; // set when mode has been deleted
Bram Moolenaar4e427192006-03-10 21:34:27 +00002949#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 int new_wcol, new_wrow;
2951#endif
2952#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01002953 int shape_changed = FALSE; // adjusted cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954#endif
2955 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 int old_wcol, old_wrow;
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00002957 int wait_tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958
2959 /*
2960 * This function doesn't work very well when called recursively. This may
2961 * happen though, because of:
2962 * 1. The call to add_to_showcmd(). char_avail() is then used to check if
2963 * there is a character available, which calls this function. In that
2964 * case we must return NUL, to indicate no character is available.
2965 * 2. A GUI callback function writes to the screen, causing a
2966 * wait_return().
2967 * Using ":normal" can also do this, but it saves the typeahead buffer,
2968 * thus it should be OK. But don't get a key from the user then.
2969 */
Bram Moolenaare2c38102016-01-31 14:55:40 +01002970 if (vgetc_busy > 0 && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 return NUL;
2972
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002973 ++vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974
2975 if (advance)
2976 KeyStuffed = FALSE;
2977
2978 init_typebuf();
2979 start_stuff();
2980 if (advance && typebuf.tb_maplen == 0)
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02002981 reg_executing = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 do
2983 {
2984/*
2985 * get a character: 1. from the stuffbuffer
2986 */
2987 if (typeahead_char != 0)
2988 {
2989 c = typeahead_char;
2990 if (advance)
2991 typeahead_char = 0;
2992 }
2993 else
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01002994 c = read_readbuffers(advance);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 if (c != NUL && !got_int)
2996 {
2997 if (advance)
2998 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002999 // KeyTyped = FALSE; When the command that stuffed something
3000 // was typed, behave like the stuffed command was typed.
3001 // needed for CTRL-W CTRL-] to open a fold, for example.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 KeyStuffed = TRUE;
3003 }
3004 if (typebuf.tb_no_abbr_cnt == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003005 typebuf.tb_no_abbr_cnt = 1; // no abbreviations now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 }
3007 else
3008 {
3009 /*
3010 * Loop until we either find a matching mapped key, or we
3011 * are sure that it is not a mapped key.
3012 * If a mapped key sequence is found we go back to the start to
3013 * try re-mapping.
3014 */
3015 for (;;)
3016 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003017 long wait_time;
3018 int keylen = 0;
Bram Moolenaareda35f72019-08-03 14:59:44 +02003019#ifdef FEAT_CMDL_INFO
3020 int showcmd_idx;
3021#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 /*
3023 * ui_breakcheck() is slow, don't use it too often when
3024 * inside a mapping. But call it each time for typed
3025 * characters.
3026 */
3027 if (typebuf.tb_maplen)
3028 line_breakcheck();
3029 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003030 ui_breakcheck(); // check for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 if (got_int)
3032 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003033 // flush all input
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003034 c = inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 0L);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003035
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 /*
3037 * If inchar() returns TRUE (script file was active) or we
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003038 * are inside a mapping, get out of Insert mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 * Otherwise we behave like having gotten a CTRL-C.
3040 * As a result typing CTRL-C in insert mode will
3041 * really insert a CTRL-C.
3042 */
3043 if ((c || typebuf.tb_maplen)
3044 && (State & (INSERT + CMDLINE)))
3045 c = ESC;
3046 else
3047 c = Ctrl_C;
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02003048 flush_buffers(FLUSH_INPUT); // flush all typeahead
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049
Bram Moolenaard9dfd572006-10-03 13:36:13 +00003050 if (advance)
3051 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003052 // Also record this character, it might be needed to
3053 // get out of Insert mode.
Bram Moolenaard9dfd572006-10-03 13:36:13 +00003054 *typebuf.tb_buf = c;
3055 gotchars(typebuf.tb_buf, 1);
3056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 cmd_silent = FALSE;
3058
3059 break;
3060 }
3061 else if (typebuf.tb_len > 0)
3062 {
3063 /*
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003064 * Check for a mapping in "typebuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 */
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003066 map_result_T result = handle_mapping(
3067 &keylen, &timedout, &mapdepth);
3068
3069 if (result == map_result_retry)
3070 // try mapping again
3071 continue;
3072 if (result == map_result_fail)
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003073 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003074 // failed, use the outer loop
3075 c = -1;
3076 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003078 if (result == map_result_get)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080/*
3081 * get a character: 2. from the typeahead buffer
3082 */
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003083 c = typebuf.tb_buf[typebuf.tb_off];
Bram Moolenaar30613902019-12-01 22:11:18 +01003084 if (advance) // remove chars from tb_buf
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003085 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003086 cmd_silent = (typebuf.tb_silent > 0);
3087 if (typebuf.tb_maplen > 0)
3088 KeyTyped = FALSE;
3089 else
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003090 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003091 KeyTyped = TRUE;
Bram Moolenaar30613902019-12-01 22:11:18 +01003092 // write char to script file(s)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003093 gotchars(typebuf.tb_buf
3094 + typebuf.tb_off, 1);
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003095 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003096 KeyNoremap = typebuf.tb_noremap[
3097 typebuf.tb_off];
3098 del_typebuf(1, 0);
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003099 }
Bram Moolenaar52797ba2021-12-16 14:45:13 +00003100 break; // got character, break the for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 }
3102
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003103 // not enough characters, get more
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 }
3105
3106/*
3107 * get a character: 3. from the user - handle <Esc> in Insert mode
3108 */
3109 /*
Bram Moolenaarf085f422017-06-07 20:39:47 +02003110 * Special case: if we get an <ESC> in insert mode and there
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 * are no more characters at once, we pretend to go out of
3112 * insert mode. This prevents the one second delay after
3113 * typing an <ESC>. If we get something after all, we may
3114 * have to redisplay the mode. That the cursor is in the wrong
3115 * place does not matter.
3116 */
3117 c = 0;
3118#ifdef FEAT_CMDL_INFO
3119 new_wcol = curwin->w_wcol;
3120 new_wrow = curwin->w_wrow;
3121#endif
3122 if ( advance
3123 && typebuf.tb_len == 1
3124 && typebuf.tb_buf[typebuf.tb_off] == ESC
3125 && !no_mapping
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 && ex_normal_busy == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 && typebuf.tb_maplen == 0
3128 && (State & INSERT)
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003129 && (p_timeout
3130 || (keylen == KEYLEN_PART_KEY && p_ttimeout))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 && (c = inchar(typebuf.tb_buf + typebuf.tb_off
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003132 + typebuf.tb_len, 3, 25L)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 {
3134 colnr_T col = 0, vcol;
3135 char_u *ptr;
3136
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003137 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 {
3139 unshowmode(TRUE);
3140 mode_deleted = TRUE;
3141 }
3142#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003143 // may show a different cursor shape
Bram Moolenaarf085f422017-06-07 20:39:47 +02003144 if (gui.in_use && State != NORMAL && !cmd_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 {
3146 int save_State;
3147
3148 save_State = State;
3149 State = NORMAL;
3150 gui_update_cursor(TRUE, FALSE);
3151 State = save_State;
3152 shape_changed = TRUE;
3153 }
3154#endif
3155 validate_cursor();
3156 old_wcol = curwin->w_wcol;
3157 old_wrow = curwin->w_wrow;
3158
Bram Moolenaar30613902019-12-01 22:11:18 +01003159 // move cursor left, if possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 if (curwin->w_cursor.col != 0)
3161 {
3162 if (curwin->w_wcol > 0)
3163 {
3164 if (did_ai)
3165 {
3166 /*
3167 * We are expecting to truncate the trailing
3168 * white-space, so find the last non-white
3169 * character -- webb
3170 */
3171 col = vcol = curwin->w_wcol = 0;
3172 ptr = ml_get_curline();
3173 while (col < curwin->w_cursor.col)
3174 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01003175 if (!VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 curwin->w_wcol = vcol;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003177 vcol += lbr_chartabsize(ptr, ptr + col,
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01003178 vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003180 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 ++col;
3183 }
3184 curwin->w_wrow = curwin->w_cline_row
Bram Moolenaar02631462017-09-22 15:20:32 +02003185 + curwin->w_wcol / curwin->w_width;
3186 curwin->w_wcol %= curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 curwin->w_wcol += curwin_col_off();
Bram Moolenaar30613902019-12-01 22:11:18 +01003188 col = 0; // no correction needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 }
3190 else
3191 {
3192 --curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 col = curwin->w_cursor.col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 }
3195 }
3196 else if (curwin->w_p_wrap && curwin->w_wrow)
3197 {
3198 --curwin->w_wrow;
Bram Moolenaar02631462017-09-22 15:20:32 +02003199 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 col = curwin->w_cursor.col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 if (has_mbyte && col > 0 && curwin->w_wcol > 0)
3203 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003204 // Correct when the cursor is on the right halve
3205 // of a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 ptr = ml_get_curline();
3207 col -= (*mb_head_off)(ptr, ptr + col);
3208 if ((*mb_ptr2cells)(ptr + col) > 1)
3209 --curwin->w_wcol;
3210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 }
3212 setcursor();
3213 out_flush();
3214#ifdef FEAT_CMDL_INFO
3215 new_wcol = curwin->w_wcol;
3216 new_wrow = curwin->w_wrow;
3217#endif
3218 curwin->w_wcol = old_wcol;
3219 curwin->w_wrow = old_wrow;
3220 }
3221 if (c < 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003222 continue; // end of input script reached
Bram Moolenaar20c38922014-07-23 20:41:14 +02003223
Bram Moolenaar30613902019-12-01 22:11:18 +01003224 // Allow mapping for just typed characters. When we get here c
3225 // is the number of extra bytes and typebuf.tb_len is 1.
Bram Moolenaar20c38922014-07-23 20:41:14 +02003226 for (n = 1; n <= c; ++n)
3227 typebuf.tb_noremap[typebuf.tb_off + n] = RM_YES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 typebuf.tb_len += c;
3229
Bram Moolenaar30613902019-12-01 22:11:18 +01003230 // buffer full, don't map
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 if (typebuf.tb_len >= typebuf.tb_maplen + MAXMAPLEN)
3232 {
3233 timedout = TRUE;
3234 continue;
3235 }
3236
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 if (ex_normal_busy > 0)
3238 {
Bram Moolenaare2c38102016-01-31 14:55:40 +01003239#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 static int tc = 0;
Bram Moolenaare2c38102016-01-31 14:55:40 +01003241#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242
Bram Moolenaar30613902019-12-01 22:11:18 +01003243 // No typeahead left and inside ":normal". Must return
3244 // something to avoid getting stuck. When an incomplete
3245 // mapping is present, behave like it timed out.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 if (typebuf.tb_len > 0)
3247 {
3248 timedout = TRUE;
3249 continue;
3250 }
Bram Moolenaar189832b2020-09-23 12:29:11 +02003251
Bram Moolenaar30613902019-12-01 22:11:18 +01003252 // When 'insertmode' is set, ESC just beeps in Insert
3253 // mode. Use CTRL-L to make edit() return.
3254 // For the command line only CTRL-C always breaks it.
3255 // For the cmdline window: Alternate between ESC and
3256 // CTRL-C: ESC for most situations and CTRL-C to close the
3257 // cmdline window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 if (p_im && (State & INSERT))
3259 c = Ctrl_L;
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003260#ifdef FEAT_TERMINAL
3261 else if (terminal_is_active())
3262 c = K_CANCEL;
3263#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 else if ((State & CMDLINE)
Bram Moolenaare2c38102016-01-31 14:55:40 +01003265#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 || (cmdwin_type > 0 && tc == ESC)
Bram Moolenaare2c38102016-01-31 14:55:40 +01003267#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 )
3269 c = Ctrl_C;
3270 else
3271 c = ESC;
Bram Moolenaare2c38102016-01-31 14:55:40 +01003272#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 tc = c;
Bram Moolenaare2c38102016-01-31 14:55:40 +01003274#endif
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01003275 // return from main_loop()
3276 if (pending_exmode_active)
3277 exmode_active = EXMODE_NORMAL;
3278
Bram Moolenaarb37a65e2022-01-01 12:42:56 +00003279 // no chars to block abbreviation for
3280 typebuf.tb_no_abbr_cnt = 0;
3281
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 break;
3283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284
3285/*
3286 * get a character: 3. from the user - update display
3287 */
Bram Moolenaar30613902019-12-01 22:11:18 +01003288 // In insert mode a screen update is skipped when characters
3289 // are still available. But when those available characters
3290 // are part of a mapping, and we are going to do a blocking
3291 // wait here. Need to update the screen to display the
3292 // changed text so far. Also for when 'lazyredraw' is set and
3293 // redrawing was postponed because there was something in the
3294 // input buffer (e.g., termresponse).
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01003295 if (((State & INSERT) != 0 || p_lz) && (State & CMDLINE) == 0
3296 && advance && must_redraw != 0 && !need_wait_return)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 {
3298 update_screen(0);
Bram Moolenaar30613902019-12-01 22:11:18 +01003299 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 }
3301
3302 /*
3303 * If we have a partial match (and are going to wait for more
3304 * input from the user), show the partially matched characters
3305 * to the user with showcmd.
3306 */
3307#ifdef FEAT_CMDL_INFO
Bram Moolenaareda35f72019-08-03 14:59:44 +02003308 showcmd_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309#endif
3310 c1 = 0;
3311 if (typebuf.tb_len > 0 && advance && !exmode_active)
3312 {
3313 if (((State & (NORMAL | INSERT)) || State == LANGMAP)
3314 && State != HITRETURN)
3315 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003316 // this looks nice when typing a dead character map
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 if (State & INSERT
3318 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
3319 + typebuf.tb_len - 1) == 1)
3320 {
3321 edit_putchar(typebuf.tb_buf[typebuf.tb_off
3322 + typebuf.tb_len - 1], FALSE);
Bram Moolenaar30613902019-12-01 22:11:18 +01003323 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 c1 = 1;
3325 }
3326#ifdef FEAT_CMDL_INFO
Bram Moolenaar30613902019-12-01 22:11:18 +01003327 // need to use the col and row from above here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 old_wcol = curwin->w_wcol;
3329 old_wrow = curwin->w_wrow;
3330 curwin->w_wcol = new_wcol;
3331 curwin->w_wrow = new_wrow;
3332 push_showcmd();
3333 if (typebuf.tb_len > SHOWCMD_COLS)
Bram Moolenaareda35f72019-08-03 14:59:44 +02003334 showcmd_idx = typebuf.tb_len - SHOWCMD_COLS;
3335 while (showcmd_idx < typebuf.tb_len)
3336 (void)add_to_showcmd(
3337 typebuf.tb_buf[typebuf.tb_off + showcmd_idx++]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 curwin->w_wcol = old_wcol;
3339 curwin->w_wrow = old_wrow;
3340#endif
3341 }
3342
Bram Moolenaar30613902019-12-01 22:11:18 +01003343 // this looks nice when typing a dead character map
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 if ((State & CMDLINE)
3345#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3346 && cmdline_star == 0
3347#endif
3348 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
3349 + typebuf.tb_len - 1) == 1)
3350 {
3351 putcmdline(typebuf.tb_buf[typebuf.tb_off
3352 + typebuf.tb_len - 1], FALSE);
3353 c1 = 1;
3354 }
3355 }
3356
3357/*
3358 * get a character: 3. from the user - get it
3359 */
Bram Moolenaar83f4cbd2018-06-12 21:35:40 +02003360 if (typebuf.tb_len == 0)
3361 // timedout may have been set while waiting for a mapping
3362 // that has a <Nop> RHS.
3363 timedout = FALSE;
3364
Bram Moolenaar652de232019-04-04 20:13:09 +02003365 if (advance)
3366 {
3367 if (typebuf.tb_len == 0
3368 || !(p_timeout
3369 || (p_ttimeout && keylen == KEYLEN_PART_KEY)))
3370 // blocking wait
3371 wait_time = -1L;
3372 else if (keylen == KEYLEN_PART_KEY && p_ttm >= 0)
3373 wait_time = p_ttm;
3374 else
3375 wait_time = p_tm;
3376 }
3377 else
3378 wait_time = 0;
3379
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003380 wait_tb_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 c = inchar(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len,
3382 typebuf.tb_buflen - typebuf.tb_off - typebuf.tb_len - 1,
Bram Moolenaar652de232019-04-04 20:13:09 +02003383 wait_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384
3385#ifdef FEAT_CMDL_INFO
Bram Moolenaareda35f72019-08-03 14:59:44 +02003386 if (showcmd_idx != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 pop_showcmd();
3388#endif
3389 if (c1 == 1)
3390 {
3391 if (State & INSERT)
3392 edit_unputchar();
3393 if (State & CMDLINE)
3394 unputcmdline();
Bram Moolenaarbc256d92012-06-06 12:06:15 +02003395 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003396 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 }
3398
3399 if (c < 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003400 continue; // end of input script reached
3401 if (c == NUL) // no character available
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 {
3403 if (!advance)
3404 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01003405 if (wait_tb_len > 0) // timed out
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 {
3407 timedout = TRUE;
3408 continue;
3409 }
3410 }
3411 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003412 { // allow mapping for just typed characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 while (typebuf.tb_buf[typebuf.tb_off
3414 + typebuf.tb_len] != NUL)
3415 typebuf.tb_noremap[typebuf.tb_off
3416 + typebuf.tb_len++] = RM_YES;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003417#ifdef HAVE_INPUT_METHOD
Bram Moolenaar30613902019-12-01 22:11:18 +01003418 // Get IM status right after getting keys, not after the
3419 // timeout for a mapping (focus may be lost by then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 vgetc_im_active = im_get_status();
3421#endif
3422 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003423 } // for (;;)
3424 } // if (!character from stuffbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425
Bram Moolenaar30613902019-12-01 22:11:18 +01003426 // if advance is FALSE don't loop on NULs
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003427 } while ((c < 0 && c != K_CANCEL) || (advance && c == NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428
3429 /*
3430 * The "INSERT" message is taken care of here:
3431 * if we return an ESC to exit insert mode, the message is deleted
3432 * if we don't return an ESC but deleted the message before, redisplay it
3433 */
Bram Moolenaar09df3122006-01-23 22:23:09 +00003434 if (advance && p_smd && msg_silent == 0 && (State & INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003436 if (c == ESC && !mode_deleted && !no_mapping && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 {
3438 if (typebuf.tb_len && !KeyTyped)
Bram Moolenaar30613902019-12-01 22:11:18 +01003439 redraw_cmdline = TRUE; // delete mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 else
3441 unshowmode(FALSE);
3442 }
3443 else if (c != ESC && mode_deleted)
3444 {
3445 if (typebuf.tb_len && !KeyTyped)
Bram Moolenaar30613902019-12-01 22:11:18 +01003446 redraw_cmdline = TRUE; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 else
3448 showmode();
3449 }
3450 }
3451#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003452 // may unshow different cursor shape
Bram Moolenaarf085f422017-06-07 20:39:47 +02003453 if (gui.in_use && shape_changed)
3454 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455#endif
Bram Moolenaar6edbbd82019-03-10 09:41:51 +01003456 if (timedout && c == ESC)
3457 {
3458 char_u nop_buf[3];
3459
3460 // When recording there will be no timeout. Add a <Nop> after the ESC
3461 // to avoid that it forms a key code with following characters.
3462 nop_buf[0] = K_SPECIAL;
3463 nop_buf[1] = KS_EXTRA;
3464 nop_buf[2] = KE_NOP;
3465 gotchars(nop_buf, 3);
3466 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003468 --vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469
3470 return c;
3471}
3472
3473/*
3474 * inchar() - get one character from
3475 * 1. a scriptfile
3476 * 2. the keyboard
3477 *
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003478 * As many characters as we can get (up to 'maxlen') are put in "buf" and
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 * NUL terminated (buffer length must be 'maxlen' + 1).
3480 * Minimum for "maxlen" is 3!!!!
3481 *
3482 * "tb_change_cnt" is the value of typebuf.tb_change_cnt if "buf" points into
3483 * it. When typebuf.tb_change_cnt changes (e.g., when a message is received
3484 * from a remote client) "buf" can no longer be used. "tb_change_cnt" is 0
3485 * otherwise.
3486 *
3487 * If we got an interrupt all input is read until none is available.
3488 *
3489 * If wait_time == 0 there is no waiting for the char.
3490 * If wait_time == n we wait for n msec for a character to arrive.
3491 * If wait_time == -1 we wait forever for a character to arrive.
3492 *
3493 * Return the number of obtained characters.
3494 * Return -1 when end of input script reached.
3495 */
Bram Moolenaarcda77642016-06-04 13:32:35 +02003496 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003497inchar(
3498 char_u *buf,
3499 int maxlen,
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003500 long wait_time) // milliseconds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501{
Bram Moolenaar30613902019-12-01 22:11:18 +01003502 int len = 0; // init for GCC
3503 int retesc = FALSE; // return ESC with gotint
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 int script_char;
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003505 int tb_change_cnt = typebuf.tb_change_cnt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506
Bram Moolenaar30613902019-12-01 22:11:18 +01003507 if (wait_time == -1L || wait_time > 100L) // flush output before waiting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 {
3509 cursor_on();
Bram Moolenaara338adc2018-01-31 20:51:47 +01003510 out_flush_cursor(FALSE, FALSE);
3511#if defined(FEAT_GUI) && defined(FEAT_MOUSESHAPE)
3512 if (gui.in_use && postponed_mouseshape)
3513 update_mouseshape(-1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514#endif
3515 }
3516
3517 /*
3518 * Don't reset these when at the hit-return prompt, otherwise a endless
3519 * recursive loop may result (write error in swapfile, hit-return, timeout
3520 * on char wait, flush swapfile, write error....).
3521 */
3522 if (State != HITRETURN)
3523 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003524 did_outofmem_msg = FALSE; // display out of memory message (again)
3525 did_swapwrite_msg = FALSE; // display swap file write error again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003527 undo_off = FALSE; // restart undo now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528
3529 /*
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00003530 * Get a character from a script file if there is one.
3531 * If interrupted: Stop reading script files, close them all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 */
3533 script_char = -1;
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00003534 while (scriptin[curscript] != NULL && script_char < 0
3535#ifdef FEAT_EVAL
3536 && !ignore_script
3537#endif
3538 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02003540#ifdef MESSAGE_QUEUE
3541 parse_queued_messages();
Bram Moolenaarf2330482008-06-24 20:19:36 +00003542#endif
3543
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 if (got_int || (script_char = getc(scriptin[curscript])) < 0)
3545 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003546 // Reached EOF.
3547 // Careful: closescript() frees typebuf.tb_buf[] and buf[] may
3548 // point inside typebuf.tb_buf[]. Don't use buf[] after this!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 closescript();
3550 /*
3551 * When reading script file is interrupted, return an ESC to get
3552 * back to normal mode.
3553 * Otherwise return -1, because typebuf.tb_buf[] has changed.
3554 */
3555 if (got_int)
3556 retesc = TRUE;
3557 else
3558 return -1;
3559 }
3560 else
3561 {
3562 buf[0] = script_char;
3563 len = 1;
3564 }
3565 }
3566
Bram Moolenaar30613902019-12-01 22:11:18 +01003567 if (script_char < 0) // did not get a character from script
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 {
3569 /*
3570 * If we got an interrupt, skip all previously typed characters and
3571 * return TRUE if quit reading script file.
3572 * Stop reading typeahead when a single CTRL-C was read,
3573 * fill_input_buf() returns this when not able to read from stdin.
3574 * Don't use buf[] here, closescript() may have freed typebuf.tb_buf[]
3575 * and buf may be pointing inside typebuf.tb_buf[].
3576 */
3577 if (got_int)
3578 {
3579#define DUM_LEN MAXMAPLEN * 3 + 3
3580 char_u dum[DUM_LEN + 1];
3581
3582 for (;;)
3583 {
3584 len = ui_inchar(dum, DUM_LEN, 0L, 0);
3585 if (len == 0 || (len == 1 && dum[0] == 3))
3586 break;
3587 }
3588 return retesc;
3589 }
3590
3591 /*
3592 * Always flush the output characters when getting input characters
Bram Moolenaarcb574f42019-01-25 22:29:57 +01003593 * from the user and not just peeking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 */
Bram Moolenaarcb574f42019-01-25 22:29:57 +01003595 if (wait_time == -1L || wait_time > 10L)
3596 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597
3598 /*
3599 * Fill up to a third of the buffer, because each character may be
3600 * tripled below.
3601 */
3602 len = ui_inchar(buf, maxlen / 3, wait_time, tb_change_cnt);
3603 }
3604
Bram Moolenaar30613902019-12-01 22:11:18 +01003605 // If the typebuf was changed further down, it is like nothing was added by
3606 // this call.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 if (typebuf_changed(tb_change_cnt))
3608 return 0;
3609
Bram Moolenaar30613902019-12-01 22:11:18 +01003610 // Note the change in the typeahead buffer, this matters for when
3611 // vgetorpeek() is called recursively, e.g. using getchar(1) in a timer
3612 // function.
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003613 if (len > 0 && ++typebuf.tb_change_cnt == 0)
3614 typebuf.tb_change_cnt = 1;
3615
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003616 return fix_input_buffer(buf, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617}
3618
3619/*
3620 * Fix typed characters for use by vgetc() and check_termcode().
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003621 * "buf[]" must have room to triple the number of bytes!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 * Returns the new length.
3623 */
3624 int
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003625fix_input_buffer(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626{
3627 int i;
3628 char_u *p = buf;
3629
3630 /*
3631 * Two characters are special: NUL and K_SPECIAL.
3632 * When compiled With the GUI CSI is also special.
3633 * Replace NUL by K_SPECIAL KS_ZERO KE_FILLER
3634 * Replace K_SPECIAL by K_SPECIAL KS_SPECIAL KE_FILLER
3635 * Replace CSI by K_SPECIAL KS_EXTRA KE_CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 */
3637 for (i = len; --i >= 0; ++p)
3638 {
3639#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003640 // When the GUI is used any character can come after a CSI, don't
3641 // escape it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 if (gui.in_use && p[0] == CSI && i >= 2)
3643 {
3644 p += 2;
3645 i -= 2;
3646 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003647# ifndef MSWIN
Bram Moolenaar30613902019-12-01 22:11:18 +01003648 // When the GUI is not used CSI needs to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 else if (!gui.in_use && p[0] == CSI)
3650 {
3651 mch_memmove(p + 3, p + 1, (size_t)i);
3652 *p++ = K_SPECIAL;
3653 *p++ = KS_EXTRA;
3654 *p = (int)KE_CSI;
3655 len += 2;
3656 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003657# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 else
3659#endif
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003660 if (p[0] == NUL || (p[0] == K_SPECIAL
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003661 // timeout may generate K_CURSORHOLD
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003662 && (i < 2 || p[1] != KS_EXTRA || p[2] != (int)KE_CURSORHOLD)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003663#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003664 // Win32 console passes modifiers
3665 && (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003666# ifdef VIMDLL
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003667 gui.in_use ||
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003668# endif
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003669 (i < 2 || p[1] != KS_MODIFIER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670#endif
3671 ))
3672 {
3673 mch_memmove(p + 3, p + 1, (size_t)i);
3674 p[2] = K_THIRD(p[0]);
3675 p[1] = K_SECOND(p[0]);
3676 p[0] = K_SPECIAL;
3677 p += 2;
3678 len += 2;
3679 }
3680 }
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003681 *p = NUL; // add trailing NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 return len;
3683}
3684
3685#if defined(USE_INPUT_BUF) || defined(PROTO)
3686/*
3687 * Return TRUE when bytes are in the input buffer or in the typeahead buffer.
3688 * Normally the input buffer would be sufficient, but the server_to_input_buf()
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003689 * or feedkeys() may insert characters in the typeahead buffer while we are
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003690 * waiting for input to arrive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 */
3692 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003693input_available(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694{
3695 return (!vim_is_input_buf_empty()
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003696# if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
3697 || typebuf_was_filled
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698# endif
3699 );
3700}
3701#endif
Bram Moolenaar957cf672020-11-12 14:21:06 +01003702
3703/*
3704 * Function passed to do_cmdline() to get the command after a <Cmd> key from
3705 * typeahead.
3706 */
3707 char_u *
3708getcmdkeycmd(
3709 int promptc UNUSED,
3710 void *cookie UNUSED,
3711 int indent UNUSED,
3712 getline_opt_T do_concat UNUSED)
3713{
3714 garray_T line_ga;
3715 int c1 = -1;
3716 int c2;
3717 int cmod = 0;
3718 int aborted = FALSE;
3719
3720 ga_init2(&line_ga, 1, 32);
3721
3722 // no mapping for these characters
3723 no_mapping++;
3724
3725 got_int = FALSE;
3726 while (c1 != NUL && !aborted)
3727 {
Bram Moolenaarca359cb2020-11-15 20:49:41 +01003728 if (ga_grow(&line_ga, 32) != OK)
3729 {
3730 aborted = TRUE;
3731 break;
3732 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003733
3734 if (vgetorpeek(FALSE) == NUL)
3735 {
3736 // incomplete <Cmd> is an error, because there is not much the user
3737 // could do in this state.
3738 emsg(_(e_cmd_mapping_must_end_with_cr));
3739 aborted = TRUE;
3740 break;
3741 }
3742
3743 // Get one character at a time.
3744 c1 = vgetorpeek(TRUE);
3745
3746 // Get two extra bytes for special keys
3747 if (c1 == K_SPECIAL)
3748 {
3749 c1 = vgetorpeek(TRUE);
3750 c2 = vgetorpeek(TRUE);
3751 if (c1 == KS_MODIFIER)
3752 {
3753 cmod = c2;
3754 continue;
3755 }
3756 c1 = TO_SPECIAL(c1, c2);
3757 }
Bram Moolenaar4a441202020-11-28 14:43:26 +01003758 if (c1 == Ctrl_V)
3759 {
3760 // CTRL-V is followed by octal, hex or other characters, reverses
3761 // what AppendToRedobuffLit() does.
3762 no_reduce_keys = TRUE; // don't merge modifyOtherKeys
Bram Moolenaar0684e362020-12-03 19:54:42 +01003763 c1 = get_literal(TRUE);
Bram Moolenaar4a441202020-11-28 14:43:26 +01003764 no_reduce_keys = FALSE;
3765 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003766
3767 if (got_int)
3768 aborted = TRUE;
3769 else if (c1 == '\r' || c1 == '\n')
3770 c1 = NUL; // end the line
3771 else if (c1 == ESC)
3772 aborted = TRUE;
3773 else if (c1 == K_COMMAND)
3774 {
3775 // give a nicer error message for this special case
3776 emsg(_(e_cmd_mapping_must_end_with_cr_before_second_cmd));
3777 aborted = TRUE;
3778 }
3779 else if (IS_SPECIAL(c1))
3780 {
3781 if (c1 == K_SNR)
Bram Moolenaarc77534c2020-11-18 11:34:37 +01003782 ga_concat(&line_ga, (char_u *)"<SNR>");
Bram Moolenaar957cf672020-11-12 14:21:06 +01003783 else
3784 {
3785 semsg(e_cmd_maping_must_not_include_str_key,
3786 get_special_key_name(c1, cmod));
3787 aborted = TRUE;
3788 }
3789 }
3790 else
3791 ga_append(&line_ga, (char)c1);
3792
3793 cmod = 0;
3794 }
3795
3796 no_mapping--;
3797
3798 if (aborted)
3799 ga_clear(&line_ga);
3800
3801 return (char_u *)line_ga.ga_data;
3802}