blob: fcee7dda721cb750b5e1f683634961c647e97ea7 [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;
450}
451
452/*
453 * The previous contents of the redo buffer is kept in old_redobuffer.
454 * This is used for the CTRL-O <.> command in insert mode.
455 */
456 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100457ResetRedobuff(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458{
459 if (!block_redo)
460 {
461 free_buff(&old_redobuff);
462 old_redobuff = redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200463 redobuff.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464 }
465}
466
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100467/*
468 * Discard the contents of the redo buffer and restore the previous redo
469 * buffer.
470 */
471 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100472CancelRedo(void)
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100473{
474 if (!block_redo)
475 {
476 free_buff(&redobuff);
477 redobuff = old_redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200478 old_redobuff.bh_first.b_next = NULL;
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100479 start_stuff();
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100480 while (read_readbuffers(TRUE) != NUL)
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100481 ;
482 }
483}
484
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485/*
486 * Save redobuff and old_redobuff to save_redobuff and save_old_redobuff.
487 * Used before executing autocommands and user functions.
488 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489 void
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200490saveRedobuff(save_redo_T *save_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491{
492 char_u *s;
493
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200494 save_redo->sr_redobuff = redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200495 redobuff.bh_first.b_next = NULL;
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200496 save_redo->sr_old_redobuff = old_redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200497 old_redobuff.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498
Bram Moolenaar30613902019-12-01 22:11:18 +0100499 // Make a copy, so that ":normal ." in a function works.
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200500 s = get_buffcont(&save_redo->sr_redobuff, FALSE);
501 if (s != NULL)
502 {
503 add_buff(&redobuff, s, -1L);
504 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 }
506}
507
508/*
509 * Restore redobuff and old_redobuff from save_redobuff and save_old_redobuff.
510 * Used after executing autocommands and user functions.
511 */
512 void
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200513restoreRedobuff(save_redo_T *save_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514{
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200515 free_buff(&redobuff);
516 redobuff = save_redo->sr_redobuff;
517 free_buff(&old_redobuff);
518 old_redobuff = save_redo->sr_old_redobuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520
521/*
522 * Append "s" to the redo buffer.
523 * K_SPECIAL and CSI should already have been escaped.
524 */
525 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100526AppendToRedobuff(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527{
528 if (!block_redo)
529 add_buff(&redobuff, s, -1L);
530}
531
532/*
533 * Append to Redo buffer literally, escaping special characters with CTRL-V.
534 * K_SPECIAL and CSI are escaped as well.
535 */
536 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100537AppendToRedobuffLit(
538 char_u *str,
Bram Moolenaar30613902019-12-01 22:11:18 +0100539 int len) // length of "str" or -1 for up to the NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540{
Bram Moolenaarebefac62005-12-28 22:39:57 +0000541 char_u *s = str;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 int c;
543 char_u *start;
544
545 if (block_redo)
546 return;
547
Bram Moolenaarebefac62005-12-28 22:39:57 +0000548 while (len < 0 ? *s != NUL : s - str < len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100550 // Put a string of normal characters in the redo buffer (that's
551 // faster).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 start = s;
553 while (*s >= ' '
554#ifndef EBCDIC
Bram Moolenaar30613902019-12-01 22:11:18 +0100555 && *s < DEL // EBCDIC: all chars above space are normal
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556#endif
Bram Moolenaarebefac62005-12-28 22:39:57 +0000557 && (len < 0 || s - str < len))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558 ++s;
559
Bram Moolenaar30613902019-12-01 22:11:18 +0100560 // Don't put '0' or '^' as last character, just in case a CTRL-D is
561 // typed next.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562 if (*s == NUL && (s[-1] == '0' || s[-1] == '^'))
563 --s;
564 if (s > start)
565 add_buff(&redobuff, start, (long)(s - start));
566
Bram Moolenaarebefac62005-12-28 22:39:57 +0000567 if (*s == NUL || (len >= 0 && s - str >= len))
568 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569
Bram Moolenaar30613902019-12-01 22:11:18 +0100570 // Handle a special or multibyte character.
Bram Moolenaarebefac62005-12-28 22:39:57 +0000571 if (has_mbyte)
Bram Moolenaar30613902019-12-01 22:11:18 +0100572 // Handle composing chars separately.
Bram Moolenaarebefac62005-12-28 22:39:57 +0000573 c = mb_cptr2char_adv(&s);
574 else
Bram Moolenaarebefac62005-12-28 22:39:57 +0000575 c = *s++;
576 if (c < ' ' || c == DEL || (*s == NUL && (c == '0' || c == '^')))
577 add_char_buff(&redobuff, Ctrl_V);
578
Bram Moolenaar30613902019-12-01 22:11:18 +0100579 // CTRL-V '0' must be inserted as CTRL-V 048 (EBCDIC: xf0)
Bram Moolenaarebefac62005-12-28 22:39:57 +0000580 if (*s == NUL && c == '0')
581#ifdef EBCDIC
582 add_buff(&redobuff, (char_u *)"xf0", 3L);
583#else
584 add_buff(&redobuff, (char_u *)"048", 3L);
585#endif
586 else
587 add_char_buff(&redobuff, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 }
589}
590
591/*
592 * Append a character to the redo buffer.
593 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
594 */
595 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100596AppendCharToRedobuff(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597{
598 if (!block_redo)
599 add_char_buff(&redobuff, c);
600}
601
602/*
603 * Append a number to the redo buffer.
604 */
605 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100606AppendNumberToRedobuff(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607{
608 if (!block_redo)
609 add_num_buff(&redobuff, n);
610}
611
612/*
613 * Append string "s" to the stuff buffer.
614 * CSI and K_SPECIAL must already have been escaped.
615 */
616 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100617stuffReadbuff(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100619 add_buff(&readbuf1, s, -1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620}
621
Bram Moolenaar4f5ce332014-07-30 16:00:58 +0200622/*
623 * Append string "s" to the redo stuff buffer.
624 * CSI and K_SPECIAL must already have been escaped.
625 */
626 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100627stuffRedoReadbuff(char_u *s)
Bram Moolenaar4f5ce332014-07-30 16:00:58 +0200628{
629 add_buff(&readbuf2, s, -1L);
630}
631
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100633stuffReadbuffLen(char_u *s, long len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100635 add_buff(&readbuf1, s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636}
637
638#if defined(FEAT_EVAL) || defined(PROTO)
639/*
640 * Stuff "s" into the stuff buffer, leaving special key codes unmodified and
641 * escaping other K_SPECIAL and CSI bytes.
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200642 * Change CR, LF and ESC into a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 */
644 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100645stuffReadbuffSpec(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646{
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200647 int c;
648
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 while (*s != NUL)
650 {
651 if (*s == K_SPECIAL && s[1] != NUL && s[2] != NUL)
652 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100653 // Insert special key literally.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 stuffReadbuffLen(s, 3L);
655 s += 3;
656 }
657 else
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200658 {
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200659 c = mb_ptr2char_adv(&s);
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200660 if (c == CAR || c == NL || c == ESC)
661 c = ' ';
662 stuffcharReadbuff(c);
663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 }
665}
666#endif
667
668/*
669 * Append a character to the stuff buffer.
670 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
671 */
672 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100673stuffcharReadbuff(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100675 add_char_buff(&readbuf1, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676}
677
678/*
679 * Append a number to the stuff buffer.
680 */
681 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100682stuffnumReadbuff(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100684 add_num_buff(&readbuf1, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685}
686
687/*
Bram Moolenaar11abd092020-05-01 14:26:37 +0200688 * Stuff a string into the typeahead buffer, such that edit() will insert it
689 * literally ("literally" TRUE) or interpret is as typed characters.
690 */
691 void
692stuffescaped(char_u *arg, int literally)
693{
694 int c;
695 char_u *start;
696
697 while (*arg != NUL)
698 {
699 // Stuff a sequence of normal ASCII characters, that's fast. Also
700 // stuff K_SPECIAL to get the effect of a special key when "literally"
701 // is TRUE.
702 start = arg;
703 while ((*arg >= ' '
704#ifndef EBCDIC
705 && *arg < DEL // EBCDIC: chars above space are normal
706#endif
707 )
708 || (*arg == K_SPECIAL && !literally))
709 ++arg;
710 if (arg > start)
711 stuffReadbuffLen(start, (long)(arg - start));
712
713 // stuff a single special character
714 if (*arg != NUL)
715 {
716 if (has_mbyte)
717 c = mb_cptr2char_adv(&arg);
718 else
719 c = *arg++;
720 if (literally && ((c < ' ' && c != TAB) || c == DEL))
721 stuffcharReadbuff(Ctrl_V);
722 stuffcharReadbuff(c);
723 }
724 }
725}
726
727/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 * Read a character from the redo buffer. Translates K_SPECIAL, CSI and
729 * multibyte characters.
730 * The redo buffer is left as it is.
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100731 * If init is TRUE, prepare for redo, return FAIL if nothing to redo, OK
732 * otherwise.
733 * If old is TRUE, use old_redobuff instead of redobuff.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 */
735 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100736read_redo(int init, int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100738 static buffblock_T *bp;
739 static char_u *p;
740 int c;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100741 int n;
742 char_u buf[MB_MAXBYTES + 1];
743 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744
745 if (init)
746 {
747 if (old_redo)
Bram Moolenaar285e3352018-04-18 23:01:13 +0200748 bp = old_redobuff.bh_first.b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 else
Bram Moolenaar285e3352018-04-18 23:01:13 +0200750 bp = redobuff.bh_first.b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 if (bp == NULL)
752 return FAIL;
753 p = bp->b_str;
754 return OK;
755 }
756 if ((c = *p) != NUL)
757 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100758 // Reverse the conversion done by add_char_buff()
759 // For a multi-byte character get all the bytes and return the
760 // converted character.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 if (has_mbyte && (c != K_SPECIAL || p[1] == KS_SPECIAL))
762 n = MB_BYTE2LEN_CHECK(c);
763 else
764 n = 1;
765 for (i = 0; ; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100767 if (c == K_SPECIAL) // special key or escaped K_SPECIAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 {
769 c = TO_SPECIAL(p[1], p[2]);
770 p += 2;
771 }
772#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +0100773 if (c == CSI) // escaped CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 p += 2;
775#endif
776 if (*++p == NUL && bp->b_next != NULL)
777 {
778 bp = bp->b_next;
779 p = bp->b_str;
780 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 buf[i] = c;
Bram Moolenaar30613902019-12-01 22:11:18 +0100782 if (i == n - 1) // last byte of a character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 {
784 if (n != 1)
785 c = (*mb_ptr2char)(buf);
786 break;
787 }
788 c = *p;
Bram Moolenaar30613902019-12-01 22:11:18 +0100789 if (c == NUL) // cannot happen?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 }
792 }
793
794 return c;
795}
796
797/*
798 * Copy the rest of the redo buffer into the stuff buffer (in a slow way).
799 * If old_redo is TRUE, use old_redobuff instead of redobuff.
800 * The escaped K_SPECIAL and CSI are copied without translation.
801 */
802 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100803copy_redo(int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804{
805 int c;
806
807 while ((c = read_redo(FALSE, old_redo)) != NUL)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100808 add_char_buff(&readbuf2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809}
810
811/*
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100812 * Stuff the redo buffer into readbuf2.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 * Insert the redo count into the command.
814 * If "old_redo" is TRUE, the last but one command is repeated
815 * instead of the last command (inserting text). This is used for
816 * CTRL-O <.> in insert mode
817 *
818 * return FAIL for failure, OK otherwise
819 */
820 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100821start_redo(long count, int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822{
823 int c;
824
Bram Moolenaar30613902019-12-01 22:11:18 +0100825 // init the pointers; return if nothing to redo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 if (read_redo(TRUE, old_redo) == FAIL)
827 return FAIL;
828
829 c = read_redo(FALSE, old_redo);
830
Bram Moolenaar30613902019-12-01 22:11:18 +0100831 // copy the buffer name, if present
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 if (c == '"')
833 {
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100834 add_buff(&readbuf2, (char_u *)"\"", 1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 c = read_redo(FALSE, old_redo);
836
Bram Moolenaar30613902019-12-01 22:11:18 +0100837 // if a numbered buffer is used, increment the number
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 if (c >= '1' && c < '9')
839 ++c;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100840 add_char_buff(&readbuf2, c);
Bram Moolenaar833093b2018-05-23 21:53:52 +0200841
Bram Moolenaar30613902019-12-01 22:11:18 +0100842 // the expression register should be re-evaluated
Bram Moolenaar833093b2018-05-23 21:53:52 +0200843 if (c == '=')
844 {
845 add_char_buff(&readbuf2, CAR);
846 cmd_silent = TRUE;
847 }
848
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 c = read_redo(FALSE, old_redo);
850 }
851
Bram Moolenaar30613902019-12-01 22:11:18 +0100852 if (c == 'v') // redo Visual
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 {
854 VIsual = curwin->w_cursor;
855 VIsual_active = TRUE;
856 VIsual_select = FALSE;
857 VIsual_reselect = TRUE;
858 redo_VIsual_busy = TRUE;
859 c = read_redo(FALSE, old_redo);
860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861
Bram Moolenaar30613902019-12-01 22:11:18 +0100862 // try to enter the count (in place of a previous count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 if (count)
864 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100865 while (VIM_ISDIGIT(c)) // skip "old" count
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 c = read_redo(FALSE, old_redo);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100867 add_num_buff(&readbuf2, count);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 }
869
Bram Moolenaar30613902019-12-01 22:11:18 +0100870 // copy from the redo buffer into the stuff buffer
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100871 add_char_buff(&readbuf2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 copy_redo(old_redo);
873 return OK;
874}
875
876/*
877 * Repeat the last insert (R, o, O, a, A, i or I command) by stuffing
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100878 * the redo buffer into readbuf2.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 * return FAIL for failure, OK otherwise
880 */
881 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100882start_redo_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883{
884 int c;
885
886 if (read_redo(TRUE, FALSE) == FAIL)
887 return FAIL;
888 start_stuff();
889
Bram Moolenaar30613902019-12-01 22:11:18 +0100890 // skip the count and the command character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 while ((c = read_redo(FALSE, FALSE)) != NUL)
892 {
893 if (vim_strchr((char_u *)"AaIiRrOo", c) != NULL)
894 {
895 if (c == 'O' || c == 'o')
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100896 add_buff(&readbuf2, NL_STR, -1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 break;
898 }
899 }
900
Bram Moolenaar30613902019-12-01 22:11:18 +0100901 // copy the typed text from the redo buffer into the stuff buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 copy_redo(FALSE);
903 block_redo = TRUE;
904 return OK;
905}
906
907 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100908stop_redo_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909{
910 block_redo = FALSE;
911}
912
913/*
914 * Initialize typebuf.tb_buf to point to typebuf_init.
915 * alloc() cannot be used here: In out-of-memory situations it would
916 * be impossible to type anything.
917 */
918 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100919init_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920{
921 if (typebuf.tb_buf == NULL)
922 {
923 typebuf.tb_buf = typebuf_init;
924 typebuf.tb_noremap = noremapbuf_init;
925 typebuf.tb_buflen = TYPELEN_INIT;
926 typebuf.tb_len = 0;
Bram Moolenaard34f9b12017-04-08 18:41:13 +0200927 typebuf.tb_off = MAXMAPLEN + 4;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 typebuf.tb_change_cnt = 1;
929 }
930}
931
932/*
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200933 * Returns TRUE when keys cannot be remapped.
934 */
935 int
936noremap_keys(void)
937{
938 return KeyNoremap & (RM_NONE|RM_SCRIPT);
939}
940
941/*
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100942 * Insert a string in position 'offset' in the typeahead buffer (for "@r"
943 * and ":normal" command, vgetorpeek() and check_termcode()).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 *
945 * If noremap is REMAP_YES, new string can be mapped again.
946 * If noremap is REMAP_NONE, new string cannot be mapped again.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000947 * If noremap is REMAP_SKIP, fist char of new string cannot be mapped again,
948 * but abbreviations are allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 * If noremap is REMAP_SCRIPT, new string cannot be mapped again, except for
950 * script-local mappings.
951 * If noremap is > 0, that many characters of the new string cannot be mapped.
952 *
953 * If nottyped is TRUE, the string does not return KeyTyped (don't use when
954 * offset is non-zero!).
955 *
956 * If silent is TRUE, cmd_silent is set when the characters are obtained.
957 *
958 * return FAIL for failure, OK otherwise
959 */
960 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100961ins_typebuf(
962 char_u *str,
963 int noremap,
964 int offset,
965 int nottyped,
966 int silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967{
968 char_u *s1, *s2;
969 int newlen;
970 int addlen;
971 int i;
972 int newoff;
973 int val;
974 int nrm;
975
976 init_typebuf();
977 if (++typebuf.tb_change_cnt == 0)
978 typebuf.tb_change_cnt = 1;
Bram Moolenaard103ee72019-09-18 21:15:31 +0200979 state_no_longer_safe("ins_typebuf()");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980
981 addlen = (int)STRLEN(str);
Bram Moolenaar4e86cba2007-05-06 11:56:32 +0000982
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 if (offset == 0 && addlen <= typebuf.tb_off)
984 {
Bram Moolenaarcaa55b62017-01-10 13:51:09 +0100985 /*
986 * Easy case: there is room in front of typebuf.tb_buf[typebuf.tb_off]
987 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 typebuf.tb_off -= addlen;
989 mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen);
990 }
Bram Moolenaard34f9b12017-04-08 18:41:13 +0200991 else if (typebuf.tb_len == 0 && typebuf.tb_buflen
992 >= addlen + 3 * (MAXMAPLEN + 4))
993 {
994 /*
995 * Buffer is empty and string fits in the existing buffer.
996 * Leave some space before and after, if possible.
997 */
998 typebuf.tb_off = (typebuf.tb_buflen - addlen - 3 * (MAXMAPLEN + 4)) / 2;
999 mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen);
1000 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 else
1002 {
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001003 /*
1004 * Need to allocate a new buffer.
Bram Moolenaard34f9b12017-04-08 18:41:13 +02001005 * In typebuf.tb_buf there must always be room for 3 * (MAXMAPLEN + 4)
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001006 * characters. We add some extra room to avoid having to allocate too
1007 * often.
1008 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 newoff = MAXMAPLEN + 4;
1010 newlen = typebuf.tb_len + addlen + newoff + 4 * (MAXMAPLEN + 4);
Bram Moolenaar30613902019-12-01 22:11:18 +01001011 if (newlen < 0) // string is getting too long
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001013 emsg(_(e_toocompl)); // also calls flush_buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 setcursor();
1015 return FAIL;
1016 }
1017 s1 = alloc(newlen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001018 if (s1 == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 return FAIL;
1020 s2 = alloc(newlen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001021 if (s2 == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 {
1023 vim_free(s1);
1024 return FAIL;
1025 }
1026 typebuf.tb_buflen = newlen;
1027
Bram Moolenaar30613902019-12-01 22:11:18 +01001028 // copy the old chars, before the insertion point
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 mch_memmove(s1 + newoff, typebuf.tb_buf + typebuf.tb_off,
1030 (size_t)offset);
Bram Moolenaar30613902019-12-01 22:11:18 +01001031 // copy the new chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 mch_memmove(s1 + newoff + offset, str, (size_t)addlen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001033 // copy the old chars, after the insertion point, including the NUL at
1034 // the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 mch_memmove(s1 + newoff + offset + addlen,
1036 typebuf.tb_buf + typebuf.tb_off + offset,
1037 (size_t)(typebuf.tb_len - offset + 1));
1038 if (typebuf.tb_buf != typebuf_init)
1039 vim_free(typebuf.tb_buf);
1040 typebuf.tb_buf = s1;
1041
1042 mch_memmove(s2 + newoff, typebuf.tb_noremap + typebuf.tb_off,
1043 (size_t)offset);
1044 mch_memmove(s2 + newoff + offset + addlen,
1045 typebuf.tb_noremap + typebuf.tb_off + offset,
1046 (size_t)(typebuf.tb_len - offset));
1047 if (typebuf.tb_noremap != noremapbuf_init)
1048 vim_free(typebuf.tb_noremap);
1049 typebuf.tb_noremap = s2;
1050
1051 typebuf.tb_off = newoff;
1052 }
1053 typebuf.tb_len += addlen;
1054
Bram Moolenaar30613902019-12-01 22:11:18 +01001055 // If noremap == REMAP_SCRIPT: do remap script-local mappings.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 if (noremap == REMAP_SCRIPT)
1057 val = RM_SCRIPT;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001058 else if (noremap == REMAP_SKIP)
1059 val = RM_ABBR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 else
1061 val = RM_NONE;
1062
1063 /*
1064 * Adjust typebuf.tb_noremap[] for the new characters:
1065 * If noremap == REMAP_NONE or REMAP_SCRIPT: new characters are
1066 * (sometimes) not remappable
1067 * If noremap == REMAP_YES: all the new characters are mappable
1068 * If noremap > 0: "noremap" characters are not remappable, the rest
1069 * mappable
1070 */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001071 if (noremap == REMAP_SKIP)
1072 nrm = 1;
1073 else if (noremap < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 nrm = addlen;
1075 else
1076 nrm = noremap;
1077 for (i = 0; i < addlen; ++i)
1078 typebuf.tb_noremap[typebuf.tb_off + i + offset] =
1079 (--nrm >= 0) ? val : RM_YES;
1080
Bram Moolenaar30613902019-12-01 22:11:18 +01001081 // tb_maplen and tb_silent only remember the length of mapped and/or
1082 // silent mappings at the start of the buffer, assuming that a mapped
1083 // sequence doesn't result in typed characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 if (nottyped || typebuf.tb_maplen > offset)
1085 typebuf.tb_maplen += addlen;
1086 if (silent || typebuf.tb_silent > offset)
1087 {
1088 typebuf.tb_silent += addlen;
1089 cmd_silent = TRUE;
1090 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001091 if (typebuf.tb_no_abbr_cnt && offset == 0) // and not used for abbrev.s
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 typebuf.tb_no_abbr_cnt += addlen;
1093
1094 return OK;
1095}
1096
1097/*
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001098 * Put character "c" back into the typeahead buffer.
1099 * Can be used for a character obtained by vgetc() that needs to be put back.
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00001100 * Uses cmd_silent, KeyTyped and KeyNoremap to restore the flags belonging to
1101 * the char.
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001102 */
1103 void
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001104ins_char_typebuf(int c, int modifier)
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001105{
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001106 char_u buf[MB_MAXBYTES + 4];
1107 int idx = 0;
1108
1109 if (modifier != 0)
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001110 {
1111 buf[0] = K_SPECIAL;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001112 buf[1] = KS_MODIFIER;
1113 buf[2] = modifier;
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001114 buf[3] = NUL;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001115 idx = 3;
1116 }
1117 if (IS_SPECIAL(c))
1118 {
1119 buf[idx] = K_SPECIAL;
1120 buf[idx + 1] = K_SECOND(c);
1121 buf[idx + 2] = K_THIRD(c);
1122 buf[idx + 3] = NUL;
1123 idx += 3;
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001124 }
1125 else
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001126 buf[(*mb_char2bytes)(c, buf + idx) + idx] = NUL;
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00001127 (void)ins_typebuf(buf, KeyNoremap, 0, !KeyTyped, cmd_silent);
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001128}
1129
1130/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 * Return TRUE if the typeahead buffer was changed (while waiting for a
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001132 * character to arrive). Happens when a message was received from a client or
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001133 * from feedkeys().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 * But check in a more generic way to avoid trouble: When "typebuf.tb_buf"
1135 * changed it was reallocated and the old pointer can no longer be used.
1136 * Or "typebuf.tb_off" may have been changed and we would overwrite characters
1137 * that was just added.
1138 */
1139 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001140typebuf_changed(
Bram Moolenaar30613902019-12-01 22:11:18 +01001141 int tb_change_cnt) // old value of typebuf.tb_change_cnt
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142{
1143 return (tb_change_cnt != 0 && (typebuf.tb_change_cnt != tb_change_cnt
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001144#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
1145 || typebuf_was_filled
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146#endif
1147 ));
1148}
1149
1150/*
1151 * Return TRUE if there are no characters in the typeahead buffer that have
1152 * not been typed (result from a mapping or come from ":normal").
1153 */
1154 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001155typebuf_typed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156{
1157 return typebuf.tb_maplen == 0;
1158}
1159
1160/*
1161 * Return the number of characters that are mapped (or not typed).
1162 */
1163 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001164typebuf_maplen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165{
1166 return typebuf.tb_maplen;
1167}
1168
1169/*
1170 * remove "len" characters from typebuf.tb_buf[typebuf.tb_off + offset]
1171 */
1172 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001173del_typebuf(int len, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174{
1175 int i;
1176
1177 if (len == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01001178 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179
1180 typebuf.tb_len -= len;
1181
1182 /*
1183 * Easy case: Just increase typebuf.tb_off.
1184 */
1185 if (offset == 0 && typebuf.tb_buflen - (typebuf.tb_off + len)
1186 >= 3 * MAXMAPLEN + 3)
1187 typebuf.tb_off += len;
1188 /*
1189 * Have to move the characters in typebuf.tb_buf[] and typebuf.tb_noremap[]
1190 */
1191 else
1192 {
1193 i = typebuf.tb_off + offset;
1194 /*
1195 * Leave some extra room at the end to avoid reallocation.
1196 */
1197 if (typebuf.tb_off > MAXMAPLEN)
1198 {
1199 mch_memmove(typebuf.tb_buf + MAXMAPLEN,
1200 typebuf.tb_buf + typebuf.tb_off, (size_t)offset);
1201 mch_memmove(typebuf.tb_noremap + MAXMAPLEN,
1202 typebuf.tb_noremap + typebuf.tb_off, (size_t)offset);
1203 typebuf.tb_off = MAXMAPLEN;
1204 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001205 // adjust typebuf.tb_buf (include the NUL at the end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset,
1207 typebuf.tb_buf + i + len,
1208 (size_t)(typebuf.tb_len - offset + 1));
Bram Moolenaar30613902019-12-01 22:11:18 +01001209 // adjust typebuf.tb_noremap[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 mch_memmove(typebuf.tb_noremap + typebuf.tb_off + offset,
1211 typebuf.tb_noremap + i + len,
1212 (size_t)(typebuf.tb_len - offset));
1213 }
1214
Bram Moolenaar30613902019-12-01 22:11:18 +01001215 if (typebuf.tb_maplen > offset) // adjust tb_maplen
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 {
1217 if (typebuf.tb_maplen < offset + len)
1218 typebuf.tb_maplen = offset;
1219 else
1220 typebuf.tb_maplen -= len;
1221 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001222 if (typebuf.tb_silent > offset) // adjust tb_silent
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 {
1224 if (typebuf.tb_silent < offset + len)
1225 typebuf.tb_silent = offset;
1226 else
1227 typebuf.tb_silent -= len;
1228 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001229 if (typebuf.tb_no_abbr_cnt > offset) // adjust tb_no_abbr_cnt
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 {
1231 if (typebuf.tb_no_abbr_cnt < offset + len)
1232 typebuf.tb_no_abbr_cnt = offset;
1233 else
1234 typebuf.tb_no_abbr_cnt -= len;
1235 }
1236
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001237#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar30613902019-12-01 22:11:18 +01001238 // Reset the flag that text received from a client or from feedkeys()
1239 // was inserted in the typeahead buffer.
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001240 typebuf_was_filled = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241#endif
1242 if (++typebuf.tb_change_cnt == 0)
1243 typebuf.tb_change_cnt = 1;
1244}
1245
1246/*
1247 * Write typed characters to script file.
1248 * If recording is on put the character in the recordbuffer.
1249 */
1250 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001251gotchars(char_u *chars, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252{
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001253 char_u *s = chars;
1254 int i;
1255 static char_u buf[4];
1256 static int buflen = 0;
1257 int todo = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258
Bram Moolenaar4e86cba2007-05-06 11:56:32 +00001259 while (todo--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 {
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001261 buf[buflen++] = *s++;
1262
1263 // When receiving a special key sequence, store it until we have all
1264 // the bytes and we can decide what to do with it.
1265 if (buflen == 1 && buf[0] == K_SPECIAL)
1266 continue;
1267 if (buflen == 2)
1268 continue;
1269 if (buflen == 3 && buf[1] == KS_EXTRA
1270 && (buf[2] == KE_FOCUSGAINED || buf[2] == KE_FOCUSLOST))
1271 {
1272 // Drop K_FOCUSGAINED and K_FOCUSLOST, they are not useful in a
1273 // recording.
1274 buflen = 0;
1275 continue;
1276 }
1277
Bram Moolenaar30613902019-12-01 22:11:18 +01001278 // Handle one byte at a time; no translation to be done.
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001279 for (i = 0; i < buflen; ++i)
1280 updatescript(buf[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001282 if (reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 {
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001284 buf[buflen] = NUL;
1285 add_buff(&recordbuff, buf, (long)buflen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001286 // remember how many chars were last recorded
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001287 last_recorded_len += buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 }
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001289 buflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 }
1291 may_sync_undo();
1292
1293#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01001294 // output "debug mode" message next time in debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 debug_did_msg = FALSE;
1296#endif
1297
Bram Moolenaar30613902019-12-01 22:11:18 +01001298 // Since characters have been typed, consider the following to be in
1299 // another mapping. Search string will be kept in history.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 ++maptick;
1301}
1302
1303/*
1304 * Sync undo. Called when typed characters are obtained from the typeahead
1305 * buffer, or when a menu is used.
1306 * Do not sync:
1307 * - In Insert mode, unless cursor key has been used.
1308 * - While reading a script file.
1309 * - When no_u_sync is non-zero.
1310 */
1311 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001312may_sync_undo(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313{
1314 if ((!(State & (INSERT + CMDLINE)) || arrow_used)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001315 && scriptin[curscript] == NULL)
1316 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317}
1318
1319/*
1320 * Make "typebuf" empty and allocate new buffers.
1321 * Returns FAIL when out of memory.
1322 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001323 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001324alloc_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325{
1326 typebuf.tb_buf = alloc(TYPELEN_INIT);
1327 typebuf.tb_noremap = alloc(TYPELEN_INIT);
1328 if (typebuf.tb_buf == NULL || typebuf.tb_noremap == NULL)
1329 {
1330 free_typebuf();
1331 return FAIL;
1332 }
1333 typebuf.tb_buflen = TYPELEN_INIT;
Bram Moolenaar30613902019-12-01 22:11:18 +01001334 typebuf.tb_off = MAXMAPLEN + 4; // can insert without realloc
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 typebuf.tb_len = 0;
1336 typebuf.tb_maplen = 0;
1337 typebuf.tb_silent = 0;
1338 typebuf.tb_no_abbr_cnt = 0;
1339 if (++typebuf.tb_change_cnt == 0)
1340 typebuf.tb_change_cnt = 1;
Bram Moolenaare49b4bb2020-03-11 13:01:40 +01001341#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
1342 typebuf_was_filled = FALSE;
1343#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 return OK;
1345}
1346
1347/*
1348 * Free the buffers of "typebuf".
1349 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001350 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001351free_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352{
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001353 if (typebuf.tb_buf == typebuf_init)
Bram Moolenaar95f09602016-11-10 20:01:45 +01001354 internal_error("Free typebuf 1");
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001355 else
Bram Moolenaar0f1c6702019-09-28 15:24:00 +02001356 VIM_CLEAR(typebuf.tb_buf);
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00001357 if (typebuf.tb_noremap == noremapbuf_init)
Bram Moolenaar95f09602016-11-10 20:01:45 +01001358 internal_error("Free typebuf 2");
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001359 else
Bram Moolenaar0f1c6702019-09-28 15:24:00 +02001360 VIM_CLEAR(typebuf.tb_noremap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361}
1362
1363/*
1364 * When doing ":so! file", the current typeahead needs to be saved, and
1365 * restored when "file" has been read completely.
1366 */
1367static typebuf_T saved_typebuf[NSCRIPT];
1368
1369 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001370save_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371{
1372 init_typebuf();
1373 saved_typebuf[curscript] = typebuf;
Bram Moolenaar30613902019-12-01 22:11:18 +01001374 // If out of memory: restore typebuf and close file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 if (alloc_typebuf() == FAIL)
1376 {
1377 closescript();
1378 return FAIL;
1379 }
1380 return OK;
1381}
1382
Bram Moolenaar30613902019-12-01 22:11:18 +01001383static int old_char = -1; // character put back by vungetc()
1384static int old_mod_mask; // mod_mask for ungotten character
1385static int old_mouse_row; // mouse_row related to old_char
1386static int old_mouse_col; // mouse_col related to old_char
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001387
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388/*
1389 * Save all three kinds of typeahead, so that the user must type at a prompt.
1390 */
1391 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001392save_typeahead(tasave_T *tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393{
1394 tp->save_typebuf = typebuf;
1395 tp->typebuf_valid = (alloc_typebuf() == OK);
1396 if (!tp->typebuf_valid)
1397 typebuf = tp->save_typebuf;
1398
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001399 tp->old_char = old_char;
1400 tp->old_mod_mask = old_mod_mask;
1401 old_char = -1;
1402
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001403 tp->save_readbuf1 = readbuf1;
Bram Moolenaar285e3352018-04-18 23:01:13 +02001404 readbuf1.bh_first.b_next = NULL;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001405 tp->save_readbuf2 = readbuf2;
Bram Moolenaar285e3352018-04-18 23:01:13 +02001406 readbuf2.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407# ifdef USE_INPUT_BUF
1408 tp->save_inputbuf = get_input_buf();
1409# endif
1410}
1411
1412/*
1413 * Restore the typeahead to what it was before calling save_typeahead().
1414 * The allocated memory is freed, can only be called once!
1415 */
1416 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001417restore_typeahead(tasave_T *tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418{
1419 if (tp->typebuf_valid)
1420 {
1421 free_typebuf();
1422 typebuf = tp->save_typebuf;
1423 }
1424
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001425 old_char = tp->old_char;
1426 old_mod_mask = tp->old_mod_mask;
1427
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001428 free_buff(&readbuf1);
1429 readbuf1 = tp->save_readbuf1;
1430 free_buff(&readbuf2);
1431 readbuf2 = tp->save_readbuf2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432# ifdef USE_INPUT_BUF
1433 set_input_buf(tp->save_inputbuf);
1434# endif
1435}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436
1437/*
1438 * Open a new script file for the ":source!" command.
1439 */
1440 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001441openscript(
1442 char_u *name,
Bram Moolenaar30613902019-12-01 22:11:18 +01001443 int directly) // when TRUE execute directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444{
1445 if (curscript + 1 == NSCRIPT)
1446 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001447 emsg(_(e_nesting));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 return;
1449 }
Bram Moolenaar53575522019-05-22 22:38:25 +02001450
1451 // Disallow sourcing a file in the sandbox, the commands would be executed
1452 // later, possibly outside of the sandbox.
1453 if (check_secure())
1454 return;
1455
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001456#ifdef FEAT_EVAL
1457 if (ignore_script)
Bram Moolenaar30613902019-12-01 22:11:18 +01001458 // Not reading from script, also don't open one. Warning message?
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001459 return;
1460#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461
Bram Moolenaar30613902019-12-01 22:11:18 +01001462 if (scriptin[curscript] != NULL) // already reading script
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 ++curscript;
Bram Moolenaar30613902019-12-01 22:11:18 +01001464 // use NameBuff for expanded name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 expand_env(name, NameBuff, MAXPATHL);
1466 if ((scriptin[curscript] = mch_fopen((char *)NameBuff, READBIN)) == NULL)
1467 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001468 semsg(_(e_notopen), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 if (curscript)
1470 --curscript;
1471 return;
1472 }
1473 if (save_typebuf() == FAIL)
1474 return;
1475
1476 /*
1477 * Execute the commands from the file right now when using ":source!"
1478 * after ":global" or ":argdo" or in a loop. Also when another command
1479 * follows. This means the display won't be updated. Don't do this
1480 * always, "make test" would fail.
1481 */
1482 if (directly)
1483 {
1484 oparg_T oa;
1485 int oldcurscript;
1486 int save_State = State;
1487 int save_restart_edit = restart_edit;
1488 int save_insertmode = p_im;
1489 int save_finish_op = finish_op;
1490 int save_msg_scroll = msg_scroll;
1491
1492 State = NORMAL;
Bram Moolenaar30613902019-12-01 22:11:18 +01001493 msg_scroll = FALSE; // no msg scrolling in Normal mode
1494 restart_edit = 0; // don't go to Insert mode
1495 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 clear_oparg(&oa);
1497 finish_op = FALSE;
1498
1499 oldcurscript = curscript;
1500 do
1501 {
Bram Moolenaar386b43e2019-05-19 21:57:11 +02001502 update_topline_cursor(); // update cursor position and topline
1503 normal_cmd(&oa, FALSE); // execute one command
1504 vpeekc(); // check for end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 }
1506 while (scriptin[oldcurscript] != NULL);
1507
1508 State = save_State;
1509 msg_scroll = save_msg_scroll;
1510 restart_edit = save_restart_edit;
1511 p_im = save_insertmode;
1512 finish_op = save_finish_op;
1513 }
1514}
1515
1516/*
1517 * Close the currently active input script.
1518 */
1519 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001520closescript(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521{
1522 free_typebuf();
1523 typebuf = saved_typebuf[curscript];
1524
1525 fclose(scriptin[curscript]);
1526 scriptin[curscript] = NULL;
1527 if (curscript > 0)
1528 --curscript;
1529}
1530
Bram Moolenaarea408852005-06-25 22:49:46 +00001531#if defined(EXITFREE) || defined(PROTO)
1532 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001533close_all_scripts(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00001534{
1535 while (scriptin[0] != NULL)
1536 closescript();
1537}
1538#endif
1539
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540/*
1541 * Return TRUE when reading keys from a script file.
1542 */
1543 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001544using_script(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545{
1546 return scriptin[curscript] != NULL;
1547}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548
1549/*
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001550 * This function is called just before doing a blocking wait. Thus after
1551 * waiting 'updatetime' for a character to arrive.
1552 */
1553 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001554before_blocking(void)
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001555{
1556 updatescript(0);
1557#ifdef FEAT_EVAL
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001558 if (may_garbage_collect)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001559 garbage_collect(FALSE);
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001560#endif
1561}
1562
1563/*
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001564 * updatescript() is called when a character can be written into the script file
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 * or when we have waited some time for a character (c == 0)
1566 *
1567 * All the changed memfiles are synced if c == 0 or when the number of typed
1568 * characters reaches 'updatecount' and 'updatecount' is non-zero.
1569 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001570 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001571updatescript(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572{
1573 static int count = 0;
1574
1575 if (c && scriptout)
1576 putc(c, scriptout);
1577 if (c == 0 || (p_uc > 0 && ++count >= p_uc))
1578 {
1579 ml_sync_all(c == 0, TRUE);
1580 count = 0;
1581 }
1582}
1583
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584/*
Bram Moolenaar975a8802020-06-06 22:36:24 +02001585 * Convert "c" plus "modifiers" to merge the effect of modifyOtherKeys into the
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001586 * character.
1587 */
1588 int
Bram Moolenaar975a8802020-06-06 22:36:24 +02001589merge_modifyOtherKeys(int c_arg, int *modifiers)
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001590{
1591 int c = c_arg;
1592
Bram Moolenaar975a8802020-06-06 22:36:24 +02001593 if (*modifiers & MOD_MASK_CTRL)
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001594 {
1595 if ((c >= '`' && c <= 0x7f) || (c >= '@' && c <= '_'))
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001596 c &= 0x1f;
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001597 else if (c == '6')
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001598 // CTRL-6 is equivalent to CTRL-^
1599 c = 0x1e;
Bram Moolenaarf4ae6b22020-05-30 19:52:46 +02001600#ifdef FEAT_GUI_GTK
1601 // These mappings look arbitrary at the first glance, but in fact
1602 // resemble quite exactly the behaviour of the GTK+ 1.2 GUI on my
1603 // machine. The only difference is BS vs. DEL for CTRL-8 (makes
1604 // more sense and is consistent with usual terminal behaviour).
1605 else if (c == '2')
1606 c = NUL;
1607 else if (c >= '3' && c <= '7')
1608 c = c ^ 0x28;
1609 else if (c == '8')
1610 c = BS;
1611 else if (c == '?')
1612 c = DEL;
1613#endif
1614 if (c != c_arg)
Bram Moolenaar975a8802020-06-06 22:36:24 +02001615 *modifiers &= ~MOD_MASK_CTRL;
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001616 }
Bram Moolenaar975a8802020-06-06 22:36:24 +02001617 if ((*modifiers & (MOD_MASK_META | MOD_MASK_ALT))
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001618 && c >= 0 && c <= 127)
1619 {
1620 c += 0x80;
Bram Moolenaar975a8802020-06-06 22:36:24 +02001621 *modifiers &= ~(MOD_MASK_META|MOD_MASK_ALT);
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001622 }
1623 return c;
1624}
1625
1626/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 * Get the next input character.
1628 * Can return a special key or a multi-byte character.
1629 * Can return NUL when called recursively, use safe_vgetc() if that's not
1630 * wanted.
1631 * This translates escaped K_SPECIAL and CSI bytes to a K_SPECIAL or CSI byte.
1632 * Collects the bytes of a multibyte character into the whole character.
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00001633 * Returns the modifiers in the global "mod_mask".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 */
1635 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001636vgetc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637{
1638 int c, c2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001640 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001643#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01001644 // Do garbage collection when garbagecollect() was called previously and
1645 // we are now at the toplevel.
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001646 if (may_garbage_collect && want_garbage_collect)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001647 garbage_collect(FALSE);
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001648#endif
1649
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 /*
1651 * If a character was put back with vungetc, it was already processed.
1652 * Return it directly.
1653 */
1654 if (old_char != -1)
1655 {
1656 c = old_char;
1657 old_char = -1;
1658 mod_mask = old_mod_mask;
Bram Moolenaarb8978712013-03-16 21:42:16 +01001659 mouse_row = old_mouse_row;
1660 mouse_col = old_mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001662 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 {
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001664 mod_mask = 0;
1665 vgetc_mod_mask = 0;
1666 vgetc_char = 0;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001667 last_recorded_len = 0;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001668
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001669 for (;;) // this is done twice if there are modifiers
1670 {
1671 int did_inc = FALSE;
Bram Moolenaar2455c4e2015-09-15 18:29:39 +02001672
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001673 if (mod_mask
Bram Moolenaar39719052017-09-05 22:20:46 +02001674#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001675 || im_is_preediting()
Bram Moolenaar39719052017-09-05 22:20:46 +02001676#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001677#if defined(FEAT_PROP_POPUP)
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001678 || popup_no_mapping()
1679#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001680 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 {
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001682 // no mapping after modifier has been read
1683 ++no_mapping;
1684 ++allow_keys;
1685 did_inc = TRUE; // mod_mask may change value
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 }
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001687 c = vgetorpeek(TRUE);
1688 if (did_inc)
1689 {
1690 --no_mapping;
1691 --allow_keys;
1692 }
1693
1694 // Get two extra bytes for special keys
1695 if (c == K_SPECIAL
1696#ifdef FEAT_GUI
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001697 || (c == CSI)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001698#endif
1699 )
1700 {
1701 int save_allow_keys = allow_keys;
1702
1703 ++no_mapping;
1704 allow_keys = 0; // make sure BS is not found
1705 c2 = vgetorpeek(TRUE); // no mapping for these chars
1706 c = vgetorpeek(TRUE);
1707 --no_mapping;
1708 allow_keys = save_allow_keys;
1709 if (c2 == KS_MODIFIER)
1710 {
1711 mod_mask = c;
1712 continue;
1713 }
1714 c = TO_SPECIAL(c2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715
Bram Moolenaar4f974752019-02-17 17:44:42 +01001716#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001717 // Handle K_TEAROFF here, the caller of vgetc() doesn't need to
1718 // know that a menu was torn off
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001719 if (
1720# ifdef VIMDLL
1721 gui.in_use &&
1722# endif
1723 c == K_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 {
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001725 char_u name[200];
1726 int i;
1727
1728 // get menu path, it ends with a <CR>
1729 for (i = 0; (c = vgetorpeek(TRUE)) != '\r'; )
1730 {
1731 name[i] = c;
1732 if (i < 199)
1733 ++i;
1734 }
1735 name[i] = NUL;
1736 gui_make_tearoff(name);
1737 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739#endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001740#if defined(FEAT_GUI) && defined(FEAT_GUI_GTK) && defined(FEAT_MENU)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001741 // GTK: <F10> normally selects the menu, but it's passed until
1742 // here to allow mapping it. Intercept and invoke the GTK
1743 // behavior if it's not mapped.
1744 if (c == K_F10 && gui.menubar != NULL)
1745 {
1746 gtk_menu_shell_select_first(
1747 GTK_MENU_SHELL(gui.menubar), FALSE);
1748 continue;
1749 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001750#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751#ifdef FEAT_GUI
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001752 // Handle focus event here, so that the caller doesn't need to
1753 // know about it. Return K_IGNORE so that we loop once (needed
1754 // if 'lazyredraw' is set).
1755 if (c == K_FOCUSGAINED || c == K_FOCUSLOST)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001756 {
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001757 ui_focus_change(c == K_FOCUSGAINED);
1758 c = K_IGNORE;
Bram Moolenaara9dd2d32019-04-29 21:58:41 +02001759 }
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001760
1761 // Translate K_CSI to CSI. The special key is only used to
1762 // avoid it being recognized as the start of a special key.
1763 if (c == K_CSI)
1764 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001766 }
1767 // a keypad or special function key was not mapped, use it like
1768 // its ASCII equivalent
1769 switch (c)
1770 {
1771 case K_KPLUS: c = '+'; break;
1772 case K_KMINUS: c = '-'; break;
1773 case K_KDIVIDE: c = '/'; break;
1774 case K_KMULTIPLY: c = '*'; break;
1775 case K_KENTER: c = CAR; break;
1776 case K_KPOINT:
Bram Moolenaar4f974752019-02-17 17:44:42 +01001777#ifdef MSWIN
Bram Moolenaar8e85db02018-07-27 23:16:51 +02001778 // Can be either '.' or a ',',
1779 // depending on the type of keypad.
1780 c = MapVirtualKey(VK_DECIMAL, 2); break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001781#else
Bram Moolenaar8e85db02018-07-27 23:16:51 +02001782 c = '.'; break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001783#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001784 case K_K0: c = '0'; break;
1785 case K_K1: c = '1'; break;
1786 case K_K2: c = '2'; break;
1787 case K_K3: c = '3'; break;
1788 case K_K4: c = '4'; break;
1789 case K_K5: c = '5'; break;
1790 case K_K6: c = '6'; break;
1791 case K_K7: c = '7'; break;
1792 case K_K8: c = '8'; break;
1793 case K_K9: c = '9'; break;
Bram Moolenaara88d9682005-03-25 21:45:43 +00001794
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001795 case K_XHOME:
1796 case K_ZHOME: if (mod_mask == MOD_MASK_SHIFT)
Bram Moolenaara88d9682005-03-25 21:45:43 +00001797 {
1798 c = K_S_HOME;
1799 mod_mask = 0;
1800 }
1801 else if (mod_mask == MOD_MASK_CTRL)
1802 {
1803 c = K_C_HOME;
1804 mod_mask = 0;
1805 }
1806 else
1807 c = K_HOME;
1808 break;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001809 case K_XEND:
1810 case K_ZEND: if (mod_mask == MOD_MASK_SHIFT)
Bram Moolenaara88d9682005-03-25 21:45:43 +00001811 {
1812 c = K_S_END;
1813 mod_mask = 0;
1814 }
1815 else if (mod_mask == MOD_MASK_CTRL)
1816 {
1817 c = K_C_END;
1818 mod_mask = 0;
1819 }
1820 else
1821 c = K_END;
1822 break;
1823
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001824 case K_XUP: c = K_UP; break;
1825 case K_XDOWN: c = K_DOWN; break;
1826 case K_XLEFT: c = K_LEFT; break;
1827 case K_XRIGHT: c = K_RIGHT; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001830 // For a multi-byte character get all the bytes and return the
1831 // converted character.
1832 // Note: This will loop until enough bytes are received!
1833 if (has_mbyte && (n = MB_BYTE2LEN_CHECK(c)) > 1)
1834 {
1835 ++no_mapping;
1836 buf[0] = c;
1837 for (i = 1; i < n; ++i)
1838 {
1839 buf[i] = vgetorpeek(TRUE);
1840 if (buf[i] == K_SPECIAL
1841#ifdef FEAT_GUI
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001842 || (buf[i] == CSI)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001843#endif
1844 )
1845 {
1846 // Must be a K_SPECIAL - KS_SPECIAL - KE_FILLER
1847 // sequence, which represents a K_SPECIAL (0x80),
1848 // or a CSI - KS_EXTRA - KE_CSI sequence, which
1849 // represents a CSI (0x9B),
1850 // or a K_SPECIAL - KS_EXTRA - KE_CSI, which is CSI
1851 // too.
1852 c = vgetorpeek(TRUE);
1853 if (vgetorpeek(TRUE) == (int)KE_CSI && c == KS_EXTRA)
1854 buf[i] = CSI;
1855 }
1856 }
1857 --no_mapping;
1858 c = (*mb_ptr2char)(buf);
1859 }
1860
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02001861 if (vgetc_char == 0)
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001862 {
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001863 vgetc_mod_mask = mod_mask;
1864 vgetc_char = c;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001865 }
Bram Moolenaar00eab7f2019-10-10 21:49:28 +02001866
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001867 break;
1868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001870
1871#ifdef FEAT_EVAL
1872 /*
1873 * In the main loop "may_garbage_collect" can be set to do garbage
1874 * collection in the first next vgetc(). It's disabled after that to
1875 * avoid internally used Lists and Dicts to be freed.
1876 */
1877 may_garbage_collect = FALSE;
1878#endif
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001879
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001880#ifdef FEAT_BEVAL_TERM
Bram Moolenaarc2f50542019-07-05 23:24:56 +02001881 if (c != K_MOUSEMOVE && c != K_IGNORE && c != K_CURSORHOLD)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001882 {
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001883 // Don't trigger 'balloonexpr' unless only the mouse was moved.
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001884 bevalexpr_due_set = FALSE;
1885 ui_remove_balloon();
1886 }
1887#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001888#ifdef FEAT_PROP_POPUP
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001889 if (popup_do_filter(c))
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02001890 {
1891 if (c == Ctrl_C)
1892 got_int = FALSE; // avoid looping
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001893 c = K_IGNORE;
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02001894 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001895#endif
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001896
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001897 // Need to process the character before we know it's safe to do something
1898 // else.
1899 if (c != K_IGNORE)
Bram Moolenaard103ee72019-09-18 21:15:31 +02001900 state_no_longer_safe("key typed");
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001901
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001902 return c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903}
1904
1905/*
1906 * Like vgetc(), but never return a NUL when called recursively, get a key
1907 * directly from the user (ignoring typeahead).
1908 */
1909 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001910safe_vgetc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911{
1912 int c;
1913
1914 c = vgetc();
1915 if (c == NUL)
1916 c = get_keystroke();
1917 return c;
1918}
1919
1920/*
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001921 * Like safe_vgetc(), but loop to handle K_IGNORE.
1922 * Also ignore scrollbar events.
1923 */
1924 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001925plain_vgetc(void)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001926{
1927 int c;
1928
1929 do
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001930 c = safe_vgetc();
Bram Moolenaarabab0b02019-03-30 18:47:01 +01001931 while (c == K_IGNORE || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR);
Bram Moolenaarec2da362017-01-21 20:04:22 +01001932
1933 if (c == K_PS)
Bram Moolenaar30613902019-12-01 22:11:18 +01001934 // Only handle the first pasted character. Drop the rest, since we
1935 // don't know what to do with it.
Bram Moolenaarec2da362017-01-21 20:04:22 +01001936 c = bracketed_paste(PASTE_ONE_CHAR, FALSE, NULL);
1937
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001938 return c;
1939}
1940
1941/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 * Check if a character is available, such that vgetc() will not block.
1943 * If the next character is a special character or multi-byte, the returned
1944 * character is not valid!.
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02001945 * Returns NUL if no character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 */
1947 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001948vpeekc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949{
1950 if (old_char != -1)
1951 return old_char;
1952 return vgetorpeek(FALSE);
1953}
1954
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01001955#if defined(FEAT_TERMRESPONSE) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956/*
1957 * Like vpeekc(), but don't allow mapping. Do allow checking for terminal
1958 * codes.
1959 */
1960 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001961vpeekc_nomap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962{
1963 int c;
1964
1965 ++no_mapping;
1966 ++allow_keys;
1967 c = vpeekc();
1968 --no_mapping;
1969 --allow_keys;
1970 return c;
1971}
1972#endif
1973
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974/*
1975 * Check if any character is available, also half an escape sequence.
1976 * Trick: when no typeahead found, but there is something in the typeahead
1977 * buffer, it must be an ESC that is recognized as the start of a key code.
1978 */
1979 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001980vpeekc_any(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981{
1982 int c;
1983
1984 c = vpeekc();
1985 if (c == NUL && typebuf.tb_len > 0)
1986 c = ESC;
1987 return c;
1988}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989
1990/*
1991 * Call vpeekc() without causing anything to be mapped.
1992 * Return TRUE if a character is available, FALSE otherwise.
1993 */
1994 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001995char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996{
1997 int retval;
1998
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01001999#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01002000 // When test_override("char_avail", 1) was called pretend there is no
2001 // typeahead.
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01002002 if (disable_char_avail_for_testing)
2003 return FALSE;
2004#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 ++no_mapping;
2006 retval = vpeekc();
2007 --no_mapping;
2008 return (retval != NUL);
2009}
2010
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002011#if defined(FEAT_EVAL) || defined(PROTO)
2012/*
2013 * "getchar()" function
2014 */
2015 void
2016f_getchar(typval_T *argvars, typval_T *rettv)
2017{
2018 varnumber_T n;
2019 int error = FALSE;
2020
2021#ifdef MESSAGE_QUEUE
2022 // vpeekc() used to check for messages, but that caused problems, invoking
2023 // a callback where it was not expected. Some plugins use getchar(1) in a
2024 // loop to await a message, therefore make sure we check for messages here.
2025 parse_queued_messages();
2026#endif
2027
Bram Moolenaar30613902019-12-01 22:11:18 +01002028 // Position the cursor. Needed after a message that ends in a space.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002029 windgoto(msg_row, msg_col);
2030
2031 ++no_mapping;
2032 ++allow_keys;
2033 for (;;)
2034 {
2035 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar30613902019-12-01 22:11:18 +01002036 // getchar(): blocking wait.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002037 n = plain_vgetc();
2038 else if (tv_get_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar30613902019-12-01 22:11:18 +01002039 // getchar(1): only check if char avail
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002040 n = vpeekc_any();
2041 else if (error || vpeekc_any() == NUL)
Bram Moolenaar30613902019-12-01 22:11:18 +01002042 // illegal argument or getchar(0) and no char avail: return zero
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002043 n = 0;
2044 else
Bram Moolenaar30613902019-12-01 22:11:18 +01002045 // getchar(0) and char avail: return char
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002046 n = plain_vgetc();
2047
2048 if (n == K_IGNORE)
2049 continue;
2050 break;
2051 }
2052 --no_mapping;
2053 --allow_keys;
2054
2055 set_vim_var_nr(VV_MOUSE_WIN, 0);
2056 set_vim_var_nr(VV_MOUSE_WINID, 0);
2057 set_vim_var_nr(VV_MOUSE_LNUM, 0);
2058 set_vim_var_nr(VV_MOUSE_COL, 0);
2059
2060 rettv->vval.v_number = n;
2061 if (IS_SPECIAL(n) || mod_mask != 0)
2062 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002063 char_u temp[10]; // modifier: 3, mbyte-char: 6, NUL: 1
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002064 int i = 0;
2065
Bram Moolenaar30613902019-12-01 22:11:18 +01002066 // Turn a special key into three bytes, plus modifier.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002067 if (mod_mask != 0)
2068 {
2069 temp[i++] = K_SPECIAL;
2070 temp[i++] = KS_MODIFIER;
2071 temp[i++] = mod_mask;
2072 }
2073 if (IS_SPECIAL(n))
2074 {
2075 temp[i++] = K_SPECIAL;
2076 temp[i++] = K_SECOND(n);
2077 temp[i++] = K_THIRD(n);
2078 }
2079 else if (has_mbyte)
2080 i += (*mb_char2bytes)(n, temp + i);
2081 else
2082 temp[i++] = n;
2083 temp[i++] = NUL;
2084 rettv->v_type = VAR_STRING;
2085 rettv->vval.v_string = vim_strsave(temp);
2086
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002087 if (is_mouse_key(n))
2088 {
2089 int row = mouse_row;
2090 int col = mouse_col;
2091 win_T *win;
2092 linenr_T lnum;
2093 win_T *wp;
2094 int winnr = 1;
2095
2096 if (row >= 0 && col >= 0)
2097 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002098 // Find the window at the mouse coordinates and compute the
2099 // text position.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002100 win = mouse_find_win(&row, &col, FIND_POPUP);
2101 if (win == NULL)
2102 return;
2103 (void)mouse_comp_pos(win, &row, &col, &lnum, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002104#ifdef FEAT_PROP_POPUP
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002105 if (WIN_IS_POPUP(win))
2106 winnr = 0;
2107 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002108#endif
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002109 for (wp = firstwin; wp != win && wp != NULL;
2110 wp = wp->w_next)
2111 ++winnr;
2112 set_vim_var_nr(VV_MOUSE_WIN, winnr);
2113 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
2114 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
2115 set_vim_var_nr(VV_MOUSE_COL, col + 1);
2116 }
2117 }
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002118 }
2119}
2120
2121/*
2122 * "getcharmod()" function
2123 */
2124 void
2125f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
2126{
2127 rettv->vval.v_number = mod_mask;
2128}
2129#endif // FEAT_EVAL
2130
2131#if defined(MESSAGE_QUEUE) || defined(PROTO)
2132# define MAX_REPEAT_PARSE 8
2133
2134/*
2135 * Process messages that have been queued for netbeans or clientserver.
2136 * Also check if any jobs have ended.
2137 * These functions can call arbitrary vimscript and should only be called when
2138 * it is safe to do so.
2139 */
2140 void
2141parse_queued_messages(void)
2142{
Bram Moolenaar1cac7092019-10-22 21:54:31 +02002143 int old_curwin_id;
2144 int old_curbuf_fnum;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002145 int i;
2146 int save_may_garbage_collect = may_garbage_collect;
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002147 static int entered = 0;
Bram Moolenaard103ee72019-09-18 21:15:31 +02002148 int was_safe = get_was_safe_state();
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002149
2150 // Do not handle messages while redrawing, because it may cause buffers to
2151 // change or be wiped while they are being redrawn.
2152 if (updating_screen)
2153 return;
2154
Bram Moolenaar1cac7092019-10-22 21:54:31 +02002155 // If memory allocation fails during startup we'll exit but curbuf or
2156 // curwin could be NULL.
2157 if (curbuf == NULL || curwin == NULL)
2158 return;
2159
2160 old_curbuf_fnum = curbuf->b_fnum;
2161 old_curwin_id = curwin->w_id;
2162
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002163 ++entered;
2164
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002165 // may_garbage_collect is set in main_loop() to do garbage collection when
2166 // blocking to wait on a character. We don't want that while parsing
2167 // messages, a callback may invoke vgetc() while lists and dicts are in use
2168 // in the call stack.
2169 may_garbage_collect = FALSE;
2170
2171 // Loop when a job ended, but don't keep looping forever.
2172 for (i = 0; i < MAX_REPEAT_PARSE; ++i)
2173 {
2174 // For Win32 mch_breakcheck() does not check for input, do it here.
Bram Moolenaar80a8d382020-05-03 22:57:32 +02002175# if (defined(MSWIN) || defined(__HAIKU__)) && defined(FEAT_JOB_CHANNEL)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002176 channel_handle_events(FALSE);
2177# endif
2178
2179# ifdef FEAT_NETBEANS_INTG
2180 // Process the queued netbeans messages.
2181 netbeans_parse_messages();
2182# endif
2183# ifdef FEAT_JOB_CHANNEL
2184 // Write any buffer lines still to be written.
2185 channel_write_any_lines();
2186
2187 // Process the messages queued on channels.
2188 channel_parse_messages();
2189# endif
2190# if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
2191 // Process the queued clientserver messages.
2192 server_parse_messages();
2193# endif
2194# ifdef FEAT_JOB_CHANNEL
2195 // Check if any jobs have ended. If so, repeat the above to handle
2196 // changes, e.g. stdin may have been closed.
2197 if (job_check_ended())
2198 continue;
2199# endif
2200# ifdef FEAT_TERMINAL
2201 free_unused_terminals();
2202# endif
2203# ifdef FEAT_SOUND_CANBERRA
2204 if (has_sound_callback_in_queue())
2205 invoke_sound_callback();
2206# endif
2207 break;
2208 }
2209
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002210 // When not nested we'll go back to waiting for a typed character. If it
2211 // was safe before then this triggers a SafeStateAgain autocommand event.
Bram Moolenaard103ee72019-09-18 21:15:31 +02002212 if (entered == 1 && was_safe)
Bram Moolenaar37d18072019-09-17 20:28:38 +02002213 may_trigger_safestateagain();
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002214
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002215 may_garbage_collect = save_may_garbage_collect;
2216
2217 // If the current window or buffer changed we need to bail out of the
2218 // waiting loop. E.g. when a job exit callback closes the terminal window.
2219 if (curwin->w_id != old_curwin_id || curbuf->b_fnum != old_curbuf_fnum)
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02002220 ins_char_typebuf(K_IGNORE, 0);
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002221
2222 --entered;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002223}
2224#endif
2225
2226
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002227typedef enum {
2228 map_result_fail, // failed, break loop
2229 map_result_get, // get a character from typeahead
2230 map_result_retry, // try to map again
2231 map_result_nomatch // no matching mapping, get char
2232} map_result_T;
2233
2234/*
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002235 * Check if the bytes at the start of the typeahead buffer are a character used
2236 * in CTRL-X mode. This includes the form with a CTRL modifier.
2237 */
2238 static int
2239at_ctrl_x_key(void)
2240{
2241 char_u *p = typebuf.tb_buf + typebuf.tb_off;
2242 int c = *p;
2243
2244 if (typebuf.tb_len > 3
2245 && c == K_SPECIAL
2246 && p[1] == KS_MODIFIER
2247 && (p[2] & MOD_MASK_CTRL))
2248 c = p[3] & 0x1f;
2249 return vim_is_ctrl_x_key(c);
2250}
2251
2252/*
Bram Moolenaar975a8802020-06-06 22:36:24 +02002253 * Check if typebuf.tb_buf[] contains a modifer plus key that can be changed
2254 * into just a key, apply that.
2255 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
2256 * + "max_offset"].
2257 * Return the length of the replaced bytes, zero if nothing changed.
2258 */
2259 static int
2260check_simplify_modifier(int max_offset)
2261{
2262 int offset;
2263 char_u *tp;
2264
2265 for (offset = 0; offset < max_offset; ++offset)
2266 {
2267 if (offset + 3 >= typebuf.tb_len)
2268 break;
2269 tp = typebuf.tb_buf + typebuf.tb_off + offset;
2270 if (tp[0] == K_SPECIAL && tp[1] == KS_MODIFIER)
2271 {
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002272 // A modifier was not used for a mapping, apply it to ASCII keys.
2273 // Shift would already have been applied.
Bram Moolenaar975a8802020-06-06 22:36:24 +02002274 int modifier = tp[2];
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002275 int c = tp[3];
2276 int new_c = merge_modifyOtherKeys(c, &modifier);
Bram Moolenaar975a8802020-06-06 22:36:24 +02002277
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002278 if (new_c != c)
Bram Moolenaar975a8802020-06-06 22:36:24 +02002279 {
2280 char_u new_string[MB_MAXBYTES];
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002281 int len;
Bram Moolenaar975a8802020-06-06 22:36:24 +02002282
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002283 if (offset == 0)
2284 {
2285 // At the start: remember the character and mod_mask before
2286 // merging, in some cases, e.g. at the hit-return prompt,
2287 // they are put back in the typeahead buffer.
2288 vgetc_char = c;
2289 vgetc_mod_mask = tp[2];
2290 }
2291 len = mb_char2bytes(new_c, new_string);
2292 if (modifier == 0)
2293 {
2294 if (put_string_in_typebuf(offset, 4, new_string, len,
Bram Moolenaar975a8802020-06-06 22:36:24 +02002295 NULL, 0, 0) == FAIL)
2296 return -1;
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002297 }
2298 else
2299 {
2300 tp[2] = modifier;
2301 if (put_string_in_typebuf(offset + 3, 1, new_string, len,
2302 NULL, 0, 0) == FAIL)
2303 return -1;
2304 }
Bram Moolenaar975a8802020-06-06 22:36:24 +02002305 return len;
2306 }
2307 }
2308 }
2309 return 0;
2310}
2311
2312/*
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002313 * Handle mappings in the typeahead buffer.
2314 * - When something was mapped, return map_result_retry for recursive mappings.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002315 * - When nothing mapped and typeahead has a character: return map_result_get.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002316 * - When there is no match yet, return map_result_nomatch, need to get more
2317 * typeahead.
2318 */
2319 static int
2320handle_mapping(
2321 int *keylenp,
2322 int *timedout,
2323 int *mapdepth)
2324{
2325 mapblock_T *mp = NULL;
2326 mapblock_T *mp2;
2327 mapblock_T *mp_match;
2328 int mp_match_len = 0;
2329 int max_mlen = 0;
2330 int tb_c1;
2331 int mlen;
2332#ifdef FEAT_LANGMAP
2333 int nolmaplen;
2334#endif
2335 int keylen = *keylenp;
2336 int i;
2337 int local_State = get_real_state();
2338
2339 /*
2340 * Check for a mappable key sequence.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002341 * Walk through one maphash[] list until we find an entry that matches.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002342 *
2343 * Don't look for mappings if:
2344 * - no_mapping set: mapping disabled (e.g. for CTRL-V)
2345 * - maphash_valid not set: no mappings present.
2346 * - typebuf.tb_buf[typebuf.tb_off] should not be remapped
2347 * - in insert or cmdline mode and 'paste' option set
Bram Moolenaareda35f72019-08-03 14:59:44 +02002348 * - waiting for "hit return to continue" and CR or SPACE typed
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002349 * - waiting for a char with --more--
2350 * - in Ctrl-X mode, and we get a valid char for that mode
2351 */
2352 tb_c1 = typebuf.tb_buf[typebuf.tb_off];
2353 if (no_mapping == 0 && is_maphash_valid()
2354 && (no_zero_mapping == 0 || tb_c1 != '0')
2355 && (typebuf.tb_maplen == 0
2356 || (p_remap
2357 && (typebuf.tb_noremap[typebuf.tb_off]
2358 & (RM_NONE|RM_ABBR)) == 0))
2359 && !(p_paste && (State & (INSERT + CMDLINE)))
2360 && !(State == HITRETURN && (tb_c1 == CAR || tb_c1 == ' '))
2361 && State != ASKMORE
2362 && State != CONFIRM
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002363 && !((ctrl_x_mode_not_default() && at_ctrl_x_key())
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002364 || ((compl_cont_status & CONT_LOCAL)
Bram Moolenaare2c453d2019-08-21 14:37:09 +02002365 && (tb_c1 == Ctrl_N || tb_c1 == Ctrl_P))))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002366 {
Bram Moolenaarc9983702020-05-28 21:03:53 +02002367#ifdef FEAT_GUI
2368 if (gui.in_use && tb_c1 == CSI && typebuf.tb_len >= 2
2369 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_MODIFIER)
2370 {
2371 // The GUI code sends CSI KS_MODIFIER {flags}, but mappings expect
2372 // K_SPECIAL KS_MODIFIER {flags}.
2373 tb_c1 = K_SPECIAL;
2374 }
2375#endif
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002376#ifdef FEAT_LANGMAP
2377 if (tb_c1 == K_SPECIAL)
2378 nolmaplen = 2;
2379 else
2380 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002381 LANGMAP_ADJUST(tb_c1, (State & (CMDLINE | INSERT)) == 0
2382 && get_real_state() != SELECTMODE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002383 nolmaplen = 0;
2384 }
2385#endif
2386 // First try buffer-local mappings.
2387 mp = get_buf_maphash_list(local_State, tb_c1);
2388 mp2 = get_maphash_list(local_State, tb_c1);
2389 if (mp == NULL)
2390 {
2391 // There are no buffer-local mappings.
2392 mp = mp2;
2393 mp2 = NULL;
2394 }
Bram Moolenaareda35f72019-08-03 14:59:44 +02002395
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002396 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002397 * Loop until a partly matching mapping is found or all (local)
2398 * mappings have been checked.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002399 * The longest full match is remembered in "mp_match".
Bram Moolenaareda35f72019-08-03 14:59:44 +02002400 * A full match is only accepted if there is no partly match, so "aa"
2401 * and "aaa" can both be mapped.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002402 */
2403 mp_match = NULL;
2404 mp_match_len = 0;
2405 for ( ; mp != NULL;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002406 mp->m_next == NULL ? (mp = mp2, mp2 = NULL) : (mp = mp->m_next))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002407 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002408 // Only consider an entry if the first character matches and it is
2409 // for the current state.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002410 // Skip ":lmap" mappings if keys were mapped.
2411 if (mp->m_keys[0] == tb_c1
2412 && (mp->m_mode & local_State)
Bram Moolenaar46cd43b2020-06-04 22:22:11 +02002413 && !(mp->m_simplified && seenModifyOtherKeys
2414 && typebuf.tb_maplen == 0)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002415 && ((mp->m_mode & LANGMAP) == 0 || typebuf.tb_maplen == 0))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002416 {
2417#ifdef FEAT_LANGMAP
2418 int nomap = nolmaplen;
2419 int c2;
2420#endif
2421 // find the match length of this mapping
2422 for (mlen = 1; mlen < typebuf.tb_len; ++mlen)
2423 {
2424#ifdef FEAT_LANGMAP
2425 c2 = typebuf.tb_buf[typebuf.tb_off + mlen];
2426 if (nomap > 0)
2427 --nomap;
2428 else if (c2 == K_SPECIAL)
2429 nomap = 2;
2430 else
2431 LANGMAP_ADJUST(c2, TRUE);
2432 if (mp->m_keys[mlen] != c2)
2433#else
2434 if (mp->m_keys[mlen] !=
Bram Moolenaarc9983702020-05-28 21:03:53 +02002435 typebuf.tb_buf[typebuf.tb_off + mlen])
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002436#endif
2437 break;
2438 }
2439
Bram Moolenaareda35f72019-08-03 14:59:44 +02002440 // Don't allow mapping the first byte(s) of a multi-byte char.
2441 // Happens when mapping <M-a> and then changing 'encoding'.
2442 // Beware that 0x80 is escaped.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002443 {
2444 char_u *p1 = mp->m_keys;
2445 char_u *p2 = mb_unescape(&p1);
2446
2447 if (has_mbyte && p2 != NULL
Bram Moolenaar1614a142019-10-06 22:00:13 +02002448 && MB_BYTE2LEN(tb_c1) > mb_ptr2len(p2))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002449 mlen = 0;
2450 }
2451
2452 // Check an entry whether it matches.
2453 // - Full match: mlen == keylen
2454 // - Partly match: mlen == typebuf.tb_len
2455 keylen = mp->m_keylen;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002456 if (mlen == keylen || (mlen == typebuf.tb_len
2457 && typebuf.tb_len < keylen))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002458 {
2459 char_u *s;
2460 int n;
2461
Bram Moolenaareda35f72019-08-03 14:59:44 +02002462 // If only script-local mappings are allowed, check if the
2463 // mapping starts with K_SNR.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002464 s = typebuf.tb_noremap + typebuf.tb_off;
2465 if (*s == RM_SCRIPT
2466 && (mp->m_keys[0] != K_SPECIAL
2467 || mp->m_keys[1] != KS_EXTRA
Bram Moolenaareda35f72019-08-03 14:59:44 +02002468 || mp->m_keys[2] != (int)KE_SNR))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002469 continue;
2470
Bram Moolenaareda35f72019-08-03 14:59:44 +02002471 // If one of the typed keys cannot be remapped, skip the
2472 // entry.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002473 for (n = mlen; --n >= 0; )
2474 if (*s++ & (RM_NONE|RM_ABBR))
2475 break;
2476 if (n >= 0)
2477 continue;
2478
2479 if (keylen > typebuf.tb_len)
2480 {
2481 if (!*timedout && !(mp_match != NULL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002482 && mp_match->m_nowait))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002483 {
2484 // break at a partly match
2485 keylen = KEYLEN_PART_MAP;
2486 break;
2487 }
2488 }
2489 else if (keylen > mp_match_len)
2490 {
2491 // found a longer match
2492 mp_match = mp;
2493 mp_match_len = keylen;
2494 }
2495 }
2496 else
Bram Moolenaareda35f72019-08-03 14:59:44 +02002497 // No match; may have to check for termcode at next
2498 // character.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002499 if (max_mlen < mlen)
2500 max_mlen = mlen;
2501 }
2502 }
2503
Bram Moolenaareda35f72019-08-03 14:59:44 +02002504 // If no partly match found, use the longest full match.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002505 if (keylen != KEYLEN_PART_MAP)
2506 {
2507 mp = mp_match;
2508 keylen = mp_match_len;
2509 }
2510 }
2511
2512 /*
2513 * Check for match with 'pastetoggle'
2514 */
2515 if (*p_pt != NUL && mp == NULL && (State & (INSERT|NORMAL)))
2516 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002517 for (mlen = 0; mlen < typebuf.tb_len && p_pt[mlen]; ++mlen)
2518 if (p_pt[mlen] != typebuf.tb_buf[typebuf.tb_off + mlen])
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002519 break;
2520 if (p_pt[mlen] == NUL) // match
2521 {
2522 // write chars to script file(s)
2523 if (mlen > typebuf.tb_maplen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002524 gotchars(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_maplen,
2525 mlen - typebuf.tb_maplen);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002526
2527 del_typebuf(mlen, 0); // remove the chars
Bram Moolenaareda35f72019-08-03 14:59:44 +02002528 set_option_value((char_u *)"paste", (long)!p_paste, NULL, 0);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002529 if (!(State & INSERT))
2530 {
2531 msg_col = 0;
2532 msg_row = Rows - 1;
2533 msg_clr_eos(); // clear ruler
2534 }
2535 status_redraw_all();
2536 redraw_statuslines();
2537 showmode();
2538 setcursor();
2539 *keylenp = keylen;
2540 return map_result_retry;
2541 }
2542 // Need more chars for partly match.
2543 if (mlen == typebuf.tb_len)
2544 keylen = KEYLEN_PART_KEY;
2545 else if (max_mlen < mlen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002546 // no match, may have to check for termcode at next character
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002547 max_mlen = mlen + 1;
2548 }
2549
Bram Moolenaareda35f72019-08-03 14:59:44 +02002550 if ((mp == NULL || max_mlen >= mp_match_len) && keylen != KEYLEN_PART_MAP)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002551 {
2552 int save_keylen = keylen;
2553
2554 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002555 * When no matching mapping found or found a non-matching mapping that
2556 * matches at least what the matching mapping matched:
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002557 * Check if we have a terminal code, when:
Bram Moolenaareda35f72019-08-03 14:59:44 +02002558 * - mapping is allowed,
2559 * - keys have not been mapped,
2560 * - and not an ESC sequence, not in insert mode or p_ek is on,
2561 * - and when not timed out,
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002562 */
2563 if ((no_mapping == 0 || allow_keys != 0)
2564 && (typebuf.tb_maplen == 0
2565 || (p_remap && typebuf.tb_noremap[
Bram Moolenaareda35f72019-08-03 14:59:44 +02002566 typebuf.tb_off] == RM_YES))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002567 && !*timedout)
2568 {
2569 keylen = check_termcode(max_mlen + 1,
2570 NULL, 0, NULL);
2571
Bram Moolenaareda35f72019-08-03 14:59:44 +02002572 // If no termcode matched but 'pastetoggle' matched partially it's
2573 // like an incomplete key sequence.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002574 if (keylen == 0 && save_keylen == KEYLEN_PART_KEY)
2575 keylen = KEYLEN_PART_KEY;
2576
Bram Moolenaar975a8802020-06-06 22:36:24 +02002577 // If no termcode matched, try to include the modifier into the
2578 // key. This for when modifyOtherKeys is working.
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002579 if (keylen == 0 && !no_reduce_keys)
Bram Moolenaar975a8802020-06-06 22:36:24 +02002580 keylen = check_simplify_modifier(max_mlen + 1);
2581
Bram Moolenaareda35f72019-08-03 14:59:44 +02002582 // When getting a partial match, but the last characters were not
2583 // typed, don't wait for a typed character to complete the
2584 // termcode. This helps a lot when a ":normal" command ends in an
2585 // ESC.
2586 if (keylen < 0 && typebuf.tb_len == typebuf.tb_maplen)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002587 keylen = 0;
2588 }
2589 else
2590 keylen = 0;
2591 if (keylen == 0) // no matching terminal code
2592 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002593#ifdef AMIGA
2594 // check for window bounds report
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002595 if (typebuf.tb_maplen == 0 && (typebuf.tb_buf[
Bram Moolenaareda35f72019-08-03 14:59:44 +02002596 typebuf.tb_off] & 0xff) == CSI)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002597 {
2598 char_u *s;
2599
2600 for (s = typebuf.tb_buf + typebuf.tb_off + 1;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002601 s < typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len
2602 && (VIM_ISDIGIT(*s) || *s == ';' || *s == ' ');
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002603 ++s)
2604 ;
2605 if (*s == 'r' || *s == '|') // found one
2606 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002607 del_typebuf(
2608 (int)(s + 1 - (typebuf.tb_buf + typebuf.tb_off)), 0);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002609 // get size and redraw screen
2610 shell_resized();
2611 *keylenp = keylen;
2612 return map_result_retry;
2613 }
2614 if (*s == NUL) // need more characters
2615 keylen = KEYLEN_PART_KEY;
2616 }
2617 if (keylen >= 0)
2618#endif
Bram Moolenaareda35f72019-08-03 14:59:44 +02002619 // When there was a matching mapping and no termcode could be
2620 // replaced after another one, use that mapping (loop around).
2621 // If there was no mapping at all use the character from the
2622 // typeahead buffer right here.
2623 if (mp == NULL)
2624 {
2625 *keylenp = keylen;
2626 return map_result_get; // got character, break for loop
2627 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002628 }
2629
2630 if (keylen > 0) // full matching terminal code
2631 {
2632#if defined(FEAT_GUI) && defined(FEAT_MENU)
2633 if (typebuf.tb_len >= 2
Bram Moolenaareda35f72019-08-03 14:59:44 +02002634 && typebuf.tb_buf[typebuf.tb_off] == K_SPECIAL
2635 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_MENU)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002636 {
2637 int idx;
2638
Bram Moolenaareda35f72019-08-03 14:59:44 +02002639 // Using a menu may cause a break in undo! It's like using
2640 // gotchars(), but without recording or writing to a script
2641 // file.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002642 may_sync_undo();
2643 del_typebuf(3, 0);
2644 idx = get_menu_index(current_menu, local_State);
2645 if (idx != MENU_INDEX_INVALID)
2646 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002647 // In Select mode and a Visual mode menu is used: Switch
2648 // to Visual mode temporarily. Append K_SELECT to switch
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002649 // back to Select mode.
2650 if (VIsual_active && VIsual_select
Bram Moolenaareda35f72019-08-03 14:59:44 +02002651 && (current_menu->modes & VISUAL))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002652 {
2653 VIsual_select = FALSE;
2654 (void)ins_typebuf(K_SELECT_STRING,
Bram Moolenaareda35f72019-08-03 14:59:44 +02002655 REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002656 }
2657 ins_typebuf(current_menu->strings[idx],
2658 current_menu->noremap[idx],
Bram Moolenaareda35f72019-08-03 14:59:44 +02002659 0, TRUE, current_menu->silent[idx]);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002660 }
2661 }
2662#endif // FEAT_GUI && FEAT_MENU
2663 *keylenp = keylen;
2664 return map_result_retry; // try mapping again
2665 }
2666
Bram Moolenaareda35f72019-08-03 14:59:44 +02002667 // Partial match: get some more characters. When a matching mapping
2668 // was found use that one.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002669 if (mp == NULL || keylen < 0)
2670 keylen = KEYLEN_PART_KEY;
2671 else
2672 keylen = mp_match_len;
2673 }
2674
2675 /*
2676 * complete match
2677 */
2678 if (keylen >= 0 && keylen <= typebuf.tb_len)
2679 {
2680 char_u *map_str;
2681
2682#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002683 int save_m_expr;
2684 int save_m_noremap;
2685 int save_m_silent;
2686 char_u *save_m_keys;
2687 char_u *save_m_str;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002688#else
2689# define save_m_noremap mp->m_noremap
2690# define save_m_silent mp->m_silent
2691#endif
2692
2693 // write chars to script file(s)
2694 if (keylen > typebuf.tb_maplen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002695 gotchars(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_maplen,
2696 keylen - typebuf.tb_maplen);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002697
2698 cmd_silent = (typebuf.tb_silent > 0);
2699 del_typebuf(keylen, 0); // remove the mapped keys
2700
2701 /*
2702 * Put the replacement string in front of mapstr.
2703 * The depth check catches ":map x y" and ":map y x".
2704 */
2705 if (++*mapdepth >= p_mmd)
2706 {
2707 emsg(_("E223: recursive mapping"));
2708 if (State & CMDLINE)
2709 redrawcmdline();
2710 else
2711 setcursor();
2712 flush_buffers(FLUSH_MINIMAL);
Bram Moolenaar30613902019-12-01 22:11:18 +01002713 *mapdepth = 0; // for next one
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002714 *keylenp = keylen;
2715 return map_result_fail;
2716 }
2717
2718 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002719 * In Select mode and a Visual mode mapping is used: Switch to Visual
2720 * mode temporarily. Append K_SELECT to switch back to Select mode.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002721 */
Bram Moolenaareda35f72019-08-03 14:59:44 +02002722 if (VIsual_active && VIsual_select && (mp->m_mode & VISUAL))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002723 {
2724 VIsual_select = FALSE;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002725 (void)ins_typebuf(K_SELECT_STRING, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002726 }
2727
2728#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002729 // Copy the values from *mp that are used, because evaluating the
2730 // expression may invoke a function that redefines the mapping, thereby
2731 // making *mp invalid.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002732 save_m_expr = mp->m_expr;
2733 save_m_noremap = mp->m_noremap;
2734 save_m_silent = mp->m_silent;
2735 save_m_keys = NULL; // only saved when needed
2736 save_m_str = NULL; // only saved when needed
2737
2738 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002739 * Handle ":map <expr>": evaluate the {rhs} as an expression. Also
2740 * save and restore the command line for "normal :".
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002741 */
2742 if (mp->m_expr)
2743 {
2744 int save_vgetc_busy = vgetc_busy;
2745 int save_may_garbage_collect = may_garbage_collect;
Bram Moolenaar4ebe0e62019-11-22 20:55:40 +01002746 int was_screen_col = screen_cur_col;
2747 int was_screen_row = screen_cur_row;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002748
2749 vgetc_busy = 0;
2750 may_garbage_collect = FALSE;
2751
2752 save_m_keys = vim_strsave(mp->m_keys);
2753 save_m_str = vim_strsave(mp->m_str);
2754 map_str = eval_map_expr(save_m_str, NUL);
2755
Bram Moolenaar4ebe0e62019-11-22 20:55:40 +01002756 // The mapping may do anything, but we expect it to take care of
2757 // redrawing. Do put the cursor back where it was.
2758 windgoto(was_screen_row, was_screen_col);
2759 out_flush();
2760
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002761 vgetc_busy = save_vgetc_busy;
2762 may_garbage_collect = save_may_garbage_collect;
2763 }
2764 else
2765#endif
2766 map_str = mp->m_str;
2767
2768 /*
2769 * Insert the 'to' part in the typebuf.tb_buf.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002770 * If 'from' field is the same as the start of the 'to' field, don't
2771 * remap the first character (but do allow abbreviations).
2772 * If m_noremap is set, don't remap the whole 'to' part.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002773 */
2774 if (map_str == NULL)
2775 i = FAIL;
2776 else
2777 {
2778 int noremap;
2779
2780 if (save_m_noremap != REMAP_YES)
2781 noremap = save_m_noremap;
2782 else if (
2783#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002784 STRNCMP(map_str, save_m_keys != NULL ? save_m_keys : mp->m_keys,
2785 (size_t)keylen)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002786#else
2787 STRNCMP(map_str, mp->m_keys, (size_t)keylen)
2788#endif
2789 != 0)
2790 noremap = REMAP_YES;
2791 else
2792 noremap = REMAP_SKIP;
2793 i = ins_typebuf(map_str, noremap,
2794 0, TRUE, cmd_silent || save_m_silent);
2795#ifdef FEAT_EVAL
2796 if (save_m_expr)
2797 vim_free(map_str);
2798#endif
2799 }
2800#ifdef FEAT_EVAL
2801 vim_free(save_m_keys);
2802 vim_free(save_m_str);
2803#endif
2804 *keylenp = keylen;
2805 if (i == FAIL)
2806 return map_result_fail;
2807 return map_result_retry;
2808 }
2809
2810 *keylenp = keylen;
2811 return map_result_nomatch;
2812}
2813
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002814/*
2815 * unget one character (can only be done once!)
2816 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002818vungetc(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819{
2820 old_char = c;
2821 old_mod_mask = mod_mask;
Bram Moolenaarb8978712013-03-16 21:42:16 +01002822 old_mouse_row = mouse_row;
2823 old_mouse_col = mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824}
2825
2826/*
Bram Moolenaar32526b32019-01-19 17:43:09 +01002827 * Get a byte:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 * 1. from the stuffbuffer
2829 * This is used for abbreviated commands like "D" -> "d$".
2830 * Also used to redo a command for ".".
2831 * 2. from the typeahead buffer
2832 * Stores text obtained previously but not used yet.
2833 * Also stores the result of mappings.
2834 * Also used for the ":normal" command.
2835 * 3. from the user
2836 * This may do a blocking wait if "advance" is TRUE.
2837 *
2838 * if "advance" is TRUE (vgetc()):
Bram Moolenaard29459b2016-08-26 22:29:11 +02002839 * Really get the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 * KeyTyped is set to TRUE in the case the user typed the key.
2841 * KeyStuffed is TRUE if the character comes from the stuff buffer.
2842 * if "advance" is FALSE (vpeekc()):
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02002843 * Just look whether there is a character available.
2844 * Return NUL if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 *
2846 * When "no_mapping" is zero, checks for mappings in the current mode.
2847 * Only returns one byte (of a multi-byte character).
2848 * K_SPECIAL and CSI may be escaped, need to get two more bytes then.
2849 */
2850 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002851vgetorpeek(int advance)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852{
2853 int c, c1;
Bram Moolenaar30613902019-12-01 22:11:18 +01002854 int timedout = FALSE; // waited for more than 1 second
2855 // for mapping to complete
2856 int mapdepth = 0; // check for recursive mapping
2857 int mode_deleted = FALSE; // set when mode has been deleted
Bram Moolenaar4e427192006-03-10 21:34:27 +00002858#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 int new_wcol, new_wrow;
2860#endif
2861#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01002862 int shape_changed = FALSE; // adjusted cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863#endif
2864 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 int old_wcol, old_wrow;
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00002866 int wait_tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867
2868 /*
2869 * This function doesn't work very well when called recursively. This may
2870 * happen though, because of:
2871 * 1. The call to add_to_showcmd(). char_avail() is then used to check if
2872 * there is a character available, which calls this function. In that
2873 * case we must return NUL, to indicate no character is available.
2874 * 2. A GUI callback function writes to the screen, causing a
2875 * wait_return().
2876 * Using ":normal" can also do this, but it saves the typeahead buffer,
2877 * thus it should be OK. But don't get a key from the user then.
2878 */
Bram Moolenaare2c38102016-01-31 14:55:40 +01002879 if (vgetc_busy > 0 && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 return NUL;
2881
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002882 ++vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883
2884 if (advance)
2885 KeyStuffed = FALSE;
2886
2887 init_typebuf();
2888 start_stuff();
2889 if (advance && typebuf.tb_maplen == 0)
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02002890 reg_executing = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 do
2892 {
2893/*
2894 * get a character: 1. from the stuffbuffer
2895 */
2896 if (typeahead_char != 0)
2897 {
2898 c = typeahead_char;
2899 if (advance)
2900 typeahead_char = 0;
2901 }
2902 else
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01002903 c = read_readbuffers(advance);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 if (c != NUL && !got_int)
2905 {
2906 if (advance)
2907 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002908 // KeyTyped = FALSE; When the command that stuffed something
2909 // was typed, behave like the stuffed command was typed.
2910 // needed for CTRL-W CTRL-] to open a fold, for example.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 KeyStuffed = TRUE;
2912 }
2913 if (typebuf.tb_no_abbr_cnt == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01002914 typebuf.tb_no_abbr_cnt = 1; // no abbreviations now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 }
2916 else
2917 {
2918 /*
2919 * Loop until we either find a matching mapped key, or we
2920 * are sure that it is not a mapped key.
2921 * If a mapped key sequence is found we go back to the start to
2922 * try re-mapping.
2923 */
2924 for (;;)
2925 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002926 long wait_time;
2927 int keylen = 0;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002928#ifdef FEAT_CMDL_INFO
2929 int showcmd_idx;
2930#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 /*
2932 * ui_breakcheck() is slow, don't use it too often when
2933 * inside a mapping. But call it each time for typed
2934 * characters.
2935 */
2936 if (typebuf.tb_maplen)
2937 line_breakcheck();
2938 else
Bram Moolenaar30613902019-12-01 22:11:18 +01002939 ui_breakcheck(); // check for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 if (got_int)
2941 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002942 // flush all input
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02002943 c = inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 0L);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002944
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 /*
2946 * If inchar() returns TRUE (script file was active) or we
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002947 * are inside a mapping, get out of Insert mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 * Otherwise we behave like having gotten a CTRL-C.
2949 * As a result typing CTRL-C in insert mode will
2950 * really insert a CTRL-C.
2951 */
2952 if ((c || typebuf.tb_maplen)
2953 && (State & (INSERT + CMDLINE)))
2954 c = ESC;
2955 else
2956 c = Ctrl_C;
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02002957 flush_buffers(FLUSH_INPUT); // flush all typeahead
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958
Bram Moolenaard9dfd572006-10-03 13:36:13 +00002959 if (advance)
2960 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002961 // Also record this character, it might be needed to
2962 // get out of Insert mode.
Bram Moolenaard9dfd572006-10-03 13:36:13 +00002963 *typebuf.tb_buf = c;
2964 gotchars(typebuf.tb_buf, 1);
2965 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 cmd_silent = FALSE;
2967
2968 break;
2969 }
2970 else if (typebuf.tb_len > 0)
2971 {
2972 /*
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002973 * Check for a mapping in "typebuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 */
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002975 map_result_T result = handle_mapping(
2976 &keylen, &timedout, &mapdepth);
2977
2978 if (result == map_result_retry)
2979 // try mapping again
2980 continue;
2981 if (result == map_result_fail)
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02002982 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002983 // failed, use the outer loop
2984 c = -1;
2985 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002987 if (result == map_result_get)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989/*
2990 * get a character: 2. from the typeahead buffer
2991 */
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002992 c = typebuf.tb_buf[typebuf.tb_off];
Bram Moolenaar30613902019-12-01 22:11:18 +01002993 if (advance) // remove chars from tb_buf
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02002994 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002995 cmd_silent = (typebuf.tb_silent > 0);
2996 if (typebuf.tb_maplen > 0)
2997 KeyTyped = FALSE;
2998 else
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02002999 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003000 KeyTyped = TRUE;
Bram Moolenaar30613902019-12-01 22:11:18 +01003001 // write char to script file(s)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003002 gotchars(typebuf.tb_buf
3003 + typebuf.tb_off, 1);
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003004 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003005 KeyNoremap = typebuf.tb_noremap[
3006 typebuf.tb_off];
3007 del_typebuf(1, 0);
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003008 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003009 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 }
3011
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003012 // not enough characters, get more
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 }
3014
3015/*
3016 * get a character: 3. from the user - handle <Esc> in Insert mode
3017 */
3018 /*
Bram Moolenaarf085f422017-06-07 20:39:47 +02003019 * Special case: if we get an <ESC> in insert mode and there
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 * are no more characters at once, we pretend to go out of
3021 * insert mode. This prevents the one second delay after
3022 * typing an <ESC>. If we get something after all, we may
3023 * have to redisplay the mode. That the cursor is in the wrong
3024 * place does not matter.
3025 */
3026 c = 0;
3027#ifdef FEAT_CMDL_INFO
3028 new_wcol = curwin->w_wcol;
3029 new_wrow = curwin->w_wrow;
3030#endif
3031 if ( advance
3032 && typebuf.tb_len == 1
3033 && typebuf.tb_buf[typebuf.tb_off] == ESC
3034 && !no_mapping
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 && ex_normal_busy == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 && typebuf.tb_maplen == 0
3037 && (State & INSERT)
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003038 && (p_timeout
3039 || (keylen == KEYLEN_PART_KEY && p_ttimeout))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 && (c = inchar(typebuf.tb_buf + typebuf.tb_off
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003041 + typebuf.tb_len, 3, 25L)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 {
3043 colnr_T col = 0, vcol;
3044 char_u *ptr;
3045
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003046 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 {
3048 unshowmode(TRUE);
3049 mode_deleted = TRUE;
3050 }
3051#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003052 // may show a different cursor shape
Bram Moolenaarf085f422017-06-07 20:39:47 +02003053 if (gui.in_use && State != NORMAL && !cmd_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 {
3055 int save_State;
3056
3057 save_State = State;
3058 State = NORMAL;
3059 gui_update_cursor(TRUE, FALSE);
3060 State = save_State;
3061 shape_changed = TRUE;
3062 }
3063#endif
3064 validate_cursor();
3065 old_wcol = curwin->w_wcol;
3066 old_wrow = curwin->w_wrow;
3067
Bram Moolenaar30613902019-12-01 22:11:18 +01003068 // move cursor left, if possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 if (curwin->w_cursor.col != 0)
3070 {
3071 if (curwin->w_wcol > 0)
3072 {
3073 if (did_ai)
3074 {
3075 /*
3076 * We are expecting to truncate the trailing
3077 * white-space, so find the last non-white
3078 * character -- webb
3079 */
3080 col = vcol = curwin->w_wcol = 0;
3081 ptr = ml_get_curline();
3082 while (col < curwin->w_cursor.col)
3083 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01003084 if (!VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 curwin->w_wcol = vcol;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003086 vcol += lbr_chartabsize(ptr, ptr + col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003089 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 ++col;
3092 }
3093 curwin->w_wrow = curwin->w_cline_row
Bram Moolenaar02631462017-09-22 15:20:32 +02003094 + curwin->w_wcol / curwin->w_width;
3095 curwin->w_wcol %= curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 curwin->w_wcol += curwin_col_off();
Bram Moolenaar30613902019-12-01 22:11:18 +01003097 col = 0; // no correction needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 }
3099 else
3100 {
3101 --curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 col = curwin->w_cursor.col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 }
3104 }
3105 else if (curwin->w_p_wrap && curwin->w_wrow)
3106 {
3107 --curwin->w_wrow;
Bram Moolenaar02631462017-09-22 15:20:32 +02003108 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 col = curwin->w_cursor.col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 if (has_mbyte && col > 0 && curwin->w_wcol > 0)
3112 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003113 // Correct when the cursor is on the right halve
3114 // of a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 ptr = ml_get_curline();
3116 col -= (*mb_head_off)(ptr, ptr + col);
3117 if ((*mb_ptr2cells)(ptr + col) > 1)
3118 --curwin->w_wcol;
3119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 }
3121 setcursor();
3122 out_flush();
3123#ifdef FEAT_CMDL_INFO
3124 new_wcol = curwin->w_wcol;
3125 new_wrow = curwin->w_wrow;
3126#endif
3127 curwin->w_wcol = old_wcol;
3128 curwin->w_wrow = old_wrow;
3129 }
3130 if (c < 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003131 continue; // end of input script reached
Bram Moolenaar20c38922014-07-23 20:41:14 +02003132
Bram Moolenaar30613902019-12-01 22:11:18 +01003133 // Allow mapping for just typed characters. When we get here c
3134 // is the number of extra bytes and typebuf.tb_len is 1.
Bram Moolenaar20c38922014-07-23 20:41:14 +02003135 for (n = 1; n <= c; ++n)
3136 typebuf.tb_noremap[typebuf.tb_off + n] = RM_YES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 typebuf.tb_len += c;
3138
Bram Moolenaar30613902019-12-01 22:11:18 +01003139 // buffer full, don't map
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 if (typebuf.tb_len >= typebuf.tb_maplen + MAXMAPLEN)
3141 {
3142 timedout = TRUE;
3143 continue;
3144 }
3145
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 if (ex_normal_busy > 0)
3147 {
Bram Moolenaare2c38102016-01-31 14:55:40 +01003148#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 static int tc = 0;
Bram Moolenaare2c38102016-01-31 14:55:40 +01003150#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151
Bram Moolenaar30613902019-12-01 22:11:18 +01003152 // No typeahead left and inside ":normal". Must return
3153 // something to avoid getting stuck. When an incomplete
3154 // mapping is present, behave like it timed out.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 if (typebuf.tb_len > 0)
3156 {
3157 timedout = TRUE;
3158 continue;
3159 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003160 // When 'insertmode' is set, ESC just beeps in Insert
3161 // mode. Use CTRL-L to make edit() return.
3162 // For the command line only CTRL-C always breaks it.
3163 // For the cmdline window: Alternate between ESC and
3164 // CTRL-C: ESC for most situations and CTRL-C to close the
3165 // cmdline window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 if (p_im && (State & INSERT))
3167 c = Ctrl_L;
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003168#ifdef FEAT_TERMINAL
3169 else if (terminal_is_active())
3170 c = K_CANCEL;
3171#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 else if ((State & CMDLINE)
Bram Moolenaare2c38102016-01-31 14:55:40 +01003173#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 || (cmdwin_type > 0 && tc == ESC)
Bram Moolenaare2c38102016-01-31 14:55:40 +01003175#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 )
3177 c = Ctrl_C;
3178 else
3179 c = ESC;
Bram Moolenaare2c38102016-01-31 14:55:40 +01003180#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 tc = c;
Bram Moolenaare2c38102016-01-31 14:55:40 +01003182#endif
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01003183 // return from main_loop()
3184 if (pending_exmode_active)
3185 exmode_active = EXMODE_NORMAL;
3186
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 break;
3188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189
3190/*
3191 * get a character: 3. from the user - update display
3192 */
Bram Moolenaar30613902019-12-01 22:11:18 +01003193 // In insert mode a screen update is skipped when characters
3194 // are still available. But when those available characters
3195 // are part of a mapping, and we are going to do a blocking
3196 // wait here. Need to update the screen to display the
3197 // changed text so far. Also for when 'lazyredraw' is set and
3198 // redrawing was postponed because there was something in the
3199 // input buffer (e.g., termresponse).
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01003200 if (((State & INSERT) != 0 || p_lz) && (State & CMDLINE) == 0
3201 && advance && must_redraw != 0 && !need_wait_return)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 {
3203 update_screen(0);
Bram Moolenaar30613902019-12-01 22:11:18 +01003204 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 }
3206
3207 /*
3208 * If we have a partial match (and are going to wait for more
3209 * input from the user), show the partially matched characters
3210 * to the user with showcmd.
3211 */
3212#ifdef FEAT_CMDL_INFO
Bram Moolenaareda35f72019-08-03 14:59:44 +02003213 showcmd_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214#endif
3215 c1 = 0;
3216 if (typebuf.tb_len > 0 && advance && !exmode_active)
3217 {
3218 if (((State & (NORMAL | INSERT)) || State == LANGMAP)
3219 && State != HITRETURN)
3220 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003221 // this looks nice when typing a dead character map
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 if (State & INSERT
3223 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
3224 + typebuf.tb_len - 1) == 1)
3225 {
3226 edit_putchar(typebuf.tb_buf[typebuf.tb_off
3227 + typebuf.tb_len - 1], FALSE);
Bram Moolenaar30613902019-12-01 22:11:18 +01003228 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 c1 = 1;
3230 }
3231#ifdef FEAT_CMDL_INFO
Bram Moolenaar30613902019-12-01 22:11:18 +01003232 // need to use the col and row from above here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 old_wcol = curwin->w_wcol;
3234 old_wrow = curwin->w_wrow;
3235 curwin->w_wcol = new_wcol;
3236 curwin->w_wrow = new_wrow;
3237 push_showcmd();
3238 if (typebuf.tb_len > SHOWCMD_COLS)
Bram Moolenaareda35f72019-08-03 14:59:44 +02003239 showcmd_idx = typebuf.tb_len - SHOWCMD_COLS;
3240 while (showcmd_idx < typebuf.tb_len)
3241 (void)add_to_showcmd(
3242 typebuf.tb_buf[typebuf.tb_off + showcmd_idx++]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 curwin->w_wcol = old_wcol;
3244 curwin->w_wrow = old_wrow;
3245#endif
3246 }
3247
Bram Moolenaar30613902019-12-01 22:11:18 +01003248 // this looks nice when typing a dead character map
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 if ((State & CMDLINE)
3250#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3251 && cmdline_star == 0
3252#endif
3253 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
3254 + typebuf.tb_len - 1) == 1)
3255 {
3256 putcmdline(typebuf.tb_buf[typebuf.tb_off
3257 + typebuf.tb_len - 1], FALSE);
3258 c1 = 1;
3259 }
3260 }
3261
3262/*
3263 * get a character: 3. from the user - get it
3264 */
Bram Moolenaar83f4cbd2018-06-12 21:35:40 +02003265 if (typebuf.tb_len == 0)
3266 // timedout may have been set while waiting for a mapping
3267 // that has a <Nop> RHS.
3268 timedout = FALSE;
3269
Bram Moolenaar652de232019-04-04 20:13:09 +02003270 if (advance)
3271 {
3272 if (typebuf.tb_len == 0
3273 || !(p_timeout
3274 || (p_ttimeout && keylen == KEYLEN_PART_KEY)))
3275 // blocking wait
3276 wait_time = -1L;
3277 else if (keylen == KEYLEN_PART_KEY && p_ttm >= 0)
3278 wait_time = p_ttm;
3279 else
3280 wait_time = p_tm;
3281 }
3282 else
3283 wait_time = 0;
3284
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003285 wait_tb_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 c = inchar(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len,
3287 typebuf.tb_buflen - typebuf.tb_off - typebuf.tb_len - 1,
Bram Moolenaar652de232019-04-04 20:13:09 +02003288 wait_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289
3290#ifdef FEAT_CMDL_INFO
Bram Moolenaareda35f72019-08-03 14:59:44 +02003291 if (showcmd_idx != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 pop_showcmd();
3293#endif
3294 if (c1 == 1)
3295 {
3296 if (State & INSERT)
3297 edit_unputchar();
3298 if (State & CMDLINE)
3299 unputcmdline();
Bram Moolenaarbc256d92012-06-06 12:06:15 +02003300 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003301 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 }
3303
3304 if (c < 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003305 continue; // end of input script reached
3306 if (c == NUL) // no character available
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 {
3308 if (!advance)
3309 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01003310 if (wait_tb_len > 0) // timed out
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 {
3312 timedout = TRUE;
3313 continue;
3314 }
3315 }
3316 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003317 { // allow mapping for just typed characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 while (typebuf.tb_buf[typebuf.tb_off
3319 + typebuf.tb_len] != NUL)
3320 typebuf.tb_noremap[typebuf.tb_off
3321 + typebuf.tb_len++] = RM_YES;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003322#ifdef HAVE_INPUT_METHOD
Bram Moolenaar30613902019-12-01 22:11:18 +01003323 // Get IM status right after getting keys, not after the
3324 // timeout for a mapping (focus may be lost by then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 vgetc_im_active = im_get_status();
3326#endif
3327 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003328 } // for (;;)
3329 } // if (!character from stuffbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330
Bram Moolenaar30613902019-12-01 22:11:18 +01003331 // if advance is FALSE don't loop on NULs
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003332 } while ((c < 0 && c != K_CANCEL) || (advance && c == NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333
3334 /*
3335 * The "INSERT" message is taken care of here:
3336 * if we return an ESC to exit insert mode, the message is deleted
3337 * if we don't return an ESC but deleted the message before, redisplay it
3338 */
Bram Moolenaar09df3122006-01-23 22:23:09 +00003339 if (advance && p_smd && msg_silent == 0 && (State & INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003341 if (c == ESC && !mode_deleted && !no_mapping && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 {
3343 if (typebuf.tb_len && !KeyTyped)
Bram Moolenaar30613902019-12-01 22:11:18 +01003344 redraw_cmdline = TRUE; // delete mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 else
3346 unshowmode(FALSE);
3347 }
3348 else if (c != ESC && mode_deleted)
3349 {
3350 if (typebuf.tb_len && !KeyTyped)
Bram Moolenaar30613902019-12-01 22:11:18 +01003351 redraw_cmdline = TRUE; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 else
3353 showmode();
3354 }
3355 }
3356#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003357 // may unshow different cursor shape
Bram Moolenaarf085f422017-06-07 20:39:47 +02003358 if (gui.in_use && shape_changed)
3359 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360#endif
Bram Moolenaar6edbbd82019-03-10 09:41:51 +01003361 if (timedout && c == ESC)
3362 {
3363 char_u nop_buf[3];
3364
3365 // When recording there will be no timeout. Add a <Nop> after the ESC
3366 // to avoid that it forms a key code with following characters.
3367 nop_buf[0] = K_SPECIAL;
3368 nop_buf[1] = KS_EXTRA;
3369 nop_buf[2] = KE_NOP;
3370 gotchars(nop_buf, 3);
3371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003373 --vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374
3375 return c;
3376}
3377
3378/*
3379 * inchar() - get one character from
3380 * 1. a scriptfile
3381 * 2. the keyboard
3382 *
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003383 * As many characters as we can get (up to 'maxlen') are put in "buf" and
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 * NUL terminated (buffer length must be 'maxlen' + 1).
3385 * Minimum for "maxlen" is 3!!!!
3386 *
3387 * "tb_change_cnt" is the value of typebuf.tb_change_cnt if "buf" points into
3388 * it. When typebuf.tb_change_cnt changes (e.g., when a message is received
3389 * from a remote client) "buf" can no longer be used. "tb_change_cnt" is 0
3390 * otherwise.
3391 *
3392 * If we got an interrupt all input is read until none is available.
3393 *
3394 * If wait_time == 0 there is no waiting for the char.
3395 * If wait_time == n we wait for n msec for a character to arrive.
3396 * If wait_time == -1 we wait forever for a character to arrive.
3397 *
3398 * Return the number of obtained characters.
3399 * Return -1 when end of input script reached.
3400 */
Bram Moolenaarcda77642016-06-04 13:32:35 +02003401 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003402inchar(
3403 char_u *buf,
3404 int maxlen,
Bram Moolenaar30613902019-12-01 22:11:18 +01003405 long wait_time) // milli seconds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406{
Bram Moolenaar30613902019-12-01 22:11:18 +01003407 int len = 0; // init for GCC
3408 int retesc = FALSE; // return ESC with gotint
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 int script_char;
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003410 int tb_change_cnt = typebuf.tb_change_cnt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411
Bram Moolenaar30613902019-12-01 22:11:18 +01003412 if (wait_time == -1L || wait_time > 100L) // flush output before waiting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 {
3414 cursor_on();
Bram Moolenaara338adc2018-01-31 20:51:47 +01003415 out_flush_cursor(FALSE, FALSE);
3416#if defined(FEAT_GUI) && defined(FEAT_MOUSESHAPE)
3417 if (gui.in_use && postponed_mouseshape)
3418 update_mouseshape(-1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419#endif
3420 }
3421
3422 /*
3423 * Don't reset these when at the hit-return prompt, otherwise a endless
3424 * recursive loop may result (write error in swapfile, hit-return, timeout
3425 * on char wait, flush swapfile, write error....).
3426 */
3427 if (State != HITRETURN)
3428 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003429 did_outofmem_msg = FALSE; // display out of memory message (again)
3430 did_swapwrite_msg = FALSE; // display swap file write error again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003432 undo_off = FALSE; // restart undo now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433
3434 /*
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00003435 * Get a character from a script file if there is one.
3436 * If interrupted: Stop reading script files, close them all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 */
3438 script_char = -1;
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00003439 while (scriptin[curscript] != NULL && script_char < 0
3440#ifdef FEAT_EVAL
3441 && !ignore_script
3442#endif
3443 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02003445#ifdef MESSAGE_QUEUE
3446 parse_queued_messages();
Bram Moolenaarf2330482008-06-24 20:19:36 +00003447#endif
3448
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 if (got_int || (script_char = getc(scriptin[curscript])) < 0)
3450 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003451 // Reached EOF.
3452 // Careful: closescript() frees typebuf.tb_buf[] and buf[] may
3453 // point inside typebuf.tb_buf[]. Don't use buf[] after this!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 closescript();
3455 /*
3456 * When reading script file is interrupted, return an ESC to get
3457 * back to normal mode.
3458 * Otherwise return -1, because typebuf.tb_buf[] has changed.
3459 */
3460 if (got_int)
3461 retesc = TRUE;
3462 else
3463 return -1;
3464 }
3465 else
3466 {
3467 buf[0] = script_char;
3468 len = 1;
3469 }
3470 }
3471
Bram Moolenaar30613902019-12-01 22:11:18 +01003472 if (script_char < 0) // did not get a character from script
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 {
3474 /*
3475 * If we got an interrupt, skip all previously typed characters and
3476 * return TRUE if quit reading script file.
3477 * Stop reading typeahead when a single CTRL-C was read,
3478 * fill_input_buf() returns this when not able to read from stdin.
3479 * Don't use buf[] here, closescript() may have freed typebuf.tb_buf[]
3480 * and buf may be pointing inside typebuf.tb_buf[].
3481 */
3482 if (got_int)
3483 {
3484#define DUM_LEN MAXMAPLEN * 3 + 3
3485 char_u dum[DUM_LEN + 1];
3486
3487 for (;;)
3488 {
3489 len = ui_inchar(dum, DUM_LEN, 0L, 0);
3490 if (len == 0 || (len == 1 && dum[0] == 3))
3491 break;
3492 }
3493 return retesc;
3494 }
3495
3496 /*
3497 * Always flush the output characters when getting input characters
Bram Moolenaarcb574f42019-01-25 22:29:57 +01003498 * from the user and not just peeking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 */
Bram Moolenaarcb574f42019-01-25 22:29:57 +01003500 if (wait_time == -1L || wait_time > 10L)
3501 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502
3503 /*
3504 * Fill up to a third of the buffer, because each character may be
3505 * tripled below.
3506 */
3507 len = ui_inchar(buf, maxlen / 3, wait_time, tb_change_cnt);
3508 }
3509
Bram Moolenaar30613902019-12-01 22:11:18 +01003510 // If the typebuf was changed further down, it is like nothing was added by
3511 // this call.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 if (typebuf_changed(tb_change_cnt))
3513 return 0;
3514
Bram Moolenaar30613902019-12-01 22:11:18 +01003515 // Note the change in the typeahead buffer, this matters for when
3516 // vgetorpeek() is called recursively, e.g. using getchar(1) in a timer
3517 // function.
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003518 if (len > 0 && ++typebuf.tb_change_cnt == 0)
3519 typebuf.tb_change_cnt = 1;
3520
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003521 return fix_input_buffer(buf, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522}
3523
3524/*
3525 * Fix typed characters for use by vgetc() and check_termcode().
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003526 * "buf[]" must have room to triple the number of bytes!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 * Returns the new length.
3528 */
3529 int
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003530fix_input_buffer(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531{
3532 int i;
3533 char_u *p = buf;
3534
3535 /*
3536 * Two characters are special: NUL and K_SPECIAL.
3537 * When compiled With the GUI CSI is also special.
3538 * Replace NUL by K_SPECIAL KS_ZERO KE_FILLER
3539 * Replace K_SPECIAL by K_SPECIAL KS_SPECIAL KE_FILLER
3540 * Replace CSI by K_SPECIAL KS_EXTRA KE_CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 */
3542 for (i = len; --i >= 0; ++p)
3543 {
3544#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003545 // When the GUI is used any character can come after a CSI, don't
3546 // escape it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 if (gui.in_use && p[0] == CSI && i >= 2)
3548 {
3549 p += 2;
3550 i -= 2;
3551 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003552# ifndef MSWIN
Bram Moolenaar30613902019-12-01 22:11:18 +01003553 // When the GUI is not used CSI needs to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 else if (!gui.in_use && p[0] == CSI)
3555 {
3556 mch_memmove(p + 3, p + 1, (size_t)i);
3557 *p++ = K_SPECIAL;
3558 *p++ = KS_EXTRA;
3559 *p = (int)KE_CSI;
3560 len += 2;
3561 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003562# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 else
3564#endif
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003565 if (p[0] == NUL || (p[0] == K_SPECIAL
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003566 // timeout may generate K_CURSORHOLD
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003567 && (i < 2 || p[1] != KS_EXTRA || p[2] != (int)KE_CURSORHOLD)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003568#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003569 // Win32 console passes modifiers
3570 && (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003571# ifdef VIMDLL
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003572 gui.in_use ||
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003573# endif
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003574 (i < 2 || p[1] != KS_MODIFIER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575#endif
3576 ))
3577 {
3578 mch_memmove(p + 3, p + 1, (size_t)i);
3579 p[2] = K_THIRD(p[0]);
3580 p[1] = K_SECOND(p[0]);
3581 p[0] = K_SPECIAL;
3582 p += 2;
3583 len += 2;
3584 }
3585 }
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003586 *p = NUL; // add trailing NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 return len;
3588}
3589
3590#if defined(USE_INPUT_BUF) || defined(PROTO)
3591/*
3592 * Return TRUE when bytes are in the input buffer or in the typeahead buffer.
3593 * Normally the input buffer would be sufficient, but the server_to_input_buf()
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003594 * or feedkeys() may insert characters in the typeahead buffer while we are
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003595 * waiting for input to arrive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 */
3597 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003598input_available(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599{
3600 return (!vim_is_input_buf_empty()
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003601# if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
3602 || typebuf_was_filled
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603# endif
3604 );
3605}
3606#endif