blob: d510e45d91c551b50e7b06dfdefcd59d1149af91 [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;
Bram Moolenaarddf7dba2022-09-05 16:53:21 +010088int last_used_sid = -1;
Bram Moolenaare32c3c42022-01-15 18:26:04 +000089#endif
90
Bram Moolenaard25c16e2016-01-29 22:13:30 +010091static int read_readbuf(buffheader_T *buf, int advance);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010092static void init_typebuf(void);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010093static void may_sync_undo(void);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020094static void free_typebuf(void);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010095static void closescript(void);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020096static void updatescript(int c);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010097static int vgetorpeek(int);
Bram Moolenaar0f0f2302017-08-30 18:52:56 +020098static int inchar(char_u *buf, int maxlen, long wait_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +000099
100/*
101 * Free and clear a buffer.
102 */
Bram Moolenaarbdff0122020-04-05 18:56:05 +0200103 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100104free_buff(buffheader_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100106 buffblock_T *p, *np;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107
Bram Moolenaar285e3352018-04-18 23:01:13 +0200108 for (p = buf->bh_first.b_next; p != NULL; p = np)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000109 {
110 np = p->b_next;
111 vim_free(p);
112 }
Bram Moolenaar285e3352018-04-18 23:01:13 +0200113 buf->bh_first.b_next = NULL;
Bram Moolenaar166788c2022-01-27 21:56:40 +0000114 buf->bh_curr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115}
116
117/*
118 * Return the contents of a buffer as a single string.
119 * K_SPECIAL and CSI in the returned string are escaped.
120 */
121 static char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100122get_buffcont(
123 buffheader_T *buffer,
Bram Moolenaar30613902019-12-01 22:11:18 +0100124 int dozero) // count == zero is not an error
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125{
126 long_u count = 0;
127 char_u *p = NULL;
128 char_u *p2;
129 char_u *str;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200130 buffblock_T *bp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131
Bram Moolenaar30613902019-12-01 22:11:18 +0100132 // compute the total length of the string
Bram Moolenaar285e3352018-04-18 23:01:13 +0200133 for (bp = buffer->bh_first.b_next; bp != NULL; bp = bp->b_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134 count += (long_u)STRLEN(bp->b_str);
135
Bram Moolenaarf39d9e92023-04-22 22:54:40 +0100136 if ((count > 0 || dozero) && (p = alloc(count + 1)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137 {
138 p2 = p;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200139 for (bp = buffer->bh_first.b_next; bp != NULL; bp = bp->b_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140 for (str = bp->b_str; *str; )
141 *p2++ = *str++;
142 *p2 = NUL;
143 }
Bram Moolenaarf39d9e92023-04-22 22:54:40 +0100144 return p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145}
146
147/*
148 * Return the contents of the record buffer as a single string
149 * and clear the record buffer.
150 * K_SPECIAL and CSI in the returned string are escaped.
151 */
152 char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100153get_recorded(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154{
155 char_u *p;
156 size_t len;
157
158 p = get_buffcont(&recordbuff, TRUE);
159 free_buff(&recordbuff);
160
161 /*
162 * Remove the characters that were added the last time, these must be the
163 * (possibly mapped) characters that stopped the recording.
164 */
165 len = STRLEN(p);
166 if ((int)len >= last_recorded_len)
167 {
168 len -= last_recorded_len;
169 p[len] = NUL;
170 }
171
172 /*
173 * When stopping recording from Insert mode with CTRL-O q, also remove the
174 * CTRL-O.
175 */
176 if (len > 0 && restart_edit != 0 && p[len - 1] == Ctrl_O)
177 p[len - 1] = NUL;
178
179 return (p);
180}
181
182/*
183 * Return the contents of the redo buffer as a single string.
184 * K_SPECIAL and CSI in the returned string are escaped.
185 */
186 char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100187get_inserted(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188{
Bram Moolenaarf2330482008-06-24 20:19:36 +0000189 return get_buffcont(&redobuff, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190}
191
192/*
Bram Moolenaar4e86cba2007-05-06 11:56:32 +0000193 * Add string "s" after the current block of buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194 * K_SPECIAL and CSI should have been escaped already.
195 */
196 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100197add_buff(
198 buffheader_T *buf,
199 char_u *s,
Bram Moolenaar30613902019-12-01 22:11:18 +0100200 long slen) // length of "s" or -1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100202 buffblock_T *p;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200203 long_u len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204
205 if (slen < 0)
206 slen = (long)STRLEN(s);
Bram Moolenaar30613902019-12-01 22:11:18 +0100207 if (slen == 0) // don't add empty strings
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208 return;
209
Bram Moolenaar30613902019-12-01 22:11:18 +0100210 if (buf->bh_first.b_next == NULL) // first add to list
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211 {
212 buf->bh_space = 0;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200213 buf->bh_curr = &(buf->bh_first);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214 }
Bram Moolenaar30613902019-12-01 22:11:18 +0100215 else if (buf->bh_curr == NULL) // buffer has already been read
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216 {
Bram Moolenaar6d057012021-12-31 18:49:43 +0000217 iemsg(_(e_add_to_internal_buffer_that_was_already_read_from));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218 return;
219 }
220 else if (buf->bh_index != 0)
Bram Moolenaar285e3352018-04-18 23:01:13 +0200221 mch_memmove(buf->bh_first.b_next->b_str,
222 buf->bh_first.b_next->b_str + buf->bh_index,
223 STRLEN(buf->bh_first.b_next->b_str + buf->bh_index) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224 buf->bh_index = 0;
225
226 if (buf->bh_space >= (int)slen)
227 {
228 len = (long_u)STRLEN(buf->bh_curr->b_str);
Bram Moolenaarb6356332005-07-18 21:40:44 +0000229 vim_strncpy(buf->bh_curr->b_str + len, s, (size_t)slen);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230 buf->bh_space -= slen;
231 }
232 else
233 {
234 if (slen < MINIMAL_SIZE)
235 len = MINIMAL_SIZE;
236 else
237 len = slen;
Bram Moolenaar47ed5532019-08-08 20:49:14 +0200238 p = alloc(offsetof(buffblock_T, b_str) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239 if (p == NULL)
Bram Moolenaar30613902019-12-01 22:11:18 +0100240 return; // no space, just forget it
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000241 buf->bh_space = (int)(len - slen);
Bram Moolenaarb6356332005-07-18 21:40:44 +0000242 vim_strncpy(p->b_str, s, (size_t)slen);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243
Bram Moolenaar285e3352018-04-18 23:01:13 +0200244 p->b_next = buf->bh_curr->b_next;
245 buf->bh_curr->b_next = p;
246 buf->bh_curr = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248}
249
250/*
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000251 * Delete "slen" bytes from the end of "buf".
252 * Only works when it was just added.
253 */
254 static void
255delete_buff_tail(buffheader_T *buf, int slen)
256{
Bram Moolenaara4bc2dd2022-01-27 19:27:16 +0000257 int len;
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000258
Bram Moolenaar37cf4132022-01-27 20:47:03 +0000259 if (buf->bh_curr == NULL)
Bram Moolenaara4bc2dd2022-01-27 19:27:16 +0000260 return; // nothing to delete
261 len = (int)STRLEN(buf->bh_curr->b_str);
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000262 if (len < slen)
263 return;
264
265 buf->bh_curr->b_str[len - slen] = NUL;
266 buf->bh_space += slen;
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000267}
268
269/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270 * Add number "n" to buffer "buf".
271 */
272 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100273add_num_buff(buffheader_T *buf, long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274{
275 char_u number[32];
276
277 sprintf((char *)number, "%ld", n);
278 add_buff(buf, number, -1L);
279}
280
281/*
282 * Add character 'c' to buffer "buf".
283 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
284 */
285 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100286add_char_buff(buffheader_T *buf, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288 char_u bytes[MB_MAXBYTES + 1];
289 int len;
290 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291 char_u temp[4];
292
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293 if (IS_SPECIAL(c))
294 len = 1;
295 else
296 len = (*mb_char2bytes)(c, bytes);
297 for (i = 0; i < len; ++i)
298 {
299 if (!IS_SPECIAL(c))
300 c = bytes[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301
302 if (IS_SPECIAL(c) || c == K_SPECIAL || c == NUL)
303 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100304 // translate special key code into three byte sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305 temp[0] = K_SPECIAL;
306 temp[1] = K_SECOND(c);
307 temp[2] = K_THIRD(c);
308 temp[3] = NUL;
309 }
310#ifdef FEAT_GUI
311 else if (c == CSI)
312 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100313 // Translate a CSI to a CSI - KS_EXTRA - KE_CSI sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314 temp[0] = CSI;
315 temp[1] = KS_EXTRA;
316 temp[2] = (int)KE_CSI;
317 temp[3] = NUL;
318 }
319#endif
320 else
321 {
322 temp[0] = c;
323 temp[1] = NUL;
324 }
325 add_buff(buf, temp, -1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327}
328
Bram Moolenaar30613902019-12-01 22:11:18 +0100329// First read ahead buffer. Used for translated commands.
Bram Moolenaar285e3352018-04-18 23:01:13 +0200330static buffheader_T readbuf1 = {{NULL, {NUL}}, NULL, 0, 0};
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100331
Bram Moolenaar30613902019-12-01 22:11:18 +0100332// Second read ahead buffer. Used for redo.
Bram Moolenaar285e3352018-04-18 23:01:13 +0200333static buffheader_T readbuf2 = {{NULL, {NUL}}, NULL, 0, 0};
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100334
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335/*
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100336 * Get one byte from the read buffers. Use readbuf1 one first, use readbuf2
337 * if that one is empty.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338 * If advance == TRUE go to the next char.
339 * No translation is done K_SPECIAL and CSI are escaped.
340 */
341 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100342read_readbuffers(int advance)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100344 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100346 c = read_readbuf(&readbuf1, advance);
347 if (c == NUL)
348 c = read_readbuf(&readbuf2, advance);
349 return c;
350}
351
352 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100353read_readbuf(buffheader_T *buf, int advance)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100354{
355 char_u c;
356 buffblock_T *curr;
357
Bram Moolenaar30613902019-12-01 22:11:18 +0100358 if (buf->bh_first.b_next == NULL) // buffer is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359 return NUL;
360
Bram Moolenaar285e3352018-04-18 23:01:13 +0200361 curr = buf->bh_first.b_next;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100362 c = curr->b_str[buf->bh_index];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363
364 if (advance)
365 {
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100366 if (curr->b_str[++buf->bh_index] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367 {
Bram Moolenaar285e3352018-04-18 23:01:13 +0200368 buf->bh_first.b_next = curr->b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369 vim_free(curr);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100370 buf->bh_index = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371 }
372 }
373 return c;
374}
375
376/*
Bram Moolenaar06811f32014-02-15 16:17:07 +0100377 * Prepare the read buffers for reading (if they contain something).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378 */
379 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100380start_stuff(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381{
Bram Moolenaar285e3352018-04-18 23:01:13 +0200382 if (readbuf1.bh_first.b_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 {
Bram Moolenaar285e3352018-04-18 23:01:13 +0200384 readbuf1.bh_curr = &(readbuf1.bh_first);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100385 readbuf1.bh_space = 0;
386 }
Bram Moolenaar285e3352018-04-18 23:01:13 +0200387 if (readbuf2.bh_first.b_next != NULL)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100388 {
Bram Moolenaar285e3352018-04-18 23:01:13 +0200389 readbuf2.bh_curr = &(readbuf2.bh_first);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100390 readbuf2.bh_space = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391 }
392}
393
394/*
395 * Return TRUE if the stuff buffer is empty.
396 */
397 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100398stuff_empty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399{
Bram Moolenaar285e3352018-04-18 23:01:13 +0200400 return (readbuf1.bh_first.b_next == NULL
401 && readbuf2.bh_first.b_next == NULL);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100402}
403
Bram Moolenaar113e1072019-01-20 15:30:40 +0100404#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100405/*
406 * Return TRUE if readbuf1 is empty. There may still be redo characters in
407 * redbuf2.
408 */
409 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100410readbuf1_empty(void)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100411{
Bram Moolenaar285e3352018-04-18 23:01:13 +0200412 return (readbuf1.bh_first.b_next == NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100414#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415
416/*
417 * Set a typeahead character that won't be flushed.
418 */
419 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100420typeahead_noflush(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421{
422 typeahead_char = c;
423}
424
425/*
426 * Remove the contents of the stuff buffer and the mapped characters in the
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100427 * typeahead buffer (used in case of an error). If "flush_typeahead" is true,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428 * flush all typeahead characters (used when interrupted by a CTRL-C).
429 */
430 void
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200431flush_buffers(flush_buffers_T flush_typeahead)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432{
433 init_typebuf();
434
435 start_stuff();
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100436 while (read_readbuffers(TRUE) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437 ;
438
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200439 if (flush_typeahead == FLUSH_MINIMAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 {
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200441 // remove mapped characters at the start only
442 typebuf.tb_off += typebuf.tb_maplen;
443 typebuf.tb_len -= typebuf.tb_maplen;
Bram Moolenaare49b4bb2020-03-11 13:01:40 +0100444#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
445 if (typebuf.tb_len == 0)
446 typebuf_was_filled = FALSE;
447#endif
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200448 }
449 else
450 {
451 // remove typeahead
452 if (flush_typeahead == FLUSH_INPUT)
453 // We have to get all characters, because we may delete the first
454 // part of an escape sequence. In an xterm we get one char at a
455 // time and we have to get them all.
456 while (inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 10L) != 0)
457 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458 typebuf.tb_off = MAXMAPLEN;
459 typebuf.tb_len = 0;
Bram Moolenaar4eb65312017-06-24 18:49:00 +0200460#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar30613902019-12-01 22:11:18 +0100461 // Reset the flag that text received from a client or from feedkeys()
462 // was inserted in the typeahead buffer.
Bram Moolenaar4eb65312017-06-24 18:49:00 +0200463 typebuf_was_filled = FALSE;
464#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466 typebuf.tb_maplen = 0;
467 typebuf.tb_silent = 0;
468 cmd_silent = FALSE;
469 typebuf.tb_no_abbr_cnt = 0;
Bram Moolenaarb8d732e2020-08-05 22:07:26 +0200470 if (++typebuf.tb_change_cnt == 0)
471 typebuf.tb_change_cnt = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472}
473
474/*
475 * The previous contents of the redo buffer is kept in old_redobuffer.
476 * This is used for the CTRL-O <.> command in insert mode.
477 */
478 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100479ResetRedobuff(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000481 if (block_redo)
482 return;
483
484 free_buff(&old_redobuff);
485 old_redobuff = redobuff;
486 redobuff.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487}
488
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100489/*
490 * Discard the contents of the redo buffer and restore the previous redo
491 * buffer.
492 */
493 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100494CancelRedo(void)
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100495{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000496 if (block_redo)
497 return;
498
499 free_buff(&redobuff);
500 redobuff = old_redobuff;
501 old_redobuff.bh_first.b_next = NULL;
502 start_stuff();
503 while (read_readbuffers(TRUE) != NUL)
504 ;
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100505}
506
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507/*
508 * Save redobuff and old_redobuff to save_redobuff and save_old_redobuff.
509 * Used before executing autocommands and user functions.
510 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 void
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200512saveRedobuff(save_redo_T *save_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513{
514 char_u *s;
515
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200516 save_redo->sr_redobuff = redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200517 redobuff.bh_first.b_next = NULL;
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200518 save_redo->sr_old_redobuff = old_redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200519 old_redobuff.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520
Bram Moolenaar30613902019-12-01 22:11:18 +0100521 // Make a copy, so that ":normal ." in a function works.
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200522 s = get_buffcont(&save_redo->sr_redobuff, FALSE);
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000523 if (s == NULL)
524 return;
525
526 add_buff(&redobuff, s, -1L);
527 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528}
529
530/*
531 * Restore redobuff and old_redobuff from save_redobuff and save_old_redobuff.
532 * Used after executing autocommands and user functions.
533 */
534 void
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200535restoreRedobuff(save_redo_T *save_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536{
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200537 free_buff(&redobuff);
538 redobuff = save_redo->sr_redobuff;
539 free_buff(&old_redobuff);
540 old_redobuff = save_redo->sr_old_redobuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542
543/*
544 * Append "s" to the redo buffer.
545 * K_SPECIAL and CSI should already have been escaped.
546 */
547 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100548AppendToRedobuff(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549{
550 if (!block_redo)
551 add_buff(&redobuff, s, -1L);
552}
553
554/*
555 * Append to Redo buffer literally, escaping special characters with CTRL-V.
556 * K_SPECIAL and CSI are escaped as well.
557 */
558 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100559AppendToRedobuffLit(
560 char_u *str,
Bram Moolenaar30613902019-12-01 22:11:18 +0100561 int len) // length of "str" or -1 for up to the NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562{
Bram Moolenaarebefac62005-12-28 22:39:57 +0000563 char_u *s = str;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564 int c;
565 char_u *start;
566
567 if (block_redo)
568 return;
569
Bram Moolenaarebefac62005-12-28 22:39:57 +0000570 while (len < 0 ? *s != NUL : s - str < len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100572 // Put a string of normal characters in the redo buffer (that's
573 // faster).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 start = s;
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000575 while (*s >= ' ' && *s < DEL && (len < 0 || s - str < len))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 ++s;
577
Bram Moolenaar30613902019-12-01 22:11:18 +0100578 // Don't put '0' or '^' as last character, just in case a CTRL-D is
579 // typed next.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 if (*s == NUL && (s[-1] == '0' || s[-1] == '^'))
581 --s;
582 if (s > start)
583 add_buff(&redobuff, start, (long)(s - start));
584
Bram Moolenaarebefac62005-12-28 22:39:57 +0000585 if (*s == NUL || (len >= 0 && s - str >= len))
586 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587
Bram Moolenaar30613902019-12-01 22:11:18 +0100588 // Handle a special or multibyte character.
Bram Moolenaarebefac62005-12-28 22:39:57 +0000589 if (has_mbyte)
Bram Moolenaar30613902019-12-01 22:11:18 +0100590 // Handle composing chars separately.
Bram Moolenaarebefac62005-12-28 22:39:57 +0000591 c = mb_cptr2char_adv(&s);
592 else
Bram Moolenaarebefac62005-12-28 22:39:57 +0000593 c = *s++;
594 if (c < ' ' || c == DEL || (*s == NUL && (c == '0' || c == '^')))
595 add_char_buff(&redobuff, Ctrl_V);
596
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000597 // CTRL-V '0' must be inserted as CTRL-V 048
Bram Moolenaarebefac62005-12-28 22:39:57 +0000598 if (*s == NUL && c == '0')
Bram Moolenaarebefac62005-12-28 22:39:57 +0000599 add_buff(&redobuff, (char_u *)"048", 3L);
Bram Moolenaarebefac62005-12-28 22:39:57 +0000600 else
601 add_char_buff(&redobuff, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 }
603}
604
605/*
zeertzjq3ab3a862023-05-06 16:22:04 +0100606 * Append "s" to the redo buffer, leaving 3-byte special key codes unmodified
607 * and escaping other K_SPECIAL and CSI bytes.
608 */
609 void
610AppendToRedobuffSpec(char_u *s)
611{
612 while (*s != NUL)
613 {
614 if (*s == K_SPECIAL && s[1] != NUL && s[2] != NUL)
615 {
616 // insert special key literally
617 add_buff(&redobuff, s, 3L);
618 s += 3;
619 }
620 else
621 add_char_buff(&redobuff, mb_cptr2char_adv(&s));
622 }
623}
624
625/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 * Append a character to the redo buffer.
627 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
628 */
629 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100630AppendCharToRedobuff(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631{
632 if (!block_redo)
633 add_char_buff(&redobuff, c);
634}
635
636/*
637 * Append a number to the redo buffer.
638 */
639 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100640AppendNumberToRedobuff(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641{
642 if (!block_redo)
643 add_num_buff(&redobuff, n);
644}
645
646/*
647 * Append string "s" to the stuff buffer.
648 * CSI and K_SPECIAL must already have been escaped.
649 */
650 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100651stuffReadbuff(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100653 add_buff(&readbuf1, s, -1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654}
655
Bram Moolenaar4f5ce332014-07-30 16:00:58 +0200656/*
657 * Append string "s" to the redo stuff buffer.
658 * CSI and K_SPECIAL must already have been escaped.
659 */
660 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100661stuffRedoReadbuff(char_u *s)
Bram Moolenaar4f5ce332014-07-30 16:00:58 +0200662{
663 add_buff(&readbuf2, s, -1L);
664}
665
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200666 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100667stuffReadbuffLen(char_u *s, long len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100669 add_buff(&readbuf1, s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670}
671
672#if defined(FEAT_EVAL) || defined(PROTO)
673/*
674 * Stuff "s" into the stuff buffer, leaving special key codes unmodified and
675 * escaping other K_SPECIAL and CSI bytes.
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200676 * Change CR, LF and ESC into a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 */
678 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100679stuffReadbuffSpec(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680{
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200681 int c;
682
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 while (*s != NUL)
684 {
685 if (*s == K_SPECIAL && s[1] != NUL && s[2] != NUL)
686 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100687 // Insert special key literally.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 stuffReadbuffLen(s, 3L);
689 s += 3;
690 }
691 else
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200692 {
zeertzjqe3a529b2022-06-05 19:01:37 +0100693 c = mb_cptr2char_adv(&s);
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200694 if (c == CAR || c == NL || c == ESC)
695 c = ' ';
696 stuffcharReadbuff(c);
697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 }
699}
700#endif
701
702/*
703 * Append a character to the stuff buffer.
704 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
705 */
706 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100707stuffcharReadbuff(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100709 add_char_buff(&readbuf1, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710}
711
712/*
713 * Append a number to the stuff buffer.
714 */
715 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100716stuffnumReadbuff(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100718 add_num_buff(&readbuf1, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719}
720
721/*
Bram Moolenaar11abd092020-05-01 14:26:37 +0200722 * Stuff a string into the typeahead buffer, such that edit() will insert it
723 * literally ("literally" TRUE) or interpret is as typed characters.
724 */
725 void
726stuffescaped(char_u *arg, int literally)
727{
728 int c;
729 char_u *start;
730
731 while (*arg != NUL)
732 {
733 // Stuff a sequence of normal ASCII characters, that's fast. Also
734 // stuff K_SPECIAL to get the effect of a special key when "literally"
735 // is TRUE.
736 start = arg;
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000737 while ((*arg >= ' ' && *arg < DEL)
Bram Moolenaar11abd092020-05-01 14:26:37 +0200738 || (*arg == K_SPECIAL && !literally))
739 ++arg;
740 if (arg > start)
741 stuffReadbuffLen(start, (long)(arg - start));
742
743 // stuff a single special character
744 if (*arg != NUL)
745 {
746 if (has_mbyte)
747 c = mb_cptr2char_adv(&arg);
748 else
749 c = *arg++;
750 if (literally && ((c < ' ' && c != TAB) || c == DEL))
751 stuffcharReadbuff(Ctrl_V);
752 stuffcharReadbuff(c);
753 }
754 }
755}
756
757/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 * Read a character from the redo buffer. Translates K_SPECIAL, CSI and
759 * multibyte characters.
760 * The redo buffer is left as it is.
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100761 * If init is TRUE, prepare for redo, return FAIL if nothing to redo, OK
762 * otherwise.
763 * If old is TRUE, use old_redobuff instead of redobuff.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 */
765 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100766read_redo(int init, int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100768 static buffblock_T *bp;
769 static char_u *p;
770 int c;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100771 int n;
772 char_u buf[MB_MAXBYTES + 1];
773 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774
775 if (init)
776 {
777 if (old_redo)
Bram Moolenaar285e3352018-04-18 23:01:13 +0200778 bp = old_redobuff.bh_first.b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 else
Bram Moolenaar285e3352018-04-18 23:01:13 +0200780 bp = redobuff.bh_first.b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 if (bp == NULL)
782 return FAIL;
783 p = bp->b_str;
784 return OK;
785 }
786 if ((c = *p) != NUL)
787 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100788 // Reverse the conversion done by add_char_buff()
789 // For a multi-byte character get all the bytes and return the
790 // converted character.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 if (has_mbyte && (c != K_SPECIAL || p[1] == KS_SPECIAL))
792 n = MB_BYTE2LEN_CHECK(c);
793 else
794 n = 1;
795 for (i = 0; ; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100797 if (c == K_SPECIAL) // special key or escaped K_SPECIAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 {
799 c = TO_SPECIAL(p[1], p[2]);
800 p += 2;
801 }
802#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +0100803 if (c == CSI) // escaped CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 p += 2;
805#endif
806 if (*++p == NUL && bp->b_next != NULL)
807 {
808 bp = bp->b_next;
809 p = bp->b_str;
810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 buf[i] = c;
Bram Moolenaar30613902019-12-01 22:11:18 +0100812 if (i == n - 1) // last byte of a character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 {
814 if (n != 1)
815 c = (*mb_ptr2char)(buf);
816 break;
817 }
818 c = *p;
Bram Moolenaar30613902019-12-01 22:11:18 +0100819 if (c == NUL) // cannot happen?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 }
822 }
823
824 return c;
825}
826
827/*
828 * Copy the rest of the redo buffer into the stuff buffer (in a slow way).
829 * If old_redo is TRUE, use old_redobuff instead of redobuff.
830 * The escaped K_SPECIAL and CSI are copied without translation.
831 */
832 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100833copy_redo(int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834{
835 int c;
836
837 while ((c = read_redo(FALSE, old_redo)) != NUL)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100838 add_char_buff(&readbuf2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839}
840
841/*
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100842 * Stuff the redo buffer into readbuf2.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 * Insert the redo count into the command.
844 * If "old_redo" is TRUE, the last but one command is repeated
845 * instead of the last command (inserting text). This is used for
846 * CTRL-O <.> in insert mode
847 *
848 * return FAIL for failure, OK otherwise
849 */
850 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100851start_redo(long count, int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852{
853 int c;
854
Bram Moolenaar30613902019-12-01 22:11:18 +0100855 // init the pointers; return if nothing to redo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 if (read_redo(TRUE, old_redo) == FAIL)
857 return FAIL;
858
859 c = read_redo(FALSE, old_redo);
860
Bram Moolenaarddf7dba2022-09-05 16:53:21 +0100861#ifdef FEAT_EVAL
862 if (c == K_SID)
863 {
864 // Copy the <SID>{sid}; sequence
865 add_char_buff(&readbuf2, c);
866 for (;;)
867 {
868 c = read_redo(FALSE, old_redo);
869 add_char_buff(&readbuf2, c);
870 if (!isdigit(c))
871 break;
872 }
873 c = read_redo(FALSE, old_redo);
874 }
875#endif
876
Bram Moolenaar30613902019-12-01 22:11:18 +0100877 // copy the buffer name, if present
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 if (c == '"')
879 {
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100880 add_buff(&readbuf2, (char_u *)"\"", 1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 c = read_redo(FALSE, old_redo);
882
Bram Moolenaar30613902019-12-01 22:11:18 +0100883 // if a numbered buffer is used, increment the number
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 if (c >= '1' && c < '9')
885 ++c;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100886 add_char_buff(&readbuf2, c);
Bram Moolenaar833093b2018-05-23 21:53:52 +0200887
Bram Moolenaar30613902019-12-01 22:11:18 +0100888 // the expression register should be re-evaluated
Bram Moolenaar833093b2018-05-23 21:53:52 +0200889 if (c == '=')
890 {
891 add_char_buff(&readbuf2, CAR);
892 cmd_silent = TRUE;
893 }
894
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 c = read_redo(FALSE, old_redo);
896 }
897
Bram Moolenaar30613902019-12-01 22:11:18 +0100898 if (c == 'v') // redo Visual
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 {
900 VIsual = curwin->w_cursor;
901 VIsual_active = TRUE;
902 VIsual_select = FALSE;
903 VIsual_reselect = TRUE;
904 redo_VIsual_busy = TRUE;
905 c = read_redo(FALSE, old_redo);
906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907
Bram Moolenaar30613902019-12-01 22:11:18 +0100908 // try to enter the count (in place of a previous count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 if (count)
910 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100911 while (VIM_ISDIGIT(c)) // skip "old" count
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 c = read_redo(FALSE, old_redo);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100913 add_num_buff(&readbuf2, count);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 }
915
Bram Moolenaarddf7dba2022-09-05 16:53:21 +0100916 // copy the rest from the redo buffer into the stuff buffer
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100917 add_char_buff(&readbuf2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 copy_redo(old_redo);
919 return OK;
920}
921
922/*
923 * Repeat the last insert (R, o, O, a, A, i or I command) by stuffing
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100924 * the redo buffer into readbuf2.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 * return FAIL for failure, OK otherwise
926 */
927 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100928start_redo_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929{
930 int c;
931
932 if (read_redo(TRUE, FALSE) == FAIL)
933 return FAIL;
934 start_stuff();
935
Bram Moolenaar30613902019-12-01 22:11:18 +0100936 // skip the count and the command character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 while ((c = read_redo(FALSE, FALSE)) != NUL)
938 {
939 if (vim_strchr((char_u *)"AaIiRrOo", c) != NULL)
940 {
941 if (c == 'O' || c == 'o')
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100942 add_buff(&readbuf2, NL_STR, -1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 break;
944 }
945 }
946
Bram Moolenaar30613902019-12-01 22:11:18 +0100947 // copy the typed text from the redo buffer into the stuff buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 copy_redo(FALSE);
949 block_redo = TRUE;
950 return OK;
951}
952
953 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100954stop_redo_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955{
956 block_redo = FALSE;
957}
958
959/*
960 * Initialize typebuf.tb_buf to point to typebuf_init.
961 * alloc() cannot be used here: In out-of-memory situations it would
962 * be impossible to type anything.
963 */
964 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100965init_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000967 if (typebuf.tb_buf != NULL)
968 return;
969
970 typebuf.tb_buf = typebuf_init;
971 typebuf.tb_noremap = noremapbuf_init;
972 typebuf.tb_buflen = TYPELEN_INIT;
973 typebuf.tb_len = 0;
974 typebuf.tb_off = MAXMAPLEN + 4;
975 typebuf.tb_change_cnt = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976}
977
978/*
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200979 * Returns TRUE when keys cannot be remapped.
980 */
981 int
982noremap_keys(void)
983{
984 return KeyNoremap & (RM_NONE|RM_SCRIPT);
985}
986
987/*
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100988 * Insert a string in position 'offset' in the typeahead buffer (for "@r"
989 * and ":normal" command, vgetorpeek() and check_termcode()).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 *
Bram Moolenaar88456cd2022-11-18 22:14:09 +0000991 * If "noremap" is REMAP_YES, new string can be mapped again.
992 * If "noremap" is REMAP_NONE, new string cannot be mapped again.
993 * If "noremap" is REMAP_SKIP, first char of new string cannot be mapped again,
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000994 * but abbreviations are allowed.
Bram Moolenaar88456cd2022-11-18 22:14:09 +0000995 * If "noremap" is REMAP_SCRIPT, new string cannot be mapped again, except for
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 * script-local mappings.
Bram Moolenaar88456cd2022-11-18 22:14:09 +0000997 * If "noremap" is > 0, that many characters of the new string cannot be mapped.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 *
Bram Moolenaar88456cd2022-11-18 22:14:09 +0000999 * If "nottyped" is TRUE, the string does not return KeyTyped (don't use when
1000 * "offset" is non-zero!).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 *
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001002 * If "silent" is TRUE, cmd_silent is set when the characters are obtained.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 *
1004 * return FAIL for failure, OK otherwise
1005 */
1006 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001007ins_typebuf(
1008 char_u *str,
1009 int noremap,
1010 int offset,
1011 int nottyped,
1012 int silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013{
1014 char_u *s1, *s2;
1015 int newlen;
1016 int addlen;
1017 int i;
1018 int newoff;
1019 int val;
1020 int nrm;
1021
1022 init_typebuf();
1023 if (++typebuf.tb_change_cnt == 0)
1024 typebuf.tb_change_cnt = 1;
Bram Moolenaard103ee72019-09-18 21:15:31 +02001025 state_no_longer_safe("ins_typebuf()");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026
1027 addlen = (int)STRLEN(str);
Bram Moolenaar4e86cba2007-05-06 11:56:32 +00001028
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 if (offset == 0 && addlen <= typebuf.tb_off)
1030 {
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001031 /*
1032 * Easy case: there is room in front of typebuf.tb_buf[typebuf.tb_off]
1033 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 typebuf.tb_off -= addlen;
1035 mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen);
1036 }
Bram Moolenaard34f9b12017-04-08 18:41:13 +02001037 else if (typebuf.tb_len == 0 && typebuf.tb_buflen
1038 >= addlen + 3 * (MAXMAPLEN + 4))
1039 {
1040 /*
1041 * Buffer is empty and string fits in the existing buffer.
1042 * Leave some space before and after, if possible.
1043 */
1044 typebuf.tb_off = (typebuf.tb_buflen - addlen - 3 * (MAXMAPLEN + 4)) / 2;
1045 mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen);
1046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 else
1048 {
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001049 int extra;
1050
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001051 /*
1052 * Need to allocate a new buffer.
Bram Moolenaard34f9b12017-04-08 18:41:13 +02001053 * In typebuf.tb_buf there must always be room for 3 * (MAXMAPLEN + 4)
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001054 * characters. We add some extra room to avoid having to allocate too
1055 * often.
1056 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 newoff = MAXMAPLEN + 4;
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001058 extra = addlen + newoff + 4 * (MAXMAPLEN + 4);
1059 if (typebuf.tb_len > 2147483647 - extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 {
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001061 // string is getting too long for a 32 bit int
Bram Moolenaar12f3c1b2021-12-05 21:46:34 +00001062 emsg(_(e_command_too_complex)); // also calls flush_buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 setcursor();
1064 return FAIL;
1065 }
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001066 newlen = typebuf.tb_len + extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 s1 = alloc(newlen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001068 if (s1 == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 return FAIL;
1070 s2 = alloc(newlen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001071 if (s2 == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 {
1073 vim_free(s1);
1074 return FAIL;
1075 }
1076 typebuf.tb_buflen = newlen;
1077
Bram Moolenaar30613902019-12-01 22:11:18 +01001078 // copy the old chars, before the insertion point
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 mch_memmove(s1 + newoff, typebuf.tb_buf + typebuf.tb_off,
1080 (size_t)offset);
Bram Moolenaar30613902019-12-01 22:11:18 +01001081 // copy the new chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 mch_memmove(s1 + newoff + offset, str, (size_t)addlen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001083 // copy the old chars, after the insertion point, including the NUL at
1084 // the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 mch_memmove(s1 + newoff + offset + addlen,
1086 typebuf.tb_buf + typebuf.tb_off + offset,
1087 (size_t)(typebuf.tb_len - offset + 1));
1088 if (typebuf.tb_buf != typebuf_init)
1089 vim_free(typebuf.tb_buf);
1090 typebuf.tb_buf = s1;
1091
1092 mch_memmove(s2 + newoff, typebuf.tb_noremap + typebuf.tb_off,
1093 (size_t)offset);
1094 mch_memmove(s2 + newoff + offset + addlen,
1095 typebuf.tb_noremap + typebuf.tb_off + offset,
1096 (size_t)(typebuf.tb_len - offset));
1097 if (typebuf.tb_noremap != noremapbuf_init)
1098 vim_free(typebuf.tb_noremap);
1099 typebuf.tb_noremap = s2;
1100
1101 typebuf.tb_off = newoff;
1102 }
1103 typebuf.tb_len += addlen;
1104
Bram Moolenaar30613902019-12-01 22:11:18 +01001105 // If noremap == REMAP_SCRIPT: do remap script-local mappings.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 if (noremap == REMAP_SCRIPT)
1107 val = RM_SCRIPT;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001108 else if (noremap == REMAP_SKIP)
1109 val = RM_ABBR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 else
1111 val = RM_NONE;
1112
1113 /*
1114 * Adjust typebuf.tb_noremap[] for the new characters:
1115 * If noremap == REMAP_NONE or REMAP_SCRIPT: new characters are
1116 * (sometimes) not remappable
1117 * If noremap == REMAP_YES: all the new characters are mappable
1118 * If noremap > 0: "noremap" characters are not remappable, the rest
1119 * mappable
1120 */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001121 if (noremap == REMAP_SKIP)
1122 nrm = 1;
1123 else if (noremap < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 nrm = addlen;
1125 else
1126 nrm = noremap;
1127 for (i = 0; i < addlen; ++i)
1128 typebuf.tb_noremap[typebuf.tb_off + i + offset] =
1129 (--nrm >= 0) ? val : RM_YES;
1130
Bram Moolenaar30613902019-12-01 22:11:18 +01001131 // tb_maplen and tb_silent only remember the length of mapped and/or
1132 // silent mappings at the start of the buffer, assuming that a mapped
1133 // sequence doesn't result in typed characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 if (nottyped || typebuf.tb_maplen > offset)
1135 typebuf.tb_maplen += addlen;
1136 if (silent || typebuf.tb_silent > offset)
1137 {
1138 typebuf.tb_silent += addlen;
1139 cmd_silent = TRUE;
1140 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001141 if (typebuf.tb_no_abbr_cnt && offset == 0) // and not used for abbrev.s
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 typebuf.tb_no_abbr_cnt += addlen;
1143
1144 return OK;
1145}
1146
1147/*
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001148 * Put character "c" back into the typeahead buffer.
1149 * Can be used for a character obtained by vgetc() that needs to be put back.
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00001150 * Uses cmd_silent, KeyTyped and KeyNoremap to restore the flags belonging to
1151 * the char.
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001152 * Returns the length of what was inserted.
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001153 */
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001154 int
zeertzjq2e7cba32022-06-10 15:30:32 +01001155ins_char_typebuf(int c, int modifiers)
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001156{
zeertzjq6cac7702022-01-04 18:01:21 +00001157 char_u buf[MB_MAXBYTES * 3 + 4];
zeertzjq2e7cba32022-06-10 15:30:32 +01001158 int len = special_to_buf(c, modifiers, TRUE, buf);
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001159
zeertzjq2e7cba32022-06-10 15:30:32 +01001160 buf[len] = NUL;
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00001161 (void)ins_typebuf(buf, KeyNoremap, 0, !KeyTyped, cmd_silent);
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001162 return len;
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001163}
1164
1165/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 * Return TRUE if the typeahead buffer was changed (while waiting for a
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001167 * character to arrive). Happens when a message was received from a client or
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001168 * from feedkeys().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 * But check in a more generic way to avoid trouble: When "typebuf.tb_buf"
1170 * changed it was reallocated and the old pointer can no longer be used.
1171 * Or "typebuf.tb_off" may have been changed and we would overwrite characters
1172 * that was just added.
1173 */
1174 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001175typebuf_changed(
Bram Moolenaar30613902019-12-01 22:11:18 +01001176 int tb_change_cnt) // old value of typebuf.tb_change_cnt
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177{
1178 return (tb_change_cnt != 0 && (typebuf.tb_change_cnt != tb_change_cnt
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001179#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
1180 || typebuf_was_filled
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181#endif
1182 ));
1183}
1184
1185/*
1186 * Return TRUE if there are no characters in the typeahead buffer that have
1187 * not been typed (result from a mapping or come from ":normal").
1188 */
1189 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001190typebuf_typed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191{
1192 return typebuf.tb_maplen == 0;
1193}
1194
1195/*
1196 * Return the number of characters that are mapped (or not typed).
1197 */
1198 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001199typebuf_maplen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200{
1201 return typebuf.tb_maplen;
1202}
1203
1204/*
1205 * remove "len" characters from typebuf.tb_buf[typebuf.tb_off + offset]
1206 */
1207 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001208del_typebuf(int len, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209{
1210 int i;
1211
1212 if (len == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01001213 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214
1215 typebuf.tb_len -= len;
1216
1217 /*
1218 * Easy case: Just increase typebuf.tb_off.
1219 */
1220 if (offset == 0 && typebuf.tb_buflen - (typebuf.tb_off + len)
1221 >= 3 * MAXMAPLEN + 3)
1222 typebuf.tb_off += len;
1223 /*
1224 * Have to move the characters in typebuf.tb_buf[] and typebuf.tb_noremap[]
1225 */
1226 else
1227 {
1228 i = typebuf.tb_off + offset;
1229 /*
1230 * Leave some extra room at the end to avoid reallocation.
1231 */
1232 if (typebuf.tb_off > MAXMAPLEN)
1233 {
1234 mch_memmove(typebuf.tb_buf + MAXMAPLEN,
1235 typebuf.tb_buf + typebuf.tb_off, (size_t)offset);
1236 mch_memmove(typebuf.tb_noremap + MAXMAPLEN,
1237 typebuf.tb_noremap + typebuf.tb_off, (size_t)offset);
1238 typebuf.tb_off = MAXMAPLEN;
1239 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001240 // adjust typebuf.tb_buf (include the NUL at the end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset,
1242 typebuf.tb_buf + i + len,
1243 (size_t)(typebuf.tb_len - offset + 1));
Bram Moolenaar30613902019-12-01 22:11:18 +01001244 // adjust typebuf.tb_noremap[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 mch_memmove(typebuf.tb_noremap + typebuf.tb_off + offset,
1246 typebuf.tb_noremap + i + len,
1247 (size_t)(typebuf.tb_len - offset));
1248 }
1249
Bram Moolenaar30613902019-12-01 22:11:18 +01001250 if (typebuf.tb_maplen > offset) // adjust tb_maplen
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 {
1252 if (typebuf.tb_maplen < offset + len)
1253 typebuf.tb_maplen = offset;
1254 else
1255 typebuf.tb_maplen -= len;
1256 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001257 if (typebuf.tb_silent > offset) // adjust tb_silent
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 {
1259 if (typebuf.tb_silent < offset + len)
1260 typebuf.tb_silent = offset;
1261 else
1262 typebuf.tb_silent -= len;
1263 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001264 if (typebuf.tb_no_abbr_cnt > offset) // adjust tb_no_abbr_cnt
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 {
1266 if (typebuf.tb_no_abbr_cnt < offset + len)
1267 typebuf.tb_no_abbr_cnt = offset;
1268 else
1269 typebuf.tb_no_abbr_cnt -= len;
1270 }
1271
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001272#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar30613902019-12-01 22:11:18 +01001273 // Reset the flag that text received from a client or from feedkeys()
1274 // was inserted in the typeahead buffer.
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001275 typebuf_was_filled = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276#endif
1277 if (++typebuf.tb_change_cnt == 0)
1278 typebuf.tb_change_cnt = 1;
1279}
1280
1281/*
1282 * Write typed characters to script file.
1283 * If recording is on put the character in the recordbuffer.
1284 */
1285 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001286gotchars(char_u *chars, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287{
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001288 char_u *s = chars;
1289 int i;
1290 static char_u buf[4];
1291 static int buflen = 0;
1292 int todo = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293
Bram Moolenaar4e86cba2007-05-06 11:56:32 +00001294 while (todo--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 {
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001296 buf[buflen++] = *s++;
1297
1298 // When receiving a special key sequence, store it until we have all
1299 // the bytes and we can decide what to do with it.
1300 if (buflen == 1 && buf[0] == K_SPECIAL)
1301 continue;
1302 if (buflen == 2)
1303 continue;
1304 if (buflen == 3 && buf[1] == KS_EXTRA
1305 && (buf[2] == KE_FOCUSGAINED || buf[2] == KE_FOCUSLOST))
1306 {
1307 // Drop K_FOCUSGAINED and K_FOCUSLOST, they are not useful in a
1308 // recording.
1309 buflen = 0;
1310 continue;
1311 }
1312
Bram Moolenaar30613902019-12-01 22:11:18 +01001313 // Handle one byte at a time; no translation to be done.
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001314 for (i = 0; i < buflen; ++i)
1315 updatescript(buf[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001317 if (reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 {
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001319 buf[buflen] = NUL;
1320 add_buff(&recordbuff, buf, (long)buflen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001321 // remember how many chars were last recorded
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001322 last_recorded_len += buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 }
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001324 buflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 }
1326 may_sync_undo();
1327
1328#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01001329 // output "debug mode" message next time in debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 debug_did_msg = FALSE;
1331#endif
1332
Bram Moolenaar30613902019-12-01 22:11:18 +01001333 // Since characters have been typed, consider the following to be in
1334 // another mapping. Search string will be kept in history.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 ++maptick;
1336}
1337
1338/*
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001339 * Undo the last gotchars() for "len" bytes. To be used when putting a typed
1340 * character back into the typeahead buffer, thus gotchars() will be called
1341 * again.
1342 * Only affects recorded characters.
1343 */
1344 void
1345ungetchars(int len)
1346{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001347 if (reg_recording == 0)
1348 return;
1349
1350 delete_buff_tail(&recordbuff, len);
1351 last_recorded_len -= len;
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001352}
1353
1354/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 * Sync undo. Called when typed characters are obtained from the typeahead
1356 * buffer, or when a menu is used.
1357 * Do not sync:
1358 * - In Insert mode, unless cursor key has been used.
1359 * - While reading a script file.
1360 * - When no_u_sync is non-zero.
1361 */
1362 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001363may_sync_undo(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364{
Bram Moolenaar24959102022-05-07 20:01:16 +01001365 if ((!(State & (MODE_INSERT | MODE_CMDLINE)) || arrow_used)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001366 && scriptin[curscript] == NULL)
1367 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368}
1369
1370/*
1371 * Make "typebuf" empty and allocate new buffers.
1372 * Returns FAIL when out of memory.
1373 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001374 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001375alloc_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376{
1377 typebuf.tb_buf = alloc(TYPELEN_INIT);
1378 typebuf.tb_noremap = alloc(TYPELEN_INIT);
1379 if (typebuf.tb_buf == NULL || typebuf.tb_noremap == NULL)
1380 {
1381 free_typebuf();
1382 return FAIL;
1383 }
1384 typebuf.tb_buflen = TYPELEN_INIT;
Bram Moolenaar30613902019-12-01 22:11:18 +01001385 typebuf.tb_off = MAXMAPLEN + 4; // can insert without realloc
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 typebuf.tb_len = 0;
1387 typebuf.tb_maplen = 0;
1388 typebuf.tb_silent = 0;
1389 typebuf.tb_no_abbr_cnt = 0;
1390 if (++typebuf.tb_change_cnt == 0)
1391 typebuf.tb_change_cnt = 1;
Bram Moolenaare49b4bb2020-03-11 13:01:40 +01001392#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
1393 typebuf_was_filled = FALSE;
1394#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 return OK;
1396}
1397
1398/*
1399 * Free the buffers of "typebuf".
1400 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001401 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001402free_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403{
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001404 if (typebuf.tb_buf == typebuf_init)
Bram Moolenaar95f09602016-11-10 20:01:45 +01001405 internal_error("Free typebuf 1");
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001406 else
Bram Moolenaar0f1c6702019-09-28 15:24:00 +02001407 VIM_CLEAR(typebuf.tb_buf);
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00001408 if (typebuf.tb_noremap == noremapbuf_init)
Bram Moolenaar95f09602016-11-10 20:01:45 +01001409 internal_error("Free typebuf 2");
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001410 else
Bram Moolenaar0f1c6702019-09-28 15:24:00 +02001411 VIM_CLEAR(typebuf.tb_noremap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412}
1413
1414/*
1415 * When doing ":so! file", the current typeahead needs to be saved, and
1416 * restored when "file" has been read completely.
1417 */
1418static typebuf_T saved_typebuf[NSCRIPT];
1419
1420 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001421save_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422{
1423 init_typebuf();
1424 saved_typebuf[curscript] = typebuf;
Bram Moolenaar30613902019-12-01 22:11:18 +01001425 // If out of memory: restore typebuf and close file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 if (alloc_typebuf() == FAIL)
1427 {
1428 closescript();
1429 return FAIL;
1430 }
1431 return OK;
1432}
1433
Bram Moolenaar30613902019-12-01 22:11:18 +01001434static int old_char = -1; // character put back by vungetc()
1435static int old_mod_mask; // mod_mask for ungotten character
1436static int old_mouse_row; // mouse_row related to old_char
1437static int old_mouse_col; // mouse_col related to old_char
zeertzjq0f68e6c2022-04-05 13:17:01 +01001438static int old_KeyStuffed; // whether old_char was stuffed
1439
zeertzjq6d4e7252022-04-07 13:58:04 +01001440static int can_get_old_char(void)
zeertzjq0f68e6c2022-04-05 13:17:01 +01001441{
1442 // If the old character was not stuffed and characters have been added to
1443 // the stuff buffer, need to first get the stuffed characters instead.
1444 return old_char != -1 && (old_KeyStuffed || stuff_empty());
1445}
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001446
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447/*
1448 * Save all three kinds of typeahead, so that the user must type at a prompt.
1449 */
1450 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001451save_typeahead(tasave_T *tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452{
1453 tp->save_typebuf = typebuf;
1454 tp->typebuf_valid = (alloc_typebuf() == OK);
1455 if (!tp->typebuf_valid)
1456 typebuf = tp->save_typebuf;
1457
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001458 tp->old_char = old_char;
1459 tp->old_mod_mask = old_mod_mask;
1460 old_char = -1;
1461
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001462 tp->save_readbuf1 = readbuf1;
Bram Moolenaar285e3352018-04-18 23:01:13 +02001463 readbuf1.bh_first.b_next = NULL;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001464 tp->save_readbuf2 = readbuf2;
Bram Moolenaar285e3352018-04-18 23:01:13 +02001465 readbuf2.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466# ifdef USE_INPUT_BUF
1467 tp->save_inputbuf = get_input_buf();
1468# endif
1469}
1470
1471/*
1472 * Restore the typeahead to what it was before calling save_typeahead().
1473 * The allocated memory is freed, can only be called once!
Bram Moolenaarc41badb2021-06-07 22:04:52 +02001474 * When "overwrite" is FALSE input typed later is kept.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 */
1476 void
Bram Moolenaarc41badb2021-06-07 22:04:52 +02001477restore_typeahead(tasave_T *tp, int overwrite UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478{
1479 if (tp->typebuf_valid)
1480 {
1481 free_typebuf();
1482 typebuf = tp->save_typebuf;
1483 }
1484
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001485 old_char = tp->old_char;
1486 old_mod_mask = tp->old_mod_mask;
1487
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001488 free_buff(&readbuf1);
1489 readbuf1 = tp->save_readbuf1;
1490 free_buff(&readbuf2);
1491 readbuf2 = tp->save_readbuf2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492# ifdef USE_INPUT_BUF
Bram Moolenaarc41badb2021-06-07 22:04:52 +02001493 set_input_buf(tp->save_inputbuf, overwrite);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494# endif
1495}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496
1497/*
1498 * Open a new script file for the ":source!" command.
1499 */
1500 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001501openscript(
1502 char_u *name,
Bram Moolenaar30613902019-12-01 22:11:18 +01001503 int directly) // when TRUE execute directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504{
1505 if (curscript + 1 == NSCRIPT)
1506 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001507 emsg(_(e_scripts_nested_too_deep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 return;
1509 }
Bram Moolenaar53575522019-05-22 22:38:25 +02001510
1511 // Disallow sourcing a file in the sandbox, the commands would be executed
1512 // later, possibly outside of the sandbox.
1513 if (check_secure())
1514 return;
1515
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001516#ifdef FEAT_EVAL
1517 if (ignore_script)
Bram Moolenaar30613902019-12-01 22:11:18 +01001518 // Not reading from script, also don't open one. Warning message?
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001519 return;
1520#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521
Bram Moolenaar30613902019-12-01 22:11:18 +01001522 if (scriptin[curscript] != NULL) // already reading script
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 ++curscript;
Bram Moolenaar30613902019-12-01 22:11:18 +01001524 // use NameBuff for expanded name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 expand_env(name, NameBuff, MAXPATHL);
1526 if ((scriptin[curscript] = mch_fopen((char *)NameBuff, READBIN)) == NULL)
1527 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001528 semsg(_(e_cant_open_file_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 if (curscript)
1530 --curscript;
1531 return;
1532 }
1533 if (save_typebuf() == FAIL)
1534 return;
1535
1536 /*
1537 * Execute the commands from the file right now when using ":source!"
1538 * after ":global" or ":argdo" or in a loop. Also when another command
1539 * follows. This means the display won't be updated. Don't do this
1540 * always, "make test" would fail.
1541 */
1542 if (directly)
1543 {
1544 oparg_T oa;
1545 int oldcurscript;
1546 int save_State = State;
1547 int save_restart_edit = restart_edit;
1548 int save_insertmode = p_im;
1549 int save_finish_op = finish_op;
1550 int save_msg_scroll = msg_scroll;
1551
Bram Moolenaar24959102022-05-07 20:01:16 +01001552 State = MODE_NORMAL;
Bram Moolenaar30613902019-12-01 22:11:18 +01001553 msg_scroll = FALSE; // no msg scrolling in Normal mode
1554 restart_edit = 0; // don't go to Insert mode
1555 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 clear_oparg(&oa);
1557 finish_op = FALSE;
1558
1559 oldcurscript = curscript;
1560 do
1561 {
Bram Moolenaar386b43e2019-05-19 21:57:11 +02001562 update_topline_cursor(); // update cursor position and topline
1563 normal_cmd(&oa, FALSE); // execute one command
Bram Moolenaarae97b942020-07-09 19:16:35 +02001564 (void)vpeekc(); // check for end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 }
1566 while (scriptin[oldcurscript] != NULL);
1567
1568 State = save_State;
1569 msg_scroll = save_msg_scroll;
1570 restart_edit = save_restart_edit;
1571 p_im = save_insertmode;
1572 finish_op = save_finish_op;
1573 }
1574}
1575
1576/*
1577 * Close the currently active input script.
1578 */
1579 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001580closescript(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581{
1582 free_typebuf();
1583 typebuf = saved_typebuf[curscript];
1584
1585 fclose(scriptin[curscript]);
1586 scriptin[curscript] = NULL;
1587 if (curscript > 0)
1588 --curscript;
1589}
1590
Bram Moolenaarea408852005-06-25 22:49:46 +00001591#if defined(EXITFREE) || defined(PROTO)
1592 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001593close_all_scripts(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00001594{
1595 while (scriptin[0] != NULL)
1596 closescript();
1597}
1598#endif
1599
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600/*
1601 * Return TRUE when reading keys from a script file.
1602 */
1603 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001604using_script(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605{
1606 return scriptin[curscript] != NULL;
1607}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608
1609/*
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001610 * This function is called just before doing a blocking wait. Thus after
1611 * waiting 'updatetime' for a character to arrive.
1612 */
1613 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001614before_blocking(void)
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001615{
1616 updatescript(0);
1617#ifdef FEAT_EVAL
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001618 if (may_garbage_collect)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001619 garbage_collect(FALSE);
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001620#endif
1621}
1622
1623/*
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001624 * updatescript() is called when a character can be written into the script
1625 * file or when we have waited some time for a character (c == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 *
1627 * All the changed memfiles are synced if c == 0 or when the number of typed
1628 * characters reaches 'updatecount' and 'updatecount' is non-zero.
1629 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001630 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001631updatescript(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632{
1633 static int count = 0;
1634
1635 if (c && scriptout)
1636 putc(c, scriptout);
1637 if (c == 0 || (p_uc > 0 && ++count >= p_uc))
1638 {
1639 ml_sync_all(c == 0, TRUE);
1640 count = 0;
1641 }
1642}
1643
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644/*
Bram Moolenaar975a8802020-06-06 22:36:24 +02001645 * Convert "c" plus "modifiers" to merge the effect of modifyOtherKeys into the
Bram Moolenaarea83c192023-03-18 17:22:46 +00001646 * character. Also for when the Kitty key protocol is used.
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001647 */
1648 int
Bram Moolenaar975a8802020-06-06 22:36:24 +02001649merge_modifyOtherKeys(int c_arg, int *modifiers)
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001650{
1651 int c = c_arg;
1652
Bram Moolenaarea83c192023-03-18 17:22:46 +00001653 // CTRL only uses the lower 5 bits of the character.
zeertzjqbad8a012022-04-29 16:44:00 +01001654 if (*modifiers & MOD_MASK_CTRL)
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001655 {
1656 if ((c >= '`' && c <= 0x7f) || (c >= '@' && c <= '_'))
zeertzjq17c95d92022-04-26 12:51:07 +01001657 {
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001658 c &= 0x1f;
zeertzjq17c95d92022-04-26 12:51:07 +01001659 if (c == NUL)
1660 c = K_ZERO;
1661 }
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001662 else if (c == '6')
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001663 // CTRL-6 is equivalent to CTRL-^
1664 c = 0x1e;
Bram Moolenaarf4ae6b22020-05-30 19:52:46 +02001665#ifdef FEAT_GUI_GTK
1666 // These mappings look arbitrary at the first glance, but in fact
1667 // resemble quite exactly the behaviour of the GTK+ 1.2 GUI on my
1668 // machine. The only difference is BS vs. DEL for CTRL-8 (makes
1669 // more sense and is consistent with usual terminal behaviour).
1670 else if (c == '2')
1671 c = NUL;
1672 else if (c >= '3' && c <= '7')
1673 c = c ^ 0x28;
1674 else if (c == '8')
1675 c = BS;
1676 else if (c == '?')
1677 c = DEL;
1678#endif
1679 if (c != c_arg)
Bram Moolenaar975a8802020-06-06 22:36:24 +02001680 *modifiers &= ~MOD_MASK_CTRL;
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001681 }
Bram Moolenaarea83c192023-03-18 17:22:46 +00001682
1683 // Alt/Meta sets the 8th bit of the character.
Bram Moolenaar975a8802020-06-06 22:36:24 +02001684 if ((*modifiers & (MOD_MASK_META | MOD_MASK_ALT))
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001685 && c >= 0 && c <= 127)
1686 {
Bram Moolenaarea83c192023-03-18 17:22:46 +00001687 // Some terminals (esp. Kitty) do not include Shift in the character.
1688 // Apply it here to get consistency across terminals. Only do ASCII
1689 // letters, for other characters it depends on the keyboard layout.
1690 if ((*modifiers & MOD_MASK_SHIFT) && c >= 'a' && c <= 'z')
1691 {
1692 c += 'a' - 'A';
1693 *modifiers &= ~MOD_MASK_SHIFT;
1694 }
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001695 c += 0x80;
Bram Moolenaara9a6b032023-02-05 18:00:42 +00001696 *modifiers &= ~(MOD_MASK_META | MOD_MASK_ALT);
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001697 }
Bram Moolenaarea83c192023-03-18 17:22:46 +00001698
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001699 return c;
1700}
1701
1702/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 * Get the next input character.
1704 * Can return a special key or a multi-byte character.
1705 * Can return NUL when called recursively, use safe_vgetc() if that's not
1706 * wanted.
1707 * This translates escaped K_SPECIAL and CSI bytes to a K_SPECIAL or CSI byte.
1708 * Collects the bytes of a multibyte character into the whole character.
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00001709 * Returns the modifiers in the global "mod_mask".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 */
1711 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001712vgetc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713{
1714 int c, c2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001716 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001719#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01001720 // Do garbage collection when garbagecollect() was called previously and
1721 // we are now at the toplevel.
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001722 if (may_garbage_collect && want_garbage_collect)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001723 garbage_collect(FALSE);
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001724#endif
1725
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 /*
1727 * If a character was put back with vungetc, it was already processed.
1728 * Return it directly.
1729 */
zeertzjq0f68e6c2022-04-05 13:17:01 +01001730 if (can_get_old_char())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 {
1732 c = old_char;
1733 old_char = -1;
1734 mod_mask = old_mod_mask;
Bram Moolenaarb8978712013-03-16 21:42:16 +01001735 mouse_row = old_mouse_row;
1736 mouse_col = old_mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001738 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 {
zeertzjq81b46a62022-04-09 17:58:49 +01001740 // number of characters recorded from the last vgetc() call
1741 static int last_vgetc_recorded_len = 0;
1742
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001743 mod_mask = 0;
1744 vgetc_mod_mask = 0;
1745 vgetc_char = 0;
zeertzjq81b46a62022-04-09 17:58:49 +01001746
1747 // last_recorded_len can be larger than last_vgetc_recorded_len
1748 // if peeking records more
1749 last_recorded_len -= last_vgetc_recorded_len;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001750
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001751 for (;;) // this is done twice if there are modifiers
1752 {
1753 int did_inc = FALSE;
Bram Moolenaar2455c4e2015-09-15 18:29:39 +02001754
Bram Moolenaar78aed952022-09-24 15:36:35 +01001755 // No mapping after modifier has been read, using an input method
1756 // and when a popup window has disabled mapping.
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001757 if (mod_mask
Bram Moolenaar39719052017-09-05 22:20:46 +02001758#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001759 || im_is_preediting()
Bram Moolenaar39719052017-09-05 22:20:46 +02001760#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001761#if defined(FEAT_PROP_POPUP)
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001762 || popup_no_mapping()
1763#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001764 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 {
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001766 ++no_mapping;
1767 ++allow_keys;
Bram Moolenaar78aed952022-09-24 15:36:35 +01001768 // mod_mask value may change, remember we did the increment
1769 did_inc = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 }
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01001771 c = vgetorpeek(TRUE);
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001772 if (did_inc)
1773 {
1774 --no_mapping;
1775 --allow_keys;
1776 }
1777
Christopher Plewright03193062022-11-22 12:58:27 +00001778 // Get two extra bytes for special keys, handle modifiers.
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001779 if (c == K_SPECIAL
Christopher Plewright03193062022-11-22 12:58:27 +00001780#ifdef FEAT_GUI
Christopher Plewright605d02a2022-10-19 11:54:46 +01001781 || c == CSI
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001782#endif
1783 )
1784 {
1785 int save_allow_keys = allow_keys;
1786
1787 ++no_mapping;
1788 allow_keys = 0; // make sure BS is not found
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01001789 c2 = vgetorpeek(TRUE); // no mapping for these chars
1790 c = vgetorpeek(TRUE);
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001791 --no_mapping;
1792 allow_keys = save_allow_keys;
1793 if (c2 == KS_MODIFIER)
1794 {
1795 mod_mask = c;
1796 continue;
1797 }
1798 c = TO_SPECIAL(c2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799
Bram Moolenaardffa6ea2022-11-29 20:33:20 +00001800 // K_ESC is used to avoid ambiguity with the single Esc
1801 // character that might be the start of an escape sequence.
1802 // Convert it back to a single Esc here.
1803 if (c == K_ESC)
1804 c = ESC;
1805
Bram Moolenaar4f974752019-02-17 17:44:42 +01001806#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001807 // Handle K_TEAROFF here, the caller of vgetc() doesn't need to
1808 // know that a menu was torn off
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001809 if (
1810# ifdef VIMDLL
1811 gui.in_use &&
1812# endif
1813 c == K_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 {
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001815 char_u name[200];
K.Takata54119102022-02-03 13:33:03 +00001816 int j;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001817
1818 // get menu path, it ends with a <CR>
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01001819 for (j = 0; (c = vgetorpeek(TRUE)) != '\r'; )
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001820 {
K.Takata54119102022-02-03 13:33:03 +00001821 name[j] = c;
1822 if (j < 199)
1823 ++j;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001824 }
K.Takata54119102022-02-03 13:33:03 +00001825 name[j] = NUL;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001826 gui_make_tearoff(name);
1827 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829#endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001830#if defined(FEAT_GUI) && defined(FEAT_GUI_GTK) && defined(FEAT_MENU)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001831 // GTK: <F10> normally selects the menu, but it's passed until
1832 // here to allow mapping it. Intercept and invoke the GTK
1833 // behavior if it's not mapped.
1834 if (c == K_F10 && gui.menubar != NULL)
1835 {
1836 gtk_menu_shell_select_first(
1837 GTK_MENU_SHELL(gui.menubar), FALSE);
1838 continue;
1839 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001840#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841#ifdef FEAT_GUI
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001842 // Handle focus event here, so that the caller doesn't need to
1843 // know about it. Return K_IGNORE so that we loop once (needed
1844 // if 'lazyredraw' is set).
1845 if (c == K_FOCUSGAINED || c == K_FOCUSLOST)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001846 {
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001847 ui_focus_change(c == K_FOCUSGAINED);
1848 c = K_IGNORE;
Bram Moolenaara9dd2d32019-04-29 21:58:41 +02001849 }
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001850
1851 // Translate K_CSI to CSI. The special key is only used to
1852 // avoid it being recognized as the start of a special key.
1853 if (c == K_CSI)
1854 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855#endif
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01001856#ifdef FEAT_EVAL
1857 if (c == K_SID)
1858 {
1859 int j;
1860
1861 // Handle <SID>{sid}; Do up to 20 digits for safety.
1862 last_used_sid = 0;
1863 for (j = 0; j < 20 && isdigit(c = vgetorpeek(TRUE)); ++j)
1864 last_used_sid = last_used_sid * 10 + (c - '0');
1865 last_used_map = NULL;
1866 continue;
1867 }
1868#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001869 }
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01001870
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001871 // a keypad or special function key was not mapped, use it like
1872 // its ASCII equivalent
1873 switch (c)
1874 {
1875 case K_KPLUS: c = '+'; break;
1876 case K_KMINUS: c = '-'; break;
1877 case K_KDIVIDE: c = '/'; break;
1878 case K_KMULTIPLY: c = '*'; break;
1879 case K_KENTER: c = CAR; break;
1880 case K_KPOINT:
Bram Moolenaar4f974752019-02-17 17:44:42 +01001881#ifdef MSWIN
Bram Moolenaar8e85db02018-07-27 23:16:51 +02001882 // Can be either '.' or a ',',
1883 // depending on the type of keypad.
1884 c = MapVirtualKey(VK_DECIMAL, 2); break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001885#else
Bram Moolenaar8e85db02018-07-27 23:16:51 +02001886 c = '.'; break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001887#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001888 case K_K0: c = '0'; break;
1889 case K_K1: c = '1'; break;
1890 case K_K2: c = '2'; break;
1891 case K_K3: c = '3'; break;
1892 case K_K4: c = '4'; break;
1893 case K_K5: c = '5'; break;
1894 case K_K6: c = '6'; break;
1895 case K_K7: c = '7'; break;
1896 case K_K8: c = '8'; break;
1897 case K_K9: c = '9'; break;
Bram Moolenaara88d9682005-03-25 21:45:43 +00001898
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001899 case K_XHOME:
1900 case K_ZHOME: if (mod_mask == MOD_MASK_SHIFT)
Bram Moolenaara88d9682005-03-25 21:45:43 +00001901 {
1902 c = K_S_HOME;
1903 mod_mask = 0;
1904 }
1905 else if (mod_mask == MOD_MASK_CTRL)
1906 {
1907 c = K_C_HOME;
1908 mod_mask = 0;
1909 }
1910 else
1911 c = K_HOME;
1912 break;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001913 case K_XEND:
1914 case K_ZEND: if (mod_mask == MOD_MASK_SHIFT)
Bram Moolenaara88d9682005-03-25 21:45:43 +00001915 {
1916 c = K_S_END;
1917 mod_mask = 0;
1918 }
1919 else if (mod_mask == MOD_MASK_CTRL)
1920 {
1921 c = K_C_END;
1922 mod_mask = 0;
1923 }
1924 else
1925 c = K_END;
1926 break;
1927
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001928 case K_XUP: c = K_UP; break;
1929 case K_XDOWN: c = K_DOWN; break;
1930 case K_XLEFT: c = K_LEFT; break;
1931 case K_XRIGHT: c = K_RIGHT; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001934 // For a multi-byte character get all the bytes and return the
1935 // converted character.
1936 // Note: This will loop until enough bytes are received!
1937 if (has_mbyte && (n = MB_BYTE2LEN_CHECK(c)) > 1)
1938 {
1939 ++no_mapping;
1940 buf[0] = c;
1941 for (i = 1; i < n; ++i)
1942 {
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01001943 buf[i] = vgetorpeek(TRUE);
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001944 if (buf[i] == K_SPECIAL
1945#ifdef FEAT_GUI
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001946 || (buf[i] == CSI)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001947#endif
1948 )
1949 {
1950 // Must be a K_SPECIAL - KS_SPECIAL - KE_FILLER
1951 // sequence, which represents a K_SPECIAL (0x80),
1952 // or a CSI - KS_EXTRA - KE_CSI sequence, which
1953 // represents a CSI (0x9B),
1954 // or a K_SPECIAL - KS_EXTRA - KE_CSI, which is CSI
1955 // too.
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01001956 c = vgetorpeek(TRUE);
1957 if (vgetorpeek(TRUE) == KE_CSI && c == KS_EXTRA)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001958 buf[i] = CSI;
1959 }
1960 }
1961 --no_mapping;
1962 c = (*mb_ptr2char)(buf);
1963 }
1964
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02001965 if (vgetc_char == 0)
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001966 {
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001967 vgetc_mod_mask = mod_mask;
1968 vgetc_char = c;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001969 }
Bram Moolenaar00eab7f2019-10-10 21:49:28 +02001970
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001971 break;
1972 }
zeertzjq81b46a62022-04-09 17:58:49 +01001973
1974 last_vgetc_recorded_len = last_recorded_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001976
1977#ifdef FEAT_EVAL
1978 /*
1979 * In the main loop "may_garbage_collect" can be set to do garbage
1980 * collection in the first next vgetc(). It's disabled after that to
1981 * avoid internally used Lists and Dicts to be freed.
1982 */
1983 may_garbage_collect = FALSE;
1984#endif
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001985
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001986#ifdef FEAT_BEVAL_TERM
Bram Moolenaarc2f50542019-07-05 23:24:56 +02001987 if (c != K_MOUSEMOVE && c != K_IGNORE && c != K_CURSORHOLD)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001988 {
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001989 // Don't trigger 'balloonexpr' unless only the mouse was moved.
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001990 bevalexpr_due_set = FALSE;
1991 ui_remove_balloon();
1992 }
1993#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001994#ifdef FEAT_PROP_POPUP
Bram Moolenaar4934ad02020-09-28 22:29:58 +02001995 // Only filter keys that do not come from ":normal". Keys from feedkeys()
1996 // are filtered.
1997 if ((!ex_normal_busy || in_feedkeys) && popup_do_filter(c))
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02001998 {
1999 if (c == Ctrl_C)
2000 got_int = FALSE; // avoid looping
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002001 c = K_IGNORE;
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02002002 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002003#endif
Bram Moolenaar9fecb462006-09-05 10:59:47 +00002004
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002005 // Need to process the character before we know it's safe to do something
2006 // else.
2007 if (c != K_IGNORE)
Bram Moolenaard103ee72019-09-18 21:15:31 +02002008 state_no_longer_safe("key typed");
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002009
Bram Moolenaar9fecb462006-09-05 10:59:47 +00002010 return c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011}
2012
2013/*
2014 * Like vgetc(), but never return a NUL when called recursively, get a key
2015 * directly from the user (ignoring typeahead).
2016 */
2017 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002018safe_vgetc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019{
2020 int c;
2021
2022 c = vgetc();
2023 if (c == NUL)
2024 c = get_keystroke();
2025 return c;
2026}
2027
2028/*
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002029 * Like safe_vgetc(), but loop to handle K_IGNORE.
2030 * Also ignore scrollbar events.
Bram Moolenaar78aed952022-09-24 15:36:35 +01002031 * Does not handle bracketed paste - do not use the result for commands.
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002032 */
Bram Moolenaar78aed952022-09-24 15:36:35 +01002033 static int
2034plain_vgetc_nopaste(void)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002035{
2036 int c;
2037
2038 do
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002039 c = safe_vgetc();
Bram Moolenaar3a006592021-05-05 19:58:17 +02002040 while (c == K_IGNORE
2041 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
2042 || c == K_MOUSEMOVE);
Bram Moolenaar78aed952022-09-24 15:36:35 +01002043 return c;
2044}
2045
2046/*
2047 * Like safe_vgetc(), but loop to handle K_IGNORE.
2048 * Also ignore scrollbar events.
2049 */
2050 int
2051plain_vgetc(void)
2052{
2053 int c = plain_vgetc_nopaste();
Bram Moolenaarec2da362017-01-21 20:04:22 +01002054
2055 if (c == K_PS)
Bram Moolenaar30613902019-12-01 22:11:18 +01002056 // Only handle the first pasted character. Drop the rest, since we
2057 // don't know what to do with it.
Bram Moolenaarec2da362017-01-21 20:04:22 +01002058 c = bracketed_paste(PASTE_ONE_CHAR, FALSE, NULL);
2059
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002060 return c;
2061}
2062
2063/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 * Check if a character is available, such that vgetc() will not block.
2065 * If the next character is a special character or multi-byte, the returned
2066 * character is not valid!.
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02002067 * Returns NUL if no character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 */
2069 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002070vpeekc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071{
zeertzjq0f68e6c2022-04-05 13:17:01 +01002072 if (can_get_old_char())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 return old_char;
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01002074 return vgetorpeek(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075}
2076
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01002077#if defined(FEAT_TERMRESPONSE) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078/*
2079 * Like vpeekc(), but don't allow mapping. Do allow checking for terminal
2080 * codes.
2081 */
2082 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002083vpeekc_nomap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084{
2085 int c;
2086
2087 ++no_mapping;
2088 ++allow_keys;
2089 c = vpeekc();
2090 --no_mapping;
2091 --allow_keys;
2092 return c;
2093}
2094#endif
2095
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096/*
2097 * Check if any character is available, also half an escape sequence.
2098 * Trick: when no typeahead found, but there is something in the typeahead
2099 * buffer, it must be an ESC that is recognized as the start of a key code.
2100 */
2101 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002102vpeekc_any(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103{
2104 int c;
2105
2106 c = vpeekc();
2107 if (c == NUL && typebuf.tb_len > 0)
2108 c = ESC;
2109 return c;
2110}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111
2112/*
2113 * Call vpeekc() without causing anything to be mapped.
2114 * Return TRUE if a character is available, FALSE otherwise.
2115 */
2116 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002117char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118{
2119 int retval;
2120
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01002121#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01002122 // When test_override("char_avail", 1) was called pretend there is no
2123 // typeahead.
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01002124 if (disable_char_avail_for_testing)
2125 return FALSE;
2126#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 ++no_mapping;
2128 retval = vpeekc();
2129 --no_mapping;
2130 return (retval != NUL);
2131}
2132
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002133#if defined(FEAT_EVAL) || defined(PROTO)
2134/*
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002135 * "getchar()" and "getcharstr()" functions
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002136 */
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002137 static void
2138getchar_common(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002139{
2140 varnumber_T n;
2141 int error = FALSE;
2142
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002143 if (in_vim9script() && check_for_opt_bool_arg(argvars, 0) == FAIL)
2144 return;
2145
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002146#ifdef MESSAGE_QUEUE
2147 // vpeekc() used to check for messages, but that caused problems, invoking
2148 // a callback where it was not expected. Some plugins use getchar(1) in a
2149 // loop to await a message, therefore make sure we check for messages here.
2150 parse_queued_messages();
2151#endif
2152
Bram Moolenaar30613902019-12-01 22:11:18 +01002153 // Position the cursor. Needed after a message that ends in a space.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002154 windgoto(msg_row, msg_col);
2155
2156 ++no_mapping;
2157 ++allow_keys;
2158 for (;;)
2159 {
2160 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar30613902019-12-01 22:11:18 +01002161 // getchar(): blocking wait.
Bram Moolenaar78aed952022-09-24 15:36:35 +01002162 n = plain_vgetc_nopaste();
Bram Moolenaarc08cc722020-09-05 17:51:23 +02002163 else if (tv_get_bool_chk(&argvars[0], &error))
Bram Moolenaar30613902019-12-01 22:11:18 +01002164 // getchar(1): only check if char avail
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002165 n = vpeekc_any();
2166 else if (error || vpeekc_any() == NUL)
Bram Moolenaar30613902019-12-01 22:11:18 +01002167 // illegal argument or getchar(0) and no char avail: return zero
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002168 n = 0;
2169 else
Bram Moolenaar15183b42020-09-05 19:59:39 +02002170 // getchar(0) and char avail() != NUL: get a character.
2171 // Note that vpeekc_any() returns K_SPECIAL for K_IGNORE.
2172 n = safe_vgetc();
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002173
Bram Moolenaar15183b42020-09-05 19:59:39 +02002174 if (n == K_IGNORE || n == K_MOUSEMOVE
2175 || n == K_VER_SCROLLBAR || n == K_HOR_SCROLLBAR)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002176 continue;
2177 break;
2178 }
2179 --no_mapping;
2180 --allow_keys;
2181
2182 set_vim_var_nr(VV_MOUSE_WIN, 0);
2183 set_vim_var_nr(VV_MOUSE_WINID, 0);
2184 set_vim_var_nr(VV_MOUSE_LNUM, 0);
2185 set_vim_var_nr(VV_MOUSE_COL, 0);
2186
2187 rettv->vval.v_number = n;
zeertzjqad6c45f2022-02-20 19:05:10 +00002188 if (n != 0 && (IS_SPECIAL(n) || mod_mask != 0))
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002189 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002190 char_u temp[10]; // modifier: 3, mbyte-char: 6, NUL: 1
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002191 int i = 0;
2192
Bram Moolenaar30613902019-12-01 22:11:18 +01002193 // Turn a special key into three bytes, plus modifier.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002194 if (mod_mask != 0)
2195 {
2196 temp[i++] = K_SPECIAL;
2197 temp[i++] = KS_MODIFIER;
2198 temp[i++] = mod_mask;
2199 }
2200 if (IS_SPECIAL(n))
2201 {
2202 temp[i++] = K_SPECIAL;
2203 temp[i++] = K_SECOND(n);
2204 temp[i++] = K_THIRD(n);
2205 }
2206 else if (has_mbyte)
2207 i += (*mb_char2bytes)(n, temp + i);
2208 else
2209 temp[i++] = n;
2210 temp[i++] = NUL;
2211 rettv->v_type = VAR_STRING;
2212 rettv->vval.v_string = vim_strsave(temp);
2213
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002214 if (is_mouse_key(n))
2215 {
2216 int row = mouse_row;
2217 int col = mouse_col;
2218 win_T *win;
2219 linenr_T lnum;
2220 win_T *wp;
2221 int winnr = 1;
2222
2223 if (row >= 0 && col >= 0)
2224 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002225 // Find the window at the mouse coordinates and compute the
2226 // text position.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002227 win = mouse_find_win(&row, &col, FIND_POPUP);
2228 if (win == NULL)
2229 return;
2230 (void)mouse_comp_pos(win, &row, &col, &lnum, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002231#ifdef FEAT_PROP_POPUP
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002232 if (WIN_IS_POPUP(win))
2233 winnr = 0;
2234 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002235#endif
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002236 for (wp = firstwin; wp != win && wp != NULL;
2237 wp = wp->w_next)
2238 ++winnr;
2239 set_vim_var_nr(VV_MOUSE_WIN, winnr);
2240 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
2241 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
2242 set_vim_var_nr(VV_MOUSE_COL, col + 1);
2243 }
2244 }
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002245 }
2246}
2247
2248/*
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002249 * "getchar()" function
2250 */
2251 void
2252f_getchar(typval_T *argvars, typval_T *rettv)
2253{
2254 getchar_common(argvars, rettv);
2255}
2256
2257/*
2258 * "getcharstr()" function
2259 */
2260 void
2261f_getcharstr(typval_T *argvars, typval_T *rettv)
2262{
2263 getchar_common(argvars, rettv);
2264
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00002265 if (rettv->v_type != VAR_NUMBER)
2266 return;
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002267
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00002268 char_u temp[7]; // mbyte-char: 6, NUL: 1
2269 varnumber_T n = rettv->vval.v_number;
2270 int i = 0;
2271
2272 if (n != 0)
2273 {
2274 if (has_mbyte)
2275 i += (*mb_char2bytes)(n, temp + i);
2276 else
2277 temp[i++] = n;
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002278 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00002279 temp[i++] = NUL;
2280 rettv->v_type = VAR_STRING;
2281 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002282}
2283
2284/*
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002285 * "getcharmod()" function
2286 */
2287 void
2288f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
2289{
2290 rettv->vval.v_number = mod_mask;
2291}
2292#endif // FEAT_EVAL
2293
2294#if defined(MESSAGE_QUEUE) || defined(PROTO)
2295# define MAX_REPEAT_PARSE 8
2296
2297/*
2298 * Process messages that have been queued for netbeans or clientserver.
2299 * Also check if any jobs have ended.
h-east965d2ed2021-10-04 21:51:57 +01002300 * These functions can call arbitrary Vim script and should only be called when
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002301 * it is safe to do so.
2302 */
2303 void
2304parse_queued_messages(void)
2305{
Bram Moolenaar1cac7092019-10-22 21:54:31 +02002306 int old_curwin_id;
2307 int old_curbuf_fnum;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002308 int i;
2309 int save_may_garbage_collect = may_garbage_collect;
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002310 static int entered = 0;
Bram Moolenaard103ee72019-09-18 21:15:31 +02002311 int was_safe = get_was_safe_state();
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002312
2313 // Do not handle messages while redrawing, because it may cause buffers to
2314 // change or be wiped while they are being redrawn.
Bram Moolenaar4778b4d2020-11-04 11:03:12 +01002315 // Also bail out when parsing messages was explicitly disabled.
2316 if (updating_screen || dont_parse_messages)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002317 return;
2318
Bram Moolenaar1cac7092019-10-22 21:54:31 +02002319 // If memory allocation fails during startup we'll exit but curbuf or
2320 // curwin could be NULL.
2321 if (curbuf == NULL || curwin == NULL)
2322 return;
2323
2324 old_curbuf_fnum = curbuf->b_fnum;
2325 old_curwin_id = curwin->w_id;
2326
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002327 ++entered;
2328
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002329 // may_garbage_collect is set in main_loop() to do garbage collection when
2330 // blocking to wait on a character. We don't want that while parsing
2331 // messages, a callback may invoke vgetc() while lists and dicts are in use
2332 // in the call stack.
2333 may_garbage_collect = FALSE;
2334
2335 // Loop when a job ended, but don't keep looping forever.
2336 for (i = 0; i < MAX_REPEAT_PARSE; ++i)
2337 {
2338 // For Win32 mch_breakcheck() does not check for input, do it here.
Bram Moolenaar80a8d382020-05-03 22:57:32 +02002339# if (defined(MSWIN) || defined(__HAIKU__)) && defined(FEAT_JOB_CHANNEL)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002340 channel_handle_events(FALSE);
2341# endif
2342
2343# ifdef FEAT_NETBEANS_INTG
2344 // Process the queued netbeans messages.
2345 netbeans_parse_messages();
2346# endif
2347# ifdef FEAT_JOB_CHANNEL
2348 // Write any buffer lines still to be written.
2349 channel_write_any_lines();
2350
2351 // Process the messages queued on channels.
2352 channel_parse_messages();
2353# endif
2354# if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
2355 // Process the queued clientserver messages.
2356 server_parse_messages();
2357# endif
2358# ifdef FEAT_JOB_CHANNEL
2359 // Check if any jobs have ended. If so, repeat the above to handle
2360 // changes, e.g. stdin may have been closed.
2361 if (job_check_ended())
2362 continue;
2363# endif
2364# ifdef FEAT_TERMINAL
2365 free_unused_terminals();
2366# endif
Yee Cheng Chin4314e4f2022-10-08 13:50:05 +01002367
2368# ifdef FEAT_SOUND_MACOSX
2369 process_cfrunloop();
2370# endif
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002371# ifdef FEAT_SOUND_CANBERRA
2372 if (has_sound_callback_in_queue())
2373 invoke_sound_callback();
2374# endif
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002375#ifdef SIGUSR1
2376 if (got_sigusr1)
2377 {
2378 apply_autocmds(EVENT_SIGUSR1, NULL, NULL, FALSE, curbuf);
2379 got_sigusr1 = FALSE;
2380 }
2381#endif
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002382 break;
2383 }
2384
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002385 // When not nested we'll go back to waiting for a typed character. If it
2386 // was safe before then this triggers a SafeStateAgain autocommand event.
Bram Moolenaard103ee72019-09-18 21:15:31 +02002387 if (entered == 1 && was_safe)
Bram Moolenaar37d18072019-09-17 20:28:38 +02002388 may_trigger_safestateagain();
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002389
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002390 may_garbage_collect = save_may_garbage_collect;
2391
2392 // If the current window or buffer changed we need to bail out of the
2393 // waiting loop. E.g. when a job exit callback closes the terminal window.
2394 if (curwin->w_id != old_curwin_id || curbuf->b_fnum != old_curbuf_fnum)
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02002395 ins_char_typebuf(K_IGNORE, 0);
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002396
2397 --entered;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002398}
2399#endif
2400
2401
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002402typedef enum {
2403 map_result_fail, // failed, break loop
2404 map_result_get, // get a character from typeahead
2405 map_result_retry, // try to map again
2406 map_result_nomatch // no matching mapping, get char
2407} map_result_T;
2408
2409/*
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002410 * Check if the bytes at the start of the typeahead buffer are a character used
zeertzjqee446032022-04-30 15:02:22 +01002411 * in Insert mode completion. This includes the form with a CTRL modifier.
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002412 */
2413 static int
zeertzjqee446032022-04-30 15:02:22 +01002414at_ins_compl_key(void)
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002415{
2416 char_u *p = typebuf.tb_buf + typebuf.tb_off;
2417 int c = *p;
2418
2419 if (typebuf.tb_len > 3
Bram Moolenaard979d642022-03-04 14:51:06 +00002420 && (c == K_SPECIAL || c == CSI) // CSI is used by the GUI
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002421 && p[1] == KS_MODIFIER
2422 && (p[2] & MOD_MASK_CTRL))
2423 c = p[3] & 0x1f;
zeertzjqee446032022-04-30 15:02:22 +01002424 return (ctrl_x_mode_not_default() && vim_is_ctrl_x_key(c))
2425 || (compl_status_local() && (c == Ctrl_N || c == Ctrl_P));
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002426}
2427
2428/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002429 * Check if typebuf.tb_buf[] contains a modifier plus key that can be changed
Bram Moolenaar975a8802020-06-06 22:36:24 +02002430 * into just a key, apply that.
2431 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
2432 * + "max_offset"].
zeertzjq12e21e32022-04-27 11:58:01 +01002433 * Return the length of the replaced bytes, 0 if nothing changed, -1 for error.
Bram Moolenaar975a8802020-06-06 22:36:24 +02002434 */
2435 static int
2436check_simplify_modifier(int max_offset)
2437{
2438 int offset;
2439 char_u *tp;
2440
2441 for (offset = 0; offset < max_offset; ++offset)
2442 {
2443 if (offset + 3 >= typebuf.tb_len)
2444 break;
2445 tp = typebuf.tb_buf + typebuf.tb_off + offset;
zeertzjqc47b16a2022-09-05 13:05:29 +01002446 if ((tp[0] == K_SPECIAL || tp[0] == CSI) && tp[1] == KS_MODIFIER)
Bram Moolenaar975a8802020-06-06 22:36:24 +02002447 {
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002448 // A modifier was not used for a mapping, apply it to ASCII keys.
2449 // Shift would already have been applied.
Bram Moolenaar975a8802020-06-06 22:36:24 +02002450 int modifier = tp[2];
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002451 int c = tp[3];
2452 int new_c = merge_modifyOtherKeys(c, &modifier);
Bram Moolenaar975a8802020-06-06 22:36:24 +02002453
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002454 if (new_c != c)
Bram Moolenaar975a8802020-06-06 22:36:24 +02002455 {
2456 char_u new_string[MB_MAXBYTES];
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002457 int len;
Bram Moolenaar975a8802020-06-06 22:36:24 +02002458
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002459 if (offset == 0)
2460 {
2461 // At the start: remember the character and mod_mask before
2462 // merging, in some cases, e.g. at the hit-return prompt,
2463 // they are put back in the typeahead buffer.
2464 vgetc_char = c;
2465 vgetc_mod_mask = tp[2];
2466 }
zeertzjq12e21e32022-04-27 11:58:01 +01002467 if (IS_SPECIAL(new_c))
2468 {
2469 new_string[0] = K_SPECIAL;
2470 new_string[1] = K_SECOND(new_c);
2471 new_string[2] = K_THIRD(new_c);
2472 len = 3;
2473 }
2474 else
2475 len = mb_char2bytes(new_c, new_string);
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002476 if (modifier == 0)
2477 {
2478 if (put_string_in_typebuf(offset, 4, new_string, len,
zeertzjqfc78a032022-04-26 22:11:38 +01002479 NULL, 0, NULL) == FAIL)
Bram Moolenaar975a8802020-06-06 22:36:24 +02002480 return -1;
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002481 }
2482 else
2483 {
2484 tp[2] = modifier;
2485 if (put_string_in_typebuf(offset + 3, 1, new_string, len,
zeertzjqfc78a032022-04-26 22:11:38 +01002486 NULL, 0, NULL) == FAIL)
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002487 return -1;
2488 }
Bram Moolenaar975a8802020-06-06 22:36:24 +02002489 return len;
2490 }
2491 }
2492 }
2493 return 0;
2494}
2495
2496/*
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00002497 * Return TRUE if the terminal sends modifiers with various keys. This is when
2498 * modifyOtherKeys level 2 is enabled or the kitty keyboard protocol is
2499 * enabled.
2500 */
2501 static int
2502key_protocol_enabled(void)
2503{
Bram Moolenaarc255b782022-11-26 19:16:48 +00002504 // If xterm has responded to XTQMODKEYS it overrules seenModifyOtherKeys.
2505 int using_mok = modify_otherkeys_state != MOKS_INITIAL
2506 ? modify_otherkeys_state == MOKS_ENABLED
2507 : seenModifyOtherKeys;
2508 return using_mok || kitty_protocol_state == KKPS_ENABLED;
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00002509}
2510
2511/*
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002512 * Handle mappings in the typeahead buffer.
2513 * - When something was mapped, return map_result_retry for recursive mappings.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002514 * - When nothing mapped and typeahead has a character: return map_result_get.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002515 * - When there is no match yet, return map_result_nomatch, need to get more
2516 * typeahead.
zeertzjq12e21e32022-04-27 11:58:01 +01002517 * - On failure (out of memory) return map_result_fail.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002518 */
2519 static int
2520handle_mapping(
2521 int *keylenp,
2522 int *timedout,
2523 int *mapdepth)
2524{
2525 mapblock_T *mp = NULL;
2526 mapblock_T *mp2;
2527 mapblock_T *mp_match;
2528 int mp_match_len = 0;
2529 int max_mlen = 0;
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002530 int want_termcode = 0; // 1 if termcode expected after max_mlen
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002531 int tb_c1;
2532 int mlen;
2533#ifdef FEAT_LANGMAP
2534 int nolmaplen;
2535#endif
2536 int keylen = *keylenp;
2537 int i;
2538 int local_State = get_real_state();
Bram Moolenaar1fc34222022-03-03 13:56:24 +00002539 int is_plug_map = FALSE;
2540
zeertzjqbb404f52022-07-23 06:25:29 +01002541 // If typeahead starts with <Plug> then remap, even for a "noremap" mapping.
Bram Moolenaaraf043e12022-07-02 12:08:16 +01002542 if (typebuf.tb_len >= 3
2543 && typebuf.tb_buf[typebuf.tb_off] == K_SPECIAL
Bram Moolenaar1fc34222022-03-03 13:56:24 +00002544 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_EXTRA
2545 && typebuf.tb_buf[typebuf.tb_off + 2] == KE_PLUG)
2546 is_plug_map = TRUE;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002547
2548 /*
2549 * Check for a mappable key sequence.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002550 * Walk through one maphash[] list until we find an entry that matches.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002551 *
2552 * Don't look for mappings if:
2553 * - no_mapping set: mapping disabled (e.g. for CTRL-V)
2554 * - maphash_valid not set: no mappings present.
2555 * - typebuf.tb_buf[typebuf.tb_off] should not be remapped
2556 * - in insert or cmdline mode and 'paste' option set
Bram Moolenaareda35f72019-08-03 14:59:44 +02002557 * - waiting for "hit return to continue" and CR or SPACE typed
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002558 * - waiting for a char with --more--
2559 * - in Ctrl-X mode, and we get a valid char for that mode
2560 */
2561 tb_c1 = typebuf.tb_buf[typebuf.tb_off];
2562 if (no_mapping == 0 && is_maphash_valid()
2563 && (no_zero_mapping == 0 || tb_c1 != '0')
Bram Moolenaar1fc34222022-03-03 13:56:24 +00002564 && (typebuf.tb_maplen == 0 || is_plug_map
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002565 || (p_remap
2566 && (typebuf.tb_noremap[typebuf.tb_off]
2567 & (RM_NONE|RM_ABBR)) == 0))
Bram Moolenaar24959102022-05-07 20:01:16 +01002568 && !(p_paste && (State & (MODE_INSERT | MODE_CMDLINE)))
2569 && !(State == MODE_HITRETURN && (tb_c1 == CAR || tb_c1 == ' '))
2570 && State != MODE_ASKMORE
2571 && State != MODE_CONFIRM
zeertzjqee446032022-04-30 15:02:22 +01002572 && !at_ins_compl_key())
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002573 {
Christopher Plewright03193062022-11-22 12:58:27 +00002574#ifdef FEAT_GUI
2575 if (gui.in_use && tb_c1 == CSI && typebuf.tb_len >= 2
2576 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_MODIFIER)
Bram Moolenaarc9983702020-05-28 21:03:53 +02002577 {
2578 // The GUI code sends CSI KS_MODIFIER {flags}, but mappings expect
2579 // K_SPECIAL KS_MODIFIER {flags}.
2580 tb_c1 = K_SPECIAL;
2581 }
2582#endif
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002583#ifdef FEAT_LANGMAP
2584 if (tb_c1 == K_SPECIAL)
2585 nolmaplen = 2;
2586 else
2587 {
Bram Moolenaar24959102022-05-07 20:01:16 +01002588 LANGMAP_ADJUST(tb_c1, (State & (MODE_CMDLINE | MODE_INSERT)) == 0
2589 && get_real_state() != MODE_SELECT);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002590 nolmaplen = 0;
2591 }
2592#endif
2593 // First try buffer-local mappings.
2594 mp = get_buf_maphash_list(local_State, tb_c1);
2595 mp2 = get_maphash_list(local_State, tb_c1);
2596 if (mp == NULL)
2597 {
2598 // There are no buffer-local mappings.
2599 mp = mp2;
2600 mp2 = NULL;
2601 }
Bram Moolenaareda35f72019-08-03 14:59:44 +02002602
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002603 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002604 * Loop until a partly matching mapping is found or all (local)
2605 * mappings have been checked.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002606 * The longest full match is remembered in "mp_match".
Bram Moolenaareda35f72019-08-03 14:59:44 +02002607 * A full match is only accepted if there is no partly match, so "aa"
2608 * and "aaa" can both be mapped.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002609 */
2610 mp_match = NULL;
2611 mp_match_len = 0;
2612 for ( ; mp != NULL;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002613 mp->m_next == NULL ? (mp = mp2, mp2 = NULL) : (mp = mp->m_next))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002614 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002615 // Only consider an entry if the first character matches and it is
2616 // for the current state.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002617 // Skip ":lmap" mappings if keys were mapped.
2618 if (mp->m_keys[0] == tb_c1
2619 && (mp->m_mode & local_State)
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00002620 && !(mp->m_simplified && key_protocol_enabled()
Bram Moolenaar46cd43b2020-06-04 22:22:11 +02002621 && typebuf.tb_maplen == 0)
Christopher Plewright4c366782022-10-20 13:11:15 +01002622 && ((mp->m_mode & MODE_LANGMAP) == 0
2623 || typebuf.tb_maplen == 0))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002624 {
2625#ifdef FEAT_LANGMAP
2626 int nomap = nolmaplen;
zeertzjq49660f52022-10-20 17:59:38 +01002627 int modifiers = 0;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002628#endif
2629 // find the match length of this mapping
2630 for (mlen = 1; mlen < typebuf.tb_len; ++mlen)
2631 {
zeertzjq49660f52022-10-20 17:59:38 +01002632 int c2 = typebuf.tb_buf[typebuf.tb_off + mlen];
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002633#ifdef FEAT_LANGMAP
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002634 if (nomap > 0)
zeertzjq49660f52022-10-20 17:59:38 +01002635 {
2636 if (nomap == 2 && c2 == KS_MODIFIER)
2637 modifiers = 1;
2638 else if (nomap == 1 && modifiers == 1)
2639 modifiers = c2;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002640 --nomap;
zeertzjq49660f52022-10-20 17:59:38 +01002641 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002642 else
zeertzjq49660f52022-10-20 17:59:38 +01002643 {
2644 if (c2 == K_SPECIAL)
2645 nomap = 2;
2646 else if (merge_modifyOtherKeys(c2, &modifiers) == c2)
2647 // Only apply 'langmap' if merging modifiers into
2648 // the key will not result in another character,
2649 // so that 'langmap' behaves consistently in
2650 // different terminals and GUIs.
2651 LANGMAP_ADJUST(c2, TRUE);
2652 modifiers = 0;
2653 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002654#endif
zeertzjq49660f52022-10-20 17:59:38 +01002655 if (mp->m_keys[mlen] != c2)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002656 break;
2657 }
2658
Bram Moolenaareda35f72019-08-03 14:59:44 +02002659 // Don't allow mapping the first byte(s) of a multi-byte char.
2660 // Happens when mapping <M-a> and then changing 'encoding'.
2661 // Beware that 0x80 is escaped.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002662 {
2663 char_u *p1 = mp->m_keys;
2664 char_u *p2 = mb_unescape(&p1);
2665
2666 if (has_mbyte && p2 != NULL
Bram Moolenaar1614a142019-10-06 22:00:13 +02002667 && MB_BYTE2LEN(tb_c1) > mb_ptr2len(p2))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002668 mlen = 0;
2669 }
2670
2671 // Check an entry whether it matches.
2672 // - Full match: mlen == keylen
2673 // - Partly match: mlen == typebuf.tb_len
2674 keylen = mp->m_keylen;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002675 if (mlen == keylen || (mlen == typebuf.tb_len
2676 && typebuf.tb_len < keylen))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002677 {
2678 char_u *s;
2679 int n;
2680
Bram Moolenaareda35f72019-08-03 14:59:44 +02002681 // If only script-local mappings are allowed, check if the
2682 // mapping starts with K_SNR.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002683 s = typebuf.tb_noremap + typebuf.tb_off;
2684 if (*s == RM_SCRIPT
2685 && (mp->m_keys[0] != K_SPECIAL
2686 || mp->m_keys[1] != KS_EXTRA
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01002687 || mp->m_keys[2] != KE_SNR))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002688 continue;
2689
Bram Moolenaareda35f72019-08-03 14:59:44 +02002690 // If one of the typed keys cannot be remapped, skip the
2691 // entry.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002692 for (n = mlen; --n >= 0; )
2693 if (*s++ & (RM_NONE|RM_ABBR))
2694 break;
Bram Moolenaar1fc34222022-03-03 13:56:24 +00002695 if (!is_plug_map && n >= 0)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002696 continue;
2697
2698 if (keylen > typebuf.tb_len)
2699 {
2700 if (!*timedout && !(mp_match != NULL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002701 && mp_match->m_nowait))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002702 {
2703 // break at a partly match
2704 keylen = KEYLEN_PART_MAP;
2705 break;
2706 }
2707 }
2708 else if (keylen > mp_match_len)
2709 {
2710 // found a longer match
2711 mp_match = mp;
2712 mp_match_len = keylen;
2713 }
2714 }
2715 else
Bram Moolenaareda35f72019-08-03 14:59:44 +02002716 // No match; may have to check for termcode at next
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002717 // character. If the first character that didn't match is
2718 // K_SPECIAL then check for a termcode. This isn't perfect
2719 // but should work in most cases.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002720 if (max_mlen < mlen)
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002721 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002722 max_mlen = mlen;
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002723 want_termcode = mp->m_keys[mlen] == K_SPECIAL;
2724 }
2725 else if (max_mlen == mlen && mp->m_keys[mlen] == K_SPECIAL)
2726 want_termcode = 1;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002727 }
2728 }
2729
Bram Moolenaareda35f72019-08-03 14:59:44 +02002730 // If no partly match found, use the longest full match.
Bram Moolenaar196c3852022-03-04 19:22:36 +00002731 if (keylen != KEYLEN_PART_MAP && mp_match != NULL)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002732 {
2733 mp = mp_match;
2734 keylen = mp_match_len;
2735 }
2736 }
2737
2738 /*
2739 * Check for match with 'pastetoggle'
2740 */
Bram Moolenaar24959102022-05-07 20:01:16 +01002741 if (*p_pt != NUL && mp == NULL && (State & (MODE_INSERT | MODE_NORMAL)))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002742 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002743 for (mlen = 0; mlen < typebuf.tb_len && p_pt[mlen]; ++mlen)
2744 if (p_pt[mlen] != typebuf.tb_buf[typebuf.tb_off + mlen])
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002745 break;
2746 if (p_pt[mlen] == NUL) // match
2747 {
2748 // write chars to script file(s)
2749 if (mlen > typebuf.tb_maplen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002750 gotchars(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_maplen,
2751 mlen - typebuf.tb_maplen);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002752
2753 del_typebuf(mlen, 0); // remove the chars
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002754 set_option_value_give_err((char_u *)"paste",
2755 (long)!p_paste, NULL, 0);
Bram Moolenaar24959102022-05-07 20:01:16 +01002756 if (!(State & MODE_INSERT))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002757 {
2758 msg_col = 0;
2759 msg_row = Rows - 1;
2760 msg_clr_eos(); // clear ruler
2761 }
2762 status_redraw_all();
2763 redraw_statuslines();
2764 showmode();
2765 setcursor();
2766 *keylenp = keylen;
2767 return map_result_retry;
2768 }
2769 // Need more chars for partly match.
2770 if (mlen == typebuf.tb_len)
2771 keylen = KEYLEN_PART_KEY;
2772 else if (max_mlen < mlen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002773 // no match, may have to check for termcode at next character
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002774 max_mlen = mlen + 1;
2775 }
2776
Bram Moolenaarbbf84e22022-03-12 13:48:39 +00002777 // May check for a terminal code when there is no mapping or only a partial
2778 // mapping. Also check if there is a full mapping with <Esc>, unless timed
2779 // out, since that is nearly always a partial match with a terminal code.
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002780 if ((mp == NULL || max_mlen + want_termcode > mp_match_len
Bram Moolenaarbbf84e22022-03-12 13:48:39 +00002781 || (mp_match_len == 1 && *mp->m_keys == ESC && !*timedout))
2782 && keylen != KEYLEN_PART_MAP)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002783 {
2784 int save_keylen = keylen;
2785
2786 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002787 * When no matching mapping found or found a non-matching mapping that
2788 * matches at least what the matching mapping matched:
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002789 * Check if we have a terminal code, when:
Bram Moolenaareda35f72019-08-03 14:59:44 +02002790 * - mapping is allowed,
2791 * - keys have not been mapped,
2792 * - and not an ESC sequence, not in insert mode or p_ek is on,
2793 * - and when not timed out,
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002794 */
zeertzjq68a573c2022-04-28 14:10:01 +01002795 if (no_mapping == 0 || allow_keys != 0)
2796 {
2797 if ((typebuf.tb_maplen == 0
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002798 || (p_remap && typebuf.tb_noremap[
Bram Moolenaareda35f72019-08-03 14:59:44 +02002799 typebuf.tb_off] == RM_YES))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002800 && !*timedout)
zeertzjq68a573c2022-04-28 14:10:01 +01002801 keylen = check_termcode(max_mlen + 1, NULL, 0, NULL);
2802 else
2803 keylen = 0;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002804
Bram Moolenaar0684e362020-12-03 19:54:42 +01002805 // If no termcode matched but 'pastetoggle' matched partially
2806 // it's like an incomplete key sequence.
zeertzjq68a573c2022-04-28 14:10:01 +01002807 if (keylen == 0 && save_keylen == KEYLEN_PART_KEY && !*timedout)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002808 keylen = KEYLEN_PART_KEY;
2809
Bram Moolenaar975a8802020-06-06 22:36:24 +02002810 // If no termcode matched, try to include the modifier into the
zeertzjqa3f83fe2021-11-22 12:47:39 +00002811 // key. This is for when modifyOtherKeys is working.
Bram Moolenaarc896adb2022-11-19 19:02:40 +00002812#ifdef FEAT_TERMINAL
2813 check_no_reduce_keys(); // may update the no_reduce_keys flag
2814#endif
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002815 if (keylen == 0 && !no_reduce_keys)
zeertzjq12e21e32022-04-27 11:58:01 +01002816 {
Bram Moolenaar975a8802020-06-06 22:36:24 +02002817 keylen = check_simplify_modifier(max_mlen + 1);
zeertzjq12e21e32022-04-27 11:58:01 +01002818 if (keylen < 0)
2819 // ins_typebuf() failed
2820 return map_result_fail;
2821 }
Bram Moolenaar975a8802020-06-06 22:36:24 +02002822
Bram Moolenaareda35f72019-08-03 14:59:44 +02002823 // When getting a partial match, but the last characters were not
2824 // typed, don't wait for a typed character to complete the
2825 // termcode. This helps a lot when a ":normal" command ends in an
2826 // ESC.
2827 if (keylen < 0 && typebuf.tb_len == typebuf.tb_maplen)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002828 keylen = 0;
2829 }
2830 else
2831 keylen = 0;
2832 if (keylen == 0) // no matching terminal code
2833 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002834#ifdef AMIGA
2835 // check for window bounds report
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002836 if (typebuf.tb_maplen == 0 && (typebuf.tb_buf[
Bram Moolenaareda35f72019-08-03 14:59:44 +02002837 typebuf.tb_off] & 0xff) == CSI)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002838 {
2839 char_u *s;
2840
2841 for (s = typebuf.tb_buf + typebuf.tb_off + 1;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002842 s < typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len
2843 && (VIM_ISDIGIT(*s) || *s == ';' || *s == ' ');
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002844 ++s)
2845 ;
2846 if (*s == 'r' || *s == '|') // found one
2847 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002848 del_typebuf(
2849 (int)(s + 1 - (typebuf.tb_buf + typebuf.tb_off)), 0);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002850 // get size and redraw screen
2851 shell_resized();
2852 *keylenp = keylen;
2853 return map_result_retry;
2854 }
2855 if (*s == NUL) // need more characters
2856 keylen = KEYLEN_PART_KEY;
2857 }
2858 if (keylen >= 0)
2859#endif
Bram Moolenaareda35f72019-08-03 14:59:44 +02002860 // When there was a matching mapping and no termcode could be
2861 // replaced after another one, use that mapping (loop around).
2862 // If there was no mapping at all use the character from the
2863 // typeahead buffer right here.
2864 if (mp == NULL)
2865 {
2866 *keylenp = keylen;
Bram Moolenaar52797ba2021-12-16 14:45:13 +00002867 return map_result_get; // get character from typeahead
Bram Moolenaareda35f72019-08-03 14:59:44 +02002868 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002869 }
2870
2871 if (keylen > 0) // full matching terminal code
2872 {
2873#if defined(FEAT_GUI) && defined(FEAT_MENU)
2874 if (typebuf.tb_len >= 2
Bram Moolenaareda35f72019-08-03 14:59:44 +02002875 && typebuf.tb_buf[typebuf.tb_off] == K_SPECIAL
2876 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_MENU)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002877 {
2878 int idx;
2879
Bram Moolenaareda35f72019-08-03 14:59:44 +02002880 // Using a menu may cause a break in undo! It's like using
2881 // gotchars(), but without recording or writing to a script
2882 // file.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002883 may_sync_undo();
2884 del_typebuf(3, 0);
2885 idx = get_menu_index(current_menu, local_State);
2886 if (idx != MENU_INDEX_INVALID)
2887 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002888 // In Select mode and a Visual mode menu is used: Switch
2889 // to Visual mode temporarily. Append K_SELECT to switch
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002890 // back to Select mode.
2891 if (VIsual_active && VIsual_select
Bram Moolenaar24959102022-05-07 20:01:16 +01002892 && (current_menu->modes & MODE_VISUAL))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002893 {
2894 VIsual_select = FALSE;
2895 (void)ins_typebuf(K_SELECT_STRING,
Bram Moolenaareda35f72019-08-03 14:59:44 +02002896 REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002897 }
2898 ins_typebuf(current_menu->strings[idx],
2899 current_menu->noremap[idx],
Bram Moolenaareda35f72019-08-03 14:59:44 +02002900 0, TRUE, current_menu->silent[idx]);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002901 }
2902 }
2903#endif // FEAT_GUI && FEAT_MENU
2904 *keylenp = keylen;
2905 return map_result_retry; // try mapping again
2906 }
2907
Bram Moolenaareda35f72019-08-03 14:59:44 +02002908 // Partial match: get some more characters. When a matching mapping
2909 // was found use that one.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002910 if (mp == NULL || keylen < 0)
2911 keylen = KEYLEN_PART_KEY;
2912 else
2913 keylen = mp_match_len;
2914 }
2915
2916 /*
2917 * complete match
2918 */
2919 if (keylen >= 0 && keylen <= typebuf.tb_len)
2920 {
2921 char_u *map_str;
2922
2923#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002924 int save_m_expr;
2925 int save_m_noremap;
2926 int save_m_silent;
2927 char_u *save_m_keys;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002928#else
2929# define save_m_noremap mp->m_noremap
2930# define save_m_silent mp->m_silent
2931#endif
2932
2933 // write chars to script file(s)
2934 if (keylen > typebuf.tb_maplen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002935 gotchars(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_maplen,
2936 keylen - typebuf.tb_maplen);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002937
2938 cmd_silent = (typebuf.tb_silent > 0);
2939 del_typebuf(keylen, 0); // remove the mapped keys
2940
2941 /*
2942 * Put the replacement string in front of mapstr.
2943 * The depth check catches ":map x y" and ":map y x".
2944 */
2945 if (++*mapdepth >= p_mmd)
2946 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00002947 emsg(_(e_recursive_mapping));
Bram Moolenaar24959102022-05-07 20:01:16 +01002948 if (State & MODE_CMDLINE)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002949 redrawcmdline();
2950 else
2951 setcursor();
2952 flush_buffers(FLUSH_MINIMAL);
Bram Moolenaar30613902019-12-01 22:11:18 +01002953 *mapdepth = 0; // for next one
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002954 *keylenp = keylen;
2955 return map_result_fail;
2956 }
2957
2958 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002959 * In Select mode and a Visual mode mapping is used: Switch to Visual
2960 * mode temporarily. Append K_SELECT to switch back to Select mode.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002961 */
Bram Moolenaar24959102022-05-07 20:01:16 +01002962 if (VIsual_active && VIsual_select && (mp->m_mode & MODE_VISUAL))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002963 {
2964 VIsual_select = FALSE;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002965 (void)ins_typebuf(K_SELECT_STRING, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002966 }
2967
2968#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002969 // Copy the values from *mp that are used, because evaluating the
2970 // expression may invoke a function that redefines the mapping, thereby
2971 // making *mp invalid.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002972 save_m_expr = mp->m_expr;
2973 save_m_noremap = mp->m_noremap;
2974 save_m_silent = mp->m_silent;
2975 save_m_keys = NULL; // only saved when needed
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002976
2977 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002978 * Handle ":map <expr>": evaluate the {rhs} as an expression. Also
2979 * save and restore the command line for "normal :".
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002980 */
2981 if (mp->m_expr)
2982 {
2983 int save_vgetc_busy = vgetc_busy;
2984 int save_may_garbage_collect = may_garbage_collect;
Bram Moolenaar4ebe0e62019-11-22 20:55:40 +01002985 int was_screen_col = screen_cur_col;
2986 int was_screen_row = screen_cur_row;
Bram Moolenaar74a0a5b2022-02-10 14:07:41 +00002987 int prev_did_emsg = did_emsg;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002988
2989 vgetc_busy = 0;
2990 may_garbage_collect = FALSE;
2991
2992 save_m_keys = vim_strsave(mp->m_keys);
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002993 map_str = eval_map_expr(mp, NUL);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002994
Bram Moolenaar4ebe0e62019-11-22 20:55:40 +01002995 // The mapping may do anything, but we expect it to take care of
2996 // redrawing. Do put the cursor back where it was.
2997 windgoto(was_screen_row, was_screen_col);
2998 out_flush();
2999
Bram Moolenaar74a0a5b2022-02-10 14:07:41 +00003000 // If an error was displayed and the expression returns an empty
3001 // string, generate a <Nop> to allow for a redraw.
3002 if (prev_did_emsg != did_emsg
3003 && (map_str == NULL || *map_str == NUL))
3004 {
3005 char_u buf[4];
3006
3007 vim_free(map_str);
3008 buf[0] = K_SPECIAL;
3009 buf[1] = KS_EXTRA;
3010 buf[2] = KE_IGNORE;
3011 buf[3] = NUL;
3012 map_str = vim_strsave(buf);
Bram Moolenaar24959102022-05-07 20:01:16 +01003013 if (State & MODE_CMDLINE)
Bram Moolenaar74a0a5b2022-02-10 14:07:41 +00003014 {
3015 // redraw the command below the error
3016 msg_didout = TRUE;
3017 if (msg_row < cmdline_row)
3018 msg_row = cmdline_row;
3019 redrawcmd();
3020 }
3021 }
3022
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003023 vgetc_busy = save_vgetc_busy;
3024 may_garbage_collect = save_may_garbage_collect;
3025 }
3026 else
3027#endif
3028 map_str = mp->m_str;
3029
3030 /*
3031 * Insert the 'to' part in the typebuf.tb_buf.
Bram Moolenaareda35f72019-08-03 14:59:44 +02003032 * If 'from' field is the same as the start of the 'to' field, don't
3033 * remap the first character (but do allow abbreviations).
3034 * If m_noremap is set, don't remap the whole 'to' part.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003035 */
3036 if (map_str == NULL)
3037 i = FAIL;
3038 else
3039 {
3040 int noremap;
3041
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01003042#ifdef FEAT_EVAL
3043 last_used_map = mp;
3044 last_used_sid = -1;
3045#endif
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003046 if (save_m_noremap != REMAP_YES)
3047 noremap = save_m_noremap;
3048 else if (
3049#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02003050 STRNCMP(map_str, save_m_keys != NULL ? save_m_keys : mp->m_keys,
3051 (size_t)keylen)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003052#else
3053 STRNCMP(map_str, mp->m_keys, (size_t)keylen)
3054#endif
3055 != 0)
3056 noremap = REMAP_YES;
3057 else
3058 noremap = REMAP_SKIP;
3059 i = ins_typebuf(map_str, noremap,
3060 0, TRUE, cmd_silent || save_m_silent);
3061#ifdef FEAT_EVAL
3062 if (save_m_expr)
3063 vim_free(map_str);
3064#endif
3065 }
3066#ifdef FEAT_EVAL
3067 vim_free(save_m_keys);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003068#endif
3069 *keylenp = keylen;
3070 if (i == FAIL)
3071 return map_result_fail;
3072 return map_result_retry;
3073 }
3074
3075 *keylenp = keylen;
3076 return map_result_nomatch;
3077}
3078
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003079/*
3080 * unget one character (can only be done once!)
zeertzjq6d4e7252022-04-07 13:58:04 +01003081 * If the character was stuffed, vgetc() will get it next time it is called.
zeertzjq0f68e6c2022-04-05 13:17:01 +01003082 * Otherwise vgetc() will only get it when the stuff buffer is empty.
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003083 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003085vungetc(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086{
3087 old_char = c;
3088 old_mod_mask = mod_mask;
Bram Moolenaarb8978712013-03-16 21:42:16 +01003089 old_mouse_row = mouse_row;
3090 old_mouse_col = mouse_col;
zeertzjq0f68e6c2022-04-05 13:17:01 +01003091 old_KeyStuffed = KeyStuffed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092}
3093
3094/*
zeertzjq6d4e7252022-04-07 13:58:04 +01003095 * When peeking and not getting a character, reg_executing cannot be cleared
3096 * yet, so set a flag to clear it later.
3097 */
3098 static void
3099check_end_reg_executing(int advance)
3100{
3101 if (reg_executing != 0 && (typebuf.tb_maplen == 0
3102 || pending_end_reg_executing))
3103 {
3104 if (advance)
3105 {
3106 reg_executing = 0;
3107 pending_end_reg_executing = FALSE;
3108 }
3109 else
3110 pending_end_reg_executing = TRUE;
3111 }
3112
3113}
3114
3115/*
Bram Moolenaar32526b32019-01-19 17:43:09 +01003116 * Get a byte:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 * 1. from the stuffbuffer
3118 * This is used for abbreviated commands like "D" -> "d$".
3119 * Also used to redo a command for ".".
3120 * 2. from the typeahead buffer
3121 * Stores text obtained previously but not used yet.
3122 * Also stores the result of mappings.
3123 * Also used for the ":normal" command.
3124 * 3. from the user
3125 * This may do a blocking wait if "advance" is TRUE.
3126 *
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003127 * if "advance" is TRUE (vgetc()):
Bram Moolenaard29459b2016-08-26 22:29:11 +02003128 * Really get the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 * KeyTyped is set to TRUE in the case the user typed the key.
3130 * KeyStuffed is TRUE if the character comes from the stuff buffer.
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003131 * if "advance" is FALSE (vpeekc()):
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02003132 * Just look whether there is a character available.
3133 * Return NUL if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 *
3135 * When "no_mapping" is zero, checks for mappings in the current mode.
3136 * Only returns one byte (of a multi-byte character).
3137 * K_SPECIAL and CSI may be escaped, need to get two more bytes then.
3138 */
3139 static int
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003140vgetorpeek(int advance)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141{
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003142 int c;
Bram Moolenaarb8329db2022-07-06 13:31:28 +01003143 int timedout = FALSE; // waited for more than 'timeoutlen'
3144 // for mapping to complete or
3145 // 'ttimeoutlen' for complete key code
Bram Moolenaar30613902019-12-01 22:11:18 +01003146 int mapdepth = 0; // check for recursive mapping
3147 int mode_deleted = FALSE; // set when mode has been deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 int new_wcol, new_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003150 int shape_changed = FALSE; // adjusted cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151#endif
3152 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 int old_wcol, old_wrow;
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003154 int wait_tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155
3156 /*
3157 * This function doesn't work very well when called recursively. This may
3158 * happen though, because of:
3159 * 1. The call to add_to_showcmd(). char_avail() is then used to check if
3160 * there is a character available, which calls this function. In that
3161 * case we must return NUL, to indicate no character is available.
3162 * 2. A GUI callback function writes to the screen, causing a
3163 * wait_return().
3164 * Using ":normal" can also do this, but it saves the typeahead buffer,
3165 * thus it should be OK. But don't get a key from the user then.
3166 */
Bram Moolenaare2c38102016-01-31 14:55:40 +01003167 if (vgetc_busy > 0 && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 return NUL;
3169
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003170 ++vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171
3172 if (advance)
Bram Moolenaar8d696372022-08-21 10:40:07 +01003173 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 KeyStuffed = FALSE;
Bram Moolenaar8d696372022-08-21 10:40:07 +01003175 typebuf_was_empty = FALSE;
3176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177
3178 init_typebuf();
3179 start_stuff();
zeertzjq6d4e7252022-04-07 13:58:04 +01003180 check_end_reg_executing(advance);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 do
3182 {
3183/*
3184 * get a character: 1. from the stuffbuffer
3185 */
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003186 if (typeahead_char != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 {
3188 c = typeahead_char;
3189 if (advance)
3190 typeahead_char = 0;
3191 }
3192 else
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01003193 c = read_readbuffers(advance);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 if (c != NUL && !got_int)
3195 {
3196 if (advance)
3197 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003198 // KeyTyped = FALSE; When the command that stuffed something
3199 // was typed, behave like the stuffed command was typed.
3200 // needed for CTRL-W CTRL-] to open a fold, for example.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 KeyStuffed = TRUE;
3202 }
3203 if (typebuf.tb_no_abbr_cnt == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003204 typebuf.tb_no_abbr_cnt = 1; // no abbreviations now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 }
3206 else
3207 {
3208 /*
3209 * Loop until we either find a matching mapped key, or we
3210 * are sure that it is not a mapped key.
3211 * If a mapped key sequence is found we go back to the start to
3212 * try re-mapping.
3213 */
3214 for (;;)
3215 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003216 long wait_time;
3217 int keylen = 0;
Bram Moolenaareda35f72019-08-03 14:59:44 +02003218 int showcmd_idx;
zeertzjq6d4e7252022-04-07 13:58:04 +01003219 check_end_reg_executing(advance);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 /*
3221 * ui_breakcheck() is slow, don't use it too often when
3222 * inside a mapping. But call it each time for typed
3223 * characters.
3224 */
3225 if (typebuf.tb_maplen)
3226 line_breakcheck();
3227 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003228 ui_breakcheck(); // check for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 if (got_int)
3230 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003231 // flush all input
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003232 c = inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 0L);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003233
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 /*
3235 * If inchar() returns TRUE (script file was active) or we
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003236 * are inside a mapping, get out of Insert mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 * Otherwise we behave like having gotten a CTRL-C.
3238 * As a result typing CTRL-C in insert mode will
3239 * really insert a CTRL-C.
3240 */
3241 if ((c || typebuf.tb_maplen)
Bram Moolenaar24959102022-05-07 20:01:16 +01003242 && (State & (MODE_INSERT | MODE_CMDLINE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 c = ESC;
3244 else
3245 c = Ctrl_C;
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02003246 flush_buffers(FLUSH_INPUT); // flush all typeahead
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247
Bram Moolenaard9dfd572006-10-03 13:36:13 +00003248 if (advance)
3249 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003250 // Also record this character, it might be needed to
3251 // get out of Insert mode.
Bram Moolenaard9dfd572006-10-03 13:36:13 +00003252 *typebuf.tb_buf = c;
3253 gotchars(typebuf.tb_buf, 1);
3254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 cmd_silent = FALSE;
3256
3257 break;
3258 }
3259 else if (typebuf.tb_len > 0)
3260 {
3261 /*
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003262 * Check for a mapping in "typebuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 */
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003264 map_result_T result = handle_mapping(
3265 &keylen, &timedout, &mapdepth);
3266
3267 if (result == map_result_retry)
3268 // try mapping again
3269 continue;
3270 if (result == map_result_fail)
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003271 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003272 // failed, use the outer loop
3273 c = -1;
3274 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003276 if (result == map_result_get)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278/*
3279 * get a character: 2. from the typeahead buffer
3280 */
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003281 c = typebuf.tb_buf[typebuf.tb_off];
Bram Moolenaar30613902019-12-01 22:11:18 +01003282 if (advance) // remove chars from tb_buf
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003283 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003284 cmd_silent = (typebuf.tb_silent > 0);
3285 if (typebuf.tb_maplen > 0)
3286 KeyTyped = FALSE;
3287 else
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003288 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003289 KeyTyped = TRUE;
Bram Moolenaar30613902019-12-01 22:11:18 +01003290 // write char to script file(s)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003291 gotchars(typebuf.tb_buf
3292 + typebuf.tb_off, 1);
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003293 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003294 KeyNoremap = typebuf.tb_noremap[
3295 typebuf.tb_off];
3296 del_typebuf(1, 0);
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003297 }
Bram Moolenaar52797ba2021-12-16 14:45:13 +00003298 break; // got character, break the for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 }
3300
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003301 // not enough characters, get more
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 }
3303
3304/*
3305 * get a character: 3. from the user - handle <Esc> in Insert mode
3306 */
3307 /*
Bram Moolenaard330e842022-11-24 20:23:24 +00003308 * Special case: if we get an <ESC> in Insert mode and there
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 * are no more characters at once, we pretend to go out of
Bram Moolenaard330e842022-11-24 20:23:24 +00003310 * Insert mode. This prevents the one second delay after
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 * typing an <ESC>. If we get something after all, we may
3312 * have to redisplay the mode. That the cursor is in the wrong
3313 * place does not matter.
Bram Moolenaard330e842022-11-24 20:23:24 +00003314 * Do not do this if the kitty keyboard protocol is used, every
3315 * <ESC> is the start of an escape sequence then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 */
3317 c = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 new_wcol = curwin->w_wcol;
3319 new_wrow = curwin->w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 if ( advance
3321 && typebuf.tb_len == 1
3322 && typebuf.tb_buf[typebuf.tb_off] == ESC
3323 && !no_mapping
Bram Moolenaard330e842022-11-24 20:23:24 +00003324 && kitty_protocol_state != KKPS_ENABLED
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 && ex_normal_busy == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 && typebuf.tb_maplen == 0
Bram Moolenaar24959102022-05-07 20:01:16 +01003327 && (State & MODE_INSERT)
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003328 && (p_timeout
3329 || (keylen == KEYLEN_PART_KEY && p_ttimeout))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 && (c = inchar(typebuf.tb_buf + typebuf.tb_off
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003331 + typebuf.tb_len, 3, 25L)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003333 colnr_T col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 char_u *ptr;
3335
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003336 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 {
3338 unshowmode(TRUE);
3339 mode_deleted = TRUE;
3340 }
3341#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003342 // may show a different cursor shape
Bram Moolenaar24959102022-05-07 20:01:16 +01003343 if (gui.in_use && State != MODE_NORMAL && !cmd_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 {
3345 int save_State;
3346
3347 save_State = State;
Bram Moolenaar24959102022-05-07 20:01:16 +01003348 State = MODE_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 gui_update_cursor(TRUE, FALSE);
3350 State = save_State;
3351 shape_changed = TRUE;
3352 }
3353#endif
3354 validate_cursor();
3355 old_wcol = curwin->w_wcol;
3356 old_wrow = curwin->w_wrow;
3357
Bram Moolenaar30613902019-12-01 22:11:18 +01003358 // move cursor left, if possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 if (curwin->w_cursor.col != 0)
3360 {
3361 if (curwin->w_wcol > 0)
3362 {
Bram Moolenaar0f843ef2023-01-25 17:34:41 +00003363 // After auto-indenting and no text is following,
3364 // we are expecting to truncate the trailing
3365 // white-space, so find the last non-white
3366 // character -- webb
3367 if (did_ai && *skipwhite(ml_get_curline()
3368 + curwin->w_cursor.col) == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003370 chartabsize_T cts;
3371
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003372 curwin->w_wcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 ptr = ml_get_curline();
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003374 init_chartabsize_arg(&cts, curwin,
3375 curwin->w_cursor.lnum, 0, ptr, ptr);
3376 while (cts.cts_ptr < ptr + curwin->w_cursor.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003378 if (!VIM_ISWHITE(*cts.cts_ptr))
3379 curwin->w_wcol = cts.cts_vcol;
3380 cts.cts_vcol += lbr_chartabsize(&cts);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 if (has_mbyte)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003382 cts.cts_ptr +=
3383 (*mb_ptr2len)(cts.cts_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 else
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003385 ++cts.cts_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003387 clear_chartabsize_arg(&cts);
3388
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 curwin->w_wrow = curwin->w_cline_row
Bram Moolenaar02631462017-09-22 15:20:32 +02003390 + curwin->w_wcol / curwin->w_width;
3391 curwin->w_wcol %= curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 curwin->w_wcol += curwin_col_off();
Bram Moolenaar30613902019-12-01 22:11:18 +01003393 col = 0; // no correction needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 }
3395 else
3396 {
3397 --curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 col = curwin->w_cursor.col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 }
3400 }
3401 else if (curwin->w_p_wrap && curwin->w_wrow)
3402 {
3403 --curwin->w_wrow;
Bram Moolenaar02631462017-09-22 15:20:32 +02003404 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 col = curwin->w_cursor.col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 if (has_mbyte && col > 0 && curwin->w_wcol > 0)
3408 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003409 // Correct when the cursor is on the right halve
3410 // of a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 ptr = ml_get_curline();
3412 col -= (*mb_head_off)(ptr, ptr + col);
3413 if ((*mb_ptr2cells)(ptr + col) > 1)
3414 --curwin->w_wcol;
3415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 }
3417 setcursor();
3418 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 new_wcol = curwin->w_wcol;
3420 new_wrow = curwin->w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 curwin->w_wcol = old_wcol;
3422 curwin->w_wrow = old_wrow;
3423 }
3424 if (c < 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003425 continue; // end of input script reached
Bram Moolenaar20c38922014-07-23 20:41:14 +02003426
Bram Moolenaar30613902019-12-01 22:11:18 +01003427 // Allow mapping for just typed characters. When we get here c
3428 // is the number of extra bytes and typebuf.tb_len is 1.
Bram Moolenaar20c38922014-07-23 20:41:14 +02003429 for (n = 1; n <= c; ++n)
3430 typebuf.tb_noremap[typebuf.tb_off + n] = RM_YES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 typebuf.tb_len += c;
3432
Bram Moolenaar30613902019-12-01 22:11:18 +01003433 // buffer full, don't map
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 if (typebuf.tb_len >= typebuf.tb_maplen + MAXMAPLEN)
3435 {
3436 timedout = TRUE;
3437 continue;
3438 }
3439
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 if (ex_normal_busy > 0)
3441 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 static int tc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443
Bram Moolenaar30613902019-12-01 22:11:18 +01003444 // No typeahead left and inside ":normal". Must return
3445 // something to avoid getting stuck. When an incomplete
3446 // mapping is present, behave like it timed out.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 if (typebuf.tb_len > 0)
3448 {
3449 timedout = TRUE;
3450 continue;
3451 }
Bram Moolenaar189832b2020-09-23 12:29:11 +02003452
Bram Moolenaar30613902019-12-01 22:11:18 +01003453 // When 'insertmode' is set, ESC just beeps in Insert
3454 // mode. Use CTRL-L to make edit() return.
3455 // For the command line only CTRL-C always breaks it.
3456 // For the cmdline window: Alternate between ESC and
3457 // CTRL-C: ESC for most situations and CTRL-C to close the
3458 // cmdline window.
Bram Moolenaar24959102022-05-07 20:01:16 +01003459 if (p_im && (State & MODE_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 c = Ctrl_L;
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003461#ifdef FEAT_TERMINAL
3462 else if (terminal_is_active())
3463 c = K_CANCEL;
3464#endif
Bram Moolenaar24959102022-05-07 20:01:16 +01003465 else if ((State & MODE_CMDLINE)
Martin Tournoij7904fa42022-10-04 16:28:45 +01003466 || (cmdwin_type > 0 && tc == ESC))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 c = Ctrl_C;
3468 else
3469 c = ESC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 tc = c;
Bram Moolenaar8d696372022-08-21 10:40:07 +01003471 // set a flag to indicate this wasn't a normal char
3472 if (advance)
3473 typebuf_was_empty = TRUE;
3474
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01003475 // return from main_loop()
3476 if (pending_exmode_active)
3477 exmode_active = EXMODE_NORMAL;
3478
Bram Moolenaarb37a65e2022-01-01 12:42:56 +00003479 // no chars to block abbreviation for
3480 typebuf.tb_no_abbr_cnt = 0;
3481
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 break;
3483 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484
3485/*
3486 * get a character: 3. from the user - update display
3487 */
Bram Moolenaar30613902019-12-01 22:11:18 +01003488 // In insert mode a screen update is skipped when characters
3489 // are still available. But when those available characters
3490 // are part of a mapping, and we are going to do a blocking
3491 // wait here. Need to update the screen to display the
3492 // changed text so far. Also for when 'lazyredraw' is set and
3493 // redrawing was postponed because there was something in the
3494 // input buffer (e.g., termresponse).
Bram Moolenaar24959102022-05-07 20:01:16 +01003495 if (((State & MODE_INSERT) != 0 || p_lz)
3496 && (State & MODE_CMDLINE) == 0
3497 && advance && must_redraw != 0 && !need_wait_return)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 {
3499 update_screen(0);
Bram Moolenaar30613902019-12-01 22:11:18 +01003500 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 }
3502
3503 /*
3504 * If we have a partial match (and are going to wait for more
3505 * input from the user), show the partially matched characters
3506 * to the user with showcmd.
3507 */
Bram Moolenaareda35f72019-08-03 14:59:44 +02003508 showcmd_idx = 0;
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003509 int showing_partial = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 if (typebuf.tb_len > 0 && advance && !exmode_active)
3511 {
Bram Moolenaar24959102022-05-07 20:01:16 +01003512 if (((State & (MODE_NORMAL | MODE_INSERT))
3513 || State == MODE_LANGMAP)
3514 && State != MODE_HITRETURN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003516 // this looks nice when typing a dead character map
Bram Moolenaar24959102022-05-07 20:01:16 +01003517 if (State & MODE_INSERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
3519 + typebuf.tb_len - 1) == 1)
3520 {
3521 edit_putchar(typebuf.tb_buf[typebuf.tb_off
3522 + typebuf.tb_len - 1], FALSE);
Bram Moolenaar30613902019-12-01 22:11:18 +01003523 setcursor(); // put cursor back where it belongs
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003524 showing_partial = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003526 // need to use the col and row from above here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 old_wcol = curwin->w_wcol;
3528 old_wrow = curwin->w_wrow;
3529 curwin->w_wcol = new_wcol;
3530 curwin->w_wrow = new_wrow;
3531 push_showcmd();
3532 if (typebuf.tb_len > SHOWCMD_COLS)
Bram Moolenaareda35f72019-08-03 14:59:44 +02003533 showcmd_idx = typebuf.tb_len - SHOWCMD_COLS;
3534 while (showcmd_idx < typebuf.tb_len)
3535 (void)add_to_showcmd(
3536 typebuf.tb_buf[typebuf.tb_off + showcmd_idx++]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 curwin->w_wcol = old_wcol;
3538 curwin->w_wrow = old_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 }
3540
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003541 // This looks nice when typing a dead character map.
3542 // There is no actual command line for get_number().
Bram Moolenaar24959102022-05-07 20:01:16 +01003543 if ((State & MODE_CMDLINE)
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003544 && get_cmdline_info()->cmdbuff != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3546 && cmdline_star == 0
3547#endif
3548 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
3549 + typebuf.tb_len - 1) == 1)
3550 {
3551 putcmdline(typebuf.tb_buf[typebuf.tb_off
3552 + typebuf.tb_len - 1], FALSE);
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003553 showing_partial = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 }
3555 }
3556
3557/*
3558 * get a character: 3. from the user - get it
3559 */
Bram Moolenaar83f4cbd2018-06-12 21:35:40 +02003560 if (typebuf.tb_len == 0)
zeertzjq3760bfd2022-06-06 16:22:46 +01003561 // timedout may have been set if a mapping with empty RHS
3562 // fully matched while longer mappings timed out.
Bram Moolenaar83f4cbd2018-06-12 21:35:40 +02003563 timedout = FALSE;
3564
Bram Moolenaar652de232019-04-04 20:13:09 +02003565 if (advance)
3566 {
3567 if (typebuf.tb_len == 0
3568 || !(p_timeout
3569 || (p_ttimeout && keylen == KEYLEN_PART_KEY)))
3570 // blocking wait
3571 wait_time = -1L;
3572 else if (keylen == KEYLEN_PART_KEY && p_ttm >= 0)
3573 wait_time = p_ttm;
3574 else
3575 wait_time = p_tm;
3576 }
3577 else
3578 wait_time = 0;
3579
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003580 wait_tb_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 c = inchar(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len,
3582 typebuf.tb_buflen - typebuf.tb_off - typebuf.tb_len - 1,
Bram Moolenaar652de232019-04-04 20:13:09 +02003583 wait_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584
Bram Moolenaareda35f72019-08-03 14:59:44 +02003585 if (showcmd_idx != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 pop_showcmd();
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003587 if (showing_partial)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 {
Bram Moolenaar24959102022-05-07 20:01:16 +01003589 if (State & MODE_INSERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 edit_unputchar();
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003591 if ((State & MODE_CMDLINE)
3592 && get_cmdline_info()->cmdbuff != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 unputcmdline();
Bram Moolenaarbc256d92012-06-06 12:06:15 +02003594 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003595 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 }
3597
3598 if (c < 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003599 continue; // end of input script reached
3600 if (c == NUL) // no character available
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 {
3602 if (!advance)
3603 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01003604 if (wait_tb_len > 0) // timed out
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 {
3606 timedout = TRUE;
3607 continue;
3608 }
3609 }
3610 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003611 { // allow mapping for just typed characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 while (typebuf.tb_buf[typebuf.tb_off
3613 + typebuf.tb_len] != NUL)
3614 typebuf.tb_noremap[typebuf.tb_off
3615 + typebuf.tb_len++] = RM_YES;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003616#ifdef HAVE_INPUT_METHOD
Bram Moolenaar30613902019-12-01 22:11:18 +01003617 // Get IM status right after getting keys, not after the
3618 // timeout for a mapping (focus may be lost by then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 vgetc_im_active = im_get_status();
3620#endif
3621 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003622 } // for (;;)
3623 } // if (!character from stuffbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624
Bram Moolenaar30613902019-12-01 22:11:18 +01003625 // if advance is FALSE don't loop on NULs
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003626 } while ((c < 0 && c != K_CANCEL) || (advance && c == NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627
3628 /*
3629 * The "INSERT" message is taken care of here:
3630 * if we return an ESC to exit insert mode, the message is deleted
3631 * if we don't return an ESC but deleted the message before, redisplay it
3632 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003633 if (advance && p_smd && msg_silent == 0 && (State & MODE_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003635 if (c == ESC && !mode_deleted && !no_mapping && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 {
3637 if (typebuf.tb_len && !KeyTyped)
Bram Moolenaar30613902019-12-01 22:11:18 +01003638 redraw_cmdline = TRUE; // delete mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 else
3640 unshowmode(FALSE);
3641 }
3642 else if (c != ESC && mode_deleted)
3643 {
3644 if (typebuf.tb_len && !KeyTyped)
Bram Moolenaar30613902019-12-01 22:11:18 +01003645 redraw_cmdline = TRUE; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 else
3647 showmode();
3648 }
3649 }
3650#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003651 // may unshow different cursor shape
Bram Moolenaarf085f422017-06-07 20:39:47 +02003652 if (gui.in_use && shape_changed)
3653 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654#endif
Bram Moolenaar6edbbd82019-03-10 09:41:51 +01003655 if (timedout && c == ESC)
3656 {
3657 char_u nop_buf[3];
3658
3659 // When recording there will be no timeout. Add a <Nop> after the ESC
3660 // to avoid that it forms a key code with following characters.
3661 nop_buf[0] = K_SPECIAL;
3662 nop_buf[1] = KS_EXTRA;
3663 nop_buf[2] = KE_NOP;
3664 gotchars(nop_buf, 3);
3665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003667 --vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668
3669 return c;
3670}
3671
3672/*
3673 * inchar() - get one character from
3674 * 1. a scriptfile
3675 * 2. the keyboard
3676 *
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003677 * As many characters as we can get (up to 'maxlen') are put in "buf" and
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 * NUL terminated (buffer length must be 'maxlen' + 1).
3679 * Minimum for "maxlen" is 3!!!!
3680 *
3681 * "tb_change_cnt" is the value of typebuf.tb_change_cnt if "buf" points into
3682 * it. When typebuf.tb_change_cnt changes (e.g., when a message is received
3683 * from a remote client) "buf" can no longer be used. "tb_change_cnt" is 0
3684 * otherwise.
3685 *
3686 * If we got an interrupt all input is read until none is available.
3687 *
3688 * If wait_time == 0 there is no waiting for the char.
3689 * If wait_time == n we wait for n msec for a character to arrive.
3690 * If wait_time == -1 we wait forever for a character to arrive.
3691 *
3692 * Return the number of obtained characters.
3693 * Return -1 when end of input script reached.
3694 */
Bram Moolenaarcda77642016-06-04 13:32:35 +02003695 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003696inchar(
3697 char_u *buf,
3698 int maxlen,
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003699 long wait_time) // milliseconds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700{
Bram Moolenaar30613902019-12-01 22:11:18 +01003701 int len = 0; // init for GCC
3702 int retesc = FALSE; // return ESC with gotint
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 int script_char;
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003704 int tb_change_cnt = typebuf.tb_change_cnt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705
Bram Moolenaar30613902019-12-01 22:11:18 +01003706 if (wait_time == -1L || wait_time > 100L) // flush output before waiting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 {
3708 cursor_on();
Bram Moolenaara338adc2018-01-31 20:51:47 +01003709 out_flush_cursor(FALSE, FALSE);
3710#if defined(FEAT_GUI) && defined(FEAT_MOUSESHAPE)
3711 if (gui.in_use && postponed_mouseshape)
3712 update_mouseshape(-1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713#endif
3714 }
3715
3716 /*
3717 * Don't reset these when at the hit-return prompt, otherwise a endless
3718 * recursive loop may result (write error in swapfile, hit-return, timeout
3719 * on char wait, flush swapfile, write error....).
3720 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003721 if (State != MODE_HITRETURN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003723 did_outofmem_msg = FALSE; // display out of memory message (again)
3724 did_swapwrite_msg = FALSE; // display swap file write error again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003726 undo_off = FALSE; // restart undo now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727
3728 /*
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00003729 * Get a character from a script file if there is one.
3730 * If interrupted: Stop reading script files, close them all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 */
3732 script_char = -1;
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00003733 while (scriptin[curscript] != NULL && script_char < 0
3734#ifdef FEAT_EVAL
3735 && !ignore_script
3736#endif
3737 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02003739#ifdef MESSAGE_QUEUE
3740 parse_queued_messages();
Bram Moolenaarf2330482008-06-24 20:19:36 +00003741#endif
3742
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 if (got_int || (script_char = getc(scriptin[curscript])) < 0)
3744 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003745 // Reached EOF.
3746 // Careful: closescript() frees typebuf.tb_buf[] and buf[] may
3747 // point inside typebuf.tb_buf[]. Don't use buf[] after this!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 closescript();
3749 /*
3750 * When reading script file is interrupted, return an ESC to get
3751 * back to normal mode.
3752 * Otherwise return -1, because typebuf.tb_buf[] has changed.
3753 */
3754 if (got_int)
3755 retesc = TRUE;
3756 else
3757 return -1;
3758 }
3759 else
3760 {
3761 buf[0] = script_char;
3762 len = 1;
3763 }
3764 }
3765
Bram Moolenaar30613902019-12-01 22:11:18 +01003766 if (script_char < 0) // did not get a character from script
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 {
3768 /*
3769 * If we got an interrupt, skip all previously typed characters and
3770 * return TRUE if quit reading script file.
3771 * Stop reading typeahead when a single CTRL-C was read,
3772 * fill_input_buf() returns this when not able to read from stdin.
3773 * Don't use buf[] here, closescript() may have freed typebuf.tb_buf[]
3774 * and buf may be pointing inside typebuf.tb_buf[].
3775 */
3776 if (got_int)
3777 {
kylo252ae6f1d82022-02-16 19:24:07 +00003778#define DUM_LEN (MAXMAPLEN * 3 + 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 char_u dum[DUM_LEN + 1];
3780
3781 for (;;)
3782 {
3783 len = ui_inchar(dum, DUM_LEN, 0L, 0);
zeertzjq17c95d92022-04-26 12:51:07 +01003784 if (len == 0 || (len == 1 && dum[0] == Ctrl_C))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 break;
3786 }
3787 return retesc;
3788 }
3789
3790 /*
3791 * Always flush the output characters when getting input characters
Bram Moolenaarcb574f42019-01-25 22:29:57 +01003792 * from the user and not just peeking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 */
Bram Moolenaarcb574f42019-01-25 22:29:57 +01003794 if (wait_time == -1L || wait_time > 10L)
3795 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796
3797 /*
3798 * Fill up to a third of the buffer, because each character may be
3799 * tripled below.
3800 */
3801 len = ui_inchar(buf, maxlen / 3, wait_time, tb_change_cnt);
3802 }
3803
Bram Moolenaar30613902019-12-01 22:11:18 +01003804 // If the typebuf was changed further down, it is like nothing was added by
3805 // this call.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 if (typebuf_changed(tb_change_cnt))
3807 return 0;
3808
Bram Moolenaar30613902019-12-01 22:11:18 +01003809 // Note the change in the typeahead buffer, this matters for when
3810 // vgetorpeek() is called recursively, e.g. using getchar(1) in a timer
3811 // function.
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003812 if (len > 0 && ++typebuf.tb_change_cnt == 0)
3813 typebuf.tb_change_cnt = 1;
3814
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003815 return fix_input_buffer(buf, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816}
3817
3818/*
3819 * Fix typed characters for use by vgetc() and check_termcode().
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003820 * "buf[]" must have room to triple the number of bytes!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 * Returns the new length.
3822 */
3823 int
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003824fix_input_buffer(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825{
3826 int i;
3827 char_u *p = buf;
3828
3829 /*
3830 * Two characters are special: NUL and K_SPECIAL.
3831 * When compiled With the GUI CSI is also special.
3832 * Replace NUL by K_SPECIAL KS_ZERO KE_FILLER
3833 * Replace K_SPECIAL by K_SPECIAL KS_SPECIAL KE_FILLER
3834 * Replace CSI by K_SPECIAL KS_EXTRA KE_CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 */
3836 for (i = len; --i >= 0; ++p)
3837 {
3838#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003839 // When the GUI is used any character can come after a CSI, don't
3840 // escape it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 if (gui.in_use && p[0] == CSI && i >= 2)
3842 {
3843 p += 2;
3844 i -= 2;
3845 }
zeertzjq646bb722022-02-16 17:51:47 +00003846# ifndef MSWIN
3847 // When not on MS-Windows and the GUI is not used CSI needs to be
3848 // escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 else if (!gui.in_use && p[0] == CSI)
3850 {
3851 mch_memmove(p + 3, p + 1, (size_t)i);
3852 *p++ = K_SPECIAL;
3853 *p++ = KS_EXTRA;
3854 *p = (int)KE_CSI;
3855 len += 2;
3856 }
zeertzjq646bb722022-02-16 17:51:47 +00003857# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 else
3859#endif
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003860 if (p[0] == NUL || (p[0] == K_SPECIAL
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003861 // timeout may generate K_CURSORHOLD
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003862 && (i < 2 || p[1] != KS_EXTRA || p[2] != (int)KE_CURSORHOLD)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003863#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003864 // Win32 console passes modifiers
3865 && (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003866# ifdef VIMDLL
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003867 gui.in_use ||
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003868# endif
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003869 (i < 2 || p[1] != KS_MODIFIER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870#endif
3871 ))
3872 {
3873 mch_memmove(p + 3, p + 1, (size_t)i);
3874 p[2] = K_THIRD(p[0]);
3875 p[1] = K_SECOND(p[0]);
3876 p[0] = K_SPECIAL;
3877 p += 2;
3878 len += 2;
3879 }
3880 }
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003881 *p = NUL; // add trailing NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 return len;
3883}
3884
3885#if defined(USE_INPUT_BUF) || defined(PROTO)
3886/*
3887 * Return TRUE when bytes are in the input buffer or in the typeahead buffer.
3888 * Normally the input buffer would be sufficient, but the server_to_input_buf()
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003889 * or feedkeys() may insert characters in the typeahead buffer while we are
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003890 * waiting for input to arrive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 */
3892 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003893input_available(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894{
3895 return (!vim_is_input_buf_empty()
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003896# if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
3897 || typebuf_was_filled
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898# endif
3899 );
3900}
3901#endif
Bram Moolenaar957cf672020-11-12 14:21:06 +01003902
3903/*
3904 * Function passed to do_cmdline() to get the command after a <Cmd> key from
3905 * typeahead.
3906 */
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003907 static char_u *
Bram Moolenaar957cf672020-11-12 14:21:06 +01003908getcmdkeycmd(
3909 int promptc UNUSED,
3910 void *cookie UNUSED,
3911 int indent UNUSED,
3912 getline_opt_T do_concat UNUSED)
3913{
3914 garray_T line_ga;
3915 int c1 = -1;
3916 int c2;
3917 int cmod = 0;
3918 int aborted = FALSE;
3919
3920 ga_init2(&line_ga, 1, 32);
3921
3922 // no mapping for these characters
3923 no_mapping++;
3924
3925 got_int = FALSE;
3926 while (c1 != NUL && !aborted)
3927 {
Yegappan Lakshmananfadc02a2023-01-27 21:03:12 +00003928 if (ga_grow(&line_ga, 32) == FAIL)
Bram Moolenaarca359cb2020-11-15 20:49:41 +01003929 {
3930 aborted = TRUE;
3931 break;
3932 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003933
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003934 if (vgetorpeek(FALSE) == NUL)
Bram Moolenaar957cf672020-11-12 14:21:06 +01003935 {
3936 // incomplete <Cmd> is an error, because there is not much the user
3937 // could do in this state.
3938 emsg(_(e_cmd_mapping_must_end_with_cr));
3939 aborted = TRUE;
3940 break;
3941 }
3942
3943 // Get one character at a time.
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003944 c1 = vgetorpeek(TRUE);
3945
Bram Moolenaar957cf672020-11-12 14:21:06 +01003946 // Get two extra bytes for special keys
3947 if (c1 == K_SPECIAL)
3948 {
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003949 c1 = vgetorpeek(TRUE);
3950 c2 = vgetorpeek(TRUE);
Bram Moolenaar957cf672020-11-12 14:21:06 +01003951 if (c1 == KS_MODIFIER)
3952 {
3953 cmod = c2;
3954 continue;
3955 }
3956 c1 = TO_SPECIAL(c1, c2);
Bram Moolenaardffa6ea2022-11-29 20:33:20 +00003957
3958 // K_ESC is used to avoid ambiguity with the single Esc character
3959 // that might be the start of an escape sequence. Convert it back
3960 // to a single Esc here.
3961 if (c1 == K_ESC)
3962 c1 = ESC;
Bram Moolenaar957cf672020-11-12 14:21:06 +01003963 }
3964
3965 if (got_int)
3966 aborted = TRUE;
3967 else if (c1 == '\r' || c1 == '\n')
3968 c1 = NUL; // end the line
3969 else if (c1 == ESC)
3970 aborted = TRUE;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003971 else if (c1 == K_COMMAND || c1 == K_SCRIPT_COMMAND)
Bram Moolenaar957cf672020-11-12 14:21:06 +01003972 {
3973 // give a nicer error message for this special case
3974 emsg(_(e_cmd_mapping_must_end_with_cr_before_second_cmd));
3975 aborted = TRUE;
3976 }
zeertzjq3ab3a862023-05-06 16:22:04 +01003977 else if (c1 == K_SNR)
Bram Moolenaar957cf672020-11-12 14:21:06 +01003978 {
zeertzjq3ab3a862023-05-06 16:22:04 +01003979 ga_concat(&line_ga, (char_u *)"<SNR>");
Bram Moolenaar957cf672020-11-12 14:21:06 +01003980 }
3981 else
zeertzjq3ab3a862023-05-06 16:22:04 +01003982 {
3983 if (cmod != 0)
3984 {
3985 ga_append(&line_ga, K_SPECIAL);
3986 ga_append(&line_ga, KS_MODIFIER);
3987 ga_append(&line_ga, cmod);
3988 }
3989 if (IS_SPECIAL(c1))
3990 {
3991 ga_append(&line_ga, K_SPECIAL);
3992 ga_append(&line_ga, K_SECOND(c1));
3993 ga_append(&line_ga, K_THIRD(c1));
3994 }
3995 else
3996 ga_append(&line_ga, c1);
3997 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003998
3999 cmod = 0;
4000 }
4001
4002 no_mapping--;
4003
4004 if (aborted)
4005 ga_clear(&line_ga);
4006
4007 return (char_u *)line_ga.ga_data;
4008}
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004009
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004010#if defined(FEAT_EVAL) || defined(PROTO)
4011/*
Bram Moolenaar6b066c62023-02-20 18:44:33 +00004012 * If there was a mapping we get its SID. Otherwise, use "last_used_sid", it
4013 * is set when redo'ing.
4014 * Put this SID in the redo buffer, so that "." will use the same script
4015 * context.
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004016 */
4017 void
4018may_add_last_used_map_to_redobuff(void)
4019{
Bram Moolenaar6b066c62023-02-20 18:44:33 +00004020 char_u buf[3 + 20];
4021 int sid = -1;
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004022
Bram Moolenaar6b066c62023-02-20 18:44:33 +00004023 if (last_used_map != NULL)
4024 sid = last_used_map->m_script_ctx.sc_sid;
4025 if (sid < 0)
4026 sid = last_used_sid;
4027
4028 if (sid < 0)
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004029 return;
4030
4031 // <K_SID>{nr};
4032 buf[0] = K_SPECIAL;
4033 buf[1] = KS_EXTRA;
4034 buf[2] = KE_SID;
Bram Moolenaar6b066c62023-02-20 18:44:33 +00004035 vim_snprintf((char *)buf + 3, 20, "%d;", sid);
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004036 add_buff(&redobuff, buf, -1L);
4037}
4038#endif
4039
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004040 int
Bram Moolenaard4e2f502022-01-15 18:48:32 +00004041do_cmdkey_command(int key UNUSED, int flags)
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004042{
4043 int res;
4044#ifdef FEAT_EVAL
Bram Moolenaara9725222022-01-16 13:30:33 +00004045 sctx_T save_current_sctx = {-1, 0, 0, 0};
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004046
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004047 if (key == K_SCRIPT_COMMAND
4048 && (last_used_map != NULL || SCRIPT_ID_VALID(last_used_sid)))
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004049 {
4050 save_current_sctx = current_sctx;
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004051 if (last_used_map != NULL)
4052 current_sctx = last_used_map->m_script_ctx;
4053 else
4054 {
4055 current_sctx.sc_sid = last_used_sid;
4056 current_sctx.sc_lnum = 0;
4057 current_sctx.sc_version = SCRIPT_ITEM(last_used_sid)->sn_version;
4058 }
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004059 }
4060#endif
4061
4062 res = do_cmdline(NULL, getcmdkeycmd, NULL, flags);
4063
4064#ifdef FEAT_EVAL
Bram Moolenaara9725222022-01-16 13:30:33 +00004065 if (save_current_sctx.sc_sid >= 0)
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004066 current_sctx = save_current_sctx;
4067#endif
4068
4069 return res;
4070}
4071
4072#if defined(FEAT_EVAL) || defined(PROTO)
4073 void
Bram Moolenaarf61c89d2022-01-19 22:51:48 +00004074reset_last_used_map(mapblock_T *mp)
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004075{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00004076 if (last_used_map != mp)
4077 return;
4078
4079 last_used_map = NULL;
4080 last_used_sid = -1;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004081}
4082#endif