blob: ff71952316d45975e7cdb2dd6a5c6b5c2bc57105 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
zeertzjqa3f83fe2021-11-22 12:47:39 +000011 * getchar.c: Code related to getting a character from the user or a script
12 * file, manipulations with redo buffer and stuff buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013 */
14
15#include "vim.h"
16
17/*
18 * These buffers are used for storing:
19 * - stuffed characters: A command that is translated into another command.
20 * - redo characters: will redo the last change.
Bram Moolenaarf6f95d92009-11-11 15:23:37 +000021 * - recorded characters: for the "q" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022 *
23 * The bytes are stored like in the typeahead buffer:
24 * - K_SPECIAL introduces a special key (two more bytes follow). A literal
25 * K_SPECIAL is stored as K_SPECIAL KS_SPECIAL KE_FILLER.
26 * - CSI introduces a GUI termcap code (also when gui.in_use is FALSE,
27 * otherwise switching the GUI on would make mappings invalid).
28 * A literal CSI is stored as CSI KS_EXTRA KE_CSI.
29 * These translations are also done on multi-byte characters!
30 *
31 * Escaping CSI bytes is done by the system-specific input functions, called
32 * by ui_inchar().
33 * Escaping K_SPECIAL is done by inchar().
34 * Un-escaping is done by vgetc().
35 */
36
Bram Moolenaar30613902019-12-01 22:11:18 +010037#define MINIMAL_SIZE 20 // minimal size for b_str
Bram Moolenaar071d4272004-06-13 20:20:40 +000038
Bram Moolenaar285e3352018-04-18 23:01:13 +020039static buffheader_T redobuff = {{NULL, {NUL}}, NULL, 0, 0};
40static buffheader_T old_redobuff = {{NULL, {NUL}}, NULL, 0, 0};
41static buffheader_T recordbuff = {{NULL, {NUL}}, NULL, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +000042
Bram Moolenaar30613902019-12-01 22:11:18 +010043static int typeahead_char = 0; // typeahead char that's not flushed
Bram Moolenaar071d4272004-06-13 20:20:40 +000044
45/*
46 * when block_redo is TRUE redo buffer will not be changed
47 * used by edit() to repeat insertions and 'V' command for redoing
48 */
49static int block_redo = FALSE;
50
Bram Moolenaar459fd782019-10-13 16:43:39 +020051static int KeyNoremap = 0; // remapping flags
Bram Moolenaar071d4272004-06-13 20:20:40 +000052
53/*
Bram Moolenaarfd89d7e2016-06-04 20:25:05 +020054 * Variables used by vgetorpeek() and flush_buffers().
Bram Moolenaar071d4272004-06-13 20:20:40 +000055 *
56 * typebuf.tb_buf[] contains all characters that are not consumed yet.
57 * typebuf.tb_buf[typebuf.tb_off] is the first valid character.
58 * typebuf.tb_buf[typebuf.tb_off + typebuf.tb_len - 1] is the last valid char.
59 * typebuf.tb_buf[typebuf.tb_off + typebuf.tb_len] must be NUL.
60 * The head of the buffer may contain the result of mappings, abbreviations
61 * and @a commands. The length of this part is typebuf.tb_maplen.
62 * typebuf.tb_silent is the part where <silent> applies.
63 * After the head are characters that come from the terminal.
64 * typebuf.tb_no_abbr_cnt is the number of characters in typebuf.tb_buf that
65 * should not be considered for abbreviations.
66 * Some parts of typebuf.tb_buf may not be mapped. These parts are remembered
67 * in typebuf.tb_noremap[], which is the same length as typebuf.tb_buf and
68 * contains RM_NONE for the characters that are not to be remapped.
69 * typebuf.tb_noremap[typebuf.tb_off] is the first valid flag.
70 * (typebuf has been put in globals.h, because check_termcode() needs it).
71 */
Bram Moolenaar30613902019-12-01 22:11:18 +010072#define RM_YES 0 // tb_noremap: remap
73#define RM_NONE 1 // tb_noremap: don't remap
74#define RM_SCRIPT 2 // tb_noremap: remap local script mappings
75#define RM_ABBR 4 // tb_noremap: don't remap, do abbrev.
Bram Moolenaar071d4272004-06-13 20:20:40 +000076
Bram Moolenaar30613902019-12-01 22:11:18 +010077// typebuf.tb_buf has three parts: room in front (for result of mappings), the
78// middle for typeahead and room for new characters (which needs to be 3 *
Dominique Pelleaf4a61a2021-12-27 17:21:41 +000079// MAXMAPLEN for the Amiga).
Bram Moolenaar071d4272004-06-13 20:20:40 +000080#define TYPELEN_INIT (5 * (MAXMAPLEN + 3))
Bram Moolenaar30613902019-12-01 22:11:18 +010081static char_u typebuf_init[TYPELEN_INIT]; // initial typebuf.tb_buf
82static char_u noremapbuf_init[TYPELEN_INIT]; // initial typebuf.tb_noremap
Bram Moolenaar071d4272004-06-13 20:20:40 +000083
Bram Moolenaar30613902019-12-01 22:11:18 +010084static int last_recorded_len = 0; // number of last recorded chars
Bram Moolenaar071d4272004-06-13 20:20:40 +000085
Bram Moolenaare32c3c42022-01-15 18:26:04 +000086#ifdef FEAT_EVAL
87mapblock_T *last_used_map = NULL;
88#endif
89
Bram Moolenaard25c16e2016-01-29 22:13:30 +010090static int read_readbuf(buffheader_T *buf, int advance);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010091static void init_typebuf(void);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010092static void may_sync_undo(void);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020093static void free_typebuf(void);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010094static void closescript(void);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020095static void updatescript(int c);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010096static int vgetorpeek(int);
Bram Moolenaar0f0f2302017-08-30 18:52:56 +020097static int inchar(char_u *buf, int maxlen, long wait_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +000098
99/*
100 * Free and clear a buffer.
101 */
Bram Moolenaarbdff0122020-04-05 18:56:05 +0200102 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100103free_buff(buffheader_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100105 buffblock_T *p, *np;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106
Bram Moolenaar285e3352018-04-18 23:01:13 +0200107 for (p = buf->bh_first.b_next; p != NULL; p = np)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108 {
109 np = p->b_next;
110 vim_free(p);
111 }
Bram Moolenaar285e3352018-04-18 23:01:13 +0200112 buf->bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113}
114
115/*
116 * Return the contents of a buffer as a single string.
117 * K_SPECIAL and CSI in the returned string are escaped.
118 */
119 static char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100120get_buffcont(
121 buffheader_T *buffer,
Bram Moolenaar30613902019-12-01 22:11:18 +0100122 int dozero) // count == zero is not an error
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123{
124 long_u count = 0;
125 char_u *p = NULL;
126 char_u *p2;
127 char_u *str;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200128 buffblock_T *bp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129
Bram Moolenaar30613902019-12-01 22:11:18 +0100130 // compute the total length of the string
Bram Moolenaar285e3352018-04-18 23:01:13 +0200131 for (bp = buffer->bh_first.b_next; bp != NULL; bp = bp->b_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132 count += (long_u)STRLEN(bp->b_str);
133
Bram Moolenaar18a4ba22019-05-24 19:39:03 +0200134 if ((count || dozero) && (p = alloc(count + 1)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135 {
136 p2 = p;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200137 for (bp = buffer->bh_first.b_next; bp != NULL; bp = bp->b_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138 for (str = bp->b_str; *str; )
139 *p2++ = *str++;
140 *p2 = NUL;
141 }
142 return (p);
143}
144
145/*
146 * Return the contents of the record buffer as a single string
147 * and clear the record buffer.
148 * K_SPECIAL and CSI in the returned string are escaped.
149 */
150 char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100151get_recorded(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152{
153 char_u *p;
154 size_t len;
155
156 p = get_buffcont(&recordbuff, TRUE);
157 free_buff(&recordbuff);
158
159 /*
160 * Remove the characters that were added the last time, these must be the
161 * (possibly mapped) characters that stopped the recording.
162 */
163 len = STRLEN(p);
164 if ((int)len >= last_recorded_len)
165 {
166 len -= last_recorded_len;
167 p[len] = NUL;
168 }
169
170 /*
171 * When stopping recording from Insert mode with CTRL-O q, also remove the
172 * CTRL-O.
173 */
174 if (len > 0 && restart_edit != 0 && p[len - 1] == Ctrl_O)
175 p[len - 1] = NUL;
176
177 return (p);
178}
179
180/*
181 * Return the contents of the redo buffer as a single string.
182 * K_SPECIAL and CSI in the returned string are escaped.
183 */
184 char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100185get_inserted(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186{
Bram Moolenaarf2330482008-06-24 20:19:36 +0000187 return get_buffcont(&redobuff, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188}
189
190/*
Bram Moolenaar4e86cba2007-05-06 11:56:32 +0000191 * Add string "s" after the current block of buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 * K_SPECIAL and CSI should have been escaped already.
193 */
194 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100195add_buff(
196 buffheader_T *buf,
197 char_u *s,
Bram Moolenaar30613902019-12-01 22:11:18 +0100198 long slen) // length of "s" or -1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100200 buffblock_T *p;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200201 long_u len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202
203 if (slen < 0)
204 slen = (long)STRLEN(s);
Bram Moolenaar30613902019-12-01 22:11:18 +0100205 if (slen == 0) // don't add empty strings
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 return;
207
Bram Moolenaar30613902019-12-01 22:11:18 +0100208 if (buf->bh_first.b_next == NULL) // first add to list
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 {
210 buf->bh_space = 0;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200211 buf->bh_curr = &(buf->bh_first);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212 }
Bram Moolenaar30613902019-12-01 22:11:18 +0100213 else if (buf->bh_curr == NULL) // buffer has already been read
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214 {
Bram Moolenaar6d057012021-12-31 18:49:43 +0000215 iemsg(_(e_add_to_internal_buffer_that_was_already_read_from));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216 return;
217 }
218 else if (buf->bh_index != 0)
Bram Moolenaar285e3352018-04-18 23:01:13 +0200219 mch_memmove(buf->bh_first.b_next->b_str,
220 buf->bh_first.b_next->b_str + buf->bh_index,
221 STRLEN(buf->bh_first.b_next->b_str + buf->bh_index) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222 buf->bh_index = 0;
223
224 if (buf->bh_space >= (int)slen)
225 {
226 len = (long_u)STRLEN(buf->bh_curr->b_str);
Bram Moolenaarb6356332005-07-18 21:40:44 +0000227 vim_strncpy(buf->bh_curr->b_str + len, s, (size_t)slen);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228 buf->bh_space -= slen;
229 }
230 else
231 {
232 if (slen < MINIMAL_SIZE)
233 len = MINIMAL_SIZE;
234 else
235 len = slen;
Bram Moolenaar47ed5532019-08-08 20:49:14 +0200236 p = alloc(offsetof(buffblock_T, b_str) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 if (p == NULL)
Bram Moolenaar30613902019-12-01 22:11:18 +0100238 return; // no space, just forget it
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000239 buf->bh_space = (int)(len - slen);
Bram Moolenaarb6356332005-07-18 21:40:44 +0000240 vim_strncpy(p->b_str, s, (size_t)slen);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241
Bram Moolenaar285e3352018-04-18 23:01:13 +0200242 p->b_next = buf->bh_curr->b_next;
243 buf->bh_curr->b_next = p;
244 buf->bh_curr = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246}
247
248/*
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000249 * Delete "slen" bytes from the end of "buf".
250 * Only works when it was just added.
251 */
252 static void
253delete_buff_tail(buffheader_T *buf, int slen)
254{
255 int len = (int)STRLEN(buf->bh_curr->b_str);
256
257 if (len >= slen)
258 {
259 buf->bh_curr->b_str[len - slen] = NUL;
260 buf->bh_space += slen;
261 }
262}
263
264/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265 * Add number "n" to buffer "buf".
266 */
267 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100268add_num_buff(buffheader_T *buf, long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269{
270 char_u number[32];
271
272 sprintf((char *)number, "%ld", n);
273 add_buff(buf, number, -1L);
274}
275
276/*
277 * Add character 'c' to buffer "buf".
278 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
279 */
280 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100281add_char_buff(buffheader_T *buf, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283 char_u bytes[MB_MAXBYTES + 1];
284 int len;
285 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286 char_u temp[4];
287
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288 if (IS_SPECIAL(c))
289 len = 1;
290 else
291 len = (*mb_char2bytes)(c, bytes);
292 for (i = 0; i < len; ++i)
293 {
294 if (!IS_SPECIAL(c))
295 c = bytes[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296
297 if (IS_SPECIAL(c) || c == K_SPECIAL || c == NUL)
298 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100299 // translate special key code into three byte sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +0000300 temp[0] = K_SPECIAL;
301 temp[1] = K_SECOND(c);
302 temp[2] = K_THIRD(c);
303 temp[3] = NUL;
304 }
305#ifdef FEAT_GUI
306 else if (c == CSI)
307 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100308 // Translate a CSI to a CSI - KS_EXTRA - KE_CSI sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309 temp[0] = CSI;
310 temp[1] = KS_EXTRA;
311 temp[2] = (int)KE_CSI;
312 temp[3] = NUL;
313 }
314#endif
315 else
316 {
317 temp[0] = c;
318 temp[1] = NUL;
319 }
320 add_buff(buf, temp, -1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322}
323
Bram Moolenaar30613902019-12-01 22:11:18 +0100324// First read ahead buffer. Used for translated commands.
Bram Moolenaar285e3352018-04-18 23:01:13 +0200325static buffheader_T readbuf1 = {{NULL, {NUL}}, NULL, 0, 0};
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100326
Bram Moolenaar30613902019-12-01 22:11:18 +0100327// Second read ahead buffer. Used for redo.
Bram Moolenaar285e3352018-04-18 23:01:13 +0200328static buffheader_T readbuf2 = {{NULL, {NUL}}, NULL, 0, 0};
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100329
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330/*
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100331 * Get one byte from the read buffers. Use readbuf1 one first, use readbuf2
332 * if that one is empty.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333 * If advance == TRUE go to the next char.
334 * No translation is done K_SPECIAL and CSI are escaped.
335 */
336 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100337read_readbuffers(int advance)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100339 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100341 c = read_readbuf(&readbuf1, advance);
342 if (c == NUL)
343 c = read_readbuf(&readbuf2, advance);
344 return c;
345}
346
347 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100348read_readbuf(buffheader_T *buf, int advance)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100349{
350 char_u c;
351 buffblock_T *curr;
352
Bram Moolenaar30613902019-12-01 22:11:18 +0100353 if (buf->bh_first.b_next == NULL) // buffer is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354 return NUL;
355
Bram Moolenaar285e3352018-04-18 23:01:13 +0200356 curr = buf->bh_first.b_next;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100357 c = curr->b_str[buf->bh_index];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358
359 if (advance)
360 {
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100361 if (curr->b_str[++buf->bh_index] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362 {
Bram Moolenaar285e3352018-04-18 23:01:13 +0200363 buf->bh_first.b_next = curr->b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364 vim_free(curr);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100365 buf->bh_index = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366 }
367 }
368 return c;
369}
370
371/*
Bram Moolenaar06811f32014-02-15 16:17:07 +0100372 * Prepare the read buffers for reading (if they contain something).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373 */
374 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100375start_stuff(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376{
Bram Moolenaar285e3352018-04-18 23:01:13 +0200377 if (readbuf1.bh_first.b_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378 {
Bram Moolenaar285e3352018-04-18 23:01:13 +0200379 readbuf1.bh_curr = &(readbuf1.bh_first);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100380 readbuf1.bh_space = 0;
381 }
Bram Moolenaar285e3352018-04-18 23:01:13 +0200382 if (readbuf2.bh_first.b_next != NULL)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100383 {
Bram Moolenaar285e3352018-04-18 23:01:13 +0200384 readbuf2.bh_curr = &(readbuf2.bh_first);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100385 readbuf2.bh_space = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386 }
387}
388
389/*
390 * Return TRUE if the stuff buffer is empty.
391 */
392 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100393stuff_empty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394{
Bram Moolenaar285e3352018-04-18 23:01:13 +0200395 return (readbuf1.bh_first.b_next == NULL
396 && readbuf2.bh_first.b_next == NULL);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100397}
398
Bram Moolenaar113e1072019-01-20 15:30:40 +0100399#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100400/*
401 * Return TRUE if readbuf1 is empty. There may still be redo characters in
402 * redbuf2.
403 */
404 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100405readbuf1_empty(void)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100406{
Bram Moolenaar285e3352018-04-18 23:01:13 +0200407 return (readbuf1.bh_first.b_next == NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100409#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410
411/*
412 * Set a typeahead character that won't be flushed.
413 */
414 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100415typeahead_noflush(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416{
417 typeahead_char = c;
418}
419
420/*
421 * Remove the contents of the stuff buffer and the mapped characters in the
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100422 * typeahead buffer (used in case of an error). If "flush_typeahead" is true,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423 * flush all typeahead characters (used when interrupted by a CTRL-C).
424 */
425 void
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200426flush_buffers(flush_buffers_T flush_typeahead)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427{
428 init_typebuf();
429
430 start_stuff();
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100431 while (read_readbuffers(TRUE) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432 ;
433
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200434 if (flush_typeahead == FLUSH_MINIMAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435 {
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200436 // remove mapped characters at the start only
437 typebuf.tb_off += typebuf.tb_maplen;
438 typebuf.tb_len -= typebuf.tb_maplen;
Bram Moolenaare49b4bb2020-03-11 13:01:40 +0100439#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
440 if (typebuf.tb_len == 0)
441 typebuf_was_filled = FALSE;
442#endif
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200443 }
444 else
445 {
446 // remove typeahead
447 if (flush_typeahead == FLUSH_INPUT)
448 // We have to get all characters, because we may delete the first
449 // part of an escape sequence. In an xterm we get one char at a
450 // time and we have to get them all.
451 while (inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 10L) != 0)
452 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453 typebuf.tb_off = MAXMAPLEN;
454 typebuf.tb_len = 0;
Bram Moolenaar4eb65312017-06-24 18:49:00 +0200455#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar30613902019-12-01 22:11:18 +0100456 // Reset the flag that text received from a client or from feedkeys()
457 // was inserted in the typeahead buffer.
Bram Moolenaar4eb65312017-06-24 18:49:00 +0200458 typebuf_was_filled = FALSE;
459#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461 typebuf.tb_maplen = 0;
462 typebuf.tb_silent = 0;
463 cmd_silent = FALSE;
464 typebuf.tb_no_abbr_cnt = 0;
Bram Moolenaarb8d732e2020-08-05 22:07:26 +0200465 if (++typebuf.tb_change_cnt == 0)
466 typebuf.tb_change_cnt = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467}
468
469/*
470 * The previous contents of the redo buffer is kept in old_redobuffer.
471 * This is used for the CTRL-O <.> command in insert mode.
472 */
473 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100474ResetRedobuff(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475{
476 if (!block_redo)
477 {
478 free_buff(&old_redobuff);
479 old_redobuff = redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200480 redobuff.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481 }
482}
483
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100484/*
485 * Discard the contents of the redo buffer and restore the previous redo
486 * buffer.
487 */
488 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100489CancelRedo(void)
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100490{
491 if (!block_redo)
492 {
493 free_buff(&redobuff);
494 redobuff = old_redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200495 old_redobuff.bh_first.b_next = NULL;
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100496 start_stuff();
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100497 while (read_readbuffers(TRUE) != NUL)
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100498 ;
499 }
500}
501
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502/*
503 * Save redobuff and old_redobuff to save_redobuff and save_old_redobuff.
504 * Used before executing autocommands and user functions.
505 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 void
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200507saveRedobuff(save_redo_T *save_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508{
509 char_u *s;
510
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200511 save_redo->sr_redobuff = redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200512 redobuff.bh_first.b_next = NULL;
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200513 save_redo->sr_old_redobuff = old_redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200514 old_redobuff.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515
Bram Moolenaar30613902019-12-01 22:11:18 +0100516 // Make a copy, so that ":normal ." in a function works.
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200517 s = get_buffcont(&save_redo->sr_redobuff, FALSE);
518 if (s != NULL)
519 {
520 add_buff(&redobuff, s, -1L);
521 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 }
523}
524
525/*
526 * Restore redobuff and old_redobuff from save_redobuff and save_old_redobuff.
527 * Used after executing autocommands and user functions.
528 */
529 void
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200530restoreRedobuff(save_redo_T *save_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531{
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200532 free_buff(&redobuff);
533 redobuff = save_redo->sr_redobuff;
534 free_buff(&old_redobuff);
535 old_redobuff = save_redo->sr_old_redobuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537
538/*
539 * Append "s" to the redo buffer.
540 * K_SPECIAL and CSI should already have been escaped.
541 */
542 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100543AppendToRedobuff(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544{
545 if (!block_redo)
546 add_buff(&redobuff, s, -1L);
547}
548
549/*
550 * Append to Redo buffer literally, escaping special characters with CTRL-V.
551 * K_SPECIAL and CSI are escaped as well.
552 */
553 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100554AppendToRedobuffLit(
555 char_u *str,
Bram Moolenaar30613902019-12-01 22:11:18 +0100556 int len) // length of "str" or -1 for up to the NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557{
Bram Moolenaarebefac62005-12-28 22:39:57 +0000558 char_u *s = str;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559 int c;
560 char_u *start;
561
562 if (block_redo)
563 return;
564
Bram Moolenaarebefac62005-12-28 22:39:57 +0000565 while (len < 0 ? *s != NUL : s - str < len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100567 // Put a string of normal characters in the redo buffer (that's
568 // faster).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 start = s;
570 while (*s >= ' '
571#ifndef EBCDIC
Bram Moolenaar30613902019-12-01 22:11:18 +0100572 && *s < DEL // EBCDIC: all chars above space are normal
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573#endif
Bram Moolenaarebefac62005-12-28 22:39:57 +0000574 && (len < 0 || s - str < len))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 ++s;
576
Bram Moolenaar30613902019-12-01 22:11:18 +0100577 // Don't put '0' or '^' as last character, just in case a CTRL-D is
578 // typed next.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 if (*s == NUL && (s[-1] == '0' || s[-1] == '^'))
580 --s;
581 if (s > start)
582 add_buff(&redobuff, start, (long)(s - start));
583
Bram Moolenaarebefac62005-12-28 22:39:57 +0000584 if (*s == NUL || (len >= 0 && s - str >= len))
585 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586
Bram Moolenaar30613902019-12-01 22:11:18 +0100587 // Handle a special or multibyte character.
Bram Moolenaarebefac62005-12-28 22:39:57 +0000588 if (has_mbyte)
Bram Moolenaar30613902019-12-01 22:11:18 +0100589 // Handle composing chars separately.
Bram Moolenaarebefac62005-12-28 22:39:57 +0000590 c = mb_cptr2char_adv(&s);
591 else
Bram Moolenaarebefac62005-12-28 22:39:57 +0000592 c = *s++;
593 if (c < ' ' || c == DEL || (*s == NUL && (c == '0' || c == '^')))
594 add_char_buff(&redobuff, Ctrl_V);
595
Bram Moolenaar30613902019-12-01 22:11:18 +0100596 // CTRL-V '0' must be inserted as CTRL-V 048 (EBCDIC: xf0)
Bram Moolenaarebefac62005-12-28 22:39:57 +0000597 if (*s == NUL && c == '0')
598#ifdef EBCDIC
599 add_buff(&redobuff, (char_u *)"xf0", 3L);
600#else
601 add_buff(&redobuff, (char_u *)"048", 3L);
602#endif
603 else
604 add_char_buff(&redobuff, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 }
606}
607
608/*
609 * Append a character to the redo buffer.
610 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
611 */
612 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100613AppendCharToRedobuff(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614{
615 if (!block_redo)
616 add_char_buff(&redobuff, c);
617}
618
619/*
620 * Append a number to the redo buffer.
621 */
622 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100623AppendNumberToRedobuff(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624{
625 if (!block_redo)
626 add_num_buff(&redobuff, n);
627}
628
629/*
630 * Append string "s" to the stuff buffer.
631 * CSI and K_SPECIAL must already have been escaped.
632 */
633 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100634stuffReadbuff(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100636 add_buff(&readbuf1, s, -1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637}
638
Bram Moolenaar4f5ce332014-07-30 16:00:58 +0200639/*
640 * Append string "s" to the redo stuff buffer.
641 * CSI and K_SPECIAL must already have been escaped.
642 */
643 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100644stuffRedoReadbuff(char_u *s)
Bram Moolenaar4f5ce332014-07-30 16:00:58 +0200645{
646 add_buff(&readbuf2, s, -1L);
647}
648
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200649 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100650stuffReadbuffLen(char_u *s, long len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100652 add_buff(&readbuf1, s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653}
654
655#if defined(FEAT_EVAL) || defined(PROTO)
656/*
657 * Stuff "s" into the stuff buffer, leaving special key codes unmodified and
658 * escaping other K_SPECIAL and CSI bytes.
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200659 * Change CR, LF and ESC into a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 */
661 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100662stuffReadbuffSpec(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663{
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200664 int c;
665
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 while (*s != NUL)
667 {
668 if (*s == K_SPECIAL && s[1] != NUL && s[2] != NUL)
669 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100670 // Insert special key literally.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 stuffReadbuffLen(s, 3L);
672 s += 3;
673 }
674 else
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200675 {
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200676 c = mb_ptr2char_adv(&s);
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200677 if (c == CAR || c == NL || c == ESC)
678 c = ' ';
679 stuffcharReadbuff(c);
680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 }
682}
683#endif
684
685/*
686 * Append a character to the stuff buffer.
687 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
688 */
689 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100690stuffcharReadbuff(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100692 add_char_buff(&readbuf1, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693}
694
695/*
696 * Append a number to the stuff buffer.
697 */
698 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100699stuffnumReadbuff(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100701 add_num_buff(&readbuf1, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702}
703
704/*
Bram Moolenaar11abd092020-05-01 14:26:37 +0200705 * Stuff a string into the typeahead buffer, such that edit() will insert it
706 * literally ("literally" TRUE) or interpret is as typed characters.
707 */
708 void
709stuffescaped(char_u *arg, int literally)
710{
711 int c;
712 char_u *start;
713
714 while (*arg != NUL)
715 {
716 // Stuff a sequence of normal ASCII characters, that's fast. Also
717 // stuff K_SPECIAL to get the effect of a special key when "literally"
718 // is TRUE.
719 start = arg;
720 while ((*arg >= ' '
721#ifndef EBCDIC
722 && *arg < DEL // EBCDIC: chars above space are normal
723#endif
724 )
725 || (*arg == K_SPECIAL && !literally))
726 ++arg;
727 if (arg > start)
728 stuffReadbuffLen(start, (long)(arg - start));
729
730 // stuff a single special character
731 if (*arg != NUL)
732 {
733 if (has_mbyte)
734 c = mb_cptr2char_adv(&arg);
735 else
736 c = *arg++;
737 if (literally && ((c < ' ' && c != TAB) || c == DEL))
738 stuffcharReadbuff(Ctrl_V);
739 stuffcharReadbuff(c);
740 }
741 }
742}
743
744/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 * Read a character from the redo buffer. Translates K_SPECIAL, CSI and
746 * multibyte characters.
747 * The redo buffer is left as it is.
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100748 * If init is TRUE, prepare for redo, return FAIL if nothing to redo, OK
749 * otherwise.
750 * If old is TRUE, use old_redobuff instead of redobuff.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 */
752 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100753read_redo(int init, int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100755 static buffblock_T *bp;
756 static char_u *p;
757 int c;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100758 int n;
759 char_u buf[MB_MAXBYTES + 1];
760 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761
762 if (init)
763 {
764 if (old_redo)
Bram Moolenaar285e3352018-04-18 23:01:13 +0200765 bp = old_redobuff.bh_first.b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 else
Bram Moolenaar285e3352018-04-18 23:01:13 +0200767 bp = redobuff.bh_first.b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 if (bp == NULL)
769 return FAIL;
770 p = bp->b_str;
771 return OK;
772 }
773 if ((c = *p) != NUL)
774 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100775 // Reverse the conversion done by add_char_buff()
776 // For a multi-byte character get all the bytes and return the
777 // converted character.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 if (has_mbyte && (c != K_SPECIAL || p[1] == KS_SPECIAL))
779 n = MB_BYTE2LEN_CHECK(c);
780 else
781 n = 1;
782 for (i = 0; ; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100784 if (c == K_SPECIAL) // special key or escaped K_SPECIAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 {
786 c = TO_SPECIAL(p[1], p[2]);
787 p += 2;
788 }
789#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +0100790 if (c == CSI) // escaped CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 p += 2;
792#endif
793 if (*++p == NUL && bp->b_next != NULL)
794 {
795 bp = bp->b_next;
796 p = bp->b_str;
797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 buf[i] = c;
Bram Moolenaar30613902019-12-01 22:11:18 +0100799 if (i == n - 1) // last byte of a character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 {
801 if (n != 1)
802 c = (*mb_ptr2char)(buf);
803 break;
804 }
805 c = *p;
Bram Moolenaar30613902019-12-01 22:11:18 +0100806 if (c == NUL) // cannot happen?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 }
809 }
810
811 return c;
812}
813
814/*
815 * Copy the rest of the redo buffer into the stuff buffer (in a slow way).
816 * If old_redo is TRUE, use old_redobuff instead of redobuff.
817 * The escaped K_SPECIAL and CSI are copied without translation.
818 */
819 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100820copy_redo(int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821{
822 int c;
823
824 while ((c = read_redo(FALSE, old_redo)) != NUL)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100825 add_char_buff(&readbuf2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826}
827
828/*
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100829 * Stuff the redo buffer into readbuf2.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 * Insert the redo count into the command.
831 * If "old_redo" is TRUE, the last but one command is repeated
832 * instead of the last command (inserting text). This is used for
833 * CTRL-O <.> in insert mode
834 *
835 * return FAIL for failure, OK otherwise
836 */
837 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100838start_redo(long count, int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839{
840 int c;
841
Bram Moolenaar30613902019-12-01 22:11:18 +0100842 // init the pointers; return if nothing to redo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 if (read_redo(TRUE, old_redo) == FAIL)
844 return FAIL;
845
846 c = read_redo(FALSE, old_redo);
847
Bram Moolenaar30613902019-12-01 22:11:18 +0100848 // copy the buffer name, if present
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 if (c == '"')
850 {
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100851 add_buff(&readbuf2, (char_u *)"\"", 1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 c = read_redo(FALSE, old_redo);
853
Bram Moolenaar30613902019-12-01 22:11:18 +0100854 // if a numbered buffer is used, increment the number
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 if (c >= '1' && c < '9')
856 ++c;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100857 add_char_buff(&readbuf2, c);
Bram Moolenaar833093b2018-05-23 21:53:52 +0200858
Bram Moolenaar30613902019-12-01 22:11:18 +0100859 // the expression register should be re-evaluated
Bram Moolenaar833093b2018-05-23 21:53:52 +0200860 if (c == '=')
861 {
862 add_char_buff(&readbuf2, CAR);
863 cmd_silent = TRUE;
864 }
865
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 c = read_redo(FALSE, old_redo);
867 }
868
Bram Moolenaar30613902019-12-01 22:11:18 +0100869 if (c == 'v') // redo Visual
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 {
871 VIsual = curwin->w_cursor;
872 VIsual_active = TRUE;
873 VIsual_select = FALSE;
874 VIsual_reselect = TRUE;
875 redo_VIsual_busy = TRUE;
876 c = read_redo(FALSE, old_redo);
877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878
Bram Moolenaar30613902019-12-01 22:11:18 +0100879 // try to enter the count (in place of a previous count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 if (count)
881 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100882 while (VIM_ISDIGIT(c)) // skip "old" count
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 c = read_redo(FALSE, old_redo);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100884 add_num_buff(&readbuf2, count);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 }
886
Bram Moolenaar30613902019-12-01 22:11:18 +0100887 // copy from the redo buffer into the stuff buffer
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100888 add_char_buff(&readbuf2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 copy_redo(old_redo);
890 return OK;
891}
892
893/*
894 * Repeat the last insert (R, o, O, a, A, i or I command) by stuffing
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100895 * the redo buffer into readbuf2.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 * return FAIL for failure, OK otherwise
897 */
898 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100899start_redo_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900{
901 int c;
902
903 if (read_redo(TRUE, FALSE) == FAIL)
904 return FAIL;
905 start_stuff();
906
Bram Moolenaar30613902019-12-01 22:11:18 +0100907 // skip the count and the command character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 while ((c = read_redo(FALSE, FALSE)) != NUL)
909 {
910 if (vim_strchr((char_u *)"AaIiRrOo", c) != NULL)
911 {
912 if (c == 'O' || c == 'o')
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100913 add_buff(&readbuf2, NL_STR, -1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 break;
915 }
916 }
917
Bram Moolenaar30613902019-12-01 22:11:18 +0100918 // copy the typed text from the redo buffer into the stuff buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 copy_redo(FALSE);
920 block_redo = TRUE;
921 return OK;
922}
923
924 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100925stop_redo_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926{
927 block_redo = FALSE;
928}
929
930/*
931 * Initialize typebuf.tb_buf to point to typebuf_init.
932 * alloc() cannot be used here: In out-of-memory situations it would
933 * be impossible to type anything.
934 */
935 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100936init_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937{
938 if (typebuf.tb_buf == NULL)
939 {
940 typebuf.tb_buf = typebuf_init;
941 typebuf.tb_noremap = noremapbuf_init;
942 typebuf.tb_buflen = TYPELEN_INIT;
943 typebuf.tb_len = 0;
Bram Moolenaard34f9b12017-04-08 18:41:13 +0200944 typebuf.tb_off = MAXMAPLEN + 4;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 typebuf.tb_change_cnt = 1;
946 }
947}
948
949/*
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200950 * Returns TRUE when keys cannot be remapped.
951 */
952 int
953noremap_keys(void)
954{
955 return KeyNoremap & (RM_NONE|RM_SCRIPT);
956}
957
958/*
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100959 * Insert a string in position 'offset' in the typeahead buffer (for "@r"
960 * and ":normal" command, vgetorpeek() and check_termcode()).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 *
962 * If noremap is REMAP_YES, new string can be mapped again.
963 * If noremap is REMAP_NONE, new string cannot be mapped again.
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000964 * If noremap is REMAP_SKIP, first char of new string cannot be mapped again,
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000965 * but abbreviations are allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 * If noremap is REMAP_SCRIPT, new string cannot be mapped again, except for
967 * script-local mappings.
968 * If noremap is > 0, that many characters of the new string cannot be mapped.
969 *
970 * If nottyped is TRUE, the string does not return KeyTyped (don't use when
971 * offset is non-zero!).
972 *
973 * If silent is TRUE, cmd_silent is set when the characters are obtained.
974 *
975 * return FAIL for failure, OK otherwise
976 */
977 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100978ins_typebuf(
979 char_u *str,
980 int noremap,
981 int offset,
982 int nottyped,
983 int silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984{
985 char_u *s1, *s2;
986 int newlen;
987 int addlen;
988 int i;
989 int newoff;
990 int val;
991 int nrm;
992
993 init_typebuf();
994 if (++typebuf.tb_change_cnt == 0)
995 typebuf.tb_change_cnt = 1;
Bram Moolenaard103ee72019-09-18 21:15:31 +0200996 state_no_longer_safe("ins_typebuf()");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997
998 addlen = (int)STRLEN(str);
Bram Moolenaar4e86cba2007-05-06 11:56:32 +0000999
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 if (offset == 0 && addlen <= typebuf.tb_off)
1001 {
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001002 /*
1003 * Easy case: there is room in front of typebuf.tb_buf[typebuf.tb_off]
1004 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 typebuf.tb_off -= addlen;
1006 mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen);
1007 }
Bram Moolenaard34f9b12017-04-08 18:41:13 +02001008 else if (typebuf.tb_len == 0 && typebuf.tb_buflen
1009 >= addlen + 3 * (MAXMAPLEN + 4))
1010 {
1011 /*
1012 * Buffer is empty and string fits in the existing buffer.
1013 * Leave some space before and after, if possible.
1014 */
1015 typebuf.tb_off = (typebuf.tb_buflen - addlen - 3 * (MAXMAPLEN + 4)) / 2;
1016 mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen);
1017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 else
1019 {
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001020 int extra;
1021
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001022 /*
1023 * Need to allocate a new buffer.
Bram Moolenaard34f9b12017-04-08 18:41:13 +02001024 * In typebuf.tb_buf there must always be room for 3 * (MAXMAPLEN + 4)
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001025 * characters. We add some extra room to avoid having to allocate too
1026 * often.
1027 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 newoff = MAXMAPLEN + 4;
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001029 extra = addlen + newoff + 4 * (MAXMAPLEN + 4);
1030 if (typebuf.tb_len > 2147483647 - extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 {
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001032 // string is getting too long for a 32 bit int
Bram Moolenaar12f3c1b2021-12-05 21:46:34 +00001033 emsg(_(e_command_too_complex)); // also calls flush_buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 setcursor();
1035 return FAIL;
1036 }
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001037 newlen = typebuf.tb_len + extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 s1 = alloc(newlen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001039 if (s1 == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 return FAIL;
1041 s2 = alloc(newlen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001042 if (s2 == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 {
1044 vim_free(s1);
1045 return FAIL;
1046 }
1047 typebuf.tb_buflen = newlen;
1048
Bram Moolenaar30613902019-12-01 22:11:18 +01001049 // copy the old chars, before the insertion point
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 mch_memmove(s1 + newoff, typebuf.tb_buf + typebuf.tb_off,
1051 (size_t)offset);
Bram Moolenaar30613902019-12-01 22:11:18 +01001052 // copy the new chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 mch_memmove(s1 + newoff + offset, str, (size_t)addlen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001054 // copy the old chars, after the insertion point, including the NUL at
1055 // the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 mch_memmove(s1 + newoff + offset + addlen,
1057 typebuf.tb_buf + typebuf.tb_off + offset,
1058 (size_t)(typebuf.tb_len - offset + 1));
1059 if (typebuf.tb_buf != typebuf_init)
1060 vim_free(typebuf.tb_buf);
1061 typebuf.tb_buf = s1;
1062
1063 mch_memmove(s2 + newoff, typebuf.tb_noremap + typebuf.tb_off,
1064 (size_t)offset);
1065 mch_memmove(s2 + newoff + offset + addlen,
1066 typebuf.tb_noremap + typebuf.tb_off + offset,
1067 (size_t)(typebuf.tb_len - offset));
1068 if (typebuf.tb_noremap != noremapbuf_init)
1069 vim_free(typebuf.tb_noremap);
1070 typebuf.tb_noremap = s2;
1071
1072 typebuf.tb_off = newoff;
1073 }
1074 typebuf.tb_len += addlen;
1075
Bram Moolenaar30613902019-12-01 22:11:18 +01001076 // If noremap == REMAP_SCRIPT: do remap script-local mappings.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 if (noremap == REMAP_SCRIPT)
1078 val = RM_SCRIPT;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001079 else if (noremap == REMAP_SKIP)
1080 val = RM_ABBR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 else
1082 val = RM_NONE;
1083
1084 /*
1085 * Adjust typebuf.tb_noremap[] for the new characters:
1086 * If noremap == REMAP_NONE or REMAP_SCRIPT: new characters are
1087 * (sometimes) not remappable
1088 * If noremap == REMAP_YES: all the new characters are mappable
1089 * If noremap > 0: "noremap" characters are not remappable, the rest
1090 * mappable
1091 */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001092 if (noremap == REMAP_SKIP)
1093 nrm = 1;
1094 else if (noremap < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 nrm = addlen;
1096 else
1097 nrm = noremap;
1098 for (i = 0; i < addlen; ++i)
1099 typebuf.tb_noremap[typebuf.tb_off + i + offset] =
1100 (--nrm >= 0) ? val : RM_YES;
1101
Bram Moolenaar30613902019-12-01 22:11:18 +01001102 // tb_maplen and tb_silent only remember the length of mapped and/or
1103 // silent mappings at the start of the buffer, assuming that a mapped
1104 // sequence doesn't result in typed characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 if (nottyped || typebuf.tb_maplen > offset)
1106 typebuf.tb_maplen += addlen;
1107 if (silent || typebuf.tb_silent > offset)
1108 {
1109 typebuf.tb_silent += addlen;
1110 cmd_silent = TRUE;
1111 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001112 if (typebuf.tb_no_abbr_cnt && offset == 0) // and not used for abbrev.s
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 typebuf.tb_no_abbr_cnt += addlen;
1114
1115 return OK;
1116}
1117
1118/*
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001119 * Put character "c" back into the typeahead buffer.
1120 * Can be used for a character obtained by vgetc() that needs to be put back.
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00001121 * Uses cmd_silent, KeyTyped and KeyNoremap to restore the flags belonging to
1122 * the char.
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001123 * Returns the length of what was inserted.
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001124 */
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001125 int
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001126ins_char_typebuf(int c, int modifier)
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001127{
zeertzjq6cac7702022-01-04 18:01:21 +00001128 char_u buf[MB_MAXBYTES * 3 + 4];
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001129 int len = 0;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001130
1131 if (modifier != 0)
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001132 {
1133 buf[0] = K_SPECIAL;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001134 buf[1] = KS_MODIFIER;
1135 buf[2] = modifier;
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001136 buf[3] = NUL;
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001137 len = 3;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001138 }
1139 if (IS_SPECIAL(c))
1140 {
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001141 buf[len] = K_SPECIAL;
1142 buf[len + 1] = K_SECOND(c);
1143 buf[len + 2] = K_THIRD(c);
1144 buf[len + 3] = NUL;
1145 len += 3;
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001146 }
1147 else
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001148 {
zeertzjq6cac7702022-01-04 18:01:21 +00001149 char_u *p = buf + len;
1150 int char_len = (*mb_char2bytes)(c, p);
1151#ifdef FEAT_GUI
1152 int save_gui_in_use = gui.in_use;
1153
1154 gui.in_use = FALSE;
1155#endif
1156 // if the character contains CSI or K_SPECIAL bytes they need escaping
1157 len += fix_input_buffer(p, char_len);
1158#ifdef FEAT_GUI
1159 gui.in_use = save_gui_in_use;
1160#endif
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001161 }
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00001162 (void)ins_typebuf(buf, KeyNoremap, 0, !KeyTyped, cmd_silent);
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001163 return len;
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001164}
1165
1166/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 * Return TRUE if the typeahead buffer was changed (while waiting for a
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001168 * character to arrive). Happens when a message was received from a client or
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001169 * from feedkeys().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 * But check in a more generic way to avoid trouble: When "typebuf.tb_buf"
1171 * changed it was reallocated and the old pointer can no longer be used.
1172 * Or "typebuf.tb_off" may have been changed and we would overwrite characters
1173 * that was just added.
1174 */
1175 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001176typebuf_changed(
Bram Moolenaar30613902019-12-01 22:11:18 +01001177 int tb_change_cnt) // old value of typebuf.tb_change_cnt
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178{
1179 return (tb_change_cnt != 0 && (typebuf.tb_change_cnt != tb_change_cnt
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001180#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
1181 || typebuf_was_filled
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182#endif
1183 ));
1184}
1185
1186/*
1187 * Return TRUE if there are no characters in the typeahead buffer that have
1188 * not been typed (result from a mapping or come from ":normal").
1189 */
1190 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001191typebuf_typed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192{
1193 return typebuf.tb_maplen == 0;
1194}
1195
1196/*
1197 * Return the number of characters that are mapped (or not typed).
1198 */
1199 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001200typebuf_maplen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201{
1202 return typebuf.tb_maplen;
1203}
1204
1205/*
1206 * remove "len" characters from typebuf.tb_buf[typebuf.tb_off + offset]
1207 */
1208 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001209del_typebuf(int len, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210{
1211 int i;
1212
1213 if (len == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01001214 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215
1216 typebuf.tb_len -= len;
1217
1218 /*
1219 * Easy case: Just increase typebuf.tb_off.
1220 */
1221 if (offset == 0 && typebuf.tb_buflen - (typebuf.tb_off + len)
1222 >= 3 * MAXMAPLEN + 3)
1223 typebuf.tb_off += len;
1224 /*
1225 * Have to move the characters in typebuf.tb_buf[] and typebuf.tb_noremap[]
1226 */
1227 else
1228 {
1229 i = typebuf.tb_off + offset;
1230 /*
1231 * Leave some extra room at the end to avoid reallocation.
1232 */
1233 if (typebuf.tb_off > MAXMAPLEN)
1234 {
1235 mch_memmove(typebuf.tb_buf + MAXMAPLEN,
1236 typebuf.tb_buf + typebuf.tb_off, (size_t)offset);
1237 mch_memmove(typebuf.tb_noremap + MAXMAPLEN,
1238 typebuf.tb_noremap + typebuf.tb_off, (size_t)offset);
1239 typebuf.tb_off = MAXMAPLEN;
1240 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001241 // adjust typebuf.tb_buf (include the NUL at the end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset,
1243 typebuf.tb_buf + i + len,
1244 (size_t)(typebuf.tb_len - offset + 1));
Bram Moolenaar30613902019-12-01 22:11:18 +01001245 // adjust typebuf.tb_noremap[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 mch_memmove(typebuf.tb_noremap + typebuf.tb_off + offset,
1247 typebuf.tb_noremap + i + len,
1248 (size_t)(typebuf.tb_len - offset));
1249 }
1250
Bram Moolenaar30613902019-12-01 22:11:18 +01001251 if (typebuf.tb_maplen > offset) // adjust tb_maplen
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 {
1253 if (typebuf.tb_maplen < offset + len)
1254 typebuf.tb_maplen = offset;
1255 else
1256 typebuf.tb_maplen -= len;
1257 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001258 if (typebuf.tb_silent > offset) // adjust tb_silent
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 {
1260 if (typebuf.tb_silent < offset + len)
1261 typebuf.tb_silent = offset;
1262 else
1263 typebuf.tb_silent -= len;
1264 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001265 if (typebuf.tb_no_abbr_cnt > offset) // adjust tb_no_abbr_cnt
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 {
1267 if (typebuf.tb_no_abbr_cnt < offset + len)
1268 typebuf.tb_no_abbr_cnt = offset;
1269 else
1270 typebuf.tb_no_abbr_cnt -= len;
1271 }
1272
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001273#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar30613902019-12-01 22:11:18 +01001274 // Reset the flag that text received from a client or from feedkeys()
1275 // was inserted in the typeahead buffer.
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001276 typebuf_was_filled = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277#endif
1278 if (++typebuf.tb_change_cnt == 0)
1279 typebuf.tb_change_cnt = 1;
1280}
1281
1282/*
1283 * Write typed characters to script file.
1284 * If recording is on put the character in the recordbuffer.
1285 */
1286 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001287gotchars(char_u *chars, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288{
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001289 char_u *s = chars;
1290 int i;
1291 static char_u buf[4];
1292 static int buflen = 0;
1293 int todo = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294
Bram Moolenaar4e86cba2007-05-06 11:56:32 +00001295 while (todo--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 {
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001297 buf[buflen++] = *s++;
1298
1299 // When receiving a special key sequence, store it until we have all
1300 // the bytes and we can decide what to do with it.
1301 if (buflen == 1 && buf[0] == K_SPECIAL)
1302 continue;
1303 if (buflen == 2)
1304 continue;
1305 if (buflen == 3 && buf[1] == KS_EXTRA
1306 && (buf[2] == KE_FOCUSGAINED || buf[2] == KE_FOCUSLOST))
1307 {
1308 // Drop K_FOCUSGAINED and K_FOCUSLOST, they are not useful in a
1309 // recording.
1310 buflen = 0;
1311 continue;
1312 }
1313
Bram Moolenaar30613902019-12-01 22:11:18 +01001314 // Handle one byte at a time; no translation to be done.
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001315 for (i = 0; i < buflen; ++i)
1316 updatescript(buf[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001318 if (reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 {
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001320 buf[buflen] = NUL;
1321 add_buff(&recordbuff, buf, (long)buflen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001322 // remember how many chars were last recorded
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001323 last_recorded_len += buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 }
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001325 buflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 }
1327 may_sync_undo();
1328
1329#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01001330 // output "debug mode" message next time in debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 debug_did_msg = FALSE;
1332#endif
1333
Bram Moolenaar30613902019-12-01 22:11:18 +01001334 // Since characters have been typed, consider the following to be in
1335 // another mapping. Search string will be kept in history.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 ++maptick;
1337}
1338
1339/*
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001340 * Undo the last gotchars() for "len" bytes. To be used when putting a typed
1341 * character back into the typeahead buffer, thus gotchars() will be called
1342 * again.
1343 * Only affects recorded characters.
1344 */
1345 void
1346ungetchars(int len)
1347{
1348 if (reg_recording != 0)
1349 {
1350 delete_buff_tail(&recordbuff, len);
1351 last_recorded_len -= len;
1352 }
1353}
1354
1355/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 * Sync undo. Called when typed characters are obtained from the typeahead
1357 * buffer, or when a menu is used.
1358 * Do not sync:
1359 * - In Insert mode, unless cursor key has been used.
1360 * - While reading a script file.
1361 * - When no_u_sync is non-zero.
1362 */
1363 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001364may_sync_undo(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365{
1366 if ((!(State & (INSERT + CMDLINE)) || arrow_used)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001367 && scriptin[curscript] == NULL)
1368 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369}
1370
1371/*
1372 * Make "typebuf" empty and allocate new buffers.
1373 * Returns FAIL when out of memory.
1374 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001375 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001376alloc_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377{
1378 typebuf.tb_buf = alloc(TYPELEN_INIT);
1379 typebuf.tb_noremap = alloc(TYPELEN_INIT);
1380 if (typebuf.tb_buf == NULL || typebuf.tb_noremap == NULL)
1381 {
1382 free_typebuf();
1383 return FAIL;
1384 }
1385 typebuf.tb_buflen = TYPELEN_INIT;
Bram Moolenaar30613902019-12-01 22:11:18 +01001386 typebuf.tb_off = MAXMAPLEN + 4; // can insert without realloc
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 typebuf.tb_len = 0;
1388 typebuf.tb_maplen = 0;
1389 typebuf.tb_silent = 0;
1390 typebuf.tb_no_abbr_cnt = 0;
1391 if (++typebuf.tb_change_cnt == 0)
1392 typebuf.tb_change_cnt = 1;
Bram Moolenaare49b4bb2020-03-11 13:01:40 +01001393#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
1394 typebuf_was_filled = FALSE;
1395#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 return OK;
1397}
1398
1399/*
1400 * Free the buffers of "typebuf".
1401 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001402 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001403free_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404{
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001405 if (typebuf.tb_buf == typebuf_init)
Bram Moolenaar95f09602016-11-10 20:01:45 +01001406 internal_error("Free typebuf 1");
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001407 else
Bram Moolenaar0f1c6702019-09-28 15:24:00 +02001408 VIM_CLEAR(typebuf.tb_buf);
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00001409 if (typebuf.tb_noremap == noremapbuf_init)
Bram Moolenaar95f09602016-11-10 20:01:45 +01001410 internal_error("Free typebuf 2");
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001411 else
Bram Moolenaar0f1c6702019-09-28 15:24:00 +02001412 VIM_CLEAR(typebuf.tb_noremap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413}
1414
1415/*
1416 * When doing ":so! file", the current typeahead needs to be saved, and
1417 * restored when "file" has been read completely.
1418 */
1419static typebuf_T saved_typebuf[NSCRIPT];
1420
1421 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001422save_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423{
1424 init_typebuf();
1425 saved_typebuf[curscript] = typebuf;
Bram Moolenaar30613902019-12-01 22:11:18 +01001426 // If out of memory: restore typebuf and close file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 if (alloc_typebuf() == FAIL)
1428 {
1429 closescript();
1430 return FAIL;
1431 }
1432 return OK;
1433}
1434
Bram Moolenaar30613902019-12-01 22:11:18 +01001435static int old_char = -1; // character put back by vungetc()
1436static int old_mod_mask; // mod_mask for ungotten character
1437static int old_mouse_row; // mouse_row related to old_char
1438static int old_mouse_col; // mouse_col related to old_char
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001439
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440/*
1441 * Save all three kinds of typeahead, so that the user must type at a prompt.
1442 */
1443 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001444save_typeahead(tasave_T *tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445{
1446 tp->save_typebuf = typebuf;
1447 tp->typebuf_valid = (alloc_typebuf() == OK);
1448 if (!tp->typebuf_valid)
1449 typebuf = tp->save_typebuf;
1450
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001451 tp->old_char = old_char;
1452 tp->old_mod_mask = old_mod_mask;
1453 old_char = -1;
1454
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001455 tp->save_readbuf1 = readbuf1;
Bram Moolenaar285e3352018-04-18 23:01:13 +02001456 readbuf1.bh_first.b_next = NULL;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001457 tp->save_readbuf2 = readbuf2;
Bram Moolenaar285e3352018-04-18 23:01:13 +02001458 readbuf2.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459# ifdef USE_INPUT_BUF
1460 tp->save_inputbuf = get_input_buf();
1461# endif
1462}
1463
1464/*
1465 * Restore the typeahead to what it was before calling save_typeahead().
1466 * The allocated memory is freed, can only be called once!
Bram Moolenaarc41badb2021-06-07 22:04:52 +02001467 * When "overwrite" is FALSE input typed later is kept.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 */
1469 void
Bram Moolenaarc41badb2021-06-07 22:04:52 +02001470restore_typeahead(tasave_T *tp, int overwrite UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471{
1472 if (tp->typebuf_valid)
1473 {
1474 free_typebuf();
1475 typebuf = tp->save_typebuf;
1476 }
1477
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001478 old_char = tp->old_char;
1479 old_mod_mask = tp->old_mod_mask;
1480
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001481 free_buff(&readbuf1);
1482 readbuf1 = tp->save_readbuf1;
1483 free_buff(&readbuf2);
1484 readbuf2 = tp->save_readbuf2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485# ifdef USE_INPUT_BUF
Bram Moolenaarc41badb2021-06-07 22:04:52 +02001486 set_input_buf(tp->save_inputbuf, overwrite);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487# endif
1488}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489
1490/*
1491 * Open a new script file for the ":source!" command.
1492 */
1493 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001494openscript(
1495 char_u *name,
Bram Moolenaar30613902019-12-01 22:11:18 +01001496 int directly) // when TRUE execute directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497{
1498 if (curscript + 1 == NSCRIPT)
1499 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001500 emsg(_(e_scripts_nested_too_deep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 return;
1502 }
Bram Moolenaar53575522019-05-22 22:38:25 +02001503
1504 // Disallow sourcing a file in the sandbox, the commands would be executed
1505 // later, possibly outside of the sandbox.
1506 if (check_secure())
1507 return;
1508
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001509#ifdef FEAT_EVAL
1510 if (ignore_script)
Bram Moolenaar30613902019-12-01 22:11:18 +01001511 // Not reading from script, also don't open one. Warning message?
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001512 return;
1513#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514
Bram Moolenaar30613902019-12-01 22:11:18 +01001515 if (scriptin[curscript] != NULL) // already reading script
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 ++curscript;
Bram Moolenaar30613902019-12-01 22:11:18 +01001517 // use NameBuff for expanded name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 expand_env(name, NameBuff, MAXPATHL);
1519 if ((scriptin[curscript] = mch_fopen((char *)NameBuff, READBIN)) == NULL)
1520 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001521 semsg(_(e_cant_open_file_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 if (curscript)
1523 --curscript;
1524 return;
1525 }
1526 if (save_typebuf() == FAIL)
1527 return;
1528
1529 /*
1530 * Execute the commands from the file right now when using ":source!"
1531 * after ":global" or ":argdo" or in a loop. Also when another command
1532 * follows. This means the display won't be updated. Don't do this
1533 * always, "make test" would fail.
1534 */
1535 if (directly)
1536 {
1537 oparg_T oa;
1538 int oldcurscript;
1539 int save_State = State;
1540 int save_restart_edit = restart_edit;
1541 int save_insertmode = p_im;
1542 int save_finish_op = finish_op;
1543 int save_msg_scroll = msg_scroll;
1544
1545 State = NORMAL;
Bram Moolenaar30613902019-12-01 22:11:18 +01001546 msg_scroll = FALSE; // no msg scrolling in Normal mode
1547 restart_edit = 0; // don't go to Insert mode
1548 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 clear_oparg(&oa);
1550 finish_op = FALSE;
1551
1552 oldcurscript = curscript;
1553 do
1554 {
Bram Moolenaar386b43e2019-05-19 21:57:11 +02001555 update_topline_cursor(); // update cursor position and topline
1556 normal_cmd(&oa, FALSE); // execute one command
Bram Moolenaarae97b942020-07-09 19:16:35 +02001557 (void)vpeekc(); // check for end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 }
1559 while (scriptin[oldcurscript] != NULL);
1560
1561 State = save_State;
1562 msg_scroll = save_msg_scroll;
1563 restart_edit = save_restart_edit;
1564 p_im = save_insertmode;
1565 finish_op = save_finish_op;
1566 }
1567}
1568
1569/*
1570 * Close the currently active input script.
1571 */
1572 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001573closescript(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574{
1575 free_typebuf();
1576 typebuf = saved_typebuf[curscript];
1577
1578 fclose(scriptin[curscript]);
1579 scriptin[curscript] = NULL;
1580 if (curscript > 0)
1581 --curscript;
1582}
1583
Bram Moolenaarea408852005-06-25 22:49:46 +00001584#if defined(EXITFREE) || defined(PROTO)
1585 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001586close_all_scripts(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00001587{
1588 while (scriptin[0] != NULL)
1589 closescript();
1590}
1591#endif
1592
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593/*
1594 * Return TRUE when reading keys from a script file.
1595 */
1596 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001597using_script(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598{
1599 return scriptin[curscript] != NULL;
1600}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601
1602/*
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001603 * This function is called just before doing a blocking wait. Thus after
1604 * waiting 'updatetime' for a character to arrive.
1605 */
1606 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001607before_blocking(void)
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001608{
1609 updatescript(0);
1610#ifdef FEAT_EVAL
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001611 if (may_garbage_collect)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001612 garbage_collect(FALSE);
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001613#endif
1614}
1615
1616/*
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001617 * updatescript() is called when a character can be written into the script file
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 * or when we have waited some time for a character (c == 0)
1619 *
1620 * All the changed memfiles are synced if c == 0 or when the number of typed
1621 * characters reaches 'updatecount' and 'updatecount' is non-zero.
1622 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001623 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001624updatescript(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625{
1626 static int count = 0;
1627
1628 if (c && scriptout)
1629 putc(c, scriptout);
1630 if (c == 0 || (p_uc > 0 && ++count >= p_uc))
1631 {
1632 ml_sync_all(c == 0, TRUE);
1633 count = 0;
1634 }
1635}
1636
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637/*
Bram Moolenaar975a8802020-06-06 22:36:24 +02001638 * Convert "c" plus "modifiers" to merge the effect of modifyOtherKeys into the
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001639 * character.
1640 */
1641 int
Bram Moolenaar975a8802020-06-06 22:36:24 +02001642merge_modifyOtherKeys(int c_arg, int *modifiers)
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001643{
1644 int c = c_arg;
1645
Bram Moolenaar975a8802020-06-06 22:36:24 +02001646 if (*modifiers & MOD_MASK_CTRL)
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001647 {
1648 if ((c >= '`' && c <= 0x7f) || (c >= '@' && c <= '_'))
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001649 c &= 0x1f;
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001650 else if (c == '6')
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001651 // CTRL-6 is equivalent to CTRL-^
1652 c = 0x1e;
Bram Moolenaarf4ae6b22020-05-30 19:52:46 +02001653#ifdef FEAT_GUI_GTK
1654 // These mappings look arbitrary at the first glance, but in fact
1655 // resemble quite exactly the behaviour of the GTK+ 1.2 GUI on my
1656 // machine. The only difference is BS vs. DEL for CTRL-8 (makes
1657 // more sense and is consistent with usual terminal behaviour).
1658 else if (c == '2')
1659 c = NUL;
1660 else if (c >= '3' && c <= '7')
1661 c = c ^ 0x28;
1662 else if (c == '8')
1663 c = BS;
1664 else if (c == '?')
1665 c = DEL;
1666#endif
1667 if (c != c_arg)
Bram Moolenaar975a8802020-06-06 22:36:24 +02001668 *modifiers &= ~MOD_MASK_CTRL;
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001669 }
Bram Moolenaar975a8802020-06-06 22:36:24 +02001670 if ((*modifiers & (MOD_MASK_META | MOD_MASK_ALT))
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001671 && c >= 0 && c <= 127)
1672 {
1673 c += 0x80;
Bram Moolenaar975a8802020-06-06 22:36:24 +02001674 *modifiers &= ~(MOD_MASK_META|MOD_MASK_ALT);
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001675 }
1676 return c;
1677}
1678
1679/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 * Get the next input character.
1681 * Can return a special key or a multi-byte character.
1682 * Can return NUL when called recursively, use safe_vgetc() if that's not
1683 * wanted.
1684 * This translates escaped K_SPECIAL and CSI bytes to a K_SPECIAL or CSI byte.
1685 * Collects the bytes of a multibyte character into the whole character.
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00001686 * Returns the modifiers in the global "mod_mask".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 */
1688 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001689vgetc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690{
1691 int c, c2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001693 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001696#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01001697 // Do garbage collection when garbagecollect() was called previously and
1698 // we are now at the toplevel.
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001699 if (may_garbage_collect && want_garbage_collect)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001700 garbage_collect(FALSE);
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001701#endif
1702
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 /*
1704 * If a character was put back with vungetc, it was already processed.
1705 * Return it directly.
1706 */
1707 if (old_char != -1)
1708 {
1709 c = old_char;
1710 old_char = -1;
1711 mod_mask = old_mod_mask;
Bram Moolenaarb8978712013-03-16 21:42:16 +01001712 mouse_row = old_mouse_row;
1713 mouse_col = old_mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001715 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 {
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001717 mod_mask = 0;
1718 vgetc_mod_mask = 0;
1719 vgetc_char = 0;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001720 last_recorded_len = 0;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001721
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001722 for (;;) // this is done twice if there are modifiers
1723 {
1724 int did_inc = FALSE;
Bram Moolenaar2455c4e2015-09-15 18:29:39 +02001725
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001726 if (mod_mask
Bram Moolenaar39719052017-09-05 22:20:46 +02001727#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001728 || im_is_preediting()
Bram Moolenaar39719052017-09-05 22:20:46 +02001729#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001730#if defined(FEAT_PROP_POPUP)
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001731 || popup_no_mapping()
1732#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001733 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 {
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001735 // no mapping after modifier has been read
1736 ++no_mapping;
1737 ++allow_keys;
1738 did_inc = TRUE; // mod_mask may change value
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 }
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001740 c = vgetorpeek(TRUE);
1741 if (did_inc)
1742 {
1743 --no_mapping;
1744 --allow_keys;
1745 }
1746
1747 // Get two extra bytes for special keys
1748 if (c == K_SPECIAL
1749#ifdef FEAT_GUI
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001750 || (c == CSI)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001751#endif
1752 )
1753 {
1754 int save_allow_keys = allow_keys;
1755
1756 ++no_mapping;
1757 allow_keys = 0; // make sure BS is not found
1758 c2 = vgetorpeek(TRUE); // no mapping for these chars
1759 c = vgetorpeek(TRUE);
1760 --no_mapping;
1761 allow_keys = save_allow_keys;
1762 if (c2 == KS_MODIFIER)
1763 {
1764 mod_mask = c;
1765 continue;
1766 }
1767 c = TO_SPECIAL(c2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768
Bram Moolenaar4f974752019-02-17 17:44:42 +01001769#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001770 // Handle K_TEAROFF here, the caller of vgetc() doesn't need to
1771 // know that a menu was torn off
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001772 if (
1773# ifdef VIMDLL
1774 gui.in_use &&
1775# endif
1776 c == K_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 {
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001778 char_u name[200];
1779 int i;
1780
1781 // get menu path, it ends with a <CR>
1782 for (i = 0; (c = vgetorpeek(TRUE)) != '\r'; )
1783 {
1784 name[i] = c;
1785 if (i < 199)
1786 ++i;
1787 }
1788 name[i] = NUL;
1789 gui_make_tearoff(name);
1790 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792#endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001793#if defined(FEAT_GUI) && defined(FEAT_GUI_GTK) && defined(FEAT_MENU)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001794 // GTK: <F10> normally selects the menu, but it's passed until
1795 // here to allow mapping it. Intercept and invoke the GTK
1796 // behavior if it's not mapped.
1797 if (c == K_F10 && gui.menubar != NULL)
1798 {
1799 gtk_menu_shell_select_first(
1800 GTK_MENU_SHELL(gui.menubar), FALSE);
1801 continue;
1802 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001803#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804#ifdef FEAT_GUI
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001805 // Handle focus event here, so that the caller doesn't need to
1806 // know about it. Return K_IGNORE so that we loop once (needed
1807 // if 'lazyredraw' is set).
1808 if (c == K_FOCUSGAINED || c == K_FOCUSLOST)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001809 {
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001810 ui_focus_change(c == K_FOCUSGAINED);
1811 c = K_IGNORE;
Bram Moolenaara9dd2d32019-04-29 21:58:41 +02001812 }
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001813
1814 // Translate K_CSI to CSI. The special key is only used to
1815 // avoid it being recognized as the start of a special key.
1816 if (c == K_CSI)
1817 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001819 }
1820 // a keypad or special function key was not mapped, use it like
1821 // its ASCII equivalent
1822 switch (c)
1823 {
1824 case K_KPLUS: c = '+'; break;
1825 case K_KMINUS: c = '-'; break;
1826 case K_KDIVIDE: c = '/'; break;
1827 case K_KMULTIPLY: c = '*'; break;
1828 case K_KENTER: c = CAR; break;
1829 case K_KPOINT:
Bram Moolenaar4f974752019-02-17 17:44:42 +01001830#ifdef MSWIN
Bram Moolenaar8e85db02018-07-27 23:16:51 +02001831 // Can be either '.' or a ',',
1832 // depending on the type of keypad.
1833 c = MapVirtualKey(VK_DECIMAL, 2); break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001834#else
Bram Moolenaar8e85db02018-07-27 23:16:51 +02001835 c = '.'; break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001836#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001837 case K_K0: c = '0'; break;
1838 case K_K1: c = '1'; break;
1839 case K_K2: c = '2'; break;
1840 case K_K3: c = '3'; break;
1841 case K_K4: c = '4'; break;
1842 case K_K5: c = '5'; break;
1843 case K_K6: c = '6'; break;
1844 case K_K7: c = '7'; break;
1845 case K_K8: c = '8'; break;
1846 case K_K9: c = '9'; break;
Bram Moolenaara88d9682005-03-25 21:45:43 +00001847
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001848 case K_XHOME:
1849 case K_ZHOME: if (mod_mask == MOD_MASK_SHIFT)
Bram Moolenaara88d9682005-03-25 21:45:43 +00001850 {
1851 c = K_S_HOME;
1852 mod_mask = 0;
1853 }
1854 else if (mod_mask == MOD_MASK_CTRL)
1855 {
1856 c = K_C_HOME;
1857 mod_mask = 0;
1858 }
1859 else
1860 c = K_HOME;
1861 break;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001862 case K_XEND:
1863 case K_ZEND: if (mod_mask == MOD_MASK_SHIFT)
Bram Moolenaara88d9682005-03-25 21:45:43 +00001864 {
1865 c = K_S_END;
1866 mod_mask = 0;
1867 }
1868 else if (mod_mask == MOD_MASK_CTRL)
1869 {
1870 c = K_C_END;
1871 mod_mask = 0;
1872 }
1873 else
1874 c = K_END;
1875 break;
1876
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001877 case K_XUP: c = K_UP; break;
1878 case K_XDOWN: c = K_DOWN; break;
1879 case K_XLEFT: c = K_LEFT; break;
1880 case K_XRIGHT: c = K_RIGHT; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001883 // For a multi-byte character get all the bytes and return the
1884 // converted character.
1885 // Note: This will loop until enough bytes are received!
1886 if (has_mbyte && (n = MB_BYTE2LEN_CHECK(c)) > 1)
1887 {
1888 ++no_mapping;
1889 buf[0] = c;
1890 for (i = 1; i < n; ++i)
1891 {
1892 buf[i] = vgetorpeek(TRUE);
1893 if (buf[i] == K_SPECIAL
1894#ifdef FEAT_GUI
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001895 || (buf[i] == CSI)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001896#endif
1897 )
1898 {
1899 // Must be a K_SPECIAL - KS_SPECIAL - KE_FILLER
1900 // sequence, which represents a K_SPECIAL (0x80),
1901 // or a CSI - KS_EXTRA - KE_CSI sequence, which
1902 // represents a CSI (0x9B),
1903 // or a K_SPECIAL - KS_EXTRA - KE_CSI, which is CSI
1904 // too.
1905 c = vgetorpeek(TRUE);
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01001906 if (vgetorpeek(TRUE) == KE_CSI && c == KS_EXTRA)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001907 buf[i] = CSI;
1908 }
1909 }
1910 --no_mapping;
1911 c = (*mb_ptr2char)(buf);
1912 }
1913
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02001914 if (vgetc_char == 0)
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001915 {
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001916 vgetc_mod_mask = mod_mask;
1917 vgetc_char = c;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001918 }
Bram Moolenaar00eab7f2019-10-10 21:49:28 +02001919
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001920 break;
1921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001923
1924#ifdef FEAT_EVAL
1925 /*
1926 * In the main loop "may_garbage_collect" can be set to do garbage
1927 * collection in the first next vgetc(). It's disabled after that to
1928 * avoid internally used Lists and Dicts to be freed.
1929 */
1930 may_garbage_collect = FALSE;
1931#endif
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001932
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001933#ifdef FEAT_BEVAL_TERM
Bram Moolenaarc2f50542019-07-05 23:24:56 +02001934 if (c != K_MOUSEMOVE && c != K_IGNORE && c != K_CURSORHOLD)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001935 {
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001936 // Don't trigger 'balloonexpr' unless only the mouse was moved.
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001937 bevalexpr_due_set = FALSE;
1938 ui_remove_balloon();
1939 }
1940#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001941#ifdef FEAT_PROP_POPUP
Bram Moolenaar4934ad02020-09-28 22:29:58 +02001942 // Only filter keys that do not come from ":normal". Keys from feedkeys()
1943 // are filtered.
1944 if ((!ex_normal_busy || in_feedkeys) && popup_do_filter(c))
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02001945 {
1946 if (c == Ctrl_C)
1947 got_int = FALSE; // avoid looping
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001948 c = K_IGNORE;
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02001949 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001950#endif
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001951
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001952 // Need to process the character before we know it's safe to do something
1953 // else.
1954 if (c != K_IGNORE)
Bram Moolenaard103ee72019-09-18 21:15:31 +02001955 state_no_longer_safe("key typed");
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001956
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001957 return c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958}
1959
1960/*
1961 * Like vgetc(), but never return a NUL when called recursively, get a key
1962 * directly from the user (ignoring typeahead).
1963 */
1964 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001965safe_vgetc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966{
1967 int c;
1968
1969 c = vgetc();
1970 if (c == NUL)
1971 c = get_keystroke();
1972 return c;
1973}
1974
1975/*
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001976 * Like safe_vgetc(), but loop to handle K_IGNORE.
1977 * Also ignore scrollbar events.
1978 */
1979 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001980plain_vgetc(void)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001981{
1982 int c;
1983
1984 do
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001985 c = safe_vgetc();
Bram Moolenaar3a006592021-05-05 19:58:17 +02001986 while (c == K_IGNORE
1987 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
1988 || c == K_MOUSEMOVE);
Bram Moolenaarec2da362017-01-21 20:04:22 +01001989
1990 if (c == K_PS)
Bram Moolenaar30613902019-12-01 22:11:18 +01001991 // Only handle the first pasted character. Drop the rest, since we
1992 // don't know what to do with it.
Bram Moolenaarec2da362017-01-21 20:04:22 +01001993 c = bracketed_paste(PASTE_ONE_CHAR, FALSE, NULL);
1994
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001995 return c;
1996}
1997
1998/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 * Check if a character is available, such that vgetc() will not block.
2000 * If the next character is a special character or multi-byte, the returned
2001 * character is not valid!.
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02002002 * Returns NUL if no character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 */
2004 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002005vpeekc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006{
2007 if (old_char != -1)
2008 return old_char;
2009 return vgetorpeek(FALSE);
2010}
2011
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01002012#if defined(FEAT_TERMRESPONSE) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013/*
2014 * Like vpeekc(), but don't allow mapping. Do allow checking for terminal
2015 * codes.
2016 */
2017 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002018vpeekc_nomap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019{
2020 int c;
2021
2022 ++no_mapping;
2023 ++allow_keys;
2024 c = vpeekc();
2025 --no_mapping;
2026 --allow_keys;
2027 return c;
2028}
2029#endif
2030
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031/*
2032 * Check if any character is available, also half an escape sequence.
2033 * Trick: when no typeahead found, but there is something in the typeahead
2034 * buffer, it must be an ESC that is recognized as the start of a key code.
2035 */
2036 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002037vpeekc_any(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038{
2039 int c;
2040
2041 c = vpeekc();
2042 if (c == NUL && typebuf.tb_len > 0)
2043 c = ESC;
2044 return c;
2045}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046
2047/*
2048 * Call vpeekc() without causing anything to be mapped.
2049 * Return TRUE if a character is available, FALSE otherwise.
2050 */
2051 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002052char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053{
2054 int retval;
2055
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01002056#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01002057 // When test_override("char_avail", 1) was called pretend there is no
2058 // typeahead.
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01002059 if (disable_char_avail_for_testing)
2060 return FALSE;
2061#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 ++no_mapping;
2063 retval = vpeekc();
2064 --no_mapping;
2065 return (retval != NUL);
2066}
2067
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002068#if defined(FEAT_EVAL) || defined(PROTO)
2069/*
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002070 * "getchar()" and "getcharstr()" functions
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002071 */
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002072 static void
2073getchar_common(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002074{
2075 varnumber_T n;
2076 int error = FALSE;
2077
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002078 if (in_vim9script() && check_for_opt_bool_arg(argvars, 0) == FAIL)
2079 return;
2080
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002081#ifdef MESSAGE_QUEUE
2082 // vpeekc() used to check for messages, but that caused problems, invoking
2083 // a callback where it was not expected. Some plugins use getchar(1) in a
2084 // loop to await a message, therefore make sure we check for messages here.
2085 parse_queued_messages();
2086#endif
2087
Bram Moolenaar30613902019-12-01 22:11:18 +01002088 // Position the cursor. Needed after a message that ends in a space.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002089 windgoto(msg_row, msg_col);
2090
2091 ++no_mapping;
2092 ++allow_keys;
2093 for (;;)
2094 {
2095 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar30613902019-12-01 22:11:18 +01002096 // getchar(): blocking wait.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002097 n = plain_vgetc();
Bram Moolenaarc08cc722020-09-05 17:51:23 +02002098 else if (tv_get_bool_chk(&argvars[0], &error))
Bram Moolenaar30613902019-12-01 22:11:18 +01002099 // getchar(1): only check if char avail
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002100 n = vpeekc_any();
2101 else if (error || vpeekc_any() == NUL)
Bram Moolenaar30613902019-12-01 22:11:18 +01002102 // illegal argument or getchar(0) and no char avail: return zero
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002103 n = 0;
2104 else
Bram Moolenaar15183b42020-09-05 19:59:39 +02002105 // getchar(0) and char avail() != NUL: get a character.
2106 // Note that vpeekc_any() returns K_SPECIAL for K_IGNORE.
2107 n = safe_vgetc();
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002108
Bram Moolenaar15183b42020-09-05 19:59:39 +02002109 if (n == K_IGNORE || n == K_MOUSEMOVE
2110 || n == K_VER_SCROLLBAR || n == K_HOR_SCROLLBAR)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002111 continue;
2112 break;
2113 }
2114 --no_mapping;
2115 --allow_keys;
2116
2117 set_vim_var_nr(VV_MOUSE_WIN, 0);
2118 set_vim_var_nr(VV_MOUSE_WINID, 0);
2119 set_vim_var_nr(VV_MOUSE_LNUM, 0);
2120 set_vim_var_nr(VV_MOUSE_COL, 0);
2121
2122 rettv->vval.v_number = n;
2123 if (IS_SPECIAL(n) || mod_mask != 0)
2124 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002125 char_u temp[10]; // modifier: 3, mbyte-char: 6, NUL: 1
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002126 int i = 0;
2127
Bram Moolenaar30613902019-12-01 22:11:18 +01002128 // Turn a special key into three bytes, plus modifier.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002129 if (mod_mask != 0)
2130 {
2131 temp[i++] = K_SPECIAL;
2132 temp[i++] = KS_MODIFIER;
2133 temp[i++] = mod_mask;
2134 }
2135 if (IS_SPECIAL(n))
2136 {
2137 temp[i++] = K_SPECIAL;
2138 temp[i++] = K_SECOND(n);
2139 temp[i++] = K_THIRD(n);
2140 }
2141 else if (has_mbyte)
2142 i += (*mb_char2bytes)(n, temp + i);
2143 else
2144 temp[i++] = n;
2145 temp[i++] = NUL;
2146 rettv->v_type = VAR_STRING;
2147 rettv->vval.v_string = vim_strsave(temp);
2148
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002149 if (is_mouse_key(n))
2150 {
2151 int row = mouse_row;
2152 int col = mouse_col;
2153 win_T *win;
2154 linenr_T lnum;
2155 win_T *wp;
2156 int winnr = 1;
2157
2158 if (row >= 0 && col >= 0)
2159 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002160 // Find the window at the mouse coordinates and compute the
2161 // text position.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002162 win = mouse_find_win(&row, &col, FIND_POPUP);
2163 if (win == NULL)
2164 return;
2165 (void)mouse_comp_pos(win, &row, &col, &lnum, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002166#ifdef FEAT_PROP_POPUP
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002167 if (WIN_IS_POPUP(win))
2168 winnr = 0;
2169 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002170#endif
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002171 for (wp = firstwin; wp != win && wp != NULL;
2172 wp = wp->w_next)
2173 ++winnr;
2174 set_vim_var_nr(VV_MOUSE_WIN, winnr);
2175 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
2176 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
2177 set_vim_var_nr(VV_MOUSE_COL, col + 1);
2178 }
2179 }
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002180 }
2181}
2182
2183/*
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002184 * "getchar()" function
2185 */
2186 void
2187f_getchar(typval_T *argvars, typval_T *rettv)
2188{
2189 getchar_common(argvars, rettv);
2190}
2191
2192/*
2193 * "getcharstr()" function
2194 */
2195 void
2196f_getcharstr(typval_T *argvars, typval_T *rettv)
2197{
2198 getchar_common(argvars, rettv);
2199
2200 if (rettv->v_type == VAR_NUMBER)
2201 {
2202 char_u temp[7]; // mbyte-char: 6, NUL: 1
2203 varnumber_T n = rettv->vval.v_number;
2204 int i = 0;
2205
2206 if (n != 0)
2207 {
2208 if (has_mbyte)
2209 i += (*mb_char2bytes)(n, temp + i);
2210 else
2211 temp[i++] = n;
2212 }
2213 temp[i++] = NUL;
2214 rettv->v_type = VAR_STRING;
2215 rettv->vval.v_string = vim_strsave(temp);
2216 }
2217}
2218
2219/*
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002220 * "getcharmod()" function
2221 */
2222 void
2223f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
2224{
2225 rettv->vval.v_number = mod_mask;
2226}
2227#endif // FEAT_EVAL
2228
2229#if defined(MESSAGE_QUEUE) || defined(PROTO)
2230# define MAX_REPEAT_PARSE 8
2231
2232/*
2233 * Process messages that have been queued for netbeans or clientserver.
2234 * Also check if any jobs have ended.
h-east965d2ed2021-10-04 21:51:57 +01002235 * These functions can call arbitrary Vim script and should only be called when
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002236 * it is safe to do so.
2237 */
2238 void
2239parse_queued_messages(void)
2240{
Bram Moolenaar1cac7092019-10-22 21:54:31 +02002241 int old_curwin_id;
2242 int old_curbuf_fnum;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002243 int i;
2244 int save_may_garbage_collect = may_garbage_collect;
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002245 static int entered = 0;
Bram Moolenaard103ee72019-09-18 21:15:31 +02002246 int was_safe = get_was_safe_state();
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002247
2248 // Do not handle messages while redrawing, because it may cause buffers to
2249 // change or be wiped while they are being redrawn.
Bram Moolenaar4778b4d2020-11-04 11:03:12 +01002250 // Also bail out when parsing messages was explicitly disabled.
2251 if (updating_screen || dont_parse_messages)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002252 return;
2253
Bram Moolenaar1cac7092019-10-22 21:54:31 +02002254 // If memory allocation fails during startup we'll exit but curbuf or
2255 // curwin could be NULL.
2256 if (curbuf == NULL || curwin == NULL)
2257 return;
2258
2259 old_curbuf_fnum = curbuf->b_fnum;
2260 old_curwin_id = curwin->w_id;
2261
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002262 ++entered;
2263
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002264 // may_garbage_collect is set in main_loop() to do garbage collection when
2265 // blocking to wait on a character. We don't want that while parsing
2266 // messages, a callback may invoke vgetc() while lists and dicts are in use
2267 // in the call stack.
2268 may_garbage_collect = FALSE;
2269
2270 // Loop when a job ended, but don't keep looping forever.
2271 for (i = 0; i < MAX_REPEAT_PARSE; ++i)
2272 {
2273 // For Win32 mch_breakcheck() does not check for input, do it here.
Bram Moolenaar80a8d382020-05-03 22:57:32 +02002274# if (defined(MSWIN) || defined(__HAIKU__)) && defined(FEAT_JOB_CHANNEL)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002275 channel_handle_events(FALSE);
2276# endif
2277
2278# ifdef FEAT_NETBEANS_INTG
2279 // Process the queued netbeans messages.
2280 netbeans_parse_messages();
2281# endif
2282# ifdef FEAT_JOB_CHANNEL
2283 // Write any buffer lines still to be written.
2284 channel_write_any_lines();
2285
2286 // Process the messages queued on channels.
2287 channel_parse_messages();
2288# endif
2289# if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
2290 // Process the queued clientserver messages.
2291 server_parse_messages();
2292# endif
2293# ifdef FEAT_JOB_CHANNEL
2294 // Check if any jobs have ended. If so, repeat the above to handle
2295 // changes, e.g. stdin may have been closed.
2296 if (job_check_ended())
2297 continue;
2298# endif
2299# ifdef FEAT_TERMINAL
2300 free_unused_terminals();
2301# endif
2302# ifdef FEAT_SOUND_CANBERRA
2303 if (has_sound_callback_in_queue())
2304 invoke_sound_callback();
2305# endif
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002306#ifdef SIGUSR1
2307 if (got_sigusr1)
2308 {
2309 apply_autocmds(EVENT_SIGUSR1, NULL, NULL, FALSE, curbuf);
2310 got_sigusr1 = FALSE;
2311 }
2312#endif
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002313 break;
2314 }
2315
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002316 // When not nested we'll go back to waiting for a typed character. If it
2317 // was safe before then this triggers a SafeStateAgain autocommand event.
Bram Moolenaard103ee72019-09-18 21:15:31 +02002318 if (entered == 1 && was_safe)
Bram Moolenaar37d18072019-09-17 20:28:38 +02002319 may_trigger_safestateagain();
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002320
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002321 may_garbage_collect = save_may_garbage_collect;
2322
2323 // If the current window or buffer changed we need to bail out of the
2324 // waiting loop. E.g. when a job exit callback closes the terminal window.
2325 if (curwin->w_id != old_curwin_id || curbuf->b_fnum != old_curbuf_fnum)
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02002326 ins_char_typebuf(K_IGNORE, 0);
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002327
2328 --entered;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002329}
2330#endif
2331
2332
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002333typedef enum {
2334 map_result_fail, // failed, break loop
2335 map_result_get, // get a character from typeahead
2336 map_result_retry, // try to map again
2337 map_result_nomatch // no matching mapping, get char
2338} map_result_T;
2339
2340/*
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002341 * Check if the bytes at the start of the typeahead buffer are a character used
2342 * in CTRL-X mode. This includes the form with a CTRL modifier.
2343 */
2344 static int
2345at_ctrl_x_key(void)
2346{
2347 char_u *p = typebuf.tb_buf + typebuf.tb_off;
2348 int c = *p;
2349
2350 if (typebuf.tb_len > 3
2351 && c == K_SPECIAL
2352 && p[1] == KS_MODIFIER
2353 && (p[2] & MOD_MASK_CTRL))
2354 c = p[3] & 0x1f;
2355 return vim_is_ctrl_x_key(c);
2356}
2357
2358/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002359 * Check if typebuf.tb_buf[] contains a modifier plus key that can be changed
Bram Moolenaar975a8802020-06-06 22:36:24 +02002360 * into just a key, apply that.
2361 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
2362 * + "max_offset"].
2363 * Return the length of the replaced bytes, zero if nothing changed.
2364 */
2365 static int
2366check_simplify_modifier(int max_offset)
2367{
2368 int offset;
2369 char_u *tp;
2370
2371 for (offset = 0; offset < max_offset; ++offset)
2372 {
2373 if (offset + 3 >= typebuf.tb_len)
2374 break;
2375 tp = typebuf.tb_buf + typebuf.tb_off + offset;
2376 if (tp[0] == K_SPECIAL && tp[1] == KS_MODIFIER)
2377 {
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002378 // A modifier was not used for a mapping, apply it to ASCII keys.
2379 // Shift would already have been applied.
Bram Moolenaar975a8802020-06-06 22:36:24 +02002380 int modifier = tp[2];
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002381 int c = tp[3];
2382 int new_c = merge_modifyOtherKeys(c, &modifier);
Bram Moolenaar975a8802020-06-06 22:36:24 +02002383
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002384 if (new_c != c)
Bram Moolenaar975a8802020-06-06 22:36:24 +02002385 {
2386 char_u new_string[MB_MAXBYTES];
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002387 int len;
Bram Moolenaar975a8802020-06-06 22:36:24 +02002388
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002389 if (offset == 0)
2390 {
2391 // At the start: remember the character and mod_mask before
2392 // merging, in some cases, e.g. at the hit-return prompt,
2393 // they are put back in the typeahead buffer.
2394 vgetc_char = c;
2395 vgetc_mod_mask = tp[2];
2396 }
2397 len = mb_char2bytes(new_c, new_string);
2398 if (modifier == 0)
2399 {
2400 if (put_string_in_typebuf(offset, 4, new_string, len,
Bram Moolenaar975a8802020-06-06 22:36:24 +02002401 NULL, 0, 0) == FAIL)
2402 return -1;
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002403 }
2404 else
2405 {
2406 tp[2] = modifier;
2407 if (put_string_in_typebuf(offset + 3, 1, new_string, len,
2408 NULL, 0, 0) == FAIL)
2409 return -1;
2410 }
Bram Moolenaar975a8802020-06-06 22:36:24 +02002411 return len;
2412 }
2413 }
2414 }
2415 return 0;
2416}
2417
2418/*
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002419 * Handle mappings in the typeahead buffer.
2420 * - When something was mapped, return map_result_retry for recursive mappings.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002421 * - When nothing mapped and typeahead has a character: return map_result_get.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002422 * - When there is no match yet, return map_result_nomatch, need to get more
2423 * typeahead.
2424 */
2425 static int
2426handle_mapping(
2427 int *keylenp,
2428 int *timedout,
2429 int *mapdepth)
2430{
2431 mapblock_T *mp = NULL;
2432 mapblock_T *mp2;
2433 mapblock_T *mp_match;
2434 int mp_match_len = 0;
2435 int max_mlen = 0;
2436 int tb_c1;
2437 int mlen;
2438#ifdef FEAT_LANGMAP
2439 int nolmaplen;
2440#endif
2441 int keylen = *keylenp;
2442 int i;
2443 int local_State = get_real_state();
2444
2445 /*
2446 * Check for a mappable key sequence.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002447 * Walk through one maphash[] list until we find an entry that matches.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002448 *
2449 * Don't look for mappings if:
2450 * - no_mapping set: mapping disabled (e.g. for CTRL-V)
2451 * - maphash_valid not set: no mappings present.
2452 * - typebuf.tb_buf[typebuf.tb_off] should not be remapped
2453 * - in insert or cmdline mode and 'paste' option set
Bram Moolenaareda35f72019-08-03 14:59:44 +02002454 * - waiting for "hit return to continue" and CR or SPACE typed
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002455 * - waiting for a char with --more--
2456 * - in Ctrl-X mode, and we get a valid char for that mode
2457 */
2458 tb_c1 = typebuf.tb_buf[typebuf.tb_off];
2459 if (no_mapping == 0 && is_maphash_valid()
2460 && (no_zero_mapping == 0 || tb_c1 != '0')
2461 && (typebuf.tb_maplen == 0
2462 || (p_remap
2463 && (typebuf.tb_noremap[typebuf.tb_off]
2464 & (RM_NONE|RM_ABBR)) == 0))
2465 && !(p_paste && (State & (INSERT + CMDLINE)))
2466 && !(State == HITRETURN && (tb_c1 == CAR || tb_c1 == ' '))
2467 && State != ASKMORE
2468 && State != CONFIRM
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002469 && !((ctrl_x_mode_not_default() && at_ctrl_x_key())
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002470 || (compl_status_local()
Bram Moolenaare2c453d2019-08-21 14:37:09 +02002471 && (tb_c1 == Ctrl_N || tb_c1 == Ctrl_P))))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002472 {
Bram Moolenaarc9983702020-05-28 21:03:53 +02002473#ifdef FEAT_GUI
2474 if (gui.in_use && tb_c1 == CSI && typebuf.tb_len >= 2
2475 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_MODIFIER)
2476 {
2477 // The GUI code sends CSI KS_MODIFIER {flags}, but mappings expect
2478 // K_SPECIAL KS_MODIFIER {flags}.
2479 tb_c1 = K_SPECIAL;
2480 }
2481#endif
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002482#ifdef FEAT_LANGMAP
2483 if (tb_c1 == K_SPECIAL)
2484 nolmaplen = 2;
2485 else
2486 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002487 LANGMAP_ADJUST(tb_c1, (State & (CMDLINE | INSERT)) == 0
2488 && get_real_state() != SELECTMODE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002489 nolmaplen = 0;
2490 }
2491#endif
2492 // First try buffer-local mappings.
2493 mp = get_buf_maphash_list(local_State, tb_c1);
2494 mp2 = get_maphash_list(local_State, tb_c1);
2495 if (mp == NULL)
2496 {
2497 // There are no buffer-local mappings.
2498 mp = mp2;
2499 mp2 = NULL;
2500 }
Bram Moolenaareda35f72019-08-03 14:59:44 +02002501
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002502 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002503 * Loop until a partly matching mapping is found or all (local)
2504 * mappings have been checked.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002505 * The longest full match is remembered in "mp_match".
Bram Moolenaareda35f72019-08-03 14:59:44 +02002506 * A full match is only accepted if there is no partly match, so "aa"
2507 * and "aaa" can both be mapped.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002508 */
2509 mp_match = NULL;
2510 mp_match_len = 0;
2511 for ( ; mp != NULL;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002512 mp->m_next == NULL ? (mp = mp2, mp2 = NULL) : (mp = mp->m_next))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002513 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002514 // Only consider an entry if the first character matches and it is
2515 // for the current state.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002516 // Skip ":lmap" mappings if keys were mapped.
2517 if (mp->m_keys[0] == tb_c1
2518 && (mp->m_mode & local_State)
Bram Moolenaar46cd43b2020-06-04 22:22:11 +02002519 && !(mp->m_simplified && seenModifyOtherKeys
2520 && typebuf.tb_maplen == 0)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002521 && ((mp->m_mode & LANGMAP) == 0 || typebuf.tb_maplen == 0))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002522 {
2523#ifdef FEAT_LANGMAP
2524 int nomap = nolmaplen;
2525 int c2;
2526#endif
2527 // find the match length of this mapping
2528 for (mlen = 1; mlen < typebuf.tb_len; ++mlen)
2529 {
2530#ifdef FEAT_LANGMAP
2531 c2 = typebuf.tb_buf[typebuf.tb_off + mlen];
2532 if (nomap > 0)
2533 --nomap;
2534 else if (c2 == K_SPECIAL)
2535 nomap = 2;
2536 else
2537 LANGMAP_ADJUST(c2, TRUE);
2538 if (mp->m_keys[mlen] != c2)
2539#else
2540 if (mp->m_keys[mlen] !=
Bram Moolenaarc9983702020-05-28 21:03:53 +02002541 typebuf.tb_buf[typebuf.tb_off + mlen])
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002542#endif
2543 break;
2544 }
2545
Bram Moolenaareda35f72019-08-03 14:59:44 +02002546 // Don't allow mapping the first byte(s) of a multi-byte char.
2547 // Happens when mapping <M-a> and then changing 'encoding'.
2548 // Beware that 0x80 is escaped.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002549 {
2550 char_u *p1 = mp->m_keys;
2551 char_u *p2 = mb_unescape(&p1);
2552
2553 if (has_mbyte && p2 != NULL
Bram Moolenaar1614a142019-10-06 22:00:13 +02002554 && MB_BYTE2LEN(tb_c1) > mb_ptr2len(p2))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002555 mlen = 0;
2556 }
2557
2558 // Check an entry whether it matches.
2559 // - Full match: mlen == keylen
2560 // - Partly match: mlen == typebuf.tb_len
2561 keylen = mp->m_keylen;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002562 if (mlen == keylen || (mlen == typebuf.tb_len
2563 && typebuf.tb_len < keylen))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002564 {
2565 char_u *s;
2566 int n;
2567
Bram Moolenaareda35f72019-08-03 14:59:44 +02002568 // If only script-local mappings are allowed, check if the
2569 // mapping starts with K_SNR.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002570 s = typebuf.tb_noremap + typebuf.tb_off;
2571 if (*s == RM_SCRIPT
2572 && (mp->m_keys[0] != K_SPECIAL
2573 || mp->m_keys[1] != KS_EXTRA
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01002574 || mp->m_keys[2] != KE_SNR))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002575 continue;
2576
Bram Moolenaareda35f72019-08-03 14:59:44 +02002577 // If one of the typed keys cannot be remapped, skip the
2578 // entry.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002579 for (n = mlen; --n >= 0; )
2580 if (*s++ & (RM_NONE|RM_ABBR))
2581 break;
2582 if (n >= 0)
2583 continue;
2584
2585 if (keylen > typebuf.tb_len)
2586 {
2587 if (!*timedout && !(mp_match != NULL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002588 && mp_match->m_nowait))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002589 {
2590 // break at a partly match
2591 keylen = KEYLEN_PART_MAP;
2592 break;
2593 }
2594 }
2595 else if (keylen > mp_match_len)
2596 {
2597 // found a longer match
2598 mp_match = mp;
2599 mp_match_len = keylen;
2600 }
2601 }
2602 else
Bram Moolenaareda35f72019-08-03 14:59:44 +02002603 // No match; may have to check for termcode at next
2604 // character.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002605 if (max_mlen < mlen)
2606 max_mlen = mlen;
2607 }
2608 }
2609
Bram Moolenaareda35f72019-08-03 14:59:44 +02002610 // If no partly match found, use the longest full match.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002611 if (keylen != KEYLEN_PART_MAP)
2612 {
2613 mp = mp_match;
2614 keylen = mp_match_len;
2615 }
2616 }
2617
2618 /*
2619 * Check for match with 'pastetoggle'
2620 */
2621 if (*p_pt != NUL && mp == NULL && (State & (INSERT|NORMAL)))
2622 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002623 for (mlen = 0; mlen < typebuf.tb_len && p_pt[mlen]; ++mlen)
2624 if (p_pt[mlen] != typebuf.tb_buf[typebuf.tb_off + mlen])
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002625 break;
2626 if (p_pt[mlen] == NUL) // match
2627 {
2628 // write chars to script file(s)
2629 if (mlen > typebuf.tb_maplen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002630 gotchars(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_maplen,
2631 mlen - typebuf.tb_maplen);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002632
2633 del_typebuf(mlen, 0); // remove the chars
Bram Moolenaareda35f72019-08-03 14:59:44 +02002634 set_option_value((char_u *)"paste", (long)!p_paste, NULL, 0);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002635 if (!(State & INSERT))
2636 {
2637 msg_col = 0;
2638 msg_row = Rows - 1;
2639 msg_clr_eos(); // clear ruler
2640 }
2641 status_redraw_all();
2642 redraw_statuslines();
2643 showmode();
2644 setcursor();
2645 *keylenp = keylen;
2646 return map_result_retry;
2647 }
2648 // Need more chars for partly match.
2649 if (mlen == typebuf.tb_len)
2650 keylen = KEYLEN_PART_KEY;
2651 else if (max_mlen < mlen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002652 // no match, may have to check for termcode at next character
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002653 max_mlen = mlen + 1;
2654 }
2655
Bram Moolenaareda35f72019-08-03 14:59:44 +02002656 if ((mp == NULL || max_mlen >= mp_match_len) && keylen != KEYLEN_PART_MAP)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002657 {
2658 int save_keylen = keylen;
2659
2660 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002661 * When no matching mapping found or found a non-matching mapping that
2662 * matches at least what the matching mapping matched:
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002663 * Check if we have a terminal code, when:
Bram Moolenaareda35f72019-08-03 14:59:44 +02002664 * - mapping is allowed,
2665 * - keys have not been mapped,
2666 * - and not an ESC sequence, not in insert mode or p_ek is on,
2667 * - and when not timed out,
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002668 */
2669 if ((no_mapping == 0 || allow_keys != 0)
2670 && (typebuf.tb_maplen == 0
2671 || (p_remap && typebuf.tb_noremap[
Bram Moolenaareda35f72019-08-03 14:59:44 +02002672 typebuf.tb_off] == RM_YES))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002673 && !*timedout)
2674 {
Bram Moolenaar0684e362020-12-03 19:54:42 +01002675 keylen = check_termcode(max_mlen + 1, NULL, 0, NULL);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002676
Bram Moolenaar0684e362020-12-03 19:54:42 +01002677 // If no termcode matched but 'pastetoggle' matched partially
2678 // it's like an incomplete key sequence.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002679 if (keylen == 0 && save_keylen == KEYLEN_PART_KEY)
2680 keylen = KEYLEN_PART_KEY;
2681
Bram Moolenaar975a8802020-06-06 22:36:24 +02002682 // If no termcode matched, try to include the modifier into the
zeertzjqa3f83fe2021-11-22 12:47:39 +00002683 // key. This is for when modifyOtherKeys is working.
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002684 if (keylen == 0 && !no_reduce_keys)
Bram Moolenaar975a8802020-06-06 22:36:24 +02002685 keylen = check_simplify_modifier(max_mlen + 1);
2686
Bram Moolenaareda35f72019-08-03 14:59:44 +02002687 // When getting a partial match, but the last characters were not
2688 // typed, don't wait for a typed character to complete the
2689 // termcode. This helps a lot when a ":normal" command ends in an
2690 // ESC.
2691 if (keylen < 0 && typebuf.tb_len == typebuf.tb_maplen)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002692 keylen = 0;
2693 }
2694 else
2695 keylen = 0;
2696 if (keylen == 0) // no matching terminal code
2697 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002698#ifdef AMIGA
2699 // check for window bounds report
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002700 if (typebuf.tb_maplen == 0 && (typebuf.tb_buf[
Bram Moolenaareda35f72019-08-03 14:59:44 +02002701 typebuf.tb_off] & 0xff) == CSI)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002702 {
2703 char_u *s;
2704
2705 for (s = typebuf.tb_buf + typebuf.tb_off + 1;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002706 s < typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len
2707 && (VIM_ISDIGIT(*s) || *s == ';' || *s == ' ');
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002708 ++s)
2709 ;
2710 if (*s == 'r' || *s == '|') // found one
2711 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002712 del_typebuf(
2713 (int)(s + 1 - (typebuf.tb_buf + typebuf.tb_off)), 0);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002714 // get size and redraw screen
2715 shell_resized();
2716 *keylenp = keylen;
2717 return map_result_retry;
2718 }
2719 if (*s == NUL) // need more characters
2720 keylen = KEYLEN_PART_KEY;
2721 }
2722 if (keylen >= 0)
2723#endif
Bram Moolenaareda35f72019-08-03 14:59:44 +02002724 // When there was a matching mapping and no termcode could be
2725 // replaced after another one, use that mapping (loop around).
2726 // If there was no mapping at all use the character from the
2727 // typeahead buffer right here.
2728 if (mp == NULL)
2729 {
2730 *keylenp = keylen;
Bram Moolenaar52797ba2021-12-16 14:45:13 +00002731 return map_result_get; // get character from typeahead
Bram Moolenaareda35f72019-08-03 14:59:44 +02002732 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002733 }
2734
2735 if (keylen > 0) // full matching terminal code
2736 {
2737#if defined(FEAT_GUI) && defined(FEAT_MENU)
2738 if (typebuf.tb_len >= 2
Bram Moolenaareda35f72019-08-03 14:59:44 +02002739 && typebuf.tb_buf[typebuf.tb_off] == K_SPECIAL
2740 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_MENU)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002741 {
2742 int idx;
2743
Bram Moolenaareda35f72019-08-03 14:59:44 +02002744 // Using a menu may cause a break in undo! It's like using
2745 // gotchars(), but without recording or writing to a script
2746 // file.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002747 may_sync_undo();
2748 del_typebuf(3, 0);
2749 idx = get_menu_index(current_menu, local_State);
2750 if (idx != MENU_INDEX_INVALID)
2751 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002752 // In Select mode and a Visual mode menu is used: Switch
2753 // to Visual mode temporarily. Append K_SELECT to switch
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002754 // back to Select mode.
2755 if (VIsual_active && VIsual_select
Bram Moolenaareda35f72019-08-03 14:59:44 +02002756 && (current_menu->modes & VISUAL))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002757 {
2758 VIsual_select = FALSE;
2759 (void)ins_typebuf(K_SELECT_STRING,
Bram Moolenaareda35f72019-08-03 14:59:44 +02002760 REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002761 }
2762 ins_typebuf(current_menu->strings[idx],
2763 current_menu->noremap[idx],
Bram Moolenaareda35f72019-08-03 14:59:44 +02002764 0, TRUE, current_menu->silent[idx]);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002765 }
2766 }
2767#endif // FEAT_GUI && FEAT_MENU
2768 *keylenp = keylen;
2769 return map_result_retry; // try mapping again
2770 }
2771
Bram Moolenaareda35f72019-08-03 14:59:44 +02002772 // Partial match: get some more characters. When a matching mapping
2773 // was found use that one.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002774 if (mp == NULL || keylen < 0)
2775 keylen = KEYLEN_PART_KEY;
2776 else
2777 keylen = mp_match_len;
2778 }
2779
2780 /*
2781 * complete match
2782 */
2783 if (keylen >= 0 && keylen <= typebuf.tb_len)
2784 {
2785 char_u *map_str;
2786
2787#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002788 int save_m_expr;
2789 int save_m_noremap;
2790 int save_m_silent;
2791 char_u *save_m_keys;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002792#else
2793# define save_m_noremap mp->m_noremap
2794# define save_m_silent mp->m_silent
2795#endif
2796
2797 // write chars to script file(s)
2798 if (keylen > typebuf.tb_maplen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002799 gotchars(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_maplen,
2800 keylen - typebuf.tb_maplen);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002801
2802 cmd_silent = (typebuf.tb_silent > 0);
2803 del_typebuf(keylen, 0); // remove the mapped keys
2804
2805 /*
2806 * Put the replacement string in front of mapstr.
2807 * The depth check catches ":map x y" and ":map y x".
2808 */
2809 if (++*mapdepth >= p_mmd)
2810 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00002811 emsg(_(e_recursive_mapping));
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002812 if (State & CMDLINE)
2813 redrawcmdline();
2814 else
2815 setcursor();
2816 flush_buffers(FLUSH_MINIMAL);
Bram Moolenaar30613902019-12-01 22:11:18 +01002817 *mapdepth = 0; // for next one
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002818 *keylenp = keylen;
2819 return map_result_fail;
2820 }
2821
2822 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002823 * In Select mode and a Visual mode mapping is used: Switch to Visual
2824 * mode temporarily. Append K_SELECT to switch back to Select mode.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002825 */
Bram Moolenaareda35f72019-08-03 14:59:44 +02002826 if (VIsual_active && VIsual_select && (mp->m_mode & VISUAL))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002827 {
2828 VIsual_select = FALSE;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002829 (void)ins_typebuf(K_SELECT_STRING, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002830 }
2831
2832#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002833 // Copy the values from *mp that are used, because evaluating the
2834 // expression may invoke a function that redefines the mapping, thereby
2835 // making *mp invalid.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002836 save_m_expr = mp->m_expr;
2837 save_m_noremap = mp->m_noremap;
2838 save_m_silent = mp->m_silent;
2839 save_m_keys = NULL; // only saved when needed
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002840
2841 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002842 * Handle ":map <expr>": evaluate the {rhs} as an expression. Also
2843 * save and restore the command line for "normal :".
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002844 */
2845 if (mp->m_expr)
2846 {
2847 int save_vgetc_busy = vgetc_busy;
2848 int save_may_garbage_collect = may_garbage_collect;
Bram Moolenaar4ebe0e62019-11-22 20:55:40 +01002849 int was_screen_col = screen_cur_col;
2850 int was_screen_row = screen_cur_row;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002851
2852 vgetc_busy = 0;
2853 may_garbage_collect = FALSE;
2854
2855 save_m_keys = vim_strsave(mp->m_keys);
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002856 map_str = eval_map_expr(mp, NUL);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002857
Bram Moolenaar4ebe0e62019-11-22 20:55:40 +01002858 // The mapping may do anything, but we expect it to take care of
2859 // redrawing. Do put the cursor back where it was.
2860 windgoto(was_screen_row, was_screen_col);
2861 out_flush();
2862
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002863 vgetc_busy = save_vgetc_busy;
2864 may_garbage_collect = save_may_garbage_collect;
2865 }
2866 else
2867#endif
2868 map_str = mp->m_str;
2869
2870 /*
2871 * Insert the 'to' part in the typebuf.tb_buf.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002872 * If 'from' field is the same as the start of the 'to' field, don't
2873 * remap the first character (but do allow abbreviations).
2874 * If m_noremap is set, don't remap the whole 'to' part.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002875 */
2876 if (map_str == NULL)
2877 i = FAIL;
2878 else
2879 {
2880 int noremap;
2881
2882 if (save_m_noremap != REMAP_YES)
2883 noremap = save_m_noremap;
2884 else if (
2885#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002886 STRNCMP(map_str, save_m_keys != NULL ? save_m_keys : mp->m_keys,
2887 (size_t)keylen)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002888#else
2889 STRNCMP(map_str, mp->m_keys, (size_t)keylen)
2890#endif
2891 != 0)
2892 noremap = REMAP_YES;
2893 else
2894 noremap = REMAP_SKIP;
2895 i = ins_typebuf(map_str, noremap,
2896 0, TRUE, cmd_silent || save_m_silent);
2897#ifdef FEAT_EVAL
2898 if (save_m_expr)
2899 vim_free(map_str);
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002900 last_used_map = mp;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002901#endif
2902 }
2903#ifdef FEAT_EVAL
2904 vim_free(save_m_keys);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002905#endif
2906 *keylenp = keylen;
2907 if (i == FAIL)
2908 return map_result_fail;
2909 return map_result_retry;
2910 }
2911
2912 *keylenp = keylen;
2913 return map_result_nomatch;
2914}
2915
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002916/*
2917 * unget one character (can only be done once!)
2918 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002920vungetc(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921{
2922 old_char = c;
2923 old_mod_mask = mod_mask;
Bram Moolenaarb8978712013-03-16 21:42:16 +01002924 old_mouse_row = mouse_row;
2925 old_mouse_col = mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926}
2927
2928/*
Bram Moolenaar32526b32019-01-19 17:43:09 +01002929 * Get a byte:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 * 1. from the stuffbuffer
2931 * This is used for abbreviated commands like "D" -> "d$".
2932 * Also used to redo a command for ".".
2933 * 2. from the typeahead buffer
2934 * Stores text obtained previously but not used yet.
2935 * Also stores the result of mappings.
2936 * Also used for the ":normal" command.
2937 * 3. from the user
2938 * This may do a blocking wait if "advance" is TRUE.
2939 *
2940 * if "advance" is TRUE (vgetc()):
Bram Moolenaard29459b2016-08-26 22:29:11 +02002941 * Really get the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 * KeyTyped is set to TRUE in the case the user typed the key.
2943 * KeyStuffed is TRUE if the character comes from the stuff buffer.
2944 * if "advance" is FALSE (vpeekc()):
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02002945 * Just look whether there is a character available.
2946 * Return NUL if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 *
2948 * When "no_mapping" is zero, checks for mappings in the current mode.
2949 * Only returns one byte (of a multi-byte character).
2950 * K_SPECIAL and CSI may be escaped, need to get two more bytes then.
2951 */
2952 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002953vgetorpeek(int advance)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954{
2955 int c, c1;
Bram Moolenaar30613902019-12-01 22:11:18 +01002956 int timedout = FALSE; // waited for more than 1 second
2957 // for mapping to complete
2958 int mapdepth = 0; // check for recursive mapping
2959 int mode_deleted = FALSE; // set when mode has been deleted
Bram Moolenaar4e427192006-03-10 21:34:27 +00002960#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 int new_wcol, new_wrow;
2962#endif
2963#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01002964 int shape_changed = FALSE; // adjusted cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965#endif
2966 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 int old_wcol, old_wrow;
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00002968 int wait_tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969
2970 /*
2971 * This function doesn't work very well when called recursively. This may
2972 * happen though, because of:
2973 * 1. The call to add_to_showcmd(). char_avail() is then used to check if
2974 * there is a character available, which calls this function. In that
2975 * case we must return NUL, to indicate no character is available.
2976 * 2. A GUI callback function writes to the screen, causing a
2977 * wait_return().
2978 * Using ":normal" can also do this, but it saves the typeahead buffer,
2979 * thus it should be OK. But don't get a key from the user then.
2980 */
Bram Moolenaare2c38102016-01-31 14:55:40 +01002981 if (vgetc_busy > 0 && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 return NUL;
2983
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002984 ++vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985
2986 if (advance)
2987 KeyStuffed = FALSE;
2988
2989 init_typebuf();
2990 start_stuff();
2991 if (advance && typebuf.tb_maplen == 0)
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02002992 reg_executing = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 do
2994 {
2995/*
2996 * get a character: 1. from the stuffbuffer
2997 */
2998 if (typeahead_char != 0)
2999 {
3000 c = typeahead_char;
3001 if (advance)
3002 typeahead_char = 0;
3003 }
3004 else
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01003005 c = read_readbuffers(advance);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 if (c != NUL && !got_int)
3007 {
3008 if (advance)
3009 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003010 // KeyTyped = FALSE; When the command that stuffed something
3011 // was typed, behave like the stuffed command was typed.
3012 // needed for CTRL-W CTRL-] to open a fold, for example.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 KeyStuffed = TRUE;
3014 }
3015 if (typebuf.tb_no_abbr_cnt == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003016 typebuf.tb_no_abbr_cnt = 1; // no abbreviations now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 }
3018 else
3019 {
3020 /*
3021 * Loop until we either find a matching mapped key, or we
3022 * are sure that it is not a mapped key.
3023 * If a mapped key sequence is found we go back to the start to
3024 * try re-mapping.
3025 */
3026 for (;;)
3027 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003028 long wait_time;
3029 int keylen = 0;
Bram Moolenaareda35f72019-08-03 14:59:44 +02003030#ifdef FEAT_CMDL_INFO
3031 int showcmd_idx;
3032#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 /*
3034 * ui_breakcheck() is slow, don't use it too often when
3035 * inside a mapping. But call it each time for typed
3036 * characters.
3037 */
3038 if (typebuf.tb_maplen)
3039 line_breakcheck();
3040 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003041 ui_breakcheck(); // check for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 if (got_int)
3043 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003044 // flush all input
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003045 c = inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 0L);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003046
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 /*
3048 * If inchar() returns TRUE (script file was active) or we
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003049 * are inside a mapping, get out of Insert mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 * Otherwise we behave like having gotten a CTRL-C.
3051 * As a result typing CTRL-C in insert mode will
3052 * really insert a CTRL-C.
3053 */
3054 if ((c || typebuf.tb_maplen)
3055 && (State & (INSERT + CMDLINE)))
3056 c = ESC;
3057 else
3058 c = Ctrl_C;
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02003059 flush_buffers(FLUSH_INPUT); // flush all typeahead
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060
Bram Moolenaard9dfd572006-10-03 13:36:13 +00003061 if (advance)
3062 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003063 // Also record this character, it might be needed to
3064 // get out of Insert mode.
Bram Moolenaard9dfd572006-10-03 13:36:13 +00003065 *typebuf.tb_buf = c;
3066 gotchars(typebuf.tb_buf, 1);
3067 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 cmd_silent = FALSE;
3069
3070 break;
3071 }
3072 else if (typebuf.tb_len > 0)
3073 {
3074 /*
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003075 * Check for a mapping in "typebuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 */
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003077 map_result_T result = handle_mapping(
3078 &keylen, &timedout, &mapdepth);
3079
3080 if (result == map_result_retry)
3081 // try mapping again
3082 continue;
3083 if (result == map_result_fail)
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003084 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003085 // failed, use the outer loop
3086 c = -1;
3087 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003089 if (result == map_result_get)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091/*
3092 * get a character: 2. from the typeahead buffer
3093 */
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003094 c = typebuf.tb_buf[typebuf.tb_off];
Bram Moolenaar30613902019-12-01 22:11:18 +01003095 if (advance) // remove chars from tb_buf
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003096 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003097 cmd_silent = (typebuf.tb_silent > 0);
3098 if (typebuf.tb_maplen > 0)
3099 KeyTyped = FALSE;
3100 else
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003101 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003102 KeyTyped = TRUE;
Bram Moolenaar30613902019-12-01 22:11:18 +01003103 // write char to script file(s)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003104 gotchars(typebuf.tb_buf
3105 + typebuf.tb_off, 1);
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003106 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003107 KeyNoremap = typebuf.tb_noremap[
3108 typebuf.tb_off];
3109 del_typebuf(1, 0);
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003110 }
Bram Moolenaar52797ba2021-12-16 14:45:13 +00003111 break; // got character, break the for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 }
3113
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003114 // not enough characters, get more
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 }
3116
3117/*
3118 * get a character: 3. from the user - handle <Esc> in Insert mode
3119 */
3120 /*
Bram Moolenaarf085f422017-06-07 20:39:47 +02003121 * Special case: if we get an <ESC> in insert mode and there
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 * are no more characters at once, we pretend to go out of
3123 * insert mode. This prevents the one second delay after
3124 * typing an <ESC>. If we get something after all, we may
3125 * have to redisplay the mode. That the cursor is in the wrong
3126 * place does not matter.
3127 */
3128 c = 0;
3129#ifdef FEAT_CMDL_INFO
3130 new_wcol = curwin->w_wcol;
3131 new_wrow = curwin->w_wrow;
3132#endif
3133 if ( advance
3134 && typebuf.tb_len == 1
3135 && typebuf.tb_buf[typebuf.tb_off] == ESC
3136 && !no_mapping
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 && ex_normal_busy == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 && typebuf.tb_maplen == 0
3139 && (State & INSERT)
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003140 && (p_timeout
3141 || (keylen == KEYLEN_PART_KEY && p_ttimeout))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 && (c = inchar(typebuf.tb_buf + typebuf.tb_off
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003143 + typebuf.tb_len, 3, 25L)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 {
3145 colnr_T col = 0, vcol;
3146 char_u *ptr;
3147
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003148 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 {
3150 unshowmode(TRUE);
3151 mode_deleted = TRUE;
3152 }
3153#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003154 // may show a different cursor shape
Bram Moolenaarf085f422017-06-07 20:39:47 +02003155 if (gui.in_use && State != NORMAL && !cmd_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 {
3157 int save_State;
3158
3159 save_State = State;
3160 State = NORMAL;
3161 gui_update_cursor(TRUE, FALSE);
3162 State = save_State;
3163 shape_changed = TRUE;
3164 }
3165#endif
3166 validate_cursor();
3167 old_wcol = curwin->w_wcol;
3168 old_wrow = curwin->w_wrow;
3169
Bram Moolenaar30613902019-12-01 22:11:18 +01003170 // move cursor left, if possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 if (curwin->w_cursor.col != 0)
3172 {
3173 if (curwin->w_wcol > 0)
3174 {
3175 if (did_ai)
3176 {
3177 /*
3178 * We are expecting to truncate the trailing
3179 * white-space, so find the last non-white
3180 * character -- webb
3181 */
3182 col = vcol = curwin->w_wcol = 0;
3183 ptr = ml_get_curline();
3184 while (col < curwin->w_cursor.col)
3185 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01003186 if (!VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 curwin->w_wcol = vcol;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003188 vcol += lbr_chartabsize(ptr, ptr + col,
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01003189 vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003191 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 ++col;
3194 }
3195 curwin->w_wrow = curwin->w_cline_row
Bram Moolenaar02631462017-09-22 15:20:32 +02003196 + curwin->w_wcol / curwin->w_width;
3197 curwin->w_wcol %= curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 curwin->w_wcol += curwin_col_off();
Bram Moolenaar30613902019-12-01 22:11:18 +01003199 col = 0; // no correction needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 }
3201 else
3202 {
3203 --curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 col = curwin->w_cursor.col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 }
3206 }
3207 else if (curwin->w_p_wrap && curwin->w_wrow)
3208 {
3209 --curwin->w_wrow;
Bram Moolenaar02631462017-09-22 15:20:32 +02003210 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 col = curwin->w_cursor.col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 if (has_mbyte && col > 0 && curwin->w_wcol > 0)
3214 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003215 // Correct when the cursor is on the right halve
3216 // of a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 ptr = ml_get_curline();
3218 col -= (*mb_head_off)(ptr, ptr + col);
3219 if ((*mb_ptr2cells)(ptr + col) > 1)
3220 --curwin->w_wcol;
3221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 }
3223 setcursor();
3224 out_flush();
3225#ifdef FEAT_CMDL_INFO
3226 new_wcol = curwin->w_wcol;
3227 new_wrow = curwin->w_wrow;
3228#endif
3229 curwin->w_wcol = old_wcol;
3230 curwin->w_wrow = old_wrow;
3231 }
3232 if (c < 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003233 continue; // end of input script reached
Bram Moolenaar20c38922014-07-23 20:41:14 +02003234
Bram Moolenaar30613902019-12-01 22:11:18 +01003235 // Allow mapping for just typed characters. When we get here c
3236 // is the number of extra bytes and typebuf.tb_len is 1.
Bram Moolenaar20c38922014-07-23 20:41:14 +02003237 for (n = 1; n <= c; ++n)
3238 typebuf.tb_noremap[typebuf.tb_off + n] = RM_YES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 typebuf.tb_len += c;
3240
Bram Moolenaar30613902019-12-01 22:11:18 +01003241 // buffer full, don't map
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 if (typebuf.tb_len >= typebuf.tb_maplen + MAXMAPLEN)
3243 {
3244 timedout = TRUE;
3245 continue;
3246 }
3247
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 if (ex_normal_busy > 0)
3249 {
Bram Moolenaare2c38102016-01-31 14:55:40 +01003250#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 static int tc = 0;
Bram Moolenaare2c38102016-01-31 14:55:40 +01003252#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253
Bram Moolenaar30613902019-12-01 22:11:18 +01003254 // No typeahead left and inside ":normal". Must return
3255 // something to avoid getting stuck. When an incomplete
3256 // mapping is present, behave like it timed out.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 if (typebuf.tb_len > 0)
3258 {
3259 timedout = TRUE;
3260 continue;
3261 }
Bram Moolenaar189832b2020-09-23 12:29:11 +02003262
Bram Moolenaar30613902019-12-01 22:11:18 +01003263 // When 'insertmode' is set, ESC just beeps in Insert
3264 // mode. Use CTRL-L to make edit() return.
3265 // For the command line only CTRL-C always breaks it.
3266 // For the cmdline window: Alternate between ESC and
3267 // CTRL-C: ESC for most situations and CTRL-C to close the
3268 // cmdline window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 if (p_im && (State & INSERT))
3270 c = Ctrl_L;
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003271#ifdef FEAT_TERMINAL
3272 else if (terminal_is_active())
3273 c = K_CANCEL;
3274#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 else if ((State & CMDLINE)
Bram Moolenaare2c38102016-01-31 14:55:40 +01003276#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 || (cmdwin_type > 0 && tc == ESC)
Bram Moolenaare2c38102016-01-31 14:55:40 +01003278#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 )
3280 c = Ctrl_C;
3281 else
3282 c = ESC;
Bram Moolenaare2c38102016-01-31 14:55:40 +01003283#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 tc = c;
Bram Moolenaare2c38102016-01-31 14:55:40 +01003285#endif
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01003286 // return from main_loop()
3287 if (pending_exmode_active)
3288 exmode_active = EXMODE_NORMAL;
3289
Bram Moolenaarb37a65e2022-01-01 12:42:56 +00003290 // no chars to block abbreviation for
3291 typebuf.tb_no_abbr_cnt = 0;
3292
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 break;
3294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295
3296/*
3297 * get a character: 3. from the user - update display
3298 */
Bram Moolenaar30613902019-12-01 22:11:18 +01003299 // In insert mode a screen update is skipped when characters
3300 // are still available. But when those available characters
3301 // are part of a mapping, and we are going to do a blocking
3302 // wait here. Need to update the screen to display the
3303 // changed text so far. Also for when 'lazyredraw' is set and
3304 // redrawing was postponed because there was something in the
3305 // input buffer (e.g., termresponse).
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01003306 if (((State & INSERT) != 0 || p_lz) && (State & CMDLINE) == 0
3307 && advance && must_redraw != 0 && !need_wait_return)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 {
3309 update_screen(0);
Bram Moolenaar30613902019-12-01 22:11:18 +01003310 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 }
3312
3313 /*
3314 * If we have a partial match (and are going to wait for more
3315 * input from the user), show the partially matched characters
3316 * to the user with showcmd.
3317 */
3318#ifdef FEAT_CMDL_INFO
Bram Moolenaareda35f72019-08-03 14:59:44 +02003319 showcmd_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320#endif
3321 c1 = 0;
3322 if (typebuf.tb_len > 0 && advance && !exmode_active)
3323 {
3324 if (((State & (NORMAL | INSERT)) || State == LANGMAP)
3325 && State != HITRETURN)
3326 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003327 // this looks nice when typing a dead character map
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 if (State & INSERT
3329 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
3330 + typebuf.tb_len - 1) == 1)
3331 {
3332 edit_putchar(typebuf.tb_buf[typebuf.tb_off
3333 + typebuf.tb_len - 1], FALSE);
Bram Moolenaar30613902019-12-01 22:11:18 +01003334 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 c1 = 1;
3336 }
3337#ifdef FEAT_CMDL_INFO
Bram Moolenaar30613902019-12-01 22:11:18 +01003338 // need to use the col and row from above here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 old_wcol = curwin->w_wcol;
3340 old_wrow = curwin->w_wrow;
3341 curwin->w_wcol = new_wcol;
3342 curwin->w_wrow = new_wrow;
3343 push_showcmd();
3344 if (typebuf.tb_len > SHOWCMD_COLS)
Bram Moolenaareda35f72019-08-03 14:59:44 +02003345 showcmd_idx = typebuf.tb_len - SHOWCMD_COLS;
3346 while (showcmd_idx < typebuf.tb_len)
3347 (void)add_to_showcmd(
3348 typebuf.tb_buf[typebuf.tb_off + showcmd_idx++]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 curwin->w_wcol = old_wcol;
3350 curwin->w_wrow = old_wrow;
3351#endif
3352 }
3353
Bram Moolenaar30613902019-12-01 22:11:18 +01003354 // this looks nice when typing a dead character map
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 if ((State & CMDLINE)
3356#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3357 && cmdline_star == 0
3358#endif
3359 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
3360 + typebuf.tb_len - 1) == 1)
3361 {
3362 putcmdline(typebuf.tb_buf[typebuf.tb_off
3363 + typebuf.tb_len - 1], FALSE);
3364 c1 = 1;
3365 }
3366 }
3367
3368/*
3369 * get a character: 3. from the user - get it
3370 */
Bram Moolenaar83f4cbd2018-06-12 21:35:40 +02003371 if (typebuf.tb_len == 0)
3372 // timedout may have been set while waiting for a mapping
3373 // that has a <Nop> RHS.
3374 timedout = FALSE;
3375
Bram Moolenaar652de232019-04-04 20:13:09 +02003376 if (advance)
3377 {
3378 if (typebuf.tb_len == 0
3379 || !(p_timeout
3380 || (p_ttimeout && keylen == KEYLEN_PART_KEY)))
3381 // blocking wait
3382 wait_time = -1L;
3383 else if (keylen == KEYLEN_PART_KEY && p_ttm >= 0)
3384 wait_time = p_ttm;
3385 else
3386 wait_time = p_tm;
3387 }
3388 else
3389 wait_time = 0;
3390
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003391 wait_tb_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 c = inchar(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len,
3393 typebuf.tb_buflen - typebuf.tb_off - typebuf.tb_len - 1,
Bram Moolenaar652de232019-04-04 20:13:09 +02003394 wait_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395
3396#ifdef FEAT_CMDL_INFO
Bram Moolenaareda35f72019-08-03 14:59:44 +02003397 if (showcmd_idx != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 pop_showcmd();
3399#endif
3400 if (c1 == 1)
3401 {
3402 if (State & INSERT)
3403 edit_unputchar();
3404 if (State & CMDLINE)
3405 unputcmdline();
Bram Moolenaarbc256d92012-06-06 12:06:15 +02003406 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003407 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 }
3409
3410 if (c < 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003411 continue; // end of input script reached
3412 if (c == NUL) // no character available
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 {
3414 if (!advance)
3415 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01003416 if (wait_tb_len > 0) // timed out
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 {
3418 timedout = TRUE;
3419 continue;
3420 }
3421 }
3422 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003423 { // allow mapping for just typed characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 while (typebuf.tb_buf[typebuf.tb_off
3425 + typebuf.tb_len] != NUL)
3426 typebuf.tb_noremap[typebuf.tb_off
3427 + typebuf.tb_len++] = RM_YES;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003428#ifdef HAVE_INPUT_METHOD
Bram Moolenaar30613902019-12-01 22:11:18 +01003429 // Get IM status right after getting keys, not after the
3430 // timeout for a mapping (focus may be lost by then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 vgetc_im_active = im_get_status();
3432#endif
3433 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003434 } // for (;;)
3435 } // if (!character from stuffbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436
Bram Moolenaar30613902019-12-01 22:11:18 +01003437 // if advance is FALSE don't loop on NULs
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003438 } while ((c < 0 && c != K_CANCEL) || (advance && c == NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439
3440 /*
3441 * The "INSERT" message is taken care of here:
3442 * if we return an ESC to exit insert mode, the message is deleted
3443 * if we don't return an ESC but deleted the message before, redisplay it
3444 */
Bram Moolenaar09df3122006-01-23 22:23:09 +00003445 if (advance && p_smd && msg_silent == 0 && (State & INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003447 if (c == ESC && !mode_deleted && !no_mapping && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 {
3449 if (typebuf.tb_len && !KeyTyped)
Bram Moolenaar30613902019-12-01 22:11:18 +01003450 redraw_cmdline = TRUE; // delete mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 else
3452 unshowmode(FALSE);
3453 }
3454 else if (c != ESC && mode_deleted)
3455 {
3456 if (typebuf.tb_len && !KeyTyped)
Bram Moolenaar30613902019-12-01 22:11:18 +01003457 redraw_cmdline = TRUE; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 else
3459 showmode();
3460 }
3461 }
3462#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003463 // may unshow different cursor shape
Bram Moolenaarf085f422017-06-07 20:39:47 +02003464 if (gui.in_use && shape_changed)
3465 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466#endif
Bram Moolenaar6edbbd82019-03-10 09:41:51 +01003467 if (timedout && c == ESC)
3468 {
3469 char_u nop_buf[3];
3470
3471 // When recording there will be no timeout. Add a <Nop> after the ESC
3472 // to avoid that it forms a key code with following characters.
3473 nop_buf[0] = K_SPECIAL;
3474 nop_buf[1] = KS_EXTRA;
3475 nop_buf[2] = KE_NOP;
3476 gotchars(nop_buf, 3);
3477 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003479 --vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480
3481 return c;
3482}
3483
3484/*
3485 * inchar() - get one character from
3486 * 1. a scriptfile
3487 * 2. the keyboard
3488 *
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003489 * As many characters as we can get (up to 'maxlen') are put in "buf" and
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 * NUL terminated (buffer length must be 'maxlen' + 1).
3491 * Minimum for "maxlen" is 3!!!!
3492 *
3493 * "tb_change_cnt" is the value of typebuf.tb_change_cnt if "buf" points into
3494 * it. When typebuf.tb_change_cnt changes (e.g., when a message is received
3495 * from a remote client) "buf" can no longer be used. "tb_change_cnt" is 0
3496 * otherwise.
3497 *
3498 * If we got an interrupt all input is read until none is available.
3499 *
3500 * If wait_time == 0 there is no waiting for the char.
3501 * If wait_time == n we wait for n msec for a character to arrive.
3502 * If wait_time == -1 we wait forever for a character to arrive.
3503 *
3504 * Return the number of obtained characters.
3505 * Return -1 when end of input script reached.
3506 */
Bram Moolenaarcda77642016-06-04 13:32:35 +02003507 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003508inchar(
3509 char_u *buf,
3510 int maxlen,
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003511 long wait_time) // milliseconds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512{
Bram Moolenaar30613902019-12-01 22:11:18 +01003513 int len = 0; // init for GCC
3514 int retesc = FALSE; // return ESC with gotint
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 int script_char;
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003516 int tb_change_cnt = typebuf.tb_change_cnt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517
Bram Moolenaar30613902019-12-01 22:11:18 +01003518 if (wait_time == -1L || wait_time > 100L) // flush output before waiting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 {
3520 cursor_on();
Bram Moolenaara338adc2018-01-31 20:51:47 +01003521 out_flush_cursor(FALSE, FALSE);
3522#if defined(FEAT_GUI) && defined(FEAT_MOUSESHAPE)
3523 if (gui.in_use && postponed_mouseshape)
3524 update_mouseshape(-1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525#endif
3526 }
3527
3528 /*
3529 * Don't reset these when at the hit-return prompt, otherwise a endless
3530 * recursive loop may result (write error in swapfile, hit-return, timeout
3531 * on char wait, flush swapfile, write error....).
3532 */
3533 if (State != HITRETURN)
3534 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003535 did_outofmem_msg = FALSE; // display out of memory message (again)
3536 did_swapwrite_msg = FALSE; // display swap file write error again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003538 undo_off = FALSE; // restart undo now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539
3540 /*
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00003541 * Get a character from a script file if there is one.
3542 * If interrupted: Stop reading script files, close them all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 */
3544 script_char = -1;
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00003545 while (scriptin[curscript] != NULL && script_char < 0
3546#ifdef FEAT_EVAL
3547 && !ignore_script
3548#endif
3549 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02003551#ifdef MESSAGE_QUEUE
3552 parse_queued_messages();
Bram Moolenaarf2330482008-06-24 20:19:36 +00003553#endif
3554
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 if (got_int || (script_char = getc(scriptin[curscript])) < 0)
3556 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003557 // Reached EOF.
3558 // Careful: closescript() frees typebuf.tb_buf[] and buf[] may
3559 // point inside typebuf.tb_buf[]. Don't use buf[] after this!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 closescript();
3561 /*
3562 * When reading script file is interrupted, return an ESC to get
3563 * back to normal mode.
3564 * Otherwise return -1, because typebuf.tb_buf[] has changed.
3565 */
3566 if (got_int)
3567 retesc = TRUE;
3568 else
3569 return -1;
3570 }
3571 else
3572 {
3573 buf[0] = script_char;
3574 len = 1;
3575 }
3576 }
3577
Bram Moolenaar30613902019-12-01 22:11:18 +01003578 if (script_char < 0) // did not get a character from script
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 {
3580 /*
3581 * If we got an interrupt, skip all previously typed characters and
3582 * return TRUE if quit reading script file.
3583 * Stop reading typeahead when a single CTRL-C was read,
3584 * fill_input_buf() returns this when not able to read from stdin.
3585 * Don't use buf[] here, closescript() may have freed typebuf.tb_buf[]
3586 * and buf may be pointing inside typebuf.tb_buf[].
3587 */
3588 if (got_int)
3589 {
3590#define DUM_LEN MAXMAPLEN * 3 + 3
3591 char_u dum[DUM_LEN + 1];
3592
3593 for (;;)
3594 {
3595 len = ui_inchar(dum, DUM_LEN, 0L, 0);
3596 if (len == 0 || (len == 1 && dum[0] == 3))
3597 break;
3598 }
3599 return retesc;
3600 }
3601
3602 /*
3603 * Always flush the output characters when getting input characters
Bram Moolenaarcb574f42019-01-25 22:29:57 +01003604 * from the user and not just peeking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 */
Bram Moolenaarcb574f42019-01-25 22:29:57 +01003606 if (wait_time == -1L || wait_time > 10L)
3607 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608
3609 /*
3610 * Fill up to a third of the buffer, because each character may be
3611 * tripled below.
3612 */
3613 len = ui_inchar(buf, maxlen / 3, wait_time, tb_change_cnt);
3614 }
3615
Bram Moolenaar30613902019-12-01 22:11:18 +01003616 // If the typebuf was changed further down, it is like nothing was added by
3617 // this call.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 if (typebuf_changed(tb_change_cnt))
3619 return 0;
3620
Bram Moolenaar30613902019-12-01 22:11:18 +01003621 // Note the change in the typeahead buffer, this matters for when
3622 // vgetorpeek() is called recursively, e.g. using getchar(1) in a timer
3623 // function.
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003624 if (len > 0 && ++typebuf.tb_change_cnt == 0)
3625 typebuf.tb_change_cnt = 1;
3626
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003627 return fix_input_buffer(buf, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628}
3629
3630/*
3631 * Fix typed characters for use by vgetc() and check_termcode().
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003632 * "buf[]" must have room to triple the number of bytes!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 * Returns the new length.
3634 */
3635 int
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003636fix_input_buffer(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637{
3638 int i;
3639 char_u *p = buf;
3640
3641 /*
3642 * Two characters are special: NUL and K_SPECIAL.
3643 * When compiled With the GUI CSI is also special.
3644 * Replace NUL by K_SPECIAL KS_ZERO KE_FILLER
3645 * Replace K_SPECIAL by K_SPECIAL KS_SPECIAL KE_FILLER
3646 * Replace CSI by K_SPECIAL KS_EXTRA KE_CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 */
3648 for (i = len; --i >= 0; ++p)
3649 {
3650#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003651 // When the GUI is used any character can come after a CSI, don't
3652 // escape it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 if (gui.in_use && p[0] == CSI && i >= 2)
3654 {
3655 p += 2;
3656 i -= 2;
3657 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003658 // When the GUI is not used CSI needs to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 else if (!gui.in_use && p[0] == CSI)
3660 {
3661 mch_memmove(p + 3, p + 1, (size_t)i);
3662 *p++ = K_SPECIAL;
3663 *p++ = KS_EXTRA;
3664 *p = (int)KE_CSI;
3665 len += 2;
3666 }
3667 else
3668#endif
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003669 if (p[0] == NUL || (p[0] == K_SPECIAL
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003670 // timeout may generate K_CURSORHOLD
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003671 && (i < 2 || p[1] != KS_EXTRA || p[2] != (int)KE_CURSORHOLD)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003672#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003673 // Win32 console passes modifiers
3674 && (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003675# ifdef VIMDLL
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003676 gui.in_use ||
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003677# endif
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003678 (i < 2 || p[1] != KS_MODIFIER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679#endif
3680 ))
3681 {
3682 mch_memmove(p + 3, p + 1, (size_t)i);
3683 p[2] = K_THIRD(p[0]);
3684 p[1] = K_SECOND(p[0]);
3685 p[0] = K_SPECIAL;
3686 p += 2;
3687 len += 2;
3688 }
3689 }
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003690 *p = NUL; // add trailing NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 return len;
3692}
3693
3694#if defined(USE_INPUT_BUF) || defined(PROTO)
3695/*
3696 * Return TRUE when bytes are in the input buffer or in the typeahead buffer.
3697 * Normally the input buffer would be sufficient, but the server_to_input_buf()
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003698 * or feedkeys() may insert characters in the typeahead buffer while we are
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003699 * waiting for input to arrive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 */
3701 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003702input_available(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703{
3704 return (!vim_is_input_buf_empty()
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003705# if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
3706 || typebuf_was_filled
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707# endif
3708 );
3709}
3710#endif
Bram Moolenaar957cf672020-11-12 14:21:06 +01003711
3712/*
3713 * Function passed to do_cmdline() to get the command after a <Cmd> key from
3714 * typeahead.
3715 */
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003716 static char_u *
Bram Moolenaar957cf672020-11-12 14:21:06 +01003717getcmdkeycmd(
3718 int promptc UNUSED,
3719 void *cookie UNUSED,
3720 int indent UNUSED,
3721 getline_opt_T do_concat UNUSED)
3722{
3723 garray_T line_ga;
3724 int c1 = -1;
3725 int c2;
3726 int cmod = 0;
3727 int aborted = FALSE;
3728
3729 ga_init2(&line_ga, 1, 32);
3730
3731 // no mapping for these characters
3732 no_mapping++;
3733
3734 got_int = FALSE;
3735 while (c1 != NUL && !aborted)
3736 {
Bram Moolenaarca359cb2020-11-15 20:49:41 +01003737 if (ga_grow(&line_ga, 32) != OK)
3738 {
3739 aborted = TRUE;
3740 break;
3741 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003742
3743 if (vgetorpeek(FALSE) == NUL)
3744 {
3745 // incomplete <Cmd> is an error, because there is not much the user
3746 // could do in this state.
3747 emsg(_(e_cmd_mapping_must_end_with_cr));
3748 aborted = TRUE;
3749 break;
3750 }
3751
3752 // Get one character at a time.
3753 c1 = vgetorpeek(TRUE);
3754
3755 // Get two extra bytes for special keys
3756 if (c1 == K_SPECIAL)
3757 {
3758 c1 = vgetorpeek(TRUE);
3759 c2 = vgetorpeek(TRUE);
3760 if (c1 == KS_MODIFIER)
3761 {
3762 cmod = c2;
3763 continue;
3764 }
3765 c1 = TO_SPECIAL(c1, c2);
3766 }
Bram Moolenaar4a441202020-11-28 14:43:26 +01003767 if (c1 == Ctrl_V)
3768 {
3769 // CTRL-V is followed by octal, hex or other characters, reverses
3770 // what AppendToRedobuffLit() does.
3771 no_reduce_keys = TRUE; // don't merge modifyOtherKeys
Bram Moolenaar0684e362020-12-03 19:54:42 +01003772 c1 = get_literal(TRUE);
Bram Moolenaar4a441202020-11-28 14:43:26 +01003773 no_reduce_keys = FALSE;
3774 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003775
3776 if (got_int)
3777 aborted = TRUE;
3778 else if (c1 == '\r' || c1 == '\n')
3779 c1 = NUL; // end the line
3780 else if (c1 == ESC)
3781 aborted = TRUE;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003782 else if (c1 == K_COMMAND || c1 == K_SCRIPT_COMMAND)
Bram Moolenaar957cf672020-11-12 14:21:06 +01003783 {
3784 // give a nicer error message for this special case
3785 emsg(_(e_cmd_mapping_must_end_with_cr_before_second_cmd));
3786 aborted = TRUE;
3787 }
3788 else if (IS_SPECIAL(c1))
3789 {
3790 if (c1 == K_SNR)
Bram Moolenaarc77534c2020-11-18 11:34:37 +01003791 ga_concat(&line_ga, (char_u *)"<SNR>");
Bram Moolenaar957cf672020-11-12 14:21:06 +01003792 else
3793 {
3794 semsg(e_cmd_maping_must_not_include_str_key,
3795 get_special_key_name(c1, cmod));
3796 aborted = TRUE;
3797 }
3798 }
3799 else
Bram Moolenaara9725222022-01-16 13:30:33 +00003800 ga_append(&line_ga, c1);
Bram Moolenaar957cf672020-11-12 14:21:06 +01003801
3802 cmod = 0;
3803 }
3804
3805 no_mapping--;
3806
3807 if (aborted)
3808 ga_clear(&line_ga);
3809
3810 return (char_u *)line_ga.ga_data;
3811}
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003812
3813 int
Bram Moolenaard4e2f502022-01-15 18:48:32 +00003814do_cmdkey_command(int key UNUSED, int flags)
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003815{
3816 int res;
3817#ifdef FEAT_EVAL
Bram Moolenaara9725222022-01-16 13:30:33 +00003818 sctx_T save_current_sctx = {-1, 0, 0, 0};
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003819
3820 if (key == K_SCRIPT_COMMAND && last_used_map != NULL)
3821 {
3822 save_current_sctx = current_sctx;
3823 current_sctx = last_used_map->m_script_ctx;
3824 }
3825#endif
3826
3827 res = do_cmdline(NULL, getcmdkeycmd, NULL, flags);
3828
3829#ifdef FEAT_EVAL
Bram Moolenaara9725222022-01-16 13:30:33 +00003830 if (save_current_sctx.sc_sid >= 0)
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003831 current_sctx = save_current_sctx;
3832#endif
3833
3834 return res;
3835}
3836
3837#if defined(FEAT_EVAL) || defined(PROTO)
3838 void
3839reset_last_used_map(void)
3840{
3841 last_used_map = NULL;
3842}
3843#endif