blob: a6644d8efa4411e326d193815bb8c345e2a411f1 [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/*
11 * getchar.c
12 *
13 * functions related with getting a character from the user/mapping/redo/...
14 *
15 * manipulations with redo buffer and stuff buffer
16 * mappings and abbreviations
17 */
18
19#include "vim.h"
20
21/*
22 * These buffers are used for storing:
23 * - stuffed characters: A command that is translated into another command.
24 * - redo characters: will redo the last change.
Bram Moolenaarf6f95d92009-11-11 15:23:37 +000025 * - recorded characters: for the "q" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026 *
27 * The bytes are stored like in the typeahead buffer:
28 * - K_SPECIAL introduces a special key (two more bytes follow). A literal
29 * K_SPECIAL is stored as K_SPECIAL KS_SPECIAL KE_FILLER.
30 * - CSI introduces a GUI termcap code (also when gui.in_use is FALSE,
31 * otherwise switching the GUI on would make mappings invalid).
32 * A literal CSI is stored as CSI KS_EXTRA KE_CSI.
33 * These translations are also done on multi-byte characters!
34 *
35 * Escaping CSI bytes is done by the system-specific input functions, called
36 * by ui_inchar().
37 * Escaping K_SPECIAL is done by inchar().
38 * Un-escaping is done by vgetc().
39 */
40
Bram Moolenaar30613902019-12-01 22:11:18 +010041#define MINIMAL_SIZE 20 // minimal size for b_str
Bram Moolenaar071d4272004-06-13 20:20:40 +000042
Bram Moolenaar285e3352018-04-18 23:01:13 +020043static buffheader_T redobuff = {{NULL, {NUL}}, NULL, 0, 0};
44static buffheader_T old_redobuff = {{NULL, {NUL}}, NULL, 0, 0};
45static buffheader_T recordbuff = {{NULL, {NUL}}, NULL, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +000046
Bram Moolenaar30613902019-12-01 22:11:18 +010047static int typeahead_char = 0; // typeahead char that's not flushed
Bram Moolenaar071d4272004-06-13 20:20:40 +000048
49/*
50 * when block_redo is TRUE redo buffer will not be changed
51 * used by edit() to repeat insertions and 'V' command for redoing
52 */
53static int block_redo = FALSE;
54
Bram Moolenaar459fd782019-10-13 16:43:39 +020055static int KeyNoremap = 0; // remapping flags
Bram Moolenaar071d4272004-06-13 20:20:40 +000056
57/*
Bram Moolenaarfd89d7e2016-06-04 20:25:05 +020058 * Variables used by vgetorpeek() and flush_buffers().
Bram Moolenaar071d4272004-06-13 20:20:40 +000059 *
60 * typebuf.tb_buf[] contains all characters that are not consumed yet.
61 * typebuf.tb_buf[typebuf.tb_off] is the first valid character.
62 * typebuf.tb_buf[typebuf.tb_off + typebuf.tb_len - 1] is the last valid char.
63 * typebuf.tb_buf[typebuf.tb_off + typebuf.tb_len] must be NUL.
64 * The head of the buffer may contain the result of mappings, abbreviations
65 * and @a commands. The length of this part is typebuf.tb_maplen.
66 * typebuf.tb_silent is the part where <silent> applies.
67 * After the head are characters that come from the terminal.
68 * typebuf.tb_no_abbr_cnt is the number of characters in typebuf.tb_buf that
69 * should not be considered for abbreviations.
70 * Some parts of typebuf.tb_buf may not be mapped. These parts are remembered
71 * in typebuf.tb_noremap[], which is the same length as typebuf.tb_buf and
72 * contains RM_NONE for the characters that are not to be remapped.
73 * typebuf.tb_noremap[typebuf.tb_off] is the first valid flag.
74 * (typebuf has been put in globals.h, because check_termcode() needs it).
75 */
Bram Moolenaar30613902019-12-01 22:11:18 +010076#define RM_YES 0 // tb_noremap: remap
77#define RM_NONE 1 // tb_noremap: don't remap
78#define RM_SCRIPT 2 // tb_noremap: remap local script mappings
79#define RM_ABBR 4 // tb_noremap: don't remap, do abbrev.
Bram Moolenaar071d4272004-06-13 20:20:40 +000080
Bram Moolenaar30613902019-12-01 22:11:18 +010081// typebuf.tb_buf has three parts: room in front (for result of mappings), the
82// middle for typeahead and room for new characters (which needs to be 3 *
83// MAXMAPLEN) for the Amiga).
Bram Moolenaar071d4272004-06-13 20:20:40 +000084#define TYPELEN_INIT (5 * (MAXMAPLEN + 3))
Bram Moolenaar30613902019-12-01 22:11:18 +010085static char_u typebuf_init[TYPELEN_INIT]; // initial typebuf.tb_buf
86static char_u noremapbuf_init[TYPELEN_INIT]; // initial typebuf.tb_noremap
Bram Moolenaar071d4272004-06-13 20:20:40 +000087
Bram Moolenaar30613902019-12-01 22:11:18 +010088static int last_recorded_len = 0; // number of last recorded chars
Bram Moolenaar071d4272004-06-13 20:20:40 +000089
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 Moolenaarf9e3e092019-01-13 23:38:42 +0100215 iemsg(_("E222: Add to read buffer"));
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/*
249 * Add number "n" to buffer "buf".
250 */
251 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100252add_num_buff(buffheader_T *buf, long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253{
254 char_u number[32];
255
256 sprintf((char *)number, "%ld", n);
257 add_buff(buf, number, -1L);
258}
259
260/*
261 * Add character 'c' to buffer "buf".
262 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
263 */
264 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100265add_char_buff(buffheader_T *buf, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267 char_u bytes[MB_MAXBYTES + 1];
268 int len;
269 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270 char_u temp[4];
271
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272 if (IS_SPECIAL(c))
273 len = 1;
274 else
275 len = (*mb_char2bytes)(c, bytes);
276 for (i = 0; i < len; ++i)
277 {
278 if (!IS_SPECIAL(c))
279 c = bytes[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280
281 if (IS_SPECIAL(c) || c == K_SPECIAL || c == NUL)
282 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100283 // translate special key code into three byte sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284 temp[0] = K_SPECIAL;
285 temp[1] = K_SECOND(c);
286 temp[2] = K_THIRD(c);
287 temp[3] = NUL;
288 }
289#ifdef FEAT_GUI
290 else if (c == CSI)
291 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100292 // Translate a CSI to a CSI - KS_EXTRA - KE_CSI sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293 temp[0] = CSI;
294 temp[1] = KS_EXTRA;
295 temp[2] = (int)KE_CSI;
296 temp[3] = NUL;
297 }
298#endif
299 else
300 {
301 temp[0] = c;
302 temp[1] = NUL;
303 }
304 add_buff(buf, temp, -1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306}
307
Bram Moolenaar30613902019-12-01 22:11:18 +0100308// First read ahead buffer. Used for translated commands.
Bram Moolenaar285e3352018-04-18 23:01:13 +0200309static buffheader_T readbuf1 = {{NULL, {NUL}}, NULL, 0, 0};
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100310
Bram Moolenaar30613902019-12-01 22:11:18 +0100311// Second read ahead buffer. Used for redo.
Bram Moolenaar285e3352018-04-18 23:01:13 +0200312static buffheader_T readbuf2 = {{NULL, {NUL}}, NULL, 0, 0};
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100313
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314/*
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100315 * Get one byte from the read buffers. Use readbuf1 one first, use readbuf2
316 * if that one is empty.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317 * If advance == TRUE go to the next char.
318 * No translation is done K_SPECIAL and CSI are escaped.
319 */
320 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100321read_readbuffers(int advance)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100323 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100325 c = read_readbuf(&readbuf1, advance);
326 if (c == NUL)
327 c = read_readbuf(&readbuf2, advance);
328 return c;
329}
330
331 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100332read_readbuf(buffheader_T *buf, int advance)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100333{
334 char_u c;
335 buffblock_T *curr;
336
Bram Moolenaar30613902019-12-01 22:11:18 +0100337 if (buf->bh_first.b_next == NULL) // buffer is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338 return NUL;
339
Bram Moolenaar285e3352018-04-18 23:01:13 +0200340 curr = buf->bh_first.b_next;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100341 c = curr->b_str[buf->bh_index];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342
343 if (advance)
344 {
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100345 if (curr->b_str[++buf->bh_index] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346 {
Bram Moolenaar285e3352018-04-18 23:01:13 +0200347 buf->bh_first.b_next = curr->b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348 vim_free(curr);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100349 buf->bh_index = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 }
351 }
352 return c;
353}
354
355/*
Bram Moolenaar06811f32014-02-15 16:17:07 +0100356 * Prepare the read buffers for reading (if they contain something).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357 */
358 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100359start_stuff(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360{
Bram Moolenaar285e3352018-04-18 23:01:13 +0200361 if (readbuf1.bh_first.b_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362 {
Bram Moolenaar285e3352018-04-18 23:01:13 +0200363 readbuf1.bh_curr = &(readbuf1.bh_first);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100364 readbuf1.bh_space = 0;
365 }
Bram Moolenaar285e3352018-04-18 23:01:13 +0200366 if (readbuf2.bh_first.b_next != NULL)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100367 {
Bram Moolenaar285e3352018-04-18 23:01:13 +0200368 readbuf2.bh_curr = &(readbuf2.bh_first);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100369 readbuf2.bh_space = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370 }
371}
372
373/*
374 * Return TRUE if the stuff buffer is empty.
375 */
376 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100377stuff_empty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378{
Bram Moolenaar285e3352018-04-18 23:01:13 +0200379 return (readbuf1.bh_first.b_next == NULL
380 && readbuf2.bh_first.b_next == NULL);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100381}
382
Bram Moolenaar113e1072019-01-20 15:30:40 +0100383#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100384/*
385 * Return TRUE if readbuf1 is empty. There may still be redo characters in
386 * redbuf2.
387 */
388 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100389readbuf1_empty(void)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100390{
Bram Moolenaar285e3352018-04-18 23:01:13 +0200391 return (readbuf1.bh_first.b_next == NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100393#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394
395/*
396 * Set a typeahead character that won't be flushed.
397 */
398 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100399typeahead_noflush(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400{
401 typeahead_char = c;
402}
403
404/*
405 * Remove the contents of the stuff buffer and the mapped characters in the
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100406 * typeahead buffer (used in case of an error). If "flush_typeahead" is true,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407 * flush all typeahead characters (used when interrupted by a CTRL-C).
408 */
409 void
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200410flush_buffers(flush_buffers_T flush_typeahead)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411{
412 init_typebuf();
413
414 start_stuff();
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100415 while (read_readbuffers(TRUE) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416 ;
417
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200418 if (flush_typeahead == FLUSH_MINIMAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419 {
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200420 // remove mapped characters at the start only
421 typebuf.tb_off += typebuf.tb_maplen;
422 typebuf.tb_len -= typebuf.tb_maplen;
Bram Moolenaare49b4bb2020-03-11 13:01:40 +0100423#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
424 if (typebuf.tb_len == 0)
425 typebuf_was_filled = FALSE;
426#endif
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200427 }
428 else
429 {
430 // remove typeahead
431 if (flush_typeahead == FLUSH_INPUT)
432 // We have to get all characters, because we may delete the first
433 // part of an escape sequence. In an xterm we get one char at a
434 // time and we have to get them all.
435 while (inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 10L) != 0)
436 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437 typebuf.tb_off = MAXMAPLEN;
438 typebuf.tb_len = 0;
Bram Moolenaar4eb65312017-06-24 18:49:00 +0200439#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar30613902019-12-01 22:11:18 +0100440 // Reset the flag that text received from a client or from feedkeys()
441 // was inserted in the typeahead buffer.
Bram Moolenaar4eb65312017-06-24 18:49:00 +0200442 typebuf_was_filled = FALSE;
443#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445 typebuf.tb_maplen = 0;
446 typebuf.tb_silent = 0;
447 cmd_silent = FALSE;
448 typebuf.tb_no_abbr_cnt = 0;
Bram Moolenaarb8d732e2020-08-05 22:07:26 +0200449 if (++typebuf.tb_change_cnt == 0)
450 typebuf.tb_change_cnt = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451}
452
453/*
454 * The previous contents of the redo buffer is kept in old_redobuffer.
455 * This is used for the CTRL-O <.> command in insert mode.
456 */
457 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100458ResetRedobuff(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459{
460 if (!block_redo)
461 {
462 free_buff(&old_redobuff);
463 old_redobuff = redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200464 redobuff.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 }
466}
467
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100468/*
469 * Discard the contents of the redo buffer and restore the previous redo
470 * buffer.
471 */
472 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100473CancelRedo(void)
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100474{
475 if (!block_redo)
476 {
477 free_buff(&redobuff);
478 redobuff = old_redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200479 old_redobuff.bh_first.b_next = NULL;
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100480 start_stuff();
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100481 while (read_readbuffers(TRUE) != NUL)
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100482 ;
483 }
484}
485
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486/*
487 * Save redobuff and old_redobuff to save_redobuff and save_old_redobuff.
488 * Used before executing autocommands and user functions.
489 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 void
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200491saveRedobuff(save_redo_T *save_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492{
493 char_u *s;
494
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200495 save_redo->sr_redobuff = redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200496 redobuff.bh_first.b_next = NULL;
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200497 save_redo->sr_old_redobuff = old_redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200498 old_redobuff.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499
Bram Moolenaar30613902019-12-01 22:11:18 +0100500 // Make a copy, so that ":normal ." in a function works.
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200501 s = get_buffcont(&save_redo->sr_redobuff, FALSE);
502 if (s != NULL)
503 {
504 add_buff(&redobuff, s, -1L);
505 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 }
507}
508
509/*
510 * Restore redobuff and old_redobuff from save_redobuff and save_old_redobuff.
511 * Used after executing autocommands and user functions.
512 */
513 void
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200514restoreRedobuff(save_redo_T *save_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515{
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200516 free_buff(&redobuff);
517 redobuff = save_redo->sr_redobuff;
518 free_buff(&old_redobuff);
519 old_redobuff = save_redo->sr_old_redobuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521
522/*
523 * Append "s" to the redo buffer.
524 * K_SPECIAL and CSI should already have been escaped.
525 */
526 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100527AppendToRedobuff(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528{
529 if (!block_redo)
530 add_buff(&redobuff, s, -1L);
531}
532
533/*
534 * Append to Redo buffer literally, escaping special characters with CTRL-V.
535 * K_SPECIAL and CSI are escaped as well.
536 */
537 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100538AppendToRedobuffLit(
539 char_u *str,
Bram Moolenaar30613902019-12-01 22:11:18 +0100540 int len) // length of "str" or -1 for up to the NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541{
Bram Moolenaarebefac62005-12-28 22:39:57 +0000542 char_u *s = str;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 int c;
544 char_u *start;
545
546 if (block_redo)
547 return;
548
Bram Moolenaarebefac62005-12-28 22:39:57 +0000549 while (len < 0 ? *s != NUL : s - str < len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100551 // Put a string of normal characters in the redo buffer (that's
552 // faster).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 start = s;
554 while (*s >= ' '
555#ifndef EBCDIC
Bram Moolenaar30613902019-12-01 22:11:18 +0100556 && *s < DEL // EBCDIC: all chars above space are normal
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557#endif
Bram Moolenaarebefac62005-12-28 22:39:57 +0000558 && (len < 0 || s - str < len))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559 ++s;
560
Bram Moolenaar30613902019-12-01 22:11:18 +0100561 // Don't put '0' or '^' as last character, just in case a CTRL-D is
562 // typed next.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 if (*s == NUL && (s[-1] == '0' || s[-1] == '^'))
564 --s;
565 if (s > start)
566 add_buff(&redobuff, start, (long)(s - start));
567
Bram Moolenaarebefac62005-12-28 22:39:57 +0000568 if (*s == NUL || (len >= 0 && s - str >= len))
569 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570
Bram Moolenaar30613902019-12-01 22:11:18 +0100571 // Handle a special or multibyte character.
Bram Moolenaarebefac62005-12-28 22:39:57 +0000572 if (has_mbyte)
Bram Moolenaar30613902019-12-01 22:11:18 +0100573 // Handle composing chars separately.
Bram Moolenaarebefac62005-12-28 22:39:57 +0000574 c = mb_cptr2char_adv(&s);
575 else
Bram Moolenaarebefac62005-12-28 22:39:57 +0000576 c = *s++;
577 if (c < ' ' || c == DEL || (*s == NUL && (c == '0' || c == '^')))
578 add_char_buff(&redobuff, Ctrl_V);
579
Bram Moolenaar30613902019-12-01 22:11:18 +0100580 // CTRL-V '0' must be inserted as CTRL-V 048 (EBCDIC: xf0)
Bram Moolenaarebefac62005-12-28 22:39:57 +0000581 if (*s == NUL && c == '0')
582#ifdef EBCDIC
583 add_buff(&redobuff, (char_u *)"xf0", 3L);
584#else
585 add_buff(&redobuff, (char_u *)"048", 3L);
586#endif
587 else
588 add_char_buff(&redobuff, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 }
590}
591
592/*
593 * Append a character to the redo buffer.
594 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
595 */
596 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100597AppendCharToRedobuff(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598{
599 if (!block_redo)
600 add_char_buff(&redobuff, c);
601}
602
603/*
604 * Append a number to the redo buffer.
605 */
606 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100607AppendNumberToRedobuff(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608{
609 if (!block_redo)
610 add_num_buff(&redobuff, n);
611}
612
613/*
614 * Append string "s" to the stuff buffer.
615 * CSI and K_SPECIAL must already have been escaped.
616 */
617 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100618stuffReadbuff(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100620 add_buff(&readbuf1, s, -1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621}
622
Bram Moolenaar4f5ce332014-07-30 16:00:58 +0200623/*
624 * Append string "s" to the redo stuff buffer.
625 * CSI and K_SPECIAL must already have been escaped.
626 */
627 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100628stuffRedoReadbuff(char_u *s)
Bram Moolenaar4f5ce332014-07-30 16:00:58 +0200629{
630 add_buff(&readbuf2, s, -1L);
631}
632
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200633 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100634stuffReadbuffLen(char_u *s, long len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100636 add_buff(&readbuf1, s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637}
638
639#if defined(FEAT_EVAL) || defined(PROTO)
640/*
641 * Stuff "s" into the stuff buffer, leaving special key codes unmodified and
642 * escaping other K_SPECIAL and CSI bytes.
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200643 * Change CR, LF and ESC into a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 */
645 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100646stuffReadbuffSpec(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647{
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200648 int c;
649
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 while (*s != NUL)
651 {
652 if (*s == K_SPECIAL && s[1] != NUL && s[2] != NUL)
653 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100654 // Insert special key literally.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 stuffReadbuffLen(s, 3L);
656 s += 3;
657 }
658 else
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200659 {
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200660 c = mb_ptr2char_adv(&s);
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200661 if (c == CAR || c == NL || c == ESC)
662 c = ' ';
663 stuffcharReadbuff(c);
664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 }
666}
667#endif
668
669/*
670 * Append a character to the stuff buffer.
671 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
672 */
673 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100674stuffcharReadbuff(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100676 add_char_buff(&readbuf1, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677}
678
679/*
680 * Append a number to the stuff buffer.
681 */
682 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100683stuffnumReadbuff(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100685 add_num_buff(&readbuf1, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686}
687
688/*
Bram Moolenaar11abd092020-05-01 14:26:37 +0200689 * Stuff a string into the typeahead buffer, such that edit() will insert it
690 * literally ("literally" TRUE) or interpret is as typed characters.
691 */
692 void
693stuffescaped(char_u *arg, int literally)
694{
695 int c;
696 char_u *start;
697
698 while (*arg != NUL)
699 {
700 // Stuff a sequence of normal ASCII characters, that's fast. Also
701 // stuff K_SPECIAL to get the effect of a special key when "literally"
702 // is TRUE.
703 start = arg;
704 while ((*arg >= ' '
705#ifndef EBCDIC
706 && *arg < DEL // EBCDIC: chars above space are normal
707#endif
708 )
709 || (*arg == K_SPECIAL && !literally))
710 ++arg;
711 if (arg > start)
712 stuffReadbuffLen(start, (long)(arg - start));
713
714 // stuff a single special character
715 if (*arg != NUL)
716 {
717 if (has_mbyte)
718 c = mb_cptr2char_adv(&arg);
719 else
720 c = *arg++;
721 if (literally && ((c < ' ' && c != TAB) || c == DEL))
722 stuffcharReadbuff(Ctrl_V);
723 stuffcharReadbuff(c);
724 }
725 }
726}
727
728/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 * Read a character from the redo buffer. Translates K_SPECIAL, CSI and
730 * multibyte characters.
731 * The redo buffer is left as it is.
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100732 * If init is TRUE, prepare for redo, return FAIL if nothing to redo, OK
733 * otherwise.
734 * If old is TRUE, use old_redobuff instead of redobuff.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 */
736 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100737read_redo(int init, int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100739 static buffblock_T *bp;
740 static char_u *p;
741 int c;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100742 int n;
743 char_u buf[MB_MAXBYTES + 1];
744 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745
746 if (init)
747 {
748 if (old_redo)
Bram Moolenaar285e3352018-04-18 23:01:13 +0200749 bp = old_redobuff.bh_first.b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 else
Bram Moolenaar285e3352018-04-18 23:01:13 +0200751 bp = redobuff.bh_first.b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 if (bp == NULL)
753 return FAIL;
754 p = bp->b_str;
755 return OK;
756 }
757 if ((c = *p) != NUL)
758 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100759 // Reverse the conversion done by add_char_buff()
760 // For a multi-byte character get all the bytes and return the
761 // converted character.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 if (has_mbyte && (c != K_SPECIAL || p[1] == KS_SPECIAL))
763 n = MB_BYTE2LEN_CHECK(c);
764 else
765 n = 1;
766 for (i = 0; ; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100768 if (c == K_SPECIAL) // special key or escaped K_SPECIAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 {
770 c = TO_SPECIAL(p[1], p[2]);
771 p += 2;
772 }
773#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +0100774 if (c == CSI) // escaped CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 p += 2;
776#endif
777 if (*++p == NUL && bp->b_next != NULL)
778 {
779 bp = bp->b_next;
780 p = bp->b_str;
781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 buf[i] = c;
Bram Moolenaar30613902019-12-01 22:11:18 +0100783 if (i == n - 1) // last byte of a character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 {
785 if (n != 1)
786 c = (*mb_ptr2char)(buf);
787 break;
788 }
789 c = *p;
Bram Moolenaar30613902019-12-01 22:11:18 +0100790 if (c == NUL) // cannot happen?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 }
793 }
794
795 return c;
796}
797
798/*
799 * Copy the rest of the redo buffer into the stuff buffer (in a slow way).
800 * If old_redo is TRUE, use old_redobuff instead of redobuff.
801 * The escaped K_SPECIAL and CSI are copied without translation.
802 */
803 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100804copy_redo(int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805{
806 int c;
807
808 while ((c = read_redo(FALSE, old_redo)) != NUL)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100809 add_char_buff(&readbuf2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810}
811
812/*
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100813 * Stuff the redo buffer into readbuf2.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 * Insert the redo count into the command.
815 * If "old_redo" is TRUE, the last but one command is repeated
816 * instead of the last command (inserting text). This is used for
817 * CTRL-O <.> in insert mode
818 *
819 * return FAIL for failure, OK otherwise
820 */
821 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100822start_redo(long count, int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823{
824 int c;
825
Bram Moolenaar30613902019-12-01 22:11:18 +0100826 // init the pointers; return if nothing to redo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 if (read_redo(TRUE, old_redo) == FAIL)
828 return FAIL;
829
830 c = read_redo(FALSE, old_redo);
831
Bram Moolenaar30613902019-12-01 22:11:18 +0100832 // copy the buffer name, if present
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 if (c == '"')
834 {
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100835 add_buff(&readbuf2, (char_u *)"\"", 1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 c = read_redo(FALSE, old_redo);
837
Bram Moolenaar30613902019-12-01 22:11:18 +0100838 // if a numbered buffer is used, increment the number
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 if (c >= '1' && c < '9')
840 ++c;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100841 add_char_buff(&readbuf2, c);
Bram Moolenaar833093b2018-05-23 21:53:52 +0200842
Bram Moolenaar30613902019-12-01 22:11:18 +0100843 // the expression register should be re-evaluated
Bram Moolenaar833093b2018-05-23 21:53:52 +0200844 if (c == '=')
845 {
846 add_char_buff(&readbuf2, CAR);
847 cmd_silent = TRUE;
848 }
849
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 c = read_redo(FALSE, old_redo);
851 }
852
Bram Moolenaar30613902019-12-01 22:11:18 +0100853 if (c == 'v') // redo Visual
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 {
855 VIsual = curwin->w_cursor;
856 VIsual_active = TRUE;
857 VIsual_select = FALSE;
858 VIsual_reselect = TRUE;
859 redo_VIsual_busy = TRUE;
860 c = read_redo(FALSE, old_redo);
861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862
Bram Moolenaar30613902019-12-01 22:11:18 +0100863 // try to enter the count (in place of a previous count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 if (count)
865 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100866 while (VIM_ISDIGIT(c)) // skip "old" count
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 c = read_redo(FALSE, old_redo);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100868 add_num_buff(&readbuf2, count);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 }
870
Bram Moolenaar30613902019-12-01 22:11:18 +0100871 // copy from the redo buffer into the stuff buffer
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100872 add_char_buff(&readbuf2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 copy_redo(old_redo);
874 return OK;
875}
876
877/*
878 * Repeat the last insert (R, o, O, a, A, i or I command) by stuffing
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100879 * the redo buffer into readbuf2.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 * return FAIL for failure, OK otherwise
881 */
882 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100883start_redo_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884{
885 int c;
886
887 if (read_redo(TRUE, FALSE) == FAIL)
888 return FAIL;
889 start_stuff();
890
Bram Moolenaar30613902019-12-01 22:11:18 +0100891 // skip the count and the command character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 while ((c = read_redo(FALSE, FALSE)) != NUL)
893 {
894 if (vim_strchr((char_u *)"AaIiRrOo", c) != NULL)
895 {
896 if (c == 'O' || c == 'o')
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100897 add_buff(&readbuf2, NL_STR, -1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 break;
899 }
900 }
901
Bram Moolenaar30613902019-12-01 22:11:18 +0100902 // copy the typed text from the redo buffer into the stuff buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 copy_redo(FALSE);
904 block_redo = TRUE;
905 return OK;
906}
907
908 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100909stop_redo_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910{
911 block_redo = FALSE;
912}
913
914/*
915 * Initialize typebuf.tb_buf to point to typebuf_init.
916 * alloc() cannot be used here: In out-of-memory situations it would
917 * be impossible to type anything.
918 */
919 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100920init_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921{
922 if (typebuf.tb_buf == NULL)
923 {
924 typebuf.tb_buf = typebuf_init;
925 typebuf.tb_noremap = noremapbuf_init;
926 typebuf.tb_buflen = TYPELEN_INIT;
927 typebuf.tb_len = 0;
Bram Moolenaard34f9b12017-04-08 18:41:13 +0200928 typebuf.tb_off = MAXMAPLEN + 4;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 typebuf.tb_change_cnt = 1;
930 }
931}
932
933/*
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200934 * Returns TRUE when keys cannot be remapped.
935 */
936 int
937noremap_keys(void)
938{
939 return KeyNoremap & (RM_NONE|RM_SCRIPT);
940}
941
942/*
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100943 * Insert a string in position 'offset' in the typeahead buffer (for "@r"
944 * and ":normal" command, vgetorpeek() and check_termcode()).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 *
946 * If noremap is REMAP_YES, new string can be mapped again.
947 * If noremap is REMAP_NONE, new string cannot be mapped again.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000948 * If noremap is REMAP_SKIP, fist char of new string cannot be mapped again,
949 * but abbreviations are allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 * If noremap is REMAP_SCRIPT, new string cannot be mapped again, except for
951 * script-local mappings.
952 * If noremap is > 0, that many characters of the new string cannot be mapped.
953 *
954 * If nottyped is TRUE, the string does not return KeyTyped (don't use when
955 * offset is non-zero!).
956 *
957 * If silent is TRUE, cmd_silent is set when the characters are obtained.
958 *
959 * return FAIL for failure, OK otherwise
960 */
961 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100962ins_typebuf(
963 char_u *str,
964 int noremap,
965 int offset,
966 int nottyped,
967 int silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968{
969 char_u *s1, *s2;
970 int newlen;
971 int addlen;
972 int i;
973 int newoff;
974 int val;
975 int nrm;
976
977 init_typebuf();
978 if (++typebuf.tb_change_cnt == 0)
979 typebuf.tb_change_cnt = 1;
Bram Moolenaard103ee72019-09-18 21:15:31 +0200980 state_no_longer_safe("ins_typebuf()");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981
982 addlen = (int)STRLEN(str);
Bram Moolenaar4e86cba2007-05-06 11:56:32 +0000983
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 if (offset == 0 && addlen <= typebuf.tb_off)
985 {
Bram Moolenaarcaa55b62017-01-10 13:51:09 +0100986 /*
987 * Easy case: there is room in front of typebuf.tb_buf[typebuf.tb_off]
988 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 typebuf.tb_off -= addlen;
990 mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen);
991 }
Bram Moolenaard34f9b12017-04-08 18:41:13 +0200992 else if (typebuf.tb_len == 0 && typebuf.tb_buflen
993 >= addlen + 3 * (MAXMAPLEN + 4))
994 {
995 /*
996 * Buffer is empty and string fits in the existing buffer.
997 * Leave some space before and after, if possible.
998 */
999 typebuf.tb_off = (typebuf.tb_buflen - addlen - 3 * (MAXMAPLEN + 4)) / 2;
1000 mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen);
1001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 else
1003 {
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001004 int extra;
1005
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001006 /*
1007 * Need to allocate a new buffer.
Bram Moolenaard34f9b12017-04-08 18:41:13 +02001008 * In typebuf.tb_buf there must always be room for 3 * (MAXMAPLEN + 4)
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001009 * characters. We add some extra room to avoid having to allocate too
1010 * often.
1011 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 newoff = MAXMAPLEN + 4;
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001013 extra = addlen + newoff + 4 * (MAXMAPLEN + 4);
1014 if (typebuf.tb_len > 2147483647 - extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 {
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001016 // string is getting too long for a 32 bit int
Bram Moolenaar30613902019-12-01 22:11:18 +01001017 emsg(_(e_toocompl)); // also calls flush_buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 setcursor();
1019 return FAIL;
1020 }
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001021 newlen = typebuf.tb_len + extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 s1 = alloc(newlen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001023 if (s1 == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 return FAIL;
1025 s2 = alloc(newlen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001026 if (s2 == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 {
1028 vim_free(s1);
1029 return FAIL;
1030 }
1031 typebuf.tb_buflen = newlen;
1032
Bram Moolenaar30613902019-12-01 22:11:18 +01001033 // copy the old chars, before the insertion point
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 mch_memmove(s1 + newoff, typebuf.tb_buf + typebuf.tb_off,
1035 (size_t)offset);
Bram Moolenaar30613902019-12-01 22:11:18 +01001036 // copy the new chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 mch_memmove(s1 + newoff + offset, str, (size_t)addlen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001038 // copy the old chars, after the insertion point, including the NUL at
1039 // the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 mch_memmove(s1 + newoff + offset + addlen,
1041 typebuf.tb_buf + typebuf.tb_off + offset,
1042 (size_t)(typebuf.tb_len - offset + 1));
1043 if (typebuf.tb_buf != typebuf_init)
1044 vim_free(typebuf.tb_buf);
1045 typebuf.tb_buf = s1;
1046
1047 mch_memmove(s2 + newoff, typebuf.tb_noremap + typebuf.tb_off,
1048 (size_t)offset);
1049 mch_memmove(s2 + newoff + offset + addlen,
1050 typebuf.tb_noremap + typebuf.tb_off + offset,
1051 (size_t)(typebuf.tb_len - offset));
1052 if (typebuf.tb_noremap != noremapbuf_init)
1053 vim_free(typebuf.tb_noremap);
1054 typebuf.tb_noremap = s2;
1055
1056 typebuf.tb_off = newoff;
1057 }
1058 typebuf.tb_len += addlen;
1059
Bram Moolenaar30613902019-12-01 22:11:18 +01001060 // If noremap == REMAP_SCRIPT: do remap script-local mappings.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 if (noremap == REMAP_SCRIPT)
1062 val = RM_SCRIPT;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001063 else if (noremap == REMAP_SKIP)
1064 val = RM_ABBR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 else
1066 val = RM_NONE;
1067
1068 /*
1069 * Adjust typebuf.tb_noremap[] for the new characters:
1070 * If noremap == REMAP_NONE or REMAP_SCRIPT: new characters are
1071 * (sometimes) not remappable
1072 * If noremap == REMAP_YES: all the new characters are mappable
1073 * If noremap > 0: "noremap" characters are not remappable, the rest
1074 * mappable
1075 */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001076 if (noremap == REMAP_SKIP)
1077 nrm = 1;
1078 else if (noremap < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 nrm = addlen;
1080 else
1081 nrm = noremap;
1082 for (i = 0; i < addlen; ++i)
1083 typebuf.tb_noremap[typebuf.tb_off + i + offset] =
1084 (--nrm >= 0) ? val : RM_YES;
1085
Bram Moolenaar30613902019-12-01 22:11:18 +01001086 // tb_maplen and tb_silent only remember the length of mapped and/or
1087 // silent mappings at the start of the buffer, assuming that a mapped
1088 // sequence doesn't result in typed characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 if (nottyped || typebuf.tb_maplen > offset)
1090 typebuf.tb_maplen += addlen;
1091 if (silent || typebuf.tb_silent > offset)
1092 {
1093 typebuf.tb_silent += addlen;
1094 cmd_silent = TRUE;
1095 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001096 if (typebuf.tb_no_abbr_cnt && offset == 0) // and not used for abbrev.s
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 typebuf.tb_no_abbr_cnt += addlen;
1098
1099 return OK;
1100}
1101
1102/*
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001103 * Put character "c" back into the typeahead buffer.
1104 * Can be used for a character obtained by vgetc() that needs to be put back.
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00001105 * Uses cmd_silent, KeyTyped and KeyNoremap to restore the flags belonging to
1106 * the char.
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001107 */
1108 void
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001109ins_char_typebuf(int c, int modifier)
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001110{
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001111 char_u buf[MB_MAXBYTES + 4];
1112 int idx = 0;
1113
1114 if (modifier != 0)
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001115 {
1116 buf[0] = K_SPECIAL;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001117 buf[1] = KS_MODIFIER;
1118 buf[2] = modifier;
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001119 buf[3] = NUL;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001120 idx = 3;
1121 }
1122 if (IS_SPECIAL(c))
1123 {
1124 buf[idx] = K_SPECIAL;
1125 buf[idx + 1] = K_SECOND(c);
1126 buf[idx + 2] = K_THIRD(c);
1127 buf[idx + 3] = NUL;
1128 idx += 3;
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001129 }
1130 else
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001131 buf[(*mb_char2bytes)(c, buf + idx) + idx] = NUL;
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00001132 (void)ins_typebuf(buf, KeyNoremap, 0, !KeyTyped, cmd_silent);
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001133}
1134
1135/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 * Return TRUE if the typeahead buffer was changed (while waiting for a
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001137 * character to arrive). Happens when a message was received from a client or
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001138 * from feedkeys().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 * But check in a more generic way to avoid trouble: When "typebuf.tb_buf"
1140 * changed it was reallocated and the old pointer can no longer be used.
1141 * Or "typebuf.tb_off" may have been changed and we would overwrite characters
1142 * that was just added.
1143 */
1144 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001145typebuf_changed(
Bram Moolenaar30613902019-12-01 22:11:18 +01001146 int tb_change_cnt) // old value of typebuf.tb_change_cnt
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147{
1148 return (tb_change_cnt != 0 && (typebuf.tb_change_cnt != tb_change_cnt
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001149#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
1150 || typebuf_was_filled
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151#endif
1152 ));
1153}
1154
1155/*
1156 * Return TRUE if there are no characters in the typeahead buffer that have
1157 * not been typed (result from a mapping or come from ":normal").
1158 */
1159 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001160typebuf_typed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161{
1162 return typebuf.tb_maplen == 0;
1163}
1164
1165/*
1166 * Return the number of characters that are mapped (or not typed).
1167 */
1168 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001169typebuf_maplen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170{
1171 return typebuf.tb_maplen;
1172}
1173
1174/*
1175 * remove "len" characters from typebuf.tb_buf[typebuf.tb_off + offset]
1176 */
1177 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001178del_typebuf(int len, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179{
1180 int i;
1181
1182 if (len == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01001183 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184
1185 typebuf.tb_len -= len;
1186
1187 /*
1188 * Easy case: Just increase typebuf.tb_off.
1189 */
1190 if (offset == 0 && typebuf.tb_buflen - (typebuf.tb_off + len)
1191 >= 3 * MAXMAPLEN + 3)
1192 typebuf.tb_off += len;
1193 /*
1194 * Have to move the characters in typebuf.tb_buf[] and typebuf.tb_noremap[]
1195 */
1196 else
1197 {
1198 i = typebuf.tb_off + offset;
1199 /*
1200 * Leave some extra room at the end to avoid reallocation.
1201 */
1202 if (typebuf.tb_off > MAXMAPLEN)
1203 {
1204 mch_memmove(typebuf.tb_buf + MAXMAPLEN,
1205 typebuf.tb_buf + typebuf.tb_off, (size_t)offset);
1206 mch_memmove(typebuf.tb_noremap + MAXMAPLEN,
1207 typebuf.tb_noremap + typebuf.tb_off, (size_t)offset);
1208 typebuf.tb_off = MAXMAPLEN;
1209 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001210 // adjust typebuf.tb_buf (include the NUL at the end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset,
1212 typebuf.tb_buf + i + len,
1213 (size_t)(typebuf.tb_len - offset + 1));
Bram Moolenaar30613902019-12-01 22:11:18 +01001214 // adjust typebuf.tb_noremap[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 mch_memmove(typebuf.tb_noremap + typebuf.tb_off + offset,
1216 typebuf.tb_noremap + i + len,
1217 (size_t)(typebuf.tb_len - offset));
1218 }
1219
Bram Moolenaar30613902019-12-01 22:11:18 +01001220 if (typebuf.tb_maplen > offset) // adjust tb_maplen
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 {
1222 if (typebuf.tb_maplen < offset + len)
1223 typebuf.tb_maplen = offset;
1224 else
1225 typebuf.tb_maplen -= len;
1226 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001227 if (typebuf.tb_silent > offset) // adjust tb_silent
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 {
1229 if (typebuf.tb_silent < offset + len)
1230 typebuf.tb_silent = offset;
1231 else
1232 typebuf.tb_silent -= len;
1233 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001234 if (typebuf.tb_no_abbr_cnt > offset) // adjust tb_no_abbr_cnt
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 {
1236 if (typebuf.tb_no_abbr_cnt < offset + len)
1237 typebuf.tb_no_abbr_cnt = offset;
1238 else
1239 typebuf.tb_no_abbr_cnt -= len;
1240 }
1241
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001242#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar30613902019-12-01 22:11:18 +01001243 // Reset the flag that text received from a client or from feedkeys()
1244 // was inserted in the typeahead buffer.
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001245 typebuf_was_filled = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246#endif
1247 if (++typebuf.tb_change_cnt == 0)
1248 typebuf.tb_change_cnt = 1;
1249}
1250
1251/*
1252 * Write typed characters to script file.
1253 * If recording is on put the character in the recordbuffer.
1254 */
1255 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001256gotchars(char_u *chars, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257{
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001258 char_u *s = chars;
1259 int i;
1260 static char_u buf[4];
1261 static int buflen = 0;
1262 int todo = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263
Bram Moolenaar4e86cba2007-05-06 11:56:32 +00001264 while (todo--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 {
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001266 buf[buflen++] = *s++;
1267
1268 // When receiving a special key sequence, store it until we have all
1269 // the bytes and we can decide what to do with it.
1270 if (buflen == 1 && buf[0] == K_SPECIAL)
1271 continue;
1272 if (buflen == 2)
1273 continue;
1274 if (buflen == 3 && buf[1] == KS_EXTRA
1275 && (buf[2] == KE_FOCUSGAINED || buf[2] == KE_FOCUSLOST))
1276 {
1277 // Drop K_FOCUSGAINED and K_FOCUSLOST, they are not useful in a
1278 // recording.
1279 buflen = 0;
1280 continue;
1281 }
1282
Bram Moolenaar30613902019-12-01 22:11:18 +01001283 // Handle one byte at a time; no translation to be done.
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001284 for (i = 0; i < buflen; ++i)
1285 updatescript(buf[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001287 if (reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 {
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001289 buf[buflen] = NUL;
1290 add_buff(&recordbuff, buf, (long)buflen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001291 // remember how many chars were last recorded
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001292 last_recorded_len += buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 }
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001294 buflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 }
1296 may_sync_undo();
1297
1298#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01001299 // output "debug mode" message next time in debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 debug_did_msg = FALSE;
1301#endif
1302
Bram Moolenaar30613902019-12-01 22:11:18 +01001303 // Since characters have been typed, consider the following to be in
1304 // another mapping. Search string will be kept in history.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 ++maptick;
1306}
1307
1308/*
1309 * Sync undo. Called when typed characters are obtained from the typeahead
1310 * buffer, or when a menu is used.
1311 * Do not sync:
1312 * - In Insert mode, unless cursor key has been used.
1313 * - While reading a script file.
1314 * - When no_u_sync is non-zero.
1315 */
1316 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001317may_sync_undo(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318{
1319 if ((!(State & (INSERT + CMDLINE)) || arrow_used)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001320 && scriptin[curscript] == NULL)
1321 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322}
1323
1324/*
1325 * Make "typebuf" empty and allocate new buffers.
1326 * Returns FAIL when out of memory.
1327 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001328 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001329alloc_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330{
1331 typebuf.tb_buf = alloc(TYPELEN_INIT);
1332 typebuf.tb_noremap = alloc(TYPELEN_INIT);
1333 if (typebuf.tb_buf == NULL || typebuf.tb_noremap == NULL)
1334 {
1335 free_typebuf();
1336 return FAIL;
1337 }
1338 typebuf.tb_buflen = TYPELEN_INIT;
Bram Moolenaar30613902019-12-01 22:11:18 +01001339 typebuf.tb_off = MAXMAPLEN + 4; // can insert without realloc
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 typebuf.tb_len = 0;
1341 typebuf.tb_maplen = 0;
1342 typebuf.tb_silent = 0;
1343 typebuf.tb_no_abbr_cnt = 0;
1344 if (++typebuf.tb_change_cnt == 0)
1345 typebuf.tb_change_cnt = 1;
Bram Moolenaare49b4bb2020-03-11 13:01:40 +01001346#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
1347 typebuf_was_filled = FALSE;
1348#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 return OK;
1350}
1351
1352/*
1353 * Free the buffers of "typebuf".
1354 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001355 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001356free_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357{
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001358 if (typebuf.tb_buf == typebuf_init)
Bram Moolenaar95f09602016-11-10 20:01:45 +01001359 internal_error("Free typebuf 1");
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001360 else
Bram Moolenaar0f1c6702019-09-28 15:24:00 +02001361 VIM_CLEAR(typebuf.tb_buf);
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00001362 if (typebuf.tb_noremap == noremapbuf_init)
Bram Moolenaar95f09602016-11-10 20:01:45 +01001363 internal_error("Free typebuf 2");
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001364 else
Bram Moolenaar0f1c6702019-09-28 15:24:00 +02001365 VIM_CLEAR(typebuf.tb_noremap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366}
1367
1368/*
1369 * When doing ":so! file", the current typeahead needs to be saved, and
1370 * restored when "file" has been read completely.
1371 */
1372static typebuf_T saved_typebuf[NSCRIPT];
1373
1374 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001375save_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376{
1377 init_typebuf();
1378 saved_typebuf[curscript] = typebuf;
Bram Moolenaar30613902019-12-01 22:11:18 +01001379 // If out of memory: restore typebuf and close file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 if (alloc_typebuf() == FAIL)
1381 {
1382 closescript();
1383 return FAIL;
1384 }
1385 return OK;
1386}
1387
Bram Moolenaar30613902019-12-01 22:11:18 +01001388static int old_char = -1; // character put back by vungetc()
1389static int old_mod_mask; // mod_mask for ungotten character
1390static int old_mouse_row; // mouse_row related to old_char
1391static int old_mouse_col; // mouse_col related to old_char
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001392
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393/*
1394 * Save all three kinds of typeahead, so that the user must type at a prompt.
1395 */
1396 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001397save_typeahead(tasave_T *tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398{
1399 tp->save_typebuf = typebuf;
1400 tp->typebuf_valid = (alloc_typebuf() == OK);
1401 if (!tp->typebuf_valid)
1402 typebuf = tp->save_typebuf;
1403
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001404 tp->old_char = old_char;
1405 tp->old_mod_mask = old_mod_mask;
1406 old_char = -1;
1407
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001408 tp->save_readbuf1 = readbuf1;
Bram Moolenaar285e3352018-04-18 23:01:13 +02001409 readbuf1.bh_first.b_next = NULL;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001410 tp->save_readbuf2 = readbuf2;
Bram Moolenaar285e3352018-04-18 23:01:13 +02001411 readbuf2.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412# ifdef USE_INPUT_BUF
1413 tp->save_inputbuf = get_input_buf();
1414# endif
1415}
1416
1417/*
1418 * Restore the typeahead to what it was before calling save_typeahead().
1419 * The allocated memory is freed, can only be called once!
Bram Moolenaarc41badb2021-06-07 22:04:52 +02001420 * When "overwrite" is FALSE input typed later is kept.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 */
1422 void
Bram Moolenaarc41badb2021-06-07 22:04:52 +02001423restore_typeahead(tasave_T *tp, int overwrite UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424{
1425 if (tp->typebuf_valid)
1426 {
1427 free_typebuf();
1428 typebuf = tp->save_typebuf;
1429 }
1430
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001431 old_char = tp->old_char;
1432 old_mod_mask = tp->old_mod_mask;
1433
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001434 free_buff(&readbuf1);
1435 readbuf1 = tp->save_readbuf1;
1436 free_buff(&readbuf2);
1437 readbuf2 = tp->save_readbuf2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438# ifdef USE_INPUT_BUF
Bram Moolenaarc41badb2021-06-07 22:04:52 +02001439 set_input_buf(tp->save_inputbuf, overwrite);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440# endif
1441}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442
1443/*
1444 * Open a new script file for the ":source!" command.
1445 */
1446 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001447openscript(
1448 char_u *name,
Bram Moolenaar30613902019-12-01 22:11:18 +01001449 int directly) // when TRUE execute directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450{
1451 if (curscript + 1 == NSCRIPT)
1452 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001453 emsg(_(e_scripts_nested_too_deep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 return;
1455 }
Bram Moolenaar53575522019-05-22 22:38:25 +02001456
1457 // Disallow sourcing a file in the sandbox, the commands would be executed
1458 // later, possibly outside of the sandbox.
1459 if (check_secure())
1460 return;
1461
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001462#ifdef FEAT_EVAL
1463 if (ignore_script)
Bram Moolenaar30613902019-12-01 22:11:18 +01001464 // Not reading from script, also don't open one. Warning message?
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001465 return;
1466#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467
Bram Moolenaar30613902019-12-01 22:11:18 +01001468 if (scriptin[curscript] != NULL) // already reading script
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 ++curscript;
Bram Moolenaar30613902019-12-01 22:11:18 +01001470 // use NameBuff for expanded name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 expand_env(name, NameBuff, MAXPATHL);
1472 if ((scriptin[curscript] = mch_fopen((char *)NameBuff, READBIN)) == NULL)
1473 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001474 semsg(_(e_notopen), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 if (curscript)
1476 --curscript;
1477 return;
1478 }
1479 if (save_typebuf() == FAIL)
1480 return;
1481
1482 /*
1483 * Execute the commands from the file right now when using ":source!"
1484 * after ":global" or ":argdo" or in a loop. Also when another command
1485 * follows. This means the display won't be updated. Don't do this
1486 * always, "make test" would fail.
1487 */
1488 if (directly)
1489 {
1490 oparg_T oa;
1491 int oldcurscript;
1492 int save_State = State;
1493 int save_restart_edit = restart_edit;
1494 int save_insertmode = p_im;
1495 int save_finish_op = finish_op;
1496 int save_msg_scroll = msg_scroll;
1497
1498 State = NORMAL;
Bram Moolenaar30613902019-12-01 22:11:18 +01001499 msg_scroll = FALSE; // no msg scrolling in Normal mode
1500 restart_edit = 0; // don't go to Insert mode
1501 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 clear_oparg(&oa);
1503 finish_op = FALSE;
1504
1505 oldcurscript = curscript;
1506 do
1507 {
Bram Moolenaar386b43e2019-05-19 21:57:11 +02001508 update_topline_cursor(); // update cursor position and topline
1509 normal_cmd(&oa, FALSE); // execute one command
Bram Moolenaarae97b942020-07-09 19:16:35 +02001510 (void)vpeekc(); // check for end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 }
1512 while (scriptin[oldcurscript] != NULL);
1513
1514 State = save_State;
1515 msg_scroll = save_msg_scroll;
1516 restart_edit = save_restart_edit;
1517 p_im = save_insertmode;
1518 finish_op = save_finish_op;
1519 }
1520}
1521
1522/*
1523 * Close the currently active input script.
1524 */
1525 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001526closescript(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527{
1528 free_typebuf();
1529 typebuf = saved_typebuf[curscript];
1530
1531 fclose(scriptin[curscript]);
1532 scriptin[curscript] = NULL;
1533 if (curscript > 0)
1534 --curscript;
1535}
1536
Bram Moolenaarea408852005-06-25 22:49:46 +00001537#if defined(EXITFREE) || defined(PROTO)
1538 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001539close_all_scripts(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00001540{
1541 while (scriptin[0] != NULL)
1542 closescript();
1543}
1544#endif
1545
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546/*
1547 * Return TRUE when reading keys from a script file.
1548 */
1549 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001550using_script(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551{
1552 return scriptin[curscript] != NULL;
1553}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554
1555/*
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001556 * This function is called just before doing a blocking wait. Thus after
1557 * waiting 'updatetime' for a character to arrive.
1558 */
1559 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001560before_blocking(void)
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001561{
1562 updatescript(0);
1563#ifdef FEAT_EVAL
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001564 if (may_garbage_collect)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001565 garbage_collect(FALSE);
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001566#endif
1567}
1568
1569/*
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001570 * updatescript() is called when a character can be written into the script file
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 * or when we have waited some time for a character (c == 0)
1572 *
1573 * All the changed memfiles are synced if c == 0 or when the number of typed
1574 * characters reaches 'updatecount' and 'updatecount' is non-zero.
1575 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001576 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001577updatescript(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578{
1579 static int count = 0;
1580
1581 if (c && scriptout)
1582 putc(c, scriptout);
1583 if (c == 0 || (p_uc > 0 && ++count >= p_uc))
1584 {
1585 ml_sync_all(c == 0, TRUE);
1586 count = 0;
1587 }
1588}
1589
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590/*
Bram Moolenaar975a8802020-06-06 22:36:24 +02001591 * Convert "c" plus "modifiers" to merge the effect of modifyOtherKeys into the
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001592 * character.
1593 */
1594 int
Bram Moolenaar975a8802020-06-06 22:36:24 +02001595merge_modifyOtherKeys(int c_arg, int *modifiers)
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001596{
1597 int c = c_arg;
1598
Bram Moolenaar975a8802020-06-06 22:36:24 +02001599 if (*modifiers & MOD_MASK_CTRL)
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001600 {
1601 if ((c >= '`' && c <= 0x7f) || (c >= '@' && c <= '_'))
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001602 c &= 0x1f;
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001603 else if (c == '6')
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001604 // CTRL-6 is equivalent to CTRL-^
1605 c = 0x1e;
Bram Moolenaarf4ae6b22020-05-30 19:52:46 +02001606#ifdef FEAT_GUI_GTK
1607 // These mappings look arbitrary at the first glance, but in fact
1608 // resemble quite exactly the behaviour of the GTK+ 1.2 GUI on my
1609 // machine. The only difference is BS vs. DEL for CTRL-8 (makes
1610 // more sense and is consistent with usual terminal behaviour).
1611 else if (c == '2')
1612 c = NUL;
1613 else if (c >= '3' && c <= '7')
1614 c = c ^ 0x28;
1615 else if (c == '8')
1616 c = BS;
1617 else if (c == '?')
1618 c = DEL;
1619#endif
1620 if (c != c_arg)
Bram Moolenaar975a8802020-06-06 22:36:24 +02001621 *modifiers &= ~MOD_MASK_CTRL;
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001622 }
Bram Moolenaar975a8802020-06-06 22:36:24 +02001623 if ((*modifiers & (MOD_MASK_META | MOD_MASK_ALT))
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001624 && c >= 0 && c <= 127)
1625 {
1626 c += 0x80;
Bram Moolenaar975a8802020-06-06 22:36:24 +02001627 *modifiers &= ~(MOD_MASK_META|MOD_MASK_ALT);
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001628 }
1629 return c;
1630}
1631
1632/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 * Get the next input character.
1634 * Can return a special key or a multi-byte character.
1635 * Can return NUL when called recursively, use safe_vgetc() if that's not
1636 * wanted.
1637 * This translates escaped K_SPECIAL and CSI bytes to a K_SPECIAL or CSI byte.
1638 * Collects the bytes of a multibyte character into the whole character.
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00001639 * Returns the modifiers in the global "mod_mask".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 */
1641 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001642vgetc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643{
1644 int c, c2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001646 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001649#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01001650 // Do garbage collection when garbagecollect() was called previously and
1651 // we are now at the toplevel.
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001652 if (may_garbage_collect && want_garbage_collect)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001653 garbage_collect(FALSE);
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001654#endif
1655
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 /*
1657 * If a character was put back with vungetc, it was already processed.
1658 * Return it directly.
1659 */
1660 if (old_char != -1)
1661 {
1662 c = old_char;
1663 old_char = -1;
1664 mod_mask = old_mod_mask;
Bram Moolenaarb8978712013-03-16 21:42:16 +01001665 mouse_row = old_mouse_row;
1666 mouse_col = old_mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001668 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 {
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001670 mod_mask = 0;
1671 vgetc_mod_mask = 0;
1672 vgetc_char = 0;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001673 last_recorded_len = 0;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001674
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001675 for (;;) // this is done twice if there are modifiers
1676 {
1677 int did_inc = FALSE;
Bram Moolenaar2455c4e2015-09-15 18:29:39 +02001678
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001679 if (mod_mask
Bram Moolenaar39719052017-09-05 22:20:46 +02001680#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001681 || im_is_preediting()
Bram Moolenaar39719052017-09-05 22:20:46 +02001682#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001683#if defined(FEAT_PROP_POPUP)
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001684 || popup_no_mapping()
1685#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001686 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 {
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001688 // no mapping after modifier has been read
1689 ++no_mapping;
1690 ++allow_keys;
1691 did_inc = TRUE; // mod_mask may change value
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 }
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001693 c = vgetorpeek(TRUE);
1694 if (did_inc)
1695 {
1696 --no_mapping;
1697 --allow_keys;
1698 }
1699
1700 // Get two extra bytes for special keys
1701 if (c == K_SPECIAL
1702#ifdef FEAT_GUI
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001703 || (c == CSI)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001704#endif
1705 )
1706 {
1707 int save_allow_keys = allow_keys;
1708
1709 ++no_mapping;
1710 allow_keys = 0; // make sure BS is not found
1711 c2 = vgetorpeek(TRUE); // no mapping for these chars
1712 c = vgetorpeek(TRUE);
1713 --no_mapping;
1714 allow_keys = save_allow_keys;
1715 if (c2 == KS_MODIFIER)
1716 {
1717 mod_mask = c;
1718 continue;
1719 }
1720 c = TO_SPECIAL(c2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721
Bram Moolenaar4f974752019-02-17 17:44:42 +01001722#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001723 // Handle K_TEAROFF here, the caller of vgetc() doesn't need to
1724 // know that a menu was torn off
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001725 if (
1726# ifdef VIMDLL
1727 gui.in_use &&
1728# endif
1729 c == K_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 {
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001731 char_u name[200];
1732 int i;
1733
1734 // get menu path, it ends with a <CR>
1735 for (i = 0; (c = vgetorpeek(TRUE)) != '\r'; )
1736 {
1737 name[i] = c;
1738 if (i < 199)
1739 ++i;
1740 }
1741 name[i] = NUL;
1742 gui_make_tearoff(name);
1743 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745#endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001746#if defined(FEAT_GUI) && defined(FEAT_GUI_GTK) && defined(FEAT_MENU)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001747 // GTK: <F10> normally selects the menu, but it's passed until
1748 // here to allow mapping it. Intercept and invoke the GTK
1749 // behavior if it's not mapped.
1750 if (c == K_F10 && gui.menubar != NULL)
1751 {
1752 gtk_menu_shell_select_first(
1753 GTK_MENU_SHELL(gui.menubar), FALSE);
1754 continue;
1755 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001756#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757#ifdef FEAT_GUI
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001758 // Handle focus event here, so that the caller doesn't need to
1759 // know about it. Return K_IGNORE so that we loop once (needed
1760 // if 'lazyredraw' is set).
1761 if (c == K_FOCUSGAINED || c == K_FOCUSLOST)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001762 {
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001763 ui_focus_change(c == K_FOCUSGAINED);
1764 c = K_IGNORE;
Bram Moolenaara9dd2d32019-04-29 21:58:41 +02001765 }
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001766
1767 // Translate K_CSI to CSI. The special key is only used to
1768 // avoid it being recognized as the start of a special key.
1769 if (c == K_CSI)
1770 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001772 }
1773 // a keypad or special function key was not mapped, use it like
1774 // its ASCII equivalent
1775 switch (c)
1776 {
1777 case K_KPLUS: c = '+'; break;
1778 case K_KMINUS: c = '-'; break;
1779 case K_KDIVIDE: c = '/'; break;
1780 case K_KMULTIPLY: c = '*'; break;
1781 case K_KENTER: c = CAR; break;
1782 case K_KPOINT:
Bram Moolenaar4f974752019-02-17 17:44:42 +01001783#ifdef MSWIN
Bram Moolenaar8e85db02018-07-27 23:16:51 +02001784 // Can be either '.' or a ',',
1785 // depending on the type of keypad.
1786 c = MapVirtualKey(VK_DECIMAL, 2); break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001787#else
Bram Moolenaar8e85db02018-07-27 23:16:51 +02001788 c = '.'; break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001789#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001790 case K_K0: c = '0'; break;
1791 case K_K1: c = '1'; break;
1792 case K_K2: c = '2'; break;
1793 case K_K3: c = '3'; break;
1794 case K_K4: c = '4'; break;
1795 case K_K5: c = '5'; break;
1796 case K_K6: c = '6'; break;
1797 case K_K7: c = '7'; break;
1798 case K_K8: c = '8'; break;
1799 case K_K9: c = '9'; break;
Bram Moolenaara88d9682005-03-25 21:45:43 +00001800
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001801 case K_XHOME:
1802 case K_ZHOME: if (mod_mask == MOD_MASK_SHIFT)
Bram Moolenaara88d9682005-03-25 21:45:43 +00001803 {
1804 c = K_S_HOME;
1805 mod_mask = 0;
1806 }
1807 else if (mod_mask == MOD_MASK_CTRL)
1808 {
1809 c = K_C_HOME;
1810 mod_mask = 0;
1811 }
1812 else
1813 c = K_HOME;
1814 break;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001815 case K_XEND:
1816 case K_ZEND: if (mod_mask == MOD_MASK_SHIFT)
Bram Moolenaara88d9682005-03-25 21:45:43 +00001817 {
1818 c = K_S_END;
1819 mod_mask = 0;
1820 }
1821 else if (mod_mask == MOD_MASK_CTRL)
1822 {
1823 c = K_C_END;
1824 mod_mask = 0;
1825 }
1826 else
1827 c = K_END;
1828 break;
1829
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001830 case K_XUP: c = K_UP; break;
1831 case K_XDOWN: c = K_DOWN; break;
1832 case K_XLEFT: c = K_LEFT; break;
1833 case K_XRIGHT: c = K_RIGHT; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001836 // For a multi-byte character get all the bytes and return the
1837 // converted character.
1838 // Note: This will loop until enough bytes are received!
1839 if (has_mbyte && (n = MB_BYTE2LEN_CHECK(c)) > 1)
1840 {
1841 ++no_mapping;
1842 buf[0] = c;
1843 for (i = 1; i < n; ++i)
1844 {
1845 buf[i] = vgetorpeek(TRUE);
1846 if (buf[i] == K_SPECIAL
1847#ifdef FEAT_GUI
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001848 || (buf[i] == CSI)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001849#endif
1850 )
1851 {
1852 // Must be a K_SPECIAL - KS_SPECIAL - KE_FILLER
1853 // sequence, which represents a K_SPECIAL (0x80),
1854 // or a CSI - KS_EXTRA - KE_CSI sequence, which
1855 // represents a CSI (0x9B),
1856 // or a K_SPECIAL - KS_EXTRA - KE_CSI, which is CSI
1857 // too.
1858 c = vgetorpeek(TRUE);
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01001859 if (vgetorpeek(TRUE) == KE_CSI && c == KS_EXTRA)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001860 buf[i] = CSI;
1861 }
1862 }
1863 --no_mapping;
1864 c = (*mb_ptr2char)(buf);
1865 }
1866
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02001867 if (vgetc_char == 0)
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001868 {
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001869 vgetc_mod_mask = mod_mask;
1870 vgetc_char = c;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001871 }
Bram Moolenaar00eab7f2019-10-10 21:49:28 +02001872
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001873 break;
1874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001876
1877#ifdef FEAT_EVAL
1878 /*
1879 * In the main loop "may_garbage_collect" can be set to do garbage
1880 * collection in the first next vgetc(). It's disabled after that to
1881 * avoid internally used Lists and Dicts to be freed.
1882 */
1883 may_garbage_collect = FALSE;
1884#endif
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001885
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001886#ifdef FEAT_BEVAL_TERM
Bram Moolenaarc2f50542019-07-05 23:24:56 +02001887 if (c != K_MOUSEMOVE && c != K_IGNORE && c != K_CURSORHOLD)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001888 {
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001889 // Don't trigger 'balloonexpr' unless only the mouse was moved.
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001890 bevalexpr_due_set = FALSE;
1891 ui_remove_balloon();
1892 }
1893#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001894#ifdef FEAT_PROP_POPUP
Bram Moolenaar4934ad02020-09-28 22:29:58 +02001895 // Only filter keys that do not come from ":normal". Keys from feedkeys()
1896 // are filtered.
1897 if ((!ex_normal_busy || in_feedkeys) && popup_do_filter(c))
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02001898 {
1899 if (c == Ctrl_C)
1900 got_int = FALSE; // avoid looping
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001901 c = K_IGNORE;
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02001902 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001903#endif
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001904
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001905 // Need to process the character before we know it's safe to do something
1906 // else.
1907 if (c != K_IGNORE)
Bram Moolenaard103ee72019-09-18 21:15:31 +02001908 state_no_longer_safe("key typed");
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001909
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001910 return c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911}
1912
1913/*
1914 * Like vgetc(), but never return a NUL when called recursively, get a key
1915 * directly from the user (ignoring typeahead).
1916 */
1917 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001918safe_vgetc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919{
1920 int c;
1921
1922 c = vgetc();
1923 if (c == NUL)
1924 c = get_keystroke();
1925 return c;
1926}
1927
1928/*
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001929 * Like safe_vgetc(), but loop to handle K_IGNORE.
1930 * Also ignore scrollbar events.
1931 */
1932 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001933plain_vgetc(void)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001934{
1935 int c;
1936
1937 do
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001938 c = safe_vgetc();
Bram Moolenaar3a006592021-05-05 19:58:17 +02001939 while (c == K_IGNORE
1940 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
1941 || c == K_MOUSEMOVE);
Bram Moolenaarec2da362017-01-21 20:04:22 +01001942
1943 if (c == K_PS)
Bram Moolenaar30613902019-12-01 22:11:18 +01001944 // Only handle the first pasted character. Drop the rest, since we
1945 // don't know what to do with it.
Bram Moolenaarec2da362017-01-21 20:04:22 +01001946 c = bracketed_paste(PASTE_ONE_CHAR, FALSE, NULL);
1947
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001948 return c;
1949}
1950
1951/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 * Check if a character is available, such that vgetc() will not block.
1953 * If the next character is a special character or multi-byte, the returned
1954 * character is not valid!.
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02001955 * Returns NUL if no character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 */
1957 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001958vpeekc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959{
1960 if (old_char != -1)
1961 return old_char;
1962 return vgetorpeek(FALSE);
1963}
1964
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01001965#if defined(FEAT_TERMRESPONSE) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966/*
1967 * Like vpeekc(), but don't allow mapping. Do allow checking for terminal
1968 * codes.
1969 */
1970 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001971vpeekc_nomap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972{
1973 int c;
1974
1975 ++no_mapping;
1976 ++allow_keys;
1977 c = vpeekc();
1978 --no_mapping;
1979 --allow_keys;
1980 return c;
1981}
1982#endif
1983
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984/*
1985 * Check if any character is available, also half an escape sequence.
1986 * Trick: when no typeahead found, but there is something in the typeahead
1987 * buffer, it must be an ESC that is recognized as the start of a key code.
1988 */
1989 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001990vpeekc_any(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991{
1992 int c;
1993
1994 c = vpeekc();
1995 if (c == NUL && typebuf.tb_len > 0)
1996 c = ESC;
1997 return c;
1998}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999
2000/*
2001 * Call vpeekc() without causing anything to be mapped.
2002 * Return TRUE if a character is available, FALSE otherwise.
2003 */
2004 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002005char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006{
2007 int retval;
2008
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01002009#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01002010 // When test_override("char_avail", 1) was called pretend there is no
2011 // typeahead.
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01002012 if (disable_char_avail_for_testing)
2013 return FALSE;
2014#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 ++no_mapping;
2016 retval = vpeekc();
2017 --no_mapping;
2018 return (retval != NUL);
2019}
2020
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002021#if defined(FEAT_EVAL) || defined(PROTO)
2022/*
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002023 * "getchar()" and "getcharstr()" functions
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002024 */
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002025 static void
2026getchar_common(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002027{
2028 varnumber_T n;
2029 int error = FALSE;
2030
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002031 if (in_vim9script() && check_for_opt_bool_arg(argvars, 0) == FAIL)
2032 return;
2033
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002034#ifdef MESSAGE_QUEUE
2035 // vpeekc() used to check for messages, but that caused problems, invoking
2036 // a callback where it was not expected. Some plugins use getchar(1) in a
2037 // loop to await a message, therefore make sure we check for messages here.
2038 parse_queued_messages();
2039#endif
2040
Bram Moolenaar30613902019-12-01 22:11:18 +01002041 // Position the cursor. Needed after a message that ends in a space.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002042 windgoto(msg_row, msg_col);
2043
2044 ++no_mapping;
2045 ++allow_keys;
2046 for (;;)
2047 {
2048 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar30613902019-12-01 22:11:18 +01002049 // getchar(): blocking wait.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002050 n = plain_vgetc();
Bram Moolenaarc08cc722020-09-05 17:51:23 +02002051 else if (tv_get_bool_chk(&argvars[0], &error))
Bram Moolenaar30613902019-12-01 22:11:18 +01002052 // getchar(1): only check if char avail
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002053 n = vpeekc_any();
2054 else if (error || vpeekc_any() == NUL)
Bram Moolenaar30613902019-12-01 22:11:18 +01002055 // illegal argument or getchar(0) and no char avail: return zero
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002056 n = 0;
2057 else
Bram Moolenaar15183b42020-09-05 19:59:39 +02002058 // getchar(0) and char avail() != NUL: get a character.
2059 // Note that vpeekc_any() returns K_SPECIAL for K_IGNORE.
2060 n = safe_vgetc();
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002061
Bram Moolenaar15183b42020-09-05 19:59:39 +02002062 if (n == K_IGNORE || n == K_MOUSEMOVE
2063 || n == K_VER_SCROLLBAR || n == K_HOR_SCROLLBAR)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002064 continue;
2065 break;
2066 }
2067 --no_mapping;
2068 --allow_keys;
2069
2070 set_vim_var_nr(VV_MOUSE_WIN, 0);
2071 set_vim_var_nr(VV_MOUSE_WINID, 0);
2072 set_vim_var_nr(VV_MOUSE_LNUM, 0);
2073 set_vim_var_nr(VV_MOUSE_COL, 0);
2074
2075 rettv->vval.v_number = n;
2076 if (IS_SPECIAL(n) || mod_mask != 0)
2077 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002078 char_u temp[10]; // modifier: 3, mbyte-char: 6, NUL: 1
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002079 int i = 0;
2080
Bram Moolenaar30613902019-12-01 22:11:18 +01002081 // Turn a special key into three bytes, plus modifier.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002082 if (mod_mask != 0)
2083 {
2084 temp[i++] = K_SPECIAL;
2085 temp[i++] = KS_MODIFIER;
2086 temp[i++] = mod_mask;
2087 }
2088 if (IS_SPECIAL(n))
2089 {
2090 temp[i++] = K_SPECIAL;
2091 temp[i++] = K_SECOND(n);
2092 temp[i++] = K_THIRD(n);
2093 }
2094 else if (has_mbyte)
2095 i += (*mb_char2bytes)(n, temp + i);
2096 else
2097 temp[i++] = n;
2098 temp[i++] = NUL;
2099 rettv->v_type = VAR_STRING;
2100 rettv->vval.v_string = vim_strsave(temp);
2101
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002102 if (is_mouse_key(n))
2103 {
2104 int row = mouse_row;
2105 int col = mouse_col;
2106 win_T *win;
2107 linenr_T lnum;
2108 win_T *wp;
2109 int winnr = 1;
2110
2111 if (row >= 0 && col >= 0)
2112 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002113 // Find the window at the mouse coordinates and compute the
2114 // text position.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002115 win = mouse_find_win(&row, &col, FIND_POPUP);
2116 if (win == NULL)
2117 return;
2118 (void)mouse_comp_pos(win, &row, &col, &lnum, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002119#ifdef FEAT_PROP_POPUP
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002120 if (WIN_IS_POPUP(win))
2121 winnr = 0;
2122 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002123#endif
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002124 for (wp = firstwin; wp != win && wp != NULL;
2125 wp = wp->w_next)
2126 ++winnr;
2127 set_vim_var_nr(VV_MOUSE_WIN, winnr);
2128 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
2129 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
2130 set_vim_var_nr(VV_MOUSE_COL, col + 1);
2131 }
2132 }
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002133 }
2134}
2135
2136/*
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002137 * "getchar()" function
2138 */
2139 void
2140f_getchar(typval_T *argvars, typval_T *rettv)
2141{
2142 getchar_common(argvars, rettv);
2143}
2144
2145/*
2146 * "getcharstr()" function
2147 */
2148 void
2149f_getcharstr(typval_T *argvars, typval_T *rettv)
2150{
2151 getchar_common(argvars, rettv);
2152
2153 if (rettv->v_type == VAR_NUMBER)
2154 {
2155 char_u temp[7]; // mbyte-char: 6, NUL: 1
2156 varnumber_T n = rettv->vval.v_number;
2157 int i = 0;
2158
2159 if (n != 0)
2160 {
2161 if (has_mbyte)
2162 i += (*mb_char2bytes)(n, temp + i);
2163 else
2164 temp[i++] = n;
2165 }
2166 temp[i++] = NUL;
2167 rettv->v_type = VAR_STRING;
2168 rettv->vval.v_string = vim_strsave(temp);
2169 }
2170}
2171
2172/*
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002173 * "getcharmod()" function
2174 */
2175 void
2176f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
2177{
2178 rettv->vval.v_number = mod_mask;
2179}
2180#endif // FEAT_EVAL
2181
2182#if defined(MESSAGE_QUEUE) || defined(PROTO)
2183# define MAX_REPEAT_PARSE 8
2184
2185/*
2186 * Process messages that have been queued for netbeans or clientserver.
2187 * Also check if any jobs have ended.
h-east965d2ed2021-10-04 21:51:57 +01002188 * These functions can call arbitrary Vim script and should only be called when
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002189 * it is safe to do so.
2190 */
2191 void
2192parse_queued_messages(void)
2193{
Bram Moolenaar1cac7092019-10-22 21:54:31 +02002194 int old_curwin_id;
2195 int old_curbuf_fnum;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002196 int i;
2197 int save_may_garbage_collect = may_garbage_collect;
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002198 static int entered = 0;
Bram Moolenaard103ee72019-09-18 21:15:31 +02002199 int was_safe = get_was_safe_state();
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002200
2201 // Do not handle messages while redrawing, because it may cause buffers to
2202 // change or be wiped while they are being redrawn.
Bram Moolenaar4778b4d2020-11-04 11:03:12 +01002203 // Also bail out when parsing messages was explicitly disabled.
2204 if (updating_screen || dont_parse_messages)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002205 return;
2206
Bram Moolenaar1cac7092019-10-22 21:54:31 +02002207 // If memory allocation fails during startup we'll exit but curbuf or
2208 // curwin could be NULL.
2209 if (curbuf == NULL || curwin == NULL)
2210 return;
2211
2212 old_curbuf_fnum = curbuf->b_fnum;
2213 old_curwin_id = curwin->w_id;
2214
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002215 ++entered;
2216
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002217 // may_garbage_collect is set in main_loop() to do garbage collection when
2218 // blocking to wait on a character. We don't want that while parsing
2219 // messages, a callback may invoke vgetc() while lists and dicts are in use
2220 // in the call stack.
2221 may_garbage_collect = FALSE;
2222
2223 // Loop when a job ended, but don't keep looping forever.
2224 for (i = 0; i < MAX_REPEAT_PARSE; ++i)
2225 {
2226 // For Win32 mch_breakcheck() does not check for input, do it here.
Bram Moolenaar80a8d382020-05-03 22:57:32 +02002227# if (defined(MSWIN) || defined(__HAIKU__)) && defined(FEAT_JOB_CHANNEL)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002228 channel_handle_events(FALSE);
2229# endif
2230
2231# ifdef FEAT_NETBEANS_INTG
2232 // Process the queued netbeans messages.
2233 netbeans_parse_messages();
2234# endif
2235# ifdef FEAT_JOB_CHANNEL
2236 // Write any buffer lines still to be written.
2237 channel_write_any_lines();
2238
2239 // Process the messages queued on channels.
2240 channel_parse_messages();
2241# endif
2242# if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
2243 // Process the queued clientserver messages.
2244 server_parse_messages();
2245# endif
2246# ifdef FEAT_JOB_CHANNEL
2247 // Check if any jobs have ended. If so, repeat the above to handle
2248 // changes, e.g. stdin may have been closed.
2249 if (job_check_ended())
2250 continue;
2251# endif
2252# ifdef FEAT_TERMINAL
2253 free_unused_terminals();
2254# endif
2255# ifdef FEAT_SOUND_CANBERRA
2256 if (has_sound_callback_in_queue())
2257 invoke_sound_callback();
2258# endif
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002259#ifdef SIGUSR1
2260 if (got_sigusr1)
2261 {
2262 apply_autocmds(EVENT_SIGUSR1, NULL, NULL, FALSE, curbuf);
2263 got_sigusr1 = FALSE;
2264 }
2265#endif
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002266 break;
2267 }
2268
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002269 // When not nested we'll go back to waiting for a typed character. If it
2270 // was safe before then this triggers a SafeStateAgain autocommand event.
Bram Moolenaard103ee72019-09-18 21:15:31 +02002271 if (entered == 1 && was_safe)
Bram Moolenaar37d18072019-09-17 20:28:38 +02002272 may_trigger_safestateagain();
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002273
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002274 may_garbage_collect = save_may_garbage_collect;
2275
2276 // If the current window or buffer changed we need to bail out of the
2277 // waiting loop. E.g. when a job exit callback closes the terminal window.
2278 if (curwin->w_id != old_curwin_id || curbuf->b_fnum != old_curbuf_fnum)
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02002279 ins_char_typebuf(K_IGNORE, 0);
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002280
2281 --entered;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002282}
2283#endif
2284
2285
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002286typedef enum {
2287 map_result_fail, // failed, break loop
2288 map_result_get, // get a character from typeahead
2289 map_result_retry, // try to map again
2290 map_result_nomatch // no matching mapping, get char
2291} map_result_T;
2292
2293/*
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002294 * Check if the bytes at the start of the typeahead buffer are a character used
2295 * in CTRL-X mode. This includes the form with a CTRL modifier.
2296 */
2297 static int
2298at_ctrl_x_key(void)
2299{
2300 char_u *p = typebuf.tb_buf + typebuf.tb_off;
2301 int c = *p;
2302
2303 if (typebuf.tb_len > 3
2304 && c == K_SPECIAL
2305 && p[1] == KS_MODIFIER
2306 && (p[2] & MOD_MASK_CTRL))
2307 c = p[3] & 0x1f;
2308 return vim_is_ctrl_x_key(c);
2309}
2310
2311/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002312 * Check if typebuf.tb_buf[] contains a modifier plus key that can be changed
Bram Moolenaar975a8802020-06-06 22:36:24 +02002313 * into just a key, apply that.
2314 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
2315 * + "max_offset"].
2316 * Return the length of the replaced bytes, zero if nothing changed.
2317 */
2318 static int
2319check_simplify_modifier(int max_offset)
2320{
2321 int offset;
2322 char_u *tp;
2323
2324 for (offset = 0; offset < max_offset; ++offset)
2325 {
2326 if (offset + 3 >= typebuf.tb_len)
2327 break;
2328 tp = typebuf.tb_buf + typebuf.tb_off + offset;
2329 if (tp[0] == K_SPECIAL && tp[1] == KS_MODIFIER)
2330 {
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002331 // A modifier was not used for a mapping, apply it to ASCII keys.
2332 // Shift would already have been applied.
Bram Moolenaar975a8802020-06-06 22:36:24 +02002333 int modifier = tp[2];
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002334 int c = tp[3];
2335 int new_c = merge_modifyOtherKeys(c, &modifier);
Bram Moolenaar975a8802020-06-06 22:36:24 +02002336
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002337 if (new_c != c)
Bram Moolenaar975a8802020-06-06 22:36:24 +02002338 {
2339 char_u new_string[MB_MAXBYTES];
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002340 int len;
Bram Moolenaar975a8802020-06-06 22:36:24 +02002341
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002342 if (offset == 0)
2343 {
2344 // At the start: remember the character and mod_mask before
2345 // merging, in some cases, e.g. at the hit-return prompt,
2346 // they are put back in the typeahead buffer.
2347 vgetc_char = c;
2348 vgetc_mod_mask = tp[2];
2349 }
2350 len = mb_char2bytes(new_c, new_string);
2351 if (modifier == 0)
2352 {
2353 if (put_string_in_typebuf(offset, 4, new_string, len,
Bram Moolenaar975a8802020-06-06 22:36:24 +02002354 NULL, 0, 0) == FAIL)
2355 return -1;
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002356 }
2357 else
2358 {
2359 tp[2] = modifier;
2360 if (put_string_in_typebuf(offset + 3, 1, new_string, len,
2361 NULL, 0, 0) == FAIL)
2362 return -1;
2363 }
Bram Moolenaar975a8802020-06-06 22:36:24 +02002364 return len;
2365 }
2366 }
2367 }
2368 return 0;
2369}
2370
2371/*
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002372 * Handle mappings in the typeahead buffer.
2373 * - When something was mapped, return map_result_retry for recursive mappings.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002374 * - When nothing mapped and typeahead has a character: return map_result_get.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002375 * - When there is no match yet, return map_result_nomatch, need to get more
2376 * typeahead.
2377 */
2378 static int
2379handle_mapping(
2380 int *keylenp,
2381 int *timedout,
2382 int *mapdepth)
2383{
2384 mapblock_T *mp = NULL;
2385 mapblock_T *mp2;
2386 mapblock_T *mp_match;
2387 int mp_match_len = 0;
2388 int max_mlen = 0;
2389 int tb_c1;
2390 int mlen;
2391#ifdef FEAT_LANGMAP
2392 int nolmaplen;
2393#endif
2394 int keylen = *keylenp;
2395 int i;
2396 int local_State = get_real_state();
2397
2398 /*
2399 * Check for a mappable key sequence.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002400 * Walk through one maphash[] list until we find an entry that matches.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002401 *
2402 * Don't look for mappings if:
2403 * - no_mapping set: mapping disabled (e.g. for CTRL-V)
2404 * - maphash_valid not set: no mappings present.
2405 * - typebuf.tb_buf[typebuf.tb_off] should not be remapped
2406 * - in insert or cmdline mode and 'paste' option set
Bram Moolenaareda35f72019-08-03 14:59:44 +02002407 * - waiting for "hit return to continue" and CR or SPACE typed
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002408 * - waiting for a char with --more--
2409 * - in Ctrl-X mode, and we get a valid char for that mode
2410 */
2411 tb_c1 = typebuf.tb_buf[typebuf.tb_off];
2412 if (no_mapping == 0 && is_maphash_valid()
2413 && (no_zero_mapping == 0 || tb_c1 != '0')
2414 && (typebuf.tb_maplen == 0
2415 || (p_remap
2416 && (typebuf.tb_noremap[typebuf.tb_off]
2417 & (RM_NONE|RM_ABBR)) == 0))
2418 && !(p_paste && (State & (INSERT + CMDLINE)))
2419 && !(State == HITRETURN && (tb_c1 == CAR || tb_c1 == ' '))
2420 && State != ASKMORE
2421 && State != CONFIRM
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002422 && !((ctrl_x_mode_not_default() && at_ctrl_x_key())
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002423 || ((compl_cont_status & CONT_LOCAL)
Bram Moolenaare2c453d2019-08-21 14:37:09 +02002424 && (tb_c1 == Ctrl_N || tb_c1 == Ctrl_P))))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002425 {
Bram Moolenaarc9983702020-05-28 21:03:53 +02002426#ifdef FEAT_GUI
2427 if (gui.in_use && tb_c1 == CSI && typebuf.tb_len >= 2
2428 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_MODIFIER)
2429 {
2430 // The GUI code sends CSI KS_MODIFIER {flags}, but mappings expect
2431 // K_SPECIAL KS_MODIFIER {flags}.
2432 tb_c1 = K_SPECIAL;
2433 }
2434#endif
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002435#ifdef FEAT_LANGMAP
2436 if (tb_c1 == K_SPECIAL)
2437 nolmaplen = 2;
2438 else
2439 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002440 LANGMAP_ADJUST(tb_c1, (State & (CMDLINE | INSERT)) == 0
2441 && get_real_state() != SELECTMODE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002442 nolmaplen = 0;
2443 }
2444#endif
2445 // First try buffer-local mappings.
2446 mp = get_buf_maphash_list(local_State, tb_c1);
2447 mp2 = get_maphash_list(local_State, tb_c1);
2448 if (mp == NULL)
2449 {
2450 // There are no buffer-local mappings.
2451 mp = mp2;
2452 mp2 = NULL;
2453 }
Bram Moolenaareda35f72019-08-03 14:59:44 +02002454
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002455 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002456 * Loop until a partly matching mapping is found or all (local)
2457 * mappings have been checked.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002458 * The longest full match is remembered in "mp_match".
Bram Moolenaareda35f72019-08-03 14:59:44 +02002459 * A full match is only accepted if there is no partly match, so "aa"
2460 * and "aaa" can both be mapped.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002461 */
2462 mp_match = NULL;
2463 mp_match_len = 0;
2464 for ( ; mp != NULL;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002465 mp->m_next == NULL ? (mp = mp2, mp2 = NULL) : (mp = mp->m_next))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002466 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002467 // Only consider an entry if the first character matches and it is
2468 // for the current state.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002469 // Skip ":lmap" mappings if keys were mapped.
2470 if (mp->m_keys[0] == tb_c1
2471 && (mp->m_mode & local_State)
Bram Moolenaar46cd43b2020-06-04 22:22:11 +02002472 && !(mp->m_simplified && seenModifyOtherKeys
2473 && typebuf.tb_maplen == 0)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002474 && ((mp->m_mode & LANGMAP) == 0 || typebuf.tb_maplen == 0))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002475 {
2476#ifdef FEAT_LANGMAP
2477 int nomap = nolmaplen;
2478 int c2;
2479#endif
2480 // find the match length of this mapping
2481 for (mlen = 1; mlen < typebuf.tb_len; ++mlen)
2482 {
2483#ifdef FEAT_LANGMAP
2484 c2 = typebuf.tb_buf[typebuf.tb_off + mlen];
2485 if (nomap > 0)
2486 --nomap;
2487 else if (c2 == K_SPECIAL)
2488 nomap = 2;
2489 else
2490 LANGMAP_ADJUST(c2, TRUE);
2491 if (mp->m_keys[mlen] != c2)
2492#else
2493 if (mp->m_keys[mlen] !=
Bram Moolenaarc9983702020-05-28 21:03:53 +02002494 typebuf.tb_buf[typebuf.tb_off + mlen])
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002495#endif
2496 break;
2497 }
2498
Bram Moolenaareda35f72019-08-03 14:59:44 +02002499 // Don't allow mapping the first byte(s) of a multi-byte char.
2500 // Happens when mapping <M-a> and then changing 'encoding'.
2501 // Beware that 0x80 is escaped.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002502 {
2503 char_u *p1 = mp->m_keys;
2504 char_u *p2 = mb_unescape(&p1);
2505
2506 if (has_mbyte && p2 != NULL
Bram Moolenaar1614a142019-10-06 22:00:13 +02002507 && MB_BYTE2LEN(tb_c1) > mb_ptr2len(p2))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002508 mlen = 0;
2509 }
2510
2511 // Check an entry whether it matches.
2512 // - Full match: mlen == keylen
2513 // - Partly match: mlen == typebuf.tb_len
2514 keylen = mp->m_keylen;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002515 if (mlen == keylen || (mlen == typebuf.tb_len
2516 && typebuf.tb_len < keylen))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002517 {
2518 char_u *s;
2519 int n;
2520
Bram Moolenaareda35f72019-08-03 14:59:44 +02002521 // If only script-local mappings are allowed, check if the
2522 // mapping starts with K_SNR.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002523 s = typebuf.tb_noremap + typebuf.tb_off;
2524 if (*s == RM_SCRIPT
2525 && (mp->m_keys[0] != K_SPECIAL
2526 || mp->m_keys[1] != KS_EXTRA
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01002527 || mp->m_keys[2] != KE_SNR))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002528 continue;
2529
Bram Moolenaareda35f72019-08-03 14:59:44 +02002530 // If one of the typed keys cannot be remapped, skip the
2531 // entry.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002532 for (n = mlen; --n >= 0; )
2533 if (*s++ & (RM_NONE|RM_ABBR))
2534 break;
2535 if (n >= 0)
2536 continue;
2537
2538 if (keylen > typebuf.tb_len)
2539 {
2540 if (!*timedout && !(mp_match != NULL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002541 && mp_match->m_nowait))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002542 {
2543 // break at a partly match
2544 keylen = KEYLEN_PART_MAP;
2545 break;
2546 }
2547 }
2548 else if (keylen > mp_match_len)
2549 {
2550 // found a longer match
2551 mp_match = mp;
2552 mp_match_len = keylen;
2553 }
2554 }
2555 else
Bram Moolenaareda35f72019-08-03 14:59:44 +02002556 // No match; may have to check for termcode at next
2557 // character.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002558 if (max_mlen < mlen)
2559 max_mlen = mlen;
2560 }
2561 }
2562
Bram Moolenaareda35f72019-08-03 14:59:44 +02002563 // If no partly match found, use the longest full match.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002564 if (keylen != KEYLEN_PART_MAP)
2565 {
2566 mp = mp_match;
2567 keylen = mp_match_len;
2568 }
2569 }
2570
2571 /*
2572 * Check for match with 'pastetoggle'
2573 */
2574 if (*p_pt != NUL && mp == NULL && (State & (INSERT|NORMAL)))
2575 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002576 for (mlen = 0; mlen < typebuf.tb_len && p_pt[mlen]; ++mlen)
2577 if (p_pt[mlen] != typebuf.tb_buf[typebuf.tb_off + mlen])
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002578 break;
2579 if (p_pt[mlen] == NUL) // match
2580 {
2581 // write chars to script file(s)
2582 if (mlen > typebuf.tb_maplen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002583 gotchars(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_maplen,
2584 mlen - typebuf.tb_maplen);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002585
2586 del_typebuf(mlen, 0); // remove the chars
Bram Moolenaareda35f72019-08-03 14:59:44 +02002587 set_option_value((char_u *)"paste", (long)!p_paste, NULL, 0);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002588 if (!(State & INSERT))
2589 {
2590 msg_col = 0;
2591 msg_row = Rows - 1;
2592 msg_clr_eos(); // clear ruler
2593 }
2594 status_redraw_all();
2595 redraw_statuslines();
2596 showmode();
2597 setcursor();
2598 *keylenp = keylen;
2599 return map_result_retry;
2600 }
2601 // Need more chars for partly match.
2602 if (mlen == typebuf.tb_len)
2603 keylen = KEYLEN_PART_KEY;
2604 else if (max_mlen < mlen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002605 // no match, may have to check for termcode at next character
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002606 max_mlen = mlen + 1;
2607 }
2608
Bram Moolenaareda35f72019-08-03 14:59:44 +02002609 if ((mp == NULL || max_mlen >= mp_match_len) && keylen != KEYLEN_PART_MAP)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002610 {
2611 int save_keylen = keylen;
2612
2613 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002614 * When no matching mapping found or found a non-matching mapping that
2615 * matches at least what the matching mapping matched:
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002616 * Check if we have a terminal code, when:
Bram Moolenaareda35f72019-08-03 14:59:44 +02002617 * - mapping is allowed,
2618 * - keys have not been mapped,
2619 * - and not an ESC sequence, not in insert mode or p_ek is on,
2620 * - and when not timed out,
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002621 */
2622 if ((no_mapping == 0 || allow_keys != 0)
2623 && (typebuf.tb_maplen == 0
2624 || (p_remap && typebuf.tb_noremap[
Bram Moolenaareda35f72019-08-03 14:59:44 +02002625 typebuf.tb_off] == RM_YES))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002626 && !*timedout)
2627 {
Bram Moolenaar0684e362020-12-03 19:54:42 +01002628 keylen = check_termcode(max_mlen + 1, NULL, 0, NULL);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002629
Bram Moolenaar0684e362020-12-03 19:54:42 +01002630 // If no termcode matched but 'pastetoggle' matched partially
2631 // it's like an incomplete key sequence.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002632 if (keylen == 0 && save_keylen == KEYLEN_PART_KEY)
2633 keylen = KEYLEN_PART_KEY;
2634
Bram Moolenaar975a8802020-06-06 22:36:24 +02002635 // If no termcode matched, try to include the modifier into the
2636 // key. This for when modifyOtherKeys is working.
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002637 if (keylen == 0 && !no_reduce_keys)
Bram Moolenaar975a8802020-06-06 22:36:24 +02002638 keylen = check_simplify_modifier(max_mlen + 1);
2639
Bram Moolenaareda35f72019-08-03 14:59:44 +02002640 // When getting a partial match, but the last characters were not
2641 // typed, don't wait for a typed character to complete the
2642 // termcode. This helps a lot when a ":normal" command ends in an
2643 // ESC.
2644 if (keylen < 0 && typebuf.tb_len == typebuf.tb_maplen)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002645 keylen = 0;
2646 }
2647 else
2648 keylen = 0;
2649 if (keylen == 0) // no matching terminal code
2650 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002651#ifdef AMIGA
2652 // check for window bounds report
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002653 if (typebuf.tb_maplen == 0 && (typebuf.tb_buf[
Bram Moolenaareda35f72019-08-03 14:59:44 +02002654 typebuf.tb_off] & 0xff) == CSI)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002655 {
2656 char_u *s;
2657
2658 for (s = typebuf.tb_buf + typebuf.tb_off + 1;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002659 s < typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len
2660 && (VIM_ISDIGIT(*s) || *s == ';' || *s == ' ');
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002661 ++s)
2662 ;
2663 if (*s == 'r' || *s == '|') // found one
2664 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002665 del_typebuf(
2666 (int)(s + 1 - (typebuf.tb_buf + typebuf.tb_off)), 0);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002667 // get size and redraw screen
2668 shell_resized();
2669 *keylenp = keylen;
2670 return map_result_retry;
2671 }
2672 if (*s == NUL) // need more characters
2673 keylen = KEYLEN_PART_KEY;
2674 }
2675 if (keylen >= 0)
2676#endif
Bram Moolenaareda35f72019-08-03 14:59:44 +02002677 // When there was a matching mapping and no termcode could be
2678 // replaced after another one, use that mapping (loop around).
2679 // If there was no mapping at all use the character from the
2680 // typeahead buffer right here.
2681 if (mp == NULL)
2682 {
2683 *keylenp = keylen;
2684 return map_result_get; // got character, break for loop
2685 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002686 }
2687
2688 if (keylen > 0) // full matching terminal code
2689 {
2690#if defined(FEAT_GUI) && defined(FEAT_MENU)
2691 if (typebuf.tb_len >= 2
Bram Moolenaareda35f72019-08-03 14:59:44 +02002692 && typebuf.tb_buf[typebuf.tb_off] == K_SPECIAL
2693 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_MENU)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002694 {
2695 int idx;
2696
Bram Moolenaareda35f72019-08-03 14:59:44 +02002697 // Using a menu may cause a break in undo! It's like using
2698 // gotchars(), but without recording or writing to a script
2699 // file.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002700 may_sync_undo();
2701 del_typebuf(3, 0);
2702 idx = get_menu_index(current_menu, local_State);
2703 if (idx != MENU_INDEX_INVALID)
2704 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002705 // In Select mode and a Visual mode menu is used: Switch
2706 // to Visual mode temporarily. Append K_SELECT to switch
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002707 // back to Select mode.
2708 if (VIsual_active && VIsual_select
Bram Moolenaareda35f72019-08-03 14:59:44 +02002709 && (current_menu->modes & VISUAL))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002710 {
2711 VIsual_select = FALSE;
2712 (void)ins_typebuf(K_SELECT_STRING,
Bram Moolenaareda35f72019-08-03 14:59:44 +02002713 REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002714 }
2715 ins_typebuf(current_menu->strings[idx],
2716 current_menu->noremap[idx],
Bram Moolenaareda35f72019-08-03 14:59:44 +02002717 0, TRUE, current_menu->silent[idx]);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002718 }
2719 }
2720#endif // FEAT_GUI && FEAT_MENU
2721 *keylenp = keylen;
2722 return map_result_retry; // try mapping again
2723 }
2724
Bram Moolenaareda35f72019-08-03 14:59:44 +02002725 // Partial match: get some more characters. When a matching mapping
2726 // was found use that one.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002727 if (mp == NULL || keylen < 0)
2728 keylen = KEYLEN_PART_KEY;
2729 else
2730 keylen = mp_match_len;
2731 }
2732
2733 /*
2734 * complete match
2735 */
2736 if (keylen >= 0 && keylen <= typebuf.tb_len)
2737 {
2738 char_u *map_str;
2739
2740#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002741 int save_m_expr;
2742 int save_m_noremap;
2743 int save_m_silent;
2744 char_u *save_m_keys;
2745 char_u *save_m_str;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002746#else
2747# define save_m_noremap mp->m_noremap
2748# define save_m_silent mp->m_silent
2749#endif
2750
2751 // write chars to script file(s)
2752 if (keylen > typebuf.tb_maplen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002753 gotchars(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_maplen,
2754 keylen - typebuf.tb_maplen);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002755
2756 cmd_silent = (typebuf.tb_silent > 0);
2757 del_typebuf(keylen, 0); // remove the mapped keys
2758
2759 /*
2760 * Put the replacement string in front of mapstr.
2761 * The depth check catches ":map x y" and ":map y x".
2762 */
2763 if (++*mapdepth >= p_mmd)
2764 {
2765 emsg(_("E223: recursive mapping"));
2766 if (State & CMDLINE)
2767 redrawcmdline();
2768 else
2769 setcursor();
2770 flush_buffers(FLUSH_MINIMAL);
Bram Moolenaar30613902019-12-01 22:11:18 +01002771 *mapdepth = 0; // for next one
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002772 *keylenp = keylen;
2773 return map_result_fail;
2774 }
2775
2776 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002777 * In Select mode and a Visual mode mapping is used: Switch to Visual
2778 * mode temporarily. Append K_SELECT to switch back to Select mode.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002779 */
Bram Moolenaareda35f72019-08-03 14:59:44 +02002780 if (VIsual_active && VIsual_select && (mp->m_mode & VISUAL))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002781 {
2782 VIsual_select = FALSE;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002783 (void)ins_typebuf(K_SELECT_STRING, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002784 }
2785
2786#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002787 // Copy the values from *mp that are used, because evaluating the
2788 // expression may invoke a function that redefines the mapping, thereby
2789 // making *mp invalid.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002790 save_m_expr = mp->m_expr;
2791 save_m_noremap = mp->m_noremap;
2792 save_m_silent = mp->m_silent;
2793 save_m_keys = NULL; // only saved when needed
2794 save_m_str = NULL; // only saved when needed
2795
2796 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002797 * Handle ":map <expr>": evaluate the {rhs} as an expression. Also
2798 * save and restore the command line for "normal :".
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002799 */
2800 if (mp->m_expr)
2801 {
2802 int save_vgetc_busy = vgetc_busy;
2803 int save_may_garbage_collect = may_garbage_collect;
Bram Moolenaar4ebe0e62019-11-22 20:55:40 +01002804 int was_screen_col = screen_cur_col;
2805 int was_screen_row = screen_cur_row;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002806
2807 vgetc_busy = 0;
2808 may_garbage_collect = FALSE;
2809
2810 save_m_keys = vim_strsave(mp->m_keys);
2811 save_m_str = vim_strsave(mp->m_str);
2812 map_str = eval_map_expr(save_m_str, NUL);
2813
Bram Moolenaar4ebe0e62019-11-22 20:55:40 +01002814 // The mapping may do anything, but we expect it to take care of
2815 // redrawing. Do put the cursor back where it was.
2816 windgoto(was_screen_row, was_screen_col);
2817 out_flush();
2818
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002819 vgetc_busy = save_vgetc_busy;
2820 may_garbage_collect = save_may_garbage_collect;
2821 }
2822 else
2823#endif
2824 map_str = mp->m_str;
2825
2826 /*
2827 * Insert the 'to' part in the typebuf.tb_buf.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002828 * If 'from' field is the same as the start of the 'to' field, don't
2829 * remap the first character (but do allow abbreviations).
2830 * If m_noremap is set, don't remap the whole 'to' part.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002831 */
2832 if (map_str == NULL)
2833 i = FAIL;
2834 else
2835 {
2836 int noremap;
2837
2838 if (save_m_noremap != REMAP_YES)
2839 noremap = save_m_noremap;
2840 else if (
2841#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002842 STRNCMP(map_str, save_m_keys != NULL ? save_m_keys : mp->m_keys,
2843 (size_t)keylen)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002844#else
2845 STRNCMP(map_str, mp->m_keys, (size_t)keylen)
2846#endif
2847 != 0)
2848 noremap = REMAP_YES;
2849 else
2850 noremap = REMAP_SKIP;
2851 i = ins_typebuf(map_str, noremap,
2852 0, TRUE, cmd_silent || save_m_silent);
2853#ifdef FEAT_EVAL
2854 if (save_m_expr)
2855 vim_free(map_str);
2856#endif
2857 }
2858#ifdef FEAT_EVAL
2859 vim_free(save_m_keys);
2860 vim_free(save_m_str);
2861#endif
2862 *keylenp = keylen;
2863 if (i == FAIL)
2864 return map_result_fail;
2865 return map_result_retry;
2866 }
2867
2868 *keylenp = keylen;
2869 return map_result_nomatch;
2870}
2871
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002872/*
2873 * unget one character (can only be done once!)
2874 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002876vungetc(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877{
2878 old_char = c;
2879 old_mod_mask = mod_mask;
Bram Moolenaarb8978712013-03-16 21:42:16 +01002880 old_mouse_row = mouse_row;
2881 old_mouse_col = mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882}
2883
2884/*
Bram Moolenaar32526b32019-01-19 17:43:09 +01002885 * Get a byte:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 * 1. from the stuffbuffer
2887 * This is used for abbreviated commands like "D" -> "d$".
2888 * Also used to redo a command for ".".
2889 * 2. from the typeahead buffer
2890 * Stores text obtained previously but not used yet.
2891 * Also stores the result of mappings.
2892 * Also used for the ":normal" command.
2893 * 3. from the user
2894 * This may do a blocking wait if "advance" is TRUE.
2895 *
2896 * if "advance" is TRUE (vgetc()):
Bram Moolenaard29459b2016-08-26 22:29:11 +02002897 * Really get the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 * KeyTyped is set to TRUE in the case the user typed the key.
2899 * KeyStuffed is TRUE if the character comes from the stuff buffer.
2900 * if "advance" is FALSE (vpeekc()):
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02002901 * Just look whether there is a character available.
2902 * Return NUL if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 *
2904 * When "no_mapping" is zero, checks for mappings in the current mode.
2905 * Only returns one byte (of a multi-byte character).
2906 * K_SPECIAL and CSI may be escaped, need to get two more bytes then.
2907 */
2908 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002909vgetorpeek(int advance)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910{
2911 int c, c1;
Bram Moolenaar30613902019-12-01 22:11:18 +01002912 int timedout = FALSE; // waited for more than 1 second
2913 // for mapping to complete
2914 int mapdepth = 0; // check for recursive mapping
2915 int mode_deleted = FALSE; // set when mode has been deleted
Bram Moolenaar4e427192006-03-10 21:34:27 +00002916#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 int new_wcol, new_wrow;
2918#endif
2919#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01002920 int shape_changed = FALSE; // adjusted cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921#endif
2922 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 int old_wcol, old_wrow;
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00002924 int wait_tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925
2926 /*
2927 * This function doesn't work very well when called recursively. This may
2928 * happen though, because of:
2929 * 1. The call to add_to_showcmd(). char_avail() is then used to check if
2930 * there is a character available, which calls this function. In that
2931 * case we must return NUL, to indicate no character is available.
2932 * 2. A GUI callback function writes to the screen, causing a
2933 * wait_return().
2934 * Using ":normal" can also do this, but it saves the typeahead buffer,
2935 * thus it should be OK. But don't get a key from the user then.
2936 */
Bram Moolenaare2c38102016-01-31 14:55:40 +01002937 if (vgetc_busy > 0 && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 return NUL;
2939
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002940 ++vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941
2942 if (advance)
2943 KeyStuffed = FALSE;
2944
2945 init_typebuf();
2946 start_stuff();
2947 if (advance && typebuf.tb_maplen == 0)
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02002948 reg_executing = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 do
2950 {
2951/*
2952 * get a character: 1. from the stuffbuffer
2953 */
2954 if (typeahead_char != 0)
2955 {
2956 c = typeahead_char;
2957 if (advance)
2958 typeahead_char = 0;
2959 }
2960 else
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01002961 c = read_readbuffers(advance);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 if (c != NUL && !got_int)
2963 {
2964 if (advance)
2965 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002966 // KeyTyped = FALSE; When the command that stuffed something
2967 // was typed, behave like the stuffed command was typed.
2968 // needed for CTRL-W CTRL-] to open a fold, for example.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 KeyStuffed = TRUE;
2970 }
2971 if (typebuf.tb_no_abbr_cnt == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01002972 typebuf.tb_no_abbr_cnt = 1; // no abbreviations now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 }
2974 else
2975 {
2976 /*
2977 * Loop until we either find a matching mapped key, or we
2978 * are sure that it is not a mapped key.
2979 * If a mapped key sequence is found we go back to the start to
2980 * try re-mapping.
2981 */
2982 for (;;)
2983 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002984 long wait_time;
2985 int keylen = 0;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002986#ifdef FEAT_CMDL_INFO
2987 int showcmd_idx;
2988#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 /*
2990 * ui_breakcheck() is slow, don't use it too often when
2991 * inside a mapping. But call it each time for typed
2992 * characters.
2993 */
2994 if (typebuf.tb_maplen)
2995 line_breakcheck();
2996 else
Bram Moolenaar30613902019-12-01 22:11:18 +01002997 ui_breakcheck(); // check for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 if (got_int)
2999 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003000 // flush all input
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003001 c = inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 0L);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003002
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 /*
3004 * If inchar() returns TRUE (script file was active) or we
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003005 * are inside a mapping, get out of Insert mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 * Otherwise we behave like having gotten a CTRL-C.
3007 * As a result typing CTRL-C in insert mode will
3008 * really insert a CTRL-C.
3009 */
3010 if ((c || typebuf.tb_maplen)
3011 && (State & (INSERT + CMDLINE)))
3012 c = ESC;
3013 else
3014 c = Ctrl_C;
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02003015 flush_buffers(FLUSH_INPUT); // flush all typeahead
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016
Bram Moolenaard9dfd572006-10-03 13:36:13 +00003017 if (advance)
3018 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003019 // Also record this character, it might be needed to
3020 // get out of Insert mode.
Bram Moolenaard9dfd572006-10-03 13:36:13 +00003021 *typebuf.tb_buf = c;
3022 gotchars(typebuf.tb_buf, 1);
3023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 cmd_silent = FALSE;
3025
3026 break;
3027 }
3028 else if (typebuf.tb_len > 0)
3029 {
3030 /*
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003031 * Check for a mapping in "typebuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 */
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003033 map_result_T result = handle_mapping(
3034 &keylen, &timedout, &mapdepth);
3035
3036 if (result == map_result_retry)
3037 // try mapping again
3038 continue;
3039 if (result == map_result_fail)
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003040 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003041 // failed, use the outer loop
3042 c = -1;
3043 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003045 if (result == map_result_get)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047/*
3048 * get a character: 2. from the typeahead buffer
3049 */
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003050 c = typebuf.tb_buf[typebuf.tb_off];
Bram Moolenaar30613902019-12-01 22:11:18 +01003051 if (advance) // remove chars from tb_buf
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003052 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003053 cmd_silent = (typebuf.tb_silent > 0);
3054 if (typebuf.tb_maplen > 0)
3055 KeyTyped = FALSE;
3056 else
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003057 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003058 KeyTyped = TRUE;
Bram Moolenaar30613902019-12-01 22:11:18 +01003059 // write char to script file(s)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003060 gotchars(typebuf.tb_buf
3061 + typebuf.tb_off, 1);
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003062 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003063 KeyNoremap = typebuf.tb_noremap[
3064 typebuf.tb_off];
3065 del_typebuf(1, 0);
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003066 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003067 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 }
3069
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003070 // not enough characters, get more
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 }
3072
3073/*
3074 * get a character: 3. from the user - handle <Esc> in Insert mode
3075 */
3076 /*
Bram Moolenaarf085f422017-06-07 20:39:47 +02003077 * Special case: if we get an <ESC> in insert mode and there
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 * are no more characters at once, we pretend to go out of
3079 * insert mode. This prevents the one second delay after
3080 * typing an <ESC>. If we get something after all, we may
3081 * have to redisplay the mode. That the cursor is in the wrong
3082 * place does not matter.
3083 */
3084 c = 0;
3085#ifdef FEAT_CMDL_INFO
3086 new_wcol = curwin->w_wcol;
3087 new_wrow = curwin->w_wrow;
3088#endif
3089 if ( advance
3090 && typebuf.tb_len == 1
3091 && typebuf.tb_buf[typebuf.tb_off] == ESC
3092 && !no_mapping
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 && ex_normal_busy == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 && typebuf.tb_maplen == 0
3095 && (State & INSERT)
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003096 && (p_timeout
3097 || (keylen == KEYLEN_PART_KEY && p_ttimeout))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 && (c = inchar(typebuf.tb_buf + typebuf.tb_off
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003099 + typebuf.tb_len, 3, 25L)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 {
3101 colnr_T col = 0, vcol;
3102 char_u *ptr;
3103
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003104 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 {
3106 unshowmode(TRUE);
3107 mode_deleted = TRUE;
3108 }
3109#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003110 // may show a different cursor shape
Bram Moolenaarf085f422017-06-07 20:39:47 +02003111 if (gui.in_use && State != NORMAL && !cmd_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 {
3113 int save_State;
3114
3115 save_State = State;
3116 State = NORMAL;
3117 gui_update_cursor(TRUE, FALSE);
3118 State = save_State;
3119 shape_changed = TRUE;
3120 }
3121#endif
3122 validate_cursor();
3123 old_wcol = curwin->w_wcol;
3124 old_wrow = curwin->w_wrow;
3125
Bram Moolenaar30613902019-12-01 22:11:18 +01003126 // move cursor left, if possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 if (curwin->w_cursor.col != 0)
3128 {
3129 if (curwin->w_wcol > 0)
3130 {
3131 if (did_ai)
3132 {
3133 /*
3134 * We are expecting to truncate the trailing
3135 * white-space, so find the last non-white
3136 * character -- webb
3137 */
3138 col = vcol = curwin->w_wcol = 0;
3139 ptr = ml_get_curline();
3140 while (col < curwin->w_cursor.col)
3141 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01003142 if (!VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 curwin->w_wcol = vcol;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003144 vcol += lbr_chartabsize(ptr, ptr + col,
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01003145 vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003147 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 ++col;
3150 }
3151 curwin->w_wrow = curwin->w_cline_row
Bram Moolenaar02631462017-09-22 15:20:32 +02003152 + curwin->w_wcol / curwin->w_width;
3153 curwin->w_wcol %= curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 curwin->w_wcol += curwin_col_off();
Bram Moolenaar30613902019-12-01 22:11:18 +01003155 col = 0; // no correction needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 }
3157 else
3158 {
3159 --curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 col = curwin->w_cursor.col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 }
3162 }
3163 else if (curwin->w_p_wrap && curwin->w_wrow)
3164 {
3165 --curwin->w_wrow;
Bram Moolenaar02631462017-09-22 15:20:32 +02003166 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 col = curwin->w_cursor.col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 if (has_mbyte && col > 0 && curwin->w_wcol > 0)
3170 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003171 // Correct when the cursor is on the right halve
3172 // of a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 ptr = ml_get_curline();
3174 col -= (*mb_head_off)(ptr, ptr + col);
3175 if ((*mb_ptr2cells)(ptr + col) > 1)
3176 --curwin->w_wcol;
3177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 }
3179 setcursor();
3180 out_flush();
3181#ifdef FEAT_CMDL_INFO
3182 new_wcol = curwin->w_wcol;
3183 new_wrow = curwin->w_wrow;
3184#endif
3185 curwin->w_wcol = old_wcol;
3186 curwin->w_wrow = old_wrow;
3187 }
3188 if (c < 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003189 continue; // end of input script reached
Bram Moolenaar20c38922014-07-23 20:41:14 +02003190
Bram Moolenaar30613902019-12-01 22:11:18 +01003191 // Allow mapping for just typed characters. When we get here c
3192 // is the number of extra bytes and typebuf.tb_len is 1.
Bram Moolenaar20c38922014-07-23 20:41:14 +02003193 for (n = 1; n <= c; ++n)
3194 typebuf.tb_noremap[typebuf.tb_off + n] = RM_YES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 typebuf.tb_len += c;
3196
Bram Moolenaar30613902019-12-01 22:11:18 +01003197 // buffer full, don't map
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 if (typebuf.tb_len >= typebuf.tb_maplen + MAXMAPLEN)
3199 {
3200 timedout = TRUE;
3201 continue;
3202 }
3203
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 if (ex_normal_busy > 0)
3205 {
Bram Moolenaare2c38102016-01-31 14:55:40 +01003206#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 static int tc = 0;
Bram Moolenaare2c38102016-01-31 14:55:40 +01003208#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209
Bram Moolenaar30613902019-12-01 22:11:18 +01003210 // No typeahead left and inside ":normal". Must return
3211 // something to avoid getting stuck. When an incomplete
3212 // mapping is present, behave like it timed out.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 if (typebuf.tb_len > 0)
3214 {
3215 timedout = TRUE;
3216 continue;
3217 }
Bram Moolenaar189832b2020-09-23 12:29:11 +02003218
Bram Moolenaar30613902019-12-01 22:11:18 +01003219 // When 'insertmode' is set, ESC just beeps in Insert
3220 // mode. Use CTRL-L to make edit() return.
3221 // For the command line only CTRL-C always breaks it.
3222 // For the cmdline window: Alternate between ESC and
3223 // CTRL-C: ESC for most situations and CTRL-C to close the
3224 // cmdline window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 if (p_im && (State & INSERT))
3226 c = Ctrl_L;
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003227#ifdef FEAT_TERMINAL
3228 else if (terminal_is_active())
3229 c = K_CANCEL;
3230#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 else if ((State & CMDLINE)
Bram Moolenaare2c38102016-01-31 14:55:40 +01003232#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 || (cmdwin_type > 0 && tc == ESC)
Bram Moolenaare2c38102016-01-31 14:55:40 +01003234#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 )
3236 c = Ctrl_C;
3237 else
3238 c = ESC;
Bram Moolenaare2c38102016-01-31 14:55:40 +01003239#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 tc = c;
Bram Moolenaare2c38102016-01-31 14:55:40 +01003241#endif
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01003242 // return from main_loop()
3243 if (pending_exmode_active)
3244 exmode_active = EXMODE_NORMAL;
3245
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 break;
3247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248
3249/*
3250 * get a character: 3. from the user - update display
3251 */
Bram Moolenaar30613902019-12-01 22:11:18 +01003252 // In insert mode a screen update is skipped when characters
3253 // are still available. But when those available characters
3254 // are part of a mapping, and we are going to do a blocking
3255 // wait here. Need to update the screen to display the
3256 // changed text so far. Also for when 'lazyredraw' is set and
3257 // redrawing was postponed because there was something in the
3258 // input buffer (e.g., termresponse).
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01003259 if (((State & INSERT) != 0 || p_lz) && (State & CMDLINE) == 0
3260 && advance && must_redraw != 0 && !need_wait_return)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 {
3262 update_screen(0);
Bram Moolenaar30613902019-12-01 22:11:18 +01003263 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 }
3265
3266 /*
3267 * If we have a partial match (and are going to wait for more
3268 * input from the user), show the partially matched characters
3269 * to the user with showcmd.
3270 */
3271#ifdef FEAT_CMDL_INFO
Bram Moolenaareda35f72019-08-03 14:59:44 +02003272 showcmd_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273#endif
3274 c1 = 0;
3275 if (typebuf.tb_len > 0 && advance && !exmode_active)
3276 {
3277 if (((State & (NORMAL | INSERT)) || State == LANGMAP)
3278 && State != HITRETURN)
3279 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003280 // this looks nice when typing a dead character map
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 if (State & INSERT
3282 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
3283 + typebuf.tb_len - 1) == 1)
3284 {
3285 edit_putchar(typebuf.tb_buf[typebuf.tb_off
3286 + typebuf.tb_len - 1], FALSE);
Bram Moolenaar30613902019-12-01 22:11:18 +01003287 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 c1 = 1;
3289 }
3290#ifdef FEAT_CMDL_INFO
Bram Moolenaar30613902019-12-01 22:11:18 +01003291 // need to use the col and row from above here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 old_wcol = curwin->w_wcol;
3293 old_wrow = curwin->w_wrow;
3294 curwin->w_wcol = new_wcol;
3295 curwin->w_wrow = new_wrow;
3296 push_showcmd();
3297 if (typebuf.tb_len > SHOWCMD_COLS)
Bram Moolenaareda35f72019-08-03 14:59:44 +02003298 showcmd_idx = typebuf.tb_len - SHOWCMD_COLS;
3299 while (showcmd_idx < typebuf.tb_len)
3300 (void)add_to_showcmd(
3301 typebuf.tb_buf[typebuf.tb_off + showcmd_idx++]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 curwin->w_wcol = old_wcol;
3303 curwin->w_wrow = old_wrow;
3304#endif
3305 }
3306
Bram Moolenaar30613902019-12-01 22:11:18 +01003307 // this looks nice when typing a dead character map
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 if ((State & CMDLINE)
3309#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3310 && cmdline_star == 0
3311#endif
3312 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
3313 + typebuf.tb_len - 1) == 1)
3314 {
3315 putcmdline(typebuf.tb_buf[typebuf.tb_off
3316 + typebuf.tb_len - 1], FALSE);
3317 c1 = 1;
3318 }
3319 }
3320
3321/*
3322 * get a character: 3. from the user - get it
3323 */
Bram Moolenaar83f4cbd2018-06-12 21:35:40 +02003324 if (typebuf.tb_len == 0)
3325 // timedout may have been set while waiting for a mapping
3326 // that has a <Nop> RHS.
3327 timedout = FALSE;
3328
Bram Moolenaar652de232019-04-04 20:13:09 +02003329 if (advance)
3330 {
3331 if (typebuf.tb_len == 0
3332 || !(p_timeout
3333 || (p_ttimeout && keylen == KEYLEN_PART_KEY)))
3334 // blocking wait
3335 wait_time = -1L;
3336 else if (keylen == KEYLEN_PART_KEY && p_ttm >= 0)
3337 wait_time = p_ttm;
3338 else
3339 wait_time = p_tm;
3340 }
3341 else
3342 wait_time = 0;
3343
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003344 wait_tb_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 c = inchar(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len,
3346 typebuf.tb_buflen - typebuf.tb_off - typebuf.tb_len - 1,
Bram Moolenaar652de232019-04-04 20:13:09 +02003347 wait_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348
3349#ifdef FEAT_CMDL_INFO
Bram Moolenaareda35f72019-08-03 14:59:44 +02003350 if (showcmd_idx != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 pop_showcmd();
3352#endif
3353 if (c1 == 1)
3354 {
3355 if (State & INSERT)
3356 edit_unputchar();
3357 if (State & CMDLINE)
3358 unputcmdline();
Bram Moolenaarbc256d92012-06-06 12:06:15 +02003359 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003360 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 }
3362
3363 if (c < 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003364 continue; // end of input script reached
3365 if (c == NUL) // no character available
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 {
3367 if (!advance)
3368 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01003369 if (wait_tb_len > 0) // timed out
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 {
3371 timedout = TRUE;
3372 continue;
3373 }
3374 }
3375 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003376 { // allow mapping for just typed characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 while (typebuf.tb_buf[typebuf.tb_off
3378 + typebuf.tb_len] != NUL)
3379 typebuf.tb_noremap[typebuf.tb_off
3380 + typebuf.tb_len++] = RM_YES;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003381#ifdef HAVE_INPUT_METHOD
Bram Moolenaar30613902019-12-01 22:11:18 +01003382 // Get IM status right after getting keys, not after the
3383 // timeout for a mapping (focus may be lost by then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 vgetc_im_active = im_get_status();
3385#endif
3386 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003387 } // for (;;)
3388 } // if (!character from stuffbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389
Bram Moolenaar30613902019-12-01 22:11:18 +01003390 // if advance is FALSE don't loop on NULs
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003391 } while ((c < 0 && c != K_CANCEL) || (advance && c == NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392
3393 /*
3394 * The "INSERT" message is taken care of here:
3395 * if we return an ESC to exit insert mode, the message is deleted
3396 * if we don't return an ESC but deleted the message before, redisplay it
3397 */
Bram Moolenaar09df3122006-01-23 22:23:09 +00003398 if (advance && p_smd && msg_silent == 0 && (State & INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003400 if (c == ESC && !mode_deleted && !no_mapping && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 {
3402 if (typebuf.tb_len && !KeyTyped)
Bram Moolenaar30613902019-12-01 22:11:18 +01003403 redraw_cmdline = TRUE; // delete mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 else
3405 unshowmode(FALSE);
3406 }
3407 else if (c != ESC && mode_deleted)
3408 {
3409 if (typebuf.tb_len && !KeyTyped)
Bram Moolenaar30613902019-12-01 22:11:18 +01003410 redraw_cmdline = TRUE; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 else
3412 showmode();
3413 }
3414 }
3415#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003416 // may unshow different cursor shape
Bram Moolenaarf085f422017-06-07 20:39:47 +02003417 if (gui.in_use && shape_changed)
3418 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419#endif
Bram Moolenaar6edbbd82019-03-10 09:41:51 +01003420 if (timedout && c == ESC)
3421 {
3422 char_u nop_buf[3];
3423
3424 // When recording there will be no timeout. Add a <Nop> after the ESC
3425 // to avoid that it forms a key code with following characters.
3426 nop_buf[0] = K_SPECIAL;
3427 nop_buf[1] = KS_EXTRA;
3428 nop_buf[2] = KE_NOP;
3429 gotchars(nop_buf, 3);
3430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003432 --vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433
3434 return c;
3435}
3436
3437/*
3438 * inchar() - get one character from
3439 * 1. a scriptfile
3440 * 2. the keyboard
3441 *
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003442 * As many characters as we can get (up to 'maxlen') are put in "buf" and
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 * NUL terminated (buffer length must be 'maxlen' + 1).
3444 * Minimum for "maxlen" is 3!!!!
3445 *
3446 * "tb_change_cnt" is the value of typebuf.tb_change_cnt if "buf" points into
3447 * it. When typebuf.tb_change_cnt changes (e.g., when a message is received
3448 * from a remote client) "buf" can no longer be used. "tb_change_cnt" is 0
3449 * otherwise.
3450 *
3451 * If we got an interrupt all input is read until none is available.
3452 *
3453 * If wait_time == 0 there is no waiting for the char.
3454 * If wait_time == n we wait for n msec for a character to arrive.
3455 * If wait_time == -1 we wait forever for a character to arrive.
3456 *
3457 * Return the number of obtained characters.
3458 * Return -1 when end of input script reached.
3459 */
Bram Moolenaarcda77642016-06-04 13:32:35 +02003460 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003461inchar(
3462 char_u *buf,
3463 int maxlen,
Bram Moolenaar30613902019-12-01 22:11:18 +01003464 long wait_time) // milli seconds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465{
Bram Moolenaar30613902019-12-01 22:11:18 +01003466 int len = 0; // init for GCC
3467 int retesc = FALSE; // return ESC with gotint
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 int script_char;
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003469 int tb_change_cnt = typebuf.tb_change_cnt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470
Bram Moolenaar30613902019-12-01 22:11:18 +01003471 if (wait_time == -1L || wait_time > 100L) // flush output before waiting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 {
3473 cursor_on();
Bram Moolenaara338adc2018-01-31 20:51:47 +01003474 out_flush_cursor(FALSE, FALSE);
3475#if defined(FEAT_GUI) && defined(FEAT_MOUSESHAPE)
3476 if (gui.in_use && postponed_mouseshape)
3477 update_mouseshape(-1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478#endif
3479 }
3480
3481 /*
3482 * Don't reset these when at the hit-return prompt, otherwise a endless
3483 * recursive loop may result (write error in swapfile, hit-return, timeout
3484 * on char wait, flush swapfile, write error....).
3485 */
3486 if (State != HITRETURN)
3487 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003488 did_outofmem_msg = FALSE; // display out of memory message (again)
3489 did_swapwrite_msg = FALSE; // display swap file write error again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003491 undo_off = FALSE; // restart undo now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492
3493 /*
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00003494 * Get a character from a script file if there is one.
3495 * If interrupted: Stop reading script files, close them all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 */
3497 script_char = -1;
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00003498 while (scriptin[curscript] != NULL && script_char < 0
3499#ifdef FEAT_EVAL
3500 && !ignore_script
3501#endif
3502 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02003504#ifdef MESSAGE_QUEUE
3505 parse_queued_messages();
Bram Moolenaarf2330482008-06-24 20:19:36 +00003506#endif
3507
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 if (got_int || (script_char = getc(scriptin[curscript])) < 0)
3509 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003510 // Reached EOF.
3511 // Careful: closescript() frees typebuf.tb_buf[] and buf[] may
3512 // point inside typebuf.tb_buf[]. Don't use buf[] after this!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 closescript();
3514 /*
3515 * When reading script file is interrupted, return an ESC to get
3516 * back to normal mode.
3517 * Otherwise return -1, because typebuf.tb_buf[] has changed.
3518 */
3519 if (got_int)
3520 retesc = TRUE;
3521 else
3522 return -1;
3523 }
3524 else
3525 {
3526 buf[0] = script_char;
3527 len = 1;
3528 }
3529 }
3530
Bram Moolenaar30613902019-12-01 22:11:18 +01003531 if (script_char < 0) // did not get a character from script
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 {
3533 /*
3534 * If we got an interrupt, skip all previously typed characters and
3535 * return TRUE if quit reading script file.
3536 * Stop reading typeahead when a single CTRL-C was read,
3537 * fill_input_buf() returns this when not able to read from stdin.
3538 * Don't use buf[] here, closescript() may have freed typebuf.tb_buf[]
3539 * and buf may be pointing inside typebuf.tb_buf[].
3540 */
3541 if (got_int)
3542 {
3543#define DUM_LEN MAXMAPLEN * 3 + 3
3544 char_u dum[DUM_LEN + 1];
3545
3546 for (;;)
3547 {
3548 len = ui_inchar(dum, DUM_LEN, 0L, 0);
3549 if (len == 0 || (len == 1 && dum[0] == 3))
3550 break;
3551 }
3552 return retesc;
3553 }
3554
3555 /*
3556 * Always flush the output characters when getting input characters
Bram Moolenaarcb574f42019-01-25 22:29:57 +01003557 * from the user and not just peeking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 */
Bram Moolenaarcb574f42019-01-25 22:29:57 +01003559 if (wait_time == -1L || wait_time > 10L)
3560 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561
3562 /*
3563 * Fill up to a third of the buffer, because each character may be
3564 * tripled below.
3565 */
3566 len = ui_inchar(buf, maxlen / 3, wait_time, tb_change_cnt);
3567 }
3568
Bram Moolenaar30613902019-12-01 22:11:18 +01003569 // If the typebuf was changed further down, it is like nothing was added by
3570 // this call.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 if (typebuf_changed(tb_change_cnt))
3572 return 0;
3573
Bram Moolenaar30613902019-12-01 22:11:18 +01003574 // Note the change in the typeahead buffer, this matters for when
3575 // vgetorpeek() is called recursively, e.g. using getchar(1) in a timer
3576 // function.
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003577 if (len > 0 && ++typebuf.tb_change_cnt == 0)
3578 typebuf.tb_change_cnt = 1;
3579
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003580 return fix_input_buffer(buf, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581}
3582
3583/*
3584 * Fix typed characters for use by vgetc() and check_termcode().
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003585 * "buf[]" must have room to triple the number of bytes!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 * Returns the new length.
3587 */
3588 int
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003589fix_input_buffer(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590{
3591 int i;
3592 char_u *p = buf;
3593
3594 /*
3595 * Two characters are special: NUL and K_SPECIAL.
3596 * When compiled With the GUI CSI is also special.
3597 * Replace NUL by K_SPECIAL KS_ZERO KE_FILLER
3598 * Replace K_SPECIAL by K_SPECIAL KS_SPECIAL KE_FILLER
3599 * Replace CSI by K_SPECIAL KS_EXTRA KE_CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 */
3601 for (i = len; --i >= 0; ++p)
3602 {
3603#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003604 // When the GUI is used any character can come after a CSI, don't
3605 // escape it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 if (gui.in_use && p[0] == CSI && i >= 2)
3607 {
3608 p += 2;
3609 i -= 2;
3610 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003611# ifndef MSWIN
Bram Moolenaar30613902019-12-01 22:11:18 +01003612 // When the GUI is not used CSI needs to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 else if (!gui.in_use && p[0] == CSI)
3614 {
3615 mch_memmove(p + 3, p + 1, (size_t)i);
3616 *p++ = K_SPECIAL;
3617 *p++ = KS_EXTRA;
3618 *p = (int)KE_CSI;
3619 len += 2;
3620 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003621# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 else
3623#endif
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003624 if (p[0] == NUL || (p[0] == K_SPECIAL
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003625 // timeout may generate K_CURSORHOLD
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003626 && (i < 2 || p[1] != KS_EXTRA || p[2] != (int)KE_CURSORHOLD)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003627#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003628 // Win32 console passes modifiers
3629 && (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003630# ifdef VIMDLL
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003631 gui.in_use ||
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003632# endif
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003633 (i < 2 || p[1] != KS_MODIFIER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634#endif
3635 ))
3636 {
3637 mch_memmove(p + 3, p + 1, (size_t)i);
3638 p[2] = K_THIRD(p[0]);
3639 p[1] = K_SECOND(p[0]);
3640 p[0] = K_SPECIAL;
3641 p += 2;
3642 len += 2;
3643 }
3644 }
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003645 *p = NUL; // add trailing NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 return len;
3647}
3648
3649#if defined(USE_INPUT_BUF) || defined(PROTO)
3650/*
3651 * Return TRUE when bytes are in the input buffer or in the typeahead buffer.
3652 * Normally the input buffer would be sufficient, but the server_to_input_buf()
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003653 * or feedkeys() may insert characters in the typeahead buffer while we are
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003654 * waiting for input to arrive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 */
3656 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003657input_available(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658{
3659 return (!vim_is_input_buf_empty()
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003660# if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
3661 || typebuf_was_filled
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662# endif
3663 );
3664}
3665#endif
Bram Moolenaar957cf672020-11-12 14:21:06 +01003666
3667/*
3668 * Function passed to do_cmdline() to get the command after a <Cmd> key from
3669 * typeahead.
3670 */
3671 char_u *
3672getcmdkeycmd(
3673 int promptc UNUSED,
3674 void *cookie UNUSED,
3675 int indent UNUSED,
3676 getline_opt_T do_concat UNUSED)
3677{
3678 garray_T line_ga;
3679 int c1 = -1;
3680 int c2;
3681 int cmod = 0;
3682 int aborted = FALSE;
3683
3684 ga_init2(&line_ga, 1, 32);
3685
3686 // no mapping for these characters
3687 no_mapping++;
3688
3689 got_int = FALSE;
3690 while (c1 != NUL && !aborted)
3691 {
Bram Moolenaarca359cb2020-11-15 20:49:41 +01003692 if (ga_grow(&line_ga, 32) != OK)
3693 {
3694 aborted = TRUE;
3695 break;
3696 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003697
3698 if (vgetorpeek(FALSE) == NUL)
3699 {
3700 // incomplete <Cmd> is an error, because there is not much the user
3701 // could do in this state.
3702 emsg(_(e_cmd_mapping_must_end_with_cr));
3703 aborted = TRUE;
3704 break;
3705 }
3706
3707 // Get one character at a time.
3708 c1 = vgetorpeek(TRUE);
3709
3710 // Get two extra bytes for special keys
3711 if (c1 == K_SPECIAL)
3712 {
3713 c1 = vgetorpeek(TRUE);
3714 c2 = vgetorpeek(TRUE);
3715 if (c1 == KS_MODIFIER)
3716 {
3717 cmod = c2;
3718 continue;
3719 }
3720 c1 = TO_SPECIAL(c1, c2);
3721 }
Bram Moolenaar4a441202020-11-28 14:43:26 +01003722 if (c1 == Ctrl_V)
3723 {
3724 // CTRL-V is followed by octal, hex or other characters, reverses
3725 // what AppendToRedobuffLit() does.
3726 no_reduce_keys = TRUE; // don't merge modifyOtherKeys
Bram Moolenaar0684e362020-12-03 19:54:42 +01003727 c1 = get_literal(TRUE);
Bram Moolenaar4a441202020-11-28 14:43:26 +01003728 no_reduce_keys = FALSE;
3729 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003730
3731 if (got_int)
3732 aborted = TRUE;
3733 else if (c1 == '\r' || c1 == '\n')
3734 c1 = NUL; // end the line
3735 else if (c1 == ESC)
3736 aborted = TRUE;
3737 else if (c1 == K_COMMAND)
3738 {
3739 // give a nicer error message for this special case
3740 emsg(_(e_cmd_mapping_must_end_with_cr_before_second_cmd));
3741 aborted = TRUE;
3742 }
3743 else if (IS_SPECIAL(c1))
3744 {
3745 if (c1 == K_SNR)
Bram Moolenaarc77534c2020-11-18 11:34:37 +01003746 ga_concat(&line_ga, (char_u *)"<SNR>");
Bram Moolenaar957cf672020-11-12 14:21:06 +01003747 else
3748 {
3749 semsg(e_cmd_maping_must_not_include_str_key,
3750 get_special_key_name(c1, cmod));
3751 aborted = TRUE;
3752 }
3753 }
3754 else
3755 ga_append(&line_ga, (char)c1);
3756
3757 cmod = 0;
3758 }
3759
3760 no_mapping--;
3761
3762 if (aborted)
3763 ga_clear(&line_ga);
3764
3765 return (char_u *)line_ga.ga_data;
3766}