blob: 6602eeac53636fb9f62f08b678d9d8ad3e7abe8e [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/*
zeertzjqa3f83fe2021-11-22 12:47:39 +000011 * getchar.c: Code related to getting a character from the user or a script
12 * file, manipulations with redo buffer and stuff buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013 */
14
15#include "vim.h"
16
17/*
18 * These buffers are used for storing:
19 * - stuffed characters: A command that is translated into another command.
20 * - redo characters: will redo the last change.
Bram Moolenaarf6f95d92009-11-11 15:23:37 +000021 * - recorded characters: for the "q" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022 *
23 * The bytes are stored like in the typeahead buffer:
24 * - K_SPECIAL introduces a special key (two more bytes follow). A literal
25 * K_SPECIAL is stored as K_SPECIAL KS_SPECIAL KE_FILLER.
26 * - CSI introduces a GUI termcap code (also when gui.in_use is FALSE,
27 * otherwise switching the GUI on would make mappings invalid).
28 * A literal CSI is stored as CSI KS_EXTRA KE_CSI.
29 * These translations are also done on multi-byte characters!
30 *
31 * Escaping CSI bytes is done by the system-specific input functions, called
32 * by ui_inchar().
33 * Escaping K_SPECIAL is done by inchar().
34 * Un-escaping is done by vgetc().
35 */
36
Bram Moolenaar30613902019-12-01 22:11:18 +010037#define MINIMAL_SIZE 20 // minimal size for b_str
Bram Moolenaar071d4272004-06-13 20:20:40 +000038
Bram Moolenaar285e3352018-04-18 23:01:13 +020039static buffheader_T redobuff = {{NULL, {NUL}}, NULL, 0, 0};
40static buffheader_T old_redobuff = {{NULL, {NUL}}, NULL, 0, 0};
41static buffheader_T recordbuff = {{NULL, {NUL}}, NULL, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +000042
Bram Moolenaar30613902019-12-01 22:11:18 +010043static int typeahead_char = 0; // typeahead char that's not flushed
Bram Moolenaar071d4272004-06-13 20:20:40 +000044
45/*
46 * when block_redo is TRUE redo buffer will not be changed
47 * used by edit() to repeat insertions and 'V' command for redoing
48 */
49static int block_redo = FALSE;
50
Bram Moolenaar459fd782019-10-13 16:43:39 +020051static int KeyNoremap = 0; // remapping flags
Bram Moolenaar071d4272004-06-13 20:20:40 +000052
53/*
Bram Moolenaarfd89d7e2016-06-04 20:25:05 +020054 * Variables used by vgetorpeek() and flush_buffers().
Bram Moolenaar071d4272004-06-13 20:20:40 +000055 *
56 * typebuf.tb_buf[] contains all characters that are not consumed yet.
57 * typebuf.tb_buf[typebuf.tb_off] is the first valid character.
58 * typebuf.tb_buf[typebuf.tb_off + typebuf.tb_len - 1] is the last valid char.
59 * typebuf.tb_buf[typebuf.tb_off + typebuf.tb_len] must be NUL.
60 * The head of the buffer may contain the result of mappings, abbreviations
61 * and @a commands. The length of this part is typebuf.tb_maplen.
62 * typebuf.tb_silent is the part where <silent> applies.
63 * After the head are characters that come from the terminal.
64 * typebuf.tb_no_abbr_cnt is the number of characters in typebuf.tb_buf that
65 * should not be considered for abbreviations.
66 * Some parts of typebuf.tb_buf may not be mapped. These parts are remembered
67 * in typebuf.tb_noremap[], which is the same length as typebuf.tb_buf and
68 * contains RM_NONE for the characters that are not to be remapped.
69 * typebuf.tb_noremap[typebuf.tb_off] is the first valid flag.
70 * (typebuf has been put in globals.h, because check_termcode() needs it).
71 */
Bram Moolenaar30613902019-12-01 22:11:18 +010072#define RM_YES 0 // tb_noremap: remap
73#define RM_NONE 1 // tb_noremap: don't remap
74#define RM_SCRIPT 2 // tb_noremap: remap local script mappings
75#define RM_ABBR 4 // tb_noremap: don't remap, do abbrev.
Bram Moolenaar071d4272004-06-13 20:20:40 +000076
Bram Moolenaar30613902019-12-01 22:11:18 +010077// typebuf.tb_buf has three parts: room in front (for result of mappings), the
78// middle for typeahead and room for new characters (which needs to be 3 *
Dominique Pelleaf4a61a2021-12-27 17:21:41 +000079// MAXMAPLEN for the Amiga).
Bram Moolenaar071d4272004-06-13 20:20:40 +000080#define TYPELEN_INIT (5 * (MAXMAPLEN + 3))
Bram Moolenaar30613902019-12-01 22:11:18 +010081static char_u typebuf_init[TYPELEN_INIT]; // initial typebuf.tb_buf
82static char_u noremapbuf_init[TYPELEN_INIT]; // initial typebuf.tb_noremap
Bram Moolenaar071d4272004-06-13 20:20:40 +000083
Bram Moolenaar30613902019-12-01 22:11:18 +010084static int last_recorded_len = 0; // number of last recorded chars
Bram Moolenaar071d4272004-06-13 20:20:40 +000085
Bram Moolenaare32c3c42022-01-15 18:26:04 +000086#ifdef FEAT_EVAL
87mapblock_T *last_used_map = NULL;
88#endif
89
Bram Moolenaard25c16e2016-01-29 22:13:30 +010090static int read_readbuf(buffheader_T *buf, int advance);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010091static void init_typebuf(void);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010092static void may_sync_undo(void);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020093static void free_typebuf(void);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010094static void closescript(void);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020095static void updatescript(int c);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010096static int vgetorpeek(int);
Bram Moolenaar0f0f2302017-08-30 18:52:56 +020097static int inchar(char_u *buf, int maxlen, long wait_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +000098
99/*
100 * Free and clear a buffer.
101 */
Bram Moolenaarbdff0122020-04-05 18:56:05 +0200102 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100103free_buff(buffheader_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100105 buffblock_T *p, *np;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106
Bram Moolenaar285e3352018-04-18 23:01:13 +0200107 for (p = buf->bh_first.b_next; p != NULL; p = np)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108 {
109 np = p->b_next;
110 vim_free(p);
111 }
Bram Moolenaar285e3352018-04-18 23:01:13 +0200112 buf->bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113}
114
115/*
116 * Return the contents of a buffer as a single string.
117 * K_SPECIAL and CSI in the returned string are escaped.
118 */
119 static char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100120get_buffcont(
121 buffheader_T *buffer,
Bram Moolenaar30613902019-12-01 22:11:18 +0100122 int dozero) // count == zero is not an error
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123{
124 long_u count = 0;
125 char_u *p = NULL;
126 char_u *p2;
127 char_u *str;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200128 buffblock_T *bp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129
Bram Moolenaar30613902019-12-01 22:11:18 +0100130 // compute the total length of the string
Bram Moolenaar285e3352018-04-18 23:01:13 +0200131 for (bp = buffer->bh_first.b_next; bp != NULL; bp = bp->b_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132 count += (long_u)STRLEN(bp->b_str);
133
Bram Moolenaar18a4ba22019-05-24 19:39:03 +0200134 if ((count || dozero) && (p = alloc(count + 1)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135 {
136 p2 = p;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200137 for (bp = buffer->bh_first.b_next; bp != NULL; bp = bp->b_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138 for (str = bp->b_str; *str; )
139 *p2++ = *str++;
140 *p2 = NUL;
141 }
142 return (p);
143}
144
145/*
146 * Return the contents of the record buffer as a single string
147 * and clear the record buffer.
148 * K_SPECIAL and CSI in the returned string are escaped.
149 */
150 char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100151get_recorded(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152{
153 char_u *p;
154 size_t len;
155
156 p = get_buffcont(&recordbuff, TRUE);
157 free_buff(&recordbuff);
158
159 /*
160 * Remove the characters that were added the last time, these must be the
161 * (possibly mapped) characters that stopped the recording.
162 */
163 len = STRLEN(p);
164 if ((int)len >= last_recorded_len)
165 {
166 len -= last_recorded_len;
167 p[len] = NUL;
168 }
169
170 /*
171 * When stopping recording from Insert mode with CTRL-O q, also remove the
172 * CTRL-O.
173 */
174 if (len > 0 && restart_edit != 0 && p[len - 1] == Ctrl_O)
175 p[len - 1] = NUL;
176
177 return (p);
178}
179
180/*
181 * Return the contents of the redo buffer as a single string.
182 * K_SPECIAL and CSI in the returned string are escaped.
183 */
184 char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100185get_inserted(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186{
Bram Moolenaarf2330482008-06-24 20:19:36 +0000187 return get_buffcont(&redobuff, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188}
189
190/*
Bram Moolenaar4e86cba2007-05-06 11:56:32 +0000191 * Add string "s" after the current block of buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 * K_SPECIAL and CSI should have been escaped already.
193 */
194 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100195add_buff(
196 buffheader_T *buf,
197 char_u *s,
Bram Moolenaar30613902019-12-01 22:11:18 +0100198 long slen) // length of "s" or -1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100200 buffblock_T *p;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200201 long_u len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202
203 if (slen < 0)
204 slen = (long)STRLEN(s);
Bram Moolenaar30613902019-12-01 22:11:18 +0100205 if (slen == 0) // don't add empty strings
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 return;
207
Bram Moolenaar30613902019-12-01 22:11:18 +0100208 if (buf->bh_first.b_next == NULL) // first add to list
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 {
210 buf->bh_space = 0;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200211 buf->bh_curr = &(buf->bh_first);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212 }
Bram Moolenaar30613902019-12-01 22:11:18 +0100213 else if (buf->bh_curr == NULL) // buffer has already been read
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214 {
Bram Moolenaar6d057012021-12-31 18:49:43 +0000215 iemsg(_(e_add_to_internal_buffer_that_was_already_read_from));
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 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246}
247
248/*
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000249 * Delete "slen" bytes from the end of "buf".
250 * Only works when it was just added.
251 */
252 static void
253delete_buff_tail(buffheader_T *buf, int slen)
254{
Bram Moolenaara4bc2dd2022-01-27 19:27:16 +0000255 int len;
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000256
Bram Moolenaar37cf4132022-01-27 20:47:03 +0000257 if (buf->bh_curr == NULL)
Bram Moolenaara4bc2dd2022-01-27 19:27:16 +0000258 return; // nothing to delete
259 len = (int)STRLEN(buf->bh_curr->b_str);
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000260 if (len >= slen)
261 {
262 buf->bh_curr->b_str[len - slen] = NUL;
263 buf->bh_space += slen;
264 }
265}
266
267/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268 * Add number "n" to buffer "buf".
269 */
270 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100271add_num_buff(buffheader_T *buf, long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272{
273 char_u number[32];
274
275 sprintf((char *)number, "%ld", n);
276 add_buff(buf, number, -1L);
277}
278
279/*
280 * Add character 'c' to buffer "buf".
281 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
282 */
283 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100284add_char_buff(buffheader_T *buf, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286 char_u bytes[MB_MAXBYTES + 1];
287 int len;
288 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289 char_u temp[4];
290
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291 if (IS_SPECIAL(c))
292 len = 1;
293 else
294 len = (*mb_char2bytes)(c, bytes);
295 for (i = 0; i < len; ++i)
296 {
297 if (!IS_SPECIAL(c))
298 c = bytes[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299
300 if (IS_SPECIAL(c) || c == K_SPECIAL || c == NUL)
301 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100302 // translate special key code into three byte sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303 temp[0] = K_SPECIAL;
304 temp[1] = K_SECOND(c);
305 temp[2] = K_THIRD(c);
306 temp[3] = NUL;
307 }
308#ifdef FEAT_GUI
309 else if (c == CSI)
310 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100311 // Translate a CSI to a CSI - KS_EXTRA - KE_CSI sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312 temp[0] = CSI;
313 temp[1] = KS_EXTRA;
314 temp[2] = (int)KE_CSI;
315 temp[3] = NUL;
316 }
317#endif
318 else
319 {
320 temp[0] = c;
321 temp[1] = NUL;
322 }
323 add_buff(buf, temp, -1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325}
326
Bram Moolenaar30613902019-12-01 22:11:18 +0100327// First read ahead buffer. Used for translated commands.
Bram Moolenaar285e3352018-04-18 23:01:13 +0200328static buffheader_T readbuf1 = {{NULL, {NUL}}, NULL, 0, 0};
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100329
Bram Moolenaar30613902019-12-01 22:11:18 +0100330// Second read ahead buffer. Used for redo.
Bram Moolenaar285e3352018-04-18 23:01:13 +0200331static buffheader_T readbuf2 = {{NULL, {NUL}}, NULL, 0, 0};
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100332
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333/*
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100334 * Get one byte from the read buffers. Use readbuf1 one first, use readbuf2
335 * if that one is empty.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336 * If advance == TRUE go to the next char.
337 * No translation is done K_SPECIAL and CSI are escaped.
338 */
339 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100340read_readbuffers(int advance)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100342 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100344 c = read_readbuf(&readbuf1, advance);
345 if (c == NUL)
346 c = read_readbuf(&readbuf2, advance);
347 return c;
348}
349
350 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100351read_readbuf(buffheader_T *buf, int advance)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100352{
353 char_u c;
354 buffblock_T *curr;
355
Bram Moolenaar30613902019-12-01 22:11:18 +0100356 if (buf->bh_first.b_next == NULL) // buffer is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357 return NUL;
358
Bram Moolenaar285e3352018-04-18 23:01:13 +0200359 curr = buf->bh_first.b_next;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100360 c = curr->b_str[buf->bh_index];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361
362 if (advance)
363 {
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100364 if (curr->b_str[++buf->bh_index] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365 {
Bram Moolenaar285e3352018-04-18 23:01:13 +0200366 buf->bh_first.b_next = curr->b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367 vim_free(curr);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100368 buf->bh_index = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369 }
370 }
371 return c;
372}
373
374/*
Bram Moolenaar06811f32014-02-15 16:17:07 +0100375 * Prepare the read buffers for reading (if they contain something).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376 */
377 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100378start_stuff(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379{
Bram Moolenaar285e3352018-04-18 23:01:13 +0200380 if (readbuf1.bh_first.b_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381 {
Bram Moolenaar285e3352018-04-18 23:01:13 +0200382 readbuf1.bh_curr = &(readbuf1.bh_first);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100383 readbuf1.bh_space = 0;
384 }
Bram Moolenaar285e3352018-04-18 23:01:13 +0200385 if (readbuf2.bh_first.b_next != NULL)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100386 {
Bram Moolenaar285e3352018-04-18 23:01:13 +0200387 readbuf2.bh_curr = &(readbuf2.bh_first);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100388 readbuf2.bh_space = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389 }
390}
391
392/*
393 * Return TRUE if the stuff buffer is empty.
394 */
395 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100396stuff_empty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397{
Bram Moolenaar285e3352018-04-18 23:01:13 +0200398 return (readbuf1.bh_first.b_next == NULL
399 && readbuf2.bh_first.b_next == NULL);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100400}
401
Bram Moolenaar113e1072019-01-20 15:30:40 +0100402#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100403/*
404 * Return TRUE if readbuf1 is empty. There may still be redo characters in
405 * redbuf2.
406 */
407 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100408readbuf1_empty(void)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100409{
Bram Moolenaar285e3352018-04-18 23:01:13 +0200410 return (readbuf1.bh_first.b_next == NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100412#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413
414/*
415 * Set a typeahead character that won't be flushed.
416 */
417 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100418typeahead_noflush(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419{
420 typeahead_char = c;
421}
422
423/*
424 * Remove the contents of the stuff buffer and the mapped characters in the
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100425 * typeahead buffer (used in case of an error). If "flush_typeahead" is true,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426 * flush all typeahead characters (used when interrupted by a CTRL-C).
427 */
428 void
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200429flush_buffers(flush_buffers_T flush_typeahead)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430{
431 init_typebuf();
432
433 start_stuff();
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100434 while (read_readbuffers(TRUE) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435 ;
436
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200437 if (flush_typeahead == FLUSH_MINIMAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438 {
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200439 // remove mapped characters at the start only
440 typebuf.tb_off += typebuf.tb_maplen;
441 typebuf.tb_len -= typebuf.tb_maplen;
Bram Moolenaare49b4bb2020-03-11 13:01:40 +0100442#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
443 if (typebuf.tb_len == 0)
444 typebuf_was_filled = FALSE;
445#endif
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200446 }
447 else
448 {
449 // remove typeahead
450 if (flush_typeahead == FLUSH_INPUT)
451 // We have to get all characters, because we may delete the first
452 // part of an escape sequence. In an xterm we get one char at a
453 // time and we have to get them all.
454 while (inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 10L) != 0)
455 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456 typebuf.tb_off = MAXMAPLEN;
457 typebuf.tb_len = 0;
Bram Moolenaar4eb65312017-06-24 18:49:00 +0200458#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar30613902019-12-01 22:11:18 +0100459 // Reset the flag that text received from a client or from feedkeys()
460 // was inserted in the typeahead buffer.
Bram Moolenaar4eb65312017-06-24 18:49:00 +0200461 typebuf_was_filled = FALSE;
462#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464 typebuf.tb_maplen = 0;
465 typebuf.tb_silent = 0;
466 cmd_silent = FALSE;
467 typebuf.tb_no_abbr_cnt = 0;
Bram Moolenaarb8d732e2020-08-05 22:07:26 +0200468 if (++typebuf.tb_change_cnt == 0)
469 typebuf.tb_change_cnt = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470}
471
472/*
473 * The previous contents of the redo buffer is kept in old_redobuffer.
474 * This is used for the CTRL-O <.> command in insert mode.
475 */
476 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100477ResetRedobuff(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478{
479 if (!block_redo)
480 {
481 free_buff(&old_redobuff);
482 old_redobuff = redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200483 redobuff.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 }
485}
486
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100487/*
488 * Discard the contents of the redo buffer and restore the previous redo
489 * buffer.
490 */
491 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100492CancelRedo(void)
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100493{
494 if (!block_redo)
495 {
496 free_buff(&redobuff);
497 redobuff = old_redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200498 old_redobuff.bh_first.b_next = NULL;
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100499 start_stuff();
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100500 while (read_readbuffers(TRUE) != NUL)
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100501 ;
502 }
503}
504
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505/*
506 * Save redobuff and old_redobuff to save_redobuff and save_old_redobuff.
507 * Used before executing autocommands and user functions.
508 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 void
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200510saveRedobuff(save_redo_T *save_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511{
512 char_u *s;
513
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200514 save_redo->sr_redobuff = redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200515 redobuff.bh_first.b_next = NULL;
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200516 save_redo->sr_old_redobuff = old_redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200517 old_redobuff.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518
Bram Moolenaar30613902019-12-01 22:11:18 +0100519 // Make a copy, so that ":normal ." in a function works.
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200520 s = get_buffcont(&save_redo->sr_redobuff, FALSE);
521 if (s != NULL)
522 {
523 add_buff(&redobuff, s, -1L);
524 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 }
526}
527
528/*
529 * Restore redobuff and old_redobuff from save_redobuff and save_old_redobuff.
530 * Used after executing autocommands and user functions.
531 */
532 void
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200533restoreRedobuff(save_redo_T *save_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534{
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200535 free_buff(&redobuff);
536 redobuff = save_redo->sr_redobuff;
537 free_buff(&old_redobuff);
538 old_redobuff = save_redo->sr_old_redobuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540
541/*
542 * Append "s" to the redo buffer.
543 * K_SPECIAL and CSI should already have been escaped.
544 */
545 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100546AppendToRedobuff(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547{
548 if (!block_redo)
549 add_buff(&redobuff, s, -1L);
550}
551
552/*
553 * Append to Redo buffer literally, escaping special characters with CTRL-V.
554 * K_SPECIAL and CSI are escaped as well.
555 */
556 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100557AppendToRedobuffLit(
558 char_u *str,
Bram Moolenaar30613902019-12-01 22:11:18 +0100559 int len) // length of "str" or -1 for up to the NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560{
Bram Moolenaarebefac62005-12-28 22:39:57 +0000561 char_u *s = str;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562 int c;
563 char_u *start;
564
565 if (block_redo)
566 return;
567
Bram Moolenaarebefac62005-12-28 22:39:57 +0000568 while (len < 0 ? *s != NUL : s - str < len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100570 // Put a string of normal characters in the redo buffer (that's
571 // faster).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 start = s;
573 while (*s >= ' '
574#ifndef EBCDIC
Bram Moolenaar30613902019-12-01 22:11:18 +0100575 && *s < DEL // EBCDIC: all chars above space are normal
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576#endif
Bram Moolenaarebefac62005-12-28 22:39:57 +0000577 && (len < 0 || s - str < len))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 ++s;
579
Bram Moolenaar30613902019-12-01 22:11:18 +0100580 // Don't put '0' or '^' as last character, just in case a CTRL-D is
581 // typed next.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 if (*s == NUL && (s[-1] == '0' || s[-1] == '^'))
583 --s;
584 if (s > start)
585 add_buff(&redobuff, start, (long)(s - start));
586
Bram Moolenaarebefac62005-12-28 22:39:57 +0000587 if (*s == NUL || (len >= 0 && s - str >= len))
588 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589
Bram Moolenaar30613902019-12-01 22:11:18 +0100590 // Handle a special or multibyte character.
Bram Moolenaarebefac62005-12-28 22:39:57 +0000591 if (has_mbyte)
Bram Moolenaar30613902019-12-01 22:11:18 +0100592 // Handle composing chars separately.
Bram Moolenaarebefac62005-12-28 22:39:57 +0000593 c = mb_cptr2char_adv(&s);
594 else
Bram Moolenaarebefac62005-12-28 22:39:57 +0000595 c = *s++;
596 if (c < ' ' || c == DEL || (*s == NUL && (c == '0' || c == '^')))
597 add_char_buff(&redobuff, Ctrl_V);
598
Bram Moolenaar30613902019-12-01 22:11:18 +0100599 // CTRL-V '0' must be inserted as CTRL-V 048 (EBCDIC: xf0)
Bram Moolenaarebefac62005-12-28 22:39:57 +0000600 if (*s == NUL && c == '0')
601#ifdef EBCDIC
602 add_buff(&redobuff, (char_u *)"xf0", 3L);
603#else
604 add_buff(&redobuff, (char_u *)"048", 3L);
605#endif
606 else
607 add_char_buff(&redobuff, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 }
609}
610
611/*
612 * Append a character to the redo buffer.
613 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
614 */
615 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100616AppendCharToRedobuff(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617{
618 if (!block_redo)
619 add_char_buff(&redobuff, c);
620}
621
622/*
623 * Append a number to the redo buffer.
624 */
625 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100626AppendNumberToRedobuff(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627{
628 if (!block_redo)
629 add_num_buff(&redobuff, n);
630}
631
632/*
633 * Append string "s" to the stuff buffer.
634 * CSI and K_SPECIAL must already have been escaped.
635 */
636 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100637stuffReadbuff(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100639 add_buff(&readbuf1, s, -1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640}
641
Bram Moolenaar4f5ce332014-07-30 16:00:58 +0200642/*
643 * Append string "s" to the redo stuff buffer.
644 * CSI and K_SPECIAL must already have been escaped.
645 */
646 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100647stuffRedoReadbuff(char_u *s)
Bram Moolenaar4f5ce332014-07-30 16:00:58 +0200648{
649 add_buff(&readbuf2, s, -1L);
650}
651
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200652 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100653stuffReadbuffLen(char_u *s, long len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100655 add_buff(&readbuf1, s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656}
657
658#if defined(FEAT_EVAL) || defined(PROTO)
659/*
660 * Stuff "s" into the stuff buffer, leaving special key codes unmodified and
661 * escaping other K_SPECIAL and CSI bytes.
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200662 * Change CR, LF and ESC into a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 */
664 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100665stuffReadbuffSpec(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666{
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200667 int c;
668
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 while (*s != NUL)
670 {
671 if (*s == K_SPECIAL && s[1] != NUL && s[2] != NUL)
672 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100673 // Insert special key literally.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 stuffReadbuffLen(s, 3L);
675 s += 3;
676 }
677 else
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200678 {
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200679 c = mb_ptr2char_adv(&s);
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200680 if (c == CAR || c == NL || c == ESC)
681 c = ' ';
682 stuffcharReadbuff(c);
683 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 }
685}
686#endif
687
688/*
689 * Append a character to the stuff buffer.
690 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
691 */
692 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100693stuffcharReadbuff(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100695 add_char_buff(&readbuf1, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696}
697
698/*
699 * Append a number to the stuff buffer.
700 */
701 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100702stuffnumReadbuff(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100704 add_num_buff(&readbuf1, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705}
706
707/*
Bram Moolenaar11abd092020-05-01 14:26:37 +0200708 * Stuff a string into the typeahead buffer, such that edit() will insert it
709 * literally ("literally" TRUE) or interpret is as typed characters.
710 */
711 void
712stuffescaped(char_u *arg, int literally)
713{
714 int c;
715 char_u *start;
716
717 while (*arg != NUL)
718 {
719 // Stuff a sequence of normal ASCII characters, that's fast. Also
720 // stuff K_SPECIAL to get the effect of a special key when "literally"
721 // is TRUE.
722 start = arg;
723 while ((*arg >= ' '
724#ifndef EBCDIC
725 && *arg < DEL // EBCDIC: chars above space are normal
726#endif
727 )
728 || (*arg == K_SPECIAL && !literally))
729 ++arg;
730 if (arg > start)
731 stuffReadbuffLen(start, (long)(arg - start));
732
733 // stuff a single special character
734 if (*arg != NUL)
735 {
736 if (has_mbyte)
737 c = mb_cptr2char_adv(&arg);
738 else
739 c = *arg++;
740 if (literally && ((c < ' ' && c != TAB) || c == DEL))
741 stuffcharReadbuff(Ctrl_V);
742 stuffcharReadbuff(c);
743 }
744 }
745}
746
747/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 * Read a character from the redo buffer. Translates K_SPECIAL, CSI and
749 * multibyte characters.
750 * The redo buffer is left as it is.
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100751 * If init is TRUE, prepare for redo, return FAIL if nothing to redo, OK
752 * otherwise.
753 * If old is TRUE, use old_redobuff instead of redobuff.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 */
755 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100756read_redo(int init, int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100758 static buffblock_T *bp;
759 static char_u *p;
760 int c;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100761 int n;
762 char_u buf[MB_MAXBYTES + 1];
763 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764
765 if (init)
766 {
767 if (old_redo)
Bram Moolenaar285e3352018-04-18 23:01:13 +0200768 bp = old_redobuff.bh_first.b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 else
Bram Moolenaar285e3352018-04-18 23:01:13 +0200770 bp = redobuff.bh_first.b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 if (bp == NULL)
772 return FAIL;
773 p = bp->b_str;
774 return OK;
775 }
776 if ((c = *p) != NUL)
777 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100778 // Reverse the conversion done by add_char_buff()
779 // For a multi-byte character get all the bytes and return the
780 // converted character.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 if (has_mbyte && (c != K_SPECIAL || p[1] == KS_SPECIAL))
782 n = MB_BYTE2LEN_CHECK(c);
783 else
784 n = 1;
785 for (i = 0; ; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100787 if (c == K_SPECIAL) // special key or escaped K_SPECIAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 {
789 c = TO_SPECIAL(p[1], p[2]);
790 p += 2;
791 }
792#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +0100793 if (c == CSI) // escaped CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 p += 2;
795#endif
796 if (*++p == NUL && bp->b_next != NULL)
797 {
798 bp = bp->b_next;
799 p = bp->b_str;
800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 buf[i] = c;
Bram Moolenaar30613902019-12-01 22:11:18 +0100802 if (i == n - 1) // last byte of a character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 {
804 if (n != 1)
805 c = (*mb_ptr2char)(buf);
806 break;
807 }
808 c = *p;
Bram Moolenaar30613902019-12-01 22:11:18 +0100809 if (c == NUL) // cannot happen?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 }
812 }
813
814 return c;
815}
816
817/*
818 * Copy the rest of the redo buffer into the stuff buffer (in a slow way).
819 * If old_redo is TRUE, use old_redobuff instead of redobuff.
820 * The escaped K_SPECIAL and CSI are copied without translation.
821 */
822 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100823copy_redo(int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824{
825 int c;
826
827 while ((c = read_redo(FALSE, old_redo)) != NUL)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100828 add_char_buff(&readbuf2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829}
830
831/*
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100832 * Stuff the redo buffer into readbuf2.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 * Insert the redo count into the command.
834 * If "old_redo" is TRUE, the last but one command is repeated
835 * instead of the last command (inserting text). This is used for
836 * CTRL-O <.> in insert mode
837 *
838 * return FAIL for failure, OK otherwise
839 */
840 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100841start_redo(long count, int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842{
843 int c;
844
Bram Moolenaar30613902019-12-01 22:11:18 +0100845 // init the pointers; return if nothing to redo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 if (read_redo(TRUE, old_redo) == FAIL)
847 return FAIL;
848
849 c = read_redo(FALSE, old_redo);
850
Bram Moolenaar30613902019-12-01 22:11:18 +0100851 // copy the buffer name, if present
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 if (c == '"')
853 {
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100854 add_buff(&readbuf2, (char_u *)"\"", 1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 c = read_redo(FALSE, old_redo);
856
Bram Moolenaar30613902019-12-01 22:11:18 +0100857 // if a numbered buffer is used, increment the number
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 if (c >= '1' && c < '9')
859 ++c;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100860 add_char_buff(&readbuf2, c);
Bram Moolenaar833093b2018-05-23 21:53:52 +0200861
Bram Moolenaar30613902019-12-01 22:11:18 +0100862 // the expression register should be re-evaluated
Bram Moolenaar833093b2018-05-23 21:53:52 +0200863 if (c == '=')
864 {
865 add_char_buff(&readbuf2, CAR);
866 cmd_silent = TRUE;
867 }
868
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 c = read_redo(FALSE, old_redo);
870 }
871
Bram Moolenaar30613902019-12-01 22:11:18 +0100872 if (c == 'v') // redo Visual
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 {
874 VIsual = curwin->w_cursor;
875 VIsual_active = TRUE;
876 VIsual_select = FALSE;
877 VIsual_reselect = TRUE;
878 redo_VIsual_busy = TRUE;
879 c = read_redo(FALSE, old_redo);
880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881
Bram Moolenaar30613902019-12-01 22:11:18 +0100882 // try to enter the count (in place of a previous count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 if (count)
884 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100885 while (VIM_ISDIGIT(c)) // skip "old" count
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 c = read_redo(FALSE, old_redo);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100887 add_num_buff(&readbuf2, count);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 }
889
Bram Moolenaar30613902019-12-01 22:11:18 +0100890 // copy from the redo buffer into the stuff buffer
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100891 add_char_buff(&readbuf2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 copy_redo(old_redo);
893 return OK;
894}
895
896/*
897 * Repeat the last insert (R, o, O, a, A, i or I command) by stuffing
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100898 * the redo buffer into readbuf2.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 * return FAIL for failure, OK otherwise
900 */
901 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100902start_redo_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903{
904 int c;
905
906 if (read_redo(TRUE, FALSE) == FAIL)
907 return FAIL;
908 start_stuff();
909
Bram Moolenaar30613902019-12-01 22:11:18 +0100910 // skip the count and the command character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 while ((c = read_redo(FALSE, FALSE)) != NUL)
912 {
913 if (vim_strchr((char_u *)"AaIiRrOo", c) != NULL)
914 {
915 if (c == 'O' || c == 'o')
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100916 add_buff(&readbuf2, NL_STR, -1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 break;
918 }
919 }
920
Bram Moolenaar30613902019-12-01 22:11:18 +0100921 // copy the typed text from the redo buffer into the stuff buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 copy_redo(FALSE);
923 block_redo = TRUE;
924 return OK;
925}
926
927 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100928stop_redo_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929{
930 block_redo = FALSE;
931}
932
933/*
934 * Initialize typebuf.tb_buf to point to typebuf_init.
935 * alloc() cannot be used here: In out-of-memory situations it would
936 * be impossible to type anything.
937 */
938 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100939init_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940{
941 if (typebuf.tb_buf == NULL)
942 {
943 typebuf.tb_buf = typebuf_init;
944 typebuf.tb_noremap = noremapbuf_init;
945 typebuf.tb_buflen = TYPELEN_INIT;
946 typebuf.tb_len = 0;
Bram Moolenaard34f9b12017-04-08 18:41:13 +0200947 typebuf.tb_off = MAXMAPLEN + 4;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 typebuf.tb_change_cnt = 1;
949 }
950}
951
952/*
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200953 * Returns TRUE when keys cannot be remapped.
954 */
955 int
956noremap_keys(void)
957{
958 return KeyNoremap & (RM_NONE|RM_SCRIPT);
959}
960
961/*
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100962 * Insert a string in position 'offset' in the typeahead buffer (for "@r"
963 * and ":normal" command, vgetorpeek() and check_termcode()).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 *
965 * If noremap is REMAP_YES, new string can be mapped again.
966 * If noremap is REMAP_NONE, new string cannot be mapped again.
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000967 * If noremap is REMAP_SKIP, first char of new string cannot be mapped again,
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000968 * but abbreviations are allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 * If noremap is REMAP_SCRIPT, new string cannot be mapped again, except for
970 * script-local mappings.
971 * If noremap is > 0, that many characters of the new string cannot be mapped.
972 *
973 * If nottyped is TRUE, the string does not return KeyTyped (don't use when
974 * offset is non-zero!).
975 *
976 * If silent is TRUE, cmd_silent is set when the characters are obtained.
977 *
978 * return FAIL for failure, OK otherwise
979 */
980 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100981ins_typebuf(
982 char_u *str,
983 int noremap,
984 int offset,
985 int nottyped,
986 int silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987{
988 char_u *s1, *s2;
989 int newlen;
990 int addlen;
991 int i;
992 int newoff;
993 int val;
994 int nrm;
995
996 init_typebuf();
997 if (++typebuf.tb_change_cnt == 0)
998 typebuf.tb_change_cnt = 1;
Bram Moolenaard103ee72019-09-18 21:15:31 +0200999 state_no_longer_safe("ins_typebuf()");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000
1001 addlen = (int)STRLEN(str);
Bram Moolenaar4e86cba2007-05-06 11:56:32 +00001002
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 if (offset == 0 && addlen <= typebuf.tb_off)
1004 {
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001005 /*
1006 * Easy case: there is room in front of typebuf.tb_buf[typebuf.tb_off]
1007 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 typebuf.tb_off -= addlen;
1009 mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen);
1010 }
Bram Moolenaard34f9b12017-04-08 18:41:13 +02001011 else if (typebuf.tb_len == 0 && typebuf.tb_buflen
1012 >= addlen + 3 * (MAXMAPLEN + 4))
1013 {
1014 /*
1015 * Buffer is empty and string fits in the existing buffer.
1016 * Leave some space before and after, if possible.
1017 */
1018 typebuf.tb_off = (typebuf.tb_buflen - addlen - 3 * (MAXMAPLEN + 4)) / 2;
1019 mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen);
1020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 else
1022 {
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001023 int extra;
1024
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001025 /*
1026 * Need to allocate a new buffer.
Bram Moolenaard34f9b12017-04-08 18:41:13 +02001027 * In typebuf.tb_buf there must always be room for 3 * (MAXMAPLEN + 4)
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001028 * characters. We add some extra room to avoid having to allocate too
1029 * often.
1030 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 newoff = MAXMAPLEN + 4;
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001032 extra = addlen + newoff + 4 * (MAXMAPLEN + 4);
1033 if (typebuf.tb_len > 2147483647 - extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 {
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001035 // string is getting too long for a 32 bit int
Bram Moolenaar12f3c1b2021-12-05 21:46:34 +00001036 emsg(_(e_command_too_complex)); // also calls flush_buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 setcursor();
1038 return FAIL;
1039 }
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001040 newlen = typebuf.tb_len + extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 s1 = alloc(newlen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001042 if (s1 == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 return FAIL;
1044 s2 = alloc(newlen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001045 if (s2 == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 {
1047 vim_free(s1);
1048 return FAIL;
1049 }
1050 typebuf.tb_buflen = newlen;
1051
Bram Moolenaar30613902019-12-01 22:11:18 +01001052 // copy the old chars, before the insertion point
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 mch_memmove(s1 + newoff, typebuf.tb_buf + typebuf.tb_off,
1054 (size_t)offset);
Bram Moolenaar30613902019-12-01 22:11:18 +01001055 // copy the new chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 mch_memmove(s1 + newoff + offset, str, (size_t)addlen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001057 // copy the old chars, after the insertion point, including the NUL at
1058 // the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 mch_memmove(s1 + newoff + offset + addlen,
1060 typebuf.tb_buf + typebuf.tb_off + offset,
1061 (size_t)(typebuf.tb_len - offset + 1));
1062 if (typebuf.tb_buf != typebuf_init)
1063 vim_free(typebuf.tb_buf);
1064 typebuf.tb_buf = s1;
1065
1066 mch_memmove(s2 + newoff, typebuf.tb_noremap + typebuf.tb_off,
1067 (size_t)offset);
1068 mch_memmove(s2 + newoff + offset + addlen,
1069 typebuf.tb_noremap + typebuf.tb_off + offset,
1070 (size_t)(typebuf.tb_len - offset));
1071 if (typebuf.tb_noremap != noremapbuf_init)
1072 vim_free(typebuf.tb_noremap);
1073 typebuf.tb_noremap = s2;
1074
1075 typebuf.tb_off = newoff;
1076 }
1077 typebuf.tb_len += addlen;
1078
Bram Moolenaar30613902019-12-01 22:11:18 +01001079 // If noremap == REMAP_SCRIPT: do remap script-local mappings.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 if (noremap == REMAP_SCRIPT)
1081 val = RM_SCRIPT;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001082 else if (noremap == REMAP_SKIP)
1083 val = RM_ABBR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 else
1085 val = RM_NONE;
1086
1087 /*
1088 * Adjust typebuf.tb_noremap[] for the new characters:
1089 * If noremap == REMAP_NONE or REMAP_SCRIPT: new characters are
1090 * (sometimes) not remappable
1091 * If noremap == REMAP_YES: all the new characters are mappable
1092 * If noremap > 0: "noremap" characters are not remappable, the rest
1093 * mappable
1094 */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001095 if (noremap == REMAP_SKIP)
1096 nrm = 1;
1097 else if (noremap < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 nrm = addlen;
1099 else
1100 nrm = noremap;
1101 for (i = 0; i < addlen; ++i)
1102 typebuf.tb_noremap[typebuf.tb_off + i + offset] =
1103 (--nrm >= 0) ? val : RM_YES;
1104
Bram Moolenaar30613902019-12-01 22:11:18 +01001105 // tb_maplen and tb_silent only remember the length of mapped and/or
1106 // silent mappings at the start of the buffer, assuming that a mapped
1107 // sequence doesn't result in typed characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 if (nottyped || typebuf.tb_maplen > offset)
1109 typebuf.tb_maplen += addlen;
1110 if (silent || typebuf.tb_silent > offset)
1111 {
1112 typebuf.tb_silent += addlen;
1113 cmd_silent = TRUE;
1114 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001115 if (typebuf.tb_no_abbr_cnt && offset == 0) // and not used for abbrev.s
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 typebuf.tb_no_abbr_cnt += addlen;
1117
1118 return OK;
1119}
1120
1121/*
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001122 * Put character "c" back into the typeahead buffer.
1123 * Can be used for a character obtained by vgetc() that needs to be put back.
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00001124 * Uses cmd_silent, KeyTyped and KeyNoremap to restore the flags belonging to
1125 * the char.
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001126 * Returns the length of what was inserted.
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001127 */
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001128 int
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001129ins_char_typebuf(int c, int modifier)
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001130{
zeertzjq6cac7702022-01-04 18:01:21 +00001131 char_u buf[MB_MAXBYTES * 3 + 4];
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001132 int len = 0;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001133
1134 if (modifier != 0)
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001135 {
1136 buf[0] = K_SPECIAL;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001137 buf[1] = KS_MODIFIER;
1138 buf[2] = modifier;
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001139 buf[3] = NUL;
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001140 len = 3;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001141 }
1142 if (IS_SPECIAL(c))
1143 {
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001144 buf[len] = K_SPECIAL;
1145 buf[len + 1] = K_SECOND(c);
1146 buf[len + 2] = K_THIRD(c);
1147 buf[len + 3] = NUL;
1148 len += 3;
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001149 }
1150 else
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001151 {
zeertzjq6cac7702022-01-04 18:01:21 +00001152 char_u *p = buf + len;
1153 int char_len = (*mb_char2bytes)(c, p);
1154#ifdef FEAT_GUI
1155 int save_gui_in_use = gui.in_use;
1156
1157 gui.in_use = FALSE;
1158#endif
1159 // if the character contains CSI or K_SPECIAL bytes they need escaping
1160 len += fix_input_buffer(p, char_len);
1161#ifdef FEAT_GUI
1162 gui.in_use = save_gui_in_use;
1163#endif
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001164 }
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00001165 (void)ins_typebuf(buf, KeyNoremap, 0, !KeyTyped, cmd_silent);
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001166 return len;
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001167}
1168
1169/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 * Return TRUE if the typeahead buffer was changed (while waiting for a
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001171 * character to arrive). Happens when a message was received from a client or
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001172 * from feedkeys().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 * But check in a more generic way to avoid trouble: When "typebuf.tb_buf"
1174 * changed it was reallocated and the old pointer can no longer be used.
1175 * Or "typebuf.tb_off" may have been changed and we would overwrite characters
1176 * that was just added.
1177 */
1178 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001179typebuf_changed(
Bram Moolenaar30613902019-12-01 22:11:18 +01001180 int tb_change_cnt) // old value of typebuf.tb_change_cnt
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181{
1182 return (tb_change_cnt != 0 && (typebuf.tb_change_cnt != tb_change_cnt
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001183#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
1184 || typebuf_was_filled
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185#endif
1186 ));
1187}
1188
1189/*
1190 * Return TRUE if there are no characters in the typeahead buffer that have
1191 * not been typed (result from a mapping or come from ":normal").
1192 */
1193 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001194typebuf_typed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195{
1196 return typebuf.tb_maplen == 0;
1197}
1198
1199/*
1200 * Return the number of characters that are mapped (or not typed).
1201 */
1202 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001203typebuf_maplen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204{
1205 return typebuf.tb_maplen;
1206}
1207
1208/*
1209 * remove "len" characters from typebuf.tb_buf[typebuf.tb_off + offset]
1210 */
1211 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001212del_typebuf(int len, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213{
1214 int i;
1215
1216 if (len == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01001217 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218
1219 typebuf.tb_len -= len;
1220
1221 /*
1222 * Easy case: Just increase typebuf.tb_off.
1223 */
1224 if (offset == 0 && typebuf.tb_buflen - (typebuf.tb_off + len)
1225 >= 3 * MAXMAPLEN + 3)
1226 typebuf.tb_off += len;
1227 /*
1228 * Have to move the characters in typebuf.tb_buf[] and typebuf.tb_noremap[]
1229 */
1230 else
1231 {
1232 i = typebuf.tb_off + offset;
1233 /*
1234 * Leave some extra room at the end to avoid reallocation.
1235 */
1236 if (typebuf.tb_off > MAXMAPLEN)
1237 {
1238 mch_memmove(typebuf.tb_buf + MAXMAPLEN,
1239 typebuf.tb_buf + typebuf.tb_off, (size_t)offset);
1240 mch_memmove(typebuf.tb_noremap + MAXMAPLEN,
1241 typebuf.tb_noremap + typebuf.tb_off, (size_t)offset);
1242 typebuf.tb_off = MAXMAPLEN;
1243 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001244 // adjust typebuf.tb_buf (include the NUL at the end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset,
1246 typebuf.tb_buf + i + len,
1247 (size_t)(typebuf.tb_len - offset + 1));
Bram Moolenaar30613902019-12-01 22:11:18 +01001248 // adjust typebuf.tb_noremap[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 mch_memmove(typebuf.tb_noremap + typebuf.tb_off + offset,
1250 typebuf.tb_noremap + i + len,
1251 (size_t)(typebuf.tb_len - offset));
1252 }
1253
Bram Moolenaar30613902019-12-01 22:11:18 +01001254 if (typebuf.tb_maplen > offset) // adjust tb_maplen
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 {
1256 if (typebuf.tb_maplen < offset + len)
1257 typebuf.tb_maplen = offset;
1258 else
1259 typebuf.tb_maplen -= len;
1260 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001261 if (typebuf.tb_silent > offset) // adjust tb_silent
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 {
1263 if (typebuf.tb_silent < offset + len)
1264 typebuf.tb_silent = offset;
1265 else
1266 typebuf.tb_silent -= len;
1267 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001268 if (typebuf.tb_no_abbr_cnt > offset) // adjust tb_no_abbr_cnt
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 {
1270 if (typebuf.tb_no_abbr_cnt < offset + len)
1271 typebuf.tb_no_abbr_cnt = offset;
1272 else
1273 typebuf.tb_no_abbr_cnt -= len;
1274 }
1275
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001276#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar30613902019-12-01 22:11:18 +01001277 // Reset the flag that text received from a client or from feedkeys()
1278 // was inserted in the typeahead buffer.
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001279 typebuf_was_filled = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280#endif
1281 if (++typebuf.tb_change_cnt == 0)
1282 typebuf.tb_change_cnt = 1;
1283}
1284
1285/*
1286 * Write typed characters to script file.
1287 * If recording is on put the character in the recordbuffer.
1288 */
1289 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001290gotchars(char_u *chars, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291{
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001292 char_u *s = chars;
1293 int i;
1294 static char_u buf[4];
1295 static int buflen = 0;
1296 int todo = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297
Bram Moolenaar4e86cba2007-05-06 11:56:32 +00001298 while (todo--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 {
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001300 buf[buflen++] = *s++;
1301
1302 // When receiving a special key sequence, store it until we have all
1303 // the bytes and we can decide what to do with it.
1304 if (buflen == 1 && buf[0] == K_SPECIAL)
1305 continue;
1306 if (buflen == 2)
1307 continue;
1308 if (buflen == 3 && buf[1] == KS_EXTRA
1309 && (buf[2] == KE_FOCUSGAINED || buf[2] == KE_FOCUSLOST))
1310 {
1311 // Drop K_FOCUSGAINED and K_FOCUSLOST, they are not useful in a
1312 // recording.
1313 buflen = 0;
1314 continue;
1315 }
1316
Bram Moolenaar30613902019-12-01 22:11:18 +01001317 // Handle one byte at a time; no translation to be done.
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001318 for (i = 0; i < buflen; ++i)
1319 updatescript(buf[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001321 if (reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 {
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001323 buf[buflen] = NUL;
1324 add_buff(&recordbuff, buf, (long)buflen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001325 // remember how many chars were last recorded
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001326 last_recorded_len += buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 }
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001328 buflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 }
1330 may_sync_undo();
1331
1332#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01001333 // output "debug mode" message next time in debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 debug_did_msg = FALSE;
1335#endif
1336
Bram Moolenaar30613902019-12-01 22:11:18 +01001337 // Since characters have been typed, consider the following to be in
1338 // another mapping. Search string will be kept in history.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 ++maptick;
1340}
1341
1342/*
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001343 * Undo the last gotchars() for "len" bytes. To be used when putting a typed
1344 * character back into the typeahead buffer, thus gotchars() will be called
1345 * again.
1346 * Only affects recorded characters.
1347 */
1348 void
1349ungetchars(int len)
1350{
1351 if (reg_recording != 0)
1352 {
1353 delete_buff_tail(&recordbuff, len);
1354 last_recorded_len -= len;
1355 }
1356}
1357
1358/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 * Sync undo. Called when typed characters are obtained from the typeahead
1360 * buffer, or when a menu is used.
1361 * Do not sync:
1362 * - In Insert mode, unless cursor key has been used.
1363 * - While reading a script file.
1364 * - When no_u_sync is non-zero.
1365 */
1366 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001367may_sync_undo(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368{
1369 if ((!(State & (INSERT + CMDLINE)) || arrow_used)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001370 && scriptin[curscript] == NULL)
1371 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372}
1373
1374/*
1375 * Make "typebuf" empty and allocate new buffers.
1376 * Returns FAIL when out of memory.
1377 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001378 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001379alloc_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380{
1381 typebuf.tb_buf = alloc(TYPELEN_INIT);
1382 typebuf.tb_noremap = alloc(TYPELEN_INIT);
1383 if (typebuf.tb_buf == NULL || typebuf.tb_noremap == NULL)
1384 {
1385 free_typebuf();
1386 return FAIL;
1387 }
1388 typebuf.tb_buflen = TYPELEN_INIT;
Bram Moolenaar30613902019-12-01 22:11:18 +01001389 typebuf.tb_off = MAXMAPLEN + 4; // can insert without realloc
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 typebuf.tb_len = 0;
1391 typebuf.tb_maplen = 0;
1392 typebuf.tb_silent = 0;
1393 typebuf.tb_no_abbr_cnt = 0;
1394 if (++typebuf.tb_change_cnt == 0)
1395 typebuf.tb_change_cnt = 1;
Bram Moolenaare49b4bb2020-03-11 13:01:40 +01001396#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
1397 typebuf_was_filled = FALSE;
1398#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 return OK;
1400}
1401
1402/*
1403 * Free the buffers of "typebuf".
1404 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001405 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001406free_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407{
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001408 if (typebuf.tb_buf == typebuf_init)
Bram Moolenaar95f09602016-11-10 20:01:45 +01001409 internal_error("Free typebuf 1");
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001410 else
Bram Moolenaar0f1c6702019-09-28 15:24:00 +02001411 VIM_CLEAR(typebuf.tb_buf);
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00001412 if (typebuf.tb_noremap == noremapbuf_init)
Bram Moolenaar95f09602016-11-10 20:01:45 +01001413 internal_error("Free typebuf 2");
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001414 else
Bram Moolenaar0f1c6702019-09-28 15:24:00 +02001415 VIM_CLEAR(typebuf.tb_noremap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416}
1417
1418/*
1419 * When doing ":so! file", the current typeahead needs to be saved, and
1420 * restored when "file" has been read completely.
1421 */
1422static typebuf_T saved_typebuf[NSCRIPT];
1423
1424 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001425save_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426{
1427 init_typebuf();
1428 saved_typebuf[curscript] = typebuf;
Bram Moolenaar30613902019-12-01 22:11:18 +01001429 // If out of memory: restore typebuf and close file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 if (alloc_typebuf() == FAIL)
1431 {
1432 closescript();
1433 return FAIL;
1434 }
1435 return OK;
1436}
1437
Bram Moolenaar30613902019-12-01 22:11:18 +01001438static int old_char = -1; // character put back by vungetc()
1439static int old_mod_mask; // mod_mask for ungotten character
1440static int old_mouse_row; // mouse_row related to old_char
1441static int old_mouse_col; // mouse_col related to old_char
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001442
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443/*
1444 * Save all three kinds of typeahead, so that the user must type at a prompt.
1445 */
1446 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001447save_typeahead(tasave_T *tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448{
1449 tp->save_typebuf = typebuf;
1450 tp->typebuf_valid = (alloc_typebuf() == OK);
1451 if (!tp->typebuf_valid)
1452 typebuf = tp->save_typebuf;
1453
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001454 tp->old_char = old_char;
1455 tp->old_mod_mask = old_mod_mask;
1456 old_char = -1;
1457
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001458 tp->save_readbuf1 = readbuf1;
Bram Moolenaar285e3352018-04-18 23:01:13 +02001459 readbuf1.bh_first.b_next = NULL;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001460 tp->save_readbuf2 = readbuf2;
Bram Moolenaar285e3352018-04-18 23:01:13 +02001461 readbuf2.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462# ifdef USE_INPUT_BUF
1463 tp->save_inputbuf = get_input_buf();
1464# endif
1465}
1466
1467/*
1468 * Restore the typeahead to what it was before calling save_typeahead().
1469 * The allocated memory is freed, can only be called once!
Bram Moolenaarc41badb2021-06-07 22:04:52 +02001470 * When "overwrite" is FALSE input typed later is kept.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 */
1472 void
Bram Moolenaarc41badb2021-06-07 22:04:52 +02001473restore_typeahead(tasave_T *tp, int overwrite UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474{
1475 if (tp->typebuf_valid)
1476 {
1477 free_typebuf();
1478 typebuf = tp->save_typebuf;
1479 }
1480
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001481 old_char = tp->old_char;
1482 old_mod_mask = tp->old_mod_mask;
1483
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001484 free_buff(&readbuf1);
1485 readbuf1 = tp->save_readbuf1;
1486 free_buff(&readbuf2);
1487 readbuf2 = tp->save_readbuf2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488# ifdef USE_INPUT_BUF
Bram Moolenaarc41badb2021-06-07 22:04:52 +02001489 set_input_buf(tp->save_inputbuf, overwrite);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490# endif
1491}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492
1493/*
1494 * Open a new script file for the ":source!" command.
1495 */
1496 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001497openscript(
1498 char_u *name,
Bram Moolenaar30613902019-12-01 22:11:18 +01001499 int directly) // when TRUE execute directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500{
1501 if (curscript + 1 == NSCRIPT)
1502 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001503 emsg(_(e_scripts_nested_too_deep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 return;
1505 }
Bram Moolenaar53575522019-05-22 22:38:25 +02001506
1507 // Disallow sourcing a file in the sandbox, the commands would be executed
1508 // later, possibly outside of the sandbox.
1509 if (check_secure())
1510 return;
1511
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001512#ifdef FEAT_EVAL
1513 if (ignore_script)
Bram Moolenaar30613902019-12-01 22:11:18 +01001514 // Not reading from script, also don't open one. Warning message?
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001515 return;
1516#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517
Bram Moolenaar30613902019-12-01 22:11:18 +01001518 if (scriptin[curscript] != NULL) // already reading script
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 ++curscript;
Bram Moolenaar30613902019-12-01 22:11:18 +01001520 // use NameBuff for expanded name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 expand_env(name, NameBuff, MAXPATHL);
1522 if ((scriptin[curscript] = mch_fopen((char *)NameBuff, READBIN)) == NULL)
1523 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001524 semsg(_(e_cant_open_file_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 if (curscript)
1526 --curscript;
1527 return;
1528 }
1529 if (save_typebuf() == FAIL)
1530 return;
1531
1532 /*
1533 * Execute the commands from the file right now when using ":source!"
1534 * after ":global" or ":argdo" or in a loop. Also when another command
1535 * follows. This means the display won't be updated. Don't do this
1536 * always, "make test" would fail.
1537 */
1538 if (directly)
1539 {
1540 oparg_T oa;
1541 int oldcurscript;
1542 int save_State = State;
1543 int save_restart_edit = restart_edit;
1544 int save_insertmode = p_im;
1545 int save_finish_op = finish_op;
1546 int save_msg_scroll = msg_scroll;
1547
1548 State = NORMAL;
Bram Moolenaar30613902019-12-01 22:11:18 +01001549 msg_scroll = FALSE; // no msg scrolling in Normal mode
1550 restart_edit = 0; // don't go to Insert mode
1551 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 clear_oparg(&oa);
1553 finish_op = FALSE;
1554
1555 oldcurscript = curscript;
1556 do
1557 {
Bram Moolenaar386b43e2019-05-19 21:57:11 +02001558 update_topline_cursor(); // update cursor position and topline
1559 normal_cmd(&oa, FALSE); // execute one command
Bram Moolenaarae97b942020-07-09 19:16:35 +02001560 (void)vpeekc(); // check for end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 }
1562 while (scriptin[oldcurscript] != NULL);
1563
1564 State = save_State;
1565 msg_scroll = save_msg_scroll;
1566 restart_edit = save_restart_edit;
1567 p_im = save_insertmode;
1568 finish_op = save_finish_op;
1569 }
1570}
1571
1572/*
1573 * Close the currently active input script.
1574 */
1575 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001576closescript(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577{
1578 free_typebuf();
1579 typebuf = saved_typebuf[curscript];
1580
1581 fclose(scriptin[curscript]);
1582 scriptin[curscript] = NULL;
1583 if (curscript > 0)
1584 --curscript;
1585}
1586
Bram Moolenaarea408852005-06-25 22:49:46 +00001587#if defined(EXITFREE) || defined(PROTO)
1588 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001589close_all_scripts(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00001590{
1591 while (scriptin[0] != NULL)
1592 closescript();
1593}
1594#endif
1595
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596/*
1597 * Return TRUE when reading keys from a script file.
1598 */
1599 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001600using_script(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601{
1602 return scriptin[curscript] != NULL;
1603}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604
1605/*
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001606 * This function is called just before doing a blocking wait. Thus after
1607 * waiting 'updatetime' for a character to arrive.
1608 */
1609 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001610before_blocking(void)
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001611{
1612 updatescript(0);
1613#ifdef FEAT_EVAL
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001614 if (may_garbage_collect)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001615 garbage_collect(FALSE);
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001616#endif
1617}
1618
1619/*
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001620 * updatescript() is called when a character can be written into the script file
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 * or when we have waited some time for a character (c == 0)
1622 *
1623 * All the changed memfiles are synced if c == 0 or when the number of typed
1624 * characters reaches 'updatecount' and 'updatecount' is non-zero.
1625 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001626 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001627updatescript(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628{
1629 static int count = 0;
1630
1631 if (c && scriptout)
1632 putc(c, scriptout);
1633 if (c == 0 || (p_uc > 0 && ++count >= p_uc))
1634 {
1635 ml_sync_all(c == 0, TRUE);
1636 count = 0;
1637 }
1638}
1639
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640/*
Bram Moolenaar975a8802020-06-06 22:36:24 +02001641 * Convert "c" plus "modifiers" to merge the effect of modifyOtherKeys into the
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001642 * character.
1643 */
1644 int
Bram Moolenaar975a8802020-06-06 22:36:24 +02001645merge_modifyOtherKeys(int c_arg, int *modifiers)
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001646{
1647 int c = c_arg;
1648
Bram Moolenaar975a8802020-06-06 22:36:24 +02001649 if (*modifiers & MOD_MASK_CTRL)
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001650 {
1651 if ((c >= '`' && c <= 0x7f) || (c >= '@' && c <= '_'))
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001652 c &= 0x1f;
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001653 else if (c == '6')
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001654 // CTRL-6 is equivalent to CTRL-^
1655 c = 0x1e;
Bram Moolenaarf4ae6b22020-05-30 19:52:46 +02001656#ifdef FEAT_GUI_GTK
1657 // These mappings look arbitrary at the first glance, but in fact
1658 // resemble quite exactly the behaviour of the GTK+ 1.2 GUI on my
1659 // machine. The only difference is BS vs. DEL for CTRL-8 (makes
1660 // more sense and is consistent with usual terminal behaviour).
1661 else if (c == '2')
1662 c = NUL;
1663 else if (c >= '3' && c <= '7')
1664 c = c ^ 0x28;
1665 else if (c == '8')
1666 c = BS;
1667 else if (c == '?')
1668 c = DEL;
1669#endif
1670 if (c != c_arg)
Bram Moolenaar975a8802020-06-06 22:36:24 +02001671 *modifiers &= ~MOD_MASK_CTRL;
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001672 }
Bram Moolenaar975a8802020-06-06 22:36:24 +02001673 if ((*modifiers & (MOD_MASK_META | MOD_MASK_ALT))
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001674 && c >= 0 && c <= 127)
1675 {
1676 c += 0x80;
Bram Moolenaar975a8802020-06-06 22:36:24 +02001677 *modifiers &= ~(MOD_MASK_META|MOD_MASK_ALT);
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001678 }
1679 return c;
1680}
1681
1682/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 * Get the next input character.
1684 * Can return a special key or a multi-byte character.
1685 * Can return NUL when called recursively, use safe_vgetc() if that's not
1686 * wanted.
1687 * This translates escaped K_SPECIAL and CSI bytes to a K_SPECIAL or CSI byte.
1688 * Collects the bytes of a multibyte character into the whole character.
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00001689 * Returns the modifiers in the global "mod_mask".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 */
1691 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001692vgetc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693{
1694 int c, c2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001696 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001699#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01001700 // Do garbage collection when garbagecollect() was called previously and
1701 // we are now at the toplevel.
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001702 if (may_garbage_collect && want_garbage_collect)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001703 garbage_collect(FALSE);
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001704#endif
1705
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 /*
1707 * If a character was put back with vungetc, it was already processed.
1708 * Return it directly.
1709 */
1710 if (old_char != -1)
1711 {
1712 c = old_char;
1713 old_char = -1;
1714 mod_mask = old_mod_mask;
Bram Moolenaarb8978712013-03-16 21:42:16 +01001715 mouse_row = old_mouse_row;
1716 mouse_col = old_mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001718 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 {
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001720 mod_mask = 0;
1721 vgetc_mod_mask = 0;
1722 vgetc_char = 0;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001723 last_recorded_len = 0;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001724
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001725 for (;;) // this is done twice if there are modifiers
1726 {
1727 int did_inc = FALSE;
Bram Moolenaar2455c4e2015-09-15 18:29:39 +02001728
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001729 if (mod_mask
Bram Moolenaar39719052017-09-05 22:20:46 +02001730#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001731 || im_is_preediting()
Bram Moolenaar39719052017-09-05 22:20:46 +02001732#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001733#if defined(FEAT_PROP_POPUP)
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001734 || popup_no_mapping()
1735#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001736 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 {
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001738 // no mapping after modifier has been read
1739 ++no_mapping;
1740 ++allow_keys;
1741 did_inc = TRUE; // mod_mask may change value
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 }
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001743 c = vgetorpeek(TRUE);
1744 if (did_inc)
1745 {
1746 --no_mapping;
1747 --allow_keys;
1748 }
1749
1750 // Get two extra bytes for special keys
1751 if (c == K_SPECIAL
1752#ifdef FEAT_GUI
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001753 || (c == CSI)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001754#endif
1755 )
1756 {
1757 int save_allow_keys = allow_keys;
1758
1759 ++no_mapping;
1760 allow_keys = 0; // make sure BS is not found
1761 c2 = vgetorpeek(TRUE); // no mapping for these chars
1762 c = vgetorpeek(TRUE);
1763 --no_mapping;
1764 allow_keys = save_allow_keys;
1765 if (c2 == KS_MODIFIER)
1766 {
1767 mod_mask = c;
1768 continue;
1769 }
1770 c = TO_SPECIAL(c2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771
Bram Moolenaar4f974752019-02-17 17:44:42 +01001772#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001773 // Handle K_TEAROFF here, the caller of vgetc() doesn't need to
1774 // know that a menu was torn off
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001775 if (
1776# ifdef VIMDLL
1777 gui.in_use &&
1778# endif
1779 c == K_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 {
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001781 char_u name[200];
1782 int i;
1783
1784 // get menu path, it ends with a <CR>
1785 for (i = 0; (c = vgetorpeek(TRUE)) != '\r'; )
1786 {
1787 name[i] = c;
1788 if (i < 199)
1789 ++i;
1790 }
1791 name[i] = NUL;
1792 gui_make_tearoff(name);
1793 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795#endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001796#if defined(FEAT_GUI) && defined(FEAT_GUI_GTK) && defined(FEAT_MENU)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001797 // GTK: <F10> normally selects the menu, but it's passed until
1798 // here to allow mapping it. Intercept and invoke the GTK
1799 // behavior if it's not mapped.
1800 if (c == K_F10 && gui.menubar != NULL)
1801 {
1802 gtk_menu_shell_select_first(
1803 GTK_MENU_SHELL(gui.menubar), FALSE);
1804 continue;
1805 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001806#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807#ifdef FEAT_GUI
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001808 // Handle focus event here, so that the caller doesn't need to
1809 // know about it. Return K_IGNORE so that we loop once (needed
1810 // if 'lazyredraw' is set).
1811 if (c == K_FOCUSGAINED || c == K_FOCUSLOST)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001812 {
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001813 ui_focus_change(c == K_FOCUSGAINED);
1814 c = K_IGNORE;
Bram Moolenaara9dd2d32019-04-29 21:58:41 +02001815 }
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001816
1817 // Translate K_CSI to CSI. The special key is only used to
1818 // avoid it being recognized as the start of a special key.
1819 if (c == K_CSI)
1820 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001822 }
1823 // a keypad or special function key was not mapped, use it like
1824 // its ASCII equivalent
1825 switch (c)
1826 {
1827 case K_KPLUS: c = '+'; break;
1828 case K_KMINUS: c = '-'; break;
1829 case K_KDIVIDE: c = '/'; break;
1830 case K_KMULTIPLY: c = '*'; break;
1831 case K_KENTER: c = CAR; break;
1832 case K_KPOINT:
Bram Moolenaar4f974752019-02-17 17:44:42 +01001833#ifdef MSWIN
Bram Moolenaar8e85db02018-07-27 23:16:51 +02001834 // Can be either '.' or a ',',
1835 // depending on the type of keypad.
1836 c = MapVirtualKey(VK_DECIMAL, 2); break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001837#else
Bram Moolenaar8e85db02018-07-27 23:16:51 +02001838 c = '.'; break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001839#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001840 case K_K0: c = '0'; break;
1841 case K_K1: c = '1'; break;
1842 case K_K2: c = '2'; break;
1843 case K_K3: c = '3'; break;
1844 case K_K4: c = '4'; break;
1845 case K_K5: c = '5'; break;
1846 case K_K6: c = '6'; break;
1847 case K_K7: c = '7'; break;
1848 case K_K8: c = '8'; break;
1849 case K_K9: c = '9'; break;
Bram Moolenaara88d9682005-03-25 21:45:43 +00001850
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001851 case K_XHOME:
1852 case K_ZHOME: if (mod_mask == MOD_MASK_SHIFT)
Bram Moolenaara88d9682005-03-25 21:45:43 +00001853 {
1854 c = K_S_HOME;
1855 mod_mask = 0;
1856 }
1857 else if (mod_mask == MOD_MASK_CTRL)
1858 {
1859 c = K_C_HOME;
1860 mod_mask = 0;
1861 }
1862 else
1863 c = K_HOME;
1864 break;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001865 case K_XEND:
1866 case K_ZEND: if (mod_mask == MOD_MASK_SHIFT)
Bram Moolenaara88d9682005-03-25 21:45:43 +00001867 {
1868 c = K_S_END;
1869 mod_mask = 0;
1870 }
1871 else if (mod_mask == MOD_MASK_CTRL)
1872 {
1873 c = K_C_END;
1874 mod_mask = 0;
1875 }
1876 else
1877 c = K_END;
1878 break;
1879
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001880 case K_XUP: c = K_UP; break;
1881 case K_XDOWN: c = K_DOWN; break;
1882 case K_XLEFT: c = K_LEFT; break;
1883 case K_XRIGHT: c = K_RIGHT; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001886 // For a multi-byte character get all the bytes and return the
1887 // converted character.
1888 // Note: This will loop until enough bytes are received!
1889 if (has_mbyte && (n = MB_BYTE2LEN_CHECK(c)) > 1)
1890 {
1891 ++no_mapping;
1892 buf[0] = c;
1893 for (i = 1; i < n; ++i)
1894 {
1895 buf[i] = vgetorpeek(TRUE);
1896 if (buf[i] == K_SPECIAL
1897#ifdef FEAT_GUI
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001898 || (buf[i] == CSI)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001899#endif
1900 )
1901 {
1902 // Must be a K_SPECIAL - KS_SPECIAL - KE_FILLER
1903 // sequence, which represents a K_SPECIAL (0x80),
1904 // or a CSI - KS_EXTRA - KE_CSI sequence, which
1905 // represents a CSI (0x9B),
1906 // or a K_SPECIAL - KS_EXTRA - KE_CSI, which is CSI
1907 // too.
1908 c = vgetorpeek(TRUE);
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01001909 if (vgetorpeek(TRUE) == KE_CSI && c == KS_EXTRA)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001910 buf[i] = CSI;
1911 }
1912 }
1913 --no_mapping;
1914 c = (*mb_ptr2char)(buf);
1915 }
1916
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02001917 if (vgetc_char == 0)
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001918 {
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001919 vgetc_mod_mask = mod_mask;
1920 vgetc_char = c;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001921 }
Bram Moolenaar00eab7f2019-10-10 21:49:28 +02001922
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001923 break;
1924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001926
1927#ifdef FEAT_EVAL
1928 /*
1929 * In the main loop "may_garbage_collect" can be set to do garbage
1930 * collection in the first next vgetc(). It's disabled after that to
1931 * avoid internally used Lists and Dicts to be freed.
1932 */
1933 may_garbage_collect = FALSE;
1934#endif
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001935
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001936#ifdef FEAT_BEVAL_TERM
Bram Moolenaarc2f50542019-07-05 23:24:56 +02001937 if (c != K_MOUSEMOVE && c != K_IGNORE && c != K_CURSORHOLD)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001938 {
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001939 // Don't trigger 'balloonexpr' unless only the mouse was moved.
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001940 bevalexpr_due_set = FALSE;
1941 ui_remove_balloon();
1942 }
1943#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001944#ifdef FEAT_PROP_POPUP
Bram Moolenaar4934ad02020-09-28 22:29:58 +02001945 // Only filter keys that do not come from ":normal". Keys from feedkeys()
1946 // are filtered.
1947 if ((!ex_normal_busy || in_feedkeys) && popup_do_filter(c))
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02001948 {
1949 if (c == Ctrl_C)
1950 got_int = FALSE; // avoid looping
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001951 c = K_IGNORE;
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02001952 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001953#endif
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001954
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001955 // Need to process the character before we know it's safe to do something
1956 // else.
1957 if (c != K_IGNORE)
Bram Moolenaard103ee72019-09-18 21:15:31 +02001958 state_no_longer_safe("key typed");
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001959
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001960 return c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961}
1962
1963/*
1964 * Like vgetc(), but never return a NUL when called recursively, get a key
1965 * directly from the user (ignoring typeahead).
1966 */
1967 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001968safe_vgetc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969{
1970 int c;
1971
1972 c = vgetc();
1973 if (c == NUL)
1974 c = get_keystroke();
1975 return c;
1976}
1977
1978/*
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001979 * Like safe_vgetc(), but loop to handle K_IGNORE.
1980 * Also ignore scrollbar events.
1981 */
1982 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001983plain_vgetc(void)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001984{
1985 int c;
1986
1987 do
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001988 c = safe_vgetc();
Bram Moolenaar3a006592021-05-05 19:58:17 +02001989 while (c == K_IGNORE
1990 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
1991 || c == K_MOUSEMOVE);
Bram Moolenaarec2da362017-01-21 20:04:22 +01001992
1993 if (c == K_PS)
Bram Moolenaar30613902019-12-01 22:11:18 +01001994 // Only handle the first pasted character. Drop the rest, since we
1995 // don't know what to do with it.
Bram Moolenaarec2da362017-01-21 20:04:22 +01001996 c = bracketed_paste(PASTE_ONE_CHAR, FALSE, NULL);
1997
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001998 return c;
1999}
2000
2001/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 * Check if a character is available, such that vgetc() will not block.
2003 * If the next character is a special character or multi-byte, the returned
2004 * character is not valid!.
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02002005 * Returns NUL if no character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 */
2007 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002008vpeekc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009{
2010 if (old_char != -1)
2011 return old_char;
2012 return vgetorpeek(FALSE);
2013}
2014
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01002015#if defined(FEAT_TERMRESPONSE) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016/*
2017 * Like vpeekc(), but don't allow mapping. Do allow checking for terminal
2018 * codes.
2019 */
2020 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002021vpeekc_nomap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022{
2023 int c;
2024
2025 ++no_mapping;
2026 ++allow_keys;
2027 c = vpeekc();
2028 --no_mapping;
2029 --allow_keys;
2030 return c;
2031}
2032#endif
2033
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034/*
2035 * Check if any character is available, also half an escape sequence.
2036 * Trick: when no typeahead found, but there is something in the typeahead
2037 * buffer, it must be an ESC that is recognized as the start of a key code.
2038 */
2039 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002040vpeekc_any(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041{
2042 int c;
2043
2044 c = vpeekc();
2045 if (c == NUL && typebuf.tb_len > 0)
2046 c = ESC;
2047 return c;
2048}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049
2050/*
2051 * Call vpeekc() without causing anything to be mapped.
2052 * Return TRUE if a character is available, FALSE otherwise.
2053 */
2054 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002055char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056{
2057 int retval;
2058
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01002059#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01002060 // When test_override("char_avail", 1) was called pretend there is no
2061 // typeahead.
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01002062 if (disable_char_avail_for_testing)
2063 return FALSE;
2064#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 ++no_mapping;
2066 retval = vpeekc();
2067 --no_mapping;
2068 return (retval != NUL);
2069}
2070
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002071#if defined(FEAT_EVAL) || defined(PROTO)
2072/*
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002073 * "getchar()" and "getcharstr()" functions
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002074 */
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002075 static void
2076getchar_common(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002077{
2078 varnumber_T n;
2079 int error = FALSE;
2080
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002081 if (in_vim9script() && check_for_opt_bool_arg(argvars, 0) == FAIL)
2082 return;
2083
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002084#ifdef MESSAGE_QUEUE
2085 // vpeekc() used to check for messages, but that caused problems, invoking
2086 // a callback where it was not expected. Some plugins use getchar(1) in a
2087 // loop to await a message, therefore make sure we check for messages here.
2088 parse_queued_messages();
2089#endif
2090
Bram Moolenaar30613902019-12-01 22:11:18 +01002091 // Position the cursor. Needed after a message that ends in a space.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002092 windgoto(msg_row, msg_col);
2093
2094 ++no_mapping;
2095 ++allow_keys;
2096 for (;;)
2097 {
2098 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar30613902019-12-01 22:11:18 +01002099 // getchar(): blocking wait.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002100 n = plain_vgetc();
Bram Moolenaarc08cc722020-09-05 17:51:23 +02002101 else if (tv_get_bool_chk(&argvars[0], &error))
Bram Moolenaar30613902019-12-01 22:11:18 +01002102 // getchar(1): only check if char avail
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002103 n = vpeekc_any();
2104 else if (error || vpeekc_any() == NUL)
Bram Moolenaar30613902019-12-01 22:11:18 +01002105 // illegal argument or getchar(0) and no char avail: return zero
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002106 n = 0;
2107 else
Bram Moolenaar15183b42020-09-05 19:59:39 +02002108 // getchar(0) and char avail() != NUL: get a character.
2109 // Note that vpeekc_any() returns K_SPECIAL for K_IGNORE.
2110 n = safe_vgetc();
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002111
Bram Moolenaar15183b42020-09-05 19:59:39 +02002112 if (n == K_IGNORE || n == K_MOUSEMOVE
2113 || n == K_VER_SCROLLBAR || n == K_HOR_SCROLLBAR)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002114 continue;
2115 break;
2116 }
2117 --no_mapping;
2118 --allow_keys;
2119
2120 set_vim_var_nr(VV_MOUSE_WIN, 0);
2121 set_vim_var_nr(VV_MOUSE_WINID, 0);
2122 set_vim_var_nr(VV_MOUSE_LNUM, 0);
2123 set_vim_var_nr(VV_MOUSE_COL, 0);
2124
2125 rettv->vval.v_number = n;
2126 if (IS_SPECIAL(n) || mod_mask != 0)
2127 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002128 char_u temp[10]; // modifier: 3, mbyte-char: 6, NUL: 1
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002129 int i = 0;
2130
Bram Moolenaar30613902019-12-01 22:11:18 +01002131 // Turn a special key into three bytes, plus modifier.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002132 if (mod_mask != 0)
2133 {
2134 temp[i++] = K_SPECIAL;
2135 temp[i++] = KS_MODIFIER;
2136 temp[i++] = mod_mask;
2137 }
2138 if (IS_SPECIAL(n))
2139 {
2140 temp[i++] = K_SPECIAL;
2141 temp[i++] = K_SECOND(n);
2142 temp[i++] = K_THIRD(n);
2143 }
2144 else if (has_mbyte)
2145 i += (*mb_char2bytes)(n, temp + i);
2146 else
2147 temp[i++] = n;
2148 temp[i++] = NUL;
2149 rettv->v_type = VAR_STRING;
2150 rettv->vval.v_string = vim_strsave(temp);
2151
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002152 if (is_mouse_key(n))
2153 {
2154 int row = mouse_row;
2155 int col = mouse_col;
2156 win_T *win;
2157 linenr_T lnum;
2158 win_T *wp;
2159 int winnr = 1;
2160
2161 if (row >= 0 && col >= 0)
2162 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002163 // Find the window at the mouse coordinates and compute the
2164 // text position.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002165 win = mouse_find_win(&row, &col, FIND_POPUP);
2166 if (win == NULL)
2167 return;
2168 (void)mouse_comp_pos(win, &row, &col, &lnum, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002169#ifdef FEAT_PROP_POPUP
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002170 if (WIN_IS_POPUP(win))
2171 winnr = 0;
2172 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002173#endif
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002174 for (wp = firstwin; wp != win && wp != NULL;
2175 wp = wp->w_next)
2176 ++winnr;
2177 set_vim_var_nr(VV_MOUSE_WIN, winnr);
2178 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
2179 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
2180 set_vim_var_nr(VV_MOUSE_COL, col + 1);
2181 }
2182 }
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002183 }
2184}
2185
2186/*
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002187 * "getchar()" function
2188 */
2189 void
2190f_getchar(typval_T *argvars, typval_T *rettv)
2191{
2192 getchar_common(argvars, rettv);
2193}
2194
2195/*
2196 * "getcharstr()" function
2197 */
2198 void
2199f_getcharstr(typval_T *argvars, typval_T *rettv)
2200{
2201 getchar_common(argvars, rettv);
2202
2203 if (rettv->v_type == VAR_NUMBER)
2204 {
2205 char_u temp[7]; // mbyte-char: 6, NUL: 1
2206 varnumber_T n = rettv->vval.v_number;
2207 int i = 0;
2208
2209 if (n != 0)
2210 {
2211 if (has_mbyte)
2212 i += (*mb_char2bytes)(n, temp + i);
2213 else
2214 temp[i++] = n;
2215 }
2216 temp[i++] = NUL;
2217 rettv->v_type = VAR_STRING;
2218 rettv->vval.v_string = vim_strsave(temp);
2219 }
2220}
2221
2222/*
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002223 * "getcharmod()" function
2224 */
2225 void
2226f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
2227{
2228 rettv->vval.v_number = mod_mask;
2229}
2230#endif // FEAT_EVAL
2231
2232#if defined(MESSAGE_QUEUE) || defined(PROTO)
2233# define MAX_REPEAT_PARSE 8
2234
2235/*
2236 * Process messages that have been queued for netbeans or clientserver.
2237 * Also check if any jobs have ended.
h-east965d2ed2021-10-04 21:51:57 +01002238 * These functions can call arbitrary Vim script and should only be called when
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002239 * it is safe to do so.
2240 */
2241 void
2242parse_queued_messages(void)
2243{
Bram Moolenaar1cac7092019-10-22 21:54:31 +02002244 int old_curwin_id;
2245 int old_curbuf_fnum;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002246 int i;
2247 int save_may_garbage_collect = may_garbage_collect;
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002248 static int entered = 0;
Bram Moolenaard103ee72019-09-18 21:15:31 +02002249 int was_safe = get_was_safe_state();
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002250
2251 // Do not handle messages while redrawing, because it may cause buffers to
2252 // change or be wiped while they are being redrawn.
Bram Moolenaar4778b4d2020-11-04 11:03:12 +01002253 // Also bail out when parsing messages was explicitly disabled.
2254 if (updating_screen || dont_parse_messages)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002255 return;
2256
Bram Moolenaar1cac7092019-10-22 21:54:31 +02002257 // If memory allocation fails during startup we'll exit but curbuf or
2258 // curwin could be NULL.
2259 if (curbuf == NULL || curwin == NULL)
2260 return;
2261
2262 old_curbuf_fnum = curbuf->b_fnum;
2263 old_curwin_id = curwin->w_id;
2264
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002265 ++entered;
2266
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002267 // may_garbage_collect is set in main_loop() to do garbage collection when
2268 // blocking to wait on a character. We don't want that while parsing
2269 // messages, a callback may invoke vgetc() while lists and dicts are in use
2270 // in the call stack.
2271 may_garbage_collect = FALSE;
2272
2273 // Loop when a job ended, but don't keep looping forever.
2274 for (i = 0; i < MAX_REPEAT_PARSE; ++i)
2275 {
2276 // For Win32 mch_breakcheck() does not check for input, do it here.
Bram Moolenaar80a8d382020-05-03 22:57:32 +02002277# if (defined(MSWIN) || defined(__HAIKU__)) && defined(FEAT_JOB_CHANNEL)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002278 channel_handle_events(FALSE);
2279# endif
2280
2281# ifdef FEAT_NETBEANS_INTG
2282 // Process the queued netbeans messages.
2283 netbeans_parse_messages();
2284# endif
2285# ifdef FEAT_JOB_CHANNEL
2286 // Write any buffer lines still to be written.
2287 channel_write_any_lines();
2288
2289 // Process the messages queued on channels.
2290 channel_parse_messages();
2291# endif
2292# if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
2293 // Process the queued clientserver messages.
2294 server_parse_messages();
2295# endif
2296# ifdef FEAT_JOB_CHANNEL
2297 // Check if any jobs have ended. If so, repeat the above to handle
2298 // changes, e.g. stdin may have been closed.
2299 if (job_check_ended())
2300 continue;
2301# endif
2302# ifdef FEAT_TERMINAL
2303 free_unused_terminals();
2304# endif
2305# ifdef FEAT_SOUND_CANBERRA
2306 if (has_sound_callback_in_queue())
2307 invoke_sound_callback();
2308# endif
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002309#ifdef SIGUSR1
2310 if (got_sigusr1)
2311 {
2312 apply_autocmds(EVENT_SIGUSR1, NULL, NULL, FALSE, curbuf);
2313 got_sigusr1 = FALSE;
2314 }
2315#endif
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002316 break;
2317 }
2318
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002319 // When not nested we'll go back to waiting for a typed character. If it
2320 // was safe before then this triggers a SafeStateAgain autocommand event.
Bram Moolenaard103ee72019-09-18 21:15:31 +02002321 if (entered == 1 && was_safe)
Bram Moolenaar37d18072019-09-17 20:28:38 +02002322 may_trigger_safestateagain();
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002323
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002324 may_garbage_collect = save_may_garbage_collect;
2325
2326 // If the current window or buffer changed we need to bail out of the
2327 // waiting loop. E.g. when a job exit callback closes the terminal window.
2328 if (curwin->w_id != old_curwin_id || curbuf->b_fnum != old_curbuf_fnum)
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02002329 ins_char_typebuf(K_IGNORE, 0);
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002330
2331 --entered;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002332}
2333#endif
2334
2335
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002336typedef enum {
2337 map_result_fail, // failed, break loop
2338 map_result_get, // get a character from typeahead
2339 map_result_retry, // try to map again
2340 map_result_nomatch // no matching mapping, get char
2341} map_result_T;
2342
2343/*
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002344 * Check if the bytes at the start of the typeahead buffer are a character used
2345 * in CTRL-X mode. This includes the form with a CTRL modifier.
2346 */
2347 static int
2348at_ctrl_x_key(void)
2349{
2350 char_u *p = typebuf.tb_buf + typebuf.tb_off;
2351 int c = *p;
2352
2353 if (typebuf.tb_len > 3
2354 && c == K_SPECIAL
2355 && p[1] == KS_MODIFIER
2356 && (p[2] & MOD_MASK_CTRL))
2357 c = p[3] & 0x1f;
2358 return vim_is_ctrl_x_key(c);
2359}
2360
2361/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002362 * Check if typebuf.tb_buf[] contains a modifier plus key that can be changed
Bram Moolenaar975a8802020-06-06 22:36:24 +02002363 * into just a key, apply that.
2364 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
2365 * + "max_offset"].
2366 * Return the length of the replaced bytes, zero if nothing changed.
2367 */
2368 static int
2369check_simplify_modifier(int max_offset)
2370{
2371 int offset;
2372 char_u *tp;
2373
2374 for (offset = 0; offset < max_offset; ++offset)
2375 {
2376 if (offset + 3 >= typebuf.tb_len)
2377 break;
2378 tp = typebuf.tb_buf + typebuf.tb_off + offset;
2379 if (tp[0] == K_SPECIAL && tp[1] == KS_MODIFIER)
2380 {
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002381 // A modifier was not used for a mapping, apply it to ASCII keys.
2382 // Shift would already have been applied.
Bram Moolenaar975a8802020-06-06 22:36:24 +02002383 int modifier = tp[2];
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002384 int c = tp[3];
2385 int new_c = merge_modifyOtherKeys(c, &modifier);
Bram Moolenaar975a8802020-06-06 22:36:24 +02002386
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002387 if (new_c != c)
Bram Moolenaar975a8802020-06-06 22:36:24 +02002388 {
2389 char_u new_string[MB_MAXBYTES];
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002390 int len;
Bram Moolenaar975a8802020-06-06 22:36:24 +02002391
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002392 if (offset == 0)
2393 {
2394 // At the start: remember the character and mod_mask before
2395 // merging, in some cases, e.g. at the hit-return prompt,
2396 // they are put back in the typeahead buffer.
2397 vgetc_char = c;
2398 vgetc_mod_mask = tp[2];
2399 }
2400 len = mb_char2bytes(new_c, new_string);
2401 if (modifier == 0)
2402 {
2403 if (put_string_in_typebuf(offset, 4, new_string, len,
Bram Moolenaar975a8802020-06-06 22:36:24 +02002404 NULL, 0, 0) == FAIL)
2405 return -1;
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002406 }
2407 else
2408 {
2409 tp[2] = modifier;
2410 if (put_string_in_typebuf(offset + 3, 1, new_string, len,
2411 NULL, 0, 0) == FAIL)
2412 return -1;
2413 }
Bram Moolenaar975a8802020-06-06 22:36:24 +02002414 return len;
2415 }
2416 }
2417 }
2418 return 0;
2419}
2420
2421/*
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002422 * Handle mappings in the typeahead buffer.
2423 * - When something was mapped, return map_result_retry for recursive mappings.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002424 * - When nothing mapped and typeahead has a character: return map_result_get.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002425 * - When there is no match yet, return map_result_nomatch, need to get more
2426 * typeahead.
2427 */
2428 static int
2429handle_mapping(
2430 int *keylenp,
2431 int *timedout,
2432 int *mapdepth)
2433{
2434 mapblock_T *mp = NULL;
2435 mapblock_T *mp2;
2436 mapblock_T *mp_match;
2437 int mp_match_len = 0;
2438 int max_mlen = 0;
2439 int tb_c1;
2440 int mlen;
2441#ifdef FEAT_LANGMAP
2442 int nolmaplen;
2443#endif
2444 int keylen = *keylenp;
2445 int i;
2446 int local_State = get_real_state();
2447
2448 /*
2449 * Check for a mappable key sequence.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002450 * Walk through one maphash[] list until we find an entry that matches.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002451 *
2452 * Don't look for mappings if:
2453 * - no_mapping set: mapping disabled (e.g. for CTRL-V)
2454 * - maphash_valid not set: no mappings present.
2455 * - typebuf.tb_buf[typebuf.tb_off] should not be remapped
2456 * - in insert or cmdline mode and 'paste' option set
Bram Moolenaareda35f72019-08-03 14:59:44 +02002457 * - waiting for "hit return to continue" and CR or SPACE typed
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002458 * - waiting for a char with --more--
2459 * - in Ctrl-X mode, and we get a valid char for that mode
2460 */
2461 tb_c1 = typebuf.tb_buf[typebuf.tb_off];
2462 if (no_mapping == 0 && is_maphash_valid()
2463 && (no_zero_mapping == 0 || tb_c1 != '0')
2464 && (typebuf.tb_maplen == 0
2465 || (p_remap
2466 && (typebuf.tb_noremap[typebuf.tb_off]
2467 & (RM_NONE|RM_ABBR)) == 0))
2468 && !(p_paste && (State & (INSERT + CMDLINE)))
2469 && !(State == HITRETURN && (tb_c1 == CAR || tb_c1 == ' '))
2470 && State != ASKMORE
2471 && State != CONFIRM
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002472 && !((ctrl_x_mode_not_default() && at_ctrl_x_key())
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00002473 || (compl_status_local()
Bram Moolenaare2c453d2019-08-21 14:37:09 +02002474 && (tb_c1 == Ctrl_N || tb_c1 == Ctrl_P))))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002475 {
Bram Moolenaarc9983702020-05-28 21:03:53 +02002476#ifdef FEAT_GUI
2477 if (gui.in_use && tb_c1 == CSI && typebuf.tb_len >= 2
2478 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_MODIFIER)
2479 {
2480 // The GUI code sends CSI KS_MODIFIER {flags}, but mappings expect
2481 // K_SPECIAL KS_MODIFIER {flags}.
2482 tb_c1 = K_SPECIAL;
2483 }
2484#endif
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002485#ifdef FEAT_LANGMAP
2486 if (tb_c1 == K_SPECIAL)
2487 nolmaplen = 2;
2488 else
2489 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002490 LANGMAP_ADJUST(tb_c1, (State & (CMDLINE | INSERT)) == 0
2491 && get_real_state() != SELECTMODE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002492 nolmaplen = 0;
2493 }
2494#endif
2495 // First try buffer-local mappings.
2496 mp = get_buf_maphash_list(local_State, tb_c1);
2497 mp2 = get_maphash_list(local_State, tb_c1);
2498 if (mp == NULL)
2499 {
2500 // There are no buffer-local mappings.
2501 mp = mp2;
2502 mp2 = NULL;
2503 }
Bram Moolenaareda35f72019-08-03 14:59:44 +02002504
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002505 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002506 * Loop until a partly matching mapping is found or all (local)
2507 * mappings have been checked.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002508 * The longest full match is remembered in "mp_match".
Bram Moolenaareda35f72019-08-03 14:59:44 +02002509 * A full match is only accepted if there is no partly match, so "aa"
2510 * and "aaa" can both be mapped.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002511 */
2512 mp_match = NULL;
2513 mp_match_len = 0;
2514 for ( ; mp != NULL;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002515 mp->m_next == NULL ? (mp = mp2, mp2 = NULL) : (mp = mp->m_next))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002516 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002517 // Only consider an entry if the first character matches and it is
2518 // for the current state.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002519 // Skip ":lmap" mappings if keys were mapped.
2520 if (mp->m_keys[0] == tb_c1
2521 && (mp->m_mode & local_State)
Bram Moolenaar46cd43b2020-06-04 22:22:11 +02002522 && !(mp->m_simplified && seenModifyOtherKeys
2523 && typebuf.tb_maplen == 0)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002524 && ((mp->m_mode & LANGMAP) == 0 || typebuf.tb_maplen == 0))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002525 {
2526#ifdef FEAT_LANGMAP
2527 int nomap = nolmaplen;
2528 int c2;
2529#endif
2530 // find the match length of this mapping
2531 for (mlen = 1; mlen < typebuf.tb_len; ++mlen)
2532 {
2533#ifdef FEAT_LANGMAP
2534 c2 = typebuf.tb_buf[typebuf.tb_off + mlen];
2535 if (nomap > 0)
2536 --nomap;
2537 else if (c2 == K_SPECIAL)
2538 nomap = 2;
2539 else
2540 LANGMAP_ADJUST(c2, TRUE);
2541 if (mp->m_keys[mlen] != c2)
2542#else
2543 if (mp->m_keys[mlen] !=
Bram Moolenaarc9983702020-05-28 21:03:53 +02002544 typebuf.tb_buf[typebuf.tb_off + mlen])
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002545#endif
2546 break;
2547 }
2548
Bram Moolenaareda35f72019-08-03 14:59:44 +02002549 // Don't allow mapping the first byte(s) of a multi-byte char.
2550 // Happens when mapping <M-a> and then changing 'encoding'.
2551 // Beware that 0x80 is escaped.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002552 {
2553 char_u *p1 = mp->m_keys;
2554 char_u *p2 = mb_unescape(&p1);
2555
2556 if (has_mbyte && p2 != NULL
Bram Moolenaar1614a142019-10-06 22:00:13 +02002557 && MB_BYTE2LEN(tb_c1) > mb_ptr2len(p2))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002558 mlen = 0;
2559 }
2560
2561 // Check an entry whether it matches.
2562 // - Full match: mlen == keylen
2563 // - Partly match: mlen == typebuf.tb_len
2564 keylen = mp->m_keylen;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002565 if (mlen == keylen || (mlen == typebuf.tb_len
2566 && typebuf.tb_len < keylen))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002567 {
2568 char_u *s;
2569 int n;
2570
Bram Moolenaareda35f72019-08-03 14:59:44 +02002571 // If only script-local mappings are allowed, check if the
2572 // mapping starts with K_SNR.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002573 s = typebuf.tb_noremap + typebuf.tb_off;
2574 if (*s == RM_SCRIPT
2575 && (mp->m_keys[0] != K_SPECIAL
2576 || mp->m_keys[1] != KS_EXTRA
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01002577 || mp->m_keys[2] != KE_SNR))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002578 continue;
2579
Bram Moolenaareda35f72019-08-03 14:59:44 +02002580 // If one of the typed keys cannot be remapped, skip the
2581 // entry.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002582 for (n = mlen; --n >= 0; )
2583 if (*s++ & (RM_NONE|RM_ABBR))
2584 break;
2585 if (n >= 0)
2586 continue;
2587
2588 if (keylen > typebuf.tb_len)
2589 {
2590 if (!*timedout && !(mp_match != NULL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002591 && mp_match->m_nowait))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002592 {
2593 // break at a partly match
2594 keylen = KEYLEN_PART_MAP;
2595 break;
2596 }
2597 }
2598 else if (keylen > mp_match_len)
2599 {
2600 // found a longer match
2601 mp_match = mp;
2602 mp_match_len = keylen;
2603 }
2604 }
2605 else
Bram Moolenaareda35f72019-08-03 14:59:44 +02002606 // No match; may have to check for termcode at next
2607 // character.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002608 if (max_mlen < mlen)
2609 max_mlen = mlen;
2610 }
2611 }
2612
Bram Moolenaareda35f72019-08-03 14:59:44 +02002613 // If no partly match found, use the longest full match.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002614 if (keylen != KEYLEN_PART_MAP)
2615 {
2616 mp = mp_match;
2617 keylen = mp_match_len;
2618 }
2619 }
2620
2621 /*
2622 * Check for match with 'pastetoggle'
2623 */
2624 if (*p_pt != NUL && mp == NULL && (State & (INSERT|NORMAL)))
2625 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002626 for (mlen = 0; mlen < typebuf.tb_len && p_pt[mlen]; ++mlen)
2627 if (p_pt[mlen] != typebuf.tb_buf[typebuf.tb_off + mlen])
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002628 break;
2629 if (p_pt[mlen] == NUL) // match
2630 {
2631 // write chars to script file(s)
2632 if (mlen > typebuf.tb_maplen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002633 gotchars(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_maplen,
2634 mlen - typebuf.tb_maplen);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002635
2636 del_typebuf(mlen, 0); // remove the chars
Bram Moolenaareda35f72019-08-03 14:59:44 +02002637 set_option_value((char_u *)"paste", (long)!p_paste, NULL, 0);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002638 if (!(State & INSERT))
2639 {
2640 msg_col = 0;
2641 msg_row = Rows - 1;
2642 msg_clr_eos(); // clear ruler
2643 }
2644 status_redraw_all();
2645 redraw_statuslines();
2646 showmode();
2647 setcursor();
2648 *keylenp = keylen;
2649 return map_result_retry;
2650 }
2651 // Need more chars for partly match.
2652 if (mlen == typebuf.tb_len)
2653 keylen = KEYLEN_PART_KEY;
2654 else if (max_mlen < mlen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002655 // no match, may have to check for termcode at next character
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002656 max_mlen = mlen + 1;
2657 }
2658
Bram Moolenaareda35f72019-08-03 14:59:44 +02002659 if ((mp == NULL || max_mlen >= mp_match_len) && keylen != KEYLEN_PART_MAP)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002660 {
2661 int save_keylen = keylen;
2662
2663 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002664 * When no matching mapping found or found a non-matching mapping that
2665 * matches at least what the matching mapping matched:
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002666 * Check if we have a terminal code, when:
Bram Moolenaareda35f72019-08-03 14:59:44 +02002667 * - mapping is allowed,
2668 * - keys have not been mapped,
2669 * - and not an ESC sequence, not in insert mode or p_ek is on,
2670 * - and when not timed out,
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002671 */
2672 if ((no_mapping == 0 || allow_keys != 0)
2673 && (typebuf.tb_maplen == 0
2674 || (p_remap && typebuf.tb_noremap[
Bram Moolenaareda35f72019-08-03 14:59:44 +02002675 typebuf.tb_off] == RM_YES))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002676 && !*timedout)
2677 {
Bram Moolenaar0684e362020-12-03 19:54:42 +01002678 keylen = check_termcode(max_mlen + 1, NULL, 0, NULL);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002679
Bram Moolenaar0684e362020-12-03 19:54:42 +01002680 // If no termcode matched but 'pastetoggle' matched partially
2681 // it's like an incomplete key sequence.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002682 if (keylen == 0 && save_keylen == KEYLEN_PART_KEY)
2683 keylen = KEYLEN_PART_KEY;
2684
Bram Moolenaar975a8802020-06-06 22:36:24 +02002685 // If no termcode matched, try to include the modifier into the
zeertzjqa3f83fe2021-11-22 12:47:39 +00002686 // key. This is for when modifyOtherKeys is working.
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002687 if (keylen == 0 && !no_reduce_keys)
Bram Moolenaar975a8802020-06-06 22:36:24 +02002688 keylen = check_simplify_modifier(max_mlen + 1);
2689
Bram Moolenaareda35f72019-08-03 14:59:44 +02002690 // When getting a partial match, but the last characters were not
2691 // typed, don't wait for a typed character to complete the
2692 // termcode. This helps a lot when a ":normal" command ends in an
2693 // ESC.
2694 if (keylen < 0 && typebuf.tb_len == typebuf.tb_maplen)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002695 keylen = 0;
2696 }
2697 else
2698 keylen = 0;
2699 if (keylen == 0) // no matching terminal code
2700 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002701#ifdef AMIGA
2702 // check for window bounds report
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002703 if (typebuf.tb_maplen == 0 && (typebuf.tb_buf[
Bram Moolenaareda35f72019-08-03 14:59:44 +02002704 typebuf.tb_off] & 0xff) == CSI)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002705 {
2706 char_u *s;
2707
2708 for (s = typebuf.tb_buf + typebuf.tb_off + 1;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002709 s < typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len
2710 && (VIM_ISDIGIT(*s) || *s == ';' || *s == ' ');
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002711 ++s)
2712 ;
2713 if (*s == 'r' || *s == '|') // found one
2714 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002715 del_typebuf(
2716 (int)(s + 1 - (typebuf.tb_buf + typebuf.tb_off)), 0);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002717 // get size and redraw screen
2718 shell_resized();
2719 *keylenp = keylen;
2720 return map_result_retry;
2721 }
2722 if (*s == NUL) // need more characters
2723 keylen = KEYLEN_PART_KEY;
2724 }
2725 if (keylen >= 0)
2726#endif
Bram Moolenaareda35f72019-08-03 14:59:44 +02002727 // When there was a matching mapping and no termcode could be
2728 // replaced after another one, use that mapping (loop around).
2729 // If there was no mapping at all use the character from the
2730 // typeahead buffer right here.
2731 if (mp == NULL)
2732 {
2733 *keylenp = keylen;
Bram Moolenaar52797ba2021-12-16 14:45:13 +00002734 return map_result_get; // get character from typeahead
Bram Moolenaareda35f72019-08-03 14:59:44 +02002735 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002736 }
2737
2738 if (keylen > 0) // full matching terminal code
2739 {
2740#if defined(FEAT_GUI) && defined(FEAT_MENU)
2741 if (typebuf.tb_len >= 2
Bram Moolenaareda35f72019-08-03 14:59:44 +02002742 && typebuf.tb_buf[typebuf.tb_off] == K_SPECIAL
2743 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_MENU)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002744 {
2745 int idx;
2746
Bram Moolenaareda35f72019-08-03 14:59:44 +02002747 // Using a menu may cause a break in undo! It's like using
2748 // gotchars(), but without recording or writing to a script
2749 // file.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002750 may_sync_undo();
2751 del_typebuf(3, 0);
2752 idx = get_menu_index(current_menu, local_State);
2753 if (idx != MENU_INDEX_INVALID)
2754 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002755 // In Select mode and a Visual mode menu is used: Switch
2756 // to Visual mode temporarily. Append K_SELECT to switch
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002757 // back to Select mode.
2758 if (VIsual_active && VIsual_select
Bram Moolenaareda35f72019-08-03 14:59:44 +02002759 && (current_menu->modes & VISUAL))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002760 {
2761 VIsual_select = FALSE;
2762 (void)ins_typebuf(K_SELECT_STRING,
Bram Moolenaareda35f72019-08-03 14:59:44 +02002763 REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002764 }
2765 ins_typebuf(current_menu->strings[idx],
2766 current_menu->noremap[idx],
Bram Moolenaareda35f72019-08-03 14:59:44 +02002767 0, TRUE, current_menu->silent[idx]);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002768 }
2769 }
2770#endif // FEAT_GUI && FEAT_MENU
2771 *keylenp = keylen;
2772 return map_result_retry; // try mapping again
2773 }
2774
Bram Moolenaareda35f72019-08-03 14:59:44 +02002775 // Partial match: get some more characters. When a matching mapping
2776 // was found use that one.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002777 if (mp == NULL || keylen < 0)
2778 keylen = KEYLEN_PART_KEY;
2779 else
2780 keylen = mp_match_len;
2781 }
2782
2783 /*
2784 * complete match
2785 */
2786 if (keylen >= 0 && keylen <= typebuf.tb_len)
2787 {
2788 char_u *map_str;
2789
2790#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002791 int save_m_expr;
2792 int save_m_noremap;
2793 int save_m_silent;
2794 char_u *save_m_keys;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002795#else
2796# define save_m_noremap mp->m_noremap
2797# define save_m_silent mp->m_silent
2798#endif
2799
2800 // write chars to script file(s)
2801 if (keylen > typebuf.tb_maplen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002802 gotchars(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_maplen,
2803 keylen - typebuf.tb_maplen);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002804
2805 cmd_silent = (typebuf.tb_silent > 0);
2806 del_typebuf(keylen, 0); // remove the mapped keys
2807
2808 /*
2809 * Put the replacement string in front of mapstr.
2810 * The depth check catches ":map x y" and ":map y x".
2811 */
2812 if (++*mapdepth >= p_mmd)
2813 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00002814 emsg(_(e_recursive_mapping));
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002815 if (State & CMDLINE)
2816 redrawcmdline();
2817 else
2818 setcursor();
2819 flush_buffers(FLUSH_MINIMAL);
Bram Moolenaar30613902019-12-01 22:11:18 +01002820 *mapdepth = 0; // for next one
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002821 *keylenp = keylen;
2822 return map_result_fail;
2823 }
2824
2825 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002826 * In Select mode and a Visual mode mapping is used: Switch to Visual
2827 * mode temporarily. Append K_SELECT to switch back to Select mode.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002828 */
Bram Moolenaareda35f72019-08-03 14:59:44 +02002829 if (VIsual_active && VIsual_select && (mp->m_mode & VISUAL))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002830 {
2831 VIsual_select = FALSE;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002832 (void)ins_typebuf(K_SELECT_STRING, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002833 }
2834
2835#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002836 // Copy the values from *mp that are used, because evaluating the
2837 // expression may invoke a function that redefines the mapping, thereby
2838 // making *mp invalid.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002839 save_m_expr = mp->m_expr;
2840 save_m_noremap = mp->m_noremap;
2841 save_m_silent = mp->m_silent;
2842 save_m_keys = NULL; // only saved when needed
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002843
2844 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002845 * Handle ":map <expr>": evaluate the {rhs} as an expression. Also
2846 * save and restore the command line for "normal :".
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002847 */
2848 if (mp->m_expr)
2849 {
2850 int save_vgetc_busy = vgetc_busy;
2851 int save_may_garbage_collect = may_garbage_collect;
Bram Moolenaar4ebe0e62019-11-22 20:55:40 +01002852 int was_screen_col = screen_cur_col;
2853 int was_screen_row = screen_cur_row;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002854
2855 vgetc_busy = 0;
2856 may_garbage_collect = FALSE;
2857
2858 save_m_keys = vim_strsave(mp->m_keys);
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002859 map_str = eval_map_expr(mp, NUL);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002860
Bram Moolenaar4ebe0e62019-11-22 20:55:40 +01002861 // The mapping may do anything, but we expect it to take care of
2862 // redrawing. Do put the cursor back where it was.
2863 windgoto(was_screen_row, was_screen_col);
2864 out_flush();
2865
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002866 vgetc_busy = save_vgetc_busy;
2867 may_garbage_collect = save_may_garbage_collect;
2868 }
2869 else
2870#endif
2871 map_str = mp->m_str;
2872
2873 /*
2874 * Insert the 'to' part in the typebuf.tb_buf.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002875 * If 'from' field is the same as the start of the 'to' field, don't
2876 * remap the first character (but do allow abbreviations).
2877 * If m_noremap is set, don't remap the whole 'to' part.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002878 */
2879 if (map_str == NULL)
2880 i = FAIL;
2881 else
2882 {
2883 int noremap;
2884
2885 if (save_m_noremap != REMAP_YES)
2886 noremap = save_m_noremap;
2887 else if (
2888#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002889 STRNCMP(map_str, save_m_keys != NULL ? save_m_keys : mp->m_keys,
2890 (size_t)keylen)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002891#else
2892 STRNCMP(map_str, mp->m_keys, (size_t)keylen)
2893#endif
2894 != 0)
2895 noremap = REMAP_YES;
2896 else
2897 noremap = REMAP_SKIP;
2898 i = ins_typebuf(map_str, noremap,
2899 0, TRUE, cmd_silent || save_m_silent);
2900#ifdef FEAT_EVAL
2901 if (save_m_expr)
2902 vim_free(map_str);
Bram Moolenaare32c3c42022-01-15 18:26:04 +00002903 last_used_map = mp;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002904#endif
2905 }
2906#ifdef FEAT_EVAL
2907 vim_free(save_m_keys);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002908#endif
2909 *keylenp = keylen;
2910 if (i == FAIL)
2911 return map_result_fail;
2912 return map_result_retry;
2913 }
2914
2915 *keylenp = keylen;
2916 return map_result_nomatch;
2917}
2918
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002919/*
2920 * unget one character (can only be done once!)
2921 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002923vungetc(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924{
2925 old_char = c;
2926 old_mod_mask = mod_mask;
Bram Moolenaarb8978712013-03-16 21:42:16 +01002927 old_mouse_row = mouse_row;
2928 old_mouse_col = mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929}
2930
2931/*
Bram Moolenaar32526b32019-01-19 17:43:09 +01002932 * Get a byte:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 * 1. from the stuffbuffer
2934 * This is used for abbreviated commands like "D" -> "d$".
2935 * Also used to redo a command for ".".
2936 * 2. from the typeahead buffer
2937 * Stores text obtained previously but not used yet.
2938 * Also stores the result of mappings.
2939 * Also used for the ":normal" command.
2940 * 3. from the user
2941 * This may do a blocking wait if "advance" is TRUE.
2942 *
2943 * if "advance" is TRUE (vgetc()):
Bram Moolenaard29459b2016-08-26 22:29:11 +02002944 * Really get the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 * KeyTyped is set to TRUE in the case the user typed the key.
2946 * KeyStuffed is TRUE if the character comes from the stuff buffer.
2947 * if "advance" is FALSE (vpeekc()):
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02002948 * Just look whether there is a character available.
2949 * Return NUL if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 *
2951 * When "no_mapping" is zero, checks for mappings in the current mode.
2952 * Only returns one byte (of a multi-byte character).
2953 * K_SPECIAL and CSI may be escaped, need to get two more bytes then.
2954 */
2955 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002956vgetorpeek(int advance)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957{
2958 int c, c1;
Bram Moolenaar30613902019-12-01 22:11:18 +01002959 int timedout = FALSE; // waited for more than 1 second
2960 // for mapping to complete
2961 int mapdepth = 0; // check for recursive mapping
2962 int mode_deleted = FALSE; // set when mode has been deleted
Bram Moolenaar4e427192006-03-10 21:34:27 +00002963#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 int new_wcol, new_wrow;
2965#endif
2966#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01002967 int shape_changed = FALSE; // adjusted cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968#endif
2969 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 int old_wcol, old_wrow;
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00002971 int wait_tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972
2973 /*
2974 * This function doesn't work very well when called recursively. This may
2975 * happen though, because of:
2976 * 1. The call to add_to_showcmd(). char_avail() is then used to check if
2977 * there is a character available, which calls this function. In that
2978 * case we must return NUL, to indicate no character is available.
2979 * 2. A GUI callback function writes to the screen, causing a
2980 * wait_return().
2981 * Using ":normal" can also do this, but it saves the typeahead buffer,
2982 * thus it should be OK. But don't get a key from the user then.
2983 */
Bram Moolenaare2c38102016-01-31 14:55:40 +01002984 if (vgetc_busy > 0 && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 return NUL;
2986
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002987 ++vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988
2989 if (advance)
2990 KeyStuffed = FALSE;
2991
2992 init_typebuf();
2993 start_stuff();
2994 if (advance && typebuf.tb_maplen == 0)
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02002995 reg_executing = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 do
2997 {
2998/*
2999 * get a character: 1. from the stuffbuffer
3000 */
3001 if (typeahead_char != 0)
3002 {
3003 c = typeahead_char;
3004 if (advance)
3005 typeahead_char = 0;
3006 }
3007 else
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01003008 c = read_readbuffers(advance);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 if (c != NUL && !got_int)
3010 {
3011 if (advance)
3012 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003013 // KeyTyped = FALSE; When the command that stuffed something
3014 // was typed, behave like the stuffed command was typed.
3015 // needed for CTRL-W CTRL-] to open a fold, for example.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 KeyStuffed = TRUE;
3017 }
3018 if (typebuf.tb_no_abbr_cnt == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003019 typebuf.tb_no_abbr_cnt = 1; // no abbreviations now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 }
3021 else
3022 {
3023 /*
3024 * Loop until we either find a matching mapped key, or we
3025 * are sure that it is not a mapped key.
3026 * If a mapped key sequence is found we go back to the start to
3027 * try re-mapping.
3028 */
3029 for (;;)
3030 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003031 long wait_time;
3032 int keylen = 0;
Bram Moolenaareda35f72019-08-03 14:59:44 +02003033#ifdef FEAT_CMDL_INFO
3034 int showcmd_idx;
3035#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 /*
3037 * ui_breakcheck() is slow, don't use it too often when
3038 * inside a mapping. But call it each time for typed
3039 * characters.
3040 */
3041 if (typebuf.tb_maplen)
3042 line_breakcheck();
3043 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003044 ui_breakcheck(); // check for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 if (got_int)
3046 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003047 // flush all input
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003048 c = inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 0L);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003049
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 /*
3051 * If inchar() returns TRUE (script file was active) or we
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003052 * are inside a mapping, get out of Insert mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 * Otherwise we behave like having gotten a CTRL-C.
3054 * As a result typing CTRL-C in insert mode will
3055 * really insert a CTRL-C.
3056 */
3057 if ((c || typebuf.tb_maplen)
3058 && (State & (INSERT + CMDLINE)))
3059 c = ESC;
3060 else
3061 c = Ctrl_C;
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02003062 flush_buffers(FLUSH_INPUT); // flush all typeahead
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063
Bram Moolenaard9dfd572006-10-03 13:36:13 +00003064 if (advance)
3065 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003066 // Also record this character, it might be needed to
3067 // get out of Insert mode.
Bram Moolenaard9dfd572006-10-03 13:36:13 +00003068 *typebuf.tb_buf = c;
3069 gotchars(typebuf.tb_buf, 1);
3070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 cmd_silent = FALSE;
3072
3073 break;
3074 }
3075 else if (typebuf.tb_len > 0)
3076 {
3077 /*
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003078 * Check for a mapping in "typebuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 */
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003080 map_result_T result = handle_mapping(
3081 &keylen, &timedout, &mapdepth);
3082
3083 if (result == map_result_retry)
3084 // try mapping again
3085 continue;
3086 if (result == map_result_fail)
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003087 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003088 // failed, use the outer loop
3089 c = -1;
3090 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003092 if (result == map_result_get)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094/*
3095 * get a character: 2. from the typeahead buffer
3096 */
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003097 c = typebuf.tb_buf[typebuf.tb_off];
Bram Moolenaar30613902019-12-01 22:11:18 +01003098 if (advance) // remove chars from tb_buf
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003099 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003100 cmd_silent = (typebuf.tb_silent > 0);
3101 if (typebuf.tb_maplen > 0)
3102 KeyTyped = FALSE;
3103 else
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003104 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003105 KeyTyped = TRUE;
Bram Moolenaar30613902019-12-01 22:11:18 +01003106 // write char to script file(s)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003107 gotchars(typebuf.tb_buf
3108 + typebuf.tb_off, 1);
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003109 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003110 KeyNoremap = typebuf.tb_noremap[
3111 typebuf.tb_off];
3112 del_typebuf(1, 0);
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003113 }
Bram Moolenaar52797ba2021-12-16 14:45:13 +00003114 break; // got character, break the for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 }
3116
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003117 // not enough characters, get more
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 }
3119
3120/*
3121 * get a character: 3. from the user - handle <Esc> in Insert mode
3122 */
3123 /*
Bram Moolenaarf085f422017-06-07 20:39:47 +02003124 * Special case: if we get an <ESC> in insert mode and there
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 * are no more characters at once, we pretend to go out of
3126 * insert mode. This prevents the one second delay after
3127 * typing an <ESC>. If we get something after all, we may
3128 * have to redisplay the mode. That the cursor is in the wrong
3129 * place does not matter.
3130 */
3131 c = 0;
3132#ifdef FEAT_CMDL_INFO
3133 new_wcol = curwin->w_wcol;
3134 new_wrow = curwin->w_wrow;
3135#endif
3136 if ( advance
3137 && typebuf.tb_len == 1
3138 && typebuf.tb_buf[typebuf.tb_off] == ESC
3139 && !no_mapping
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 && ex_normal_busy == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 && typebuf.tb_maplen == 0
3142 && (State & INSERT)
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003143 && (p_timeout
3144 || (keylen == KEYLEN_PART_KEY && p_ttimeout))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 && (c = inchar(typebuf.tb_buf + typebuf.tb_off
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003146 + typebuf.tb_len, 3, 25L)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 {
3148 colnr_T col = 0, vcol;
3149 char_u *ptr;
3150
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003151 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 {
3153 unshowmode(TRUE);
3154 mode_deleted = TRUE;
3155 }
3156#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003157 // may show a different cursor shape
Bram Moolenaarf085f422017-06-07 20:39:47 +02003158 if (gui.in_use && State != NORMAL && !cmd_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 {
3160 int save_State;
3161
3162 save_State = State;
3163 State = NORMAL;
3164 gui_update_cursor(TRUE, FALSE);
3165 State = save_State;
3166 shape_changed = TRUE;
3167 }
3168#endif
3169 validate_cursor();
3170 old_wcol = curwin->w_wcol;
3171 old_wrow = curwin->w_wrow;
3172
Bram Moolenaar30613902019-12-01 22:11:18 +01003173 // move cursor left, if possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 if (curwin->w_cursor.col != 0)
3175 {
3176 if (curwin->w_wcol > 0)
3177 {
3178 if (did_ai)
3179 {
3180 /*
3181 * We are expecting to truncate the trailing
3182 * white-space, so find the last non-white
3183 * character -- webb
3184 */
3185 col = vcol = curwin->w_wcol = 0;
3186 ptr = ml_get_curline();
3187 while (col < curwin->w_cursor.col)
3188 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01003189 if (!VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 curwin->w_wcol = vcol;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003191 vcol += lbr_chartabsize(ptr, ptr + col,
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01003192 vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003194 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 ++col;
3197 }
3198 curwin->w_wrow = curwin->w_cline_row
Bram Moolenaar02631462017-09-22 15:20:32 +02003199 + curwin->w_wcol / curwin->w_width;
3200 curwin->w_wcol %= curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 curwin->w_wcol += curwin_col_off();
Bram Moolenaar30613902019-12-01 22:11:18 +01003202 col = 0; // no correction needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 }
3204 else
3205 {
3206 --curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 col = curwin->w_cursor.col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 }
3209 }
3210 else if (curwin->w_p_wrap && curwin->w_wrow)
3211 {
3212 --curwin->w_wrow;
Bram Moolenaar02631462017-09-22 15:20:32 +02003213 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 col = curwin->w_cursor.col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 if (has_mbyte && col > 0 && curwin->w_wcol > 0)
3217 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003218 // Correct when the cursor is on the right halve
3219 // of a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 ptr = ml_get_curline();
3221 col -= (*mb_head_off)(ptr, ptr + col);
3222 if ((*mb_ptr2cells)(ptr + col) > 1)
3223 --curwin->w_wcol;
3224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 }
3226 setcursor();
3227 out_flush();
3228#ifdef FEAT_CMDL_INFO
3229 new_wcol = curwin->w_wcol;
3230 new_wrow = curwin->w_wrow;
3231#endif
3232 curwin->w_wcol = old_wcol;
3233 curwin->w_wrow = old_wrow;
3234 }
3235 if (c < 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003236 continue; // end of input script reached
Bram Moolenaar20c38922014-07-23 20:41:14 +02003237
Bram Moolenaar30613902019-12-01 22:11:18 +01003238 // Allow mapping for just typed characters. When we get here c
3239 // is the number of extra bytes and typebuf.tb_len is 1.
Bram Moolenaar20c38922014-07-23 20:41:14 +02003240 for (n = 1; n <= c; ++n)
3241 typebuf.tb_noremap[typebuf.tb_off + n] = RM_YES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 typebuf.tb_len += c;
3243
Bram Moolenaar30613902019-12-01 22:11:18 +01003244 // buffer full, don't map
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 if (typebuf.tb_len >= typebuf.tb_maplen + MAXMAPLEN)
3246 {
3247 timedout = TRUE;
3248 continue;
3249 }
3250
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 if (ex_normal_busy > 0)
3252 {
Bram Moolenaare2c38102016-01-31 14:55:40 +01003253#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 static int tc = 0;
Bram Moolenaare2c38102016-01-31 14:55:40 +01003255#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256
Bram Moolenaar30613902019-12-01 22:11:18 +01003257 // No typeahead left and inside ":normal". Must return
3258 // something to avoid getting stuck. When an incomplete
3259 // mapping is present, behave like it timed out.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 if (typebuf.tb_len > 0)
3261 {
3262 timedout = TRUE;
3263 continue;
3264 }
Bram Moolenaar189832b2020-09-23 12:29:11 +02003265
Bram Moolenaar30613902019-12-01 22:11:18 +01003266 // When 'insertmode' is set, ESC just beeps in Insert
3267 // mode. Use CTRL-L to make edit() return.
3268 // For the command line only CTRL-C always breaks it.
3269 // For the cmdline window: Alternate between ESC and
3270 // CTRL-C: ESC for most situations and CTRL-C to close the
3271 // cmdline window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 if (p_im && (State & INSERT))
3273 c = Ctrl_L;
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003274#ifdef FEAT_TERMINAL
3275 else if (terminal_is_active())
3276 c = K_CANCEL;
3277#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 else if ((State & CMDLINE)
Bram Moolenaare2c38102016-01-31 14:55:40 +01003279#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 || (cmdwin_type > 0 && tc == ESC)
Bram Moolenaare2c38102016-01-31 14:55:40 +01003281#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 )
3283 c = Ctrl_C;
3284 else
3285 c = ESC;
Bram Moolenaare2c38102016-01-31 14:55:40 +01003286#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 tc = c;
Bram Moolenaare2c38102016-01-31 14:55:40 +01003288#endif
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01003289 // return from main_loop()
3290 if (pending_exmode_active)
3291 exmode_active = EXMODE_NORMAL;
3292
Bram Moolenaarb37a65e2022-01-01 12:42:56 +00003293 // no chars to block abbreviation for
3294 typebuf.tb_no_abbr_cnt = 0;
3295
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 break;
3297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298
3299/*
3300 * get a character: 3. from the user - update display
3301 */
Bram Moolenaar30613902019-12-01 22:11:18 +01003302 // In insert mode a screen update is skipped when characters
3303 // are still available. But when those available characters
3304 // are part of a mapping, and we are going to do a blocking
3305 // wait here. Need to update the screen to display the
3306 // changed text so far. Also for when 'lazyredraw' is set and
3307 // redrawing was postponed because there was something in the
3308 // input buffer (e.g., termresponse).
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01003309 if (((State & INSERT) != 0 || p_lz) && (State & CMDLINE) == 0
3310 && advance && must_redraw != 0 && !need_wait_return)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 {
3312 update_screen(0);
Bram Moolenaar30613902019-12-01 22:11:18 +01003313 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 }
3315
3316 /*
3317 * If we have a partial match (and are going to wait for more
3318 * input from the user), show the partially matched characters
3319 * to the user with showcmd.
3320 */
3321#ifdef FEAT_CMDL_INFO
Bram Moolenaareda35f72019-08-03 14:59:44 +02003322 showcmd_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323#endif
3324 c1 = 0;
3325 if (typebuf.tb_len > 0 && advance && !exmode_active)
3326 {
3327 if (((State & (NORMAL | INSERT)) || State == LANGMAP)
3328 && State != HITRETURN)
3329 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003330 // this looks nice when typing a dead character map
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 if (State & INSERT
3332 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
3333 + typebuf.tb_len - 1) == 1)
3334 {
3335 edit_putchar(typebuf.tb_buf[typebuf.tb_off
3336 + typebuf.tb_len - 1], FALSE);
Bram Moolenaar30613902019-12-01 22:11:18 +01003337 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 c1 = 1;
3339 }
3340#ifdef FEAT_CMDL_INFO
Bram Moolenaar30613902019-12-01 22:11:18 +01003341 // need to use the col and row from above here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 old_wcol = curwin->w_wcol;
3343 old_wrow = curwin->w_wrow;
3344 curwin->w_wcol = new_wcol;
3345 curwin->w_wrow = new_wrow;
3346 push_showcmd();
3347 if (typebuf.tb_len > SHOWCMD_COLS)
Bram Moolenaareda35f72019-08-03 14:59:44 +02003348 showcmd_idx = typebuf.tb_len - SHOWCMD_COLS;
3349 while (showcmd_idx < typebuf.tb_len)
3350 (void)add_to_showcmd(
3351 typebuf.tb_buf[typebuf.tb_off + showcmd_idx++]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 curwin->w_wcol = old_wcol;
3353 curwin->w_wrow = old_wrow;
3354#endif
3355 }
3356
Bram Moolenaar30613902019-12-01 22:11:18 +01003357 // this looks nice when typing a dead character map
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 if ((State & CMDLINE)
3359#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3360 && cmdline_star == 0
3361#endif
3362 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
3363 + typebuf.tb_len - 1) == 1)
3364 {
3365 putcmdline(typebuf.tb_buf[typebuf.tb_off
3366 + typebuf.tb_len - 1], FALSE);
3367 c1 = 1;
3368 }
3369 }
3370
3371/*
3372 * get a character: 3. from the user - get it
3373 */
Bram Moolenaar83f4cbd2018-06-12 21:35:40 +02003374 if (typebuf.tb_len == 0)
3375 // timedout may have been set while waiting for a mapping
3376 // that has a <Nop> RHS.
3377 timedout = FALSE;
3378
Bram Moolenaar652de232019-04-04 20:13:09 +02003379 if (advance)
3380 {
3381 if (typebuf.tb_len == 0
3382 || !(p_timeout
3383 || (p_ttimeout && keylen == KEYLEN_PART_KEY)))
3384 // blocking wait
3385 wait_time = -1L;
3386 else if (keylen == KEYLEN_PART_KEY && p_ttm >= 0)
3387 wait_time = p_ttm;
3388 else
3389 wait_time = p_tm;
3390 }
3391 else
3392 wait_time = 0;
3393
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003394 wait_tb_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 c = inchar(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len,
3396 typebuf.tb_buflen - typebuf.tb_off - typebuf.tb_len - 1,
Bram Moolenaar652de232019-04-04 20:13:09 +02003397 wait_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398
3399#ifdef FEAT_CMDL_INFO
Bram Moolenaareda35f72019-08-03 14:59:44 +02003400 if (showcmd_idx != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 pop_showcmd();
3402#endif
3403 if (c1 == 1)
3404 {
3405 if (State & INSERT)
3406 edit_unputchar();
3407 if (State & CMDLINE)
3408 unputcmdline();
Bram Moolenaarbc256d92012-06-06 12:06:15 +02003409 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003410 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 }
3412
3413 if (c < 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003414 continue; // end of input script reached
3415 if (c == NUL) // no character available
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 {
3417 if (!advance)
3418 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01003419 if (wait_tb_len > 0) // timed out
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 {
3421 timedout = TRUE;
3422 continue;
3423 }
3424 }
3425 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003426 { // allow mapping for just typed characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 while (typebuf.tb_buf[typebuf.tb_off
3428 + typebuf.tb_len] != NUL)
3429 typebuf.tb_noremap[typebuf.tb_off
3430 + typebuf.tb_len++] = RM_YES;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003431#ifdef HAVE_INPUT_METHOD
Bram Moolenaar30613902019-12-01 22:11:18 +01003432 // Get IM status right after getting keys, not after the
3433 // timeout for a mapping (focus may be lost by then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 vgetc_im_active = im_get_status();
3435#endif
3436 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003437 } // for (;;)
3438 } // if (!character from stuffbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439
Bram Moolenaar30613902019-12-01 22:11:18 +01003440 // if advance is FALSE don't loop on NULs
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003441 } while ((c < 0 && c != K_CANCEL) || (advance && c == NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442
3443 /*
3444 * The "INSERT" message is taken care of here:
3445 * if we return an ESC to exit insert mode, the message is deleted
3446 * if we don't return an ESC but deleted the message before, redisplay it
3447 */
Bram Moolenaar09df3122006-01-23 22:23:09 +00003448 if (advance && p_smd && msg_silent == 0 && (State & INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003450 if (c == ESC && !mode_deleted && !no_mapping && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 {
3452 if (typebuf.tb_len && !KeyTyped)
Bram Moolenaar30613902019-12-01 22:11:18 +01003453 redraw_cmdline = TRUE; // delete mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 else
3455 unshowmode(FALSE);
3456 }
3457 else if (c != ESC && mode_deleted)
3458 {
3459 if (typebuf.tb_len && !KeyTyped)
Bram Moolenaar30613902019-12-01 22:11:18 +01003460 redraw_cmdline = TRUE; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 else
3462 showmode();
3463 }
3464 }
3465#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003466 // may unshow different cursor shape
Bram Moolenaarf085f422017-06-07 20:39:47 +02003467 if (gui.in_use && shape_changed)
3468 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469#endif
Bram Moolenaar6edbbd82019-03-10 09:41:51 +01003470 if (timedout && c == ESC)
3471 {
3472 char_u nop_buf[3];
3473
3474 // When recording there will be no timeout. Add a <Nop> after the ESC
3475 // to avoid that it forms a key code with following characters.
3476 nop_buf[0] = K_SPECIAL;
3477 nop_buf[1] = KS_EXTRA;
3478 nop_buf[2] = KE_NOP;
3479 gotchars(nop_buf, 3);
3480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003482 --vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483
3484 return c;
3485}
3486
3487/*
3488 * inchar() - get one character from
3489 * 1. a scriptfile
3490 * 2. the keyboard
3491 *
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003492 * As many characters as we can get (up to 'maxlen') are put in "buf" and
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 * NUL terminated (buffer length must be 'maxlen' + 1).
3494 * Minimum for "maxlen" is 3!!!!
3495 *
3496 * "tb_change_cnt" is the value of typebuf.tb_change_cnt if "buf" points into
3497 * it. When typebuf.tb_change_cnt changes (e.g., when a message is received
3498 * from a remote client) "buf" can no longer be used. "tb_change_cnt" is 0
3499 * otherwise.
3500 *
3501 * If we got an interrupt all input is read until none is available.
3502 *
3503 * If wait_time == 0 there is no waiting for the char.
3504 * If wait_time == n we wait for n msec for a character to arrive.
3505 * If wait_time == -1 we wait forever for a character to arrive.
3506 *
3507 * Return the number of obtained characters.
3508 * Return -1 when end of input script reached.
3509 */
Bram Moolenaarcda77642016-06-04 13:32:35 +02003510 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003511inchar(
3512 char_u *buf,
3513 int maxlen,
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003514 long wait_time) // milliseconds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515{
Bram Moolenaar30613902019-12-01 22:11:18 +01003516 int len = 0; // init for GCC
3517 int retesc = FALSE; // return ESC with gotint
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 int script_char;
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003519 int tb_change_cnt = typebuf.tb_change_cnt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520
Bram Moolenaar30613902019-12-01 22:11:18 +01003521 if (wait_time == -1L || wait_time > 100L) // flush output before waiting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 {
3523 cursor_on();
Bram Moolenaara338adc2018-01-31 20:51:47 +01003524 out_flush_cursor(FALSE, FALSE);
3525#if defined(FEAT_GUI) && defined(FEAT_MOUSESHAPE)
3526 if (gui.in_use && postponed_mouseshape)
3527 update_mouseshape(-1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528#endif
3529 }
3530
3531 /*
3532 * Don't reset these when at the hit-return prompt, otherwise a endless
3533 * recursive loop may result (write error in swapfile, hit-return, timeout
3534 * on char wait, flush swapfile, write error....).
3535 */
3536 if (State != HITRETURN)
3537 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003538 did_outofmem_msg = FALSE; // display out of memory message (again)
3539 did_swapwrite_msg = FALSE; // display swap file write error again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003541 undo_off = FALSE; // restart undo now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542
3543 /*
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00003544 * Get a character from a script file if there is one.
3545 * If interrupted: Stop reading script files, close them all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 */
3547 script_char = -1;
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00003548 while (scriptin[curscript] != NULL && script_char < 0
3549#ifdef FEAT_EVAL
3550 && !ignore_script
3551#endif
3552 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02003554#ifdef MESSAGE_QUEUE
3555 parse_queued_messages();
Bram Moolenaarf2330482008-06-24 20:19:36 +00003556#endif
3557
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 if (got_int || (script_char = getc(scriptin[curscript])) < 0)
3559 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003560 // Reached EOF.
3561 // Careful: closescript() frees typebuf.tb_buf[] and buf[] may
3562 // point inside typebuf.tb_buf[]. Don't use buf[] after this!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 closescript();
3564 /*
3565 * When reading script file is interrupted, return an ESC to get
3566 * back to normal mode.
3567 * Otherwise return -1, because typebuf.tb_buf[] has changed.
3568 */
3569 if (got_int)
3570 retesc = TRUE;
3571 else
3572 return -1;
3573 }
3574 else
3575 {
3576 buf[0] = script_char;
3577 len = 1;
3578 }
3579 }
3580
Bram Moolenaar30613902019-12-01 22:11:18 +01003581 if (script_char < 0) // did not get a character from script
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 {
3583 /*
3584 * If we got an interrupt, skip all previously typed characters and
3585 * return TRUE if quit reading script file.
3586 * Stop reading typeahead when a single CTRL-C was read,
3587 * fill_input_buf() returns this when not able to read from stdin.
3588 * Don't use buf[] here, closescript() may have freed typebuf.tb_buf[]
3589 * and buf may be pointing inside typebuf.tb_buf[].
3590 */
3591 if (got_int)
3592 {
3593#define DUM_LEN MAXMAPLEN * 3 + 3
3594 char_u dum[DUM_LEN + 1];
3595
3596 for (;;)
3597 {
3598 len = ui_inchar(dum, DUM_LEN, 0L, 0);
3599 if (len == 0 || (len == 1 && dum[0] == 3))
3600 break;
3601 }
3602 return retesc;
3603 }
3604
3605 /*
3606 * Always flush the output characters when getting input characters
Bram Moolenaarcb574f42019-01-25 22:29:57 +01003607 * from the user and not just peeking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 */
Bram Moolenaarcb574f42019-01-25 22:29:57 +01003609 if (wait_time == -1L || wait_time > 10L)
3610 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611
3612 /*
3613 * Fill up to a third of the buffer, because each character may be
3614 * tripled below.
3615 */
3616 len = ui_inchar(buf, maxlen / 3, wait_time, tb_change_cnt);
3617 }
3618
Bram Moolenaar30613902019-12-01 22:11:18 +01003619 // If the typebuf was changed further down, it is like nothing was added by
3620 // this call.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 if (typebuf_changed(tb_change_cnt))
3622 return 0;
3623
Bram Moolenaar30613902019-12-01 22:11:18 +01003624 // Note the change in the typeahead buffer, this matters for when
3625 // vgetorpeek() is called recursively, e.g. using getchar(1) in a timer
3626 // function.
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003627 if (len > 0 && ++typebuf.tb_change_cnt == 0)
3628 typebuf.tb_change_cnt = 1;
3629
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003630 return fix_input_buffer(buf, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631}
3632
3633/*
3634 * Fix typed characters for use by vgetc() and check_termcode().
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003635 * "buf[]" must have room to triple the number of bytes!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 * Returns the new length.
3637 */
3638 int
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003639fix_input_buffer(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640{
3641 int i;
3642 char_u *p = buf;
3643
3644 /*
3645 * Two characters are special: NUL and K_SPECIAL.
3646 * When compiled With the GUI CSI is also special.
3647 * Replace NUL by K_SPECIAL KS_ZERO KE_FILLER
3648 * Replace K_SPECIAL by K_SPECIAL KS_SPECIAL KE_FILLER
3649 * Replace CSI by K_SPECIAL KS_EXTRA KE_CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 */
3651 for (i = len; --i >= 0; ++p)
3652 {
3653#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003654 // When the GUI is used any character can come after a CSI, don't
3655 // escape it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 if (gui.in_use && p[0] == CSI && i >= 2)
3657 {
3658 p += 2;
3659 i -= 2;
3660 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003661 // When the GUI is not used CSI needs to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 else if (!gui.in_use && p[0] == CSI)
3663 {
3664 mch_memmove(p + 3, p + 1, (size_t)i);
3665 *p++ = K_SPECIAL;
3666 *p++ = KS_EXTRA;
3667 *p = (int)KE_CSI;
3668 len += 2;
3669 }
3670 else
3671#endif
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003672 if (p[0] == NUL || (p[0] == K_SPECIAL
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003673 // timeout may generate K_CURSORHOLD
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003674 && (i < 2 || p[1] != KS_EXTRA || p[2] != (int)KE_CURSORHOLD)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003675#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003676 // Win32 console passes modifiers
3677 && (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003678# ifdef VIMDLL
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003679 gui.in_use ||
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003680# endif
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003681 (i < 2 || p[1] != KS_MODIFIER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682#endif
3683 ))
3684 {
3685 mch_memmove(p + 3, p + 1, (size_t)i);
3686 p[2] = K_THIRD(p[0]);
3687 p[1] = K_SECOND(p[0]);
3688 p[0] = K_SPECIAL;
3689 p += 2;
3690 len += 2;
3691 }
3692 }
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003693 *p = NUL; // add trailing NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 return len;
3695}
3696
3697#if defined(USE_INPUT_BUF) || defined(PROTO)
3698/*
3699 * Return TRUE when bytes are in the input buffer or in the typeahead buffer.
3700 * Normally the input buffer would be sufficient, but the server_to_input_buf()
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003701 * or feedkeys() may insert characters in the typeahead buffer while we are
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003702 * waiting for input to arrive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 */
3704 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003705input_available(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706{
3707 return (!vim_is_input_buf_empty()
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003708# if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
3709 || typebuf_was_filled
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710# endif
3711 );
3712}
3713#endif
Bram Moolenaar957cf672020-11-12 14:21:06 +01003714
3715/*
3716 * Function passed to do_cmdline() to get the command after a <Cmd> key from
3717 * typeahead.
3718 */
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003719 static char_u *
Bram Moolenaar957cf672020-11-12 14:21:06 +01003720getcmdkeycmd(
3721 int promptc UNUSED,
3722 void *cookie UNUSED,
3723 int indent UNUSED,
3724 getline_opt_T do_concat UNUSED)
3725{
3726 garray_T line_ga;
3727 int c1 = -1;
3728 int c2;
3729 int cmod = 0;
3730 int aborted = FALSE;
3731
3732 ga_init2(&line_ga, 1, 32);
3733
3734 // no mapping for these characters
3735 no_mapping++;
3736
3737 got_int = FALSE;
3738 while (c1 != NUL && !aborted)
3739 {
Bram Moolenaarca359cb2020-11-15 20:49:41 +01003740 if (ga_grow(&line_ga, 32) != OK)
3741 {
3742 aborted = TRUE;
3743 break;
3744 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003745
3746 if (vgetorpeek(FALSE) == NUL)
3747 {
3748 // incomplete <Cmd> is an error, because there is not much the user
3749 // could do in this state.
3750 emsg(_(e_cmd_mapping_must_end_with_cr));
3751 aborted = TRUE;
3752 break;
3753 }
3754
3755 // Get one character at a time.
3756 c1 = vgetorpeek(TRUE);
3757
3758 // Get two extra bytes for special keys
3759 if (c1 == K_SPECIAL)
3760 {
3761 c1 = vgetorpeek(TRUE);
3762 c2 = vgetorpeek(TRUE);
3763 if (c1 == KS_MODIFIER)
3764 {
3765 cmod = c2;
3766 continue;
3767 }
3768 c1 = TO_SPECIAL(c1, c2);
3769 }
Bram Moolenaar4a441202020-11-28 14:43:26 +01003770 if (c1 == Ctrl_V)
3771 {
3772 // CTRL-V is followed by octal, hex or other characters, reverses
3773 // what AppendToRedobuffLit() does.
3774 no_reduce_keys = TRUE; // don't merge modifyOtherKeys
Bram Moolenaar0684e362020-12-03 19:54:42 +01003775 c1 = get_literal(TRUE);
Bram Moolenaar4a441202020-11-28 14:43:26 +01003776 no_reduce_keys = FALSE;
3777 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003778
3779 if (got_int)
3780 aborted = TRUE;
3781 else if (c1 == '\r' || c1 == '\n')
3782 c1 = NUL; // end the line
3783 else if (c1 == ESC)
3784 aborted = TRUE;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003785 else if (c1 == K_COMMAND || c1 == K_SCRIPT_COMMAND)
Bram Moolenaar957cf672020-11-12 14:21:06 +01003786 {
3787 // give a nicer error message for this special case
3788 emsg(_(e_cmd_mapping_must_end_with_cr_before_second_cmd));
3789 aborted = TRUE;
3790 }
3791 else if (IS_SPECIAL(c1))
3792 {
3793 if (c1 == K_SNR)
Bram Moolenaarc77534c2020-11-18 11:34:37 +01003794 ga_concat(&line_ga, (char_u *)"<SNR>");
Bram Moolenaar957cf672020-11-12 14:21:06 +01003795 else
3796 {
3797 semsg(e_cmd_maping_must_not_include_str_key,
3798 get_special_key_name(c1, cmod));
3799 aborted = TRUE;
3800 }
3801 }
3802 else
Bram Moolenaara9725222022-01-16 13:30:33 +00003803 ga_append(&line_ga, c1);
Bram Moolenaar957cf672020-11-12 14:21:06 +01003804
3805 cmod = 0;
3806 }
3807
3808 no_mapping--;
3809
3810 if (aborted)
3811 ga_clear(&line_ga);
3812
3813 return (char_u *)line_ga.ga_data;
3814}
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003815
3816 int
Bram Moolenaard4e2f502022-01-15 18:48:32 +00003817do_cmdkey_command(int key UNUSED, int flags)
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003818{
3819 int res;
3820#ifdef FEAT_EVAL
Bram Moolenaara9725222022-01-16 13:30:33 +00003821 sctx_T save_current_sctx = {-1, 0, 0, 0};
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003822
3823 if (key == K_SCRIPT_COMMAND && last_used_map != NULL)
3824 {
3825 save_current_sctx = current_sctx;
3826 current_sctx = last_used_map->m_script_ctx;
3827 }
3828#endif
3829
3830 res = do_cmdline(NULL, getcmdkeycmd, NULL, flags);
3831
3832#ifdef FEAT_EVAL
Bram Moolenaara9725222022-01-16 13:30:33 +00003833 if (save_current_sctx.sc_sid >= 0)
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003834 current_sctx = save_current_sctx;
3835#endif
3836
3837 return res;
3838}
3839
3840#if defined(FEAT_EVAL) || defined(PROTO)
3841 void
Bram Moolenaarf61c89d2022-01-19 22:51:48 +00003842reset_last_used_map(mapblock_T *mp)
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003843{
Bram Moolenaarf61c89d2022-01-19 22:51:48 +00003844 if (last_used_map == mp)
3845 last_used_map = NULL;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003846}
3847#endif