blob: 7bbdf35834f3c62df541f43dadc18f41ab9cc7ea [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/*
688 * Read a character from the redo buffer. Translates K_SPECIAL, CSI and
689 * multibyte characters.
690 * The redo buffer is left as it is.
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100691 * If init is TRUE, prepare for redo, return FAIL if nothing to redo, OK
692 * otherwise.
693 * If old is TRUE, use old_redobuff instead of redobuff.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 */
695 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100696read_redo(int init, int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100698 static buffblock_T *bp;
699 static char_u *p;
700 int c;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100701 int n;
702 char_u buf[MB_MAXBYTES + 1];
703 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704
705 if (init)
706 {
707 if (old_redo)
Bram Moolenaar285e3352018-04-18 23:01:13 +0200708 bp = old_redobuff.bh_first.b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 else
Bram Moolenaar285e3352018-04-18 23:01:13 +0200710 bp = redobuff.bh_first.b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 if (bp == NULL)
712 return FAIL;
713 p = bp->b_str;
714 return OK;
715 }
716 if ((c = *p) != NUL)
717 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100718 // Reverse the conversion done by add_char_buff()
719 // For a multi-byte character get all the bytes and return the
720 // converted character.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 if (has_mbyte && (c != K_SPECIAL || p[1] == KS_SPECIAL))
722 n = MB_BYTE2LEN_CHECK(c);
723 else
724 n = 1;
725 for (i = 0; ; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100727 if (c == K_SPECIAL) // special key or escaped K_SPECIAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 {
729 c = TO_SPECIAL(p[1], p[2]);
730 p += 2;
731 }
732#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +0100733 if (c == CSI) // escaped CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 p += 2;
735#endif
736 if (*++p == NUL && bp->b_next != NULL)
737 {
738 bp = bp->b_next;
739 p = bp->b_str;
740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 buf[i] = c;
Bram Moolenaar30613902019-12-01 22:11:18 +0100742 if (i == n - 1) // last byte of a character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 {
744 if (n != 1)
745 c = (*mb_ptr2char)(buf);
746 break;
747 }
748 c = *p;
Bram Moolenaar30613902019-12-01 22:11:18 +0100749 if (c == NUL) // cannot happen?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 }
752 }
753
754 return c;
755}
756
757/*
758 * Copy the rest of the redo buffer into the stuff buffer (in a slow way).
759 * If old_redo is TRUE, use old_redobuff instead of redobuff.
760 * The escaped K_SPECIAL and CSI are copied without translation.
761 */
762 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100763copy_redo(int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764{
765 int c;
766
767 while ((c = read_redo(FALSE, old_redo)) != NUL)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100768 add_char_buff(&readbuf2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769}
770
771/*
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100772 * Stuff the redo buffer into readbuf2.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 * Insert the redo count into the command.
774 * If "old_redo" is TRUE, the last but one command is repeated
775 * instead of the last command (inserting text). This is used for
776 * CTRL-O <.> in insert mode
777 *
778 * return FAIL for failure, OK otherwise
779 */
780 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100781start_redo(long count, int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782{
783 int c;
784
Bram Moolenaar30613902019-12-01 22:11:18 +0100785 // init the pointers; return if nothing to redo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 if (read_redo(TRUE, old_redo) == FAIL)
787 return FAIL;
788
789 c = read_redo(FALSE, old_redo);
790
Bram Moolenaar30613902019-12-01 22:11:18 +0100791 // copy the buffer name, if present
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 if (c == '"')
793 {
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100794 add_buff(&readbuf2, (char_u *)"\"", 1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 c = read_redo(FALSE, old_redo);
796
Bram Moolenaar30613902019-12-01 22:11:18 +0100797 // if a numbered buffer is used, increment the number
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 if (c >= '1' && c < '9')
799 ++c;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100800 add_char_buff(&readbuf2, c);
Bram Moolenaar833093b2018-05-23 21:53:52 +0200801
Bram Moolenaar30613902019-12-01 22:11:18 +0100802 // the expression register should be re-evaluated
Bram Moolenaar833093b2018-05-23 21:53:52 +0200803 if (c == '=')
804 {
805 add_char_buff(&readbuf2, CAR);
806 cmd_silent = TRUE;
807 }
808
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 c = read_redo(FALSE, old_redo);
810 }
811
Bram Moolenaar30613902019-12-01 22:11:18 +0100812 if (c == 'v') // redo Visual
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 {
814 VIsual = curwin->w_cursor;
815 VIsual_active = TRUE;
816 VIsual_select = FALSE;
817 VIsual_reselect = TRUE;
818 redo_VIsual_busy = TRUE;
819 c = read_redo(FALSE, old_redo);
820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821
Bram Moolenaar30613902019-12-01 22:11:18 +0100822 // try to enter the count (in place of a previous count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 if (count)
824 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100825 while (VIM_ISDIGIT(c)) // skip "old" count
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 c = read_redo(FALSE, old_redo);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100827 add_num_buff(&readbuf2, count);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 }
829
Bram Moolenaar30613902019-12-01 22:11:18 +0100830 // copy from the redo buffer into the stuff buffer
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100831 add_char_buff(&readbuf2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 copy_redo(old_redo);
833 return OK;
834}
835
836/*
837 * Repeat the last insert (R, o, O, a, A, i or I command) by stuffing
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100838 * the redo buffer into readbuf2.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 * return FAIL for failure, OK otherwise
840 */
841 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100842start_redo_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843{
844 int c;
845
846 if (read_redo(TRUE, FALSE) == FAIL)
847 return FAIL;
848 start_stuff();
849
Bram Moolenaar30613902019-12-01 22:11:18 +0100850 // skip the count and the command character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 while ((c = read_redo(FALSE, FALSE)) != NUL)
852 {
853 if (vim_strchr((char_u *)"AaIiRrOo", c) != NULL)
854 {
855 if (c == 'O' || c == 'o')
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100856 add_buff(&readbuf2, NL_STR, -1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 break;
858 }
859 }
860
Bram Moolenaar30613902019-12-01 22:11:18 +0100861 // copy the typed text from the redo buffer into the stuff buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 copy_redo(FALSE);
863 block_redo = TRUE;
864 return OK;
865}
866
867 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100868stop_redo_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869{
870 block_redo = FALSE;
871}
872
873/*
874 * Initialize typebuf.tb_buf to point to typebuf_init.
875 * alloc() cannot be used here: In out-of-memory situations it would
876 * be impossible to type anything.
877 */
878 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100879init_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880{
881 if (typebuf.tb_buf == NULL)
882 {
883 typebuf.tb_buf = typebuf_init;
884 typebuf.tb_noremap = noremapbuf_init;
885 typebuf.tb_buflen = TYPELEN_INIT;
886 typebuf.tb_len = 0;
Bram Moolenaard34f9b12017-04-08 18:41:13 +0200887 typebuf.tb_off = MAXMAPLEN + 4;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 typebuf.tb_change_cnt = 1;
889 }
890}
891
892/*
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200893 * Returns TRUE when keys cannot be remapped.
894 */
895 int
896noremap_keys(void)
897{
898 return KeyNoremap & (RM_NONE|RM_SCRIPT);
899}
900
901/*
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100902 * Insert a string in position 'offset' in the typeahead buffer (for "@r"
903 * and ":normal" command, vgetorpeek() and check_termcode()).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 *
905 * If noremap is REMAP_YES, new string can be mapped again.
906 * If noremap is REMAP_NONE, new string cannot be mapped again.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000907 * If noremap is REMAP_SKIP, fist char of new string cannot be mapped again,
908 * but abbreviations are allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 * If noremap is REMAP_SCRIPT, new string cannot be mapped again, except for
910 * script-local mappings.
911 * If noremap is > 0, that many characters of the new string cannot be mapped.
912 *
913 * If nottyped is TRUE, the string does not return KeyTyped (don't use when
914 * offset is non-zero!).
915 *
916 * If silent is TRUE, cmd_silent is set when the characters are obtained.
917 *
918 * return FAIL for failure, OK otherwise
919 */
920 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100921ins_typebuf(
922 char_u *str,
923 int noremap,
924 int offset,
925 int nottyped,
926 int silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927{
928 char_u *s1, *s2;
929 int newlen;
930 int addlen;
931 int i;
932 int newoff;
933 int val;
934 int nrm;
935
936 init_typebuf();
937 if (++typebuf.tb_change_cnt == 0)
938 typebuf.tb_change_cnt = 1;
Bram Moolenaard103ee72019-09-18 21:15:31 +0200939 state_no_longer_safe("ins_typebuf()");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940
941 addlen = (int)STRLEN(str);
Bram Moolenaar4e86cba2007-05-06 11:56:32 +0000942
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 if (offset == 0 && addlen <= typebuf.tb_off)
944 {
Bram Moolenaarcaa55b62017-01-10 13:51:09 +0100945 /*
946 * Easy case: there is room in front of typebuf.tb_buf[typebuf.tb_off]
947 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 typebuf.tb_off -= addlen;
949 mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen);
950 }
Bram Moolenaard34f9b12017-04-08 18:41:13 +0200951 else if (typebuf.tb_len == 0 && typebuf.tb_buflen
952 >= addlen + 3 * (MAXMAPLEN + 4))
953 {
954 /*
955 * Buffer is empty and string fits in the existing buffer.
956 * Leave some space before and after, if possible.
957 */
958 typebuf.tb_off = (typebuf.tb_buflen - addlen - 3 * (MAXMAPLEN + 4)) / 2;
959 mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen);
960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 else
962 {
Bram Moolenaarcaa55b62017-01-10 13:51:09 +0100963 /*
964 * Need to allocate a new buffer.
Bram Moolenaard34f9b12017-04-08 18:41:13 +0200965 * In typebuf.tb_buf there must always be room for 3 * (MAXMAPLEN + 4)
Bram Moolenaarcaa55b62017-01-10 13:51:09 +0100966 * characters. We add some extra room to avoid having to allocate too
967 * often.
968 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 newoff = MAXMAPLEN + 4;
970 newlen = typebuf.tb_len + addlen + newoff + 4 * (MAXMAPLEN + 4);
Bram Moolenaar30613902019-12-01 22:11:18 +0100971 if (newlen < 0) // string is getting too long
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100973 emsg(_(e_toocompl)); // also calls flush_buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 setcursor();
975 return FAIL;
976 }
977 s1 = alloc(newlen);
Bram Moolenaar30613902019-12-01 22:11:18 +0100978 if (s1 == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 return FAIL;
980 s2 = alloc(newlen);
Bram Moolenaar30613902019-12-01 22:11:18 +0100981 if (s2 == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 {
983 vim_free(s1);
984 return FAIL;
985 }
986 typebuf.tb_buflen = newlen;
987
Bram Moolenaar30613902019-12-01 22:11:18 +0100988 // copy the old chars, before the insertion point
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 mch_memmove(s1 + newoff, typebuf.tb_buf + typebuf.tb_off,
990 (size_t)offset);
Bram Moolenaar30613902019-12-01 22:11:18 +0100991 // copy the new chars
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 mch_memmove(s1 + newoff + offset, str, (size_t)addlen);
Bram Moolenaar30613902019-12-01 22:11:18 +0100993 // copy the old chars, after the insertion point, including the NUL at
994 // the end
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 mch_memmove(s1 + newoff + offset + addlen,
996 typebuf.tb_buf + typebuf.tb_off + offset,
997 (size_t)(typebuf.tb_len - offset + 1));
998 if (typebuf.tb_buf != typebuf_init)
999 vim_free(typebuf.tb_buf);
1000 typebuf.tb_buf = s1;
1001
1002 mch_memmove(s2 + newoff, typebuf.tb_noremap + typebuf.tb_off,
1003 (size_t)offset);
1004 mch_memmove(s2 + newoff + offset + addlen,
1005 typebuf.tb_noremap + typebuf.tb_off + offset,
1006 (size_t)(typebuf.tb_len - offset));
1007 if (typebuf.tb_noremap != noremapbuf_init)
1008 vim_free(typebuf.tb_noremap);
1009 typebuf.tb_noremap = s2;
1010
1011 typebuf.tb_off = newoff;
1012 }
1013 typebuf.tb_len += addlen;
1014
Bram Moolenaar30613902019-12-01 22:11:18 +01001015 // If noremap == REMAP_SCRIPT: do remap script-local mappings.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 if (noremap == REMAP_SCRIPT)
1017 val = RM_SCRIPT;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001018 else if (noremap == REMAP_SKIP)
1019 val = RM_ABBR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 else
1021 val = RM_NONE;
1022
1023 /*
1024 * Adjust typebuf.tb_noremap[] for the new characters:
1025 * If noremap == REMAP_NONE or REMAP_SCRIPT: new characters are
1026 * (sometimes) not remappable
1027 * If noremap == REMAP_YES: all the new characters are mappable
1028 * If noremap > 0: "noremap" characters are not remappable, the rest
1029 * mappable
1030 */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001031 if (noremap == REMAP_SKIP)
1032 nrm = 1;
1033 else if (noremap < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 nrm = addlen;
1035 else
1036 nrm = noremap;
1037 for (i = 0; i < addlen; ++i)
1038 typebuf.tb_noremap[typebuf.tb_off + i + offset] =
1039 (--nrm >= 0) ? val : RM_YES;
1040
Bram Moolenaar30613902019-12-01 22:11:18 +01001041 // tb_maplen and tb_silent only remember the length of mapped and/or
1042 // silent mappings at the start of the buffer, assuming that a mapped
1043 // sequence doesn't result in typed characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 if (nottyped || typebuf.tb_maplen > offset)
1045 typebuf.tb_maplen += addlen;
1046 if (silent || typebuf.tb_silent > offset)
1047 {
1048 typebuf.tb_silent += addlen;
1049 cmd_silent = TRUE;
1050 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001051 if (typebuf.tb_no_abbr_cnt && offset == 0) // and not used for abbrev.s
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 typebuf.tb_no_abbr_cnt += addlen;
1053
1054 return OK;
1055}
1056
1057/*
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001058 * Put character "c" back into the typeahead buffer.
1059 * Can be used for a character obtained by vgetc() that needs to be put back.
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00001060 * Uses cmd_silent, KeyTyped and KeyNoremap to restore the flags belonging to
1061 * the char.
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001062 */
1063 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001064ins_char_typebuf(int c)
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001065{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001066 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001067 if (IS_SPECIAL(c))
1068 {
1069 buf[0] = K_SPECIAL;
1070 buf[1] = K_SECOND(c);
1071 buf[2] = K_THIRD(c);
1072 buf[3] = NUL;
1073 }
1074 else
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001075 buf[(*mb_char2bytes)(c, buf)] = NUL;
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00001076 (void)ins_typebuf(buf, KeyNoremap, 0, !KeyTyped, cmd_silent);
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001077}
1078
1079/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 * Return TRUE if the typeahead buffer was changed (while waiting for a
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001081 * character to arrive). Happens when a message was received from a client or
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001082 * from feedkeys().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 * But check in a more generic way to avoid trouble: When "typebuf.tb_buf"
1084 * changed it was reallocated and the old pointer can no longer be used.
1085 * Or "typebuf.tb_off" may have been changed and we would overwrite characters
1086 * that was just added.
1087 */
1088 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001089typebuf_changed(
Bram Moolenaar30613902019-12-01 22:11:18 +01001090 int tb_change_cnt) // old value of typebuf.tb_change_cnt
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091{
1092 return (tb_change_cnt != 0 && (typebuf.tb_change_cnt != tb_change_cnt
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001093#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
1094 || typebuf_was_filled
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095#endif
1096 ));
1097}
1098
1099/*
1100 * Return TRUE if there are no characters in the typeahead buffer that have
1101 * not been typed (result from a mapping or come from ":normal").
1102 */
1103 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001104typebuf_typed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105{
1106 return typebuf.tb_maplen == 0;
1107}
1108
1109/*
1110 * Return the number of characters that are mapped (or not typed).
1111 */
1112 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001113typebuf_maplen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114{
1115 return typebuf.tb_maplen;
1116}
1117
1118/*
1119 * remove "len" characters from typebuf.tb_buf[typebuf.tb_off + offset]
1120 */
1121 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001122del_typebuf(int len, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123{
1124 int i;
1125
1126 if (len == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01001127 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128
1129 typebuf.tb_len -= len;
1130
1131 /*
1132 * Easy case: Just increase typebuf.tb_off.
1133 */
1134 if (offset == 0 && typebuf.tb_buflen - (typebuf.tb_off + len)
1135 >= 3 * MAXMAPLEN + 3)
1136 typebuf.tb_off += len;
1137 /*
1138 * Have to move the characters in typebuf.tb_buf[] and typebuf.tb_noremap[]
1139 */
1140 else
1141 {
1142 i = typebuf.tb_off + offset;
1143 /*
1144 * Leave some extra room at the end to avoid reallocation.
1145 */
1146 if (typebuf.tb_off > MAXMAPLEN)
1147 {
1148 mch_memmove(typebuf.tb_buf + MAXMAPLEN,
1149 typebuf.tb_buf + typebuf.tb_off, (size_t)offset);
1150 mch_memmove(typebuf.tb_noremap + MAXMAPLEN,
1151 typebuf.tb_noremap + typebuf.tb_off, (size_t)offset);
1152 typebuf.tb_off = MAXMAPLEN;
1153 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001154 // adjust typebuf.tb_buf (include the NUL at the end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset,
1156 typebuf.tb_buf + i + len,
1157 (size_t)(typebuf.tb_len - offset + 1));
Bram Moolenaar30613902019-12-01 22:11:18 +01001158 // adjust typebuf.tb_noremap[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 mch_memmove(typebuf.tb_noremap + typebuf.tb_off + offset,
1160 typebuf.tb_noremap + i + len,
1161 (size_t)(typebuf.tb_len - offset));
1162 }
1163
Bram Moolenaar30613902019-12-01 22:11:18 +01001164 if (typebuf.tb_maplen > offset) // adjust tb_maplen
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 {
1166 if (typebuf.tb_maplen < offset + len)
1167 typebuf.tb_maplen = offset;
1168 else
1169 typebuf.tb_maplen -= len;
1170 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001171 if (typebuf.tb_silent > offset) // adjust tb_silent
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 {
1173 if (typebuf.tb_silent < offset + len)
1174 typebuf.tb_silent = offset;
1175 else
1176 typebuf.tb_silent -= len;
1177 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001178 if (typebuf.tb_no_abbr_cnt > offset) // adjust tb_no_abbr_cnt
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 {
1180 if (typebuf.tb_no_abbr_cnt < offset + len)
1181 typebuf.tb_no_abbr_cnt = offset;
1182 else
1183 typebuf.tb_no_abbr_cnt -= len;
1184 }
1185
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001186#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar30613902019-12-01 22:11:18 +01001187 // Reset the flag that text received from a client or from feedkeys()
1188 // was inserted in the typeahead buffer.
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001189 typebuf_was_filled = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190#endif
1191 if (++typebuf.tb_change_cnt == 0)
1192 typebuf.tb_change_cnt = 1;
1193}
1194
1195/*
1196 * Write typed characters to script file.
1197 * If recording is on put the character in the recordbuffer.
1198 */
1199 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001200gotchars(char_u *chars, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201{
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001202 char_u *s = chars;
1203 int i;
1204 static char_u buf[4];
1205 static int buflen = 0;
1206 int todo = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207
Bram Moolenaar4e86cba2007-05-06 11:56:32 +00001208 while (todo--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 {
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001210 buf[buflen++] = *s++;
1211
1212 // When receiving a special key sequence, store it until we have all
1213 // the bytes and we can decide what to do with it.
1214 if (buflen == 1 && buf[0] == K_SPECIAL)
1215 continue;
1216 if (buflen == 2)
1217 continue;
1218 if (buflen == 3 && buf[1] == KS_EXTRA
1219 && (buf[2] == KE_FOCUSGAINED || buf[2] == KE_FOCUSLOST))
1220 {
1221 // Drop K_FOCUSGAINED and K_FOCUSLOST, they are not useful in a
1222 // recording.
1223 buflen = 0;
1224 continue;
1225 }
1226
Bram Moolenaar30613902019-12-01 22:11:18 +01001227 // Handle one byte at a time; no translation to be done.
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001228 for (i = 0; i < buflen; ++i)
1229 updatescript(buf[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001231 if (reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 {
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001233 buf[buflen] = NUL;
1234 add_buff(&recordbuff, buf, (long)buflen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001235 // remember how many chars were last recorded
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001236 last_recorded_len += buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 }
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001238 buflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 }
1240 may_sync_undo();
1241
1242#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01001243 // output "debug mode" message next time in debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 debug_did_msg = FALSE;
1245#endif
1246
Bram Moolenaar30613902019-12-01 22:11:18 +01001247 // Since characters have been typed, consider the following to be in
1248 // another mapping. Search string will be kept in history.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 ++maptick;
1250}
1251
1252/*
1253 * Sync undo. Called when typed characters are obtained from the typeahead
1254 * buffer, or when a menu is used.
1255 * Do not sync:
1256 * - In Insert mode, unless cursor key has been used.
1257 * - While reading a script file.
1258 * - When no_u_sync is non-zero.
1259 */
1260 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001261may_sync_undo(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262{
1263 if ((!(State & (INSERT + CMDLINE)) || arrow_used)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001264 && scriptin[curscript] == NULL)
1265 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266}
1267
1268/*
1269 * Make "typebuf" empty and allocate new buffers.
1270 * Returns FAIL when out of memory.
1271 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001272 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001273alloc_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274{
1275 typebuf.tb_buf = alloc(TYPELEN_INIT);
1276 typebuf.tb_noremap = alloc(TYPELEN_INIT);
1277 if (typebuf.tb_buf == NULL || typebuf.tb_noremap == NULL)
1278 {
1279 free_typebuf();
1280 return FAIL;
1281 }
1282 typebuf.tb_buflen = TYPELEN_INIT;
Bram Moolenaar30613902019-12-01 22:11:18 +01001283 typebuf.tb_off = MAXMAPLEN + 4; // can insert without realloc
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 typebuf.tb_len = 0;
1285 typebuf.tb_maplen = 0;
1286 typebuf.tb_silent = 0;
1287 typebuf.tb_no_abbr_cnt = 0;
1288 if (++typebuf.tb_change_cnt == 0)
1289 typebuf.tb_change_cnt = 1;
Bram Moolenaare49b4bb2020-03-11 13:01:40 +01001290#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
1291 typebuf_was_filled = FALSE;
1292#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 return OK;
1294}
1295
1296/*
1297 * Free the buffers of "typebuf".
1298 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001299 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001300free_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301{
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001302 if (typebuf.tb_buf == typebuf_init)
Bram Moolenaar95f09602016-11-10 20:01:45 +01001303 internal_error("Free typebuf 1");
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001304 else
Bram Moolenaar0f1c6702019-09-28 15:24:00 +02001305 VIM_CLEAR(typebuf.tb_buf);
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00001306 if (typebuf.tb_noremap == noremapbuf_init)
Bram Moolenaar95f09602016-11-10 20:01:45 +01001307 internal_error("Free typebuf 2");
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001308 else
Bram Moolenaar0f1c6702019-09-28 15:24:00 +02001309 VIM_CLEAR(typebuf.tb_noremap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310}
1311
1312/*
1313 * When doing ":so! file", the current typeahead needs to be saved, and
1314 * restored when "file" has been read completely.
1315 */
1316static typebuf_T saved_typebuf[NSCRIPT];
1317
1318 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001319save_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320{
1321 init_typebuf();
1322 saved_typebuf[curscript] = typebuf;
Bram Moolenaar30613902019-12-01 22:11:18 +01001323 // If out of memory: restore typebuf and close file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 if (alloc_typebuf() == FAIL)
1325 {
1326 closescript();
1327 return FAIL;
1328 }
1329 return OK;
1330}
1331
Bram Moolenaar30613902019-12-01 22:11:18 +01001332static int old_char = -1; // character put back by vungetc()
1333static int old_mod_mask; // mod_mask for ungotten character
1334static int old_mouse_row; // mouse_row related to old_char
1335static int old_mouse_col; // mouse_col related to old_char
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001336
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337/*
1338 * Save all three kinds of typeahead, so that the user must type at a prompt.
1339 */
1340 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001341save_typeahead(tasave_T *tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342{
1343 tp->save_typebuf = typebuf;
1344 tp->typebuf_valid = (alloc_typebuf() == OK);
1345 if (!tp->typebuf_valid)
1346 typebuf = tp->save_typebuf;
1347
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001348 tp->old_char = old_char;
1349 tp->old_mod_mask = old_mod_mask;
1350 old_char = -1;
1351
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001352 tp->save_readbuf1 = readbuf1;
Bram Moolenaar285e3352018-04-18 23:01:13 +02001353 readbuf1.bh_first.b_next = NULL;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001354 tp->save_readbuf2 = readbuf2;
Bram Moolenaar285e3352018-04-18 23:01:13 +02001355 readbuf2.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356# ifdef USE_INPUT_BUF
1357 tp->save_inputbuf = get_input_buf();
1358# endif
1359}
1360
1361/*
1362 * Restore the typeahead to what it was before calling save_typeahead().
1363 * The allocated memory is freed, can only be called once!
1364 */
1365 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001366restore_typeahead(tasave_T *tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367{
1368 if (tp->typebuf_valid)
1369 {
1370 free_typebuf();
1371 typebuf = tp->save_typebuf;
1372 }
1373
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001374 old_char = tp->old_char;
1375 old_mod_mask = tp->old_mod_mask;
1376
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001377 free_buff(&readbuf1);
1378 readbuf1 = tp->save_readbuf1;
1379 free_buff(&readbuf2);
1380 readbuf2 = tp->save_readbuf2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381# ifdef USE_INPUT_BUF
1382 set_input_buf(tp->save_inputbuf);
1383# endif
1384}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385
1386/*
1387 * Open a new script file for the ":source!" command.
1388 */
1389 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001390openscript(
1391 char_u *name,
Bram Moolenaar30613902019-12-01 22:11:18 +01001392 int directly) // when TRUE execute directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393{
1394 if (curscript + 1 == NSCRIPT)
1395 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001396 emsg(_(e_nesting));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 return;
1398 }
Bram Moolenaar53575522019-05-22 22:38:25 +02001399
1400 // Disallow sourcing a file in the sandbox, the commands would be executed
1401 // later, possibly outside of the sandbox.
1402 if (check_secure())
1403 return;
1404
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001405#ifdef FEAT_EVAL
1406 if (ignore_script)
Bram Moolenaar30613902019-12-01 22:11:18 +01001407 // Not reading from script, also don't open one. Warning message?
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001408 return;
1409#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410
Bram Moolenaar30613902019-12-01 22:11:18 +01001411 if (scriptin[curscript] != NULL) // already reading script
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 ++curscript;
Bram Moolenaar30613902019-12-01 22:11:18 +01001413 // use NameBuff for expanded name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 expand_env(name, NameBuff, MAXPATHL);
1415 if ((scriptin[curscript] = mch_fopen((char *)NameBuff, READBIN)) == NULL)
1416 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001417 semsg(_(e_notopen), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 if (curscript)
1419 --curscript;
1420 return;
1421 }
1422 if (save_typebuf() == FAIL)
1423 return;
1424
1425 /*
1426 * Execute the commands from the file right now when using ":source!"
1427 * after ":global" or ":argdo" or in a loop. Also when another command
1428 * follows. This means the display won't be updated. Don't do this
1429 * always, "make test" would fail.
1430 */
1431 if (directly)
1432 {
1433 oparg_T oa;
1434 int oldcurscript;
1435 int save_State = State;
1436 int save_restart_edit = restart_edit;
1437 int save_insertmode = p_im;
1438 int save_finish_op = finish_op;
1439 int save_msg_scroll = msg_scroll;
1440
1441 State = NORMAL;
Bram Moolenaar30613902019-12-01 22:11:18 +01001442 msg_scroll = FALSE; // no msg scrolling in Normal mode
1443 restart_edit = 0; // don't go to Insert mode
1444 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 clear_oparg(&oa);
1446 finish_op = FALSE;
1447
1448 oldcurscript = curscript;
1449 do
1450 {
Bram Moolenaar386b43e2019-05-19 21:57:11 +02001451 update_topline_cursor(); // update cursor position and topline
1452 normal_cmd(&oa, FALSE); // execute one command
1453 vpeekc(); // check for end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 }
1455 while (scriptin[oldcurscript] != NULL);
1456
1457 State = save_State;
1458 msg_scroll = save_msg_scroll;
1459 restart_edit = save_restart_edit;
1460 p_im = save_insertmode;
1461 finish_op = save_finish_op;
1462 }
1463}
1464
1465/*
1466 * Close the currently active input script.
1467 */
1468 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001469closescript(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470{
1471 free_typebuf();
1472 typebuf = saved_typebuf[curscript];
1473
1474 fclose(scriptin[curscript]);
1475 scriptin[curscript] = NULL;
1476 if (curscript > 0)
1477 --curscript;
1478}
1479
Bram Moolenaarea408852005-06-25 22:49:46 +00001480#if defined(EXITFREE) || defined(PROTO)
1481 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001482close_all_scripts(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00001483{
1484 while (scriptin[0] != NULL)
1485 closescript();
1486}
1487#endif
1488
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489/*
1490 * Return TRUE when reading keys from a script file.
1491 */
1492 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001493using_script(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494{
1495 return scriptin[curscript] != NULL;
1496}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497
1498/*
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001499 * This function is called just before doing a blocking wait. Thus after
1500 * waiting 'updatetime' for a character to arrive.
1501 */
1502 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001503before_blocking(void)
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001504{
1505 updatescript(0);
1506#ifdef FEAT_EVAL
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001507 if (may_garbage_collect)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001508 garbage_collect(FALSE);
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001509#endif
1510}
1511
1512/*
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001513 * updatescript() is called when a character can be written into the script file
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 * or when we have waited some time for a character (c == 0)
1515 *
1516 * All the changed memfiles are synced if c == 0 or when the number of typed
1517 * characters reaches 'updatecount' and 'updatecount' is non-zero.
1518 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001519 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001520updatescript(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521{
1522 static int count = 0;
1523
1524 if (c && scriptout)
1525 putc(c, scriptout);
1526 if (c == 0 || (p_uc > 0 && ++count >= p_uc))
1527 {
1528 ml_sync_all(c == 0, TRUE);
1529 count = 0;
1530 }
1531}
1532
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533/*
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001534 * Convert "c" plus "mod_mask" to merge the effect of modifyOtherKeys into the
1535 * character.
1536 */
1537 int
1538merge_modifyOtherKeys(int c_arg)
1539{
1540 int c = c_arg;
1541
1542 if (mod_mask & MOD_MASK_CTRL)
1543 {
1544 if ((c >= '`' && c <= 0x7f) || (c >= '@' && c <= '_'))
1545 {
1546 c &= 0x1f;
1547 mod_mask &= ~MOD_MASK_CTRL;
1548 }
1549 else if (c == '6')
1550 {
1551 // CTRL-6 is equivalent to CTRL-^
1552 c = 0x1e;
1553 mod_mask &= ~MOD_MASK_CTRL;
1554 }
1555 }
1556 if ((mod_mask & (MOD_MASK_META | MOD_MASK_ALT))
1557 && c >= 0 && c <= 127)
1558 {
1559 c += 0x80;
1560 mod_mask &= ~(MOD_MASK_META|MOD_MASK_ALT);
1561 }
1562 return c;
1563}
1564
1565/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 * Get the next input character.
1567 * Can return a special key or a multi-byte character.
1568 * Can return NUL when called recursively, use safe_vgetc() if that's not
1569 * wanted.
1570 * This translates escaped K_SPECIAL and CSI bytes to a K_SPECIAL or CSI byte.
1571 * Collects the bytes of a multibyte character into the whole character.
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00001572 * Returns the modifiers in the global "mod_mask".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 */
1574 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001575vgetc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576{
1577 int c, c2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001579 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001582#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01001583 // Do garbage collection when garbagecollect() was called previously and
1584 // we are now at the toplevel.
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001585 if (may_garbage_collect && want_garbage_collect)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001586 garbage_collect(FALSE);
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001587#endif
1588
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 /*
1590 * If a character was put back with vungetc, it was already processed.
1591 * Return it directly.
1592 */
1593 if (old_char != -1)
1594 {
1595 c = old_char;
1596 old_char = -1;
1597 mod_mask = old_mod_mask;
Bram Moolenaarb8978712013-03-16 21:42:16 +01001598 mouse_row = old_mouse_row;
1599 mouse_col = old_mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001601 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 {
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001603 mod_mask = 0x0;
1604 last_recorded_len = 0;
1605 for (;;) // this is done twice if there are modifiers
1606 {
1607 int did_inc = FALSE;
Bram Moolenaar2455c4e2015-09-15 18:29:39 +02001608
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001609 if (mod_mask
Bram Moolenaar39719052017-09-05 22:20:46 +02001610#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001611 || im_is_preediting()
Bram Moolenaar39719052017-09-05 22:20:46 +02001612#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001613#if defined(FEAT_PROP_POPUP)
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001614 || popup_no_mapping()
1615#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001616 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 {
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001618 // no mapping after modifier has been read
1619 ++no_mapping;
1620 ++allow_keys;
1621 did_inc = TRUE; // mod_mask may change value
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 }
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001623 c = vgetorpeek(TRUE);
1624 if (did_inc)
1625 {
1626 --no_mapping;
1627 --allow_keys;
1628 }
1629
1630 // Get two extra bytes for special keys
1631 if (c == K_SPECIAL
1632#ifdef FEAT_GUI
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001633 || (c == CSI)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001634#endif
1635 )
1636 {
1637 int save_allow_keys = allow_keys;
1638
1639 ++no_mapping;
1640 allow_keys = 0; // make sure BS is not found
1641 c2 = vgetorpeek(TRUE); // no mapping for these chars
1642 c = vgetorpeek(TRUE);
1643 --no_mapping;
1644 allow_keys = save_allow_keys;
1645 if (c2 == KS_MODIFIER)
1646 {
1647 mod_mask = c;
1648 continue;
1649 }
1650 c = TO_SPECIAL(c2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651
Bram Moolenaar4f974752019-02-17 17:44:42 +01001652#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001653 // Handle K_TEAROFF here, the caller of vgetc() doesn't need to
1654 // know that a menu was torn off
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001655 if (
1656# ifdef VIMDLL
1657 gui.in_use &&
1658# endif
1659 c == K_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 {
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001661 char_u name[200];
1662 int i;
1663
1664 // get menu path, it ends with a <CR>
1665 for (i = 0; (c = vgetorpeek(TRUE)) != '\r'; )
1666 {
1667 name[i] = c;
1668 if (i < 199)
1669 ++i;
1670 }
1671 name[i] = NUL;
1672 gui_make_tearoff(name);
1673 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675#endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001676#if defined(FEAT_GUI) && defined(FEAT_GUI_GTK) && defined(FEAT_MENU)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001677 // GTK: <F10> normally selects the menu, but it's passed until
1678 // here to allow mapping it. Intercept and invoke the GTK
1679 // behavior if it's not mapped.
1680 if (c == K_F10 && gui.menubar != NULL)
1681 {
1682 gtk_menu_shell_select_first(
1683 GTK_MENU_SHELL(gui.menubar), FALSE);
1684 continue;
1685 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001686#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687#ifdef FEAT_GUI
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001688 // Handle focus event here, so that the caller doesn't need to
1689 // know about it. Return K_IGNORE so that we loop once (needed
1690 // if 'lazyredraw' is set).
1691 if (c == K_FOCUSGAINED || c == K_FOCUSLOST)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001692 {
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001693 ui_focus_change(c == K_FOCUSGAINED);
1694 c = K_IGNORE;
Bram Moolenaara9dd2d32019-04-29 21:58:41 +02001695 }
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001696
1697 // Translate K_CSI to CSI. The special key is only used to
1698 // avoid it being recognized as the start of a special key.
1699 if (c == K_CSI)
1700 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001702 }
1703 // a keypad or special function key was not mapped, use it like
1704 // its ASCII equivalent
1705 switch (c)
1706 {
1707 case K_KPLUS: c = '+'; break;
1708 case K_KMINUS: c = '-'; break;
1709 case K_KDIVIDE: c = '/'; break;
1710 case K_KMULTIPLY: c = '*'; break;
1711 case K_KENTER: c = CAR; break;
1712 case K_KPOINT:
Bram Moolenaar4f974752019-02-17 17:44:42 +01001713#ifdef MSWIN
Bram Moolenaar8e85db02018-07-27 23:16:51 +02001714 // Can be either '.' or a ',',
1715 // depending on the type of keypad.
1716 c = MapVirtualKey(VK_DECIMAL, 2); break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001717#else
Bram Moolenaar8e85db02018-07-27 23:16:51 +02001718 c = '.'; break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001719#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001720 case K_K0: c = '0'; break;
1721 case K_K1: c = '1'; break;
1722 case K_K2: c = '2'; break;
1723 case K_K3: c = '3'; break;
1724 case K_K4: c = '4'; break;
1725 case K_K5: c = '5'; break;
1726 case K_K6: c = '6'; break;
1727 case K_K7: c = '7'; break;
1728 case K_K8: c = '8'; break;
1729 case K_K9: c = '9'; break;
Bram Moolenaara88d9682005-03-25 21:45:43 +00001730
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001731 case K_XHOME:
1732 case K_ZHOME: if (mod_mask == MOD_MASK_SHIFT)
Bram Moolenaara88d9682005-03-25 21:45:43 +00001733 {
1734 c = K_S_HOME;
1735 mod_mask = 0;
1736 }
1737 else if (mod_mask == MOD_MASK_CTRL)
1738 {
1739 c = K_C_HOME;
1740 mod_mask = 0;
1741 }
1742 else
1743 c = K_HOME;
1744 break;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001745 case K_XEND:
1746 case K_ZEND: if (mod_mask == MOD_MASK_SHIFT)
Bram Moolenaara88d9682005-03-25 21:45:43 +00001747 {
1748 c = K_S_END;
1749 mod_mask = 0;
1750 }
1751 else if (mod_mask == MOD_MASK_CTRL)
1752 {
1753 c = K_C_END;
1754 mod_mask = 0;
1755 }
1756 else
1757 c = K_END;
1758 break;
1759
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001760 case K_XUP: c = K_UP; break;
1761 case K_XDOWN: c = K_DOWN; break;
1762 case K_XLEFT: c = K_LEFT; break;
1763 case K_XRIGHT: c = K_RIGHT; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001766 // For a multi-byte character get all the bytes and return the
1767 // converted character.
1768 // Note: This will loop until enough bytes are received!
1769 if (has_mbyte && (n = MB_BYTE2LEN_CHECK(c)) > 1)
1770 {
1771 ++no_mapping;
1772 buf[0] = c;
1773 for (i = 1; i < n; ++i)
1774 {
1775 buf[i] = vgetorpeek(TRUE);
1776 if (buf[i] == K_SPECIAL
1777#ifdef FEAT_GUI
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001778 || (buf[i] == CSI)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001779#endif
1780 )
1781 {
1782 // Must be a K_SPECIAL - KS_SPECIAL - KE_FILLER
1783 // sequence, which represents a K_SPECIAL (0x80),
1784 // or a CSI - KS_EXTRA - KE_CSI sequence, which
1785 // represents a CSI (0x9B),
1786 // or a K_SPECIAL - KS_EXTRA - KE_CSI, which is CSI
1787 // too.
1788 c = vgetorpeek(TRUE);
1789 if (vgetorpeek(TRUE) == (int)KE_CSI && c == KS_EXTRA)
1790 buf[i] = CSI;
1791 }
1792 }
1793 --no_mapping;
1794 c = (*mb_ptr2char)(buf);
1795 }
1796
Bram Moolenaar00eab7f2019-10-10 21:49:28 +02001797 if (!no_reduce_keys)
Bram Moolenaar00eab7f2019-10-10 21:49:28 +02001798 // A modifier was not used for a mapping, apply it to ASCII
Bram Moolenaar459fd782019-10-13 16:43:39 +02001799 // keys. Shift would already have been applied.
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001800 c = merge_modifyOtherKeys(c);
Bram Moolenaar00eab7f2019-10-10 21:49:28 +02001801
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001802 break;
1803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001805
1806#ifdef FEAT_EVAL
1807 /*
1808 * In the main loop "may_garbage_collect" can be set to do garbage
1809 * collection in the first next vgetc(). It's disabled after that to
1810 * avoid internally used Lists and Dicts to be freed.
1811 */
1812 may_garbage_collect = FALSE;
1813#endif
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001814
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001815#ifdef FEAT_BEVAL_TERM
Bram Moolenaarc2f50542019-07-05 23:24:56 +02001816 if (c != K_MOUSEMOVE && c != K_IGNORE && c != K_CURSORHOLD)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001817 {
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001818 // Don't trigger 'balloonexpr' unless only the mouse was moved.
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001819 bevalexpr_due_set = FALSE;
1820 ui_remove_balloon();
1821 }
1822#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001823#ifdef FEAT_PROP_POPUP
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001824 if (popup_do_filter(c))
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02001825 {
1826 if (c == Ctrl_C)
1827 got_int = FALSE; // avoid looping
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001828 c = K_IGNORE;
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02001829 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001830#endif
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001831
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001832 // Need to process the character before we know it's safe to do something
1833 // else.
1834 if (c != K_IGNORE)
Bram Moolenaard103ee72019-09-18 21:15:31 +02001835 state_no_longer_safe("key typed");
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001836
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001837 return c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838}
1839
1840/*
1841 * Like vgetc(), but never return a NUL when called recursively, get a key
1842 * directly from the user (ignoring typeahead).
1843 */
1844 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001845safe_vgetc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846{
1847 int c;
1848
1849 c = vgetc();
1850 if (c == NUL)
1851 c = get_keystroke();
1852 return c;
1853}
1854
1855/*
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001856 * Like safe_vgetc(), but loop to handle K_IGNORE.
1857 * Also ignore scrollbar events.
1858 */
1859 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001860plain_vgetc(void)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001861{
1862 int c;
1863
1864 do
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001865 c = safe_vgetc();
Bram Moolenaarabab0b02019-03-30 18:47:01 +01001866 while (c == K_IGNORE || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR);
Bram Moolenaarec2da362017-01-21 20:04:22 +01001867
1868 if (c == K_PS)
Bram Moolenaar30613902019-12-01 22:11:18 +01001869 // Only handle the first pasted character. Drop the rest, since we
1870 // don't know what to do with it.
Bram Moolenaarec2da362017-01-21 20:04:22 +01001871 c = bracketed_paste(PASTE_ONE_CHAR, FALSE, NULL);
1872
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001873 return c;
1874}
1875
1876/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 * Check if a character is available, such that vgetc() will not block.
1878 * If the next character is a special character or multi-byte, the returned
1879 * character is not valid!.
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02001880 * Returns NUL if no character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 */
1882 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001883vpeekc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884{
1885 if (old_char != -1)
1886 return old_char;
1887 return vgetorpeek(FALSE);
1888}
1889
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01001890#if defined(FEAT_TERMRESPONSE) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891/*
1892 * Like vpeekc(), but don't allow mapping. Do allow checking for terminal
1893 * codes.
1894 */
1895 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001896vpeekc_nomap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897{
1898 int c;
1899
1900 ++no_mapping;
1901 ++allow_keys;
1902 c = vpeekc();
1903 --no_mapping;
1904 --allow_keys;
1905 return c;
1906}
1907#endif
1908
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909/*
1910 * Check if any character is available, also half an escape sequence.
1911 * Trick: when no typeahead found, but there is something in the typeahead
1912 * buffer, it must be an ESC that is recognized as the start of a key code.
1913 */
1914 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001915vpeekc_any(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916{
1917 int c;
1918
1919 c = vpeekc();
1920 if (c == NUL && typebuf.tb_len > 0)
1921 c = ESC;
1922 return c;
1923}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924
1925/*
1926 * Call vpeekc() without causing anything to be mapped.
1927 * Return TRUE if a character is available, FALSE otherwise.
1928 */
1929 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001930char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931{
1932 int retval;
1933
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01001934#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01001935 // When test_override("char_avail", 1) was called pretend there is no
1936 // typeahead.
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01001937 if (disable_char_avail_for_testing)
1938 return FALSE;
1939#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 ++no_mapping;
1941 retval = vpeekc();
1942 --no_mapping;
1943 return (retval != NUL);
1944}
1945
Bram Moolenaar9c658c92019-09-15 21:00:54 +02001946#if defined(FEAT_EVAL) || defined(PROTO)
1947/*
1948 * "getchar()" function
1949 */
1950 void
1951f_getchar(typval_T *argvars, typval_T *rettv)
1952{
1953 varnumber_T n;
1954 int error = FALSE;
1955
1956#ifdef MESSAGE_QUEUE
1957 // vpeekc() used to check for messages, but that caused problems, invoking
1958 // a callback where it was not expected. Some plugins use getchar(1) in a
1959 // loop to await a message, therefore make sure we check for messages here.
1960 parse_queued_messages();
1961#endif
1962
Bram Moolenaar30613902019-12-01 22:11:18 +01001963 // Position the cursor. Needed after a message that ends in a space.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02001964 windgoto(msg_row, msg_col);
1965
1966 ++no_mapping;
1967 ++allow_keys;
1968 for (;;)
1969 {
1970 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar30613902019-12-01 22:11:18 +01001971 // getchar(): blocking wait.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02001972 n = plain_vgetc();
1973 else if (tv_get_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar30613902019-12-01 22:11:18 +01001974 // getchar(1): only check if char avail
Bram Moolenaar9c658c92019-09-15 21:00:54 +02001975 n = vpeekc_any();
1976 else if (error || vpeekc_any() == NUL)
Bram Moolenaar30613902019-12-01 22:11:18 +01001977 // illegal argument or getchar(0) and no char avail: return zero
Bram Moolenaar9c658c92019-09-15 21:00:54 +02001978 n = 0;
1979 else
Bram Moolenaar30613902019-12-01 22:11:18 +01001980 // getchar(0) and char avail: return char
Bram Moolenaar9c658c92019-09-15 21:00:54 +02001981 n = plain_vgetc();
1982
1983 if (n == K_IGNORE)
1984 continue;
1985 break;
1986 }
1987 --no_mapping;
1988 --allow_keys;
1989
1990 set_vim_var_nr(VV_MOUSE_WIN, 0);
1991 set_vim_var_nr(VV_MOUSE_WINID, 0);
1992 set_vim_var_nr(VV_MOUSE_LNUM, 0);
1993 set_vim_var_nr(VV_MOUSE_COL, 0);
1994
1995 rettv->vval.v_number = n;
1996 if (IS_SPECIAL(n) || mod_mask != 0)
1997 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001998 char_u temp[10]; // modifier: 3, mbyte-char: 6, NUL: 1
Bram Moolenaar9c658c92019-09-15 21:00:54 +02001999 int i = 0;
2000
Bram Moolenaar30613902019-12-01 22:11:18 +01002001 // Turn a special key into three bytes, plus modifier.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002002 if (mod_mask != 0)
2003 {
2004 temp[i++] = K_SPECIAL;
2005 temp[i++] = KS_MODIFIER;
2006 temp[i++] = mod_mask;
2007 }
2008 if (IS_SPECIAL(n))
2009 {
2010 temp[i++] = K_SPECIAL;
2011 temp[i++] = K_SECOND(n);
2012 temp[i++] = K_THIRD(n);
2013 }
2014 else if (has_mbyte)
2015 i += (*mb_char2bytes)(n, temp + i);
2016 else
2017 temp[i++] = n;
2018 temp[i++] = NUL;
2019 rettv->v_type = VAR_STRING;
2020 rettv->vval.v_string = vim_strsave(temp);
2021
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002022 if (is_mouse_key(n))
2023 {
2024 int row = mouse_row;
2025 int col = mouse_col;
2026 win_T *win;
2027 linenr_T lnum;
2028 win_T *wp;
2029 int winnr = 1;
2030
2031 if (row >= 0 && col >= 0)
2032 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002033 // Find the window at the mouse coordinates and compute the
2034 // text position.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002035 win = mouse_find_win(&row, &col, FIND_POPUP);
2036 if (win == NULL)
2037 return;
2038 (void)mouse_comp_pos(win, &row, &col, &lnum, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002039#ifdef FEAT_PROP_POPUP
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002040 if (WIN_IS_POPUP(win))
2041 winnr = 0;
2042 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002043#endif
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002044 for (wp = firstwin; wp != win && wp != NULL;
2045 wp = wp->w_next)
2046 ++winnr;
2047 set_vim_var_nr(VV_MOUSE_WIN, winnr);
2048 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
2049 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
2050 set_vim_var_nr(VV_MOUSE_COL, col + 1);
2051 }
2052 }
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002053 }
2054}
2055
2056/*
2057 * "getcharmod()" function
2058 */
2059 void
2060f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
2061{
2062 rettv->vval.v_number = mod_mask;
2063}
2064#endif // FEAT_EVAL
2065
2066#if defined(MESSAGE_QUEUE) || defined(PROTO)
2067# define MAX_REPEAT_PARSE 8
2068
2069/*
2070 * Process messages that have been queued for netbeans or clientserver.
2071 * Also check if any jobs have ended.
2072 * These functions can call arbitrary vimscript and should only be called when
2073 * it is safe to do so.
2074 */
2075 void
2076parse_queued_messages(void)
2077{
Bram Moolenaar1cac7092019-10-22 21:54:31 +02002078 int old_curwin_id;
2079 int old_curbuf_fnum;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002080 int i;
2081 int save_may_garbage_collect = may_garbage_collect;
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002082 static int entered = 0;
Bram Moolenaard103ee72019-09-18 21:15:31 +02002083 int was_safe = get_was_safe_state();
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002084
2085 // Do not handle messages while redrawing, because it may cause buffers to
2086 // change or be wiped while they are being redrawn.
2087 if (updating_screen)
2088 return;
2089
Bram Moolenaar1cac7092019-10-22 21:54:31 +02002090 // If memory allocation fails during startup we'll exit but curbuf or
2091 // curwin could be NULL.
2092 if (curbuf == NULL || curwin == NULL)
2093 return;
2094
2095 old_curbuf_fnum = curbuf->b_fnum;
2096 old_curwin_id = curwin->w_id;
2097
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002098 ++entered;
2099
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002100 // may_garbage_collect is set in main_loop() to do garbage collection when
2101 // blocking to wait on a character. We don't want that while parsing
2102 // messages, a callback may invoke vgetc() while lists and dicts are in use
2103 // in the call stack.
2104 may_garbage_collect = FALSE;
2105
2106 // Loop when a job ended, but don't keep looping forever.
2107 for (i = 0; i < MAX_REPEAT_PARSE; ++i)
2108 {
2109 // For Win32 mch_breakcheck() does not check for input, do it here.
2110# if defined(MSWIN) && defined(FEAT_JOB_CHANNEL)
2111 channel_handle_events(FALSE);
2112# endif
2113
2114# ifdef FEAT_NETBEANS_INTG
2115 // Process the queued netbeans messages.
2116 netbeans_parse_messages();
2117# endif
2118# ifdef FEAT_JOB_CHANNEL
2119 // Write any buffer lines still to be written.
2120 channel_write_any_lines();
2121
2122 // Process the messages queued on channels.
2123 channel_parse_messages();
2124# endif
2125# if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
2126 // Process the queued clientserver messages.
2127 server_parse_messages();
2128# endif
2129# ifdef FEAT_JOB_CHANNEL
2130 // Check if any jobs have ended. If so, repeat the above to handle
2131 // changes, e.g. stdin may have been closed.
2132 if (job_check_ended())
2133 continue;
2134# endif
2135# ifdef FEAT_TERMINAL
2136 free_unused_terminals();
2137# endif
2138# ifdef FEAT_SOUND_CANBERRA
2139 if (has_sound_callback_in_queue())
2140 invoke_sound_callback();
2141# endif
2142 break;
2143 }
2144
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002145 // When not nested we'll go back to waiting for a typed character. If it
2146 // was safe before then this triggers a SafeStateAgain autocommand event.
Bram Moolenaard103ee72019-09-18 21:15:31 +02002147 if (entered == 1 && was_safe)
Bram Moolenaar37d18072019-09-17 20:28:38 +02002148 may_trigger_safestateagain();
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002149
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002150 may_garbage_collect = save_may_garbage_collect;
2151
2152 // If the current window or buffer changed we need to bail out of the
2153 // waiting loop. E.g. when a job exit callback closes the terminal window.
2154 if (curwin->w_id != old_curwin_id || curbuf->b_fnum != old_curbuf_fnum)
2155 ins_char_typebuf(K_IGNORE);
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002156
2157 --entered;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002158}
2159#endif
2160
2161
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002162typedef enum {
2163 map_result_fail, // failed, break loop
2164 map_result_get, // get a character from typeahead
2165 map_result_retry, // try to map again
2166 map_result_nomatch // no matching mapping, get char
2167} map_result_T;
2168
2169/*
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002170 * Check if the bytes at the start of the typeahead buffer are a character used
2171 * in CTRL-X mode. This includes the form with a CTRL modifier.
2172 */
2173 static int
2174at_ctrl_x_key(void)
2175{
2176 char_u *p = typebuf.tb_buf + typebuf.tb_off;
2177 int c = *p;
2178
2179 if (typebuf.tb_len > 3
2180 && c == K_SPECIAL
2181 && p[1] == KS_MODIFIER
2182 && (p[2] & MOD_MASK_CTRL))
2183 c = p[3] & 0x1f;
2184 return vim_is_ctrl_x_key(c);
2185}
2186
2187/*
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002188 * Handle mappings in the typeahead buffer.
2189 * - When something was mapped, return map_result_retry for recursive mappings.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002190 * - When nothing mapped and typeahead has a character: return map_result_get.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002191 * - When there is no match yet, return map_result_nomatch, need to get more
2192 * typeahead.
2193 */
2194 static int
2195handle_mapping(
2196 int *keylenp,
2197 int *timedout,
2198 int *mapdepth)
2199{
2200 mapblock_T *mp = NULL;
2201 mapblock_T *mp2;
2202 mapblock_T *mp_match;
2203 int mp_match_len = 0;
2204 int max_mlen = 0;
2205 int tb_c1;
2206 int mlen;
2207#ifdef FEAT_LANGMAP
2208 int nolmaplen;
2209#endif
2210 int keylen = *keylenp;
2211 int i;
2212 int local_State = get_real_state();
2213
2214 /*
2215 * Check for a mappable key sequence.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002216 * Walk through one maphash[] list until we find an entry that matches.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002217 *
2218 * Don't look for mappings if:
2219 * - no_mapping set: mapping disabled (e.g. for CTRL-V)
2220 * - maphash_valid not set: no mappings present.
2221 * - typebuf.tb_buf[typebuf.tb_off] should not be remapped
2222 * - in insert or cmdline mode and 'paste' option set
Bram Moolenaareda35f72019-08-03 14:59:44 +02002223 * - waiting for "hit return to continue" and CR or SPACE typed
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002224 * - waiting for a char with --more--
2225 * - in Ctrl-X mode, and we get a valid char for that mode
2226 */
2227 tb_c1 = typebuf.tb_buf[typebuf.tb_off];
2228 if (no_mapping == 0 && is_maphash_valid()
2229 && (no_zero_mapping == 0 || tb_c1 != '0')
2230 && (typebuf.tb_maplen == 0
2231 || (p_remap
2232 && (typebuf.tb_noremap[typebuf.tb_off]
2233 & (RM_NONE|RM_ABBR)) == 0))
2234 && !(p_paste && (State & (INSERT + CMDLINE)))
2235 && !(State == HITRETURN && (tb_c1 == CAR || tb_c1 == ' '))
2236 && State != ASKMORE
2237 && State != CONFIRM
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002238 && !((ctrl_x_mode_not_default() && at_ctrl_x_key())
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002239 || ((compl_cont_status & CONT_LOCAL)
Bram Moolenaare2c453d2019-08-21 14:37:09 +02002240 && (tb_c1 == Ctrl_N || tb_c1 == Ctrl_P))))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002241 {
2242#ifdef FEAT_LANGMAP
2243 if (tb_c1 == K_SPECIAL)
2244 nolmaplen = 2;
2245 else
2246 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002247 LANGMAP_ADJUST(tb_c1, (State & (CMDLINE | INSERT)) == 0
2248 && get_real_state() != SELECTMODE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002249 nolmaplen = 0;
2250 }
2251#endif
2252 // First try buffer-local mappings.
2253 mp = get_buf_maphash_list(local_State, tb_c1);
2254 mp2 = get_maphash_list(local_State, tb_c1);
2255 if (mp == NULL)
2256 {
2257 // There are no buffer-local mappings.
2258 mp = mp2;
2259 mp2 = NULL;
2260 }
Bram Moolenaareda35f72019-08-03 14:59:44 +02002261
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002262 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002263 * Loop until a partly matching mapping is found or all (local)
2264 * mappings have been checked.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002265 * The longest full match is remembered in "mp_match".
Bram Moolenaareda35f72019-08-03 14:59:44 +02002266 * A full match is only accepted if there is no partly match, so "aa"
2267 * and "aaa" can both be mapped.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002268 */
2269 mp_match = NULL;
2270 mp_match_len = 0;
2271 for ( ; mp != NULL;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002272 mp->m_next == NULL ? (mp = mp2, mp2 = NULL) : (mp = mp->m_next))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002273 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002274 // Only consider an entry if the first character matches and it is
2275 // for the current state.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002276 // Skip ":lmap" mappings if keys were mapped.
2277 if (mp->m_keys[0] == tb_c1
2278 && (mp->m_mode & local_State)
Bram Moolenaar459fd782019-10-13 16:43:39 +02002279 && !(mp->m_simplified && seenModifyOtherKeys)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002280 && ((mp->m_mode & LANGMAP) == 0 || typebuf.tb_maplen == 0))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002281 {
2282#ifdef FEAT_LANGMAP
2283 int nomap = nolmaplen;
2284 int c2;
2285#endif
2286 // find the match length of this mapping
2287 for (mlen = 1; mlen < typebuf.tb_len; ++mlen)
2288 {
2289#ifdef FEAT_LANGMAP
2290 c2 = typebuf.tb_buf[typebuf.tb_off + mlen];
2291 if (nomap > 0)
2292 --nomap;
2293 else if (c2 == K_SPECIAL)
2294 nomap = 2;
2295 else
2296 LANGMAP_ADJUST(c2, TRUE);
2297 if (mp->m_keys[mlen] != c2)
2298#else
2299 if (mp->m_keys[mlen] !=
2300 typebuf.tb_buf[typebuf.tb_off + mlen])
2301#endif
2302 break;
2303 }
2304
Bram Moolenaareda35f72019-08-03 14:59:44 +02002305 // Don't allow mapping the first byte(s) of a multi-byte char.
2306 // Happens when mapping <M-a> and then changing 'encoding'.
2307 // Beware that 0x80 is escaped.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002308 {
2309 char_u *p1 = mp->m_keys;
2310 char_u *p2 = mb_unescape(&p1);
2311
2312 if (has_mbyte && p2 != NULL
Bram Moolenaar1614a142019-10-06 22:00:13 +02002313 && MB_BYTE2LEN(tb_c1) > mb_ptr2len(p2))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002314 mlen = 0;
2315 }
2316
2317 // Check an entry whether it matches.
2318 // - Full match: mlen == keylen
2319 // - Partly match: mlen == typebuf.tb_len
2320 keylen = mp->m_keylen;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002321 if (mlen == keylen || (mlen == typebuf.tb_len
2322 && typebuf.tb_len < keylen))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002323 {
2324 char_u *s;
2325 int n;
2326
Bram Moolenaareda35f72019-08-03 14:59:44 +02002327 // If only script-local mappings are allowed, check if the
2328 // mapping starts with K_SNR.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002329 s = typebuf.tb_noremap + typebuf.tb_off;
2330 if (*s == RM_SCRIPT
2331 && (mp->m_keys[0] != K_SPECIAL
2332 || mp->m_keys[1] != KS_EXTRA
Bram Moolenaareda35f72019-08-03 14:59:44 +02002333 || mp->m_keys[2] != (int)KE_SNR))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002334 continue;
2335
Bram Moolenaareda35f72019-08-03 14:59:44 +02002336 // If one of the typed keys cannot be remapped, skip the
2337 // entry.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002338 for (n = mlen; --n >= 0; )
2339 if (*s++ & (RM_NONE|RM_ABBR))
2340 break;
2341 if (n >= 0)
2342 continue;
2343
2344 if (keylen > typebuf.tb_len)
2345 {
2346 if (!*timedout && !(mp_match != NULL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002347 && mp_match->m_nowait))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002348 {
2349 // break at a partly match
2350 keylen = KEYLEN_PART_MAP;
2351 break;
2352 }
2353 }
2354 else if (keylen > mp_match_len)
2355 {
2356 // found a longer match
2357 mp_match = mp;
2358 mp_match_len = keylen;
2359 }
2360 }
2361 else
Bram Moolenaareda35f72019-08-03 14:59:44 +02002362 // No match; may have to check for termcode at next
2363 // character.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002364 if (max_mlen < mlen)
2365 max_mlen = mlen;
2366 }
2367 }
2368
Bram Moolenaareda35f72019-08-03 14:59:44 +02002369 // If no partly match found, use the longest full match.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002370 if (keylen != KEYLEN_PART_MAP)
2371 {
2372 mp = mp_match;
2373 keylen = mp_match_len;
2374 }
2375 }
2376
2377 /*
2378 * Check for match with 'pastetoggle'
2379 */
2380 if (*p_pt != NUL && mp == NULL && (State & (INSERT|NORMAL)))
2381 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002382 for (mlen = 0; mlen < typebuf.tb_len && p_pt[mlen]; ++mlen)
2383 if (p_pt[mlen] != typebuf.tb_buf[typebuf.tb_off + mlen])
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002384 break;
2385 if (p_pt[mlen] == NUL) // match
2386 {
2387 // write chars to script file(s)
2388 if (mlen > typebuf.tb_maplen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002389 gotchars(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_maplen,
2390 mlen - typebuf.tb_maplen);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002391
2392 del_typebuf(mlen, 0); // remove the chars
Bram Moolenaareda35f72019-08-03 14:59:44 +02002393 set_option_value((char_u *)"paste", (long)!p_paste, NULL, 0);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002394 if (!(State & INSERT))
2395 {
2396 msg_col = 0;
2397 msg_row = Rows - 1;
2398 msg_clr_eos(); // clear ruler
2399 }
2400 status_redraw_all();
2401 redraw_statuslines();
2402 showmode();
2403 setcursor();
2404 *keylenp = keylen;
2405 return map_result_retry;
2406 }
2407 // Need more chars for partly match.
2408 if (mlen == typebuf.tb_len)
2409 keylen = KEYLEN_PART_KEY;
2410 else if (max_mlen < mlen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002411 // no match, may have to check for termcode at next character
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002412 max_mlen = mlen + 1;
2413 }
2414
Bram Moolenaareda35f72019-08-03 14:59:44 +02002415 if ((mp == NULL || max_mlen >= mp_match_len) && keylen != KEYLEN_PART_MAP)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002416 {
2417 int save_keylen = keylen;
2418
2419 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002420 * When no matching mapping found or found a non-matching mapping that
2421 * matches at least what the matching mapping matched:
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002422 * Check if we have a terminal code, when:
Bram Moolenaareda35f72019-08-03 14:59:44 +02002423 * - mapping is allowed,
2424 * - keys have not been mapped,
2425 * - and not an ESC sequence, not in insert mode or p_ek is on,
2426 * - and when not timed out,
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002427 */
2428 if ((no_mapping == 0 || allow_keys != 0)
2429 && (typebuf.tb_maplen == 0
2430 || (p_remap && typebuf.tb_noremap[
Bram Moolenaareda35f72019-08-03 14:59:44 +02002431 typebuf.tb_off] == RM_YES))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002432 && !*timedout)
2433 {
2434 keylen = check_termcode(max_mlen + 1,
2435 NULL, 0, NULL);
2436
Bram Moolenaareda35f72019-08-03 14:59:44 +02002437 // If no termcode matched but 'pastetoggle' matched partially it's
2438 // like an incomplete key sequence.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002439 if (keylen == 0 && save_keylen == KEYLEN_PART_KEY)
2440 keylen = KEYLEN_PART_KEY;
2441
Bram Moolenaareda35f72019-08-03 14:59:44 +02002442 // When getting a partial match, but the last characters were not
2443 // typed, don't wait for a typed character to complete the
2444 // termcode. This helps a lot when a ":normal" command ends in an
2445 // ESC.
2446 if (keylen < 0 && typebuf.tb_len == typebuf.tb_maplen)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002447 keylen = 0;
2448 }
2449 else
2450 keylen = 0;
2451 if (keylen == 0) // no matching terminal code
2452 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002453#ifdef AMIGA
2454 // check for window bounds report
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002455 if (typebuf.tb_maplen == 0 && (typebuf.tb_buf[
Bram Moolenaareda35f72019-08-03 14:59:44 +02002456 typebuf.tb_off] & 0xff) == CSI)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002457 {
2458 char_u *s;
2459
2460 for (s = typebuf.tb_buf + typebuf.tb_off + 1;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002461 s < typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len
2462 && (VIM_ISDIGIT(*s) || *s == ';' || *s == ' ');
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002463 ++s)
2464 ;
2465 if (*s == 'r' || *s == '|') // found one
2466 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002467 del_typebuf(
2468 (int)(s + 1 - (typebuf.tb_buf + typebuf.tb_off)), 0);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002469 // get size and redraw screen
2470 shell_resized();
2471 *keylenp = keylen;
2472 return map_result_retry;
2473 }
2474 if (*s == NUL) // need more characters
2475 keylen = KEYLEN_PART_KEY;
2476 }
2477 if (keylen >= 0)
2478#endif
Bram Moolenaareda35f72019-08-03 14:59:44 +02002479 // When there was a matching mapping and no termcode could be
2480 // replaced after another one, use that mapping (loop around).
2481 // If there was no mapping at all use the character from the
2482 // typeahead buffer right here.
2483 if (mp == NULL)
2484 {
2485 *keylenp = keylen;
2486 return map_result_get; // got character, break for loop
2487 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002488 }
2489
2490 if (keylen > 0) // full matching terminal code
2491 {
2492#if defined(FEAT_GUI) && defined(FEAT_MENU)
2493 if (typebuf.tb_len >= 2
Bram Moolenaareda35f72019-08-03 14:59:44 +02002494 && typebuf.tb_buf[typebuf.tb_off] == K_SPECIAL
2495 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_MENU)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002496 {
2497 int idx;
2498
Bram Moolenaareda35f72019-08-03 14:59:44 +02002499 // Using a menu may cause a break in undo! It's like using
2500 // gotchars(), but without recording or writing to a script
2501 // file.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002502 may_sync_undo();
2503 del_typebuf(3, 0);
2504 idx = get_menu_index(current_menu, local_State);
2505 if (idx != MENU_INDEX_INVALID)
2506 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002507 // In Select mode and a Visual mode menu is used: Switch
2508 // to Visual mode temporarily. Append K_SELECT to switch
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002509 // back to Select mode.
2510 if (VIsual_active && VIsual_select
Bram Moolenaareda35f72019-08-03 14:59:44 +02002511 && (current_menu->modes & VISUAL))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002512 {
2513 VIsual_select = FALSE;
2514 (void)ins_typebuf(K_SELECT_STRING,
Bram Moolenaareda35f72019-08-03 14:59:44 +02002515 REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002516 }
2517 ins_typebuf(current_menu->strings[idx],
2518 current_menu->noremap[idx],
Bram Moolenaareda35f72019-08-03 14:59:44 +02002519 0, TRUE, current_menu->silent[idx]);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002520 }
2521 }
2522#endif // FEAT_GUI && FEAT_MENU
2523 *keylenp = keylen;
2524 return map_result_retry; // try mapping again
2525 }
2526
Bram Moolenaareda35f72019-08-03 14:59:44 +02002527 // Partial match: get some more characters. When a matching mapping
2528 // was found use that one.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002529 if (mp == NULL || keylen < 0)
2530 keylen = KEYLEN_PART_KEY;
2531 else
2532 keylen = mp_match_len;
2533 }
2534
2535 /*
2536 * complete match
2537 */
2538 if (keylen >= 0 && keylen <= typebuf.tb_len)
2539 {
2540 char_u *map_str;
2541
2542#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002543 int save_m_expr;
2544 int save_m_noremap;
2545 int save_m_silent;
2546 char_u *save_m_keys;
2547 char_u *save_m_str;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002548#else
2549# define save_m_noremap mp->m_noremap
2550# define save_m_silent mp->m_silent
2551#endif
2552
2553 // write chars to script file(s)
2554 if (keylen > typebuf.tb_maplen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002555 gotchars(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_maplen,
2556 keylen - typebuf.tb_maplen);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002557
2558 cmd_silent = (typebuf.tb_silent > 0);
2559 del_typebuf(keylen, 0); // remove the mapped keys
2560
2561 /*
2562 * Put the replacement string in front of mapstr.
2563 * The depth check catches ":map x y" and ":map y x".
2564 */
2565 if (++*mapdepth >= p_mmd)
2566 {
2567 emsg(_("E223: recursive mapping"));
2568 if (State & CMDLINE)
2569 redrawcmdline();
2570 else
2571 setcursor();
2572 flush_buffers(FLUSH_MINIMAL);
Bram Moolenaar30613902019-12-01 22:11:18 +01002573 *mapdepth = 0; // for next one
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002574 *keylenp = keylen;
2575 return map_result_fail;
2576 }
2577
2578 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002579 * In Select mode and a Visual mode mapping is used: Switch to Visual
2580 * mode temporarily. Append K_SELECT to switch back to Select mode.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002581 */
Bram Moolenaareda35f72019-08-03 14:59:44 +02002582 if (VIsual_active && VIsual_select && (mp->m_mode & VISUAL))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002583 {
2584 VIsual_select = FALSE;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002585 (void)ins_typebuf(K_SELECT_STRING, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002586 }
2587
2588#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002589 // Copy the values from *mp that are used, because evaluating the
2590 // expression may invoke a function that redefines the mapping, thereby
2591 // making *mp invalid.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002592 save_m_expr = mp->m_expr;
2593 save_m_noremap = mp->m_noremap;
2594 save_m_silent = mp->m_silent;
2595 save_m_keys = NULL; // only saved when needed
2596 save_m_str = NULL; // only saved when needed
2597
2598 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002599 * Handle ":map <expr>": evaluate the {rhs} as an expression. Also
2600 * save and restore the command line for "normal :".
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002601 */
2602 if (mp->m_expr)
2603 {
2604 int save_vgetc_busy = vgetc_busy;
2605 int save_may_garbage_collect = may_garbage_collect;
Bram Moolenaar4ebe0e62019-11-22 20:55:40 +01002606 int was_screen_col = screen_cur_col;
2607 int was_screen_row = screen_cur_row;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002608
2609 vgetc_busy = 0;
2610 may_garbage_collect = FALSE;
2611
2612 save_m_keys = vim_strsave(mp->m_keys);
2613 save_m_str = vim_strsave(mp->m_str);
2614 map_str = eval_map_expr(save_m_str, NUL);
2615
Bram Moolenaar4ebe0e62019-11-22 20:55:40 +01002616 // The mapping may do anything, but we expect it to take care of
2617 // redrawing. Do put the cursor back where it was.
2618 windgoto(was_screen_row, was_screen_col);
2619 out_flush();
2620
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002621 vgetc_busy = save_vgetc_busy;
2622 may_garbage_collect = save_may_garbage_collect;
2623 }
2624 else
2625#endif
2626 map_str = mp->m_str;
2627
2628 /*
2629 * Insert the 'to' part in the typebuf.tb_buf.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002630 * If 'from' field is the same as the start of the 'to' field, don't
2631 * remap the first character (but do allow abbreviations).
2632 * If m_noremap is set, don't remap the whole 'to' part.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002633 */
2634 if (map_str == NULL)
2635 i = FAIL;
2636 else
2637 {
2638 int noremap;
2639
2640 if (save_m_noremap != REMAP_YES)
2641 noremap = save_m_noremap;
2642 else if (
2643#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002644 STRNCMP(map_str, save_m_keys != NULL ? save_m_keys : mp->m_keys,
2645 (size_t)keylen)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002646#else
2647 STRNCMP(map_str, mp->m_keys, (size_t)keylen)
2648#endif
2649 != 0)
2650 noremap = REMAP_YES;
2651 else
2652 noremap = REMAP_SKIP;
2653 i = ins_typebuf(map_str, noremap,
2654 0, TRUE, cmd_silent || save_m_silent);
2655#ifdef FEAT_EVAL
2656 if (save_m_expr)
2657 vim_free(map_str);
2658#endif
2659 }
2660#ifdef FEAT_EVAL
2661 vim_free(save_m_keys);
2662 vim_free(save_m_str);
2663#endif
2664 *keylenp = keylen;
2665 if (i == FAIL)
2666 return map_result_fail;
2667 return map_result_retry;
2668 }
2669
2670 *keylenp = keylen;
2671 return map_result_nomatch;
2672}
2673
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002674/*
2675 * unget one character (can only be done once!)
2676 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002678vungetc(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679{
2680 old_char = c;
2681 old_mod_mask = mod_mask;
Bram Moolenaarb8978712013-03-16 21:42:16 +01002682 old_mouse_row = mouse_row;
2683 old_mouse_col = mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684}
2685
2686/*
Bram Moolenaar32526b32019-01-19 17:43:09 +01002687 * Get a byte:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 * 1. from the stuffbuffer
2689 * This is used for abbreviated commands like "D" -> "d$".
2690 * Also used to redo a command for ".".
2691 * 2. from the typeahead buffer
2692 * Stores text obtained previously but not used yet.
2693 * Also stores the result of mappings.
2694 * Also used for the ":normal" command.
2695 * 3. from the user
2696 * This may do a blocking wait if "advance" is TRUE.
2697 *
2698 * if "advance" is TRUE (vgetc()):
Bram Moolenaard29459b2016-08-26 22:29:11 +02002699 * Really get the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 * KeyTyped is set to TRUE in the case the user typed the key.
2701 * KeyStuffed is TRUE if the character comes from the stuff buffer.
2702 * if "advance" is FALSE (vpeekc()):
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02002703 * Just look whether there is a character available.
2704 * Return NUL if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 *
2706 * When "no_mapping" is zero, checks for mappings in the current mode.
2707 * Only returns one byte (of a multi-byte character).
2708 * K_SPECIAL and CSI may be escaped, need to get two more bytes then.
2709 */
2710 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002711vgetorpeek(int advance)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712{
2713 int c, c1;
Bram Moolenaar30613902019-12-01 22:11:18 +01002714 int timedout = FALSE; // waited for more than 1 second
2715 // for mapping to complete
2716 int mapdepth = 0; // check for recursive mapping
2717 int mode_deleted = FALSE; // set when mode has been deleted
Bram Moolenaar4e427192006-03-10 21:34:27 +00002718#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 int new_wcol, new_wrow;
2720#endif
2721#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01002722 int shape_changed = FALSE; // adjusted cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723#endif
2724 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 int old_wcol, old_wrow;
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00002726 int wait_tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727
2728 /*
2729 * This function doesn't work very well when called recursively. This may
2730 * happen though, because of:
2731 * 1. The call to add_to_showcmd(). char_avail() is then used to check if
2732 * there is a character available, which calls this function. In that
2733 * case we must return NUL, to indicate no character is available.
2734 * 2. A GUI callback function writes to the screen, causing a
2735 * wait_return().
2736 * Using ":normal" can also do this, but it saves the typeahead buffer,
2737 * thus it should be OK. But don't get a key from the user then.
2738 */
Bram Moolenaare2c38102016-01-31 14:55:40 +01002739 if (vgetc_busy > 0 && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 return NUL;
2741
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002742 ++vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743
2744 if (advance)
2745 KeyStuffed = FALSE;
2746
2747 init_typebuf();
2748 start_stuff();
2749 if (advance && typebuf.tb_maplen == 0)
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02002750 reg_executing = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 do
2752 {
2753/*
2754 * get a character: 1. from the stuffbuffer
2755 */
2756 if (typeahead_char != 0)
2757 {
2758 c = typeahead_char;
2759 if (advance)
2760 typeahead_char = 0;
2761 }
2762 else
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01002763 c = read_readbuffers(advance);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 if (c != NUL && !got_int)
2765 {
2766 if (advance)
2767 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002768 // KeyTyped = FALSE; When the command that stuffed something
2769 // was typed, behave like the stuffed command was typed.
2770 // needed for CTRL-W CTRL-] to open a fold, for example.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 KeyStuffed = TRUE;
2772 }
2773 if (typebuf.tb_no_abbr_cnt == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01002774 typebuf.tb_no_abbr_cnt = 1; // no abbreviations now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 }
2776 else
2777 {
2778 /*
2779 * Loop until we either find a matching mapped key, or we
2780 * are sure that it is not a mapped key.
2781 * If a mapped key sequence is found we go back to the start to
2782 * try re-mapping.
2783 */
2784 for (;;)
2785 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002786 long wait_time;
2787 int keylen = 0;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002788#ifdef FEAT_CMDL_INFO
2789 int showcmd_idx;
2790#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 /*
2792 * ui_breakcheck() is slow, don't use it too often when
2793 * inside a mapping. But call it each time for typed
2794 * characters.
2795 */
2796 if (typebuf.tb_maplen)
2797 line_breakcheck();
2798 else
Bram Moolenaar30613902019-12-01 22:11:18 +01002799 ui_breakcheck(); // check for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 if (got_int)
2801 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002802 // flush all input
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02002803 c = inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 0L);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002804
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 /*
2806 * If inchar() returns TRUE (script file was active) or we
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002807 * are inside a mapping, get out of Insert mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 * Otherwise we behave like having gotten a CTRL-C.
2809 * As a result typing CTRL-C in insert mode will
2810 * really insert a CTRL-C.
2811 */
2812 if ((c || typebuf.tb_maplen)
2813 && (State & (INSERT + CMDLINE)))
2814 c = ESC;
2815 else
2816 c = Ctrl_C;
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02002817 flush_buffers(FLUSH_INPUT); // flush all typeahead
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818
Bram Moolenaard9dfd572006-10-03 13:36:13 +00002819 if (advance)
2820 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002821 // Also record this character, it might be needed to
2822 // get out of Insert mode.
Bram Moolenaard9dfd572006-10-03 13:36:13 +00002823 *typebuf.tb_buf = c;
2824 gotchars(typebuf.tb_buf, 1);
2825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 cmd_silent = FALSE;
2827
2828 break;
2829 }
2830 else if (typebuf.tb_len > 0)
2831 {
2832 /*
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002833 * Check for a mapping in "typebuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 */
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002835 map_result_T result = handle_mapping(
2836 &keylen, &timedout, &mapdepth);
2837
2838 if (result == map_result_retry)
2839 // try mapping again
2840 continue;
2841 if (result == map_result_fail)
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02002842 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002843 // failed, use the outer loop
2844 c = -1;
2845 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002847 if (result == map_result_get)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849/*
2850 * get a character: 2. from the typeahead buffer
2851 */
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002852 c = typebuf.tb_buf[typebuf.tb_off];
Bram Moolenaar30613902019-12-01 22:11:18 +01002853 if (advance) // remove chars from tb_buf
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02002854 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002855 cmd_silent = (typebuf.tb_silent > 0);
2856 if (typebuf.tb_maplen > 0)
2857 KeyTyped = FALSE;
2858 else
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02002859 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002860 KeyTyped = TRUE;
Bram Moolenaar30613902019-12-01 22:11:18 +01002861 // write char to script file(s)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002862 gotchars(typebuf.tb_buf
2863 + typebuf.tb_off, 1);
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02002864 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002865 KeyNoremap = typebuf.tb_noremap[
2866 typebuf.tb_off];
2867 del_typebuf(1, 0);
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02002868 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002869 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 }
2871
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002872 // not enough characters, get more
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 }
2874
2875/*
2876 * get a character: 3. from the user - handle <Esc> in Insert mode
2877 */
2878 /*
Bram Moolenaarf085f422017-06-07 20:39:47 +02002879 * Special case: if we get an <ESC> in insert mode and there
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 * are no more characters at once, we pretend to go out of
2881 * insert mode. This prevents the one second delay after
2882 * typing an <ESC>. If we get something after all, we may
2883 * have to redisplay the mode. That the cursor is in the wrong
2884 * place does not matter.
2885 */
2886 c = 0;
2887#ifdef FEAT_CMDL_INFO
2888 new_wcol = curwin->w_wcol;
2889 new_wrow = curwin->w_wrow;
2890#endif
2891 if ( advance
2892 && typebuf.tb_len == 1
2893 && typebuf.tb_buf[typebuf.tb_off] == ESC
2894 && !no_mapping
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 && ex_normal_busy == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 && typebuf.tb_maplen == 0
2897 && (State & INSERT)
Bram Moolenaar946ffd42010-12-30 12:30:31 +01002898 && (p_timeout
2899 || (keylen == KEYLEN_PART_KEY && p_ttimeout))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 && (c = inchar(typebuf.tb_buf + typebuf.tb_off
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02002901 + typebuf.tb_len, 3, 25L)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 {
2903 colnr_T col = 0, vcol;
2904 char_u *ptr;
2905
Bram Moolenaar28c258f2006-01-25 22:02:51 +00002906 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 {
2908 unshowmode(TRUE);
2909 mode_deleted = TRUE;
2910 }
2911#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01002912 // may show a different cursor shape
Bram Moolenaarf085f422017-06-07 20:39:47 +02002913 if (gui.in_use && State != NORMAL && !cmd_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 {
2915 int save_State;
2916
2917 save_State = State;
2918 State = NORMAL;
2919 gui_update_cursor(TRUE, FALSE);
2920 State = save_State;
2921 shape_changed = TRUE;
2922 }
2923#endif
2924 validate_cursor();
2925 old_wcol = curwin->w_wcol;
2926 old_wrow = curwin->w_wrow;
2927
Bram Moolenaar30613902019-12-01 22:11:18 +01002928 // move cursor left, if possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 if (curwin->w_cursor.col != 0)
2930 {
2931 if (curwin->w_wcol > 0)
2932 {
2933 if (did_ai)
2934 {
2935 /*
2936 * We are expecting to truncate the trailing
2937 * white-space, so find the last non-white
2938 * character -- webb
2939 */
2940 col = vcol = curwin->w_wcol = 0;
2941 ptr = ml_get_curline();
2942 while (col < curwin->w_cursor.col)
2943 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002944 if (!VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 curwin->w_wcol = vcol;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002946 vcol += lbr_chartabsize(ptr, ptr + col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002949 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 ++col;
2952 }
2953 curwin->w_wrow = curwin->w_cline_row
Bram Moolenaar02631462017-09-22 15:20:32 +02002954 + curwin->w_wcol / curwin->w_width;
2955 curwin->w_wcol %= curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 curwin->w_wcol += curwin_col_off();
Bram Moolenaar30613902019-12-01 22:11:18 +01002957 col = 0; // no correction needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 }
2959 else
2960 {
2961 --curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 col = curwin->w_cursor.col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 }
2964 }
2965 else if (curwin->w_p_wrap && curwin->w_wrow)
2966 {
2967 --curwin->w_wrow;
Bram Moolenaar02631462017-09-22 15:20:32 +02002968 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 col = curwin->w_cursor.col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 if (has_mbyte && col > 0 && curwin->w_wcol > 0)
2972 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002973 // Correct when the cursor is on the right halve
2974 // of a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 ptr = ml_get_curline();
2976 col -= (*mb_head_off)(ptr, ptr + col);
2977 if ((*mb_ptr2cells)(ptr + col) > 1)
2978 --curwin->w_wcol;
2979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 }
2981 setcursor();
2982 out_flush();
2983#ifdef FEAT_CMDL_INFO
2984 new_wcol = curwin->w_wcol;
2985 new_wrow = curwin->w_wrow;
2986#endif
2987 curwin->w_wcol = old_wcol;
2988 curwin->w_wrow = old_wrow;
2989 }
2990 if (c < 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01002991 continue; // end of input script reached
Bram Moolenaar20c38922014-07-23 20:41:14 +02002992
Bram Moolenaar30613902019-12-01 22:11:18 +01002993 // Allow mapping for just typed characters. When we get here c
2994 // is the number of extra bytes and typebuf.tb_len is 1.
Bram Moolenaar20c38922014-07-23 20:41:14 +02002995 for (n = 1; n <= c; ++n)
2996 typebuf.tb_noremap[typebuf.tb_off + n] = RM_YES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997 typebuf.tb_len += c;
2998
Bram Moolenaar30613902019-12-01 22:11:18 +01002999 // buffer full, don't map
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 if (typebuf.tb_len >= typebuf.tb_maplen + MAXMAPLEN)
3001 {
3002 timedout = TRUE;
3003 continue;
3004 }
3005
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 if (ex_normal_busy > 0)
3007 {
Bram Moolenaare2c38102016-01-31 14:55:40 +01003008#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 static int tc = 0;
Bram Moolenaare2c38102016-01-31 14:55:40 +01003010#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011
Bram Moolenaar30613902019-12-01 22:11:18 +01003012 // No typeahead left and inside ":normal". Must return
3013 // something to avoid getting stuck. When an incomplete
3014 // mapping is present, behave like it timed out.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 if (typebuf.tb_len > 0)
3016 {
3017 timedout = TRUE;
3018 continue;
3019 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003020 // When 'insertmode' is set, ESC just beeps in Insert
3021 // mode. Use CTRL-L to make edit() return.
3022 // For the command line only CTRL-C always breaks it.
3023 // For the cmdline window: Alternate between ESC and
3024 // CTRL-C: ESC for most situations and CTRL-C to close the
3025 // cmdline window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 if (p_im && (State & INSERT))
3027 c = Ctrl_L;
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003028#ifdef FEAT_TERMINAL
3029 else if (terminal_is_active())
3030 c = K_CANCEL;
3031#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 else if ((State & CMDLINE)
Bram Moolenaare2c38102016-01-31 14:55:40 +01003033#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 || (cmdwin_type > 0 && tc == ESC)
Bram Moolenaare2c38102016-01-31 14:55:40 +01003035#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 )
3037 c = Ctrl_C;
3038 else
3039 c = ESC;
Bram Moolenaare2c38102016-01-31 14:55:40 +01003040#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 tc = c;
Bram Moolenaare2c38102016-01-31 14:55:40 +01003042#endif
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01003043 // return from main_loop()
3044 if (pending_exmode_active)
3045 exmode_active = EXMODE_NORMAL;
3046
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 break;
3048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049
3050/*
3051 * get a character: 3. from the user - update display
3052 */
Bram Moolenaar30613902019-12-01 22:11:18 +01003053 // In insert mode a screen update is skipped when characters
3054 // are still available. But when those available characters
3055 // are part of a mapping, and we are going to do a blocking
3056 // wait here. Need to update the screen to display the
3057 // changed text so far. Also for when 'lazyredraw' is set and
3058 // redrawing was postponed because there was something in the
3059 // input buffer (e.g., termresponse).
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01003060 if (((State & INSERT) != 0 || p_lz) && (State & CMDLINE) == 0
3061 && advance && must_redraw != 0 && !need_wait_return)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062 {
3063 update_screen(0);
Bram Moolenaar30613902019-12-01 22:11:18 +01003064 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 }
3066
3067 /*
3068 * If we have a partial match (and are going to wait for more
3069 * input from the user), show the partially matched characters
3070 * to the user with showcmd.
3071 */
3072#ifdef FEAT_CMDL_INFO
Bram Moolenaareda35f72019-08-03 14:59:44 +02003073 showcmd_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074#endif
3075 c1 = 0;
3076 if (typebuf.tb_len > 0 && advance && !exmode_active)
3077 {
3078 if (((State & (NORMAL | INSERT)) || State == LANGMAP)
3079 && State != HITRETURN)
3080 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003081 // this looks nice when typing a dead character map
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 if (State & INSERT
3083 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
3084 + typebuf.tb_len - 1) == 1)
3085 {
3086 edit_putchar(typebuf.tb_buf[typebuf.tb_off
3087 + typebuf.tb_len - 1], FALSE);
Bram Moolenaar30613902019-12-01 22:11:18 +01003088 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 c1 = 1;
3090 }
3091#ifdef FEAT_CMDL_INFO
Bram Moolenaar30613902019-12-01 22:11:18 +01003092 // need to use the col and row from above here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 old_wcol = curwin->w_wcol;
3094 old_wrow = curwin->w_wrow;
3095 curwin->w_wcol = new_wcol;
3096 curwin->w_wrow = new_wrow;
3097 push_showcmd();
3098 if (typebuf.tb_len > SHOWCMD_COLS)
Bram Moolenaareda35f72019-08-03 14:59:44 +02003099 showcmd_idx = typebuf.tb_len - SHOWCMD_COLS;
3100 while (showcmd_idx < typebuf.tb_len)
3101 (void)add_to_showcmd(
3102 typebuf.tb_buf[typebuf.tb_off + showcmd_idx++]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 curwin->w_wcol = old_wcol;
3104 curwin->w_wrow = old_wrow;
3105#endif
3106 }
3107
Bram Moolenaar30613902019-12-01 22:11:18 +01003108 // this looks nice when typing a dead character map
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 if ((State & CMDLINE)
3110#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3111 && cmdline_star == 0
3112#endif
3113 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
3114 + typebuf.tb_len - 1) == 1)
3115 {
3116 putcmdline(typebuf.tb_buf[typebuf.tb_off
3117 + typebuf.tb_len - 1], FALSE);
3118 c1 = 1;
3119 }
3120 }
3121
3122/*
3123 * get a character: 3. from the user - get it
3124 */
Bram Moolenaar83f4cbd2018-06-12 21:35:40 +02003125 if (typebuf.tb_len == 0)
3126 // timedout may have been set while waiting for a mapping
3127 // that has a <Nop> RHS.
3128 timedout = FALSE;
3129
Bram Moolenaar652de232019-04-04 20:13:09 +02003130 if (advance)
3131 {
3132 if (typebuf.tb_len == 0
3133 || !(p_timeout
3134 || (p_ttimeout && keylen == KEYLEN_PART_KEY)))
3135 // blocking wait
3136 wait_time = -1L;
3137 else if (keylen == KEYLEN_PART_KEY && p_ttm >= 0)
3138 wait_time = p_ttm;
3139 else
3140 wait_time = p_tm;
3141 }
3142 else
3143 wait_time = 0;
3144
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003145 wait_tb_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 c = inchar(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len,
3147 typebuf.tb_buflen - typebuf.tb_off - typebuf.tb_len - 1,
Bram Moolenaar652de232019-04-04 20:13:09 +02003148 wait_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149
3150#ifdef FEAT_CMDL_INFO
Bram Moolenaareda35f72019-08-03 14:59:44 +02003151 if (showcmd_idx != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 pop_showcmd();
3153#endif
3154 if (c1 == 1)
3155 {
3156 if (State & INSERT)
3157 edit_unputchar();
3158 if (State & CMDLINE)
3159 unputcmdline();
Bram Moolenaarbc256d92012-06-06 12:06:15 +02003160 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003161 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 }
3163
3164 if (c < 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003165 continue; // end of input script reached
3166 if (c == NUL) // no character available
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 {
3168 if (!advance)
3169 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01003170 if (wait_tb_len > 0) // timed out
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 {
3172 timedout = TRUE;
3173 continue;
3174 }
3175 }
3176 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003177 { // allow mapping for just typed characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 while (typebuf.tb_buf[typebuf.tb_off
3179 + typebuf.tb_len] != NUL)
3180 typebuf.tb_noremap[typebuf.tb_off
3181 + typebuf.tb_len++] = RM_YES;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003182#ifdef HAVE_INPUT_METHOD
Bram Moolenaar30613902019-12-01 22:11:18 +01003183 // Get IM status right after getting keys, not after the
3184 // timeout for a mapping (focus may be lost by then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 vgetc_im_active = im_get_status();
3186#endif
3187 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003188 } // for (;;)
3189 } // if (!character from stuffbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190
Bram Moolenaar30613902019-12-01 22:11:18 +01003191 // if advance is FALSE don't loop on NULs
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003192 } while ((c < 0 && c != K_CANCEL) || (advance && c == NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193
3194 /*
3195 * The "INSERT" message is taken care of here:
3196 * if we return an ESC to exit insert mode, the message is deleted
3197 * if we don't return an ESC but deleted the message before, redisplay it
3198 */
Bram Moolenaar09df3122006-01-23 22:23:09 +00003199 if (advance && p_smd && msg_silent == 0 && (State & INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003201 if (c == ESC && !mode_deleted && !no_mapping && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 {
3203 if (typebuf.tb_len && !KeyTyped)
Bram Moolenaar30613902019-12-01 22:11:18 +01003204 redraw_cmdline = TRUE; // delete mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 else
3206 unshowmode(FALSE);
3207 }
3208 else if (c != ESC && mode_deleted)
3209 {
3210 if (typebuf.tb_len && !KeyTyped)
Bram Moolenaar30613902019-12-01 22:11:18 +01003211 redraw_cmdline = TRUE; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 else
3213 showmode();
3214 }
3215 }
3216#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003217 // may unshow different cursor shape
Bram Moolenaarf085f422017-06-07 20:39:47 +02003218 if (gui.in_use && shape_changed)
3219 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220#endif
Bram Moolenaar6edbbd82019-03-10 09:41:51 +01003221 if (timedout && c == ESC)
3222 {
3223 char_u nop_buf[3];
3224
3225 // When recording there will be no timeout. Add a <Nop> after the ESC
3226 // to avoid that it forms a key code with following characters.
3227 nop_buf[0] = K_SPECIAL;
3228 nop_buf[1] = KS_EXTRA;
3229 nop_buf[2] = KE_NOP;
3230 gotchars(nop_buf, 3);
3231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003233 --vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234
3235 return c;
3236}
3237
3238/*
3239 * inchar() - get one character from
3240 * 1. a scriptfile
3241 * 2. the keyboard
3242 *
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003243 * As many characters as we can get (up to 'maxlen') are put in "buf" and
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 * NUL terminated (buffer length must be 'maxlen' + 1).
3245 * Minimum for "maxlen" is 3!!!!
3246 *
3247 * "tb_change_cnt" is the value of typebuf.tb_change_cnt if "buf" points into
3248 * it. When typebuf.tb_change_cnt changes (e.g., when a message is received
3249 * from a remote client) "buf" can no longer be used. "tb_change_cnt" is 0
3250 * otherwise.
3251 *
3252 * If we got an interrupt all input is read until none is available.
3253 *
3254 * If wait_time == 0 there is no waiting for the char.
3255 * If wait_time == n we wait for n msec for a character to arrive.
3256 * If wait_time == -1 we wait forever for a character to arrive.
3257 *
3258 * Return the number of obtained characters.
3259 * Return -1 when end of input script reached.
3260 */
Bram Moolenaarcda77642016-06-04 13:32:35 +02003261 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003262inchar(
3263 char_u *buf,
3264 int maxlen,
Bram Moolenaar30613902019-12-01 22:11:18 +01003265 long wait_time) // milli seconds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266{
Bram Moolenaar30613902019-12-01 22:11:18 +01003267 int len = 0; // init for GCC
3268 int retesc = FALSE; // return ESC with gotint
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 int script_char;
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003270 int tb_change_cnt = typebuf.tb_change_cnt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271
Bram Moolenaar30613902019-12-01 22:11:18 +01003272 if (wait_time == -1L || wait_time > 100L) // flush output before waiting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 {
3274 cursor_on();
Bram Moolenaara338adc2018-01-31 20:51:47 +01003275 out_flush_cursor(FALSE, FALSE);
3276#if defined(FEAT_GUI) && defined(FEAT_MOUSESHAPE)
3277 if (gui.in_use && postponed_mouseshape)
3278 update_mouseshape(-1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279#endif
3280 }
3281
3282 /*
3283 * Don't reset these when at the hit-return prompt, otherwise a endless
3284 * recursive loop may result (write error in swapfile, hit-return, timeout
3285 * on char wait, flush swapfile, write error....).
3286 */
3287 if (State != HITRETURN)
3288 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003289 did_outofmem_msg = FALSE; // display out of memory message (again)
3290 did_swapwrite_msg = FALSE; // display swap file write error again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003292 undo_off = FALSE; // restart undo now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293
3294 /*
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00003295 * Get a character from a script file if there is one.
3296 * If interrupted: Stop reading script files, close them all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 */
3298 script_char = -1;
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00003299 while (scriptin[curscript] != NULL && script_char < 0
3300#ifdef FEAT_EVAL
3301 && !ignore_script
3302#endif
3303 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02003305#ifdef MESSAGE_QUEUE
3306 parse_queued_messages();
Bram Moolenaarf2330482008-06-24 20:19:36 +00003307#endif
3308
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 if (got_int || (script_char = getc(scriptin[curscript])) < 0)
3310 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003311 // Reached EOF.
3312 // Careful: closescript() frees typebuf.tb_buf[] and buf[] may
3313 // point inside typebuf.tb_buf[]. Don't use buf[] after this!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 closescript();
3315 /*
3316 * When reading script file is interrupted, return an ESC to get
3317 * back to normal mode.
3318 * Otherwise return -1, because typebuf.tb_buf[] has changed.
3319 */
3320 if (got_int)
3321 retesc = TRUE;
3322 else
3323 return -1;
3324 }
3325 else
3326 {
3327 buf[0] = script_char;
3328 len = 1;
3329 }
3330 }
3331
Bram Moolenaar30613902019-12-01 22:11:18 +01003332 if (script_char < 0) // did not get a character from script
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 {
3334 /*
3335 * If we got an interrupt, skip all previously typed characters and
3336 * return TRUE if quit reading script file.
3337 * Stop reading typeahead when a single CTRL-C was read,
3338 * fill_input_buf() returns this when not able to read from stdin.
3339 * Don't use buf[] here, closescript() may have freed typebuf.tb_buf[]
3340 * and buf may be pointing inside typebuf.tb_buf[].
3341 */
3342 if (got_int)
3343 {
3344#define DUM_LEN MAXMAPLEN * 3 + 3
3345 char_u dum[DUM_LEN + 1];
3346
3347 for (;;)
3348 {
3349 len = ui_inchar(dum, DUM_LEN, 0L, 0);
3350 if (len == 0 || (len == 1 && dum[0] == 3))
3351 break;
3352 }
3353 return retesc;
3354 }
3355
3356 /*
3357 * Always flush the output characters when getting input characters
Bram Moolenaarcb574f42019-01-25 22:29:57 +01003358 * from the user and not just peeking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 */
Bram Moolenaarcb574f42019-01-25 22:29:57 +01003360 if (wait_time == -1L || wait_time > 10L)
3361 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362
3363 /*
3364 * Fill up to a third of the buffer, because each character may be
3365 * tripled below.
3366 */
3367 len = ui_inchar(buf, maxlen / 3, wait_time, tb_change_cnt);
3368 }
3369
Bram Moolenaar30613902019-12-01 22:11:18 +01003370 // If the typebuf was changed further down, it is like nothing was added by
3371 // this call.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 if (typebuf_changed(tb_change_cnt))
3373 return 0;
3374
Bram Moolenaar30613902019-12-01 22:11:18 +01003375 // Note the change in the typeahead buffer, this matters for when
3376 // vgetorpeek() is called recursively, e.g. using getchar(1) in a timer
3377 // function.
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003378 if (len > 0 && ++typebuf.tb_change_cnt == 0)
3379 typebuf.tb_change_cnt = 1;
3380
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003381 return fix_input_buffer(buf, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382}
3383
3384/*
3385 * Fix typed characters for use by vgetc() and check_termcode().
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003386 * "buf[]" must have room to triple the number of bytes!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 * Returns the new length.
3388 */
3389 int
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003390fix_input_buffer(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391{
3392 int i;
3393 char_u *p = buf;
3394
3395 /*
3396 * Two characters are special: NUL and K_SPECIAL.
3397 * When compiled With the GUI CSI is also special.
3398 * Replace NUL by K_SPECIAL KS_ZERO KE_FILLER
3399 * Replace K_SPECIAL by K_SPECIAL KS_SPECIAL KE_FILLER
3400 * Replace CSI by K_SPECIAL KS_EXTRA KE_CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 */
3402 for (i = len; --i >= 0; ++p)
3403 {
3404#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003405 // When the GUI is used any character can come after a CSI, don't
3406 // escape it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 if (gui.in_use && p[0] == CSI && i >= 2)
3408 {
3409 p += 2;
3410 i -= 2;
3411 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003412# ifndef MSWIN
Bram Moolenaar30613902019-12-01 22:11:18 +01003413 // When the GUI is not used CSI needs to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 else if (!gui.in_use && p[0] == CSI)
3415 {
3416 mch_memmove(p + 3, p + 1, (size_t)i);
3417 *p++ = K_SPECIAL;
3418 *p++ = KS_EXTRA;
3419 *p = (int)KE_CSI;
3420 len += 2;
3421 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003422# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 else
3424#endif
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003425 if (p[0] == NUL || (p[0] == K_SPECIAL
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003426 // timeout may generate K_CURSORHOLD
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003427 && (i < 2 || p[1] != KS_EXTRA || p[2] != (int)KE_CURSORHOLD)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003428#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003429 // Win32 console passes modifiers
3430 && (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003431# ifdef VIMDLL
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003432 gui.in_use ||
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003433# endif
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003434 (i < 2 || p[1] != KS_MODIFIER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435#endif
3436 ))
3437 {
3438 mch_memmove(p + 3, p + 1, (size_t)i);
3439 p[2] = K_THIRD(p[0]);
3440 p[1] = K_SECOND(p[0]);
3441 p[0] = K_SPECIAL;
3442 p += 2;
3443 len += 2;
3444 }
3445 }
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003446 *p = NUL; // add trailing NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 return len;
3448}
3449
3450#if defined(USE_INPUT_BUF) || defined(PROTO)
3451/*
3452 * Return TRUE when bytes are in the input buffer or in the typeahead buffer.
3453 * Normally the input buffer would be sufficient, but the server_to_input_buf()
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003454 * or feedkeys() may insert characters in the typeahead buffer while we are
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003455 * waiting for input to arrive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 */
3457 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003458input_available(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459{
3460 return (!vim_is_input_buf_empty()
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003461# if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
3462 || typebuf_was_filled
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463# endif
3464 );
3465}
3466#endif