blob: b828c27dd4b687841fba14e40e8be7de4f0dab11 [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 */
102 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;
424 }
425 else
426 {
427 // remove typeahead
428 if (flush_typeahead == FLUSH_INPUT)
429 // We have to get all characters, because we may delete the first
430 // part of an escape sequence. In an xterm we get one char at a
431 // time and we have to get them all.
432 while (inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 10L) != 0)
433 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434 typebuf.tb_off = MAXMAPLEN;
435 typebuf.tb_len = 0;
Bram Moolenaar4eb65312017-06-24 18:49:00 +0200436#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar30613902019-12-01 22:11:18 +0100437 // Reset the flag that text received from a client or from feedkeys()
438 // was inserted in the typeahead buffer.
Bram Moolenaar4eb65312017-06-24 18:49:00 +0200439 typebuf_was_filled = FALSE;
440#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442 typebuf.tb_maplen = 0;
443 typebuf.tb_silent = 0;
444 cmd_silent = FALSE;
445 typebuf.tb_no_abbr_cnt = 0;
446}
447
448/*
449 * The previous contents of the redo buffer is kept in old_redobuffer.
450 * This is used for the CTRL-O <.> command in insert mode.
451 */
452 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100453ResetRedobuff(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454{
455 if (!block_redo)
456 {
457 free_buff(&old_redobuff);
458 old_redobuff = redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200459 redobuff.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460 }
461}
462
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100463/*
464 * Discard the contents of the redo buffer and restore the previous redo
465 * buffer.
466 */
467 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100468CancelRedo(void)
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100469{
470 if (!block_redo)
471 {
472 free_buff(&redobuff);
473 redobuff = old_redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200474 old_redobuff.bh_first.b_next = NULL;
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100475 start_stuff();
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100476 while (read_readbuffers(TRUE) != NUL)
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100477 ;
478 }
479}
480
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481/*
482 * Save redobuff and old_redobuff to save_redobuff and save_old_redobuff.
483 * Used before executing autocommands and user functions.
484 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485 void
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200486saveRedobuff(save_redo_T *save_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487{
488 char_u *s;
489
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200490 save_redo->sr_redobuff = redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200491 redobuff.bh_first.b_next = NULL;
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200492 save_redo->sr_old_redobuff = old_redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200493 old_redobuff.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494
Bram Moolenaar30613902019-12-01 22:11:18 +0100495 // Make a copy, so that ":normal ." in a function works.
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200496 s = get_buffcont(&save_redo->sr_redobuff, FALSE);
497 if (s != NULL)
498 {
499 add_buff(&redobuff, s, -1L);
500 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501 }
502}
503
504/*
505 * Restore redobuff and old_redobuff from save_redobuff and save_old_redobuff.
506 * Used after executing autocommands and user functions.
507 */
508 void
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200509restoreRedobuff(save_redo_T *save_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510{
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200511 free_buff(&redobuff);
512 redobuff = save_redo->sr_redobuff;
513 free_buff(&old_redobuff);
514 old_redobuff = save_redo->sr_old_redobuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516
517/*
518 * Append "s" to the redo buffer.
519 * K_SPECIAL and CSI should already have been escaped.
520 */
521 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100522AppendToRedobuff(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523{
524 if (!block_redo)
525 add_buff(&redobuff, s, -1L);
526}
527
528/*
529 * Append to Redo buffer literally, escaping special characters with CTRL-V.
530 * K_SPECIAL and CSI are escaped as well.
531 */
532 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100533AppendToRedobuffLit(
534 char_u *str,
Bram Moolenaar30613902019-12-01 22:11:18 +0100535 int len) // length of "str" or -1 for up to the NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536{
Bram Moolenaarebefac62005-12-28 22:39:57 +0000537 char_u *s = str;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538 int c;
539 char_u *start;
540
541 if (block_redo)
542 return;
543
Bram Moolenaarebefac62005-12-28 22:39:57 +0000544 while (len < 0 ? *s != NUL : s - str < len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100546 // Put a string of normal characters in the redo buffer (that's
547 // faster).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 start = s;
549 while (*s >= ' '
550#ifndef EBCDIC
Bram Moolenaar30613902019-12-01 22:11:18 +0100551 && *s < DEL // EBCDIC: all chars above space are normal
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552#endif
Bram Moolenaarebefac62005-12-28 22:39:57 +0000553 && (len < 0 || s - str < len))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 ++s;
555
Bram Moolenaar30613902019-12-01 22:11:18 +0100556 // Don't put '0' or '^' as last character, just in case a CTRL-D is
557 // typed next.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558 if (*s == NUL && (s[-1] == '0' || s[-1] == '^'))
559 --s;
560 if (s > start)
561 add_buff(&redobuff, start, (long)(s - start));
562
Bram Moolenaarebefac62005-12-28 22:39:57 +0000563 if (*s == NUL || (len >= 0 && s - str >= len))
564 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565
Bram Moolenaar30613902019-12-01 22:11:18 +0100566 // Handle a special or multibyte character.
Bram Moolenaarebefac62005-12-28 22:39:57 +0000567 if (has_mbyte)
Bram Moolenaar30613902019-12-01 22:11:18 +0100568 // Handle composing chars separately.
Bram Moolenaarebefac62005-12-28 22:39:57 +0000569 c = mb_cptr2char_adv(&s);
570 else
Bram Moolenaarebefac62005-12-28 22:39:57 +0000571 c = *s++;
572 if (c < ' ' || c == DEL || (*s == NUL && (c == '0' || c == '^')))
573 add_char_buff(&redobuff, Ctrl_V);
574
Bram Moolenaar30613902019-12-01 22:11:18 +0100575 // CTRL-V '0' must be inserted as CTRL-V 048 (EBCDIC: xf0)
Bram Moolenaarebefac62005-12-28 22:39:57 +0000576 if (*s == NUL && c == '0')
577#ifdef EBCDIC
578 add_buff(&redobuff, (char_u *)"xf0", 3L);
579#else
580 add_buff(&redobuff, (char_u *)"048", 3L);
581#endif
582 else
583 add_char_buff(&redobuff, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 }
585}
586
587/*
588 * Append a character to the redo buffer.
589 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
590 */
591 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100592AppendCharToRedobuff(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593{
594 if (!block_redo)
595 add_char_buff(&redobuff, c);
596}
597
598/*
599 * Append a number to the redo buffer.
600 */
601 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100602AppendNumberToRedobuff(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603{
604 if (!block_redo)
605 add_num_buff(&redobuff, n);
606}
607
608/*
609 * Append string "s" to the stuff buffer.
610 * CSI and K_SPECIAL must already have been escaped.
611 */
612 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100613stuffReadbuff(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100615 add_buff(&readbuf1, s, -1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616}
617
Bram Moolenaar4f5ce332014-07-30 16:00:58 +0200618/*
619 * Append string "s" to the redo stuff buffer.
620 * CSI and K_SPECIAL must already have been escaped.
621 */
622 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100623stuffRedoReadbuff(char_u *s)
Bram Moolenaar4f5ce332014-07-30 16:00:58 +0200624{
625 add_buff(&readbuf2, s, -1L);
626}
627
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100629stuffReadbuffLen(char_u *s, long len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100631 add_buff(&readbuf1, s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632}
633
634#if defined(FEAT_EVAL) || defined(PROTO)
635/*
636 * Stuff "s" into the stuff buffer, leaving special key codes unmodified and
637 * escaping other K_SPECIAL and CSI bytes.
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200638 * Change CR, LF and ESC into a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 */
640 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100641stuffReadbuffSpec(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642{
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200643 int c;
644
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 while (*s != NUL)
646 {
647 if (*s == K_SPECIAL && s[1] != NUL && s[2] != NUL)
648 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100649 // Insert special key literally.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 stuffReadbuffLen(s, 3L);
651 s += 3;
652 }
653 else
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200654 {
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200655 c = mb_ptr2char_adv(&s);
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200656 if (c == CAR || c == NL || c == ESC)
657 c = ' ';
658 stuffcharReadbuff(c);
659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 }
661}
662#endif
663
664/*
665 * Append a character to the stuff buffer.
666 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
667 */
668 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100669stuffcharReadbuff(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100671 add_char_buff(&readbuf1, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672}
673
674/*
675 * Append a number to the stuff buffer.
676 */
677 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100678stuffnumReadbuff(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100680 add_num_buff(&readbuf1, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681}
682
683/*
684 * Read a character from the redo buffer. Translates K_SPECIAL, CSI and
685 * multibyte characters.
686 * The redo buffer is left as it is.
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100687 * If init is TRUE, prepare for redo, return FAIL if nothing to redo, OK
688 * otherwise.
689 * If old is TRUE, use old_redobuff instead of redobuff.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 */
691 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100692read_redo(int init, int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100694 static buffblock_T *bp;
695 static char_u *p;
696 int c;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100697 int n;
698 char_u buf[MB_MAXBYTES + 1];
699 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700
701 if (init)
702 {
703 if (old_redo)
Bram Moolenaar285e3352018-04-18 23:01:13 +0200704 bp = old_redobuff.bh_first.b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 else
Bram Moolenaar285e3352018-04-18 23:01:13 +0200706 bp = redobuff.bh_first.b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 if (bp == NULL)
708 return FAIL;
709 p = bp->b_str;
710 return OK;
711 }
712 if ((c = *p) != NUL)
713 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100714 // Reverse the conversion done by add_char_buff()
715 // For a multi-byte character get all the bytes and return the
716 // converted character.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 if (has_mbyte && (c != K_SPECIAL || p[1] == KS_SPECIAL))
718 n = MB_BYTE2LEN_CHECK(c);
719 else
720 n = 1;
721 for (i = 0; ; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100723 if (c == K_SPECIAL) // special key or escaped K_SPECIAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 {
725 c = TO_SPECIAL(p[1], p[2]);
726 p += 2;
727 }
728#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +0100729 if (c == CSI) // escaped CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 p += 2;
731#endif
732 if (*++p == NUL && bp->b_next != NULL)
733 {
734 bp = bp->b_next;
735 p = bp->b_str;
736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 buf[i] = c;
Bram Moolenaar30613902019-12-01 22:11:18 +0100738 if (i == n - 1) // last byte of a character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 {
740 if (n != 1)
741 c = (*mb_ptr2char)(buf);
742 break;
743 }
744 c = *p;
Bram Moolenaar30613902019-12-01 22:11:18 +0100745 if (c == NUL) // cannot happen?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 }
748 }
749
750 return c;
751}
752
753/*
754 * Copy the rest of the redo buffer into the stuff buffer (in a slow way).
755 * If old_redo is TRUE, use old_redobuff instead of redobuff.
756 * The escaped K_SPECIAL and CSI are copied without translation.
757 */
758 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100759copy_redo(int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760{
761 int c;
762
763 while ((c = read_redo(FALSE, old_redo)) != NUL)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100764 add_char_buff(&readbuf2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765}
766
767/*
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100768 * Stuff the redo buffer into readbuf2.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 * Insert the redo count into the command.
770 * If "old_redo" is TRUE, the last but one command is repeated
771 * instead of the last command (inserting text). This is used for
772 * CTRL-O <.> in insert mode
773 *
774 * return FAIL for failure, OK otherwise
775 */
776 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100777start_redo(long count, int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778{
779 int c;
780
Bram Moolenaar30613902019-12-01 22:11:18 +0100781 // init the pointers; return if nothing to redo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 if (read_redo(TRUE, old_redo) == FAIL)
783 return FAIL;
784
785 c = read_redo(FALSE, old_redo);
786
Bram Moolenaar30613902019-12-01 22:11:18 +0100787 // copy the buffer name, if present
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 if (c == '"')
789 {
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100790 add_buff(&readbuf2, (char_u *)"\"", 1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 c = read_redo(FALSE, old_redo);
792
Bram Moolenaar30613902019-12-01 22:11:18 +0100793 // if a numbered buffer is used, increment the number
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 if (c >= '1' && c < '9')
795 ++c;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100796 add_char_buff(&readbuf2, c);
Bram Moolenaar833093b2018-05-23 21:53:52 +0200797
Bram Moolenaar30613902019-12-01 22:11:18 +0100798 // the expression register should be re-evaluated
Bram Moolenaar833093b2018-05-23 21:53:52 +0200799 if (c == '=')
800 {
801 add_char_buff(&readbuf2, CAR);
802 cmd_silent = TRUE;
803 }
804
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 c = read_redo(FALSE, old_redo);
806 }
807
Bram Moolenaar30613902019-12-01 22:11:18 +0100808 if (c == 'v') // redo Visual
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 {
810 VIsual = curwin->w_cursor;
811 VIsual_active = TRUE;
812 VIsual_select = FALSE;
813 VIsual_reselect = TRUE;
814 redo_VIsual_busy = TRUE;
815 c = read_redo(FALSE, old_redo);
816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817
Bram Moolenaar30613902019-12-01 22:11:18 +0100818 // try to enter the count (in place of a previous count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 if (count)
820 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100821 while (VIM_ISDIGIT(c)) // skip "old" count
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 c = read_redo(FALSE, old_redo);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100823 add_num_buff(&readbuf2, count);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 }
825
Bram Moolenaar30613902019-12-01 22:11:18 +0100826 // copy from the redo buffer into the stuff buffer
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100827 add_char_buff(&readbuf2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 copy_redo(old_redo);
829 return OK;
830}
831
832/*
833 * Repeat the last insert (R, o, O, a, A, i or I command) by stuffing
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100834 * the redo buffer into readbuf2.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 * return FAIL for failure, OK otherwise
836 */
837 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100838start_redo_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839{
840 int c;
841
842 if (read_redo(TRUE, FALSE) == FAIL)
843 return FAIL;
844 start_stuff();
845
Bram Moolenaar30613902019-12-01 22:11:18 +0100846 // skip the count and the command character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 while ((c = read_redo(FALSE, FALSE)) != NUL)
848 {
849 if (vim_strchr((char_u *)"AaIiRrOo", c) != NULL)
850 {
851 if (c == 'O' || c == 'o')
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100852 add_buff(&readbuf2, NL_STR, -1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 break;
854 }
855 }
856
Bram Moolenaar30613902019-12-01 22:11:18 +0100857 // copy the typed text from the redo buffer into the stuff buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 copy_redo(FALSE);
859 block_redo = TRUE;
860 return OK;
861}
862
863 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100864stop_redo_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865{
866 block_redo = FALSE;
867}
868
869/*
870 * Initialize typebuf.tb_buf to point to typebuf_init.
871 * alloc() cannot be used here: In out-of-memory situations it would
872 * be impossible to type anything.
873 */
874 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100875init_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876{
877 if (typebuf.tb_buf == NULL)
878 {
879 typebuf.tb_buf = typebuf_init;
880 typebuf.tb_noremap = noremapbuf_init;
881 typebuf.tb_buflen = TYPELEN_INIT;
882 typebuf.tb_len = 0;
Bram Moolenaard34f9b12017-04-08 18:41:13 +0200883 typebuf.tb_off = MAXMAPLEN + 4;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 typebuf.tb_change_cnt = 1;
885 }
886}
887
888/*
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200889 * Returns TRUE when keys cannot be remapped.
890 */
891 int
892noremap_keys(void)
893{
894 return KeyNoremap & (RM_NONE|RM_SCRIPT);
895}
896
897/*
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100898 * Insert a string in position 'offset' in the typeahead buffer (for "@r"
899 * and ":normal" command, vgetorpeek() and check_termcode()).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 *
901 * If noremap is REMAP_YES, new string can be mapped again.
902 * If noremap is REMAP_NONE, new string cannot be mapped again.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000903 * If noremap is REMAP_SKIP, fist char of new string cannot be mapped again,
904 * but abbreviations are allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 * If noremap is REMAP_SCRIPT, new string cannot be mapped again, except for
906 * script-local mappings.
907 * If noremap is > 0, that many characters of the new string cannot be mapped.
908 *
909 * If nottyped is TRUE, the string does not return KeyTyped (don't use when
910 * offset is non-zero!).
911 *
912 * If silent is TRUE, cmd_silent is set when the characters are obtained.
913 *
914 * return FAIL for failure, OK otherwise
915 */
916 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100917ins_typebuf(
918 char_u *str,
919 int noremap,
920 int offset,
921 int nottyped,
922 int silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923{
924 char_u *s1, *s2;
925 int newlen;
926 int addlen;
927 int i;
928 int newoff;
929 int val;
930 int nrm;
931
932 init_typebuf();
933 if (++typebuf.tb_change_cnt == 0)
934 typebuf.tb_change_cnt = 1;
Bram Moolenaard103ee72019-09-18 21:15:31 +0200935 state_no_longer_safe("ins_typebuf()");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936
937 addlen = (int)STRLEN(str);
Bram Moolenaar4e86cba2007-05-06 11:56:32 +0000938
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 if (offset == 0 && addlen <= typebuf.tb_off)
940 {
Bram Moolenaarcaa55b62017-01-10 13:51:09 +0100941 /*
942 * Easy case: there is room in front of typebuf.tb_buf[typebuf.tb_off]
943 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 typebuf.tb_off -= addlen;
945 mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen);
946 }
Bram Moolenaard34f9b12017-04-08 18:41:13 +0200947 else if (typebuf.tb_len == 0 && typebuf.tb_buflen
948 >= addlen + 3 * (MAXMAPLEN + 4))
949 {
950 /*
951 * Buffer is empty and string fits in the existing buffer.
952 * Leave some space before and after, if possible.
953 */
954 typebuf.tb_off = (typebuf.tb_buflen - addlen - 3 * (MAXMAPLEN + 4)) / 2;
955 mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen);
956 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 else
958 {
Bram Moolenaarcaa55b62017-01-10 13:51:09 +0100959 /*
960 * Need to allocate a new buffer.
Bram Moolenaard34f9b12017-04-08 18:41:13 +0200961 * In typebuf.tb_buf there must always be room for 3 * (MAXMAPLEN + 4)
Bram Moolenaarcaa55b62017-01-10 13:51:09 +0100962 * characters. We add some extra room to avoid having to allocate too
963 * often.
964 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 newoff = MAXMAPLEN + 4;
966 newlen = typebuf.tb_len + addlen + newoff + 4 * (MAXMAPLEN + 4);
Bram Moolenaar30613902019-12-01 22:11:18 +0100967 if (newlen < 0) // string is getting too long
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100969 emsg(_(e_toocompl)); // also calls flush_buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 setcursor();
971 return FAIL;
972 }
973 s1 = alloc(newlen);
Bram Moolenaar30613902019-12-01 22:11:18 +0100974 if (s1 == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 return FAIL;
976 s2 = alloc(newlen);
Bram Moolenaar30613902019-12-01 22:11:18 +0100977 if (s2 == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 {
979 vim_free(s1);
980 return FAIL;
981 }
982 typebuf.tb_buflen = newlen;
983
Bram Moolenaar30613902019-12-01 22:11:18 +0100984 // copy the old chars, before the insertion point
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 mch_memmove(s1 + newoff, typebuf.tb_buf + typebuf.tb_off,
986 (size_t)offset);
Bram Moolenaar30613902019-12-01 22:11:18 +0100987 // copy the new chars
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 mch_memmove(s1 + newoff + offset, str, (size_t)addlen);
Bram Moolenaar30613902019-12-01 22:11:18 +0100989 // copy the old chars, after the insertion point, including the NUL at
990 // the end
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 mch_memmove(s1 + newoff + offset + addlen,
992 typebuf.tb_buf + typebuf.tb_off + offset,
993 (size_t)(typebuf.tb_len - offset + 1));
994 if (typebuf.tb_buf != typebuf_init)
995 vim_free(typebuf.tb_buf);
996 typebuf.tb_buf = s1;
997
998 mch_memmove(s2 + newoff, typebuf.tb_noremap + typebuf.tb_off,
999 (size_t)offset);
1000 mch_memmove(s2 + newoff + offset + addlen,
1001 typebuf.tb_noremap + typebuf.tb_off + offset,
1002 (size_t)(typebuf.tb_len - offset));
1003 if (typebuf.tb_noremap != noremapbuf_init)
1004 vim_free(typebuf.tb_noremap);
1005 typebuf.tb_noremap = s2;
1006
1007 typebuf.tb_off = newoff;
1008 }
1009 typebuf.tb_len += addlen;
1010
Bram Moolenaar30613902019-12-01 22:11:18 +01001011 // If noremap == REMAP_SCRIPT: do remap script-local mappings.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 if (noremap == REMAP_SCRIPT)
1013 val = RM_SCRIPT;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001014 else if (noremap == REMAP_SKIP)
1015 val = RM_ABBR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 else
1017 val = RM_NONE;
1018
1019 /*
1020 * Adjust typebuf.tb_noremap[] for the new characters:
1021 * If noremap == REMAP_NONE or REMAP_SCRIPT: new characters are
1022 * (sometimes) not remappable
1023 * If noremap == REMAP_YES: all the new characters are mappable
1024 * If noremap > 0: "noremap" characters are not remappable, the rest
1025 * mappable
1026 */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001027 if (noremap == REMAP_SKIP)
1028 nrm = 1;
1029 else if (noremap < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 nrm = addlen;
1031 else
1032 nrm = noremap;
1033 for (i = 0; i < addlen; ++i)
1034 typebuf.tb_noremap[typebuf.tb_off + i + offset] =
1035 (--nrm >= 0) ? val : RM_YES;
1036
Bram Moolenaar30613902019-12-01 22:11:18 +01001037 // tb_maplen and tb_silent only remember the length of mapped and/or
1038 // silent mappings at the start of the buffer, assuming that a mapped
1039 // sequence doesn't result in typed characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 if (nottyped || typebuf.tb_maplen > offset)
1041 typebuf.tb_maplen += addlen;
1042 if (silent || typebuf.tb_silent > offset)
1043 {
1044 typebuf.tb_silent += addlen;
1045 cmd_silent = TRUE;
1046 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001047 if (typebuf.tb_no_abbr_cnt && offset == 0) // and not used for abbrev.s
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 typebuf.tb_no_abbr_cnt += addlen;
1049
1050 return OK;
1051}
1052
1053/*
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001054 * Put character "c" back into the typeahead buffer.
1055 * Can be used for a character obtained by vgetc() that needs to be put back.
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00001056 * Uses cmd_silent, KeyTyped and KeyNoremap to restore the flags belonging to
1057 * the char.
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001058 */
1059 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001060ins_char_typebuf(int c)
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001061{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001062 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001063 if (IS_SPECIAL(c))
1064 {
1065 buf[0] = K_SPECIAL;
1066 buf[1] = K_SECOND(c);
1067 buf[2] = K_THIRD(c);
1068 buf[3] = NUL;
1069 }
1070 else
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001071 buf[(*mb_char2bytes)(c, buf)] = NUL;
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00001072 (void)ins_typebuf(buf, KeyNoremap, 0, !KeyTyped, cmd_silent);
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001073}
1074
1075/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 * Return TRUE if the typeahead buffer was changed (while waiting for a
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001077 * character to arrive). Happens when a message was received from a client or
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001078 * from feedkeys().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 * But check in a more generic way to avoid trouble: When "typebuf.tb_buf"
1080 * changed it was reallocated and the old pointer can no longer be used.
1081 * Or "typebuf.tb_off" may have been changed and we would overwrite characters
1082 * that was just added.
1083 */
1084 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001085typebuf_changed(
Bram Moolenaar30613902019-12-01 22:11:18 +01001086 int tb_change_cnt) // old value of typebuf.tb_change_cnt
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087{
1088 return (tb_change_cnt != 0 && (typebuf.tb_change_cnt != tb_change_cnt
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001089#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
1090 || typebuf_was_filled
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091#endif
1092 ));
1093}
1094
1095/*
1096 * Return TRUE if there are no characters in the typeahead buffer that have
1097 * not been typed (result from a mapping or come from ":normal").
1098 */
1099 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001100typebuf_typed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101{
1102 return typebuf.tb_maplen == 0;
1103}
1104
1105/*
1106 * Return the number of characters that are mapped (or not typed).
1107 */
1108 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001109typebuf_maplen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110{
1111 return typebuf.tb_maplen;
1112}
1113
1114/*
1115 * remove "len" characters from typebuf.tb_buf[typebuf.tb_off + offset]
1116 */
1117 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001118del_typebuf(int len, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119{
1120 int i;
1121
1122 if (len == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01001123 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124
1125 typebuf.tb_len -= len;
1126
1127 /*
1128 * Easy case: Just increase typebuf.tb_off.
1129 */
1130 if (offset == 0 && typebuf.tb_buflen - (typebuf.tb_off + len)
1131 >= 3 * MAXMAPLEN + 3)
1132 typebuf.tb_off += len;
1133 /*
1134 * Have to move the characters in typebuf.tb_buf[] and typebuf.tb_noremap[]
1135 */
1136 else
1137 {
1138 i = typebuf.tb_off + offset;
1139 /*
1140 * Leave some extra room at the end to avoid reallocation.
1141 */
1142 if (typebuf.tb_off > MAXMAPLEN)
1143 {
1144 mch_memmove(typebuf.tb_buf + MAXMAPLEN,
1145 typebuf.tb_buf + typebuf.tb_off, (size_t)offset);
1146 mch_memmove(typebuf.tb_noremap + MAXMAPLEN,
1147 typebuf.tb_noremap + typebuf.tb_off, (size_t)offset);
1148 typebuf.tb_off = MAXMAPLEN;
1149 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001150 // adjust typebuf.tb_buf (include the NUL at the end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset,
1152 typebuf.tb_buf + i + len,
1153 (size_t)(typebuf.tb_len - offset + 1));
Bram Moolenaar30613902019-12-01 22:11:18 +01001154 // adjust typebuf.tb_noremap[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 mch_memmove(typebuf.tb_noremap + typebuf.tb_off + offset,
1156 typebuf.tb_noremap + i + len,
1157 (size_t)(typebuf.tb_len - offset));
1158 }
1159
Bram Moolenaar30613902019-12-01 22:11:18 +01001160 if (typebuf.tb_maplen > offset) // adjust tb_maplen
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 {
1162 if (typebuf.tb_maplen < offset + len)
1163 typebuf.tb_maplen = offset;
1164 else
1165 typebuf.tb_maplen -= len;
1166 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001167 if (typebuf.tb_silent > offset) // adjust tb_silent
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 {
1169 if (typebuf.tb_silent < offset + len)
1170 typebuf.tb_silent = offset;
1171 else
1172 typebuf.tb_silent -= len;
1173 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001174 if (typebuf.tb_no_abbr_cnt > offset) // adjust tb_no_abbr_cnt
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 {
1176 if (typebuf.tb_no_abbr_cnt < offset + len)
1177 typebuf.tb_no_abbr_cnt = offset;
1178 else
1179 typebuf.tb_no_abbr_cnt -= len;
1180 }
1181
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001182#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar30613902019-12-01 22:11:18 +01001183 // Reset the flag that text received from a client or from feedkeys()
1184 // was inserted in the typeahead buffer.
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001185 typebuf_was_filled = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186#endif
1187 if (++typebuf.tb_change_cnt == 0)
1188 typebuf.tb_change_cnt = 1;
1189}
1190
1191/*
1192 * Write typed characters to script file.
1193 * If recording is on put the character in the recordbuffer.
1194 */
1195 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001196gotchars(char_u *chars, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197{
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001198 char_u *s = chars;
1199 int i;
1200 static char_u buf[4];
1201 static int buflen = 0;
1202 int todo = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203
Bram Moolenaar4e86cba2007-05-06 11:56:32 +00001204 while (todo--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 {
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001206 buf[buflen++] = *s++;
1207
1208 // When receiving a special key sequence, store it until we have all
1209 // the bytes and we can decide what to do with it.
1210 if (buflen == 1 && buf[0] == K_SPECIAL)
1211 continue;
1212 if (buflen == 2)
1213 continue;
1214 if (buflen == 3 && buf[1] == KS_EXTRA
1215 && (buf[2] == KE_FOCUSGAINED || buf[2] == KE_FOCUSLOST))
1216 {
1217 // Drop K_FOCUSGAINED and K_FOCUSLOST, they are not useful in a
1218 // recording.
1219 buflen = 0;
1220 continue;
1221 }
1222
Bram Moolenaar30613902019-12-01 22:11:18 +01001223 // Handle one byte at a time; no translation to be done.
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001224 for (i = 0; i < buflen; ++i)
1225 updatescript(buf[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001227 if (reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 {
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001229 buf[buflen] = NUL;
1230 add_buff(&recordbuff, buf, (long)buflen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001231 // remember how many chars were last recorded
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001232 last_recorded_len += buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 }
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001234 buflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 }
1236 may_sync_undo();
1237
1238#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01001239 // output "debug mode" message next time in debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 debug_did_msg = FALSE;
1241#endif
1242
Bram Moolenaar30613902019-12-01 22:11:18 +01001243 // Since characters have been typed, consider the following to be in
1244 // another mapping. Search string will be kept in history.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 ++maptick;
1246}
1247
1248/*
1249 * Sync undo. Called when typed characters are obtained from the typeahead
1250 * buffer, or when a menu is used.
1251 * Do not sync:
1252 * - In Insert mode, unless cursor key has been used.
1253 * - While reading a script file.
1254 * - When no_u_sync is non-zero.
1255 */
1256 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001257may_sync_undo(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258{
1259 if ((!(State & (INSERT + CMDLINE)) || arrow_used)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001260 && scriptin[curscript] == NULL)
1261 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262}
1263
1264/*
1265 * Make "typebuf" empty and allocate new buffers.
1266 * Returns FAIL when out of memory.
1267 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001268 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001269alloc_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270{
1271 typebuf.tb_buf = alloc(TYPELEN_INIT);
1272 typebuf.tb_noremap = alloc(TYPELEN_INIT);
1273 if (typebuf.tb_buf == NULL || typebuf.tb_noremap == NULL)
1274 {
1275 free_typebuf();
1276 return FAIL;
1277 }
1278 typebuf.tb_buflen = TYPELEN_INIT;
Bram Moolenaar30613902019-12-01 22:11:18 +01001279 typebuf.tb_off = MAXMAPLEN + 4; // can insert without realloc
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 typebuf.tb_len = 0;
1281 typebuf.tb_maplen = 0;
1282 typebuf.tb_silent = 0;
1283 typebuf.tb_no_abbr_cnt = 0;
1284 if (++typebuf.tb_change_cnt == 0)
1285 typebuf.tb_change_cnt = 1;
1286 return OK;
1287}
1288
1289/*
1290 * Free the buffers of "typebuf".
1291 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001292 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001293free_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294{
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001295 if (typebuf.tb_buf == typebuf_init)
Bram Moolenaar95f09602016-11-10 20:01:45 +01001296 internal_error("Free typebuf 1");
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001297 else
Bram Moolenaar0f1c6702019-09-28 15:24:00 +02001298 VIM_CLEAR(typebuf.tb_buf);
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00001299 if (typebuf.tb_noremap == noremapbuf_init)
Bram Moolenaar95f09602016-11-10 20:01:45 +01001300 internal_error("Free typebuf 2");
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001301 else
Bram Moolenaar0f1c6702019-09-28 15:24:00 +02001302 VIM_CLEAR(typebuf.tb_noremap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303}
1304
1305/*
1306 * When doing ":so! file", the current typeahead needs to be saved, and
1307 * restored when "file" has been read completely.
1308 */
1309static typebuf_T saved_typebuf[NSCRIPT];
1310
1311 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001312save_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313{
1314 init_typebuf();
1315 saved_typebuf[curscript] = typebuf;
Bram Moolenaar30613902019-12-01 22:11:18 +01001316 // If out of memory: restore typebuf and close file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 if (alloc_typebuf() == FAIL)
1318 {
1319 closescript();
1320 return FAIL;
1321 }
1322 return OK;
1323}
1324
Bram Moolenaar30613902019-12-01 22:11:18 +01001325static int old_char = -1; // character put back by vungetc()
1326static int old_mod_mask; // mod_mask for ungotten character
1327static int old_mouse_row; // mouse_row related to old_char
1328static int old_mouse_col; // mouse_col related to old_char
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001329
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330/*
1331 * Save all three kinds of typeahead, so that the user must type at a prompt.
1332 */
1333 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001334save_typeahead(tasave_T *tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335{
1336 tp->save_typebuf = typebuf;
1337 tp->typebuf_valid = (alloc_typebuf() == OK);
1338 if (!tp->typebuf_valid)
1339 typebuf = tp->save_typebuf;
1340
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001341 tp->old_char = old_char;
1342 tp->old_mod_mask = old_mod_mask;
1343 old_char = -1;
1344
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001345 tp->save_readbuf1 = readbuf1;
Bram Moolenaar285e3352018-04-18 23:01:13 +02001346 readbuf1.bh_first.b_next = NULL;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001347 tp->save_readbuf2 = readbuf2;
Bram Moolenaar285e3352018-04-18 23:01:13 +02001348 readbuf2.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349# ifdef USE_INPUT_BUF
1350 tp->save_inputbuf = get_input_buf();
1351# endif
1352}
1353
1354/*
1355 * Restore the typeahead to what it was before calling save_typeahead().
1356 * The allocated memory is freed, can only be called once!
1357 */
1358 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001359restore_typeahead(tasave_T *tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360{
1361 if (tp->typebuf_valid)
1362 {
1363 free_typebuf();
1364 typebuf = tp->save_typebuf;
1365 }
1366
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001367 old_char = tp->old_char;
1368 old_mod_mask = tp->old_mod_mask;
1369
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001370 free_buff(&readbuf1);
1371 readbuf1 = tp->save_readbuf1;
1372 free_buff(&readbuf2);
1373 readbuf2 = tp->save_readbuf2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374# ifdef USE_INPUT_BUF
1375 set_input_buf(tp->save_inputbuf);
1376# endif
1377}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378
1379/*
1380 * Open a new script file for the ":source!" command.
1381 */
1382 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001383openscript(
1384 char_u *name,
Bram Moolenaar30613902019-12-01 22:11:18 +01001385 int directly) // when TRUE execute directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386{
1387 if (curscript + 1 == NSCRIPT)
1388 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001389 emsg(_(e_nesting));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 return;
1391 }
Bram Moolenaar53575522019-05-22 22:38:25 +02001392
1393 // Disallow sourcing a file in the sandbox, the commands would be executed
1394 // later, possibly outside of the sandbox.
1395 if (check_secure())
1396 return;
1397
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001398#ifdef FEAT_EVAL
1399 if (ignore_script)
Bram Moolenaar30613902019-12-01 22:11:18 +01001400 // Not reading from script, also don't open one. Warning message?
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001401 return;
1402#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403
Bram Moolenaar30613902019-12-01 22:11:18 +01001404 if (scriptin[curscript] != NULL) // already reading script
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 ++curscript;
Bram Moolenaar30613902019-12-01 22:11:18 +01001406 // use NameBuff for expanded name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 expand_env(name, NameBuff, MAXPATHL);
1408 if ((scriptin[curscript] = mch_fopen((char *)NameBuff, READBIN)) == NULL)
1409 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001410 semsg(_(e_notopen), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 if (curscript)
1412 --curscript;
1413 return;
1414 }
1415 if (save_typebuf() == FAIL)
1416 return;
1417
1418 /*
1419 * Execute the commands from the file right now when using ":source!"
1420 * after ":global" or ":argdo" or in a loop. Also when another command
1421 * follows. This means the display won't be updated. Don't do this
1422 * always, "make test" would fail.
1423 */
1424 if (directly)
1425 {
1426 oparg_T oa;
1427 int oldcurscript;
1428 int save_State = State;
1429 int save_restart_edit = restart_edit;
1430 int save_insertmode = p_im;
1431 int save_finish_op = finish_op;
1432 int save_msg_scroll = msg_scroll;
1433
1434 State = NORMAL;
Bram Moolenaar30613902019-12-01 22:11:18 +01001435 msg_scroll = FALSE; // no msg scrolling in Normal mode
1436 restart_edit = 0; // don't go to Insert mode
1437 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 clear_oparg(&oa);
1439 finish_op = FALSE;
1440
1441 oldcurscript = curscript;
1442 do
1443 {
Bram Moolenaar386b43e2019-05-19 21:57:11 +02001444 update_topline_cursor(); // update cursor position and topline
1445 normal_cmd(&oa, FALSE); // execute one command
1446 vpeekc(); // check for end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 }
1448 while (scriptin[oldcurscript] != NULL);
1449
1450 State = save_State;
1451 msg_scroll = save_msg_scroll;
1452 restart_edit = save_restart_edit;
1453 p_im = save_insertmode;
1454 finish_op = save_finish_op;
1455 }
1456}
1457
1458/*
1459 * Close the currently active input script.
1460 */
1461 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001462closescript(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463{
1464 free_typebuf();
1465 typebuf = saved_typebuf[curscript];
1466
1467 fclose(scriptin[curscript]);
1468 scriptin[curscript] = NULL;
1469 if (curscript > 0)
1470 --curscript;
1471}
1472
Bram Moolenaarea408852005-06-25 22:49:46 +00001473#if defined(EXITFREE) || defined(PROTO)
1474 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001475close_all_scripts(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00001476{
1477 while (scriptin[0] != NULL)
1478 closescript();
1479}
1480#endif
1481
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482/*
1483 * Return TRUE when reading keys from a script file.
1484 */
1485 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001486using_script(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487{
1488 return scriptin[curscript] != NULL;
1489}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490
1491/*
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001492 * This function is called just before doing a blocking wait. Thus after
1493 * waiting 'updatetime' for a character to arrive.
1494 */
1495 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001496before_blocking(void)
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001497{
1498 updatescript(0);
1499#ifdef FEAT_EVAL
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001500 if (may_garbage_collect)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001501 garbage_collect(FALSE);
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001502#endif
1503}
1504
1505/*
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001506 * updatescript() is called when a character can be written into the script file
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 * or when we have waited some time for a character (c == 0)
1508 *
1509 * All the changed memfiles are synced if c == 0 or when the number of typed
1510 * characters reaches 'updatecount' and 'updatecount' is non-zero.
1511 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001512 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001513updatescript(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514{
1515 static int count = 0;
1516
1517 if (c && scriptout)
1518 putc(c, scriptout);
1519 if (c == 0 || (p_uc > 0 && ++count >= p_uc))
1520 {
1521 ml_sync_all(c == 0, TRUE);
1522 count = 0;
1523 }
1524}
1525
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526/*
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001527 * Convert "c" plus "mod_mask" to merge the effect of modifyOtherKeys into the
1528 * character.
1529 */
1530 int
1531merge_modifyOtherKeys(int c_arg)
1532{
1533 int c = c_arg;
1534
1535 if (mod_mask & MOD_MASK_CTRL)
1536 {
1537 if ((c >= '`' && c <= 0x7f) || (c >= '@' && c <= '_'))
1538 {
1539 c &= 0x1f;
1540 mod_mask &= ~MOD_MASK_CTRL;
1541 }
1542 else if (c == '6')
1543 {
1544 // CTRL-6 is equivalent to CTRL-^
1545 c = 0x1e;
1546 mod_mask &= ~MOD_MASK_CTRL;
1547 }
1548 }
1549 if ((mod_mask & (MOD_MASK_META | MOD_MASK_ALT))
1550 && c >= 0 && c <= 127)
1551 {
1552 c += 0x80;
1553 mod_mask &= ~(MOD_MASK_META|MOD_MASK_ALT);
1554 }
1555 return c;
1556}
1557
1558/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 * Get the next input character.
1560 * Can return a special key or a multi-byte character.
1561 * Can return NUL when called recursively, use safe_vgetc() if that's not
1562 * wanted.
1563 * This translates escaped K_SPECIAL and CSI bytes to a K_SPECIAL or CSI byte.
1564 * Collects the bytes of a multibyte character into the whole character.
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00001565 * Returns the modifiers in the global "mod_mask".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 */
1567 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001568vgetc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569{
1570 int c, c2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001572 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001575#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01001576 // Do garbage collection when garbagecollect() was called previously and
1577 // we are now at the toplevel.
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001578 if (may_garbage_collect && want_garbage_collect)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001579 garbage_collect(FALSE);
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001580#endif
1581
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 /*
1583 * If a character was put back with vungetc, it was already processed.
1584 * Return it directly.
1585 */
1586 if (old_char != -1)
1587 {
1588 c = old_char;
1589 old_char = -1;
1590 mod_mask = old_mod_mask;
Bram Moolenaarb8978712013-03-16 21:42:16 +01001591 mouse_row = old_mouse_row;
1592 mouse_col = old_mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001594 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 {
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001596 mod_mask = 0x0;
1597 last_recorded_len = 0;
1598 for (;;) // this is done twice if there are modifiers
1599 {
1600 int did_inc = FALSE;
Bram Moolenaar2455c4e2015-09-15 18:29:39 +02001601
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001602 if (mod_mask
Bram Moolenaar39719052017-09-05 22:20:46 +02001603#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001604 || im_is_preediting()
Bram Moolenaar39719052017-09-05 22:20:46 +02001605#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001606#if defined(FEAT_PROP_POPUP)
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001607 || popup_no_mapping()
1608#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001609 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 {
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001611 // no mapping after modifier has been read
1612 ++no_mapping;
1613 ++allow_keys;
1614 did_inc = TRUE; // mod_mask may change value
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 }
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001616 c = vgetorpeek(TRUE);
1617 if (did_inc)
1618 {
1619 --no_mapping;
1620 --allow_keys;
1621 }
1622
1623 // Get two extra bytes for special keys
1624 if (c == K_SPECIAL
1625#ifdef FEAT_GUI
Bram Moolenaara9dd2d32019-04-29 21:58:41 +02001626 || (gui.in_use && c == CSI)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001627#endif
1628 )
1629 {
1630 int save_allow_keys = allow_keys;
1631
1632 ++no_mapping;
1633 allow_keys = 0; // make sure BS is not found
1634 c2 = vgetorpeek(TRUE); // no mapping for these chars
1635 c = vgetorpeek(TRUE);
1636 --no_mapping;
1637 allow_keys = save_allow_keys;
1638 if (c2 == KS_MODIFIER)
1639 {
1640 mod_mask = c;
1641 continue;
1642 }
1643 c = TO_SPECIAL(c2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644
Bram Moolenaar4f974752019-02-17 17:44:42 +01001645#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001646 // Handle K_TEAROFF here, the caller of vgetc() doesn't need to
1647 // know that a menu was torn off
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001648 if (
1649# ifdef VIMDLL
1650 gui.in_use &&
1651# endif
1652 c == K_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 {
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001654 char_u name[200];
1655 int i;
1656
1657 // get menu path, it ends with a <CR>
1658 for (i = 0; (c = vgetorpeek(TRUE)) != '\r'; )
1659 {
1660 name[i] = c;
1661 if (i < 199)
1662 ++i;
1663 }
1664 name[i] = NUL;
1665 gui_make_tearoff(name);
1666 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668#endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001669#if defined(FEAT_GUI) && defined(FEAT_GUI_GTK) && defined(FEAT_MENU)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001670 // GTK: <F10> normally selects the menu, but it's passed until
1671 // here to allow mapping it. Intercept and invoke the GTK
1672 // behavior if it's not mapped.
1673 if (c == K_F10 && gui.menubar != NULL)
1674 {
1675 gtk_menu_shell_select_first(
1676 GTK_MENU_SHELL(gui.menubar), FALSE);
1677 continue;
1678 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001679#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680#ifdef FEAT_GUI
Bram Moolenaara9dd2d32019-04-29 21:58:41 +02001681 if (gui.in_use)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001682 {
Bram Moolenaara9dd2d32019-04-29 21:58:41 +02001683 // Handle focus event here, so that the caller doesn't
1684 // need to know about it. Return K_IGNORE so that we loop
1685 // once (needed if 'lazyredraw' is set).
1686 if (c == K_FOCUSGAINED || c == K_FOCUSLOST)
1687 {
1688 ui_focus_change(c == K_FOCUSGAINED);
1689 c = K_IGNORE;
1690 }
Bram Moolenaar9c8791f2007-09-05 19:47:23 +00001691
Bram Moolenaara9dd2d32019-04-29 21:58:41 +02001692 // Translate K_CSI to CSI. The special key is only used
1693 // to avoid it being recognized as the start of a special
1694 // key.
1695 if (c == K_CSI)
1696 c = CSI;
1697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001699 }
1700 // a keypad or special function key was not mapped, use it like
1701 // its ASCII equivalent
1702 switch (c)
1703 {
1704 case K_KPLUS: c = '+'; break;
1705 case K_KMINUS: c = '-'; break;
1706 case K_KDIVIDE: c = '/'; break;
1707 case K_KMULTIPLY: c = '*'; break;
1708 case K_KENTER: c = CAR; break;
1709 case K_KPOINT:
Bram Moolenaar4f974752019-02-17 17:44:42 +01001710#ifdef MSWIN
Bram Moolenaar8e85db02018-07-27 23:16:51 +02001711 // Can be either '.' or a ',',
1712 // depending on the type of keypad.
1713 c = MapVirtualKey(VK_DECIMAL, 2); break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001714#else
Bram Moolenaar8e85db02018-07-27 23:16:51 +02001715 c = '.'; break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001716#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001717 case K_K0: c = '0'; break;
1718 case K_K1: c = '1'; break;
1719 case K_K2: c = '2'; break;
1720 case K_K3: c = '3'; break;
1721 case K_K4: c = '4'; break;
1722 case K_K5: c = '5'; break;
1723 case K_K6: c = '6'; break;
1724 case K_K7: c = '7'; break;
1725 case K_K8: c = '8'; break;
1726 case K_K9: c = '9'; break;
Bram Moolenaara88d9682005-03-25 21:45:43 +00001727
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001728 case K_XHOME:
1729 case K_ZHOME: if (mod_mask == MOD_MASK_SHIFT)
Bram Moolenaara88d9682005-03-25 21:45:43 +00001730 {
1731 c = K_S_HOME;
1732 mod_mask = 0;
1733 }
1734 else if (mod_mask == MOD_MASK_CTRL)
1735 {
1736 c = K_C_HOME;
1737 mod_mask = 0;
1738 }
1739 else
1740 c = K_HOME;
1741 break;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001742 case K_XEND:
1743 case K_ZEND: if (mod_mask == MOD_MASK_SHIFT)
Bram Moolenaara88d9682005-03-25 21:45:43 +00001744 {
1745 c = K_S_END;
1746 mod_mask = 0;
1747 }
1748 else if (mod_mask == MOD_MASK_CTRL)
1749 {
1750 c = K_C_END;
1751 mod_mask = 0;
1752 }
1753 else
1754 c = K_END;
1755 break;
1756
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001757 case K_XUP: c = K_UP; break;
1758 case K_XDOWN: c = K_DOWN; break;
1759 case K_XLEFT: c = K_LEFT; break;
1760 case K_XRIGHT: c = K_RIGHT; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001763 // For a multi-byte character get all the bytes and return the
1764 // converted character.
1765 // Note: This will loop until enough bytes are received!
1766 if (has_mbyte && (n = MB_BYTE2LEN_CHECK(c)) > 1)
1767 {
1768 ++no_mapping;
1769 buf[0] = c;
1770 for (i = 1; i < n; ++i)
1771 {
1772 buf[i] = vgetorpeek(TRUE);
1773 if (buf[i] == K_SPECIAL
1774#ifdef FEAT_GUI
Bram Moolenaar386b43e2019-05-19 21:57:11 +02001775 || (
1776# ifdef VIMDLL
1777 gui.in_use &&
1778# endif
1779 buf[i] == CSI)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001780#endif
1781 )
1782 {
1783 // Must be a K_SPECIAL - KS_SPECIAL - KE_FILLER
1784 // sequence, which represents a K_SPECIAL (0x80),
1785 // or a CSI - KS_EXTRA - KE_CSI sequence, which
1786 // represents a CSI (0x9B),
1787 // or a K_SPECIAL - KS_EXTRA - KE_CSI, which is CSI
1788 // too.
1789 c = vgetorpeek(TRUE);
1790 if (vgetorpeek(TRUE) == (int)KE_CSI && c == KS_EXTRA)
1791 buf[i] = CSI;
1792 }
1793 }
1794 --no_mapping;
1795 c = (*mb_ptr2char)(buf);
1796 }
1797
Bram Moolenaar00eab7f2019-10-10 21:49:28 +02001798 if (!no_reduce_keys)
Bram Moolenaar00eab7f2019-10-10 21:49:28 +02001799 // A modifier was not used for a mapping, apply it to ASCII
Bram Moolenaar459fd782019-10-13 16:43:39 +02001800 // keys. Shift would already have been applied.
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001801 c = merge_modifyOtherKeys(c);
Bram Moolenaar00eab7f2019-10-10 21:49:28 +02001802
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001803 break;
1804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001806
1807#ifdef FEAT_EVAL
1808 /*
1809 * In the main loop "may_garbage_collect" can be set to do garbage
1810 * collection in the first next vgetc(). It's disabled after that to
1811 * avoid internally used Lists and Dicts to be freed.
1812 */
1813 may_garbage_collect = FALSE;
1814#endif
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001815
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001816#ifdef FEAT_BEVAL_TERM
Bram Moolenaarc2f50542019-07-05 23:24:56 +02001817 if (c != K_MOUSEMOVE && c != K_IGNORE && c != K_CURSORHOLD)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001818 {
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001819 // Don't trigger 'balloonexpr' unless only the mouse was moved.
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001820 bevalexpr_due_set = FALSE;
1821 ui_remove_balloon();
1822 }
1823#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001824#ifdef FEAT_PROP_POPUP
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001825 if (popup_do_filter(c))
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02001826 {
1827 if (c == Ctrl_C)
1828 got_int = FALSE; // avoid looping
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001829 c = K_IGNORE;
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02001830 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001831#endif
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001832
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001833 // Need to process the character before we know it's safe to do something
1834 // else.
1835 if (c != K_IGNORE)
Bram Moolenaard103ee72019-09-18 21:15:31 +02001836 state_no_longer_safe("key typed");
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001837
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001838 return c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839}
1840
1841/*
1842 * Like vgetc(), but never return a NUL when called recursively, get a key
1843 * directly from the user (ignoring typeahead).
1844 */
1845 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001846safe_vgetc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847{
1848 int c;
1849
1850 c = vgetc();
1851 if (c == NUL)
1852 c = get_keystroke();
1853 return c;
1854}
1855
1856/*
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001857 * Like safe_vgetc(), but loop to handle K_IGNORE.
1858 * Also ignore scrollbar events.
1859 */
1860 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001861plain_vgetc(void)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001862{
1863 int c;
1864
1865 do
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001866 c = safe_vgetc();
Bram Moolenaarabab0b02019-03-30 18:47:01 +01001867 while (c == K_IGNORE || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR);
Bram Moolenaarec2da362017-01-21 20:04:22 +01001868
1869 if (c == K_PS)
Bram Moolenaar30613902019-12-01 22:11:18 +01001870 // Only handle the first pasted character. Drop the rest, since we
1871 // don't know what to do with it.
Bram Moolenaarec2da362017-01-21 20:04:22 +01001872 c = bracketed_paste(PASTE_ONE_CHAR, FALSE, NULL);
1873
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001874 return c;
1875}
1876
1877/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 * Check if a character is available, such that vgetc() will not block.
1879 * If the next character is a special character or multi-byte, the returned
1880 * character is not valid!.
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02001881 * Returns NUL if no character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 */
1883 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001884vpeekc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885{
1886 if (old_char != -1)
1887 return old_char;
1888 return vgetorpeek(FALSE);
1889}
1890
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01001891#if defined(FEAT_TERMRESPONSE) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892/*
1893 * Like vpeekc(), but don't allow mapping. Do allow checking for terminal
1894 * codes.
1895 */
1896 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001897vpeekc_nomap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898{
1899 int c;
1900
1901 ++no_mapping;
1902 ++allow_keys;
1903 c = vpeekc();
1904 --no_mapping;
1905 --allow_keys;
1906 return c;
1907}
1908#endif
1909
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910/*
1911 * Check if any character is available, also half an escape sequence.
1912 * Trick: when no typeahead found, but there is something in the typeahead
1913 * buffer, it must be an ESC that is recognized as the start of a key code.
1914 */
1915 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001916vpeekc_any(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917{
1918 int c;
1919
1920 c = vpeekc();
1921 if (c == NUL && typebuf.tb_len > 0)
1922 c = ESC;
1923 return c;
1924}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925
1926/*
1927 * Call vpeekc() without causing anything to be mapped.
1928 * Return TRUE if a character is available, FALSE otherwise.
1929 */
1930 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001931char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932{
1933 int retval;
1934
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01001935#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01001936 // When test_override("char_avail", 1) was called pretend there is no
1937 // typeahead.
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01001938 if (disable_char_avail_for_testing)
1939 return FALSE;
1940#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 ++no_mapping;
1942 retval = vpeekc();
1943 --no_mapping;
1944 return (retval != NUL);
1945}
1946
Bram Moolenaar9c658c92019-09-15 21:00:54 +02001947#if defined(FEAT_EVAL) || defined(PROTO)
1948/*
1949 * "getchar()" function
1950 */
1951 void
1952f_getchar(typval_T *argvars, typval_T *rettv)
1953{
1954 varnumber_T n;
1955 int error = FALSE;
1956
1957#ifdef MESSAGE_QUEUE
1958 // vpeekc() used to check for messages, but that caused problems, invoking
1959 // a callback where it was not expected. Some plugins use getchar(1) in a
1960 // loop to await a message, therefore make sure we check for messages here.
1961 parse_queued_messages();
1962#endif
1963
Bram Moolenaar30613902019-12-01 22:11:18 +01001964 // Position the cursor. Needed after a message that ends in a space.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02001965 windgoto(msg_row, msg_col);
1966
1967 ++no_mapping;
1968 ++allow_keys;
1969 for (;;)
1970 {
1971 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar30613902019-12-01 22:11:18 +01001972 // getchar(): blocking wait.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02001973 n = plain_vgetc();
1974 else if (tv_get_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar30613902019-12-01 22:11:18 +01001975 // getchar(1): only check if char avail
Bram Moolenaar9c658c92019-09-15 21:00:54 +02001976 n = vpeekc_any();
1977 else if (error || vpeekc_any() == NUL)
Bram Moolenaar30613902019-12-01 22:11:18 +01001978 // illegal argument or getchar(0) and no char avail: return zero
Bram Moolenaar9c658c92019-09-15 21:00:54 +02001979 n = 0;
1980 else
Bram Moolenaar30613902019-12-01 22:11:18 +01001981 // getchar(0) and char avail: return char
Bram Moolenaar9c658c92019-09-15 21:00:54 +02001982 n = plain_vgetc();
1983
1984 if (n == K_IGNORE)
1985 continue;
1986 break;
1987 }
1988 --no_mapping;
1989 --allow_keys;
1990
1991 set_vim_var_nr(VV_MOUSE_WIN, 0);
1992 set_vim_var_nr(VV_MOUSE_WINID, 0);
1993 set_vim_var_nr(VV_MOUSE_LNUM, 0);
1994 set_vim_var_nr(VV_MOUSE_COL, 0);
1995
1996 rettv->vval.v_number = n;
1997 if (IS_SPECIAL(n) || mod_mask != 0)
1998 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001999 char_u temp[10]; // modifier: 3, mbyte-char: 6, NUL: 1
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002000 int i = 0;
2001
Bram Moolenaar30613902019-12-01 22:11:18 +01002002 // Turn a special key into three bytes, plus modifier.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002003 if (mod_mask != 0)
2004 {
2005 temp[i++] = K_SPECIAL;
2006 temp[i++] = KS_MODIFIER;
2007 temp[i++] = mod_mask;
2008 }
2009 if (IS_SPECIAL(n))
2010 {
2011 temp[i++] = K_SPECIAL;
2012 temp[i++] = K_SECOND(n);
2013 temp[i++] = K_THIRD(n);
2014 }
2015 else if (has_mbyte)
2016 i += (*mb_char2bytes)(n, temp + i);
2017 else
2018 temp[i++] = n;
2019 temp[i++] = NUL;
2020 rettv->v_type = VAR_STRING;
2021 rettv->vval.v_string = vim_strsave(temp);
2022
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002023 if (is_mouse_key(n))
2024 {
2025 int row = mouse_row;
2026 int col = mouse_col;
2027 win_T *win;
2028 linenr_T lnum;
2029 win_T *wp;
2030 int winnr = 1;
2031
2032 if (row >= 0 && col >= 0)
2033 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002034 // Find the window at the mouse coordinates and compute the
2035 // text position.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002036 win = mouse_find_win(&row, &col, FIND_POPUP);
2037 if (win == NULL)
2038 return;
2039 (void)mouse_comp_pos(win, &row, &col, &lnum, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002040#ifdef FEAT_PROP_POPUP
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002041 if (WIN_IS_POPUP(win))
2042 winnr = 0;
2043 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002044#endif
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002045 for (wp = firstwin; wp != win && wp != NULL;
2046 wp = wp->w_next)
2047 ++winnr;
2048 set_vim_var_nr(VV_MOUSE_WIN, winnr);
2049 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
2050 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
2051 set_vim_var_nr(VV_MOUSE_COL, col + 1);
2052 }
2053 }
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002054 }
2055}
2056
2057/*
2058 * "getcharmod()" function
2059 */
2060 void
2061f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
2062{
2063 rettv->vval.v_number = mod_mask;
2064}
2065#endif // FEAT_EVAL
2066
2067#if defined(MESSAGE_QUEUE) || defined(PROTO)
2068# define MAX_REPEAT_PARSE 8
2069
2070/*
2071 * Process messages that have been queued for netbeans or clientserver.
2072 * Also check if any jobs have ended.
2073 * These functions can call arbitrary vimscript and should only be called when
2074 * it is safe to do so.
2075 */
2076 void
2077parse_queued_messages(void)
2078{
Bram Moolenaar1cac7092019-10-22 21:54:31 +02002079 int old_curwin_id;
2080 int old_curbuf_fnum;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002081 int i;
2082 int save_may_garbage_collect = may_garbage_collect;
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002083 static int entered = 0;
Bram Moolenaard103ee72019-09-18 21:15:31 +02002084 int was_safe = get_was_safe_state();
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002085
2086 // Do not handle messages while redrawing, because it may cause buffers to
2087 // change or be wiped while they are being redrawn.
2088 if (updating_screen)
2089 return;
2090
Bram Moolenaar1cac7092019-10-22 21:54:31 +02002091 // If memory allocation fails during startup we'll exit but curbuf or
2092 // curwin could be NULL.
2093 if (curbuf == NULL || curwin == NULL)
2094 return;
2095
2096 old_curbuf_fnum = curbuf->b_fnum;
2097 old_curwin_id = curwin->w_id;
2098
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002099 ++entered;
2100
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002101 // may_garbage_collect is set in main_loop() to do garbage collection when
2102 // blocking to wait on a character. We don't want that while parsing
2103 // messages, a callback may invoke vgetc() while lists and dicts are in use
2104 // in the call stack.
2105 may_garbage_collect = FALSE;
2106
2107 // Loop when a job ended, but don't keep looping forever.
2108 for (i = 0; i < MAX_REPEAT_PARSE; ++i)
2109 {
2110 // For Win32 mch_breakcheck() does not check for input, do it here.
2111# if defined(MSWIN) && defined(FEAT_JOB_CHANNEL)
2112 channel_handle_events(FALSE);
2113# endif
2114
2115# ifdef FEAT_NETBEANS_INTG
2116 // Process the queued netbeans messages.
2117 netbeans_parse_messages();
2118# endif
2119# ifdef FEAT_JOB_CHANNEL
2120 // Write any buffer lines still to be written.
2121 channel_write_any_lines();
2122
2123 // Process the messages queued on channels.
2124 channel_parse_messages();
2125# endif
2126# if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
2127 // Process the queued clientserver messages.
2128 server_parse_messages();
2129# endif
2130# ifdef FEAT_JOB_CHANNEL
2131 // Check if any jobs have ended. If so, repeat the above to handle
2132 // changes, e.g. stdin may have been closed.
2133 if (job_check_ended())
2134 continue;
2135# endif
2136# ifdef FEAT_TERMINAL
2137 free_unused_terminals();
2138# endif
2139# ifdef FEAT_SOUND_CANBERRA
2140 if (has_sound_callback_in_queue())
2141 invoke_sound_callback();
2142# endif
2143 break;
2144 }
2145
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002146 // When not nested we'll go back to waiting for a typed character. If it
2147 // was safe before then this triggers a SafeStateAgain autocommand event.
Bram Moolenaard103ee72019-09-18 21:15:31 +02002148 if (entered == 1 && was_safe)
Bram Moolenaar37d18072019-09-17 20:28:38 +02002149 may_trigger_safestateagain();
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002150
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002151 may_garbage_collect = save_may_garbage_collect;
2152
2153 // If the current window or buffer changed we need to bail out of the
2154 // waiting loop. E.g. when a job exit callback closes the terminal window.
2155 if (curwin->w_id != old_curwin_id || curbuf->b_fnum != old_curbuf_fnum)
2156 ins_char_typebuf(K_IGNORE);
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002157
2158 --entered;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002159}
2160#endif
2161
2162
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002163typedef enum {
2164 map_result_fail, // failed, break loop
2165 map_result_get, // get a character from typeahead
2166 map_result_retry, // try to map again
2167 map_result_nomatch // no matching mapping, get char
2168} map_result_T;
2169
2170/*
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002171 * Check if the bytes at the start of the typeahead buffer are a character used
2172 * in CTRL-X mode. This includes the form with a CTRL modifier.
2173 */
2174 static int
2175at_ctrl_x_key(void)
2176{
2177 char_u *p = typebuf.tb_buf + typebuf.tb_off;
2178 int c = *p;
2179
2180 if (typebuf.tb_len > 3
2181 && c == K_SPECIAL
2182 && p[1] == KS_MODIFIER
2183 && (p[2] & MOD_MASK_CTRL))
2184 c = p[3] & 0x1f;
2185 return vim_is_ctrl_x_key(c);
2186}
2187
2188/*
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002189 * Handle mappings in the typeahead buffer.
2190 * - When something was mapped, return map_result_retry for recursive mappings.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002191 * - When nothing mapped and typeahead has a character: return map_result_get.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002192 * - When there is no match yet, return map_result_nomatch, need to get more
2193 * typeahead.
2194 */
2195 static int
2196handle_mapping(
2197 int *keylenp,
2198 int *timedout,
2199 int *mapdepth)
2200{
2201 mapblock_T *mp = NULL;
2202 mapblock_T *mp2;
2203 mapblock_T *mp_match;
2204 int mp_match_len = 0;
2205 int max_mlen = 0;
2206 int tb_c1;
2207 int mlen;
2208#ifdef FEAT_LANGMAP
2209 int nolmaplen;
2210#endif
2211 int keylen = *keylenp;
2212 int i;
2213 int local_State = get_real_state();
2214
2215 /*
2216 * Check for a mappable key sequence.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002217 * Walk through one maphash[] list until we find an entry that matches.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002218 *
2219 * Don't look for mappings if:
2220 * - no_mapping set: mapping disabled (e.g. for CTRL-V)
2221 * - maphash_valid not set: no mappings present.
2222 * - typebuf.tb_buf[typebuf.tb_off] should not be remapped
2223 * - in insert or cmdline mode and 'paste' option set
Bram Moolenaareda35f72019-08-03 14:59:44 +02002224 * - waiting for "hit return to continue" and CR or SPACE typed
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002225 * - waiting for a char with --more--
2226 * - in Ctrl-X mode, and we get a valid char for that mode
2227 */
2228 tb_c1 = typebuf.tb_buf[typebuf.tb_off];
2229 if (no_mapping == 0 && is_maphash_valid()
2230 && (no_zero_mapping == 0 || tb_c1 != '0')
2231 && (typebuf.tb_maplen == 0
2232 || (p_remap
2233 && (typebuf.tb_noremap[typebuf.tb_off]
2234 & (RM_NONE|RM_ABBR)) == 0))
2235 && !(p_paste && (State & (INSERT + CMDLINE)))
2236 && !(State == HITRETURN && (tb_c1 == CAR || tb_c1 == ' '))
2237 && State != ASKMORE
2238 && State != CONFIRM
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002239 && !((ctrl_x_mode_not_default() && at_ctrl_x_key())
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002240 || ((compl_cont_status & CONT_LOCAL)
Bram Moolenaare2c453d2019-08-21 14:37:09 +02002241 && (tb_c1 == Ctrl_N || tb_c1 == Ctrl_P))))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002242 {
2243#ifdef FEAT_LANGMAP
2244 if (tb_c1 == K_SPECIAL)
2245 nolmaplen = 2;
2246 else
2247 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002248 LANGMAP_ADJUST(tb_c1, (State & (CMDLINE | INSERT)) == 0
2249 && get_real_state() != SELECTMODE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002250 nolmaplen = 0;
2251 }
2252#endif
2253 // First try buffer-local mappings.
2254 mp = get_buf_maphash_list(local_State, tb_c1);
2255 mp2 = get_maphash_list(local_State, tb_c1);
2256 if (mp == NULL)
2257 {
2258 // There are no buffer-local mappings.
2259 mp = mp2;
2260 mp2 = NULL;
2261 }
Bram Moolenaareda35f72019-08-03 14:59:44 +02002262
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002263 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002264 * Loop until a partly matching mapping is found or all (local)
2265 * mappings have been checked.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002266 * The longest full match is remembered in "mp_match".
Bram Moolenaareda35f72019-08-03 14:59:44 +02002267 * A full match is only accepted if there is no partly match, so "aa"
2268 * and "aaa" can both be mapped.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002269 */
2270 mp_match = NULL;
2271 mp_match_len = 0;
2272 for ( ; mp != NULL;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002273 mp->m_next == NULL ? (mp = mp2, mp2 = NULL) : (mp = mp->m_next))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002274 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002275 // Only consider an entry if the first character matches and it is
2276 // for the current state.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002277 // Skip ":lmap" mappings if keys were mapped.
2278 if (mp->m_keys[0] == tb_c1
2279 && (mp->m_mode & local_State)
Bram Moolenaar459fd782019-10-13 16:43:39 +02002280 && !(mp->m_simplified && seenModifyOtherKeys)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002281 && ((mp->m_mode & LANGMAP) == 0 || typebuf.tb_maplen == 0))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002282 {
2283#ifdef FEAT_LANGMAP
2284 int nomap = nolmaplen;
2285 int c2;
2286#endif
2287 // find the match length of this mapping
2288 for (mlen = 1; mlen < typebuf.tb_len; ++mlen)
2289 {
2290#ifdef FEAT_LANGMAP
2291 c2 = typebuf.tb_buf[typebuf.tb_off + mlen];
2292 if (nomap > 0)
2293 --nomap;
2294 else if (c2 == K_SPECIAL)
2295 nomap = 2;
2296 else
2297 LANGMAP_ADJUST(c2, TRUE);
2298 if (mp->m_keys[mlen] != c2)
2299#else
2300 if (mp->m_keys[mlen] !=
2301 typebuf.tb_buf[typebuf.tb_off + mlen])
2302#endif
2303 break;
2304 }
2305
Bram Moolenaareda35f72019-08-03 14:59:44 +02002306 // Don't allow mapping the first byte(s) of a multi-byte char.
2307 // Happens when mapping <M-a> and then changing 'encoding'.
2308 // Beware that 0x80 is escaped.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002309 {
2310 char_u *p1 = mp->m_keys;
2311 char_u *p2 = mb_unescape(&p1);
2312
2313 if (has_mbyte && p2 != NULL
Bram Moolenaar1614a142019-10-06 22:00:13 +02002314 && MB_BYTE2LEN(tb_c1) > mb_ptr2len(p2))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002315 mlen = 0;
2316 }
2317
2318 // Check an entry whether it matches.
2319 // - Full match: mlen == keylen
2320 // - Partly match: mlen == typebuf.tb_len
2321 keylen = mp->m_keylen;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002322 if (mlen == keylen || (mlen == typebuf.tb_len
2323 && typebuf.tb_len < keylen))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002324 {
2325 char_u *s;
2326 int n;
2327
Bram Moolenaareda35f72019-08-03 14:59:44 +02002328 // If only script-local mappings are allowed, check if the
2329 // mapping starts with K_SNR.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002330 s = typebuf.tb_noremap + typebuf.tb_off;
2331 if (*s == RM_SCRIPT
2332 && (mp->m_keys[0] != K_SPECIAL
2333 || mp->m_keys[1] != KS_EXTRA
Bram Moolenaareda35f72019-08-03 14:59:44 +02002334 || mp->m_keys[2] != (int)KE_SNR))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002335 continue;
2336
Bram Moolenaareda35f72019-08-03 14:59:44 +02002337 // If one of the typed keys cannot be remapped, skip the
2338 // entry.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002339 for (n = mlen; --n >= 0; )
2340 if (*s++ & (RM_NONE|RM_ABBR))
2341 break;
2342 if (n >= 0)
2343 continue;
2344
2345 if (keylen > typebuf.tb_len)
2346 {
2347 if (!*timedout && !(mp_match != NULL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002348 && mp_match->m_nowait))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002349 {
2350 // break at a partly match
2351 keylen = KEYLEN_PART_MAP;
2352 break;
2353 }
2354 }
2355 else if (keylen > mp_match_len)
2356 {
2357 // found a longer match
2358 mp_match = mp;
2359 mp_match_len = keylen;
2360 }
2361 }
2362 else
Bram Moolenaareda35f72019-08-03 14:59:44 +02002363 // No match; may have to check for termcode at next
2364 // character.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002365 if (max_mlen < mlen)
2366 max_mlen = mlen;
2367 }
2368 }
2369
Bram Moolenaareda35f72019-08-03 14:59:44 +02002370 // If no partly match found, use the longest full match.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002371 if (keylen != KEYLEN_PART_MAP)
2372 {
2373 mp = mp_match;
2374 keylen = mp_match_len;
2375 }
2376 }
2377
2378 /*
2379 * Check for match with 'pastetoggle'
2380 */
2381 if (*p_pt != NUL && mp == NULL && (State & (INSERT|NORMAL)))
2382 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002383 for (mlen = 0; mlen < typebuf.tb_len && p_pt[mlen]; ++mlen)
2384 if (p_pt[mlen] != typebuf.tb_buf[typebuf.tb_off + mlen])
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002385 break;
2386 if (p_pt[mlen] == NUL) // match
2387 {
2388 // write chars to script file(s)
2389 if (mlen > typebuf.tb_maplen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002390 gotchars(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_maplen,
2391 mlen - typebuf.tb_maplen);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002392
2393 del_typebuf(mlen, 0); // remove the chars
Bram Moolenaareda35f72019-08-03 14:59:44 +02002394 set_option_value((char_u *)"paste", (long)!p_paste, NULL, 0);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002395 if (!(State & INSERT))
2396 {
2397 msg_col = 0;
2398 msg_row = Rows - 1;
2399 msg_clr_eos(); // clear ruler
2400 }
2401 status_redraw_all();
2402 redraw_statuslines();
2403 showmode();
2404 setcursor();
2405 *keylenp = keylen;
2406 return map_result_retry;
2407 }
2408 // Need more chars for partly match.
2409 if (mlen == typebuf.tb_len)
2410 keylen = KEYLEN_PART_KEY;
2411 else if (max_mlen < mlen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002412 // no match, may have to check for termcode at next character
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002413 max_mlen = mlen + 1;
2414 }
2415
Bram Moolenaareda35f72019-08-03 14:59:44 +02002416 if ((mp == NULL || max_mlen >= mp_match_len) && keylen != KEYLEN_PART_MAP)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002417 {
2418 int save_keylen = keylen;
2419
2420 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002421 * When no matching mapping found or found a non-matching mapping that
2422 * matches at least what the matching mapping matched:
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002423 * Check if we have a terminal code, when:
Bram Moolenaareda35f72019-08-03 14:59:44 +02002424 * - mapping is allowed,
2425 * - keys have not been mapped,
2426 * - and not an ESC sequence, not in insert mode or p_ek is on,
2427 * - and when not timed out,
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002428 */
2429 if ((no_mapping == 0 || allow_keys != 0)
2430 && (typebuf.tb_maplen == 0
2431 || (p_remap && typebuf.tb_noremap[
Bram Moolenaareda35f72019-08-03 14:59:44 +02002432 typebuf.tb_off] == RM_YES))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002433 && !*timedout)
2434 {
2435 keylen = check_termcode(max_mlen + 1,
2436 NULL, 0, NULL);
2437
Bram Moolenaareda35f72019-08-03 14:59:44 +02002438 // If no termcode matched but 'pastetoggle' matched partially it's
2439 // like an incomplete key sequence.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002440 if (keylen == 0 && save_keylen == KEYLEN_PART_KEY)
2441 keylen = KEYLEN_PART_KEY;
2442
Bram Moolenaareda35f72019-08-03 14:59:44 +02002443 // When getting a partial match, but the last characters were not
2444 // typed, don't wait for a typed character to complete the
2445 // termcode. This helps a lot when a ":normal" command ends in an
2446 // ESC.
2447 if (keylen < 0 && typebuf.tb_len == typebuf.tb_maplen)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002448 keylen = 0;
2449 }
2450 else
2451 keylen = 0;
2452 if (keylen == 0) // no matching terminal code
2453 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002454#ifdef AMIGA
2455 // check for window bounds report
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002456 if (typebuf.tb_maplen == 0 && (typebuf.tb_buf[
Bram Moolenaareda35f72019-08-03 14:59:44 +02002457 typebuf.tb_off] & 0xff) == CSI)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002458 {
2459 char_u *s;
2460
2461 for (s = typebuf.tb_buf + typebuf.tb_off + 1;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002462 s < typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len
2463 && (VIM_ISDIGIT(*s) || *s == ';' || *s == ' ');
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002464 ++s)
2465 ;
2466 if (*s == 'r' || *s == '|') // found one
2467 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002468 del_typebuf(
2469 (int)(s + 1 - (typebuf.tb_buf + typebuf.tb_off)), 0);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002470 // get size and redraw screen
2471 shell_resized();
2472 *keylenp = keylen;
2473 return map_result_retry;
2474 }
2475 if (*s == NUL) // need more characters
2476 keylen = KEYLEN_PART_KEY;
2477 }
2478 if (keylen >= 0)
2479#endif
Bram Moolenaareda35f72019-08-03 14:59:44 +02002480 // When there was a matching mapping and no termcode could be
2481 // replaced after another one, use that mapping (loop around).
2482 // If there was no mapping at all use the character from the
2483 // typeahead buffer right here.
2484 if (mp == NULL)
2485 {
2486 *keylenp = keylen;
2487 return map_result_get; // got character, break for loop
2488 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002489 }
2490
2491 if (keylen > 0) // full matching terminal code
2492 {
2493#if defined(FEAT_GUI) && defined(FEAT_MENU)
2494 if (typebuf.tb_len >= 2
Bram Moolenaareda35f72019-08-03 14:59:44 +02002495 && typebuf.tb_buf[typebuf.tb_off] == K_SPECIAL
2496 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_MENU)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002497 {
2498 int idx;
2499
Bram Moolenaareda35f72019-08-03 14:59:44 +02002500 // Using a menu may cause a break in undo! It's like using
2501 // gotchars(), but without recording or writing to a script
2502 // file.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002503 may_sync_undo();
2504 del_typebuf(3, 0);
2505 idx = get_menu_index(current_menu, local_State);
2506 if (idx != MENU_INDEX_INVALID)
2507 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002508 // In Select mode and a Visual mode menu is used: Switch
2509 // to Visual mode temporarily. Append K_SELECT to switch
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002510 // back to Select mode.
2511 if (VIsual_active && VIsual_select
Bram Moolenaareda35f72019-08-03 14:59:44 +02002512 && (current_menu->modes & VISUAL))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002513 {
2514 VIsual_select = FALSE;
2515 (void)ins_typebuf(K_SELECT_STRING,
Bram Moolenaareda35f72019-08-03 14:59:44 +02002516 REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002517 }
2518 ins_typebuf(current_menu->strings[idx],
2519 current_menu->noremap[idx],
Bram Moolenaareda35f72019-08-03 14:59:44 +02002520 0, TRUE, current_menu->silent[idx]);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002521 }
2522 }
2523#endif // FEAT_GUI && FEAT_MENU
2524 *keylenp = keylen;
2525 return map_result_retry; // try mapping again
2526 }
2527
Bram Moolenaareda35f72019-08-03 14:59:44 +02002528 // Partial match: get some more characters. When a matching mapping
2529 // was found use that one.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002530 if (mp == NULL || keylen < 0)
2531 keylen = KEYLEN_PART_KEY;
2532 else
2533 keylen = mp_match_len;
2534 }
2535
2536 /*
2537 * complete match
2538 */
2539 if (keylen >= 0 && keylen <= typebuf.tb_len)
2540 {
2541 char_u *map_str;
2542
2543#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002544 int save_m_expr;
2545 int save_m_noremap;
2546 int save_m_silent;
2547 char_u *save_m_keys;
2548 char_u *save_m_str;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002549#else
2550# define save_m_noremap mp->m_noremap
2551# define save_m_silent mp->m_silent
2552#endif
2553
2554 // write chars to script file(s)
2555 if (keylen > typebuf.tb_maplen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002556 gotchars(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_maplen,
2557 keylen - typebuf.tb_maplen);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002558
2559 cmd_silent = (typebuf.tb_silent > 0);
2560 del_typebuf(keylen, 0); // remove the mapped keys
2561
2562 /*
2563 * Put the replacement string in front of mapstr.
2564 * The depth check catches ":map x y" and ":map y x".
2565 */
2566 if (++*mapdepth >= p_mmd)
2567 {
2568 emsg(_("E223: recursive mapping"));
2569 if (State & CMDLINE)
2570 redrawcmdline();
2571 else
2572 setcursor();
2573 flush_buffers(FLUSH_MINIMAL);
Bram Moolenaar30613902019-12-01 22:11:18 +01002574 *mapdepth = 0; // for next one
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002575 *keylenp = keylen;
2576 return map_result_fail;
2577 }
2578
2579 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002580 * In Select mode and a Visual mode mapping is used: Switch to Visual
2581 * mode temporarily. Append K_SELECT to switch back to Select mode.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002582 */
Bram Moolenaareda35f72019-08-03 14:59:44 +02002583 if (VIsual_active && VIsual_select && (mp->m_mode & VISUAL))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002584 {
2585 VIsual_select = FALSE;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002586 (void)ins_typebuf(K_SELECT_STRING, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002587 }
2588
2589#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002590 // Copy the values from *mp that are used, because evaluating the
2591 // expression may invoke a function that redefines the mapping, thereby
2592 // making *mp invalid.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002593 save_m_expr = mp->m_expr;
2594 save_m_noremap = mp->m_noremap;
2595 save_m_silent = mp->m_silent;
2596 save_m_keys = NULL; // only saved when needed
2597 save_m_str = NULL; // only saved when needed
2598
2599 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002600 * Handle ":map <expr>": evaluate the {rhs} as an expression. Also
2601 * save and restore the command line for "normal :".
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002602 */
2603 if (mp->m_expr)
2604 {
2605 int save_vgetc_busy = vgetc_busy;
2606 int save_may_garbage_collect = may_garbage_collect;
Bram Moolenaar4ebe0e62019-11-22 20:55:40 +01002607 int was_screen_col = screen_cur_col;
2608 int was_screen_row = screen_cur_row;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002609
2610 vgetc_busy = 0;
2611 may_garbage_collect = FALSE;
2612
2613 save_m_keys = vim_strsave(mp->m_keys);
2614 save_m_str = vim_strsave(mp->m_str);
2615 map_str = eval_map_expr(save_m_str, NUL);
2616
Bram Moolenaar4ebe0e62019-11-22 20:55:40 +01002617 // The mapping may do anything, but we expect it to take care of
2618 // redrawing. Do put the cursor back where it was.
2619 windgoto(was_screen_row, was_screen_col);
2620 out_flush();
2621
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002622 vgetc_busy = save_vgetc_busy;
2623 may_garbage_collect = save_may_garbage_collect;
2624 }
2625 else
2626#endif
2627 map_str = mp->m_str;
2628
2629 /*
2630 * Insert the 'to' part in the typebuf.tb_buf.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002631 * If 'from' field is the same as the start of the 'to' field, don't
2632 * remap the first character (but do allow abbreviations).
2633 * If m_noremap is set, don't remap the whole 'to' part.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002634 */
2635 if (map_str == NULL)
2636 i = FAIL;
2637 else
2638 {
2639 int noremap;
2640
2641 if (save_m_noremap != REMAP_YES)
2642 noremap = save_m_noremap;
2643 else if (
2644#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002645 STRNCMP(map_str, save_m_keys != NULL ? save_m_keys : mp->m_keys,
2646 (size_t)keylen)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002647#else
2648 STRNCMP(map_str, mp->m_keys, (size_t)keylen)
2649#endif
2650 != 0)
2651 noremap = REMAP_YES;
2652 else
2653 noremap = REMAP_SKIP;
2654 i = ins_typebuf(map_str, noremap,
2655 0, TRUE, cmd_silent || save_m_silent);
2656#ifdef FEAT_EVAL
2657 if (save_m_expr)
2658 vim_free(map_str);
2659#endif
2660 }
2661#ifdef FEAT_EVAL
2662 vim_free(save_m_keys);
2663 vim_free(save_m_str);
2664#endif
2665 *keylenp = keylen;
2666 if (i == FAIL)
2667 return map_result_fail;
2668 return map_result_retry;
2669 }
2670
2671 *keylenp = keylen;
2672 return map_result_nomatch;
2673}
2674
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002675/*
2676 * unget one character (can only be done once!)
2677 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002679vungetc(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680{
2681 old_char = c;
2682 old_mod_mask = mod_mask;
Bram Moolenaarb8978712013-03-16 21:42:16 +01002683 old_mouse_row = mouse_row;
2684 old_mouse_col = mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685}
2686
2687/*
Bram Moolenaar32526b32019-01-19 17:43:09 +01002688 * Get a byte:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 * 1. from the stuffbuffer
2690 * This is used for abbreviated commands like "D" -> "d$".
2691 * Also used to redo a command for ".".
2692 * 2. from the typeahead buffer
2693 * Stores text obtained previously but not used yet.
2694 * Also stores the result of mappings.
2695 * Also used for the ":normal" command.
2696 * 3. from the user
2697 * This may do a blocking wait if "advance" is TRUE.
2698 *
2699 * if "advance" is TRUE (vgetc()):
Bram Moolenaard29459b2016-08-26 22:29:11 +02002700 * Really get the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 * KeyTyped is set to TRUE in the case the user typed the key.
2702 * KeyStuffed is TRUE if the character comes from the stuff buffer.
2703 * if "advance" is FALSE (vpeekc()):
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02002704 * Just look whether there is a character available.
2705 * Return NUL if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 *
2707 * When "no_mapping" is zero, checks for mappings in the current mode.
2708 * Only returns one byte (of a multi-byte character).
2709 * K_SPECIAL and CSI may be escaped, need to get two more bytes then.
2710 */
2711 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002712vgetorpeek(int advance)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713{
2714 int c, c1;
Bram Moolenaar30613902019-12-01 22:11:18 +01002715 int timedout = FALSE; // waited for more than 1 second
2716 // for mapping to complete
2717 int mapdepth = 0; // check for recursive mapping
2718 int mode_deleted = FALSE; // set when mode has been deleted
Bram Moolenaar4e427192006-03-10 21:34:27 +00002719#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 int new_wcol, new_wrow;
2721#endif
2722#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01002723 int shape_changed = FALSE; // adjusted cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724#endif
2725 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 int old_wcol, old_wrow;
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00002727 int wait_tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728
2729 /*
2730 * This function doesn't work very well when called recursively. This may
2731 * happen though, because of:
2732 * 1. The call to add_to_showcmd(). char_avail() is then used to check if
2733 * there is a character available, which calls this function. In that
2734 * case we must return NUL, to indicate no character is available.
2735 * 2. A GUI callback function writes to the screen, causing a
2736 * wait_return().
2737 * Using ":normal" can also do this, but it saves the typeahead buffer,
2738 * thus it should be OK. But don't get a key from the user then.
2739 */
Bram Moolenaare2c38102016-01-31 14:55:40 +01002740 if (vgetc_busy > 0 && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 return NUL;
2742
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002743 ++vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744
2745 if (advance)
2746 KeyStuffed = FALSE;
2747
2748 init_typebuf();
2749 start_stuff();
2750 if (advance && typebuf.tb_maplen == 0)
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02002751 reg_executing = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 do
2753 {
2754/*
2755 * get a character: 1. from the stuffbuffer
2756 */
2757 if (typeahead_char != 0)
2758 {
2759 c = typeahead_char;
2760 if (advance)
2761 typeahead_char = 0;
2762 }
2763 else
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01002764 c = read_readbuffers(advance);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 if (c != NUL && !got_int)
2766 {
2767 if (advance)
2768 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002769 // KeyTyped = FALSE; When the command that stuffed something
2770 // was typed, behave like the stuffed command was typed.
2771 // needed for CTRL-W CTRL-] to open a fold, for example.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 KeyStuffed = TRUE;
2773 }
2774 if (typebuf.tb_no_abbr_cnt == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01002775 typebuf.tb_no_abbr_cnt = 1; // no abbreviations now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 }
2777 else
2778 {
2779 /*
2780 * Loop until we either find a matching mapped key, or we
2781 * are sure that it is not a mapped key.
2782 * If a mapped key sequence is found we go back to the start to
2783 * try re-mapping.
2784 */
2785 for (;;)
2786 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002787 long wait_time;
2788 int keylen = 0;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002789#ifdef FEAT_CMDL_INFO
2790 int showcmd_idx;
2791#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 /*
2793 * ui_breakcheck() is slow, don't use it too often when
2794 * inside a mapping. But call it each time for typed
2795 * characters.
2796 */
2797 if (typebuf.tb_maplen)
2798 line_breakcheck();
2799 else
Bram Moolenaar30613902019-12-01 22:11:18 +01002800 ui_breakcheck(); // check for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 if (got_int)
2802 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002803 // flush all input
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02002804 c = inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 0L);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002805
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 /*
2807 * If inchar() returns TRUE (script file was active) or we
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002808 * are inside a mapping, get out of Insert mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 * Otherwise we behave like having gotten a CTRL-C.
2810 * As a result typing CTRL-C in insert mode will
2811 * really insert a CTRL-C.
2812 */
2813 if ((c || typebuf.tb_maplen)
2814 && (State & (INSERT + CMDLINE)))
2815 c = ESC;
2816 else
2817 c = Ctrl_C;
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02002818 flush_buffers(FLUSH_INPUT); // flush all typeahead
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819
Bram Moolenaard9dfd572006-10-03 13:36:13 +00002820 if (advance)
2821 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002822 // Also record this character, it might be needed to
2823 // get out of Insert mode.
Bram Moolenaard9dfd572006-10-03 13:36:13 +00002824 *typebuf.tb_buf = c;
2825 gotchars(typebuf.tb_buf, 1);
2826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 cmd_silent = FALSE;
2828
2829 break;
2830 }
2831 else if (typebuf.tb_len > 0)
2832 {
2833 /*
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002834 * Check for a mapping in "typebuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 */
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002836 map_result_T result = handle_mapping(
2837 &keylen, &timedout, &mapdepth);
2838
2839 if (result == map_result_retry)
2840 // try mapping again
2841 continue;
2842 if (result == map_result_fail)
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02002843 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002844 // failed, use the outer loop
2845 c = -1;
2846 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002848 if (result == map_result_get)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850/*
2851 * get a character: 2. from the typeahead buffer
2852 */
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002853 c = typebuf.tb_buf[typebuf.tb_off];
Bram Moolenaar30613902019-12-01 22:11:18 +01002854 if (advance) // remove chars from tb_buf
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02002855 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002856 cmd_silent = (typebuf.tb_silent > 0);
2857 if (typebuf.tb_maplen > 0)
2858 KeyTyped = FALSE;
2859 else
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02002860 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002861 KeyTyped = TRUE;
Bram Moolenaar30613902019-12-01 22:11:18 +01002862 // write char to script file(s)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002863 gotchars(typebuf.tb_buf
2864 + typebuf.tb_off, 1);
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02002865 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002866 KeyNoremap = typebuf.tb_noremap[
2867 typebuf.tb_off];
2868 del_typebuf(1, 0);
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02002869 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002870 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 }
2872
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002873 // not enough characters, get more
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 }
2875
2876/*
2877 * get a character: 3. from the user - handle <Esc> in Insert mode
2878 */
2879 /*
Bram Moolenaarf085f422017-06-07 20:39:47 +02002880 * Special case: if we get an <ESC> in insert mode and there
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 * are no more characters at once, we pretend to go out of
2882 * insert mode. This prevents the one second delay after
2883 * typing an <ESC>. If we get something after all, we may
2884 * have to redisplay the mode. That the cursor is in the wrong
2885 * place does not matter.
2886 */
2887 c = 0;
2888#ifdef FEAT_CMDL_INFO
2889 new_wcol = curwin->w_wcol;
2890 new_wrow = curwin->w_wrow;
2891#endif
2892 if ( advance
2893 && typebuf.tb_len == 1
2894 && typebuf.tb_buf[typebuf.tb_off] == ESC
2895 && !no_mapping
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 && ex_normal_busy == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 && typebuf.tb_maplen == 0
2898 && (State & INSERT)
Bram Moolenaar946ffd42010-12-30 12:30:31 +01002899 && (p_timeout
2900 || (keylen == KEYLEN_PART_KEY && p_ttimeout))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 && (c = inchar(typebuf.tb_buf + typebuf.tb_off
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02002902 + typebuf.tb_len, 3, 25L)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 {
2904 colnr_T col = 0, vcol;
2905 char_u *ptr;
2906
Bram Moolenaar28c258f2006-01-25 22:02:51 +00002907 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 {
2909 unshowmode(TRUE);
2910 mode_deleted = TRUE;
2911 }
2912#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01002913 // may show a different cursor shape
Bram Moolenaarf085f422017-06-07 20:39:47 +02002914 if (gui.in_use && State != NORMAL && !cmd_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 {
2916 int save_State;
2917
2918 save_State = State;
2919 State = NORMAL;
2920 gui_update_cursor(TRUE, FALSE);
2921 State = save_State;
2922 shape_changed = TRUE;
2923 }
2924#endif
2925 validate_cursor();
2926 old_wcol = curwin->w_wcol;
2927 old_wrow = curwin->w_wrow;
2928
Bram Moolenaar30613902019-12-01 22:11:18 +01002929 // move cursor left, if possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 if (curwin->w_cursor.col != 0)
2931 {
2932 if (curwin->w_wcol > 0)
2933 {
2934 if (did_ai)
2935 {
2936 /*
2937 * We are expecting to truncate the trailing
2938 * white-space, so find the last non-white
2939 * character -- webb
2940 */
2941 col = vcol = curwin->w_wcol = 0;
2942 ptr = ml_get_curline();
2943 while (col < curwin->w_cursor.col)
2944 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002945 if (!VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 curwin->w_wcol = vcol;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002947 vcol += lbr_chartabsize(ptr, ptr + col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002950 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 ++col;
2953 }
2954 curwin->w_wrow = curwin->w_cline_row
Bram Moolenaar02631462017-09-22 15:20:32 +02002955 + curwin->w_wcol / curwin->w_width;
2956 curwin->w_wcol %= curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 curwin->w_wcol += curwin_col_off();
Bram Moolenaar30613902019-12-01 22:11:18 +01002958 col = 0; // no correction needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 }
2960 else
2961 {
2962 --curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 col = curwin->w_cursor.col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 }
2965 }
2966 else if (curwin->w_p_wrap && curwin->w_wrow)
2967 {
2968 --curwin->w_wrow;
Bram Moolenaar02631462017-09-22 15:20:32 +02002969 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 col = curwin->w_cursor.col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 if (has_mbyte && col > 0 && curwin->w_wcol > 0)
2973 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002974 // Correct when the cursor is on the right halve
2975 // of a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 ptr = ml_get_curline();
2977 col -= (*mb_head_off)(ptr, ptr + col);
2978 if ((*mb_ptr2cells)(ptr + col) > 1)
2979 --curwin->w_wcol;
2980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 }
2982 setcursor();
2983 out_flush();
2984#ifdef FEAT_CMDL_INFO
2985 new_wcol = curwin->w_wcol;
2986 new_wrow = curwin->w_wrow;
2987#endif
2988 curwin->w_wcol = old_wcol;
2989 curwin->w_wrow = old_wrow;
2990 }
2991 if (c < 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01002992 continue; // end of input script reached
Bram Moolenaar20c38922014-07-23 20:41:14 +02002993
Bram Moolenaar30613902019-12-01 22:11:18 +01002994 // Allow mapping for just typed characters. When we get here c
2995 // is the number of extra bytes and typebuf.tb_len is 1.
Bram Moolenaar20c38922014-07-23 20:41:14 +02002996 for (n = 1; n <= c; ++n)
2997 typebuf.tb_noremap[typebuf.tb_off + n] = RM_YES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 typebuf.tb_len += c;
2999
Bram Moolenaar30613902019-12-01 22:11:18 +01003000 // buffer full, don't map
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 if (typebuf.tb_len >= typebuf.tb_maplen + MAXMAPLEN)
3002 {
3003 timedout = TRUE;
3004 continue;
3005 }
3006
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 if (ex_normal_busy > 0)
3008 {
Bram Moolenaare2c38102016-01-31 14:55:40 +01003009#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 static int tc = 0;
Bram Moolenaare2c38102016-01-31 14:55:40 +01003011#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012
Bram Moolenaar30613902019-12-01 22:11:18 +01003013 // No typeahead left and inside ":normal". Must return
3014 // something to avoid getting stuck. When an incomplete
3015 // mapping is present, behave like it timed out.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 if (typebuf.tb_len > 0)
3017 {
3018 timedout = TRUE;
3019 continue;
3020 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003021 // When 'insertmode' is set, ESC just beeps in Insert
3022 // mode. Use CTRL-L to make edit() return.
3023 // For the command line only CTRL-C always breaks it.
3024 // For the cmdline window: Alternate between ESC and
3025 // CTRL-C: ESC for most situations and CTRL-C to close the
3026 // cmdline window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 if (p_im && (State & INSERT))
3028 c = Ctrl_L;
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003029#ifdef FEAT_TERMINAL
3030 else if (terminal_is_active())
3031 c = K_CANCEL;
3032#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 else if ((State & CMDLINE)
Bram Moolenaare2c38102016-01-31 14:55:40 +01003034#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 || (cmdwin_type > 0 && tc == ESC)
Bram Moolenaare2c38102016-01-31 14:55:40 +01003036#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 )
3038 c = Ctrl_C;
3039 else
3040 c = ESC;
Bram Moolenaare2c38102016-01-31 14:55:40 +01003041#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 tc = c;
Bram Moolenaare2c38102016-01-31 14:55:40 +01003043#endif
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01003044 // return from main_loop()
3045 if (pending_exmode_active)
3046 exmode_active = EXMODE_NORMAL;
3047
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 break;
3049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050
3051/*
3052 * get a character: 3. from the user - update display
3053 */
Bram Moolenaar30613902019-12-01 22:11:18 +01003054 // In insert mode a screen update is skipped when characters
3055 // are still available. But when those available characters
3056 // are part of a mapping, and we are going to do a blocking
3057 // wait here. Need to update the screen to display the
3058 // changed text so far. Also for when 'lazyredraw' is set and
3059 // redrawing was postponed because there was something in the
3060 // input buffer (e.g., termresponse).
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01003061 if (((State & INSERT) != 0 || p_lz) && (State & CMDLINE) == 0
3062 && advance && must_redraw != 0 && !need_wait_return)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 {
3064 update_screen(0);
Bram Moolenaar30613902019-12-01 22:11:18 +01003065 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 }
3067
3068 /*
3069 * If we have a partial match (and are going to wait for more
3070 * input from the user), show the partially matched characters
3071 * to the user with showcmd.
3072 */
3073#ifdef FEAT_CMDL_INFO
Bram Moolenaareda35f72019-08-03 14:59:44 +02003074 showcmd_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075#endif
3076 c1 = 0;
3077 if (typebuf.tb_len > 0 && advance && !exmode_active)
3078 {
3079 if (((State & (NORMAL | INSERT)) || State == LANGMAP)
3080 && State != HITRETURN)
3081 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003082 // this looks nice when typing a dead character map
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 if (State & INSERT
3084 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
3085 + typebuf.tb_len - 1) == 1)
3086 {
3087 edit_putchar(typebuf.tb_buf[typebuf.tb_off
3088 + typebuf.tb_len - 1], FALSE);
Bram Moolenaar30613902019-12-01 22:11:18 +01003089 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 c1 = 1;
3091 }
3092#ifdef FEAT_CMDL_INFO
Bram Moolenaar30613902019-12-01 22:11:18 +01003093 // need to use the col and row from above here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 old_wcol = curwin->w_wcol;
3095 old_wrow = curwin->w_wrow;
3096 curwin->w_wcol = new_wcol;
3097 curwin->w_wrow = new_wrow;
3098 push_showcmd();
3099 if (typebuf.tb_len > SHOWCMD_COLS)
Bram Moolenaareda35f72019-08-03 14:59:44 +02003100 showcmd_idx = typebuf.tb_len - SHOWCMD_COLS;
3101 while (showcmd_idx < typebuf.tb_len)
3102 (void)add_to_showcmd(
3103 typebuf.tb_buf[typebuf.tb_off + showcmd_idx++]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 curwin->w_wcol = old_wcol;
3105 curwin->w_wrow = old_wrow;
3106#endif
3107 }
3108
Bram Moolenaar30613902019-12-01 22:11:18 +01003109 // this looks nice when typing a dead character map
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 if ((State & CMDLINE)
3111#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3112 && cmdline_star == 0
3113#endif
3114 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
3115 + typebuf.tb_len - 1) == 1)
3116 {
3117 putcmdline(typebuf.tb_buf[typebuf.tb_off
3118 + typebuf.tb_len - 1], FALSE);
3119 c1 = 1;
3120 }
3121 }
3122
3123/*
3124 * get a character: 3. from the user - get it
3125 */
Bram Moolenaar83f4cbd2018-06-12 21:35:40 +02003126 if (typebuf.tb_len == 0)
3127 // timedout may have been set while waiting for a mapping
3128 // that has a <Nop> RHS.
3129 timedout = FALSE;
3130
Bram Moolenaar652de232019-04-04 20:13:09 +02003131 if (advance)
3132 {
3133 if (typebuf.tb_len == 0
3134 || !(p_timeout
3135 || (p_ttimeout && keylen == KEYLEN_PART_KEY)))
3136 // blocking wait
3137 wait_time = -1L;
3138 else if (keylen == KEYLEN_PART_KEY && p_ttm >= 0)
3139 wait_time = p_ttm;
3140 else
3141 wait_time = p_tm;
3142 }
3143 else
3144 wait_time = 0;
3145
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003146 wait_tb_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 c = inchar(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len,
3148 typebuf.tb_buflen - typebuf.tb_off - typebuf.tb_len - 1,
Bram Moolenaar652de232019-04-04 20:13:09 +02003149 wait_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150
3151#ifdef FEAT_CMDL_INFO
Bram Moolenaareda35f72019-08-03 14:59:44 +02003152 if (showcmd_idx != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 pop_showcmd();
3154#endif
3155 if (c1 == 1)
3156 {
3157 if (State & INSERT)
3158 edit_unputchar();
3159 if (State & CMDLINE)
3160 unputcmdline();
Bram Moolenaarbc256d92012-06-06 12:06:15 +02003161 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003162 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 }
3164
3165 if (c < 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003166 continue; // end of input script reached
3167 if (c == NUL) // no character available
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 {
3169 if (!advance)
3170 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01003171 if (wait_tb_len > 0) // timed out
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 {
3173 timedout = TRUE;
3174 continue;
3175 }
3176 }
3177 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003178 { // allow mapping for just typed characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 while (typebuf.tb_buf[typebuf.tb_off
3180 + typebuf.tb_len] != NUL)
3181 typebuf.tb_noremap[typebuf.tb_off
3182 + typebuf.tb_len++] = RM_YES;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003183#ifdef HAVE_INPUT_METHOD
Bram Moolenaar30613902019-12-01 22:11:18 +01003184 // Get IM status right after getting keys, not after the
3185 // timeout for a mapping (focus may be lost by then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 vgetc_im_active = im_get_status();
3187#endif
3188 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003189 } // for (;;)
3190 } // if (!character from stuffbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191
Bram Moolenaar30613902019-12-01 22:11:18 +01003192 // if advance is FALSE don't loop on NULs
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003193 } while ((c < 0 && c != K_CANCEL) || (advance && c == NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194
3195 /*
3196 * The "INSERT" message is taken care of here:
3197 * if we return an ESC to exit insert mode, the message is deleted
3198 * if we don't return an ESC but deleted the message before, redisplay it
3199 */
Bram Moolenaar09df3122006-01-23 22:23:09 +00003200 if (advance && p_smd && msg_silent == 0 && (State & INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003202 if (c == ESC && !mode_deleted && !no_mapping && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 {
3204 if (typebuf.tb_len && !KeyTyped)
Bram Moolenaar30613902019-12-01 22:11:18 +01003205 redraw_cmdline = TRUE; // delete mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 else
3207 unshowmode(FALSE);
3208 }
3209 else if (c != ESC && mode_deleted)
3210 {
3211 if (typebuf.tb_len && !KeyTyped)
Bram Moolenaar30613902019-12-01 22:11:18 +01003212 redraw_cmdline = TRUE; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 else
3214 showmode();
3215 }
3216 }
3217#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003218 // may unshow different cursor shape
Bram Moolenaarf085f422017-06-07 20:39:47 +02003219 if (gui.in_use && shape_changed)
3220 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221#endif
Bram Moolenaar6edbbd82019-03-10 09:41:51 +01003222 if (timedout && c == ESC)
3223 {
3224 char_u nop_buf[3];
3225
3226 // When recording there will be no timeout. Add a <Nop> after the ESC
3227 // to avoid that it forms a key code with following characters.
3228 nop_buf[0] = K_SPECIAL;
3229 nop_buf[1] = KS_EXTRA;
3230 nop_buf[2] = KE_NOP;
3231 gotchars(nop_buf, 3);
3232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003234 --vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235
3236 return c;
3237}
3238
3239/*
3240 * inchar() - get one character from
3241 * 1. a scriptfile
3242 * 2. the keyboard
3243 *
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003244 * As many characters as we can get (up to 'maxlen') are put in "buf" and
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 * NUL terminated (buffer length must be 'maxlen' + 1).
3246 * Minimum for "maxlen" is 3!!!!
3247 *
3248 * "tb_change_cnt" is the value of typebuf.tb_change_cnt if "buf" points into
3249 * it. When typebuf.tb_change_cnt changes (e.g., when a message is received
3250 * from a remote client) "buf" can no longer be used. "tb_change_cnt" is 0
3251 * otherwise.
3252 *
3253 * If we got an interrupt all input is read until none is available.
3254 *
3255 * If wait_time == 0 there is no waiting for the char.
3256 * If wait_time == n we wait for n msec for a character to arrive.
3257 * If wait_time == -1 we wait forever for a character to arrive.
3258 *
3259 * Return the number of obtained characters.
3260 * Return -1 when end of input script reached.
3261 */
Bram Moolenaarcda77642016-06-04 13:32:35 +02003262 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003263inchar(
3264 char_u *buf,
3265 int maxlen,
Bram Moolenaar30613902019-12-01 22:11:18 +01003266 long wait_time) // milli seconds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267{
Bram Moolenaar30613902019-12-01 22:11:18 +01003268 int len = 0; // init for GCC
3269 int retesc = FALSE; // return ESC with gotint
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 int script_char;
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003271 int tb_change_cnt = typebuf.tb_change_cnt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272
Bram Moolenaar30613902019-12-01 22:11:18 +01003273 if (wait_time == -1L || wait_time > 100L) // flush output before waiting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 {
3275 cursor_on();
Bram Moolenaara338adc2018-01-31 20:51:47 +01003276 out_flush_cursor(FALSE, FALSE);
3277#if defined(FEAT_GUI) && defined(FEAT_MOUSESHAPE)
3278 if (gui.in_use && postponed_mouseshape)
3279 update_mouseshape(-1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280#endif
3281 }
3282
3283 /*
3284 * Don't reset these when at the hit-return prompt, otherwise a endless
3285 * recursive loop may result (write error in swapfile, hit-return, timeout
3286 * on char wait, flush swapfile, write error....).
3287 */
3288 if (State != HITRETURN)
3289 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003290 did_outofmem_msg = FALSE; // display out of memory message (again)
3291 did_swapwrite_msg = FALSE; // display swap file write error again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003293 undo_off = FALSE; // restart undo now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294
3295 /*
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00003296 * Get a character from a script file if there is one.
3297 * If interrupted: Stop reading script files, close them all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 */
3299 script_char = -1;
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00003300 while (scriptin[curscript] != NULL && script_char < 0
3301#ifdef FEAT_EVAL
3302 && !ignore_script
3303#endif
3304 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02003306#ifdef MESSAGE_QUEUE
3307 parse_queued_messages();
Bram Moolenaarf2330482008-06-24 20:19:36 +00003308#endif
3309
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 if (got_int || (script_char = getc(scriptin[curscript])) < 0)
3311 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003312 // Reached EOF.
3313 // Careful: closescript() frees typebuf.tb_buf[] and buf[] may
3314 // point inside typebuf.tb_buf[]. Don't use buf[] after this!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 closescript();
3316 /*
3317 * When reading script file is interrupted, return an ESC to get
3318 * back to normal mode.
3319 * Otherwise return -1, because typebuf.tb_buf[] has changed.
3320 */
3321 if (got_int)
3322 retesc = TRUE;
3323 else
3324 return -1;
3325 }
3326 else
3327 {
3328 buf[0] = script_char;
3329 len = 1;
3330 }
3331 }
3332
Bram Moolenaar30613902019-12-01 22:11:18 +01003333 if (script_char < 0) // did not get a character from script
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 {
3335 /*
3336 * If we got an interrupt, skip all previously typed characters and
3337 * return TRUE if quit reading script file.
3338 * Stop reading typeahead when a single CTRL-C was read,
3339 * fill_input_buf() returns this when not able to read from stdin.
3340 * Don't use buf[] here, closescript() may have freed typebuf.tb_buf[]
3341 * and buf may be pointing inside typebuf.tb_buf[].
3342 */
3343 if (got_int)
3344 {
3345#define DUM_LEN MAXMAPLEN * 3 + 3
3346 char_u dum[DUM_LEN + 1];
3347
3348 for (;;)
3349 {
3350 len = ui_inchar(dum, DUM_LEN, 0L, 0);
3351 if (len == 0 || (len == 1 && dum[0] == 3))
3352 break;
3353 }
3354 return retesc;
3355 }
3356
3357 /*
3358 * Always flush the output characters when getting input characters
Bram Moolenaarcb574f42019-01-25 22:29:57 +01003359 * from the user and not just peeking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 */
Bram Moolenaarcb574f42019-01-25 22:29:57 +01003361 if (wait_time == -1L || wait_time > 10L)
3362 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363
3364 /*
3365 * Fill up to a third of the buffer, because each character may be
3366 * tripled below.
3367 */
3368 len = ui_inchar(buf, maxlen / 3, wait_time, tb_change_cnt);
3369 }
3370
Bram Moolenaar30613902019-12-01 22:11:18 +01003371 // If the typebuf was changed further down, it is like nothing was added by
3372 // this call.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 if (typebuf_changed(tb_change_cnt))
3374 return 0;
3375
Bram Moolenaar30613902019-12-01 22:11:18 +01003376 // Note the change in the typeahead buffer, this matters for when
3377 // vgetorpeek() is called recursively, e.g. using getchar(1) in a timer
3378 // function.
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003379 if (len > 0 && ++typebuf.tb_change_cnt == 0)
3380 typebuf.tb_change_cnt = 1;
3381
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003382 return fix_input_buffer(buf, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383}
3384
3385/*
3386 * Fix typed characters for use by vgetc() and check_termcode().
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003387 * "buf[]" must have room to triple the number of bytes!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 * Returns the new length.
3389 */
3390 int
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003391fix_input_buffer(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392{
3393 int i;
3394 char_u *p = buf;
3395
3396 /*
3397 * Two characters are special: NUL and K_SPECIAL.
3398 * When compiled With the GUI CSI is also special.
3399 * Replace NUL by K_SPECIAL KS_ZERO KE_FILLER
3400 * Replace K_SPECIAL by K_SPECIAL KS_SPECIAL KE_FILLER
3401 * Replace CSI by K_SPECIAL KS_EXTRA KE_CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 */
3403 for (i = len; --i >= 0; ++p)
3404 {
3405#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003406 // When the GUI is used any character can come after a CSI, don't
3407 // escape it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 if (gui.in_use && p[0] == CSI && i >= 2)
3409 {
3410 p += 2;
3411 i -= 2;
3412 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003413# ifndef MSWIN
Bram Moolenaar30613902019-12-01 22:11:18 +01003414 // When the GUI is not used CSI needs to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 else if (!gui.in_use && p[0] == CSI)
3416 {
3417 mch_memmove(p + 3, p + 1, (size_t)i);
3418 *p++ = K_SPECIAL;
3419 *p++ = KS_EXTRA;
3420 *p = (int)KE_CSI;
3421 len += 2;
3422 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003423# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 else
3425#endif
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003426 if (p[0] == NUL || (p[0] == K_SPECIAL
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003427 // timeout may generate K_CURSORHOLD
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003428 && (i < 2 || p[1] != KS_EXTRA || p[2] != (int)KE_CURSORHOLD)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003429#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003430 // Win32 console passes modifiers
3431 && (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003432# ifdef VIMDLL
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003433 gui.in_use ||
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003434# endif
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003435 (i < 2 || p[1] != KS_MODIFIER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436#endif
3437 ))
3438 {
3439 mch_memmove(p + 3, p + 1, (size_t)i);
3440 p[2] = K_THIRD(p[0]);
3441 p[1] = K_SECOND(p[0]);
3442 p[0] = K_SPECIAL;
3443 p += 2;
3444 len += 2;
3445 }
3446 }
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003447 *p = NUL; // add trailing NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 return len;
3449}
3450
3451#if defined(USE_INPUT_BUF) || defined(PROTO)
3452/*
3453 * Return TRUE when bytes are in the input buffer or in the typeahead buffer.
3454 * Normally the input buffer would be sufficient, but the server_to_input_buf()
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003455 * or feedkeys() may insert characters in the typeahead buffer while we are
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003456 * waiting for input to arrive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 */
3458 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003459input_available(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460{
3461 return (!vim_is_input_buf_empty()
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003462# if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
3463 || typebuf_was_filled
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464# endif
3465 );
3466}
3467#endif