blob: 196a3aeebd08fff09995ed4d88efd1a713c764e3 [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 }
246 return;
247}
248
249/*
250 * Add number "n" to buffer "buf".
251 */
252 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100253add_num_buff(buffheader_T *buf, long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254{
255 char_u number[32];
256
257 sprintf((char *)number, "%ld", n);
258 add_buff(buf, number, -1L);
259}
260
261/*
262 * Add character 'c' to buffer "buf".
263 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
264 */
265 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100266add_char_buff(buffheader_T *buf, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268 char_u bytes[MB_MAXBYTES + 1];
269 int len;
270 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271 char_u temp[4];
272
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273 if (IS_SPECIAL(c))
274 len = 1;
275 else
276 len = (*mb_char2bytes)(c, bytes);
277 for (i = 0; i < len; ++i)
278 {
279 if (!IS_SPECIAL(c))
280 c = bytes[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281
282 if (IS_SPECIAL(c) || c == K_SPECIAL || c == NUL)
283 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100284 // translate special key code into three byte sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285 temp[0] = K_SPECIAL;
286 temp[1] = K_SECOND(c);
287 temp[2] = K_THIRD(c);
288 temp[3] = NUL;
289 }
290#ifdef FEAT_GUI
291 else if (c == CSI)
292 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100293 // Translate a CSI to a CSI - KS_EXTRA - KE_CSI sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294 temp[0] = CSI;
295 temp[1] = KS_EXTRA;
296 temp[2] = (int)KE_CSI;
297 temp[3] = NUL;
298 }
299#endif
300 else
301 {
302 temp[0] = c;
303 temp[1] = NUL;
304 }
305 add_buff(buf, temp, -1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307}
308
Bram Moolenaar30613902019-12-01 22:11:18 +0100309// First read ahead buffer. Used for translated commands.
Bram Moolenaar285e3352018-04-18 23:01:13 +0200310static buffheader_T readbuf1 = {{NULL, {NUL}}, NULL, 0, 0};
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100311
Bram Moolenaar30613902019-12-01 22:11:18 +0100312// Second read ahead buffer. Used for redo.
Bram Moolenaar285e3352018-04-18 23:01:13 +0200313static buffheader_T readbuf2 = {{NULL, {NUL}}, NULL, 0, 0};
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100314
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315/*
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100316 * Get one byte from the read buffers. Use readbuf1 one first, use readbuf2
317 * if that one is empty.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318 * If advance == TRUE go to the next char.
319 * No translation is done K_SPECIAL and CSI are escaped.
320 */
321 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100322read_readbuffers(int advance)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100324 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100326 c = read_readbuf(&readbuf1, advance);
327 if (c == NUL)
328 c = read_readbuf(&readbuf2, advance);
329 return c;
330}
331
332 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100333read_readbuf(buffheader_T *buf, int advance)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100334{
335 char_u c;
336 buffblock_T *curr;
337
Bram Moolenaar30613902019-12-01 22:11:18 +0100338 if (buf->bh_first.b_next == NULL) // buffer is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339 return NUL;
340
Bram Moolenaar285e3352018-04-18 23:01:13 +0200341 curr = buf->bh_first.b_next;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100342 c = curr->b_str[buf->bh_index];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343
344 if (advance)
345 {
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100346 if (curr->b_str[++buf->bh_index] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347 {
Bram Moolenaar285e3352018-04-18 23:01:13 +0200348 buf->bh_first.b_next = curr->b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 vim_free(curr);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100350 buf->bh_index = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351 }
352 }
353 return c;
354}
355
356/*
Bram Moolenaar06811f32014-02-15 16:17:07 +0100357 * Prepare the read buffers for reading (if they contain something).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 */
359 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100360start_stuff(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361{
Bram Moolenaar285e3352018-04-18 23:01:13 +0200362 if (readbuf1.bh_first.b_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363 {
Bram Moolenaar285e3352018-04-18 23:01:13 +0200364 readbuf1.bh_curr = &(readbuf1.bh_first);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100365 readbuf1.bh_space = 0;
366 }
Bram Moolenaar285e3352018-04-18 23:01:13 +0200367 if (readbuf2.bh_first.b_next != NULL)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100368 {
Bram Moolenaar285e3352018-04-18 23:01:13 +0200369 readbuf2.bh_curr = &(readbuf2.bh_first);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100370 readbuf2.bh_space = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371 }
372}
373
374/*
375 * Return TRUE if the stuff buffer is empty.
376 */
377 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100378stuff_empty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379{
Bram Moolenaar285e3352018-04-18 23:01:13 +0200380 return (readbuf1.bh_first.b_next == NULL
381 && readbuf2.bh_first.b_next == NULL);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100382}
383
Bram Moolenaar113e1072019-01-20 15:30:40 +0100384#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100385/*
386 * Return TRUE if readbuf1 is empty. There may still be redo characters in
387 * redbuf2.
388 */
389 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100390readbuf1_empty(void)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100391{
Bram Moolenaar285e3352018-04-18 23:01:13 +0200392 return (readbuf1.bh_first.b_next == NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100394#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395
396/*
397 * Set a typeahead character that won't be flushed.
398 */
399 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100400typeahead_noflush(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401{
402 typeahead_char = c;
403}
404
405/*
406 * Remove the contents of the stuff buffer and the mapped characters in the
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100407 * typeahead buffer (used in case of an error). If "flush_typeahead" is true,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408 * flush all typeahead characters (used when interrupted by a CTRL-C).
409 */
410 void
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200411flush_buffers(flush_buffers_T flush_typeahead)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412{
413 init_typebuf();
414
415 start_stuff();
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100416 while (read_readbuffers(TRUE) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417 ;
418
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200419 if (flush_typeahead == FLUSH_MINIMAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420 {
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200421 // remove mapped characters at the start only
422 typebuf.tb_off += typebuf.tb_maplen;
423 typebuf.tb_len -= typebuf.tb_maplen;
Bram Moolenaare49b4bb2020-03-11 13:01:40 +0100424#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
425 if (typebuf.tb_len == 0)
426 typebuf_was_filled = FALSE;
427#endif
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200428 }
429 else
430 {
431 // remove typeahead
432 if (flush_typeahead == FLUSH_INPUT)
433 // We have to get all characters, because we may delete the first
434 // part of an escape sequence. In an xterm we get one char at a
435 // time and we have to get them all.
436 while (inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 10L) != 0)
437 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438 typebuf.tb_off = MAXMAPLEN;
439 typebuf.tb_len = 0;
Bram Moolenaar4eb65312017-06-24 18:49:00 +0200440#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar30613902019-12-01 22:11:18 +0100441 // Reset the flag that text received from a client or from feedkeys()
442 // was inserted in the typeahead buffer.
Bram Moolenaar4eb65312017-06-24 18:49:00 +0200443 typebuf_was_filled = FALSE;
444#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446 typebuf.tb_maplen = 0;
447 typebuf.tb_silent = 0;
448 cmd_silent = FALSE;
449 typebuf.tb_no_abbr_cnt = 0;
Bram Moolenaarb8d732e2020-08-05 22:07:26 +0200450 if (++typebuf.tb_change_cnt == 0)
451 typebuf.tb_change_cnt = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452}
453
454/*
455 * The previous contents of the redo buffer is kept in old_redobuffer.
456 * This is used for the CTRL-O <.> command in insert mode.
457 */
458 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100459ResetRedobuff(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460{
461 if (!block_redo)
462 {
463 free_buff(&old_redobuff);
464 old_redobuff = redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200465 redobuff.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466 }
467}
468
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100469/*
470 * Discard the contents of the redo buffer and restore the previous redo
471 * buffer.
472 */
473 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100474CancelRedo(void)
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100475{
476 if (!block_redo)
477 {
478 free_buff(&redobuff);
479 redobuff = old_redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200480 old_redobuff.bh_first.b_next = NULL;
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100481 start_stuff();
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100482 while (read_readbuffers(TRUE) != NUL)
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100483 ;
484 }
485}
486
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487/*
488 * Save redobuff and old_redobuff to save_redobuff and save_old_redobuff.
489 * Used before executing autocommands and user functions.
490 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 void
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200492saveRedobuff(save_redo_T *save_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493{
494 char_u *s;
495
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200496 save_redo->sr_redobuff = redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200497 redobuff.bh_first.b_next = NULL;
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200498 save_redo->sr_old_redobuff = old_redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200499 old_redobuff.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500
Bram Moolenaar30613902019-12-01 22:11:18 +0100501 // Make a copy, so that ":normal ." in a function works.
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200502 s = get_buffcont(&save_redo->sr_redobuff, FALSE);
503 if (s != NULL)
504 {
505 add_buff(&redobuff, s, -1L);
506 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 }
508}
509
510/*
511 * Restore redobuff and old_redobuff from save_redobuff and save_old_redobuff.
512 * Used after executing autocommands and user functions.
513 */
514 void
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200515restoreRedobuff(save_redo_T *save_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516{
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200517 free_buff(&redobuff);
518 redobuff = save_redo->sr_redobuff;
519 free_buff(&old_redobuff);
520 old_redobuff = save_redo->sr_old_redobuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522
523/*
524 * Append "s" to the redo buffer.
525 * K_SPECIAL and CSI should already have been escaped.
526 */
527 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100528AppendToRedobuff(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529{
530 if (!block_redo)
531 add_buff(&redobuff, s, -1L);
532}
533
534/*
535 * Append to Redo buffer literally, escaping special characters with CTRL-V.
536 * K_SPECIAL and CSI are escaped as well.
537 */
538 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100539AppendToRedobuffLit(
540 char_u *str,
Bram Moolenaar30613902019-12-01 22:11:18 +0100541 int len) // length of "str" or -1 for up to the NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542{
Bram Moolenaarebefac62005-12-28 22:39:57 +0000543 char_u *s = str;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 int c;
545 char_u *start;
546
547 if (block_redo)
548 return;
549
Bram Moolenaarebefac62005-12-28 22:39:57 +0000550 while (len < 0 ? *s != NUL : s - str < len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100552 // Put a string of normal characters in the redo buffer (that's
553 // faster).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 start = s;
555 while (*s >= ' '
556#ifndef EBCDIC
Bram Moolenaar30613902019-12-01 22:11:18 +0100557 && *s < DEL // EBCDIC: all chars above space are normal
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558#endif
Bram Moolenaarebefac62005-12-28 22:39:57 +0000559 && (len < 0 || s - str < len))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 ++s;
561
Bram Moolenaar30613902019-12-01 22:11:18 +0100562 // Don't put '0' or '^' as last character, just in case a CTRL-D is
563 // typed next.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564 if (*s == NUL && (s[-1] == '0' || s[-1] == '^'))
565 --s;
566 if (s > start)
567 add_buff(&redobuff, start, (long)(s - start));
568
Bram Moolenaarebefac62005-12-28 22:39:57 +0000569 if (*s == NUL || (len >= 0 && s - str >= len))
570 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571
Bram Moolenaar30613902019-12-01 22:11:18 +0100572 // Handle a special or multibyte character.
Bram Moolenaarebefac62005-12-28 22:39:57 +0000573 if (has_mbyte)
Bram Moolenaar30613902019-12-01 22:11:18 +0100574 // Handle composing chars separately.
Bram Moolenaarebefac62005-12-28 22:39:57 +0000575 c = mb_cptr2char_adv(&s);
576 else
Bram Moolenaarebefac62005-12-28 22:39:57 +0000577 c = *s++;
578 if (c < ' ' || c == DEL || (*s == NUL && (c == '0' || c == '^')))
579 add_char_buff(&redobuff, Ctrl_V);
580
Bram Moolenaar30613902019-12-01 22:11:18 +0100581 // CTRL-V '0' must be inserted as CTRL-V 048 (EBCDIC: xf0)
Bram Moolenaarebefac62005-12-28 22:39:57 +0000582 if (*s == NUL && c == '0')
583#ifdef EBCDIC
584 add_buff(&redobuff, (char_u *)"xf0", 3L);
585#else
586 add_buff(&redobuff, (char_u *)"048", 3L);
587#endif
588 else
589 add_char_buff(&redobuff, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 }
591}
592
593/*
594 * Append a character to the redo buffer.
595 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
596 */
597 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100598AppendCharToRedobuff(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599{
600 if (!block_redo)
601 add_char_buff(&redobuff, c);
602}
603
604/*
605 * Append a number to the redo buffer.
606 */
607 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100608AppendNumberToRedobuff(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609{
610 if (!block_redo)
611 add_num_buff(&redobuff, n);
612}
613
614/*
615 * Append string "s" to the stuff buffer.
616 * CSI and K_SPECIAL must already have been escaped.
617 */
618 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100619stuffReadbuff(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100621 add_buff(&readbuf1, s, -1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622}
623
Bram Moolenaar4f5ce332014-07-30 16:00:58 +0200624/*
625 * Append string "s" to the redo stuff buffer.
626 * CSI and K_SPECIAL must already have been escaped.
627 */
628 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100629stuffRedoReadbuff(char_u *s)
Bram Moolenaar4f5ce332014-07-30 16:00:58 +0200630{
631 add_buff(&readbuf2, s, -1L);
632}
633
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100635stuffReadbuffLen(char_u *s, long len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100637 add_buff(&readbuf1, s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638}
639
640#if defined(FEAT_EVAL) || defined(PROTO)
641/*
642 * Stuff "s" into the stuff buffer, leaving special key codes unmodified and
643 * escaping other K_SPECIAL and CSI bytes.
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200644 * Change CR, LF and ESC into a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 */
646 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100647stuffReadbuffSpec(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648{
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200649 int c;
650
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 while (*s != NUL)
652 {
653 if (*s == K_SPECIAL && s[1] != NUL && s[2] != NUL)
654 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100655 // Insert special key literally.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 stuffReadbuffLen(s, 3L);
657 s += 3;
658 }
659 else
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200660 {
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200661 c = mb_ptr2char_adv(&s);
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200662 if (c == CAR || c == NL || c == ESC)
663 c = ' ';
664 stuffcharReadbuff(c);
665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 }
667}
668#endif
669
670/*
671 * Append a character to the stuff buffer.
672 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
673 */
674 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100675stuffcharReadbuff(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100677 add_char_buff(&readbuf1, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678}
679
680/*
681 * Append a number to the stuff buffer.
682 */
683 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100684stuffnumReadbuff(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100686 add_num_buff(&readbuf1, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687}
688
689/*
Bram Moolenaar11abd092020-05-01 14:26:37 +0200690 * Stuff a string into the typeahead buffer, such that edit() will insert it
691 * literally ("literally" TRUE) or interpret is as typed characters.
692 */
693 void
694stuffescaped(char_u *arg, int literally)
695{
696 int c;
697 char_u *start;
698
699 while (*arg != NUL)
700 {
701 // Stuff a sequence of normal ASCII characters, that's fast. Also
702 // stuff K_SPECIAL to get the effect of a special key when "literally"
703 // is TRUE.
704 start = arg;
705 while ((*arg >= ' '
706#ifndef EBCDIC
707 && *arg < DEL // EBCDIC: chars above space are normal
708#endif
709 )
710 || (*arg == K_SPECIAL && !literally))
711 ++arg;
712 if (arg > start)
713 stuffReadbuffLen(start, (long)(arg - start));
714
715 // stuff a single special character
716 if (*arg != NUL)
717 {
718 if (has_mbyte)
719 c = mb_cptr2char_adv(&arg);
720 else
721 c = *arg++;
722 if (literally && ((c < ' ' && c != TAB) || c == DEL))
723 stuffcharReadbuff(Ctrl_V);
724 stuffcharReadbuff(c);
725 }
726 }
727}
728
729/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 * Read a character from the redo buffer. Translates K_SPECIAL, CSI and
731 * multibyte characters.
732 * The redo buffer is left as it is.
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100733 * If init is TRUE, prepare for redo, return FAIL if nothing to redo, OK
734 * otherwise.
735 * If old is TRUE, use old_redobuff instead of redobuff.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 */
737 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100738read_redo(int init, int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100740 static buffblock_T *bp;
741 static char_u *p;
742 int c;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100743 int n;
744 char_u buf[MB_MAXBYTES + 1];
745 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746
747 if (init)
748 {
749 if (old_redo)
Bram Moolenaar285e3352018-04-18 23:01:13 +0200750 bp = old_redobuff.bh_first.b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 else
Bram Moolenaar285e3352018-04-18 23:01:13 +0200752 bp = redobuff.bh_first.b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 if (bp == NULL)
754 return FAIL;
755 p = bp->b_str;
756 return OK;
757 }
758 if ((c = *p) != NUL)
759 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100760 // Reverse the conversion done by add_char_buff()
761 // For a multi-byte character get all the bytes and return the
762 // converted character.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 if (has_mbyte && (c != K_SPECIAL || p[1] == KS_SPECIAL))
764 n = MB_BYTE2LEN_CHECK(c);
765 else
766 n = 1;
767 for (i = 0; ; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100769 if (c == K_SPECIAL) // special key or escaped K_SPECIAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 {
771 c = TO_SPECIAL(p[1], p[2]);
772 p += 2;
773 }
774#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +0100775 if (c == CSI) // escaped CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 p += 2;
777#endif
778 if (*++p == NUL && bp->b_next != NULL)
779 {
780 bp = bp->b_next;
781 p = bp->b_str;
782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 buf[i] = c;
Bram Moolenaar30613902019-12-01 22:11:18 +0100784 if (i == n - 1) // last byte of a character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 {
786 if (n != 1)
787 c = (*mb_ptr2char)(buf);
788 break;
789 }
790 c = *p;
Bram Moolenaar30613902019-12-01 22:11:18 +0100791 if (c == NUL) // cannot happen?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 }
794 }
795
796 return c;
797}
798
799/*
800 * Copy the rest of the redo buffer into the stuff buffer (in a slow way).
801 * If old_redo is TRUE, use old_redobuff instead of redobuff.
802 * The escaped K_SPECIAL and CSI are copied without translation.
803 */
804 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100805copy_redo(int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806{
807 int c;
808
809 while ((c = read_redo(FALSE, old_redo)) != NUL)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100810 add_char_buff(&readbuf2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811}
812
813/*
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100814 * Stuff the redo buffer into readbuf2.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 * Insert the redo count into the command.
816 * If "old_redo" is TRUE, the last but one command is repeated
817 * instead of the last command (inserting text). This is used for
818 * CTRL-O <.> in insert mode
819 *
820 * return FAIL for failure, OK otherwise
821 */
822 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100823start_redo(long count, int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824{
825 int c;
826
Bram Moolenaar30613902019-12-01 22:11:18 +0100827 // init the pointers; return if nothing to redo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 if (read_redo(TRUE, old_redo) == FAIL)
829 return FAIL;
830
831 c = read_redo(FALSE, old_redo);
832
Bram Moolenaar30613902019-12-01 22:11:18 +0100833 // copy the buffer name, if present
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 if (c == '"')
835 {
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100836 add_buff(&readbuf2, (char_u *)"\"", 1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 c = read_redo(FALSE, old_redo);
838
Bram Moolenaar30613902019-12-01 22:11:18 +0100839 // if a numbered buffer is used, increment the number
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 if (c >= '1' && c < '9')
841 ++c;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100842 add_char_buff(&readbuf2, c);
Bram Moolenaar833093b2018-05-23 21:53:52 +0200843
Bram Moolenaar30613902019-12-01 22:11:18 +0100844 // the expression register should be re-evaluated
Bram Moolenaar833093b2018-05-23 21:53:52 +0200845 if (c == '=')
846 {
847 add_char_buff(&readbuf2, CAR);
848 cmd_silent = TRUE;
849 }
850
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 c = read_redo(FALSE, old_redo);
852 }
853
Bram Moolenaar30613902019-12-01 22:11:18 +0100854 if (c == 'v') // redo Visual
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 {
856 VIsual = curwin->w_cursor;
857 VIsual_active = TRUE;
858 VIsual_select = FALSE;
859 VIsual_reselect = TRUE;
860 redo_VIsual_busy = TRUE;
861 c = read_redo(FALSE, old_redo);
862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863
Bram Moolenaar30613902019-12-01 22:11:18 +0100864 // try to enter the count (in place of a previous count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 if (count)
866 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100867 while (VIM_ISDIGIT(c)) // skip "old" count
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 c = read_redo(FALSE, old_redo);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100869 add_num_buff(&readbuf2, count);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 }
871
Bram Moolenaar30613902019-12-01 22:11:18 +0100872 // copy from the redo buffer into the stuff buffer
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100873 add_char_buff(&readbuf2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 copy_redo(old_redo);
875 return OK;
876}
877
878/*
879 * Repeat the last insert (R, o, O, a, A, i or I command) by stuffing
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100880 * the redo buffer into readbuf2.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 * return FAIL for failure, OK otherwise
882 */
883 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100884start_redo_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885{
886 int c;
887
888 if (read_redo(TRUE, FALSE) == FAIL)
889 return FAIL;
890 start_stuff();
891
Bram Moolenaar30613902019-12-01 22:11:18 +0100892 // skip the count and the command character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 while ((c = read_redo(FALSE, FALSE)) != NUL)
894 {
895 if (vim_strchr((char_u *)"AaIiRrOo", c) != NULL)
896 {
897 if (c == 'O' || c == 'o')
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100898 add_buff(&readbuf2, NL_STR, -1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 break;
900 }
901 }
902
Bram Moolenaar30613902019-12-01 22:11:18 +0100903 // copy the typed text from the redo buffer into the stuff buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 copy_redo(FALSE);
905 block_redo = TRUE;
906 return OK;
907}
908
909 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100910stop_redo_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911{
912 block_redo = FALSE;
913}
914
915/*
916 * Initialize typebuf.tb_buf to point to typebuf_init.
917 * alloc() cannot be used here: In out-of-memory situations it would
918 * be impossible to type anything.
919 */
920 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100921init_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922{
923 if (typebuf.tb_buf == NULL)
924 {
925 typebuf.tb_buf = typebuf_init;
926 typebuf.tb_noremap = noremapbuf_init;
927 typebuf.tb_buflen = TYPELEN_INIT;
928 typebuf.tb_len = 0;
Bram Moolenaard34f9b12017-04-08 18:41:13 +0200929 typebuf.tb_off = MAXMAPLEN + 4;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 typebuf.tb_change_cnt = 1;
931 }
932}
933
934/*
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200935 * Returns TRUE when keys cannot be remapped.
936 */
937 int
938noremap_keys(void)
939{
940 return KeyNoremap & (RM_NONE|RM_SCRIPT);
941}
942
943/*
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100944 * Insert a string in position 'offset' in the typeahead buffer (for "@r"
945 * and ":normal" command, vgetorpeek() and check_termcode()).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 *
947 * If noremap is REMAP_YES, new string can be mapped again.
948 * If noremap is REMAP_NONE, new string cannot be mapped again.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000949 * If noremap is REMAP_SKIP, fist char of new string cannot be mapped again,
950 * but abbreviations are allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 * If noremap is REMAP_SCRIPT, new string cannot be mapped again, except for
952 * script-local mappings.
953 * If noremap is > 0, that many characters of the new string cannot be mapped.
954 *
955 * If nottyped is TRUE, the string does not return KeyTyped (don't use when
956 * offset is non-zero!).
957 *
958 * If silent is TRUE, cmd_silent is set when the characters are obtained.
959 *
960 * return FAIL for failure, OK otherwise
961 */
962 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100963ins_typebuf(
964 char_u *str,
965 int noremap,
966 int offset,
967 int nottyped,
968 int silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969{
970 char_u *s1, *s2;
971 int newlen;
972 int addlen;
973 int i;
974 int newoff;
975 int val;
976 int nrm;
977
978 init_typebuf();
979 if (++typebuf.tb_change_cnt == 0)
980 typebuf.tb_change_cnt = 1;
Bram Moolenaard103ee72019-09-18 21:15:31 +0200981 state_no_longer_safe("ins_typebuf()");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982
983 addlen = (int)STRLEN(str);
Bram Moolenaar4e86cba2007-05-06 11:56:32 +0000984
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 if (offset == 0 && addlen <= typebuf.tb_off)
986 {
Bram Moolenaarcaa55b62017-01-10 13:51:09 +0100987 /*
988 * Easy case: there is room in front of typebuf.tb_buf[typebuf.tb_off]
989 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 typebuf.tb_off -= addlen;
991 mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen);
992 }
Bram Moolenaard34f9b12017-04-08 18:41:13 +0200993 else if (typebuf.tb_len == 0 && typebuf.tb_buflen
994 >= addlen + 3 * (MAXMAPLEN + 4))
995 {
996 /*
997 * Buffer is empty and string fits in the existing buffer.
998 * Leave some space before and after, if possible.
999 */
1000 typebuf.tb_off = (typebuf.tb_buflen - addlen - 3 * (MAXMAPLEN + 4)) / 2;
1001 mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen);
1002 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 else
1004 {
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001005 /*
1006 * Need to allocate a new buffer.
Bram Moolenaard34f9b12017-04-08 18:41:13 +02001007 * In typebuf.tb_buf there must always be room for 3 * (MAXMAPLEN + 4)
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001008 * characters. We add some extra room to avoid having to allocate too
1009 * often.
1010 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 newoff = MAXMAPLEN + 4;
1012 newlen = typebuf.tb_len + addlen + newoff + 4 * (MAXMAPLEN + 4);
Bram Moolenaar30613902019-12-01 22:11:18 +01001013 if (newlen < 0) // string is getting too long
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001015 emsg(_(e_toocompl)); // also calls flush_buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 setcursor();
1017 return FAIL;
1018 }
1019 s1 = alloc(newlen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001020 if (s1 == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 return FAIL;
1022 s2 = alloc(newlen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001023 if (s2 == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 {
1025 vim_free(s1);
1026 return FAIL;
1027 }
1028 typebuf.tb_buflen = newlen;
1029
Bram Moolenaar30613902019-12-01 22:11:18 +01001030 // copy the old chars, before the insertion point
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 mch_memmove(s1 + newoff, typebuf.tb_buf + typebuf.tb_off,
1032 (size_t)offset);
Bram Moolenaar30613902019-12-01 22:11:18 +01001033 // copy the new chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 mch_memmove(s1 + newoff + offset, str, (size_t)addlen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001035 // copy the old chars, after the insertion point, including the NUL at
1036 // the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 mch_memmove(s1 + newoff + offset + addlen,
1038 typebuf.tb_buf + typebuf.tb_off + offset,
1039 (size_t)(typebuf.tb_len - offset + 1));
1040 if (typebuf.tb_buf != typebuf_init)
1041 vim_free(typebuf.tb_buf);
1042 typebuf.tb_buf = s1;
1043
1044 mch_memmove(s2 + newoff, typebuf.tb_noremap + typebuf.tb_off,
1045 (size_t)offset);
1046 mch_memmove(s2 + newoff + offset + addlen,
1047 typebuf.tb_noremap + typebuf.tb_off + offset,
1048 (size_t)(typebuf.tb_len - offset));
1049 if (typebuf.tb_noremap != noremapbuf_init)
1050 vim_free(typebuf.tb_noremap);
1051 typebuf.tb_noremap = s2;
1052
1053 typebuf.tb_off = newoff;
1054 }
1055 typebuf.tb_len += addlen;
1056
Bram Moolenaar30613902019-12-01 22:11:18 +01001057 // If noremap == REMAP_SCRIPT: do remap script-local mappings.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 if (noremap == REMAP_SCRIPT)
1059 val = RM_SCRIPT;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001060 else if (noremap == REMAP_SKIP)
1061 val = RM_ABBR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 else
1063 val = RM_NONE;
1064
1065 /*
1066 * Adjust typebuf.tb_noremap[] for the new characters:
1067 * If noremap == REMAP_NONE or REMAP_SCRIPT: new characters are
1068 * (sometimes) not remappable
1069 * If noremap == REMAP_YES: all the new characters are mappable
1070 * If noremap > 0: "noremap" characters are not remappable, the rest
1071 * mappable
1072 */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001073 if (noremap == REMAP_SKIP)
1074 nrm = 1;
1075 else if (noremap < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 nrm = addlen;
1077 else
1078 nrm = noremap;
1079 for (i = 0; i < addlen; ++i)
1080 typebuf.tb_noremap[typebuf.tb_off + i + offset] =
1081 (--nrm >= 0) ? val : RM_YES;
1082
Bram Moolenaar30613902019-12-01 22:11:18 +01001083 // tb_maplen and tb_silent only remember the length of mapped and/or
1084 // silent mappings at the start of the buffer, assuming that a mapped
1085 // sequence doesn't result in typed characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 if (nottyped || typebuf.tb_maplen > offset)
1087 typebuf.tb_maplen += addlen;
1088 if (silent || typebuf.tb_silent > offset)
1089 {
1090 typebuf.tb_silent += addlen;
1091 cmd_silent = TRUE;
1092 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001093 if (typebuf.tb_no_abbr_cnt && offset == 0) // and not used for abbrev.s
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 typebuf.tb_no_abbr_cnt += addlen;
1095
1096 return OK;
1097}
1098
1099/*
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001100 * Put character "c" back into the typeahead buffer.
1101 * Can be used for a character obtained by vgetc() that needs to be put back.
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00001102 * Uses cmd_silent, KeyTyped and KeyNoremap to restore the flags belonging to
1103 * the char.
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001104 */
1105 void
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001106ins_char_typebuf(int c, int modifier)
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001107{
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001108 char_u buf[MB_MAXBYTES + 4];
1109 int idx = 0;
1110
1111 if (modifier != 0)
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001112 {
1113 buf[0] = K_SPECIAL;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001114 buf[1] = KS_MODIFIER;
1115 buf[2] = modifier;
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001116 buf[3] = NUL;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001117 idx = 3;
1118 }
1119 if (IS_SPECIAL(c))
1120 {
1121 buf[idx] = K_SPECIAL;
1122 buf[idx + 1] = K_SECOND(c);
1123 buf[idx + 2] = K_THIRD(c);
1124 buf[idx + 3] = NUL;
1125 idx += 3;
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001126 }
1127 else
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001128 buf[(*mb_char2bytes)(c, buf + idx) + idx] = NUL;
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00001129 (void)ins_typebuf(buf, KeyNoremap, 0, !KeyTyped, cmd_silent);
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001130}
1131
1132/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 * Return TRUE if the typeahead buffer was changed (while waiting for a
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001134 * character to arrive). Happens when a message was received from a client or
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001135 * from feedkeys().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 * But check in a more generic way to avoid trouble: When "typebuf.tb_buf"
1137 * changed it was reallocated and the old pointer can no longer be used.
1138 * Or "typebuf.tb_off" may have been changed and we would overwrite characters
1139 * that was just added.
1140 */
1141 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001142typebuf_changed(
Bram Moolenaar30613902019-12-01 22:11:18 +01001143 int tb_change_cnt) // old value of typebuf.tb_change_cnt
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144{
1145 return (tb_change_cnt != 0 && (typebuf.tb_change_cnt != tb_change_cnt
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001146#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
1147 || typebuf_was_filled
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148#endif
1149 ));
1150}
1151
1152/*
1153 * Return TRUE if there are no characters in the typeahead buffer that have
1154 * not been typed (result from a mapping or come from ":normal").
1155 */
1156 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001157typebuf_typed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158{
1159 return typebuf.tb_maplen == 0;
1160}
1161
1162/*
1163 * Return the number of characters that are mapped (or not typed).
1164 */
1165 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001166typebuf_maplen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167{
1168 return typebuf.tb_maplen;
1169}
1170
1171/*
1172 * remove "len" characters from typebuf.tb_buf[typebuf.tb_off + offset]
1173 */
1174 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001175del_typebuf(int len, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176{
1177 int i;
1178
1179 if (len == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01001180 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181
1182 typebuf.tb_len -= len;
1183
1184 /*
1185 * Easy case: Just increase typebuf.tb_off.
1186 */
1187 if (offset == 0 && typebuf.tb_buflen - (typebuf.tb_off + len)
1188 >= 3 * MAXMAPLEN + 3)
1189 typebuf.tb_off += len;
1190 /*
1191 * Have to move the characters in typebuf.tb_buf[] and typebuf.tb_noremap[]
1192 */
1193 else
1194 {
1195 i = typebuf.tb_off + offset;
1196 /*
1197 * Leave some extra room at the end to avoid reallocation.
1198 */
1199 if (typebuf.tb_off > MAXMAPLEN)
1200 {
1201 mch_memmove(typebuf.tb_buf + MAXMAPLEN,
1202 typebuf.tb_buf + typebuf.tb_off, (size_t)offset);
1203 mch_memmove(typebuf.tb_noremap + MAXMAPLEN,
1204 typebuf.tb_noremap + typebuf.tb_off, (size_t)offset);
1205 typebuf.tb_off = MAXMAPLEN;
1206 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001207 // adjust typebuf.tb_buf (include the NUL at the end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset,
1209 typebuf.tb_buf + i + len,
1210 (size_t)(typebuf.tb_len - offset + 1));
Bram Moolenaar30613902019-12-01 22:11:18 +01001211 // adjust typebuf.tb_noremap[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 mch_memmove(typebuf.tb_noremap + typebuf.tb_off + offset,
1213 typebuf.tb_noremap + i + len,
1214 (size_t)(typebuf.tb_len - offset));
1215 }
1216
Bram Moolenaar30613902019-12-01 22:11:18 +01001217 if (typebuf.tb_maplen > offset) // adjust tb_maplen
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 {
1219 if (typebuf.tb_maplen < offset + len)
1220 typebuf.tb_maplen = offset;
1221 else
1222 typebuf.tb_maplen -= len;
1223 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001224 if (typebuf.tb_silent > offset) // adjust tb_silent
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 {
1226 if (typebuf.tb_silent < offset + len)
1227 typebuf.tb_silent = offset;
1228 else
1229 typebuf.tb_silent -= len;
1230 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001231 if (typebuf.tb_no_abbr_cnt > offset) // adjust tb_no_abbr_cnt
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 {
1233 if (typebuf.tb_no_abbr_cnt < offset + len)
1234 typebuf.tb_no_abbr_cnt = offset;
1235 else
1236 typebuf.tb_no_abbr_cnt -= len;
1237 }
1238
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001239#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar30613902019-12-01 22:11:18 +01001240 // Reset the flag that text received from a client or from feedkeys()
1241 // was inserted in the typeahead buffer.
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001242 typebuf_was_filled = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243#endif
1244 if (++typebuf.tb_change_cnt == 0)
1245 typebuf.tb_change_cnt = 1;
1246}
1247
1248/*
1249 * Write typed characters to script file.
1250 * If recording is on put the character in the recordbuffer.
1251 */
1252 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001253gotchars(char_u *chars, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254{
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001255 char_u *s = chars;
1256 int i;
1257 static char_u buf[4];
1258 static int buflen = 0;
1259 int todo = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260
Bram Moolenaar4e86cba2007-05-06 11:56:32 +00001261 while (todo--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 {
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001263 buf[buflen++] = *s++;
1264
1265 // When receiving a special key sequence, store it until we have all
1266 // the bytes and we can decide what to do with it.
1267 if (buflen == 1 && buf[0] == K_SPECIAL)
1268 continue;
1269 if (buflen == 2)
1270 continue;
1271 if (buflen == 3 && buf[1] == KS_EXTRA
1272 && (buf[2] == KE_FOCUSGAINED || buf[2] == KE_FOCUSLOST))
1273 {
1274 // Drop K_FOCUSGAINED and K_FOCUSLOST, they are not useful in a
1275 // recording.
1276 buflen = 0;
1277 continue;
1278 }
1279
Bram Moolenaar30613902019-12-01 22:11:18 +01001280 // Handle one byte at a time; no translation to be done.
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001281 for (i = 0; i < buflen; ++i)
1282 updatescript(buf[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001284 if (reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 {
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001286 buf[buflen] = NUL;
1287 add_buff(&recordbuff, buf, (long)buflen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001288 // remember how many chars were last recorded
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001289 last_recorded_len += buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 }
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001291 buflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 }
1293 may_sync_undo();
1294
1295#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01001296 // output "debug mode" message next time in debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 debug_did_msg = FALSE;
1298#endif
1299
Bram Moolenaar30613902019-12-01 22:11:18 +01001300 // Since characters have been typed, consider the following to be in
1301 // another mapping. Search string will be kept in history.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 ++maptick;
1303}
1304
1305/*
1306 * Sync undo. Called when typed characters are obtained from the typeahead
1307 * buffer, or when a menu is used.
1308 * Do not sync:
1309 * - In Insert mode, unless cursor key has been used.
1310 * - While reading a script file.
1311 * - When no_u_sync is non-zero.
1312 */
1313 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001314may_sync_undo(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315{
1316 if ((!(State & (INSERT + CMDLINE)) || arrow_used)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001317 && scriptin[curscript] == NULL)
1318 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319}
1320
1321/*
1322 * Make "typebuf" empty and allocate new buffers.
1323 * Returns FAIL when out of memory.
1324 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001325 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001326alloc_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327{
1328 typebuf.tb_buf = alloc(TYPELEN_INIT);
1329 typebuf.tb_noremap = alloc(TYPELEN_INIT);
1330 if (typebuf.tb_buf == NULL || typebuf.tb_noremap == NULL)
1331 {
1332 free_typebuf();
1333 return FAIL;
1334 }
1335 typebuf.tb_buflen = TYPELEN_INIT;
Bram Moolenaar30613902019-12-01 22:11:18 +01001336 typebuf.tb_off = MAXMAPLEN + 4; // can insert without realloc
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 typebuf.tb_len = 0;
1338 typebuf.tb_maplen = 0;
1339 typebuf.tb_silent = 0;
1340 typebuf.tb_no_abbr_cnt = 0;
1341 if (++typebuf.tb_change_cnt == 0)
1342 typebuf.tb_change_cnt = 1;
Bram Moolenaare49b4bb2020-03-11 13:01:40 +01001343#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
1344 typebuf_was_filled = FALSE;
1345#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 return OK;
1347}
1348
1349/*
1350 * Free the buffers of "typebuf".
1351 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001352 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001353free_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354{
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001355 if (typebuf.tb_buf == typebuf_init)
Bram Moolenaar95f09602016-11-10 20:01:45 +01001356 internal_error("Free typebuf 1");
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001357 else
Bram Moolenaar0f1c6702019-09-28 15:24:00 +02001358 VIM_CLEAR(typebuf.tb_buf);
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00001359 if (typebuf.tb_noremap == noremapbuf_init)
Bram Moolenaar95f09602016-11-10 20:01:45 +01001360 internal_error("Free typebuf 2");
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001361 else
Bram Moolenaar0f1c6702019-09-28 15:24:00 +02001362 VIM_CLEAR(typebuf.tb_noremap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363}
1364
1365/*
1366 * When doing ":so! file", the current typeahead needs to be saved, and
1367 * restored when "file" has been read completely.
1368 */
1369static typebuf_T saved_typebuf[NSCRIPT];
1370
1371 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001372save_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373{
1374 init_typebuf();
1375 saved_typebuf[curscript] = typebuf;
Bram Moolenaar30613902019-12-01 22:11:18 +01001376 // If out of memory: restore typebuf and close file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 if (alloc_typebuf() == FAIL)
1378 {
1379 closescript();
1380 return FAIL;
1381 }
1382 return OK;
1383}
1384
Bram Moolenaar30613902019-12-01 22:11:18 +01001385static int old_char = -1; // character put back by vungetc()
1386static int old_mod_mask; // mod_mask for ungotten character
1387static int old_mouse_row; // mouse_row related to old_char
1388static int old_mouse_col; // mouse_col related to old_char
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001389
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390/*
1391 * Save all three kinds of typeahead, so that the user must type at a prompt.
1392 */
1393 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001394save_typeahead(tasave_T *tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395{
1396 tp->save_typebuf = typebuf;
1397 tp->typebuf_valid = (alloc_typebuf() == OK);
1398 if (!tp->typebuf_valid)
1399 typebuf = tp->save_typebuf;
1400
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001401 tp->old_char = old_char;
1402 tp->old_mod_mask = old_mod_mask;
1403 old_char = -1;
1404
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001405 tp->save_readbuf1 = readbuf1;
Bram Moolenaar285e3352018-04-18 23:01:13 +02001406 readbuf1.bh_first.b_next = NULL;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001407 tp->save_readbuf2 = readbuf2;
Bram Moolenaar285e3352018-04-18 23:01:13 +02001408 readbuf2.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409# ifdef USE_INPUT_BUF
1410 tp->save_inputbuf = get_input_buf();
1411# endif
1412}
1413
1414/*
1415 * Restore the typeahead to what it was before calling save_typeahead().
1416 * The allocated memory is freed, can only be called once!
Bram Moolenaarc41badb2021-06-07 22:04:52 +02001417 * When "overwrite" is FALSE input typed later is kept.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 */
1419 void
Bram Moolenaarc41badb2021-06-07 22:04:52 +02001420restore_typeahead(tasave_T *tp, int overwrite UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421{
1422 if (tp->typebuf_valid)
1423 {
1424 free_typebuf();
1425 typebuf = tp->save_typebuf;
1426 }
1427
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001428 old_char = tp->old_char;
1429 old_mod_mask = tp->old_mod_mask;
1430
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001431 free_buff(&readbuf1);
1432 readbuf1 = tp->save_readbuf1;
1433 free_buff(&readbuf2);
1434 readbuf2 = tp->save_readbuf2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435# ifdef USE_INPUT_BUF
Bram Moolenaarc41badb2021-06-07 22:04:52 +02001436 set_input_buf(tp->save_inputbuf, overwrite);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437# endif
1438}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439
1440/*
1441 * Open a new script file for the ":source!" command.
1442 */
1443 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001444openscript(
1445 char_u *name,
Bram Moolenaar30613902019-12-01 22:11:18 +01001446 int directly) // when TRUE execute directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447{
1448 if (curscript + 1 == NSCRIPT)
1449 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001450 emsg(_(e_scripts_nested_too_deep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 return;
1452 }
Bram Moolenaar53575522019-05-22 22:38:25 +02001453
1454 // Disallow sourcing a file in the sandbox, the commands would be executed
1455 // later, possibly outside of the sandbox.
1456 if (check_secure())
1457 return;
1458
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001459#ifdef FEAT_EVAL
1460 if (ignore_script)
Bram Moolenaar30613902019-12-01 22:11:18 +01001461 // Not reading from script, also don't open one. Warning message?
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001462 return;
1463#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464
Bram Moolenaar30613902019-12-01 22:11:18 +01001465 if (scriptin[curscript] != NULL) // already reading script
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 ++curscript;
Bram Moolenaar30613902019-12-01 22:11:18 +01001467 // use NameBuff for expanded name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 expand_env(name, NameBuff, MAXPATHL);
1469 if ((scriptin[curscript] = mch_fopen((char *)NameBuff, READBIN)) == NULL)
1470 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001471 semsg(_(e_notopen), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 if (curscript)
1473 --curscript;
1474 return;
1475 }
1476 if (save_typebuf() == FAIL)
1477 return;
1478
1479 /*
1480 * Execute the commands from the file right now when using ":source!"
1481 * after ":global" or ":argdo" or in a loop. Also when another command
1482 * follows. This means the display won't be updated. Don't do this
1483 * always, "make test" would fail.
1484 */
1485 if (directly)
1486 {
1487 oparg_T oa;
1488 int oldcurscript;
1489 int save_State = State;
1490 int save_restart_edit = restart_edit;
1491 int save_insertmode = p_im;
1492 int save_finish_op = finish_op;
1493 int save_msg_scroll = msg_scroll;
1494
1495 State = NORMAL;
Bram Moolenaar30613902019-12-01 22:11:18 +01001496 msg_scroll = FALSE; // no msg scrolling in Normal mode
1497 restart_edit = 0; // don't go to Insert mode
1498 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 clear_oparg(&oa);
1500 finish_op = FALSE;
1501
1502 oldcurscript = curscript;
1503 do
1504 {
Bram Moolenaar386b43e2019-05-19 21:57:11 +02001505 update_topline_cursor(); // update cursor position and topline
1506 normal_cmd(&oa, FALSE); // execute one command
Bram Moolenaarae97b942020-07-09 19:16:35 +02001507 (void)vpeekc(); // check for end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 }
1509 while (scriptin[oldcurscript] != NULL);
1510
1511 State = save_State;
1512 msg_scroll = save_msg_scroll;
1513 restart_edit = save_restart_edit;
1514 p_im = save_insertmode;
1515 finish_op = save_finish_op;
1516 }
1517}
1518
1519/*
1520 * Close the currently active input script.
1521 */
1522 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001523closescript(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524{
1525 free_typebuf();
1526 typebuf = saved_typebuf[curscript];
1527
1528 fclose(scriptin[curscript]);
1529 scriptin[curscript] = NULL;
1530 if (curscript > 0)
1531 --curscript;
1532}
1533
Bram Moolenaarea408852005-06-25 22:49:46 +00001534#if defined(EXITFREE) || defined(PROTO)
1535 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001536close_all_scripts(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00001537{
1538 while (scriptin[0] != NULL)
1539 closescript();
1540}
1541#endif
1542
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543/*
1544 * Return TRUE when reading keys from a script file.
1545 */
1546 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001547using_script(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548{
1549 return scriptin[curscript] != NULL;
1550}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551
1552/*
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001553 * This function is called just before doing a blocking wait. Thus after
1554 * waiting 'updatetime' for a character to arrive.
1555 */
1556 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001557before_blocking(void)
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001558{
1559 updatescript(0);
1560#ifdef FEAT_EVAL
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001561 if (may_garbage_collect)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001562 garbage_collect(FALSE);
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001563#endif
1564}
1565
1566/*
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001567 * updatescript() is called when a character can be written into the script file
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 * or when we have waited some time for a character (c == 0)
1569 *
1570 * All the changed memfiles are synced if c == 0 or when the number of typed
1571 * characters reaches 'updatecount' and 'updatecount' is non-zero.
1572 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001573 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001574updatescript(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575{
1576 static int count = 0;
1577
1578 if (c && scriptout)
1579 putc(c, scriptout);
1580 if (c == 0 || (p_uc > 0 && ++count >= p_uc))
1581 {
1582 ml_sync_all(c == 0, TRUE);
1583 count = 0;
1584 }
1585}
1586
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587/*
Bram Moolenaar975a8802020-06-06 22:36:24 +02001588 * Convert "c" plus "modifiers" to merge the effect of modifyOtherKeys into the
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001589 * character.
1590 */
1591 int
Bram Moolenaar975a8802020-06-06 22:36:24 +02001592merge_modifyOtherKeys(int c_arg, int *modifiers)
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001593{
1594 int c = c_arg;
1595
Bram Moolenaar975a8802020-06-06 22:36:24 +02001596 if (*modifiers & MOD_MASK_CTRL)
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001597 {
1598 if ((c >= '`' && c <= 0x7f) || (c >= '@' && c <= '_'))
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001599 c &= 0x1f;
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001600 else if (c == '6')
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001601 // CTRL-6 is equivalent to CTRL-^
1602 c = 0x1e;
Bram Moolenaarf4ae6b22020-05-30 19:52:46 +02001603#ifdef FEAT_GUI_GTK
1604 // These mappings look arbitrary at the first glance, but in fact
1605 // resemble quite exactly the behaviour of the GTK+ 1.2 GUI on my
1606 // machine. The only difference is BS vs. DEL for CTRL-8 (makes
1607 // more sense and is consistent with usual terminal behaviour).
1608 else if (c == '2')
1609 c = NUL;
1610 else if (c >= '3' && c <= '7')
1611 c = c ^ 0x28;
1612 else if (c == '8')
1613 c = BS;
1614 else if (c == '?')
1615 c = DEL;
1616#endif
1617 if (c != c_arg)
Bram Moolenaar975a8802020-06-06 22:36:24 +02001618 *modifiers &= ~MOD_MASK_CTRL;
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001619 }
Bram Moolenaar975a8802020-06-06 22:36:24 +02001620 if ((*modifiers & (MOD_MASK_META | MOD_MASK_ALT))
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001621 && c >= 0 && c <= 127)
1622 {
1623 c += 0x80;
Bram Moolenaar975a8802020-06-06 22:36:24 +02001624 *modifiers &= ~(MOD_MASK_META|MOD_MASK_ALT);
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001625 }
1626 return c;
1627}
1628
1629/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 * Get the next input character.
1631 * Can return a special key or a multi-byte character.
1632 * Can return NUL when called recursively, use safe_vgetc() if that's not
1633 * wanted.
1634 * This translates escaped K_SPECIAL and CSI bytes to a K_SPECIAL or CSI byte.
1635 * Collects the bytes of a multibyte character into the whole character.
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00001636 * Returns the modifiers in the global "mod_mask".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 */
1638 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001639vgetc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640{
1641 int c, c2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001643 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001646#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01001647 // Do garbage collection when garbagecollect() was called previously and
1648 // we are now at the toplevel.
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001649 if (may_garbage_collect && want_garbage_collect)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001650 garbage_collect(FALSE);
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001651#endif
1652
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 /*
1654 * If a character was put back with vungetc, it was already processed.
1655 * Return it directly.
1656 */
1657 if (old_char != -1)
1658 {
1659 c = old_char;
1660 old_char = -1;
1661 mod_mask = old_mod_mask;
Bram Moolenaarb8978712013-03-16 21:42:16 +01001662 mouse_row = old_mouse_row;
1663 mouse_col = old_mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001665 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 {
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001667 mod_mask = 0;
1668 vgetc_mod_mask = 0;
1669 vgetc_char = 0;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001670 last_recorded_len = 0;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001671
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001672 for (;;) // this is done twice if there are modifiers
1673 {
1674 int did_inc = FALSE;
Bram Moolenaar2455c4e2015-09-15 18:29:39 +02001675
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001676 if (mod_mask
Bram Moolenaar39719052017-09-05 22:20:46 +02001677#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001678 || im_is_preediting()
Bram Moolenaar39719052017-09-05 22:20:46 +02001679#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001680#if defined(FEAT_PROP_POPUP)
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001681 || popup_no_mapping()
1682#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001683 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 {
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001685 // no mapping after modifier has been read
1686 ++no_mapping;
1687 ++allow_keys;
1688 did_inc = TRUE; // mod_mask may change value
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 }
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001690 c = vgetorpeek(TRUE);
1691 if (did_inc)
1692 {
1693 --no_mapping;
1694 --allow_keys;
1695 }
1696
1697 // Get two extra bytes for special keys
1698 if (c == K_SPECIAL
1699#ifdef FEAT_GUI
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001700 || (c == CSI)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001701#endif
1702 )
1703 {
1704 int save_allow_keys = allow_keys;
1705
1706 ++no_mapping;
1707 allow_keys = 0; // make sure BS is not found
1708 c2 = vgetorpeek(TRUE); // no mapping for these chars
1709 c = vgetorpeek(TRUE);
1710 --no_mapping;
1711 allow_keys = save_allow_keys;
1712 if (c2 == KS_MODIFIER)
1713 {
1714 mod_mask = c;
1715 continue;
1716 }
1717 c = TO_SPECIAL(c2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718
Bram Moolenaar4f974752019-02-17 17:44:42 +01001719#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001720 // Handle K_TEAROFF here, the caller of vgetc() doesn't need to
1721 // know that a menu was torn off
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001722 if (
1723# ifdef VIMDLL
1724 gui.in_use &&
1725# endif
1726 c == K_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 {
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001728 char_u name[200];
1729 int i;
1730
1731 // get menu path, it ends with a <CR>
1732 for (i = 0; (c = vgetorpeek(TRUE)) != '\r'; )
1733 {
1734 name[i] = c;
1735 if (i < 199)
1736 ++i;
1737 }
1738 name[i] = NUL;
1739 gui_make_tearoff(name);
1740 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742#endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001743#if defined(FEAT_GUI) && defined(FEAT_GUI_GTK) && defined(FEAT_MENU)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001744 // GTK: <F10> normally selects the menu, but it's passed until
1745 // here to allow mapping it. Intercept and invoke the GTK
1746 // behavior if it's not mapped.
1747 if (c == K_F10 && gui.menubar != NULL)
1748 {
1749 gtk_menu_shell_select_first(
1750 GTK_MENU_SHELL(gui.menubar), FALSE);
1751 continue;
1752 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001753#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754#ifdef FEAT_GUI
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001755 // Handle focus event here, so that the caller doesn't need to
1756 // know about it. Return K_IGNORE so that we loop once (needed
1757 // if 'lazyredraw' is set).
1758 if (c == K_FOCUSGAINED || c == K_FOCUSLOST)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001759 {
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001760 ui_focus_change(c == K_FOCUSGAINED);
1761 c = K_IGNORE;
Bram Moolenaara9dd2d32019-04-29 21:58:41 +02001762 }
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001763
1764 // Translate K_CSI to CSI. The special key is only used to
1765 // avoid it being recognized as the start of a special key.
1766 if (c == K_CSI)
1767 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001769 }
1770 // a keypad or special function key was not mapped, use it like
1771 // its ASCII equivalent
1772 switch (c)
1773 {
1774 case K_KPLUS: c = '+'; break;
1775 case K_KMINUS: c = '-'; break;
1776 case K_KDIVIDE: c = '/'; break;
1777 case K_KMULTIPLY: c = '*'; break;
1778 case K_KENTER: c = CAR; break;
1779 case K_KPOINT:
Bram Moolenaar4f974752019-02-17 17:44:42 +01001780#ifdef MSWIN
Bram Moolenaar8e85db02018-07-27 23:16:51 +02001781 // Can be either '.' or a ',',
1782 // depending on the type of keypad.
1783 c = MapVirtualKey(VK_DECIMAL, 2); break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001784#else
Bram Moolenaar8e85db02018-07-27 23:16:51 +02001785 c = '.'; break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001786#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001787 case K_K0: c = '0'; break;
1788 case K_K1: c = '1'; break;
1789 case K_K2: c = '2'; break;
1790 case K_K3: c = '3'; break;
1791 case K_K4: c = '4'; break;
1792 case K_K5: c = '5'; break;
1793 case K_K6: c = '6'; break;
1794 case K_K7: c = '7'; break;
1795 case K_K8: c = '8'; break;
1796 case K_K9: c = '9'; break;
Bram Moolenaara88d9682005-03-25 21:45:43 +00001797
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001798 case K_XHOME:
1799 case K_ZHOME: if (mod_mask == MOD_MASK_SHIFT)
Bram Moolenaara88d9682005-03-25 21:45:43 +00001800 {
1801 c = K_S_HOME;
1802 mod_mask = 0;
1803 }
1804 else if (mod_mask == MOD_MASK_CTRL)
1805 {
1806 c = K_C_HOME;
1807 mod_mask = 0;
1808 }
1809 else
1810 c = K_HOME;
1811 break;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001812 case K_XEND:
1813 case K_ZEND: if (mod_mask == MOD_MASK_SHIFT)
Bram Moolenaara88d9682005-03-25 21:45:43 +00001814 {
1815 c = K_S_END;
1816 mod_mask = 0;
1817 }
1818 else if (mod_mask == MOD_MASK_CTRL)
1819 {
1820 c = K_C_END;
1821 mod_mask = 0;
1822 }
1823 else
1824 c = K_END;
1825 break;
1826
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001827 case K_XUP: c = K_UP; break;
1828 case K_XDOWN: c = K_DOWN; break;
1829 case K_XLEFT: c = K_LEFT; break;
1830 case K_XRIGHT: c = K_RIGHT; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001833 // For a multi-byte character get all the bytes and return the
1834 // converted character.
1835 // Note: This will loop until enough bytes are received!
1836 if (has_mbyte && (n = MB_BYTE2LEN_CHECK(c)) > 1)
1837 {
1838 ++no_mapping;
1839 buf[0] = c;
1840 for (i = 1; i < n; ++i)
1841 {
1842 buf[i] = vgetorpeek(TRUE);
1843 if (buf[i] == K_SPECIAL
1844#ifdef FEAT_GUI
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001845 || (buf[i] == CSI)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001846#endif
1847 )
1848 {
1849 // Must be a K_SPECIAL - KS_SPECIAL - KE_FILLER
1850 // sequence, which represents a K_SPECIAL (0x80),
1851 // or a CSI - KS_EXTRA - KE_CSI sequence, which
1852 // represents a CSI (0x9B),
1853 // or a K_SPECIAL - KS_EXTRA - KE_CSI, which is CSI
1854 // too.
1855 c = vgetorpeek(TRUE);
1856 if (vgetorpeek(TRUE) == (int)KE_CSI && c == KS_EXTRA)
1857 buf[i] = CSI;
1858 }
1859 }
1860 --no_mapping;
1861 c = (*mb_ptr2char)(buf);
1862 }
1863
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02001864 if (vgetc_char == 0)
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001865 {
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001866 vgetc_mod_mask = mod_mask;
1867 vgetc_char = c;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001868 }
Bram Moolenaar00eab7f2019-10-10 21:49:28 +02001869
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001870 break;
1871 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001873
1874#ifdef FEAT_EVAL
1875 /*
1876 * In the main loop "may_garbage_collect" can be set to do garbage
1877 * collection in the first next vgetc(). It's disabled after that to
1878 * avoid internally used Lists and Dicts to be freed.
1879 */
1880 may_garbage_collect = FALSE;
1881#endif
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001882
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001883#ifdef FEAT_BEVAL_TERM
Bram Moolenaarc2f50542019-07-05 23:24:56 +02001884 if (c != K_MOUSEMOVE && c != K_IGNORE && c != K_CURSORHOLD)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001885 {
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001886 // Don't trigger 'balloonexpr' unless only the mouse was moved.
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001887 bevalexpr_due_set = FALSE;
1888 ui_remove_balloon();
1889 }
1890#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001891#ifdef FEAT_PROP_POPUP
Bram Moolenaar4934ad02020-09-28 22:29:58 +02001892 // Only filter keys that do not come from ":normal". Keys from feedkeys()
1893 // are filtered.
1894 if ((!ex_normal_busy || in_feedkeys) && popup_do_filter(c))
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02001895 {
1896 if (c == Ctrl_C)
1897 got_int = FALSE; // avoid looping
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001898 c = K_IGNORE;
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02001899 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001900#endif
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001901
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001902 // Need to process the character before we know it's safe to do something
1903 // else.
1904 if (c != K_IGNORE)
Bram Moolenaard103ee72019-09-18 21:15:31 +02001905 state_no_longer_safe("key typed");
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001906
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001907 return c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908}
1909
1910/*
1911 * Like vgetc(), but never return a NUL when called recursively, get a key
1912 * directly from the user (ignoring typeahead).
1913 */
1914 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001915safe_vgetc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916{
1917 int c;
1918
1919 c = vgetc();
1920 if (c == NUL)
1921 c = get_keystroke();
1922 return c;
1923}
1924
1925/*
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001926 * Like safe_vgetc(), but loop to handle K_IGNORE.
1927 * Also ignore scrollbar events.
1928 */
1929 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001930plain_vgetc(void)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001931{
1932 int c;
1933
1934 do
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001935 c = safe_vgetc();
Bram Moolenaar3a006592021-05-05 19:58:17 +02001936 while (c == K_IGNORE
1937 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
1938 || c == K_MOUSEMOVE);
Bram Moolenaarec2da362017-01-21 20:04:22 +01001939
1940 if (c == K_PS)
Bram Moolenaar30613902019-12-01 22:11:18 +01001941 // Only handle the first pasted character. Drop the rest, since we
1942 // don't know what to do with it.
Bram Moolenaarec2da362017-01-21 20:04:22 +01001943 c = bracketed_paste(PASTE_ONE_CHAR, FALSE, NULL);
1944
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001945 return c;
1946}
1947
1948/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 * Check if a character is available, such that vgetc() will not block.
1950 * If the next character is a special character or multi-byte, the returned
1951 * character is not valid!.
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02001952 * Returns NUL if no character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 */
1954 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001955vpeekc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956{
1957 if (old_char != -1)
1958 return old_char;
1959 return vgetorpeek(FALSE);
1960}
1961
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01001962#if defined(FEAT_TERMRESPONSE) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963/*
1964 * Like vpeekc(), but don't allow mapping. Do allow checking for terminal
1965 * codes.
1966 */
1967 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001968vpeekc_nomap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969{
1970 int c;
1971
1972 ++no_mapping;
1973 ++allow_keys;
1974 c = vpeekc();
1975 --no_mapping;
1976 --allow_keys;
1977 return c;
1978}
1979#endif
1980
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981/*
1982 * Check if any character is available, also half an escape sequence.
1983 * Trick: when no typeahead found, but there is something in the typeahead
1984 * buffer, it must be an ESC that is recognized as the start of a key code.
1985 */
1986 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001987vpeekc_any(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988{
1989 int c;
1990
1991 c = vpeekc();
1992 if (c == NUL && typebuf.tb_len > 0)
1993 c = ESC;
1994 return c;
1995}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996
1997/*
1998 * Call vpeekc() without causing anything to be mapped.
1999 * Return TRUE if a character is available, FALSE otherwise.
2000 */
2001 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002002char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003{
2004 int retval;
2005
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01002006#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01002007 // When test_override("char_avail", 1) was called pretend there is no
2008 // typeahead.
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01002009 if (disable_char_avail_for_testing)
2010 return FALSE;
2011#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 ++no_mapping;
2013 retval = vpeekc();
2014 --no_mapping;
2015 return (retval != NUL);
2016}
2017
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002018#if defined(FEAT_EVAL) || defined(PROTO)
2019/*
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002020 * "getchar()" and "getcharstr()" functions
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002021 */
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002022 static void
2023getchar_common(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002024{
2025 varnumber_T n;
2026 int error = FALSE;
2027
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002028 if (in_vim9script() && check_for_opt_bool_arg(argvars, 0) == FAIL)
2029 return;
2030
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002031#ifdef MESSAGE_QUEUE
2032 // vpeekc() used to check for messages, but that caused problems, invoking
2033 // a callback where it was not expected. Some plugins use getchar(1) in a
2034 // loop to await a message, therefore make sure we check for messages here.
2035 parse_queued_messages();
2036#endif
2037
Bram Moolenaar30613902019-12-01 22:11:18 +01002038 // Position the cursor. Needed after a message that ends in a space.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002039 windgoto(msg_row, msg_col);
2040
2041 ++no_mapping;
2042 ++allow_keys;
2043 for (;;)
2044 {
2045 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar30613902019-12-01 22:11:18 +01002046 // getchar(): blocking wait.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002047 n = plain_vgetc();
Bram Moolenaarc08cc722020-09-05 17:51:23 +02002048 else if (tv_get_bool_chk(&argvars[0], &error))
Bram Moolenaar30613902019-12-01 22:11:18 +01002049 // getchar(1): only check if char avail
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002050 n = vpeekc_any();
2051 else if (error || vpeekc_any() == NUL)
Bram Moolenaar30613902019-12-01 22:11:18 +01002052 // illegal argument or getchar(0) and no char avail: return zero
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002053 n = 0;
2054 else
Bram Moolenaar15183b42020-09-05 19:59:39 +02002055 // getchar(0) and char avail() != NUL: get a character.
2056 // Note that vpeekc_any() returns K_SPECIAL for K_IGNORE.
2057 n = safe_vgetc();
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002058
Bram Moolenaar15183b42020-09-05 19:59:39 +02002059 if (n == K_IGNORE || n == K_MOUSEMOVE
2060 || n == K_VER_SCROLLBAR || n == K_HOR_SCROLLBAR)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002061 continue;
2062 break;
2063 }
2064 --no_mapping;
2065 --allow_keys;
2066
2067 set_vim_var_nr(VV_MOUSE_WIN, 0);
2068 set_vim_var_nr(VV_MOUSE_WINID, 0);
2069 set_vim_var_nr(VV_MOUSE_LNUM, 0);
2070 set_vim_var_nr(VV_MOUSE_COL, 0);
2071
2072 rettv->vval.v_number = n;
2073 if (IS_SPECIAL(n) || mod_mask != 0)
2074 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002075 char_u temp[10]; // modifier: 3, mbyte-char: 6, NUL: 1
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002076 int i = 0;
2077
Bram Moolenaar30613902019-12-01 22:11:18 +01002078 // Turn a special key into three bytes, plus modifier.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002079 if (mod_mask != 0)
2080 {
2081 temp[i++] = K_SPECIAL;
2082 temp[i++] = KS_MODIFIER;
2083 temp[i++] = mod_mask;
2084 }
2085 if (IS_SPECIAL(n))
2086 {
2087 temp[i++] = K_SPECIAL;
2088 temp[i++] = K_SECOND(n);
2089 temp[i++] = K_THIRD(n);
2090 }
2091 else if (has_mbyte)
2092 i += (*mb_char2bytes)(n, temp + i);
2093 else
2094 temp[i++] = n;
2095 temp[i++] = NUL;
2096 rettv->v_type = VAR_STRING;
2097 rettv->vval.v_string = vim_strsave(temp);
2098
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002099 if (is_mouse_key(n))
2100 {
2101 int row = mouse_row;
2102 int col = mouse_col;
2103 win_T *win;
2104 linenr_T lnum;
2105 win_T *wp;
2106 int winnr = 1;
2107
2108 if (row >= 0 && col >= 0)
2109 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002110 // Find the window at the mouse coordinates and compute the
2111 // text position.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002112 win = mouse_find_win(&row, &col, FIND_POPUP);
2113 if (win == NULL)
2114 return;
2115 (void)mouse_comp_pos(win, &row, &col, &lnum, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002116#ifdef FEAT_PROP_POPUP
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002117 if (WIN_IS_POPUP(win))
2118 winnr = 0;
2119 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002120#endif
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002121 for (wp = firstwin; wp != win && wp != NULL;
2122 wp = wp->w_next)
2123 ++winnr;
2124 set_vim_var_nr(VV_MOUSE_WIN, winnr);
2125 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
2126 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
2127 set_vim_var_nr(VV_MOUSE_COL, col + 1);
2128 }
2129 }
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002130 }
2131}
2132
2133/*
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002134 * "getchar()" function
2135 */
2136 void
2137f_getchar(typval_T *argvars, typval_T *rettv)
2138{
2139 getchar_common(argvars, rettv);
2140}
2141
2142/*
2143 * "getcharstr()" function
2144 */
2145 void
2146f_getcharstr(typval_T *argvars, typval_T *rettv)
2147{
2148 getchar_common(argvars, rettv);
2149
2150 if (rettv->v_type == VAR_NUMBER)
2151 {
2152 char_u temp[7]; // mbyte-char: 6, NUL: 1
2153 varnumber_T n = rettv->vval.v_number;
2154 int i = 0;
2155
2156 if (n != 0)
2157 {
2158 if (has_mbyte)
2159 i += (*mb_char2bytes)(n, temp + i);
2160 else
2161 temp[i++] = n;
2162 }
2163 temp[i++] = NUL;
2164 rettv->v_type = VAR_STRING;
2165 rettv->vval.v_string = vim_strsave(temp);
2166 }
2167}
2168
2169/*
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002170 * "getcharmod()" function
2171 */
2172 void
2173f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
2174{
2175 rettv->vval.v_number = mod_mask;
2176}
2177#endif // FEAT_EVAL
2178
2179#if defined(MESSAGE_QUEUE) || defined(PROTO)
2180# define MAX_REPEAT_PARSE 8
2181
2182/*
2183 * Process messages that have been queued for netbeans or clientserver.
2184 * Also check if any jobs have ended.
2185 * These functions can call arbitrary vimscript and should only be called when
2186 * it is safe to do so.
2187 */
2188 void
2189parse_queued_messages(void)
2190{
Bram Moolenaar1cac7092019-10-22 21:54:31 +02002191 int old_curwin_id;
2192 int old_curbuf_fnum;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002193 int i;
2194 int save_may_garbage_collect = may_garbage_collect;
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002195 static int entered = 0;
Bram Moolenaard103ee72019-09-18 21:15:31 +02002196 int was_safe = get_was_safe_state();
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002197
2198 // Do not handle messages while redrawing, because it may cause buffers to
2199 // change or be wiped while they are being redrawn.
Bram Moolenaar4778b4d2020-11-04 11:03:12 +01002200 // Also bail out when parsing messages was explicitly disabled.
2201 if (updating_screen || dont_parse_messages)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002202 return;
2203
Bram Moolenaar1cac7092019-10-22 21:54:31 +02002204 // If memory allocation fails during startup we'll exit but curbuf or
2205 // curwin could be NULL.
2206 if (curbuf == NULL || curwin == NULL)
2207 return;
2208
2209 old_curbuf_fnum = curbuf->b_fnum;
2210 old_curwin_id = curwin->w_id;
2211
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002212 ++entered;
2213
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002214 // may_garbage_collect is set in main_loop() to do garbage collection when
2215 // blocking to wait on a character. We don't want that while parsing
2216 // messages, a callback may invoke vgetc() while lists and dicts are in use
2217 // in the call stack.
2218 may_garbage_collect = FALSE;
2219
2220 // Loop when a job ended, but don't keep looping forever.
2221 for (i = 0; i < MAX_REPEAT_PARSE; ++i)
2222 {
2223 // For Win32 mch_breakcheck() does not check for input, do it here.
Bram Moolenaar80a8d382020-05-03 22:57:32 +02002224# if (defined(MSWIN) || defined(__HAIKU__)) && defined(FEAT_JOB_CHANNEL)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002225 channel_handle_events(FALSE);
2226# endif
2227
2228# ifdef FEAT_NETBEANS_INTG
2229 // Process the queued netbeans messages.
2230 netbeans_parse_messages();
2231# endif
2232# ifdef FEAT_JOB_CHANNEL
2233 // Write any buffer lines still to be written.
2234 channel_write_any_lines();
2235
2236 // Process the messages queued on channels.
2237 channel_parse_messages();
2238# endif
2239# if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
2240 // Process the queued clientserver messages.
2241 server_parse_messages();
2242# endif
2243# ifdef FEAT_JOB_CHANNEL
2244 // Check if any jobs have ended. If so, repeat the above to handle
2245 // changes, e.g. stdin may have been closed.
2246 if (job_check_ended())
2247 continue;
2248# endif
2249# ifdef FEAT_TERMINAL
2250 free_unused_terminals();
2251# endif
2252# ifdef FEAT_SOUND_CANBERRA
2253 if (has_sound_callback_in_queue())
2254 invoke_sound_callback();
2255# endif
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002256#ifdef SIGUSR1
2257 if (got_sigusr1)
2258 {
2259 apply_autocmds(EVENT_SIGUSR1, NULL, NULL, FALSE, curbuf);
2260 got_sigusr1 = FALSE;
2261 }
2262#endif
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002263 break;
2264 }
2265
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002266 // When not nested we'll go back to waiting for a typed character. If it
2267 // was safe before then this triggers a SafeStateAgain autocommand event.
Bram Moolenaard103ee72019-09-18 21:15:31 +02002268 if (entered == 1 && was_safe)
Bram Moolenaar37d18072019-09-17 20:28:38 +02002269 may_trigger_safestateagain();
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002270
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002271 may_garbage_collect = save_may_garbage_collect;
2272
2273 // If the current window or buffer changed we need to bail out of the
2274 // waiting loop. E.g. when a job exit callback closes the terminal window.
2275 if (curwin->w_id != old_curwin_id || curbuf->b_fnum != old_curbuf_fnum)
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02002276 ins_char_typebuf(K_IGNORE, 0);
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002277
2278 --entered;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002279}
2280#endif
2281
2282
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002283typedef enum {
2284 map_result_fail, // failed, break loop
2285 map_result_get, // get a character from typeahead
2286 map_result_retry, // try to map again
2287 map_result_nomatch // no matching mapping, get char
2288} map_result_T;
2289
2290/*
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002291 * Check if the bytes at the start of the typeahead buffer are a character used
2292 * in CTRL-X mode. This includes the form with a CTRL modifier.
2293 */
2294 static int
2295at_ctrl_x_key(void)
2296{
2297 char_u *p = typebuf.tb_buf + typebuf.tb_off;
2298 int c = *p;
2299
2300 if (typebuf.tb_len > 3
2301 && c == K_SPECIAL
2302 && p[1] == KS_MODIFIER
2303 && (p[2] & MOD_MASK_CTRL))
2304 c = p[3] & 0x1f;
2305 return vim_is_ctrl_x_key(c);
2306}
2307
2308/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002309 * Check if typebuf.tb_buf[] contains a modifier plus key that can be changed
Bram Moolenaar975a8802020-06-06 22:36:24 +02002310 * into just a key, apply that.
2311 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
2312 * + "max_offset"].
2313 * Return the length of the replaced bytes, zero if nothing changed.
2314 */
2315 static int
2316check_simplify_modifier(int max_offset)
2317{
2318 int offset;
2319 char_u *tp;
2320
2321 for (offset = 0; offset < max_offset; ++offset)
2322 {
2323 if (offset + 3 >= typebuf.tb_len)
2324 break;
2325 tp = typebuf.tb_buf + typebuf.tb_off + offset;
2326 if (tp[0] == K_SPECIAL && tp[1] == KS_MODIFIER)
2327 {
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002328 // A modifier was not used for a mapping, apply it to ASCII keys.
2329 // Shift would already have been applied.
Bram Moolenaar975a8802020-06-06 22:36:24 +02002330 int modifier = tp[2];
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002331 int c = tp[3];
2332 int new_c = merge_modifyOtherKeys(c, &modifier);
Bram Moolenaar975a8802020-06-06 22:36:24 +02002333
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002334 if (new_c != c)
Bram Moolenaar975a8802020-06-06 22:36:24 +02002335 {
2336 char_u new_string[MB_MAXBYTES];
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002337 int len;
Bram Moolenaar975a8802020-06-06 22:36:24 +02002338
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002339 if (offset == 0)
2340 {
2341 // At the start: remember the character and mod_mask before
2342 // merging, in some cases, e.g. at the hit-return prompt,
2343 // they are put back in the typeahead buffer.
2344 vgetc_char = c;
2345 vgetc_mod_mask = tp[2];
2346 }
2347 len = mb_char2bytes(new_c, new_string);
2348 if (modifier == 0)
2349 {
2350 if (put_string_in_typebuf(offset, 4, new_string, len,
Bram Moolenaar975a8802020-06-06 22:36:24 +02002351 NULL, 0, 0) == FAIL)
2352 return -1;
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002353 }
2354 else
2355 {
2356 tp[2] = modifier;
2357 if (put_string_in_typebuf(offset + 3, 1, new_string, len,
2358 NULL, 0, 0) == FAIL)
2359 return -1;
2360 }
Bram Moolenaar975a8802020-06-06 22:36:24 +02002361 return len;
2362 }
2363 }
2364 }
2365 return 0;
2366}
2367
2368/*
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002369 * Handle mappings in the typeahead buffer.
2370 * - When something was mapped, return map_result_retry for recursive mappings.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002371 * - When nothing mapped and typeahead has a character: return map_result_get.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002372 * - When there is no match yet, return map_result_nomatch, need to get more
2373 * typeahead.
2374 */
2375 static int
2376handle_mapping(
2377 int *keylenp,
2378 int *timedout,
2379 int *mapdepth)
2380{
2381 mapblock_T *mp = NULL;
2382 mapblock_T *mp2;
2383 mapblock_T *mp_match;
2384 int mp_match_len = 0;
2385 int max_mlen = 0;
2386 int tb_c1;
2387 int mlen;
2388#ifdef FEAT_LANGMAP
2389 int nolmaplen;
2390#endif
2391 int keylen = *keylenp;
2392 int i;
2393 int local_State = get_real_state();
2394
2395 /*
2396 * Check for a mappable key sequence.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002397 * Walk through one maphash[] list until we find an entry that matches.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002398 *
2399 * Don't look for mappings if:
2400 * - no_mapping set: mapping disabled (e.g. for CTRL-V)
2401 * - maphash_valid not set: no mappings present.
2402 * - typebuf.tb_buf[typebuf.tb_off] should not be remapped
2403 * - in insert or cmdline mode and 'paste' option set
Bram Moolenaareda35f72019-08-03 14:59:44 +02002404 * - waiting for "hit return to continue" and CR or SPACE typed
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002405 * - waiting for a char with --more--
2406 * - in Ctrl-X mode, and we get a valid char for that mode
2407 */
2408 tb_c1 = typebuf.tb_buf[typebuf.tb_off];
2409 if (no_mapping == 0 && is_maphash_valid()
2410 && (no_zero_mapping == 0 || tb_c1 != '0')
2411 && (typebuf.tb_maplen == 0
2412 || (p_remap
2413 && (typebuf.tb_noremap[typebuf.tb_off]
2414 & (RM_NONE|RM_ABBR)) == 0))
2415 && !(p_paste && (State & (INSERT + CMDLINE)))
2416 && !(State == HITRETURN && (tb_c1 == CAR || tb_c1 == ' '))
2417 && State != ASKMORE
2418 && State != CONFIRM
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002419 && !((ctrl_x_mode_not_default() && at_ctrl_x_key())
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002420 || ((compl_cont_status & CONT_LOCAL)
Bram Moolenaare2c453d2019-08-21 14:37:09 +02002421 && (tb_c1 == Ctrl_N || tb_c1 == Ctrl_P))))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002422 {
Bram Moolenaarc9983702020-05-28 21:03:53 +02002423#ifdef FEAT_GUI
2424 if (gui.in_use && tb_c1 == CSI && typebuf.tb_len >= 2
2425 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_MODIFIER)
2426 {
2427 // The GUI code sends CSI KS_MODIFIER {flags}, but mappings expect
2428 // K_SPECIAL KS_MODIFIER {flags}.
2429 tb_c1 = K_SPECIAL;
2430 }
2431#endif
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002432#ifdef FEAT_LANGMAP
2433 if (tb_c1 == K_SPECIAL)
2434 nolmaplen = 2;
2435 else
2436 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002437 LANGMAP_ADJUST(tb_c1, (State & (CMDLINE | INSERT)) == 0
2438 && get_real_state() != SELECTMODE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002439 nolmaplen = 0;
2440 }
2441#endif
2442 // First try buffer-local mappings.
2443 mp = get_buf_maphash_list(local_State, tb_c1);
2444 mp2 = get_maphash_list(local_State, tb_c1);
2445 if (mp == NULL)
2446 {
2447 // There are no buffer-local mappings.
2448 mp = mp2;
2449 mp2 = NULL;
2450 }
Bram Moolenaareda35f72019-08-03 14:59:44 +02002451
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002452 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002453 * Loop until a partly matching mapping is found or all (local)
2454 * mappings have been checked.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002455 * The longest full match is remembered in "mp_match".
Bram Moolenaareda35f72019-08-03 14:59:44 +02002456 * A full match is only accepted if there is no partly match, so "aa"
2457 * and "aaa" can both be mapped.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002458 */
2459 mp_match = NULL;
2460 mp_match_len = 0;
2461 for ( ; mp != NULL;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002462 mp->m_next == NULL ? (mp = mp2, mp2 = NULL) : (mp = mp->m_next))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002463 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002464 // Only consider an entry if the first character matches and it is
2465 // for the current state.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002466 // Skip ":lmap" mappings if keys were mapped.
2467 if (mp->m_keys[0] == tb_c1
2468 && (mp->m_mode & local_State)
Bram Moolenaar46cd43b2020-06-04 22:22:11 +02002469 && !(mp->m_simplified && seenModifyOtherKeys
2470 && typebuf.tb_maplen == 0)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002471 && ((mp->m_mode & LANGMAP) == 0 || typebuf.tb_maplen == 0))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002472 {
2473#ifdef FEAT_LANGMAP
2474 int nomap = nolmaplen;
2475 int c2;
2476#endif
2477 // find the match length of this mapping
2478 for (mlen = 1; mlen < typebuf.tb_len; ++mlen)
2479 {
2480#ifdef FEAT_LANGMAP
2481 c2 = typebuf.tb_buf[typebuf.tb_off + mlen];
2482 if (nomap > 0)
2483 --nomap;
2484 else if (c2 == K_SPECIAL)
2485 nomap = 2;
2486 else
2487 LANGMAP_ADJUST(c2, TRUE);
2488 if (mp->m_keys[mlen] != c2)
2489#else
2490 if (mp->m_keys[mlen] !=
Bram Moolenaarc9983702020-05-28 21:03:53 +02002491 typebuf.tb_buf[typebuf.tb_off + mlen])
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002492#endif
2493 break;
2494 }
2495
Bram Moolenaareda35f72019-08-03 14:59:44 +02002496 // Don't allow mapping the first byte(s) of a multi-byte char.
2497 // Happens when mapping <M-a> and then changing 'encoding'.
2498 // Beware that 0x80 is escaped.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002499 {
2500 char_u *p1 = mp->m_keys;
2501 char_u *p2 = mb_unescape(&p1);
2502
2503 if (has_mbyte && p2 != NULL
Bram Moolenaar1614a142019-10-06 22:00:13 +02002504 && MB_BYTE2LEN(tb_c1) > mb_ptr2len(p2))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002505 mlen = 0;
2506 }
2507
2508 // Check an entry whether it matches.
2509 // - Full match: mlen == keylen
2510 // - Partly match: mlen == typebuf.tb_len
2511 keylen = mp->m_keylen;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002512 if (mlen == keylen || (mlen == typebuf.tb_len
2513 && typebuf.tb_len < keylen))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002514 {
2515 char_u *s;
2516 int n;
2517
Bram Moolenaareda35f72019-08-03 14:59:44 +02002518 // If only script-local mappings are allowed, check if the
2519 // mapping starts with K_SNR.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002520 s = typebuf.tb_noremap + typebuf.tb_off;
2521 if (*s == RM_SCRIPT
2522 && (mp->m_keys[0] != K_SPECIAL
2523 || mp->m_keys[1] != KS_EXTRA
Bram Moolenaareda35f72019-08-03 14:59:44 +02002524 || mp->m_keys[2] != (int)KE_SNR))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002525 continue;
2526
Bram Moolenaareda35f72019-08-03 14:59:44 +02002527 // If one of the typed keys cannot be remapped, skip the
2528 // entry.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002529 for (n = mlen; --n >= 0; )
2530 if (*s++ & (RM_NONE|RM_ABBR))
2531 break;
2532 if (n >= 0)
2533 continue;
2534
2535 if (keylen > typebuf.tb_len)
2536 {
2537 if (!*timedout && !(mp_match != NULL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002538 && mp_match->m_nowait))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002539 {
2540 // break at a partly match
2541 keylen = KEYLEN_PART_MAP;
2542 break;
2543 }
2544 }
2545 else if (keylen > mp_match_len)
2546 {
2547 // found a longer match
2548 mp_match = mp;
2549 mp_match_len = keylen;
2550 }
2551 }
2552 else
Bram Moolenaareda35f72019-08-03 14:59:44 +02002553 // No match; may have to check for termcode at next
2554 // character.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002555 if (max_mlen < mlen)
2556 max_mlen = mlen;
2557 }
2558 }
2559
Bram Moolenaareda35f72019-08-03 14:59:44 +02002560 // If no partly match found, use the longest full match.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002561 if (keylen != KEYLEN_PART_MAP)
2562 {
2563 mp = mp_match;
2564 keylen = mp_match_len;
2565 }
2566 }
2567
2568 /*
2569 * Check for match with 'pastetoggle'
2570 */
2571 if (*p_pt != NUL && mp == NULL && (State & (INSERT|NORMAL)))
2572 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002573 for (mlen = 0; mlen < typebuf.tb_len && p_pt[mlen]; ++mlen)
2574 if (p_pt[mlen] != typebuf.tb_buf[typebuf.tb_off + mlen])
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002575 break;
2576 if (p_pt[mlen] == NUL) // match
2577 {
2578 // write chars to script file(s)
2579 if (mlen > typebuf.tb_maplen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002580 gotchars(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_maplen,
2581 mlen - typebuf.tb_maplen);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002582
2583 del_typebuf(mlen, 0); // remove the chars
Bram Moolenaareda35f72019-08-03 14:59:44 +02002584 set_option_value((char_u *)"paste", (long)!p_paste, NULL, 0);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002585 if (!(State & INSERT))
2586 {
2587 msg_col = 0;
2588 msg_row = Rows - 1;
2589 msg_clr_eos(); // clear ruler
2590 }
2591 status_redraw_all();
2592 redraw_statuslines();
2593 showmode();
2594 setcursor();
2595 *keylenp = keylen;
2596 return map_result_retry;
2597 }
2598 // Need more chars for partly match.
2599 if (mlen == typebuf.tb_len)
2600 keylen = KEYLEN_PART_KEY;
2601 else if (max_mlen < mlen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002602 // no match, may have to check for termcode at next character
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002603 max_mlen = mlen + 1;
2604 }
2605
Bram Moolenaareda35f72019-08-03 14:59:44 +02002606 if ((mp == NULL || max_mlen >= mp_match_len) && keylen != KEYLEN_PART_MAP)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002607 {
2608 int save_keylen = keylen;
2609
2610 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002611 * When no matching mapping found or found a non-matching mapping that
2612 * matches at least what the matching mapping matched:
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002613 * Check if we have a terminal code, when:
Bram Moolenaareda35f72019-08-03 14:59:44 +02002614 * - mapping is allowed,
2615 * - keys have not been mapped,
2616 * - and not an ESC sequence, not in insert mode or p_ek is on,
2617 * - and when not timed out,
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002618 */
2619 if ((no_mapping == 0 || allow_keys != 0)
2620 && (typebuf.tb_maplen == 0
2621 || (p_remap && typebuf.tb_noremap[
Bram Moolenaareda35f72019-08-03 14:59:44 +02002622 typebuf.tb_off] == RM_YES))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002623 && !*timedout)
2624 {
Bram Moolenaar0684e362020-12-03 19:54:42 +01002625 keylen = check_termcode(max_mlen + 1, NULL, 0, NULL);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002626
Bram Moolenaar0684e362020-12-03 19:54:42 +01002627 // If no termcode matched but 'pastetoggle' matched partially
2628 // it's like an incomplete key sequence.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002629 if (keylen == 0 && save_keylen == KEYLEN_PART_KEY)
2630 keylen = KEYLEN_PART_KEY;
2631
Bram Moolenaar975a8802020-06-06 22:36:24 +02002632 // If no termcode matched, try to include the modifier into the
2633 // key. This for when modifyOtherKeys is working.
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002634 if (keylen == 0 && !no_reduce_keys)
Bram Moolenaar975a8802020-06-06 22:36:24 +02002635 keylen = check_simplify_modifier(max_mlen + 1);
2636
Bram Moolenaareda35f72019-08-03 14:59:44 +02002637 // When getting a partial match, but the last characters were not
2638 // typed, don't wait for a typed character to complete the
2639 // termcode. This helps a lot when a ":normal" command ends in an
2640 // ESC.
2641 if (keylen < 0 && typebuf.tb_len == typebuf.tb_maplen)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002642 keylen = 0;
2643 }
2644 else
2645 keylen = 0;
2646 if (keylen == 0) // no matching terminal code
2647 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002648#ifdef AMIGA
2649 // check for window bounds report
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002650 if (typebuf.tb_maplen == 0 && (typebuf.tb_buf[
Bram Moolenaareda35f72019-08-03 14:59:44 +02002651 typebuf.tb_off] & 0xff) == CSI)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002652 {
2653 char_u *s;
2654
2655 for (s = typebuf.tb_buf + typebuf.tb_off + 1;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002656 s < typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len
2657 && (VIM_ISDIGIT(*s) || *s == ';' || *s == ' ');
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002658 ++s)
2659 ;
2660 if (*s == 'r' || *s == '|') // found one
2661 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002662 del_typebuf(
2663 (int)(s + 1 - (typebuf.tb_buf + typebuf.tb_off)), 0);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002664 // get size and redraw screen
2665 shell_resized();
2666 *keylenp = keylen;
2667 return map_result_retry;
2668 }
2669 if (*s == NUL) // need more characters
2670 keylen = KEYLEN_PART_KEY;
2671 }
2672 if (keylen >= 0)
2673#endif
Bram Moolenaareda35f72019-08-03 14:59:44 +02002674 // When there was a matching mapping and no termcode could be
2675 // replaced after another one, use that mapping (loop around).
2676 // If there was no mapping at all use the character from the
2677 // typeahead buffer right here.
2678 if (mp == NULL)
2679 {
2680 *keylenp = keylen;
2681 return map_result_get; // got character, break for loop
2682 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002683 }
2684
2685 if (keylen > 0) // full matching terminal code
2686 {
2687#if defined(FEAT_GUI) && defined(FEAT_MENU)
2688 if (typebuf.tb_len >= 2
Bram Moolenaareda35f72019-08-03 14:59:44 +02002689 && typebuf.tb_buf[typebuf.tb_off] == K_SPECIAL
2690 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_MENU)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002691 {
2692 int idx;
2693
Bram Moolenaareda35f72019-08-03 14:59:44 +02002694 // Using a menu may cause a break in undo! It's like using
2695 // gotchars(), but without recording or writing to a script
2696 // file.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002697 may_sync_undo();
2698 del_typebuf(3, 0);
2699 idx = get_menu_index(current_menu, local_State);
2700 if (idx != MENU_INDEX_INVALID)
2701 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002702 // In Select mode and a Visual mode menu is used: Switch
2703 // to Visual mode temporarily. Append K_SELECT to switch
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002704 // back to Select mode.
2705 if (VIsual_active && VIsual_select
Bram Moolenaareda35f72019-08-03 14:59:44 +02002706 && (current_menu->modes & VISUAL))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002707 {
2708 VIsual_select = FALSE;
2709 (void)ins_typebuf(K_SELECT_STRING,
Bram Moolenaareda35f72019-08-03 14:59:44 +02002710 REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002711 }
2712 ins_typebuf(current_menu->strings[idx],
2713 current_menu->noremap[idx],
Bram Moolenaareda35f72019-08-03 14:59:44 +02002714 0, TRUE, current_menu->silent[idx]);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002715 }
2716 }
2717#endif // FEAT_GUI && FEAT_MENU
2718 *keylenp = keylen;
2719 return map_result_retry; // try mapping again
2720 }
2721
Bram Moolenaareda35f72019-08-03 14:59:44 +02002722 // Partial match: get some more characters. When a matching mapping
2723 // was found use that one.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002724 if (mp == NULL || keylen < 0)
2725 keylen = KEYLEN_PART_KEY;
2726 else
2727 keylen = mp_match_len;
2728 }
2729
2730 /*
2731 * complete match
2732 */
2733 if (keylen >= 0 && keylen <= typebuf.tb_len)
2734 {
2735 char_u *map_str;
2736
2737#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002738 int save_m_expr;
2739 int save_m_noremap;
2740 int save_m_silent;
2741 char_u *save_m_keys;
2742 char_u *save_m_str;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002743#else
2744# define save_m_noremap mp->m_noremap
2745# define save_m_silent mp->m_silent
2746#endif
2747
2748 // write chars to script file(s)
2749 if (keylen > typebuf.tb_maplen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002750 gotchars(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_maplen,
2751 keylen - typebuf.tb_maplen);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002752
2753 cmd_silent = (typebuf.tb_silent > 0);
2754 del_typebuf(keylen, 0); // remove the mapped keys
2755
2756 /*
2757 * Put the replacement string in front of mapstr.
2758 * The depth check catches ":map x y" and ":map y x".
2759 */
2760 if (++*mapdepth >= p_mmd)
2761 {
2762 emsg(_("E223: recursive mapping"));
2763 if (State & CMDLINE)
2764 redrawcmdline();
2765 else
2766 setcursor();
2767 flush_buffers(FLUSH_MINIMAL);
Bram Moolenaar30613902019-12-01 22:11:18 +01002768 *mapdepth = 0; // for next one
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002769 *keylenp = keylen;
2770 return map_result_fail;
2771 }
2772
2773 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002774 * In Select mode and a Visual mode mapping is used: Switch to Visual
2775 * mode temporarily. Append K_SELECT to switch back to Select mode.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002776 */
Bram Moolenaareda35f72019-08-03 14:59:44 +02002777 if (VIsual_active && VIsual_select && (mp->m_mode & VISUAL))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002778 {
2779 VIsual_select = FALSE;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002780 (void)ins_typebuf(K_SELECT_STRING, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002781 }
2782
2783#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002784 // Copy the values from *mp that are used, because evaluating the
2785 // expression may invoke a function that redefines the mapping, thereby
2786 // making *mp invalid.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002787 save_m_expr = mp->m_expr;
2788 save_m_noremap = mp->m_noremap;
2789 save_m_silent = mp->m_silent;
2790 save_m_keys = NULL; // only saved when needed
2791 save_m_str = NULL; // only saved when needed
2792
2793 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002794 * Handle ":map <expr>": evaluate the {rhs} as an expression. Also
2795 * save and restore the command line for "normal :".
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002796 */
2797 if (mp->m_expr)
2798 {
2799 int save_vgetc_busy = vgetc_busy;
2800 int save_may_garbage_collect = may_garbage_collect;
Bram Moolenaar4ebe0e62019-11-22 20:55:40 +01002801 int was_screen_col = screen_cur_col;
2802 int was_screen_row = screen_cur_row;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002803
2804 vgetc_busy = 0;
2805 may_garbage_collect = FALSE;
2806
2807 save_m_keys = vim_strsave(mp->m_keys);
2808 save_m_str = vim_strsave(mp->m_str);
2809 map_str = eval_map_expr(save_m_str, NUL);
2810
Bram Moolenaar4ebe0e62019-11-22 20:55:40 +01002811 // The mapping may do anything, but we expect it to take care of
2812 // redrawing. Do put the cursor back where it was.
2813 windgoto(was_screen_row, was_screen_col);
2814 out_flush();
2815
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002816 vgetc_busy = save_vgetc_busy;
2817 may_garbage_collect = save_may_garbage_collect;
2818 }
2819 else
2820#endif
2821 map_str = mp->m_str;
2822
2823 /*
2824 * Insert the 'to' part in the typebuf.tb_buf.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002825 * If 'from' field is the same as the start of the 'to' field, don't
2826 * remap the first character (but do allow abbreviations).
2827 * If m_noremap is set, don't remap the whole 'to' part.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002828 */
2829 if (map_str == NULL)
2830 i = FAIL;
2831 else
2832 {
2833 int noremap;
2834
2835 if (save_m_noremap != REMAP_YES)
2836 noremap = save_m_noremap;
2837 else if (
2838#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002839 STRNCMP(map_str, save_m_keys != NULL ? save_m_keys : mp->m_keys,
2840 (size_t)keylen)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002841#else
2842 STRNCMP(map_str, mp->m_keys, (size_t)keylen)
2843#endif
2844 != 0)
2845 noremap = REMAP_YES;
2846 else
2847 noremap = REMAP_SKIP;
2848 i = ins_typebuf(map_str, noremap,
2849 0, TRUE, cmd_silent || save_m_silent);
2850#ifdef FEAT_EVAL
2851 if (save_m_expr)
2852 vim_free(map_str);
2853#endif
2854 }
2855#ifdef FEAT_EVAL
2856 vim_free(save_m_keys);
2857 vim_free(save_m_str);
2858#endif
2859 *keylenp = keylen;
2860 if (i == FAIL)
2861 return map_result_fail;
2862 return map_result_retry;
2863 }
2864
2865 *keylenp = keylen;
2866 return map_result_nomatch;
2867}
2868
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002869/*
2870 * unget one character (can only be done once!)
2871 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002873vungetc(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874{
2875 old_char = c;
2876 old_mod_mask = mod_mask;
Bram Moolenaarb8978712013-03-16 21:42:16 +01002877 old_mouse_row = mouse_row;
2878 old_mouse_col = mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879}
2880
2881/*
Bram Moolenaar32526b32019-01-19 17:43:09 +01002882 * Get a byte:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 * 1. from the stuffbuffer
2884 * This is used for abbreviated commands like "D" -> "d$".
2885 * Also used to redo a command for ".".
2886 * 2. from the typeahead buffer
2887 * Stores text obtained previously but not used yet.
2888 * Also stores the result of mappings.
2889 * Also used for the ":normal" command.
2890 * 3. from the user
2891 * This may do a blocking wait if "advance" is TRUE.
2892 *
2893 * if "advance" is TRUE (vgetc()):
Bram Moolenaard29459b2016-08-26 22:29:11 +02002894 * Really get the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 * KeyTyped is set to TRUE in the case the user typed the key.
2896 * KeyStuffed is TRUE if the character comes from the stuff buffer.
2897 * if "advance" is FALSE (vpeekc()):
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02002898 * Just look whether there is a character available.
2899 * Return NUL if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 *
2901 * When "no_mapping" is zero, checks for mappings in the current mode.
2902 * Only returns one byte (of a multi-byte character).
2903 * K_SPECIAL and CSI may be escaped, need to get two more bytes then.
2904 */
2905 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002906vgetorpeek(int advance)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907{
2908 int c, c1;
Bram Moolenaar30613902019-12-01 22:11:18 +01002909 int timedout = FALSE; // waited for more than 1 second
2910 // for mapping to complete
2911 int mapdepth = 0; // check for recursive mapping
2912 int mode_deleted = FALSE; // set when mode has been deleted
Bram Moolenaar4e427192006-03-10 21:34:27 +00002913#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 int new_wcol, new_wrow;
2915#endif
2916#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01002917 int shape_changed = FALSE; // adjusted cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918#endif
2919 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 int old_wcol, old_wrow;
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00002921 int wait_tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922
2923 /*
2924 * This function doesn't work very well when called recursively. This may
2925 * happen though, because of:
2926 * 1. The call to add_to_showcmd(). char_avail() is then used to check if
2927 * there is a character available, which calls this function. In that
2928 * case we must return NUL, to indicate no character is available.
2929 * 2. A GUI callback function writes to the screen, causing a
2930 * wait_return().
2931 * Using ":normal" can also do this, but it saves the typeahead buffer,
2932 * thus it should be OK. But don't get a key from the user then.
2933 */
Bram Moolenaare2c38102016-01-31 14:55:40 +01002934 if (vgetc_busy > 0 && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 return NUL;
2936
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002937 ++vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938
2939 if (advance)
2940 KeyStuffed = FALSE;
2941
2942 init_typebuf();
2943 start_stuff();
2944 if (advance && typebuf.tb_maplen == 0)
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02002945 reg_executing = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 do
2947 {
2948/*
2949 * get a character: 1. from the stuffbuffer
2950 */
2951 if (typeahead_char != 0)
2952 {
2953 c = typeahead_char;
2954 if (advance)
2955 typeahead_char = 0;
2956 }
2957 else
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01002958 c = read_readbuffers(advance);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 if (c != NUL && !got_int)
2960 {
2961 if (advance)
2962 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002963 // KeyTyped = FALSE; When the command that stuffed something
2964 // was typed, behave like the stuffed command was typed.
2965 // needed for CTRL-W CTRL-] to open a fold, for example.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 KeyStuffed = TRUE;
2967 }
2968 if (typebuf.tb_no_abbr_cnt == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01002969 typebuf.tb_no_abbr_cnt = 1; // no abbreviations now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 }
2971 else
2972 {
2973 /*
2974 * Loop until we either find a matching mapped key, or we
2975 * are sure that it is not a mapped key.
2976 * If a mapped key sequence is found we go back to the start to
2977 * try re-mapping.
2978 */
2979 for (;;)
2980 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002981 long wait_time;
2982 int keylen = 0;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002983#ifdef FEAT_CMDL_INFO
2984 int showcmd_idx;
2985#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 /*
2987 * ui_breakcheck() is slow, don't use it too often when
2988 * inside a mapping. But call it each time for typed
2989 * characters.
2990 */
2991 if (typebuf.tb_maplen)
2992 line_breakcheck();
2993 else
Bram Moolenaar30613902019-12-01 22:11:18 +01002994 ui_breakcheck(); // check for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 if (got_int)
2996 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002997 // flush all input
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02002998 c = inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 0L);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002999
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 /*
3001 * If inchar() returns TRUE (script file was active) or we
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003002 * are inside a mapping, get out of Insert mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 * Otherwise we behave like having gotten a CTRL-C.
3004 * As a result typing CTRL-C in insert mode will
3005 * really insert a CTRL-C.
3006 */
3007 if ((c || typebuf.tb_maplen)
3008 && (State & (INSERT + CMDLINE)))
3009 c = ESC;
3010 else
3011 c = Ctrl_C;
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02003012 flush_buffers(FLUSH_INPUT); // flush all typeahead
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013
Bram Moolenaard9dfd572006-10-03 13:36:13 +00003014 if (advance)
3015 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003016 // Also record this character, it might be needed to
3017 // get out of Insert mode.
Bram Moolenaard9dfd572006-10-03 13:36:13 +00003018 *typebuf.tb_buf = c;
3019 gotchars(typebuf.tb_buf, 1);
3020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 cmd_silent = FALSE;
3022
3023 break;
3024 }
3025 else if (typebuf.tb_len > 0)
3026 {
3027 /*
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003028 * Check for a mapping in "typebuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 */
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003030 map_result_T result = handle_mapping(
3031 &keylen, &timedout, &mapdepth);
3032
3033 if (result == map_result_retry)
3034 // try mapping again
3035 continue;
3036 if (result == map_result_fail)
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003037 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003038 // failed, use the outer loop
3039 c = -1;
3040 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003042 if (result == map_result_get)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044/*
3045 * get a character: 2. from the typeahead buffer
3046 */
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003047 c = typebuf.tb_buf[typebuf.tb_off];
Bram Moolenaar30613902019-12-01 22:11:18 +01003048 if (advance) // remove chars from tb_buf
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003049 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003050 cmd_silent = (typebuf.tb_silent > 0);
3051 if (typebuf.tb_maplen > 0)
3052 KeyTyped = FALSE;
3053 else
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003054 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003055 KeyTyped = TRUE;
Bram Moolenaar30613902019-12-01 22:11:18 +01003056 // write char to script file(s)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003057 gotchars(typebuf.tb_buf
3058 + typebuf.tb_off, 1);
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003059 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003060 KeyNoremap = typebuf.tb_noremap[
3061 typebuf.tb_off];
3062 del_typebuf(1, 0);
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003063 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003064 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 }
3066
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003067 // not enough characters, get more
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 }
3069
3070/*
3071 * get a character: 3. from the user - handle <Esc> in Insert mode
3072 */
3073 /*
Bram Moolenaarf085f422017-06-07 20:39:47 +02003074 * Special case: if we get an <ESC> in insert mode and there
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 * are no more characters at once, we pretend to go out of
3076 * insert mode. This prevents the one second delay after
3077 * typing an <ESC>. If we get something after all, we may
3078 * have to redisplay the mode. That the cursor is in the wrong
3079 * place does not matter.
3080 */
3081 c = 0;
3082#ifdef FEAT_CMDL_INFO
3083 new_wcol = curwin->w_wcol;
3084 new_wrow = curwin->w_wrow;
3085#endif
3086 if ( advance
3087 && typebuf.tb_len == 1
3088 && typebuf.tb_buf[typebuf.tb_off] == ESC
3089 && !no_mapping
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 && ex_normal_busy == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 && typebuf.tb_maplen == 0
3092 && (State & INSERT)
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003093 && (p_timeout
3094 || (keylen == KEYLEN_PART_KEY && p_ttimeout))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 && (c = inchar(typebuf.tb_buf + typebuf.tb_off
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003096 + typebuf.tb_len, 3, 25L)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 {
3098 colnr_T col = 0, vcol;
3099 char_u *ptr;
3100
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003101 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 {
3103 unshowmode(TRUE);
3104 mode_deleted = TRUE;
3105 }
3106#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003107 // may show a different cursor shape
Bram Moolenaarf085f422017-06-07 20:39:47 +02003108 if (gui.in_use && State != NORMAL && !cmd_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 {
3110 int save_State;
3111
3112 save_State = State;
3113 State = NORMAL;
3114 gui_update_cursor(TRUE, FALSE);
3115 State = save_State;
3116 shape_changed = TRUE;
3117 }
3118#endif
3119 validate_cursor();
3120 old_wcol = curwin->w_wcol;
3121 old_wrow = curwin->w_wrow;
3122
Bram Moolenaar30613902019-12-01 22:11:18 +01003123 // move cursor left, if possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 if (curwin->w_cursor.col != 0)
3125 {
3126 if (curwin->w_wcol > 0)
3127 {
3128 if (did_ai)
3129 {
3130 /*
3131 * We are expecting to truncate the trailing
3132 * white-space, so find the last non-white
3133 * character -- webb
3134 */
3135 col = vcol = curwin->w_wcol = 0;
3136 ptr = ml_get_curline();
3137 while (col < curwin->w_cursor.col)
3138 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01003139 if (!VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 curwin->w_wcol = vcol;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003141 vcol += lbr_chartabsize(ptr, ptr + col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003144 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 ++col;
3147 }
3148 curwin->w_wrow = curwin->w_cline_row
Bram Moolenaar02631462017-09-22 15:20:32 +02003149 + curwin->w_wcol / curwin->w_width;
3150 curwin->w_wcol %= curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 curwin->w_wcol += curwin_col_off();
Bram Moolenaar30613902019-12-01 22:11:18 +01003152 col = 0; // no correction needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 }
3154 else
3155 {
3156 --curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 col = curwin->w_cursor.col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 }
3159 }
3160 else if (curwin->w_p_wrap && curwin->w_wrow)
3161 {
3162 --curwin->w_wrow;
Bram Moolenaar02631462017-09-22 15:20:32 +02003163 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 col = curwin->w_cursor.col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 if (has_mbyte && col > 0 && curwin->w_wcol > 0)
3167 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003168 // Correct when the cursor is on the right halve
3169 // of a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 ptr = ml_get_curline();
3171 col -= (*mb_head_off)(ptr, ptr + col);
3172 if ((*mb_ptr2cells)(ptr + col) > 1)
3173 --curwin->w_wcol;
3174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 }
3176 setcursor();
3177 out_flush();
3178#ifdef FEAT_CMDL_INFO
3179 new_wcol = curwin->w_wcol;
3180 new_wrow = curwin->w_wrow;
3181#endif
3182 curwin->w_wcol = old_wcol;
3183 curwin->w_wrow = old_wrow;
3184 }
3185 if (c < 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003186 continue; // end of input script reached
Bram Moolenaar20c38922014-07-23 20:41:14 +02003187
Bram Moolenaar30613902019-12-01 22:11:18 +01003188 // Allow mapping for just typed characters. When we get here c
3189 // is the number of extra bytes and typebuf.tb_len is 1.
Bram Moolenaar20c38922014-07-23 20:41:14 +02003190 for (n = 1; n <= c; ++n)
3191 typebuf.tb_noremap[typebuf.tb_off + n] = RM_YES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 typebuf.tb_len += c;
3193
Bram Moolenaar30613902019-12-01 22:11:18 +01003194 // buffer full, don't map
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 if (typebuf.tb_len >= typebuf.tb_maplen + MAXMAPLEN)
3196 {
3197 timedout = TRUE;
3198 continue;
3199 }
3200
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 if (ex_normal_busy > 0)
3202 {
Bram Moolenaare2c38102016-01-31 14:55:40 +01003203#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 static int tc = 0;
Bram Moolenaare2c38102016-01-31 14:55:40 +01003205#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206
Bram Moolenaar30613902019-12-01 22:11:18 +01003207 // No typeahead left and inside ":normal". Must return
3208 // something to avoid getting stuck. When an incomplete
3209 // mapping is present, behave like it timed out.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 if (typebuf.tb_len > 0)
3211 {
3212 timedout = TRUE;
3213 continue;
3214 }
Bram Moolenaar189832b2020-09-23 12:29:11 +02003215
Bram Moolenaar30613902019-12-01 22:11:18 +01003216 // When 'insertmode' is set, ESC just beeps in Insert
3217 // mode. Use CTRL-L to make edit() return.
3218 // For the command line only CTRL-C always breaks it.
3219 // For the cmdline window: Alternate between ESC and
3220 // CTRL-C: ESC for most situations and CTRL-C to close the
3221 // cmdline window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 if (p_im && (State & INSERT))
3223 c = Ctrl_L;
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003224#ifdef FEAT_TERMINAL
3225 else if (terminal_is_active())
3226 c = K_CANCEL;
3227#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 else if ((State & CMDLINE)
Bram Moolenaare2c38102016-01-31 14:55:40 +01003229#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 || (cmdwin_type > 0 && tc == ESC)
Bram Moolenaare2c38102016-01-31 14:55:40 +01003231#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 )
3233 c = Ctrl_C;
3234 else
3235 c = ESC;
Bram Moolenaare2c38102016-01-31 14:55:40 +01003236#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 tc = c;
Bram Moolenaare2c38102016-01-31 14:55:40 +01003238#endif
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01003239 // return from main_loop()
3240 if (pending_exmode_active)
3241 exmode_active = EXMODE_NORMAL;
3242
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 break;
3244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245
3246/*
3247 * get a character: 3. from the user - update display
3248 */
Bram Moolenaar30613902019-12-01 22:11:18 +01003249 // In insert mode a screen update is skipped when characters
3250 // are still available. But when those available characters
3251 // are part of a mapping, and we are going to do a blocking
3252 // wait here. Need to update the screen to display the
3253 // changed text so far. Also for when 'lazyredraw' is set and
3254 // redrawing was postponed because there was something in the
3255 // input buffer (e.g., termresponse).
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01003256 if (((State & INSERT) != 0 || p_lz) && (State & CMDLINE) == 0
3257 && advance && must_redraw != 0 && !need_wait_return)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 {
3259 update_screen(0);
Bram Moolenaar30613902019-12-01 22:11:18 +01003260 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 }
3262
3263 /*
3264 * If we have a partial match (and are going to wait for more
3265 * input from the user), show the partially matched characters
3266 * to the user with showcmd.
3267 */
3268#ifdef FEAT_CMDL_INFO
Bram Moolenaareda35f72019-08-03 14:59:44 +02003269 showcmd_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270#endif
3271 c1 = 0;
3272 if (typebuf.tb_len > 0 && advance && !exmode_active)
3273 {
3274 if (((State & (NORMAL | INSERT)) || State == LANGMAP)
3275 && State != HITRETURN)
3276 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003277 // this looks nice when typing a dead character map
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 if (State & INSERT
3279 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
3280 + typebuf.tb_len - 1) == 1)
3281 {
3282 edit_putchar(typebuf.tb_buf[typebuf.tb_off
3283 + typebuf.tb_len - 1], FALSE);
Bram Moolenaar30613902019-12-01 22:11:18 +01003284 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 c1 = 1;
3286 }
3287#ifdef FEAT_CMDL_INFO
Bram Moolenaar30613902019-12-01 22:11:18 +01003288 // need to use the col and row from above here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 old_wcol = curwin->w_wcol;
3290 old_wrow = curwin->w_wrow;
3291 curwin->w_wcol = new_wcol;
3292 curwin->w_wrow = new_wrow;
3293 push_showcmd();
3294 if (typebuf.tb_len > SHOWCMD_COLS)
Bram Moolenaareda35f72019-08-03 14:59:44 +02003295 showcmd_idx = typebuf.tb_len - SHOWCMD_COLS;
3296 while (showcmd_idx < typebuf.tb_len)
3297 (void)add_to_showcmd(
3298 typebuf.tb_buf[typebuf.tb_off + showcmd_idx++]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 curwin->w_wcol = old_wcol;
3300 curwin->w_wrow = old_wrow;
3301#endif
3302 }
3303
Bram Moolenaar30613902019-12-01 22:11:18 +01003304 // this looks nice when typing a dead character map
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 if ((State & CMDLINE)
3306#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3307 && cmdline_star == 0
3308#endif
3309 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
3310 + typebuf.tb_len - 1) == 1)
3311 {
3312 putcmdline(typebuf.tb_buf[typebuf.tb_off
3313 + typebuf.tb_len - 1], FALSE);
3314 c1 = 1;
3315 }
3316 }
3317
3318/*
3319 * get a character: 3. from the user - get it
3320 */
Bram Moolenaar83f4cbd2018-06-12 21:35:40 +02003321 if (typebuf.tb_len == 0)
3322 // timedout may have been set while waiting for a mapping
3323 // that has a <Nop> RHS.
3324 timedout = FALSE;
3325
Bram Moolenaar652de232019-04-04 20:13:09 +02003326 if (advance)
3327 {
3328 if (typebuf.tb_len == 0
3329 || !(p_timeout
3330 || (p_ttimeout && keylen == KEYLEN_PART_KEY)))
3331 // blocking wait
3332 wait_time = -1L;
3333 else if (keylen == KEYLEN_PART_KEY && p_ttm >= 0)
3334 wait_time = p_ttm;
3335 else
3336 wait_time = p_tm;
3337 }
3338 else
3339 wait_time = 0;
3340
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003341 wait_tb_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 c = inchar(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len,
3343 typebuf.tb_buflen - typebuf.tb_off - typebuf.tb_len - 1,
Bram Moolenaar652de232019-04-04 20:13:09 +02003344 wait_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345
3346#ifdef FEAT_CMDL_INFO
Bram Moolenaareda35f72019-08-03 14:59:44 +02003347 if (showcmd_idx != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 pop_showcmd();
3349#endif
3350 if (c1 == 1)
3351 {
3352 if (State & INSERT)
3353 edit_unputchar();
3354 if (State & CMDLINE)
3355 unputcmdline();
Bram Moolenaarbc256d92012-06-06 12:06:15 +02003356 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003357 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 }
3359
3360 if (c < 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003361 continue; // end of input script reached
3362 if (c == NUL) // no character available
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 {
3364 if (!advance)
3365 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01003366 if (wait_tb_len > 0) // timed out
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 {
3368 timedout = TRUE;
3369 continue;
3370 }
3371 }
3372 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003373 { // allow mapping for just typed characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 while (typebuf.tb_buf[typebuf.tb_off
3375 + typebuf.tb_len] != NUL)
3376 typebuf.tb_noremap[typebuf.tb_off
3377 + typebuf.tb_len++] = RM_YES;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003378#ifdef HAVE_INPUT_METHOD
Bram Moolenaar30613902019-12-01 22:11:18 +01003379 // Get IM status right after getting keys, not after the
3380 // timeout for a mapping (focus may be lost by then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 vgetc_im_active = im_get_status();
3382#endif
3383 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003384 } // for (;;)
3385 } // if (!character from stuffbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386
Bram Moolenaar30613902019-12-01 22:11:18 +01003387 // if advance is FALSE don't loop on NULs
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003388 } while ((c < 0 && c != K_CANCEL) || (advance && c == NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389
3390 /*
3391 * The "INSERT" message is taken care of here:
3392 * if we return an ESC to exit insert mode, the message is deleted
3393 * if we don't return an ESC but deleted the message before, redisplay it
3394 */
Bram Moolenaar09df3122006-01-23 22:23:09 +00003395 if (advance && p_smd && msg_silent == 0 && (State & INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003397 if (c == ESC && !mode_deleted && !no_mapping && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 {
3399 if (typebuf.tb_len && !KeyTyped)
Bram Moolenaar30613902019-12-01 22:11:18 +01003400 redraw_cmdline = TRUE; // delete mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 else
3402 unshowmode(FALSE);
3403 }
3404 else if (c != ESC && mode_deleted)
3405 {
3406 if (typebuf.tb_len && !KeyTyped)
Bram Moolenaar30613902019-12-01 22:11:18 +01003407 redraw_cmdline = TRUE; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 else
3409 showmode();
3410 }
3411 }
3412#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003413 // may unshow different cursor shape
Bram Moolenaarf085f422017-06-07 20:39:47 +02003414 if (gui.in_use && shape_changed)
3415 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416#endif
Bram Moolenaar6edbbd82019-03-10 09:41:51 +01003417 if (timedout && c == ESC)
3418 {
3419 char_u nop_buf[3];
3420
3421 // When recording there will be no timeout. Add a <Nop> after the ESC
3422 // to avoid that it forms a key code with following characters.
3423 nop_buf[0] = K_SPECIAL;
3424 nop_buf[1] = KS_EXTRA;
3425 nop_buf[2] = KE_NOP;
3426 gotchars(nop_buf, 3);
3427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003429 --vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430
3431 return c;
3432}
3433
3434/*
3435 * inchar() - get one character from
3436 * 1. a scriptfile
3437 * 2. the keyboard
3438 *
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003439 * As many characters as we can get (up to 'maxlen') are put in "buf" and
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 * NUL terminated (buffer length must be 'maxlen' + 1).
3441 * Minimum for "maxlen" is 3!!!!
3442 *
3443 * "tb_change_cnt" is the value of typebuf.tb_change_cnt if "buf" points into
3444 * it. When typebuf.tb_change_cnt changes (e.g., when a message is received
3445 * from a remote client) "buf" can no longer be used. "tb_change_cnt" is 0
3446 * otherwise.
3447 *
3448 * If we got an interrupt all input is read until none is available.
3449 *
3450 * If wait_time == 0 there is no waiting for the char.
3451 * If wait_time == n we wait for n msec for a character to arrive.
3452 * If wait_time == -1 we wait forever for a character to arrive.
3453 *
3454 * Return the number of obtained characters.
3455 * Return -1 when end of input script reached.
3456 */
Bram Moolenaarcda77642016-06-04 13:32:35 +02003457 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003458inchar(
3459 char_u *buf,
3460 int maxlen,
Bram Moolenaar30613902019-12-01 22:11:18 +01003461 long wait_time) // milli seconds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462{
Bram Moolenaar30613902019-12-01 22:11:18 +01003463 int len = 0; // init for GCC
3464 int retesc = FALSE; // return ESC with gotint
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 int script_char;
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003466 int tb_change_cnt = typebuf.tb_change_cnt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467
Bram Moolenaar30613902019-12-01 22:11:18 +01003468 if (wait_time == -1L || wait_time > 100L) // flush output before waiting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 {
3470 cursor_on();
Bram Moolenaara338adc2018-01-31 20:51:47 +01003471 out_flush_cursor(FALSE, FALSE);
3472#if defined(FEAT_GUI) && defined(FEAT_MOUSESHAPE)
3473 if (gui.in_use && postponed_mouseshape)
3474 update_mouseshape(-1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475#endif
3476 }
3477
3478 /*
3479 * Don't reset these when at the hit-return prompt, otherwise a endless
3480 * recursive loop may result (write error in swapfile, hit-return, timeout
3481 * on char wait, flush swapfile, write error....).
3482 */
3483 if (State != HITRETURN)
3484 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003485 did_outofmem_msg = FALSE; // display out of memory message (again)
3486 did_swapwrite_msg = FALSE; // display swap file write error again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003488 undo_off = FALSE; // restart undo now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489
3490 /*
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00003491 * Get a character from a script file if there is one.
3492 * If interrupted: Stop reading script files, close them all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 */
3494 script_char = -1;
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00003495 while (scriptin[curscript] != NULL && script_char < 0
3496#ifdef FEAT_EVAL
3497 && !ignore_script
3498#endif
3499 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02003501#ifdef MESSAGE_QUEUE
3502 parse_queued_messages();
Bram Moolenaarf2330482008-06-24 20:19:36 +00003503#endif
3504
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 if (got_int || (script_char = getc(scriptin[curscript])) < 0)
3506 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003507 // Reached EOF.
3508 // Careful: closescript() frees typebuf.tb_buf[] and buf[] may
3509 // point inside typebuf.tb_buf[]. Don't use buf[] after this!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 closescript();
3511 /*
3512 * When reading script file is interrupted, return an ESC to get
3513 * back to normal mode.
3514 * Otherwise return -1, because typebuf.tb_buf[] has changed.
3515 */
3516 if (got_int)
3517 retesc = TRUE;
3518 else
3519 return -1;
3520 }
3521 else
3522 {
3523 buf[0] = script_char;
3524 len = 1;
3525 }
3526 }
3527
Bram Moolenaar30613902019-12-01 22:11:18 +01003528 if (script_char < 0) // did not get a character from script
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 {
3530 /*
3531 * If we got an interrupt, skip all previously typed characters and
3532 * return TRUE if quit reading script file.
3533 * Stop reading typeahead when a single CTRL-C was read,
3534 * fill_input_buf() returns this when not able to read from stdin.
3535 * Don't use buf[] here, closescript() may have freed typebuf.tb_buf[]
3536 * and buf may be pointing inside typebuf.tb_buf[].
3537 */
3538 if (got_int)
3539 {
3540#define DUM_LEN MAXMAPLEN * 3 + 3
3541 char_u dum[DUM_LEN + 1];
3542
3543 for (;;)
3544 {
3545 len = ui_inchar(dum, DUM_LEN, 0L, 0);
3546 if (len == 0 || (len == 1 && dum[0] == 3))
3547 break;
3548 }
3549 return retesc;
3550 }
3551
3552 /*
3553 * Always flush the output characters when getting input characters
Bram Moolenaarcb574f42019-01-25 22:29:57 +01003554 * from the user and not just peeking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 */
Bram Moolenaarcb574f42019-01-25 22:29:57 +01003556 if (wait_time == -1L || wait_time > 10L)
3557 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558
3559 /*
3560 * Fill up to a third of the buffer, because each character may be
3561 * tripled below.
3562 */
3563 len = ui_inchar(buf, maxlen / 3, wait_time, tb_change_cnt);
3564 }
3565
Bram Moolenaar30613902019-12-01 22:11:18 +01003566 // If the typebuf was changed further down, it is like nothing was added by
3567 // this call.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 if (typebuf_changed(tb_change_cnt))
3569 return 0;
3570
Bram Moolenaar30613902019-12-01 22:11:18 +01003571 // Note the change in the typeahead buffer, this matters for when
3572 // vgetorpeek() is called recursively, e.g. using getchar(1) in a timer
3573 // function.
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003574 if (len > 0 && ++typebuf.tb_change_cnt == 0)
3575 typebuf.tb_change_cnt = 1;
3576
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003577 return fix_input_buffer(buf, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578}
3579
3580/*
3581 * Fix typed characters for use by vgetc() and check_termcode().
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003582 * "buf[]" must have room to triple the number of bytes!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 * Returns the new length.
3584 */
3585 int
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003586fix_input_buffer(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587{
3588 int i;
3589 char_u *p = buf;
3590
3591 /*
3592 * Two characters are special: NUL and K_SPECIAL.
3593 * When compiled With the GUI CSI is also special.
3594 * Replace NUL by K_SPECIAL KS_ZERO KE_FILLER
3595 * Replace K_SPECIAL by K_SPECIAL KS_SPECIAL KE_FILLER
3596 * Replace CSI by K_SPECIAL KS_EXTRA KE_CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 */
3598 for (i = len; --i >= 0; ++p)
3599 {
3600#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003601 // When the GUI is used any character can come after a CSI, don't
3602 // escape it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 if (gui.in_use && p[0] == CSI && i >= 2)
3604 {
3605 p += 2;
3606 i -= 2;
3607 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003608# ifndef MSWIN
Bram Moolenaar30613902019-12-01 22:11:18 +01003609 // When the GUI is not used CSI needs to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 else if (!gui.in_use && p[0] == CSI)
3611 {
3612 mch_memmove(p + 3, p + 1, (size_t)i);
3613 *p++ = K_SPECIAL;
3614 *p++ = KS_EXTRA;
3615 *p = (int)KE_CSI;
3616 len += 2;
3617 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003618# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 else
3620#endif
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003621 if (p[0] == NUL || (p[0] == K_SPECIAL
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003622 // timeout may generate K_CURSORHOLD
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003623 && (i < 2 || p[1] != KS_EXTRA || p[2] != (int)KE_CURSORHOLD)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003624#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003625 // Win32 console passes modifiers
3626 && (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003627# ifdef VIMDLL
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003628 gui.in_use ||
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003629# endif
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003630 (i < 2 || p[1] != KS_MODIFIER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631#endif
3632 ))
3633 {
3634 mch_memmove(p + 3, p + 1, (size_t)i);
3635 p[2] = K_THIRD(p[0]);
3636 p[1] = K_SECOND(p[0]);
3637 p[0] = K_SPECIAL;
3638 p += 2;
3639 len += 2;
3640 }
3641 }
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003642 *p = NUL; // add trailing NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 return len;
3644}
3645
3646#if defined(USE_INPUT_BUF) || defined(PROTO)
3647/*
3648 * Return TRUE when bytes are in the input buffer or in the typeahead buffer.
3649 * Normally the input buffer would be sufficient, but the server_to_input_buf()
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003650 * or feedkeys() may insert characters in the typeahead buffer while we are
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003651 * waiting for input to arrive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 */
3653 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003654input_available(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655{
3656 return (!vim_is_input_buf_empty()
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003657# if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
3658 || typebuf_was_filled
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659# endif
3660 );
3661}
3662#endif
Bram Moolenaar957cf672020-11-12 14:21:06 +01003663
3664/*
3665 * Function passed to do_cmdline() to get the command after a <Cmd> key from
3666 * typeahead.
3667 */
3668 char_u *
3669getcmdkeycmd(
3670 int promptc UNUSED,
3671 void *cookie UNUSED,
3672 int indent UNUSED,
3673 getline_opt_T do_concat UNUSED)
3674{
3675 garray_T line_ga;
3676 int c1 = -1;
3677 int c2;
3678 int cmod = 0;
3679 int aborted = FALSE;
3680
3681 ga_init2(&line_ga, 1, 32);
3682
3683 // no mapping for these characters
3684 no_mapping++;
3685
3686 got_int = FALSE;
3687 while (c1 != NUL && !aborted)
3688 {
Bram Moolenaarca359cb2020-11-15 20:49:41 +01003689 if (ga_grow(&line_ga, 32) != OK)
3690 {
3691 aborted = TRUE;
3692 break;
3693 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003694
3695 if (vgetorpeek(FALSE) == NUL)
3696 {
3697 // incomplete <Cmd> is an error, because there is not much the user
3698 // could do in this state.
3699 emsg(_(e_cmd_mapping_must_end_with_cr));
3700 aborted = TRUE;
3701 break;
3702 }
3703
3704 // Get one character at a time.
3705 c1 = vgetorpeek(TRUE);
3706
3707 // Get two extra bytes for special keys
3708 if (c1 == K_SPECIAL)
3709 {
3710 c1 = vgetorpeek(TRUE);
3711 c2 = vgetorpeek(TRUE);
3712 if (c1 == KS_MODIFIER)
3713 {
3714 cmod = c2;
3715 continue;
3716 }
3717 c1 = TO_SPECIAL(c1, c2);
3718 }
Bram Moolenaar4a441202020-11-28 14:43:26 +01003719 if (c1 == Ctrl_V)
3720 {
3721 // CTRL-V is followed by octal, hex or other characters, reverses
3722 // what AppendToRedobuffLit() does.
3723 no_reduce_keys = TRUE; // don't merge modifyOtherKeys
Bram Moolenaar0684e362020-12-03 19:54:42 +01003724 c1 = get_literal(TRUE);
Bram Moolenaar4a441202020-11-28 14:43:26 +01003725 no_reduce_keys = FALSE;
3726 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003727
3728 if (got_int)
3729 aborted = TRUE;
3730 else if (c1 == '\r' || c1 == '\n')
3731 c1 = NUL; // end the line
3732 else if (c1 == ESC)
3733 aborted = TRUE;
3734 else if (c1 == K_COMMAND)
3735 {
3736 // give a nicer error message for this special case
3737 emsg(_(e_cmd_mapping_must_end_with_cr_before_second_cmd));
3738 aborted = TRUE;
3739 }
3740 else if (IS_SPECIAL(c1))
3741 {
3742 if (c1 == K_SNR)
Bram Moolenaarc77534c2020-11-18 11:34:37 +01003743 ga_concat(&line_ga, (char_u *)"<SNR>");
Bram Moolenaar957cf672020-11-12 14:21:06 +01003744 else
3745 {
3746 semsg(e_cmd_maping_must_not_include_str_key,
3747 get_special_key_name(c1, cmod));
3748 aborted = TRUE;
3749 }
3750 }
3751 else
3752 ga_append(&line_ga, (char)c1);
3753
3754 cmod = 0;
3755 }
3756
3757 no_mapping--;
3758
3759 if (aborted)
3760 ga_clear(&line_ga);
3761
3762 return (char_u *)line_ga.ga_data;
3763}