blob: 7df4bcebc9ba0656770aa28e050cd9c7957ecfb8 [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 Moolenaar8f027fe2020-03-04 23:21:35 +01001626 || (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 Moolenaar8f027fe2020-03-04 23:21:35 +01001681 // Handle focus event here, so that the caller doesn't need to
1682 // know about it. Return K_IGNORE so that we loop once (needed
1683 // if 'lazyredraw' is set).
1684 if (c == K_FOCUSGAINED || c == K_FOCUSLOST)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001685 {
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001686 ui_focus_change(c == K_FOCUSGAINED);
1687 c = K_IGNORE;
Bram Moolenaara9dd2d32019-04-29 21:58:41 +02001688 }
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001689
1690 // Translate K_CSI to CSI. The special key is only used to
1691 // avoid it being recognized as the start of a special key.
1692 if (c == K_CSI)
1693 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001695 }
1696 // a keypad or special function key was not mapped, use it like
1697 // its ASCII equivalent
1698 switch (c)
1699 {
1700 case K_KPLUS: c = '+'; break;
1701 case K_KMINUS: c = '-'; break;
1702 case K_KDIVIDE: c = '/'; break;
1703 case K_KMULTIPLY: c = '*'; break;
1704 case K_KENTER: c = CAR; break;
1705 case K_KPOINT:
Bram Moolenaar4f974752019-02-17 17:44:42 +01001706#ifdef MSWIN
Bram Moolenaar8e85db02018-07-27 23:16:51 +02001707 // Can be either '.' or a ',',
1708 // depending on the type of keypad.
1709 c = MapVirtualKey(VK_DECIMAL, 2); break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001710#else
Bram Moolenaar8e85db02018-07-27 23:16:51 +02001711 c = '.'; break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001712#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001713 case K_K0: c = '0'; break;
1714 case K_K1: c = '1'; break;
1715 case K_K2: c = '2'; break;
1716 case K_K3: c = '3'; break;
1717 case K_K4: c = '4'; break;
1718 case K_K5: c = '5'; break;
1719 case K_K6: c = '6'; break;
1720 case K_K7: c = '7'; break;
1721 case K_K8: c = '8'; break;
1722 case K_K9: c = '9'; break;
Bram Moolenaara88d9682005-03-25 21:45:43 +00001723
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001724 case K_XHOME:
1725 case K_ZHOME: if (mod_mask == MOD_MASK_SHIFT)
Bram Moolenaara88d9682005-03-25 21:45:43 +00001726 {
1727 c = K_S_HOME;
1728 mod_mask = 0;
1729 }
1730 else if (mod_mask == MOD_MASK_CTRL)
1731 {
1732 c = K_C_HOME;
1733 mod_mask = 0;
1734 }
1735 else
1736 c = K_HOME;
1737 break;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001738 case K_XEND:
1739 case K_ZEND: if (mod_mask == MOD_MASK_SHIFT)
Bram Moolenaara88d9682005-03-25 21:45:43 +00001740 {
1741 c = K_S_END;
1742 mod_mask = 0;
1743 }
1744 else if (mod_mask == MOD_MASK_CTRL)
1745 {
1746 c = K_C_END;
1747 mod_mask = 0;
1748 }
1749 else
1750 c = K_END;
1751 break;
1752
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001753 case K_XUP: c = K_UP; break;
1754 case K_XDOWN: c = K_DOWN; break;
1755 case K_XLEFT: c = K_LEFT; break;
1756 case K_XRIGHT: c = K_RIGHT; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001759 // For a multi-byte character get all the bytes and return the
1760 // converted character.
1761 // Note: This will loop until enough bytes are received!
1762 if (has_mbyte && (n = MB_BYTE2LEN_CHECK(c)) > 1)
1763 {
1764 ++no_mapping;
1765 buf[0] = c;
1766 for (i = 1; i < n; ++i)
1767 {
1768 buf[i] = vgetorpeek(TRUE);
1769 if (buf[i] == K_SPECIAL
1770#ifdef FEAT_GUI
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001771 || (buf[i] == CSI)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001772#endif
1773 )
1774 {
1775 // Must be a K_SPECIAL - KS_SPECIAL - KE_FILLER
1776 // sequence, which represents a K_SPECIAL (0x80),
1777 // or a CSI - KS_EXTRA - KE_CSI sequence, which
1778 // represents a CSI (0x9B),
1779 // or a K_SPECIAL - KS_EXTRA - KE_CSI, which is CSI
1780 // too.
1781 c = vgetorpeek(TRUE);
1782 if (vgetorpeek(TRUE) == (int)KE_CSI && c == KS_EXTRA)
1783 buf[i] = CSI;
1784 }
1785 }
1786 --no_mapping;
1787 c = (*mb_ptr2char)(buf);
1788 }
1789
Bram Moolenaar00eab7f2019-10-10 21:49:28 +02001790 if (!no_reduce_keys)
Bram Moolenaar00eab7f2019-10-10 21:49:28 +02001791 // A modifier was not used for a mapping, apply it to ASCII
Bram Moolenaar459fd782019-10-13 16:43:39 +02001792 // keys. Shift would already have been applied.
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001793 c = merge_modifyOtherKeys(c);
Bram Moolenaar00eab7f2019-10-10 21:49:28 +02001794
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001795 break;
1796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001798
1799#ifdef FEAT_EVAL
1800 /*
1801 * In the main loop "may_garbage_collect" can be set to do garbage
1802 * collection in the first next vgetc(). It's disabled after that to
1803 * avoid internally used Lists and Dicts to be freed.
1804 */
1805 may_garbage_collect = FALSE;
1806#endif
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001807
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001808#ifdef FEAT_BEVAL_TERM
Bram Moolenaarc2f50542019-07-05 23:24:56 +02001809 if (c != K_MOUSEMOVE && c != K_IGNORE && c != K_CURSORHOLD)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001810 {
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001811 // Don't trigger 'balloonexpr' unless only the mouse was moved.
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001812 bevalexpr_due_set = FALSE;
1813 ui_remove_balloon();
1814 }
1815#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001816#ifdef FEAT_PROP_POPUP
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001817 if (popup_do_filter(c))
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02001818 {
1819 if (c == Ctrl_C)
1820 got_int = FALSE; // avoid looping
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001821 c = K_IGNORE;
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02001822 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001823#endif
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001824
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001825 // Need to process the character before we know it's safe to do something
1826 // else.
1827 if (c != K_IGNORE)
Bram Moolenaard103ee72019-09-18 21:15:31 +02001828 state_no_longer_safe("key typed");
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001829
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001830 return c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831}
1832
1833/*
1834 * Like vgetc(), but never return a NUL when called recursively, get a key
1835 * directly from the user (ignoring typeahead).
1836 */
1837 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001838safe_vgetc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839{
1840 int c;
1841
1842 c = vgetc();
1843 if (c == NUL)
1844 c = get_keystroke();
1845 return c;
1846}
1847
1848/*
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001849 * Like safe_vgetc(), but loop to handle K_IGNORE.
1850 * Also ignore scrollbar events.
1851 */
1852 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001853plain_vgetc(void)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001854{
1855 int c;
1856
1857 do
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001858 c = safe_vgetc();
Bram Moolenaarabab0b02019-03-30 18:47:01 +01001859 while (c == K_IGNORE || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR);
Bram Moolenaarec2da362017-01-21 20:04:22 +01001860
1861 if (c == K_PS)
Bram Moolenaar30613902019-12-01 22:11:18 +01001862 // Only handle the first pasted character. Drop the rest, since we
1863 // don't know what to do with it.
Bram Moolenaarec2da362017-01-21 20:04:22 +01001864 c = bracketed_paste(PASTE_ONE_CHAR, FALSE, NULL);
1865
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001866 return c;
1867}
1868
1869/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 * Check if a character is available, such that vgetc() will not block.
1871 * If the next character is a special character or multi-byte, the returned
1872 * character is not valid!.
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02001873 * Returns NUL if no character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 */
1875 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001876vpeekc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877{
1878 if (old_char != -1)
1879 return old_char;
1880 return vgetorpeek(FALSE);
1881}
1882
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01001883#if defined(FEAT_TERMRESPONSE) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884/*
1885 * Like vpeekc(), but don't allow mapping. Do allow checking for terminal
1886 * codes.
1887 */
1888 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001889vpeekc_nomap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890{
1891 int c;
1892
1893 ++no_mapping;
1894 ++allow_keys;
1895 c = vpeekc();
1896 --no_mapping;
1897 --allow_keys;
1898 return c;
1899}
1900#endif
1901
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902/*
1903 * Check if any character is available, also half an escape sequence.
1904 * Trick: when no typeahead found, but there is something in the typeahead
1905 * buffer, it must be an ESC that is recognized as the start of a key code.
1906 */
1907 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001908vpeekc_any(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909{
1910 int c;
1911
1912 c = vpeekc();
1913 if (c == NUL && typebuf.tb_len > 0)
1914 c = ESC;
1915 return c;
1916}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917
1918/*
1919 * Call vpeekc() without causing anything to be mapped.
1920 * Return TRUE if a character is available, FALSE otherwise.
1921 */
1922 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001923char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924{
1925 int retval;
1926
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01001927#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01001928 // When test_override("char_avail", 1) was called pretend there is no
1929 // typeahead.
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01001930 if (disable_char_avail_for_testing)
1931 return FALSE;
1932#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 ++no_mapping;
1934 retval = vpeekc();
1935 --no_mapping;
1936 return (retval != NUL);
1937}
1938
Bram Moolenaar9c658c92019-09-15 21:00:54 +02001939#if defined(FEAT_EVAL) || defined(PROTO)
1940/*
1941 * "getchar()" function
1942 */
1943 void
1944f_getchar(typval_T *argvars, typval_T *rettv)
1945{
1946 varnumber_T n;
1947 int error = FALSE;
1948
1949#ifdef MESSAGE_QUEUE
1950 // vpeekc() used to check for messages, but that caused problems, invoking
1951 // a callback where it was not expected. Some plugins use getchar(1) in a
1952 // loop to await a message, therefore make sure we check for messages here.
1953 parse_queued_messages();
1954#endif
1955
Bram Moolenaar30613902019-12-01 22:11:18 +01001956 // Position the cursor. Needed after a message that ends in a space.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02001957 windgoto(msg_row, msg_col);
1958
1959 ++no_mapping;
1960 ++allow_keys;
1961 for (;;)
1962 {
1963 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar30613902019-12-01 22:11:18 +01001964 // getchar(): blocking wait.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02001965 n = plain_vgetc();
1966 else if (tv_get_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar30613902019-12-01 22:11:18 +01001967 // getchar(1): only check if char avail
Bram Moolenaar9c658c92019-09-15 21:00:54 +02001968 n = vpeekc_any();
1969 else if (error || vpeekc_any() == NUL)
Bram Moolenaar30613902019-12-01 22:11:18 +01001970 // illegal argument or getchar(0) and no char avail: return zero
Bram Moolenaar9c658c92019-09-15 21:00:54 +02001971 n = 0;
1972 else
Bram Moolenaar30613902019-12-01 22:11:18 +01001973 // getchar(0) and char avail: return char
Bram Moolenaar9c658c92019-09-15 21:00:54 +02001974 n = plain_vgetc();
1975
1976 if (n == K_IGNORE)
1977 continue;
1978 break;
1979 }
1980 --no_mapping;
1981 --allow_keys;
1982
1983 set_vim_var_nr(VV_MOUSE_WIN, 0);
1984 set_vim_var_nr(VV_MOUSE_WINID, 0);
1985 set_vim_var_nr(VV_MOUSE_LNUM, 0);
1986 set_vim_var_nr(VV_MOUSE_COL, 0);
1987
1988 rettv->vval.v_number = n;
1989 if (IS_SPECIAL(n) || mod_mask != 0)
1990 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001991 char_u temp[10]; // modifier: 3, mbyte-char: 6, NUL: 1
Bram Moolenaar9c658c92019-09-15 21:00:54 +02001992 int i = 0;
1993
Bram Moolenaar30613902019-12-01 22:11:18 +01001994 // Turn a special key into three bytes, plus modifier.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02001995 if (mod_mask != 0)
1996 {
1997 temp[i++] = K_SPECIAL;
1998 temp[i++] = KS_MODIFIER;
1999 temp[i++] = mod_mask;
2000 }
2001 if (IS_SPECIAL(n))
2002 {
2003 temp[i++] = K_SPECIAL;
2004 temp[i++] = K_SECOND(n);
2005 temp[i++] = K_THIRD(n);
2006 }
2007 else if (has_mbyte)
2008 i += (*mb_char2bytes)(n, temp + i);
2009 else
2010 temp[i++] = n;
2011 temp[i++] = NUL;
2012 rettv->v_type = VAR_STRING;
2013 rettv->vval.v_string = vim_strsave(temp);
2014
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002015 if (is_mouse_key(n))
2016 {
2017 int row = mouse_row;
2018 int col = mouse_col;
2019 win_T *win;
2020 linenr_T lnum;
2021 win_T *wp;
2022 int winnr = 1;
2023
2024 if (row >= 0 && col >= 0)
2025 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002026 // Find the window at the mouse coordinates and compute the
2027 // text position.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002028 win = mouse_find_win(&row, &col, FIND_POPUP);
2029 if (win == NULL)
2030 return;
2031 (void)mouse_comp_pos(win, &row, &col, &lnum, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002032#ifdef FEAT_PROP_POPUP
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002033 if (WIN_IS_POPUP(win))
2034 winnr = 0;
2035 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002036#endif
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002037 for (wp = firstwin; wp != win && wp != NULL;
2038 wp = wp->w_next)
2039 ++winnr;
2040 set_vim_var_nr(VV_MOUSE_WIN, winnr);
2041 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
2042 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
2043 set_vim_var_nr(VV_MOUSE_COL, col + 1);
2044 }
2045 }
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002046 }
2047}
2048
2049/*
2050 * "getcharmod()" function
2051 */
2052 void
2053f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
2054{
2055 rettv->vval.v_number = mod_mask;
2056}
2057#endif // FEAT_EVAL
2058
2059#if defined(MESSAGE_QUEUE) || defined(PROTO)
2060# define MAX_REPEAT_PARSE 8
2061
2062/*
2063 * Process messages that have been queued for netbeans or clientserver.
2064 * Also check if any jobs have ended.
2065 * These functions can call arbitrary vimscript and should only be called when
2066 * it is safe to do so.
2067 */
2068 void
2069parse_queued_messages(void)
2070{
Bram Moolenaar1cac7092019-10-22 21:54:31 +02002071 int old_curwin_id;
2072 int old_curbuf_fnum;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002073 int i;
2074 int save_may_garbage_collect = may_garbage_collect;
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002075 static int entered = 0;
Bram Moolenaard103ee72019-09-18 21:15:31 +02002076 int was_safe = get_was_safe_state();
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002077
2078 // Do not handle messages while redrawing, because it may cause buffers to
2079 // change or be wiped while they are being redrawn.
2080 if (updating_screen)
2081 return;
2082
Bram Moolenaar1cac7092019-10-22 21:54:31 +02002083 // If memory allocation fails during startup we'll exit but curbuf or
2084 // curwin could be NULL.
2085 if (curbuf == NULL || curwin == NULL)
2086 return;
2087
2088 old_curbuf_fnum = curbuf->b_fnum;
2089 old_curwin_id = curwin->w_id;
2090
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002091 ++entered;
2092
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002093 // may_garbage_collect is set in main_loop() to do garbage collection when
2094 // blocking to wait on a character. We don't want that while parsing
2095 // messages, a callback may invoke vgetc() while lists and dicts are in use
2096 // in the call stack.
2097 may_garbage_collect = FALSE;
2098
2099 // Loop when a job ended, but don't keep looping forever.
2100 for (i = 0; i < MAX_REPEAT_PARSE; ++i)
2101 {
2102 // For Win32 mch_breakcheck() does not check for input, do it here.
2103# if defined(MSWIN) && defined(FEAT_JOB_CHANNEL)
2104 channel_handle_events(FALSE);
2105# endif
2106
2107# ifdef FEAT_NETBEANS_INTG
2108 // Process the queued netbeans messages.
2109 netbeans_parse_messages();
2110# endif
2111# ifdef FEAT_JOB_CHANNEL
2112 // Write any buffer lines still to be written.
2113 channel_write_any_lines();
2114
2115 // Process the messages queued on channels.
2116 channel_parse_messages();
2117# endif
2118# if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
2119 // Process the queued clientserver messages.
2120 server_parse_messages();
2121# endif
2122# ifdef FEAT_JOB_CHANNEL
2123 // Check if any jobs have ended. If so, repeat the above to handle
2124 // changes, e.g. stdin may have been closed.
2125 if (job_check_ended())
2126 continue;
2127# endif
2128# ifdef FEAT_TERMINAL
2129 free_unused_terminals();
2130# endif
2131# ifdef FEAT_SOUND_CANBERRA
2132 if (has_sound_callback_in_queue())
2133 invoke_sound_callback();
2134# endif
2135 break;
2136 }
2137
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002138 // When not nested we'll go back to waiting for a typed character. If it
2139 // was safe before then this triggers a SafeStateAgain autocommand event.
Bram Moolenaard103ee72019-09-18 21:15:31 +02002140 if (entered == 1 && was_safe)
Bram Moolenaar37d18072019-09-17 20:28:38 +02002141 may_trigger_safestateagain();
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002142
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002143 may_garbage_collect = save_may_garbage_collect;
2144
2145 // If the current window or buffer changed we need to bail out of the
2146 // waiting loop. E.g. when a job exit callback closes the terminal window.
2147 if (curwin->w_id != old_curwin_id || curbuf->b_fnum != old_curbuf_fnum)
2148 ins_char_typebuf(K_IGNORE);
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002149
2150 --entered;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002151}
2152#endif
2153
2154
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002155typedef enum {
2156 map_result_fail, // failed, break loop
2157 map_result_get, // get a character from typeahead
2158 map_result_retry, // try to map again
2159 map_result_nomatch // no matching mapping, get char
2160} map_result_T;
2161
2162/*
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002163 * Check if the bytes at the start of the typeahead buffer are a character used
2164 * in CTRL-X mode. This includes the form with a CTRL modifier.
2165 */
2166 static int
2167at_ctrl_x_key(void)
2168{
2169 char_u *p = typebuf.tb_buf + typebuf.tb_off;
2170 int c = *p;
2171
2172 if (typebuf.tb_len > 3
2173 && c == K_SPECIAL
2174 && p[1] == KS_MODIFIER
2175 && (p[2] & MOD_MASK_CTRL))
2176 c = p[3] & 0x1f;
2177 return vim_is_ctrl_x_key(c);
2178}
2179
2180/*
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002181 * Handle mappings in the typeahead buffer.
2182 * - When something was mapped, return map_result_retry for recursive mappings.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002183 * - When nothing mapped and typeahead has a character: return map_result_get.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002184 * - When there is no match yet, return map_result_nomatch, need to get more
2185 * typeahead.
2186 */
2187 static int
2188handle_mapping(
2189 int *keylenp,
2190 int *timedout,
2191 int *mapdepth)
2192{
2193 mapblock_T *mp = NULL;
2194 mapblock_T *mp2;
2195 mapblock_T *mp_match;
2196 int mp_match_len = 0;
2197 int max_mlen = 0;
2198 int tb_c1;
2199 int mlen;
2200#ifdef FEAT_LANGMAP
2201 int nolmaplen;
2202#endif
2203 int keylen = *keylenp;
2204 int i;
2205 int local_State = get_real_state();
2206
2207 /*
2208 * Check for a mappable key sequence.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002209 * Walk through one maphash[] list until we find an entry that matches.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002210 *
2211 * Don't look for mappings if:
2212 * - no_mapping set: mapping disabled (e.g. for CTRL-V)
2213 * - maphash_valid not set: no mappings present.
2214 * - typebuf.tb_buf[typebuf.tb_off] should not be remapped
2215 * - in insert or cmdline mode and 'paste' option set
Bram Moolenaareda35f72019-08-03 14:59:44 +02002216 * - waiting for "hit return to continue" and CR or SPACE typed
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002217 * - waiting for a char with --more--
2218 * - in Ctrl-X mode, and we get a valid char for that mode
2219 */
2220 tb_c1 = typebuf.tb_buf[typebuf.tb_off];
2221 if (no_mapping == 0 && is_maphash_valid()
2222 && (no_zero_mapping == 0 || tb_c1 != '0')
2223 && (typebuf.tb_maplen == 0
2224 || (p_remap
2225 && (typebuf.tb_noremap[typebuf.tb_off]
2226 & (RM_NONE|RM_ABBR)) == 0))
2227 && !(p_paste && (State & (INSERT + CMDLINE)))
2228 && !(State == HITRETURN && (tb_c1 == CAR || tb_c1 == ' '))
2229 && State != ASKMORE
2230 && State != CONFIRM
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002231 && !((ctrl_x_mode_not_default() && at_ctrl_x_key())
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002232 || ((compl_cont_status & CONT_LOCAL)
Bram Moolenaare2c453d2019-08-21 14:37:09 +02002233 && (tb_c1 == Ctrl_N || tb_c1 == Ctrl_P))))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002234 {
2235#ifdef FEAT_LANGMAP
2236 if (tb_c1 == K_SPECIAL)
2237 nolmaplen = 2;
2238 else
2239 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002240 LANGMAP_ADJUST(tb_c1, (State & (CMDLINE | INSERT)) == 0
2241 && get_real_state() != SELECTMODE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002242 nolmaplen = 0;
2243 }
2244#endif
2245 // First try buffer-local mappings.
2246 mp = get_buf_maphash_list(local_State, tb_c1);
2247 mp2 = get_maphash_list(local_State, tb_c1);
2248 if (mp == NULL)
2249 {
2250 // There are no buffer-local mappings.
2251 mp = mp2;
2252 mp2 = NULL;
2253 }
Bram Moolenaareda35f72019-08-03 14:59:44 +02002254
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002255 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002256 * Loop until a partly matching mapping is found or all (local)
2257 * mappings have been checked.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002258 * The longest full match is remembered in "mp_match".
Bram Moolenaareda35f72019-08-03 14:59:44 +02002259 * A full match is only accepted if there is no partly match, so "aa"
2260 * and "aaa" can both be mapped.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002261 */
2262 mp_match = NULL;
2263 mp_match_len = 0;
2264 for ( ; mp != NULL;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002265 mp->m_next == NULL ? (mp = mp2, mp2 = NULL) : (mp = mp->m_next))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002266 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002267 // Only consider an entry if the first character matches and it is
2268 // for the current state.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002269 // Skip ":lmap" mappings if keys were mapped.
2270 if (mp->m_keys[0] == tb_c1
2271 && (mp->m_mode & local_State)
Bram Moolenaar459fd782019-10-13 16:43:39 +02002272 && !(mp->m_simplified && seenModifyOtherKeys)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002273 && ((mp->m_mode & LANGMAP) == 0 || typebuf.tb_maplen == 0))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002274 {
2275#ifdef FEAT_LANGMAP
2276 int nomap = nolmaplen;
2277 int c2;
2278#endif
2279 // find the match length of this mapping
2280 for (mlen = 1; mlen < typebuf.tb_len; ++mlen)
2281 {
2282#ifdef FEAT_LANGMAP
2283 c2 = typebuf.tb_buf[typebuf.tb_off + mlen];
2284 if (nomap > 0)
2285 --nomap;
2286 else if (c2 == K_SPECIAL)
2287 nomap = 2;
2288 else
2289 LANGMAP_ADJUST(c2, TRUE);
2290 if (mp->m_keys[mlen] != c2)
2291#else
2292 if (mp->m_keys[mlen] !=
2293 typebuf.tb_buf[typebuf.tb_off + mlen])
2294#endif
2295 break;
2296 }
2297
Bram Moolenaareda35f72019-08-03 14:59:44 +02002298 // Don't allow mapping the first byte(s) of a multi-byte char.
2299 // Happens when mapping <M-a> and then changing 'encoding'.
2300 // Beware that 0x80 is escaped.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002301 {
2302 char_u *p1 = mp->m_keys;
2303 char_u *p2 = mb_unescape(&p1);
2304
2305 if (has_mbyte && p2 != NULL
Bram Moolenaar1614a142019-10-06 22:00:13 +02002306 && MB_BYTE2LEN(tb_c1) > mb_ptr2len(p2))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002307 mlen = 0;
2308 }
2309
2310 // Check an entry whether it matches.
2311 // - Full match: mlen == keylen
2312 // - Partly match: mlen == typebuf.tb_len
2313 keylen = mp->m_keylen;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002314 if (mlen == keylen || (mlen == typebuf.tb_len
2315 && typebuf.tb_len < keylen))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002316 {
2317 char_u *s;
2318 int n;
2319
Bram Moolenaareda35f72019-08-03 14:59:44 +02002320 // If only script-local mappings are allowed, check if the
2321 // mapping starts with K_SNR.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002322 s = typebuf.tb_noremap + typebuf.tb_off;
2323 if (*s == RM_SCRIPT
2324 && (mp->m_keys[0] != K_SPECIAL
2325 || mp->m_keys[1] != KS_EXTRA
Bram Moolenaareda35f72019-08-03 14:59:44 +02002326 || mp->m_keys[2] != (int)KE_SNR))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002327 continue;
2328
Bram Moolenaareda35f72019-08-03 14:59:44 +02002329 // If one of the typed keys cannot be remapped, skip the
2330 // entry.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002331 for (n = mlen; --n >= 0; )
2332 if (*s++ & (RM_NONE|RM_ABBR))
2333 break;
2334 if (n >= 0)
2335 continue;
2336
2337 if (keylen > typebuf.tb_len)
2338 {
2339 if (!*timedout && !(mp_match != NULL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002340 && mp_match->m_nowait))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002341 {
2342 // break at a partly match
2343 keylen = KEYLEN_PART_MAP;
2344 break;
2345 }
2346 }
2347 else if (keylen > mp_match_len)
2348 {
2349 // found a longer match
2350 mp_match = mp;
2351 mp_match_len = keylen;
2352 }
2353 }
2354 else
Bram Moolenaareda35f72019-08-03 14:59:44 +02002355 // No match; may have to check for termcode at next
2356 // character.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002357 if (max_mlen < mlen)
2358 max_mlen = mlen;
2359 }
2360 }
2361
Bram Moolenaareda35f72019-08-03 14:59:44 +02002362 // If no partly match found, use the longest full match.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002363 if (keylen != KEYLEN_PART_MAP)
2364 {
2365 mp = mp_match;
2366 keylen = mp_match_len;
2367 }
2368 }
2369
2370 /*
2371 * Check for match with 'pastetoggle'
2372 */
2373 if (*p_pt != NUL && mp == NULL && (State & (INSERT|NORMAL)))
2374 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002375 for (mlen = 0; mlen < typebuf.tb_len && p_pt[mlen]; ++mlen)
2376 if (p_pt[mlen] != typebuf.tb_buf[typebuf.tb_off + mlen])
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002377 break;
2378 if (p_pt[mlen] == NUL) // match
2379 {
2380 // write chars to script file(s)
2381 if (mlen > typebuf.tb_maplen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002382 gotchars(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_maplen,
2383 mlen - typebuf.tb_maplen);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002384
2385 del_typebuf(mlen, 0); // remove the chars
Bram Moolenaareda35f72019-08-03 14:59:44 +02002386 set_option_value((char_u *)"paste", (long)!p_paste, NULL, 0);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002387 if (!(State & INSERT))
2388 {
2389 msg_col = 0;
2390 msg_row = Rows - 1;
2391 msg_clr_eos(); // clear ruler
2392 }
2393 status_redraw_all();
2394 redraw_statuslines();
2395 showmode();
2396 setcursor();
2397 *keylenp = keylen;
2398 return map_result_retry;
2399 }
2400 // Need more chars for partly match.
2401 if (mlen == typebuf.tb_len)
2402 keylen = KEYLEN_PART_KEY;
2403 else if (max_mlen < mlen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002404 // no match, may have to check for termcode at next character
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002405 max_mlen = mlen + 1;
2406 }
2407
Bram Moolenaareda35f72019-08-03 14:59:44 +02002408 if ((mp == NULL || max_mlen >= mp_match_len) && keylen != KEYLEN_PART_MAP)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002409 {
2410 int save_keylen = keylen;
2411
2412 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002413 * When no matching mapping found or found a non-matching mapping that
2414 * matches at least what the matching mapping matched:
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002415 * Check if we have a terminal code, when:
Bram Moolenaareda35f72019-08-03 14:59:44 +02002416 * - mapping is allowed,
2417 * - keys have not been mapped,
2418 * - and not an ESC sequence, not in insert mode or p_ek is on,
2419 * - and when not timed out,
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002420 */
2421 if ((no_mapping == 0 || allow_keys != 0)
2422 && (typebuf.tb_maplen == 0
2423 || (p_remap && typebuf.tb_noremap[
Bram Moolenaareda35f72019-08-03 14:59:44 +02002424 typebuf.tb_off] == RM_YES))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002425 && !*timedout)
2426 {
2427 keylen = check_termcode(max_mlen + 1,
2428 NULL, 0, NULL);
2429
Bram Moolenaareda35f72019-08-03 14:59:44 +02002430 // If no termcode matched but 'pastetoggle' matched partially it's
2431 // like an incomplete key sequence.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002432 if (keylen == 0 && save_keylen == KEYLEN_PART_KEY)
2433 keylen = KEYLEN_PART_KEY;
2434
Bram Moolenaareda35f72019-08-03 14:59:44 +02002435 // When getting a partial match, but the last characters were not
2436 // typed, don't wait for a typed character to complete the
2437 // termcode. This helps a lot when a ":normal" command ends in an
2438 // ESC.
2439 if (keylen < 0 && typebuf.tb_len == typebuf.tb_maplen)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002440 keylen = 0;
2441 }
2442 else
2443 keylen = 0;
2444 if (keylen == 0) // no matching terminal code
2445 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002446#ifdef AMIGA
2447 // check for window bounds report
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002448 if (typebuf.tb_maplen == 0 && (typebuf.tb_buf[
Bram Moolenaareda35f72019-08-03 14:59:44 +02002449 typebuf.tb_off] & 0xff) == CSI)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002450 {
2451 char_u *s;
2452
2453 for (s = typebuf.tb_buf + typebuf.tb_off + 1;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002454 s < typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len
2455 && (VIM_ISDIGIT(*s) || *s == ';' || *s == ' ');
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002456 ++s)
2457 ;
2458 if (*s == 'r' || *s == '|') // found one
2459 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002460 del_typebuf(
2461 (int)(s + 1 - (typebuf.tb_buf + typebuf.tb_off)), 0);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002462 // get size and redraw screen
2463 shell_resized();
2464 *keylenp = keylen;
2465 return map_result_retry;
2466 }
2467 if (*s == NUL) // need more characters
2468 keylen = KEYLEN_PART_KEY;
2469 }
2470 if (keylen >= 0)
2471#endif
Bram Moolenaareda35f72019-08-03 14:59:44 +02002472 // When there was a matching mapping and no termcode could be
2473 // replaced after another one, use that mapping (loop around).
2474 // If there was no mapping at all use the character from the
2475 // typeahead buffer right here.
2476 if (mp == NULL)
2477 {
2478 *keylenp = keylen;
2479 return map_result_get; // got character, break for loop
2480 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002481 }
2482
2483 if (keylen > 0) // full matching terminal code
2484 {
2485#if defined(FEAT_GUI) && defined(FEAT_MENU)
2486 if (typebuf.tb_len >= 2
Bram Moolenaareda35f72019-08-03 14:59:44 +02002487 && typebuf.tb_buf[typebuf.tb_off] == K_SPECIAL
2488 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_MENU)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002489 {
2490 int idx;
2491
Bram Moolenaareda35f72019-08-03 14:59:44 +02002492 // Using a menu may cause a break in undo! It's like using
2493 // gotchars(), but without recording or writing to a script
2494 // file.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002495 may_sync_undo();
2496 del_typebuf(3, 0);
2497 idx = get_menu_index(current_menu, local_State);
2498 if (idx != MENU_INDEX_INVALID)
2499 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002500 // In Select mode and a Visual mode menu is used: Switch
2501 // to Visual mode temporarily. Append K_SELECT to switch
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002502 // back to Select mode.
2503 if (VIsual_active && VIsual_select
Bram Moolenaareda35f72019-08-03 14:59:44 +02002504 && (current_menu->modes & VISUAL))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002505 {
2506 VIsual_select = FALSE;
2507 (void)ins_typebuf(K_SELECT_STRING,
Bram Moolenaareda35f72019-08-03 14:59:44 +02002508 REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002509 }
2510 ins_typebuf(current_menu->strings[idx],
2511 current_menu->noremap[idx],
Bram Moolenaareda35f72019-08-03 14:59:44 +02002512 0, TRUE, current_menu->silent[idx]);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002513 }
2514 }
2515#endif // FEAT_GUI && FEAT_MENU
2516 *keylenp = keylen;
2517 return map_result_retry; // try mapping again
2518 }
2519
Bram Moolenaareda35f72019-08-03 14:59:44 +02002520 // Partial match: get some more characters. When a matching mapping
2521 // was found use that one.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002522 if (mp == NULL || keylen < 0)
2523 keylen = KEYLEN_PART_KEY;
2524 else
2525 keylen = mp_match_len;
2526 }
2527
2528 /*
2529 * complete match
2530 */
2531 if (keylen >= 0 && keylen <= typebuf.tb_len)
2532 {
2533 char_u *map_str;
2534
2535#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002536 int save_m_expr;
2537 int save_m_noremap;
2538 int save_m_silent;
2539 char_u *save_m_keys;
2540 char_u *save_m_str;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002541#else
2542# define save_m_noremap mp->m_noremap
2543# define save_m_silent mp->m_silent
2544#endif
2545
2546 // write chars to script file(s)
2547 if (keylen > typebuf.tb_maplen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002548 gotchars(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_maplen,
2549 keylen - typebuf.tb_maplen);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002550
2551 cmd_silent = (typebuf.tb_silent > 0);
2552 del_typebuf(keylen, 0); // remove the mapped keys
2553
2554 /*
2555 * Put the replacement string in front of mapstr.
2556 * The depth check catches ":map x y" and ":map y x".
2557 */
2558 if (++*mapdepth >= p_mmd)
2559 {
2560 emsg(_("E223: recursive mapping"));
2561 if (State & CMDLINE)
2562 redrawcmdline();
2563 else
2564 setcursor();
2565 flush_buffers(FLUSH_MINIMAL);
Bram Moolenaar30613902019-12-01 22:11:18 +01002566 *mapdepth = 0; // for next one
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002567 *keylenp = keylen;
2568 return map_result_fail;
2569 }
2570
2571 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002572 * In Select mode and a Visual mode mapping is used: Switch to Visual
2573 * mode temporarily. Append K_SELECT to switch back to Select mode.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002574 */
Bram Moolenaareda35f72019-08-03 14:59:44 +02002575 if (VIsual_active && VIsual_select && (mp->m_mode & VISUAL))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002576 {
2577 VIsual_select = FALSE;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002578 (void)ins_typebuf(K_SELECT_STRING, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002579 }
2580
2581#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002582 // Copy the values from *mp that are used, because evaluating the
2583 // expression may invoke a function that redefines the mapping, thereby
2584 // making *mp invalid.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002585 save_m_expr = mp->m_expr;
2586 save_m_noremap = mp->m_noremap;
2587 save_m_silent = mp->m_silent;
2588 save_m_keys = NULL; // only saved when needed
2589 save_m_str = NULL; // only saved when needed
2590
2591 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002592 * Handle ":map <expr>": evaluate the {rhs} as an expression. Also
2593 * save and restore the command line for "normal :".
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002594 */
2595 if (mp->m_expr)
2596 {
2597 int save_vgetc_busy = vgetc_busy;
2598 int save_may_garbage_collect = may_garbage_collect;
Bram Moolenaar4ebe0e62019-11-22 20:55:40 +01002599 int was_screen_col = screen_cur_col;
2600 int was_screen_row = screen_cur_row;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002601
2602 vgetc_busy = 0;
2603 may_garbage_collect = FALSE;
2604
2605 save_m_keys = vim_strsave(mp->m_keys);
2606 save_m_str = vim_strsave(mp->m_str);
2607 map_str = eval_map_expr(save_m_str, NUL);
2608
Bram Moolenaar4ebe0e62019-11-22 20:55:40 +01002609 // The mapping may do anything, but we expect it to take care of
2610 // redrawing. Do put the cursor back where it was.
2611 windgoto(was_screen_row, was_screen_col);
2612 out_flush();
2613
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002614 vgetc_busy = save_vgetc_busy;
2615 may_garbage_collect = save_may_garbage_collect;
2616 }
2617 else
2618#endif
2619 map_str = mp->m_str;
2620
2621 /*
2622 * Insert the 'to' part in the typebuf.tb_buf.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002623 * If 'from' field is the same as the start of the 'to' field, don't
2624 * remap the first character (but do allow abbreviations).
2625 * If m_noremap is set, don't remap the whole 'to' part.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002626 */
2627 if (map_str == NULL)
2628 i = FAIL;
2629 else
2630 {
2631 int noremap;
2632
2633 if (save_m_noremap != REMAP_YES)
2634 noremap = save_m_noremap;
2635 else if (
2636#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002637 STRNCMP(map_str, save_m_keys != NULL ? save_m_keys : mp->m_keys,
2638 (size_t)keylen)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002639#else
2640 STRNCMP(map_str, mp->m_keys, (size_t)keylen)
2641#endif
2642 != 0)
2643 noremap = REMAP_YES;
2644 else
2645 noremap = REMAP_SKIP;
2646 i = ins_typebuf(map_str, noremap,
2647 0, TRUE, cmd_silent || save_m_silent);
2648#ifdef FEAT_EVAL
2649 if (save_m_expr)
2650 vim_free(map_str);
2651#endif
2652 }
2653#ifdef FEAT_EVAL
2654 vim_free(save_m_keys);
2655 vim_free(save_m_str);
2656#endif
2657 *keylenp = keylen;
2658 if (i == FAIL)
2659 return map_result_fail;
2660 return map_result_retry;
2661 }
2662
2663 *keylenp = keylen;
2664 return map_result_nomatch;
2665}
2666
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002667/*
2668 * unget one character (can only be done once!)
2669 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002671vungetc(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672{
2673 old_char = c;
2674 old_mod_mask = mod_mask;
Bram Moolenaarb8978712013-03-16 21:42:16 +01002675 old_mouse_row = mouse_row;
2676 old_mouse_col = mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677}
2678
2679/*
Bram Moolenaar32526b32019-01-19 17:43:09 +01002680 * Get a byte:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 * 1. from the stuffbuffer
2682 * This is used for abbreviated commands like "D" -> "d$".
2683 * Also used to redo a command for ".".
2684 * 2. from the typeahead buffer
2685 * Stores text obtained previously but not used yet.
2686 * Also stores the result of mappings.
2687 * Also used for the ":normal" command.
2688 * 3. from the user
2689 * This may do a blocking wait if "advance" is TRUE.
2690 *
2691 * if "advance" is TRUE (vgetc()):
Bram Moolenaard29459b2016-08-26 22:29:11 +02002692 * Really get the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 * KeyTyped is set to TRUE in the case the user typed the key.
2694 * KeyStuffed is TRUE if the character comes from the stuff buffer.
2695 * if "advance" is FALSE (vpeekc()):
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02002696 * Just look whether there is a character available.
2697 * Return NUL if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 *
2699 * When "no_mapping" is zero, checks for mappings in the current mode.
2700 * Only returns one byte (of a multi-byte character).
2701 * K_SPECIAL and CSI may be escaped, need to get two more bytes then.
2702 */
2703 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002704vgetorpeek(int advance)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705{
2706 int c, c1;
Bram Moolenaar30613902019-12-01 22:11:18 +01002707 int timedout = FALSE; // waited for more than 1 second
2708 // for mapping to complete
2709 int mapdepth = 0; // check for recursive mapping
2710 int mode_deleted = FALSE; // set when mode has been deleted
Bram Moolenaar4e427192006-03-10 21:34:27 +00002711#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 int new_wcol, new_wrow;
2713#endif
2714#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01002715 int shape_changed = FALSE; // adjusted cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716#endif
2717 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 int old_wcol, old_wrow;
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00002719 int wait_tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720
2721 /*
2722 * This function doesn't work very well when called recursively. This may
2723 * happen though, because of:
2724 * 1. The call to add_to_showcmd(). char_avail() is then used to check if
2725 * there is a character available, which calls this function. In that
2726 * case we must return NUL, to indicate no character is available.
2727 * 2. A GUI callback function writes to the screen, causing a
2728 * wait_return().
2729 * Using ":normal" can also do this, but it saves the typeahead buffer,
2730 * thus it should be OK. But don't get a key from the user then.
2731 */
Bram Moolenaare2c38102016-01-31 14:55:40 +01002732 if (vgetc_busy > 0 && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 return NUL;
2734
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002735 ++vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736
2737 if (advance)
2738 KeyStuffed = FALSE;
2739
2740 init_typebuf();
2741 start_stuff();
2742 if (advance && typebuf.tb_maplen == 0)
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02002743 reg_executing = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 do
2745 {
2746/*
2747 * get a character: 1. from the stuffbuffer
2748 */
2749 if (typeahead_char != 0)
2750 {
2751 c = typeahead_char;
2752 if (advance)
2753 typeahead_char = 0;
2754 }
2755 else
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01002756 c = read_readbuffers(advance);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 if (c != NUL && !got_int)
2758 {
2759 if (advance)
2760 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002761 // KeyTyped = FALSE; When the command that stuffed something
2762 // was typed, behave like the stuffed command was typed.
2763 // needed for CTRL-W CTRL-] to open a fold, for example.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 KeyStuffed = TRUE;
2765 }
2766 if (typebuf.tb_no_abbr_cnt == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01002767 typebuf.tb_no_abbr_cnt = 1; // no abbreviations now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 }
2769 else
2770 {
2771 /*
2772 * Loop until we either find a matching mapped key, or we
2773 * are sure that it is not a mapped key.
2774 * If a mapped key sequence is found we go back to the start to
2775 * try re-mapping.
2776 */
2777 for (;;)
2778 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002779 long wait_time;
2780 int keylen = 0;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002781#ifdef FEAT_CMDL_INFO
2782 int showcmd_idx;
2783#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 /*
2785 * ui_breakcheck() is slow, don't use it too often when
2786 * inside a mapping. But call it each time for typed
2787 * characters.
2788 */
2789 if (typebuf.tb_maplen)
2790 line_breakcheck();
2791 else
Bram Moolenaar30613902019-12-01 22:11:18 +01002792 ui_breakcheck(); // check for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 if (got_int)
2794 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002795 // flush all input
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02002796 c = inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 0L);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002797
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 /*
2799 * If inchar() returns TRUE (script file was active) or we
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002800 * are inside a mapping, get out of Insert mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 * Otherwise we behave like having gotten a CTRL-C.
2802 * As a result typing CTRL-C in insert mode will
2803 * really insert a CTRL-C.
2804 */
2805 if ((c || typebuf.tb_maplen)
2806 && (State & (INSERT + CMDLINE)))
2807 c = ESC;
2808 else
2809 c = Ctrl_C;
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02002810 flush_buffers(FLUSH_INPUT); // flush all typeahead
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811
Bram Moolenaard9dfd572006-10-03 13:36:13 +00002812 if (advance)
2813 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002814 // Also record this character, it might be needed to
2815 // get out of Insert mode.
Bram Moolenaard9dfd572006-10-03 13:36:13 +00002816 *typebuf.tb_buf = c;
2817 gotchars(typebuf.tb_buf, 1);
2818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 cmd_silent = FALSE;
2820
2821 break;
2822 }
2823 else if (typebuf.tb_len > 0)
2824 {
2825 /*
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002826 * Check for a mapping in "typebuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 */
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002828 map_result_T result = handle_mapping(
2829 &keylen, &timedout, &mapdepth);
2830
2831 if (result == map_result_retry)
2832 // try mapping again
2833 continue;
2834 if (result == map_result_fail)
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02002835 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002836 // failed, use the outer loop
2837 c = -1;
2838 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002840 if (result == map_result_get)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842/*
2843 * get a character: 2. from the typeahead buffer
2844 */
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002845 c = typebuf.tb_buf[typebuf.tb_off];
Bram Moolenaar30613902019-12-01 22:11:18 +01002846 if (advance) // remove chars from tb_buf
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02002847 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002848 cmd_silent = (typebuf.tb_silent > 0);
2849 if (typebuf.tb_maplen > 0)
2850 KeyTyped = FALSE;
2851 else
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02002852 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002853 KeyTyped = TRUE;
Bram Moolenaar30613902019-12-01 22:11:18 +01002854 // write char to script file(s)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002855 gotchars(typebuf.tb_buf
2856 + typebuf.tb_off, 1);
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02002857 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002858 KeyNoremap = typebuf.tb_noremap[
2859 typebuf.tb_off];
2860 del_typebuf(1, 0);
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02002861 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002862 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 }
2864
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002865 // not enough characters, get more
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 }
2867
2868/*
2869 * get a character: 3. from the user - handle <Esc> in Insert mode
2870 */
2871 /*
Bram Moolenaarf085f422017-06-07 20:39:47 +02002872 * Special case: if we get an <ESC> in insert mode and there
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 * are no more characters at once, we pretend to go out of
2874 * insert mode. This prevents the one second delay after
2875 * typing an <ESC>. If we get something after all, we may
2876 * have to redisplay the mode. That the cursor is in the wrong
2877 * place does not matter.
2878 */
2879 c = 0;
2880#ifdef FEAT_CMDL_INFO
2881 new_wcol = curwin->w_wcol;
2882 new_wrow = curwin->w_wrow;
2883#endif
2884 if ( advance
2885 && typebuf.tb_len == 1
2886 && typebuf.tb_buf[typebuf.tb_off] == ESC
2887 && !no_mapping
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 && ex_normal_busy == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 && typebuf.tb_maplen == 0
2890 && (State & INSERT)
Bram Moolenaar946ffd42010-12-30 12:30:31 +01002891 && (p_timeout
2892 || (keylen == KEYLEN_PART_KEY && p_ttimeout))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 && (c = inchar(typebuf.tb_buf + typebuf.tb_off
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02002894 + typebuf.tb_len, 3, 25L)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 {
2896 colnr_T col = 0, vcol;
2897 char_u *ptr;
2898
Bram Moolenaar28c258f2006-01-25 22:02:51 +00002899 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 {
2901 unshowmode(TRUE);
2902 mode_deleted = TRUE;
2903 }
2904#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01002905 // may show a different cursor shape
Bram Moolenaarf085f422017-06-07 20:39:47 +02002906 if (gui.in_use && State != NORMAL && !cmd_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 {
2908 int save_State;
2909
2910 save_State = State;
2911 State = NORMAL;
2912 gui_update_cursor(TRUE, FALSE);
2913 State = save_State;
2914 shape_changed = TRUE;
2915 }
2916#endif
2917 validate_cursor();
2918 old_wcol = curwin->w_wcol;
2919 old_wrow = curwin->w_wrow;
2920
Bram Moolenaar30613902019-12-01 22:11:18 +01002921 // move cursor left, if possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 if (curwin->w_cursor.col != 0)
2923 {
2924 if (curwin->w_wcol > 0)
2925 {
2926 if (did_ai)
2927 {
2928 /*
2929 * We are expecting to truncate the trailing
2930 * white-space, so find the last non-white
2931 * character -- webb
2932 */
2933 col = vcol = curwin->w_wcol = 0;
2934 ptr = ml_get_curline();
2935 while (col < curwin->w_cursor.col)
2936 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002937 if (!VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 curwin->w_wcol = vcol;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002939 vcol += lbr_chartabsize(ptr, ptr + col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002942 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 ++col;
2945 }
2946 curwin->w_wrow = curwin->w_cline_row
Bram Moolenaar02631462017-09-22 15:20:32 +02002947 + curwin->w_wcol / curwin->w_width;
2948 curwin->w_wcol %= curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 curwin->w_wcol += curwin_col_off();
Bram Moolenaar30613902019-12-01 22:11:18 +01002950 col = 0; // no correction needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 }
2952 else
2953 {
2954 --curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 col = curwin->w_cursor.col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 }
2957 }
2958 else if (curwin->w_p_wrap && curwin->w_wrow)
2959 {
2960 --curwin->w_wrow;
Bram Moolenaar02631462017-09-22 15:20:32 +02002961 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 col = curwin->w_cursor.col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 if (has_mbyte && col > 0 && curwin->w_wcol > 0)
2965 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002966 // Correct when the cursor is on the right halve
2967 // of a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 ptr = ml_get_curline();
2969 col -= (*mb_head_off)(ptr, ptr + col);
2970 if ((*mb_ptr2cells)(ptr + col) > 1)
2971 --curwin->w_wcol;
2972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 }
2974 setcursor();
2975 out_flush();
2976#ifdef FEAT_CMDL_INFO
2977 new_wcol = curwin->w_wcol;
2978 new_wrow = curwin->w_wrow;
2979#endif
2980 curwin->w_wcol = old_wcol;
2981 curwin->w_wrow = old_wrow;
2982 }
2983 if (c < 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01002984 continue; // end of input script reached
Bram Moolenaar20c38922014-07-23 20:41:14 +02002985
Bram Moolenaar30613902019-12-01 22:11:18 +01002986 // Allow mapping for just typed characters. When we get here c
2987 // is the number of extra bytes and typebuf.tb_len is 1.
Bram Moolenaar20c38922014-07-23 20:41:14 +02002988 for (n = 1; n <= c; ++n)
2989 typebuf.tb_noremap[typebuf.tb_off + n] = RM_YES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 typebuf.tb_len += c;
2991
Bram Moolenaar30613902019-12-01 22:11:18 +01002992 // buffer full, don't map
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 if (typebuf.tb_len >= typebuf.tb_maplen + MAXMAPLEN)
2994 {
2995 timedout = TRUE;
2996 continue;
2997 }
2998
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 if (ex_normal_busy > 0)
3000 {
Bram Moolenaare2c38102016-01-31 14:55:40 +01003001#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 static int tc = 0;
Bram Moolenaare2c38102016-01-31 14:55:40 +01003003#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004
Bram Moolenaar30613902019-12-01 22:11:18 +01003005 // No typeahead left and inside ":normal". Must return
3006 // something to avoid getting stuck. When an incomplete
3007 // mapping is present, behave like it timed out.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 if (typebuf.tb_len > 0)
3009 {
3010 timedout = TRUE;
3011 continue;
3012 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003013 // When 'insertmode' is set, ESC just beeps in Insert
3014 // mode. Use CTRL-L to make edit() return.
3015 // For the command line only CTRL-C always breaks it.
3016 // For the cmdline window: Alternate between ESC and
3017 // CTRL-C: ESC for most situations and CTRL-C to close the
3018 // cmdline window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019 if (p_im && (State & INSERT))
3020 c = Ctrl_L;
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003021#ifdef FEAT_TERMINAL
3022 else if (terminal_is_active())
3023 c = K_CANCEL;
3024#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 else if ((State & CMDLINE)
Bram Moolenaare2c38102016-01-31 14:55:40 +01003026#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 || (cmdwin_type > 0 && tc == ESC)
Bram Moolenaare2c38102016-01-31 14:55:40 +01003028#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 )
3030 c = Ctrl_C;
3031 else
3032 c = ESC;
Bram Moolenaare2c38102016-01-31 14:55:40 +01003033#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 tc = c;
Bram Moolenaare2c38102016-01-31 14:55:40 +01003035#endif
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01003036 // return from main_loop()
3037 if (pending_exmode_active)
3038 exmode_active = EXMODE_NORMAL;
3039
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 break;
3041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042
3043/*
3044 * get a character: 3. from the user - update display
3045 */
Bram Moolenaar30613902019-12-01 22:11:18 +01003046 // In insert mode a screen update is skipped when characters
3047 // are still available. But when those available characters
3048 // are part of a mapping, and we are going to do a blocking
3049 // wait here. Need to update the screen to display the
3050 // changed text so far. Also for when 'lazyredraw' is set and
3051 // redrawing was postponed because there was something in the
3052 // input buffer (e.g., termresponse).
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01003053 if (((State & INSERT) != 0 || p_lz) && (State & CMDLINE) == 0
3054 && advance && must_redraw != 0 && !need_wait_return)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 {
3056 update_screen(0);
Bram Moolenaar30613902019-12-01 22:11:18 +01003057 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 }
3059
3060 /*
3061 * If we have a partial match (and are going to wait for more
3062 * input from the user), show the partially matched characters
3063 * to the user with showcmd.
3064 */
3065#ifdef FEAT_CMDL_INFO
Bram Moolenaareda35f72019-08-03 14:59:44 +02003066 showcmd_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067#endif
3068 c1 = 0;
3069 if (typebuf.tb_len > 0 && advance && !exmode_active)
3070 {
3071 if (((State & (NORMAL | INSERT)) || State == LANGMAP)
3072 && State != HITRETURN)
3073 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003074 // this looks nice when typing a dead character map
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 if (State & INSERT
3076 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
3077 + typebuf.tb_len - 1) == 1)
3078 {
3079 edit_putchar(typebuf.tb_buf[typebuf.tb_off
3080 + typebuf.tb_len - 1], FALSE);
Bram Moolenaar30613902019-12-01 22:11:18 +01003081 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 c1 = 1;
3083 }
3084#ifdef FEAT_CMDL_INFO
Bram Moolenaar30613902019-12-01 22:11:18 +01003085 // need to use the col and row from above here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 old_wcol = curwin->w_wcol;
3087 old_wrow = curwin->w_wrow;
3088 curwin->w_wcol = new_wcol;
3089 curwin->w_wrow = new_wrow;
3090 push_showcmd();
3091 if (typebuf.tb_len > SHOWCMD_COLS)
Bram Moolenaareda35f72019-08-03 14:59:44 +02003092 showcmd_idx = typebuf.tb_len - SHOWCMD_COLS;
3093 while (showcmd_idx < typebuf.tb_len)
3094 (void)add_to_showcmd(
3095 typebuf.tb_buf[typebuf.tb_off + showcmd_idx++]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 curwin->w_wcol = old_wcol;
3097 curwin->w_wrow = old_wrow;
3098#endif
3099 }
3100
Bram Moolenaar30613902019-12-01 22:11:18 +01003101 // this looks nice when typing a dead character map
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 if ((State & CMDLINE)
3103#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3104 && cmdline_star == 0
3105#endif
3106 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
3107 + typebuf.tb_len - 1) == 1)
3108 {
3109 putcmdline(typebuf.tb_buf[typebuf.tb_off
3110 + typebuf.tb_len - 1], FALSE);
3111 c1 = 1;
3112 }
3113 }
3114
3115/*
3116 * get a character: 3. from the user - get it
3117 */
Bram Moolenaar83f4cbd2018-06-12 21:35:40 +02003118 if (typebuf.tb_len == 0)
3119 // timedout may have been set while waiting for a mapping
3120 // that has a <Nop> RHS.
3121 timedout = FALSE;
3122
Bram Moolenaar652de232019-04-04 20:13:09 +02003123 if (advance)
3124 {
3125 if (typebuf.tb_len == 0
3126 || !(p_timeout
3127 || (p_ttimeout && keylen == KEYLEN_PART_KEY)))
3128 // blocking wait
3129 wait_time = -1L;
3130 else if (keylen == KEYLEN_PART_KEY && p_ttm >= 0)
3131 wait_time = p_ttm;
3132 else
3133 wait_time = p_tm;
3134 }
3135 else
3136 wait_time = 0;
3137
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003138 wait_tb_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 c = inchar(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len,
3140 typebuf.tb_buflen - typebuf.tb_off - typebuf.tb_len - 1,
Bram Moolenaar652de232019-04-04 20:13:09 +02003141 wait_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142
3143#ifdef FEAT_CMDL_INFO
Bram Moolenaareda35f72019-08-03 14:59:44 +02003144 if (showcmd_idx != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 pop_showcmd();
3146#endif
3147 if (c1 == 1)
3148 {
3149 if (State & INSERT)
3150 edit_unputchar();
3151 if (State & CMDLINE)
3152 unputcmdline();
Bram Moolenaarbc256d92012-06-06 12:06:15 +02003153 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003154 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 }
3156
3157 if (c < 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003158 continue; // end of input script reached
3159 if (c == NUL) // no character available
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 {
3161 if (!advance)
3162 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01003163 if (wait_tb_len > 0) // timed out
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 {
3165 timedout = TRUE;
3166 continue;
3167 }
3168 }
3169 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003170 { // allow mapping for just typed characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 while (typebuf.tb_buf[typebuf.tb_off
3172 + typebuf.tb_len] != NUL)
3173 typebuf.tb_noremap[typebuf.tb_off
3174 + typebuf.tb_len++] = RM_YES;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003175#ifdef HAVE_INPUT_METHOD
Bram Moolenaar30613902019-12-01 22:11:18 +01003176 // Get IM status right after getting keys, not after the
3177 // timeout for a mapping (focus may be lost by then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 vgetc_im_active = im_get_status();
3179#endif
3180 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003181 } // for (;;)
3182 } // if (!character from stuffbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183
Bram Moolenaar30613902019-12-01 22:11:18 +01003184 // if advance is FALSE don't loop on NULs
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003185 } while ((c < 0 && c != K_CANCEL) || (advance && c == NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186
3187 /*
3188 * The "INSERT" message is taken care of here:
3189 * if we return an ESC to exit insert mode, the message is deleted
3190 * if we don't return an ESC but deleted the message before, redisplay it
3191 */
Bram Moolenaar09df3122006-01-23 22:23:09 +00003192 if (advance && p_smd && msg_silent == 0 && (State & INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003194 if (c == ESC && !mode_deleted && !no_mapping && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 {
3196 if (typebuf.tb_len && !KeyTyped)
Bram Moolenaar30613902019-12-01 22:11:18 +01003197 redraw_cmdline = TRUE; // delete mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 else
3199 unshowmode(FALSE);
3200 }
3201 else if (c != ESC && mode_deleted)
3202 {
3203 if (typebuf.tb_len && !KeyTyped)
Bram Moolenaar30613902019-12-01 22:11:18 +01003204 redraw_cmdline = TRUE; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 else
3206 showmode();
3207 }
3208 }
3209#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003210 // may unshow different cursor shape
Bram Moolenaarf085f422017-06-07 20:39:47 +02003211 if (gui.in_use && shape_changed)
3212 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213#endif
Bram Moolenaar6edbbd82019-03-10 09:41:51 +01003214 if (timedout && c == ESC)
3215 {
3216 char_u nop_buf[3];
3217
3218 // When recording there will be no timeout. Add a <Nop> after the ESC
3219 // to avoid that it forms a key code with following characters.
3220 nop_buf[0] = K_SPECIAL;
3221 nop_buf[1] = KS_EXTRA;
3222 nop_buf[2] = KE_NOP;
3223 gotchars(nop_buf, 3);
3224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003226 --vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227
3228 return c;
3229}
3230
3231/*
3232 * inchar() - get one character from
3233 * 1. a scriptfile
3234 * 2. the keyboard
3235 *
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003236 * As many characters as we can get (up to 'maxlen') are put in "buf" and
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 * NUL terminated (buffer length must be 'maxlen' + 1).
3238 * Minimum for "maxlen" is 3!!!!
3239 *
3240 * "tb_change_cnt" is the value of typebuf.tb_change_cnt if "buf" points into
3241 * it. When typebuf.tb_change_cnt changes (e.g., when a message is received
3242 * from a remote client) "buf" can no longer be used. "tb_change_cnt" is 0
3243 * otherwise.
3244 *
3245 * If we got an interrupt all input is read until none is available.
3246 *
3247 * If wait_time == 0 there is no waiting for the char.
3248 * If wait_time == n we wait for n msec for a character to arrive.
3249 * If wait_time == -1 we wait forever for a character to arrive.
3250 *
3251 * Return the number of obtained characters.
3252 * Return -1 when end of input script reached.
3253 */
Bram Moolenaarcda77642016-06-04 13:32:35 +02003254 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003255inchar(
3256 char_u *buf,
3257 int maxlen,
Bram Moolenaar30613902019-12-01 22:11:18 +01003258 long wait_time) // milli seconds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259{
Bram Moolenaar30613902019-12-01 22:11:18 +01003260 int len = 0; // init for GCC
3261 int retesc = FALSE; // return ESC with gotint
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 int script_char;
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003263 int tb_change_cnt = typebuf.tb_change_cnt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264
Bram Moolenaar30613902019-12-01 22:11:18 +01003265 if (wait_time == -1L || wait_time > 100L) // flush output before waiting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 {
3267 cursor_on();
Bram Moolenaara338adc2018-01-31 20:51:47 +01003268 out_flush_cursor(FALSE, FALSE);
3269#if defined(FEAT_GUI) && defined(FEAT_MOUSESHAPE)
3270 if (gui.in_use && postponed_mouseshape)
3271 update_mouseshape(-1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272#endif
3273 }
3274
3275 /*
3276 * Don't reset these when at the hit-return prompt, otherwise a endless
3277 * recursive loop may result (write error in swapfile, hit-return, timeout
3278 * on char wait, flush swapfile, write error....).
3279 */
3280 if (State != HITRETURN)
3281 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003282 did_outofmem_msg = FALSE; // display out of memory message (again)
3283 did_swapwrite_msg = FALSE; // display swap file write error again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003285 undo_off = FALSE; // restart undo now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286
3287 /*
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00003288 * Get a character from a script file if there is one.
3289 * If interrupted: Stop reading script files, close them all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 */
3291 script_char = -1;
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00003292 while (scriptin[curscript] != NULL && script_char < 0
3293#ifdef FEAT_EVAL
3294 && !ignore_script
3295#endif
3296 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02003298#ifdef MESSAGE_QUEUE
3299 parse_queued_messages();
Bram Moolenaarf2330482008-06-24 20:19:36 +00003300#endif
3301
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 if (got_int || (script_char = getc(scriptin[curscript])) < 0)
3303 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003304 // Reached EOF.
3305 // Careful: closescript() frees typebuf.tb_buf[] and buf[] may
3306 // point inside typebuf.tb_buf[]. Don't use buf[] after this!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 closescript();
3308 /*
3309 * When reading script file is interrupted, return an ESC to get
3310 * back to normal mode.
3311 * Otherwise return -1, because typebuf.tb_buf[] has changed.
3312 */
3313 if (got_int)
3314 retesc = TRUE;
3315 else
3316 return -1;
3317 }
3318 else
3319 {
3320 buf[0] = script_char;
3321 len = 1;
3322 }
3323 }
3324
Bram Moolenaar30613902019-12-01 22:11:18 +01003325 if (script_char < 0) // did not get a character from script
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 {
3327 /*
3328 * If we got an interrupt, skip all previously typed characters and
3329 * return TRUE if quit reading script file.
3330 * Stop reading typeahead when a single CTRL-C was read,
3331 * fill_input_buf() returns this when not able to read from stdin.
3332 * Don't use buf[] here, closescript() may have freed typebuf.tb_buf[]
3333 * and buf may be pointing inside typebuf.tb_buf[].
3334 */
3335 if (got_int)
3336 {
3337#define DUM_LEN MAXMAPLEN * 3 + 3
3338 char_u dum[DUM_LEN + 1];
3339
3340 for (;;)
3341 {
3342 len = ui_inchar(dum, DUM_LEN, 0L, 0);
3343 if (len == 0 || (len == 1 && dum[0] == 3))
3344 break;
3345 }
3346 return retesc;
3347 }
3348
3349 /*
3350 * Always flush the output characters when getting input characters
Bram Moolenaarcb574f42019-01-25 22:29:57 +01003351 * from the user and not just peeking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 */
Bram Moolenaarcb574f42019-01-25 22:29:57 +01003353 if (wait_time == -1L || wait_time > 10L)
3354 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355
3356 /*
3357 * Fill up to a third of the buffer, because each character may be
3358 * tripled below.
3359 */
3360 len = ui_inchar(buf, maxlen / 3, wait_time, tb_change_cnt);
3361 }
3362
Bram Moolenaar30613902019-12-01 22:11:18 +01003363 // If the typebuf was changed further down, it is like nothing was added by
3364 // this call.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 if (typebuf_changed(tb_change_cnt))
3366 return 0;
3367
Bram Moolenaar30613902019-12-01 22:11:18 +01003368 // Note the change in the typeahead buffer, this matters for when
3369 // vgetorpeek() is called recursively, e.g. using getchar(1) in a timer
3370 // function.
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003371 if (len > 0 && ++typebuf.tb_change_cnt == 0)
3372 typebuf.tb_change_cnt = 1;
3373
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003374 return fix_input_buffer(buf, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375}
3376
3377/*
3378 * Fix typed characters for use by vgetc() and check_termcode().
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003379 * "buf[]" must have room to triple the number of bytes!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 * Returns the new length.
3381 */
3382 int
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003383fix_input_buffer(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384{
3385 int i;
3386 char_u *p = buf;
3387
3388 /*
3389 * Two characters are special: NUL and K_SPECIAL.
3390 * When compiled With the GUI CSI is also special.
3391 * Replace NUL by K_SPECIAL KS_ZERO KE_FILLER
3392 * Replace K_SPECIAL by K_SPECIAL KS_SPECIAL KE_FILLER
3393 * Replace CSI by K_SPECIAL KS_EXTRA KE_CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 */
3395 for (i = len; --i >= 0; ++p)
3396 {
3397#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003398 // When the GUI is used any character can come after a CSI, don't
3399 // escape it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 if (gui.in_use && p[0] == CSI && i >= 2)
3401 {
3402 p += 2;
3403 i -= 2;
3404 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003405# ifndef MSWIN
Bram Moolenaar30613902019-12-01 22:11:18 +01003406 // When the GUI is not used CSI needs to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 else if (!gui.in_use && p[0] == CSI)
3408 {
3409 mch_memmove(p + 3, p + 1, (size_t)i);
3410 *p++ = K_SPECIAL;
3411 *p++ = KS_EXTRA;
3412 *p = (int)KE_CSI;
3413 len += 2;
3414 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003415# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 else
3417#endif
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003418 if (p[0] == NUL || (p[0] == K_SPECIAL
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003419 // timeout may generate K_CURSORHOLD
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003420 && (i < 2 || p[1] != KS_EXTRA || p[2] != (int)KE_CURSORHOLD)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003421#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003422 // Win32 console passes modifiers
3423 && (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003424# ifdef VIMDLL
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003425 gui.in_use ||
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003426# endif
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003427 (i < 2 || p[1] != KS_MODIFIER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428#endif
3429 ))
3430 {
3431 mch_memmove(p + 3, p + 1, (size_t)i);
3432 p[2] = K_THIRD(p[0]);
3433 p[1] = K_SECOND(p[0]);
3434 p[0] = K_SPECIAL;
3435 p += 2;
3436 len += 2;
3437 }
3438 }
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003439 *p = NUL; // add trailing NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 return len;
3441}
3442
3443#if defined(USE_INPUT_BUF) || defined(PROTO)
3444/*
3445 * Return TRUE when bytes are in the input buffer or in the typeahead buffer.
3446 * Normally the input buffer would be sufficient, but the server_to_input_buf()
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003447 * or feedkeys() may insert characters in the typeahead buffer while we are
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003448 * waiting for input to arrive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 */
3450 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003451input_available(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452{
3453 return (!vim_is_input_buf_empty()
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003454# if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
3455 || typebuf_was_filled
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456# endif
3457 );
3458}
3459#endif