blob: 4e1c48c523f4da42d52e2a8f1d98954848e950b0 [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/*
zeertzjq30b6d612023-05-07 17:39:23 +010046 * When block_redo is TRUE the redo buffer will not be changed.
47 * Used by edit() to repeat insertions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000048 */
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
Mike Williamsaca8f552024-04-07 18:26:22 +020084static size_t 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);
Mike Williamsaca8f552024-04-07 18:26:22 +0200166 if (len >= last_recorded_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167 {
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 {
RestorerZ68ebcee2023-05-31 17:12:14 +0100217 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{
zeertzjq30b6d612023-05-07 17:39:23 +0100612 if (block_redo)
613 return;
614
zeertzjq3ab3a862023-05-06 16:22:04 +0100615 while (*s != NUL)
616 {
617 if (*s == K_SPECIAL && s[1] != NUL && s[2] != NUL)
618 {
zeertzjq30b6d612023-05-07 17:39:23 +0100619 // Insert special key literally.
zeertzjq3ab3a862023-05-06 16:22:04 +0100620 add_buff(&redobuff, s, 3L);
621 s += 3;
622 }
623 else
624 add_char_buff(&redobuff, mb_cptr2char_adv(&s));
625 }
626}
627
628/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 * Append a character to the redo buffer.
630 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
631 */
632 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100633AppendCharToRedobuff(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634{
635 if (!block_redo)
636 add_char_buff(&redobuff, c);
637}
638
639/*
640 * Append a number to the redo buffer.
641 */
642 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100643AppendNumberToRedobuff(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644{
645 if (!block_redo)
646 add_num_buff(&redobuff, n);
647}
648
649/*
650 * Append string "s" to the stuff buffer.
651 * CSI and K_SPECIAL must already have been escaped.
652 */
653 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100654stuffReadbuff(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100656 add_buff(&readbuf1, s, -1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657}
658
Bram Moolenaar4f5ce332014-07-30 16:00:58 +0200659/*
660 * Append string "s" to the redo stuff buffer.
661 * CSI and K_SPECIAL must already have been escaped.
662 */
663 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100664stuffRedoReadbuff(char_u *s)
Bram Moolenaar4f5ce332014-07-30 16:00:58 +0200665{
666 add_buff(&readbuf2, s, -1L);
667}
668
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200669 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100670stuffReadbuffLen(char_u *s, long len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100672 add_buff(&readbuf1, s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673}
674
675#if defined(FEAT_EVAL) || defined(PROTO)
676/*
677 * Stuff "s" into the stuff buffer, leaving special key codes unmodified and
678 * escaping other K_SPECIAL and CSI bytes.
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200679 * Change CR, LF and ESC into a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 */
681 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100682stuffReadbuffSpec(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683{
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200684 int c;
685
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 while (*s != NUL)
687 {
688 if (*s == K_SPECIAL && s[1] != NUL && s[2] != NUL)
689 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100690 // Insert special key literally.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 stuffReadbuffLen(s, 3L);
692 s += 3;
693 }
694 else
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200695 {
zeertzjqe3a529b2022-06-05 19:01:37 +0100696 c = mb_cptr2char_adv(&s);
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200697 if (c == CAR || c == NL || c == ESC)
698 c = ' ';
699 stuffcharReadbuff(c);
700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 }
702}
703#endif
704
705/*
706 * Append a character to the stuff buffer.
707 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
708 */
709 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100710stuffcharReadbuff(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100712 add_char_buff(&readbuf1, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713}
714
715/*
716 * Append a number to the stuff buffer.
717 */
718 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100719stuffnumReadbuff(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100721 add_num_buff(&readbuf1, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722}
723
724/*
Bram Moolenaar11abd092020-05-01 14:26:37 +0200725 * Stuff a string into the typeahead buffer, such that edit() will insert it
726 * literally ("literally" TRUE) or interpret is as typed characters.
727 */
728 void
729stuffescaped(char_u *arg, int literally)
730{
731 int c;
732 char_u *start;
733
734 while (*arg != NUL)
735 {
736 // Stuff a sequence of normal ASCII characters, that's fast. Also
737 // stuff K_SPECIAL to get the effect of a special key when "literally"
738 // is TRUE.
739 start = arg;
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000740 while ((*arg >= ' ' && *arg < DEL)
Bram Moolenaar11abd092020-05-01 14:26:37 +0200741 || (*arg == K_SPECIAL && !literally))
742 ++arg;
743 if (arg > start)
744 stuffReadbuffLen(start, (long)(arg - start));
745
746 // stuff a single special character
747 if (*arg != NUL)
748 {
749 if (has_mbyte)
750 c = mb_cptr2char_adv(&arg);
751 else
752 c = *arg++;
753 if (literally && ((c < ' ' && c != TAB) || c == DEL))
754 stuffcharReadbuff(Ctrl_V);
755 stuffcharReadbuff(c);
756 }
757 }
758}
759
760/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 * Read a character from the redo buffer. Translates K_SPECIAL, CSI and
762 * multibyte characters.
763 * The redo buffer is left as it is.
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100764 * If init is TRUE, prepare for redo, return FAIL if nothing to redo, OK
765 * otherwise.
766 * If old is TRUE, use old_redobuff instead of redobuff.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 */
768 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100769read_redo(int init, int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100771 static buffblock_T *bp;
772 static char_u *p;
773 int c;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100774 int n;
775 char_u buf[MB_MAXBYTES + 1];
776 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777
778 if (init)
779 {
780 if (old_redo)
Bram Moolenaar285e3352018-04-18 23:01:13 +0200781 bp = old_redobuff.bh_first.b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 else
Bram Moolenaar285e3352018-04-18 23:01:13 +0200783 bp = redobuff.bh_first.b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 if (bp == NULL)
785 return FAIL;
786 p = bp->b_str;
787 return OK;
788 }
789 if ((c = *p) != NUL)
790 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100791 // Reverse the conversion done by add_char_buff()
792 // For a multi-byte character get all the bytes and return the
793 // converted character.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 if (has_mbyte && (c != K_SPECIAL || p[1] == KS_SPECIAL))
795 n = MB_BYTE2LEN_CHECK(c);
796 else
797 n = 1;
798 for (i = 0; ; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100800 if (c == K_SPECIAL) // special key or escaped K_SPECIAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 {
802 c = TO_SPECIAL(p[1], p[2]);
803 p += 2;
804 }
805#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +0100806 if (c == CSI) // escaped CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 p += 2;
808#endif
809 if (*++p == NUL && bp->b_next != NULL)
810 {
811 bp = bp->b_next;
812 p = bp->b_str;
813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 buf[i] = c;
Bram Moolenaar30613902019-12-01 22:11:18 +0100815 if (i == n - 1) // last byte of a character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 {
817 if (n != 1)
818 c = (*mb_ptr2char)(buf);
819 break;
820 }
821 c = *p;
Bram Moolenaar30613902019-12-01 22:11:18 +0100822 if (c == NUL) // cannot happen?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 }
825 }
826
827 return c;
828}
829
830/*
831 * Copy the rest of the redo buffer into the stuff buffer (in a slow way).
832 * If old_redo is TRUE, use old_redobuff instead of redobuff.
833 * The escaped K_SPECIAL and CSI are copied without translation.
834 */
835 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100836copy_redo(int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837{
838 int c;
839
840 while ((c = read_redo(FALSE, old_redo)) != NUL)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100841 add_char_buff(&readbuf2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842}
843
844/*
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100845 * Stuff the redo buffer into readbuf2.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 * Insert the redo count into the command.
847 * If "old_redo" is TRUE, the last but one command is repeated
848 * instead of the last command (inserting text). This is used for
849 * CTRL-O <.> in insert mode
850 *
851 * return FAIL for failure, OK otherwise
852 */
853 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100854start_redo(long count, int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855{
856 int c;
857
Bram Moolenaar30613902019-12-01 22:11:18 +0100858 // init the pointers; return if nothing to redo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 if (read_redo(TRUE, old_redo) == FAIL)
860 return FAIL;
861
862 c = read_redo(FALSE, old_redo);
863
Bram Moolenaarddf7dba2022-09-05 16:53:21 +0100864#ifdef FEAT_EVAL
865 if (c == K_SID)
866 {
867 // Copy the <SID>{sid}; sequence
868 add_char_buff(&readbuf2, c);
869 for (;;)
870 {
871 c = read_redo(FALSE, old_redo);
872 add_char_buff(&readbuf2, c);
Keith Thompson184f71c2024-01-04 21:19:04 +0100873 if (!SAFE_isdigit(c))
Bram Moolenaarddf7dba2022-09-05 16:53:21 +0100874 break;
875 }
876 c = read_redo(FALSE, old_redo);
877 }
878#endif
879
Bram Moolenaar30613902019-12-01 22:11:18 +0100880 // copy the buffer name, if present
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 if (c == '"')
882 {
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100883 add_buff(&readbuf2, (char_u *)"\"", 1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 c = read_redo(FALSE, old_redo);
885
Bram Moolenaar30613902019-12-01 22:11:18 +0100886 // if a numbered buffer is used, increment the number
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 if (c >= '1' && c < '9')
888 ++c;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100889 add_char_buff(&readbuf2, c);
Bram Moolenaar833093b2018-05-23 21:53:52 +0200890
Bram Moolenaar30613902019-12-01 22:11:18 +0100891 // the expression register should be re-evaluated
Bram Moolenaar833093b2018-05-23 21:53:52 +0200892 if (c == '=')
893 {
894 add_char_buff(&readbuf2, CAR);
895 cmd_silent = TRUE;
896 }
897
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 c = read_redo(FALSE, old_redo);
899 }
900
Bram Moolenaar30613902019-12-01 22:11:18 +0100901 if (c == 'v') // redo Visual
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 {
903 VIsual = curwin->w_cursor;
904 VIsual_active = TRUE;
905 VIsual_select = FALSE;
906 VIsual_reselect = TRUE;
907 redo_VIsual_busy = TRUE;
908 c = read_redo(FALSE, old_redo);
909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910
Bram Moolenaar30613902019-12-01 22:11:18 +0100911 // try to enter the count (in place of a previous count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 if (count)
913 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100914 while (VIM_ISDIGIT(c)) // skip "old" count
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 c = read_redo(FALSE, old_redo);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100916 add_num_buff(&readbuf2, count);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 }
918
Bram Moolenaarddf7dba2022-09-05 16:53:21 +0100919 // copy the rest from the redo buffer into the stuff buffer
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100920 add_char_buff(&readbuf2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 copy_redo(old_redo);
922 return OK;
923}
924
925/*
926 * Repeat the last insert (R, o, O, a, A, i or I command) by stuffing
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100927 * the redo buffer into readbuf2.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 * return FAIL for failure, OK otherwise
929 */
930 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100931start_redo_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932{
933 int c;
934
935 if (read_redo(TRUE, FALSE) == FAIL)
936 return FAIL;
937 start_stuff();
938
Bram Moolenaar30613902019-12-01 22:11:18 +0100939 // skip the count and the command character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 while ((c = read_redo(FALSE, FALSE)) != NUL)
941 {
942 if (vim_strchr((char_u *)"AaIiRrOo", c) != NULL)
943 {
944 if (c == 'O' || c == 'o')
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100945 add_buff(&readbuf2, NL_STR, -1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 break;
947 }
948 }
949
Bram Moolenaar30613902019-12-01 22:11:18 +0100950 // copy the typed text from the redo buffer into the stuff buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 copy_redo(FALSE);
952 block_redo = TRUE;
953 return OK;
954}
955
956 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100957stop_redo_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958{
959 block_redo = FALSE;
960}
961
962/*
963 * Initialize typebuf.tb_buf to point to typebuf_init.
964 * alloc() cannot be used here: In out-of-memory situations it would
965 * be impossible to type anything.
966 */
967 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100968init_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000970 if (typebuf.tb_buf != NULL)
971 return;
972
973 typebuf.tb_buf = typebuf_init;
974 typebuf.tb_noremap = noremapbuf_init;
975 typebuf.tb_buflen = TYPELEN_INIT;
976 typebuf.tb_len = 0;
977 typebuf.tb_off = MAXMAPLEN + 4;
978 typebuf.tb_change_cnt = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979}
980
981/*
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200982 * Returns TRUE when keys cannot be remapped.
983 */
984 int
985noremap_keys(void)
986{
987 return KeyNoremap & (RM_NONE|RM_SCRIPT);
988}
989
990/*
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100991 * Insert a string in position 'offset' in the typeahead buffer (for "@r"
992 * and ":normal" command, vgetorpeek() and check_termcode()).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 *
Bram Moolenaar88456cd2022-11-18 22:14:09 +0000994 * If "noremap" is REMAP_YES, new string can be mapped again.
995 * If "noremap" is REMAP_NONE, new string cannot be mapped again.
996 * If "noremap" is REMAP_SKIP, first char of new string cannot be mapped again,
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000997 * but abbreviations are allowed.
Bram Moolenaar88456cd2022-11-18 22:14:09 +0000998 * If "noremap" is REMAP_SCRIPT, new string cannot be mapped again, except for
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 * script-local mappings.
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001000 * If "noremap" is > 0, that many characters of the new string cannot be mapped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 *
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001002 * If "nottyped" is TRUE, the string does not return KeyTyped (don't use when
1003 * "offset" is non-zero!).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 *
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001005 * If "silent" is TRUE, cmd_silent is set when the characters are obtained.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 *
1007 * return FAIL for failure, OK otherwise
1008 */
1009 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001010ins_typebuf(
1011 char_u *str,
1012 int noremap,
1013 int offset,
1014 int nottyped,
1015 int silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016{
1017 char_u *s1, *s2;
1018 int newlen;
1019 int addlen;
1020 int i;
1021 int newoff;
1022 int val;
1023 int nrm;
1024
1025 init_typebuf();
1026 if (++typebuf.tb_change_cnt == 0)
1027 typebuf.tb_change_cnt = 1;
Bram Moolenaard103ee72019-09-18 21:15:31 +02001028 state_no_longer_safe("ins_typebuf()");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029
1030 addlen = (int)STRLEN(str);
Bram Moolenaar4e86cba2007-05-06 11:56:32 +00001031
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 if (offset == 0 && addlen <= typebuf.tb_off)
1033 {
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001034 /*
1035 * Easy case: there is room in front of typebuf.tb_buf[typebuf.tb_off]
1036 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 typebuf.tb_off -= addlen;
1038 mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen);
1039 }
Bram Moolenaard34f9b12017-04-08 18:41:13 +02001040 else if (typebuf.tb_len == 0 && typebuf.tb_buflen
1041 >= addlen + 3 * (MAXMAPLEN + 4))
1042 {
1043 /*
1044 * Buffer is empty and string fits in the existing buffer.
1045 * Leave some space before and after, if possible.
1046 */
1047 typebuf.tb_off = (typebuf.tb_buflen - addlen - 3 * (MAXMAPLEN + 4)) / 2;
1048 mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen);
1049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 else
1051 {
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001052 int extra;
1053
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001054 /*
1055 * Need to allocate a new buffer.
Bram Moolenaard34f9b12017-04-08 18:41:13 +02001056 * In typebuf.tb_buf there must always be room for 3 * (MAXMAPLEN + 4)
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001057 * characters. We add some extra room to avoid having to allocate too
1058 * often.
1059 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 newoff = MAXMAPLEN + 4;
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001061 extra = addlen + newoff + 4 * (MAXMAPLEN + 4);
1062 if (typebuf.tb_len > 2147483647 - extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 {
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001064 // string is getting too long for a 32 bit int
Bram Moolenaar12f3c1b2021-12-05 21:46:34 +00001065 emsg(_(e_command_too_complex)); // also calls flush_buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 setcursor();
1067 return FAIL;
1068 }
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001069 newlen = typebuf.tb_len + extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 s1 = alloc(newlen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001071 if (s1 == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 return FAIL;
1073 s2 = alloc(newlen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001074 if (s2 == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 {
1076 vim_free(s1);
1077 return FAIL;
1078 }
1079 typebuf.tb_buflen = newlen;
1080
Bram Moolenaar30613902019-12-01 22:11:18 +01001081 // copy the old chars, before the insertion point
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 mch_memmove(s1 + newoff, typebuf.tb_buf + typebuf.tb_off,
1083 (size_t)offset);
Bram Moolenaar30613902019-12-01 22:11:18 +01001084 // copy the new chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 mch_memmove(s1 + newoff + offset, str, (size_t)addlen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001086 // copy the old chars, after the insertion point, including the NUL at
1087 // the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 mch_memmove(s1 + newoff + offset + addlen,
1089 typebuf.tb_buf + typebuf.tb_off + offset,
1090 (size_t)(typebuf.tb_len - offset + 1));
1091 if (typebuf.tb_buf != typebuf_init)
1092 vim_free(typebuf.tb_buf);
1093 typebuf.tb_buf = s1;
1094
1095 mch_memmove(s2 + newoff, typebuf.tb_noremap + typebuf.tb_off,
1096 (size_t)offset);
1097 mch_memmove(s2 + newoff + offset + addlen,
1098 typebuf.tb_noremap + typebuf.tb_off + offset,
1099 (size_t)(typebuf.tb_len - offset));
1100 if (typebuf.tb_noremap != noremapbuf_init)
1101 vim_free(typebuf.tb_noremap);
1102 typebuf.tb_noremap = s2;
1103
1104 typebuf.tb_off = newoff;
1105 }
1106 typebuf.tb_len += addlen;
1107
Bram Moolenaar30613902019-12-01 22:11:18 +01001108 // If noremap == REMAP_SCRIPT: do remap script-local mappings.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 if (noremap == REMAP_SCRIPT)
1110 val = RM_SCRIPT;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001111 else if (noremap == REMAP_SKIP)
1112 val = RM_ABBR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 else
1114 val = RM_NONE;
1115
1116 /*
1117 * Adjust typebuf.tb_noremap[] for the new characters:
1118 * If noremap == REMAP_NONE or REMAP_SCRIPT: new characters are
1119 * (sometimes) not remappable
1120 * If noremap == REMAP_YES: all the new characters are mappable
1121 * If noremap > 0: "noremap" characters are not remappable, the rest
1122 * mappable
1123 */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001124 if (noremap == REMAP_SKIP)
1125 nrm = 1;
1126 else if (noremap < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 nrm = addlen;
1128 else
1129 nrm = noremap;
1130 for (i = 0; i < addlen; ++i)
1131 typebuf.tb_noremap[typebuf.tb_off + i + offset] =
1132 (--nrm >= 0) ? val : RM_YES;
1133
Bram Moolenaar30613902019-12-01 22:11:18 +01001134 // tb_maplen and tb_silent only remember the length of mapped and/or
1135 // silent mappings at the start of the buffer, assuming that a mapped
1136 // sequence doesn't result in typed characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 if (nottyped || typebuf.tb_maplen > offset)
1138 typebuf.tb_maplen += addlen;
1139 if (silent || typebuf.tb_silent > offset)
1140 {
1141 typebuf.tb_silent += addlen;
1142 cmd_silent = TRUE;
1143 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001144 if (typebuf.tb_no_abbr_cnt && offset == 0) // and not used for abbrev.s
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 typebuf.tb_no_abbr_cnt += addlen;
1146
1147 return OK;
1148}
1149
1150/*
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001151 * Put character "c" back into the typeahead buffer.
1152 * Can be used for a character obtained by vgetc() that needs to be put back.
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00001153 * Uses cmd_silent, KeyTyped and KeyNoremap to restore the flags belonging to
1154 * the char.
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001155 * Returns the length of what was inserted.
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001156 */
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001157 int
zeertzjq2e7cba32022-06-10 15:30:32 +01001158ins_char_typebuf(int c, int modifiers)
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001159{
zeertzjq6cac7702022-01-04 18:01:21 +00001160 char_u buf[MB_MAXBYTES * 3 + 4];
zeertzjq2e7cba32022-06-10 15:30:32 +01001161 int len = special_to_buf(c, modifiers, TRUE, buf);
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001162
zeertzjq2e7cba32022-06-10 15:30:32 +01001163 buf[len] = NUL;
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00001164 (void)ins_typebuf(buf, KeyNoremap, 0, !KeyTyped, cmd_silent);
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001165 return len;
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001166}
1167
1168/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 * Return TRUE if the typeahead buffer was changed (while waiting for a
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001170 * character to arrive). Happens when a message was received from a client or
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001171 * from feedkeys().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 * But check in a more generic way to avoid trouble: When "typebuf.tb_buf"
1173 * changed it was reallocated and the old pointer can no longer be used.
1174 * Or "typebuf.tb_off" may have been changed and we would overwrite characters
1175 * that was just added.
1176 */
1177 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001178typebuf_changed(
Bram Moolenaar30613902019-12-01 22:11:18 +01001179 int tb_change_cnt) // old value of typebuf.tb_change_cnt
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180{
1181 return (tb_change_cnt != 0 && (typebuf.tb_change_cnt != tb_change_cnt
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001182#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
1183 || typebuf_was_filled
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184#endif
1185 ));
1186}
1187
1188/*
1189 * Return TRUE if there are no characters in the typeahead buffer that have
1190 * not been typed (result from a mapping or come from ":normal").
1191 */
1192 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001193typebuf_typed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194{
1195 return typebuf.tb_maplen == 0;
1196}
1197
1198/*
1199 * Return the number of characters that are mapped (or not typed).
1200 */
1201 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001202typebuf_maplen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203{
1204 return typebuf.tb_maplen;
1205}
1206
1207/*
1208 * remove "len" characters from typebuf.tb_buf[typebuf.tb_off + offset]
1209 */
1210 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001211del_typebuf(int len, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212{
1213 int i;
1214
1215 if (len == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01001216 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217
1218 typebuf.tb_len -= len;
1219
1220 /*
1221 * Easy case: Just increase typebuf.tb_off.
1222 */
1223 if (offset == 0 && typebuf.tb_buflen - (typebuf.tb_off + len)
1224 >= 3 * MAXMAPLEN + 3)
1225 typebuf.tb_off += len;
1226 /*
1227 * Have to move the characters in typebuf.tb_buf[] and typebuf.tb_noremap[]
1228 */
1229 else
1230 {
1231 i = typebuf.tb_off + offset;
1232 /*
1233 * Leave some extra room at the end to avoid reallocation.
1234 */
1235 if (typebuf.tb_off > MAXMAPLEN)
1236 {
1237 mch_memmove(typebuf.tb_buf + MAXMAPLEN,
1238 typebuf.tb_buf + typebuf.tb_off, (size_t)offset);
1239 mch_memmove(typebuf.tb_noremap + MAXMAPLEN,
1240 typebuf.tb_noremap + typebuf.tb_off, (size_t)offset);
1241 typebuf.tb_off = MAXMAPLEN;
1242 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001243 // adjust typebuf.tb_buf (include the NUL at the end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset,
1245 typebuf.tb_buf + i + len,
1246 (size_t)(typebuf.tb_len - offset + 1));
Bram Moolenaar30613902019-12-01 22:11:18 +01001247 // adjust typebuf.tb_noremap[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 mch_memmove(typebuf.tb_noremap + typebuf.tb_off + offset,
1249 typebuf.tb_noremap + i + len,
1250 (size_t)(typebuf.tb_len - offset));
1251 }
1252
Bram Moolenaar30613902019-12-01 22:11:18 +01001253 if (typebuf.tb_maplen > offset) // adjust tb_maplen
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 {
1255 if (typebuf.tb_maplen < offset + len)
1256 typebuf.tb_maplen = offset;
1257 else
1258 typebuf.tb_maplen -= len;
1259 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001260 if (typebuf.tb_silent > offset) // adjust tb_silent
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 {
1262 if (typebuf.tb_silent < offset + len)
1263 typebuf.tb_silent = offset;
1264 else
1265 typebuf.tb_silent -= len;
1266 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001267 if (typebuf.tb_no_abbr_cnt > offset) // adjust tb_no_abbr_cnt
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 {
1269 if (typebuf.tb_no_abbr_cnt < offset + len)
1270 typebuf.tb_no_abbr_cnt = offset;
1271 else
1272 typebuf.tb_no_abbr_cnt -= len;
1273 }
1274
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001275#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar30613902019-12-01 22:11:18 +01001276 // Reset the flag that text received from a client or from feedkeys()
1277 // was inserted in the typeahead buffer.
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001278 typebuf_was_filled = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279#endif
1280 if (++typebuf.tb_change_cnt == 0)
1281 typebuf.tb_change_cnt = 1;
1282}
1283
zeertzjq094c4392024-04-18 22:09:37 +02001284/*
1285 * State for adding bytes to a recording or 'showcmd'.
1286 */
zeertzjqacdfb8a2024-04-17 21:28:54 +02001287typedef struct
1288{
1289 int prev_c;
1290 char_u buf[MB_MAXBYTES * 3 + 4];
1291 size_t buflen;
1292 unsigned pending;
1293 int in_special;
1294 int in_mbyte;
1295} gotchars_state_T;
1296
1297/*
1298 * Add a single byte to a recording or 'showcmd'.
1299 * Return TRUE if a full key has been received, FALSE otherwise.
1300 */
1301 static int
1302gotchars_add_byte(gotchars_state_T *state, char_u byte)
1303{
1304 int c = state->buf[state->buflen++] = byte;
1305 int retval = FALSE;
1306
1307 if (state->pending > 0)
1308 state->pending--;
1309
1310 // When receiving a special key sequence, store it until we have all
1311 // the bytes and we can decide what to do with it.
1312 if ((state->pending == 0 || state->in_mbyte)
1313 && (c == K_SPECIAL
1314#ifdef FEAT_GUI
1315 || c == CSI
1316#endif
1317 ))
1318 {
1319 state->pending += 2;
1320 if (!state->in_mbyte)
1321 state->in_special = TRUE;
1322 }
1323
1324 if (state->pending > 0)
1325 goto ret_false;
1326
1327 if (!state->in_mbyte)
1328 {
1329 if (state->in_special)
1330 {
1331 state->in_special = FALSE;
1332 if (state->prev_c == KS_MODIFIER)
1333 // When receiving a modifier, wait for the modified key.
1334 goto ret_false;
1335 c = TO_SPECIAL(state->prev_c, c);
1336 if (c == K_FOCUSGAINED || c == K_FOCUSLOST)
1337 // Drop K_FOCUSGAINED and K_FOCUSLOST, they are not useful
1338 // in a recording.
1339 state->buflen = 0;
1340 }
1341 // When receiving a multibyte character, store it until we have all
1342 // the bytes, so that it won't be split between two buffer blocks,
1343 // and delete_buff_tail() will work properly.
1344 state->pending = MB_BYTE2LEN_CHECK(c) - 1;
1345 if (state->pending > 0)
1346 {
1347 state->in_mbyte = TRUE;
1348 goto ret_false;
1349 }
1350 }
1351 else
1352 // Stored all bytes of a multibyte character.
1353 state->in_mbyte = FALSE;
1354
1355 retval = TRUE;
1356ret_false:
1357 state->prev_c = c;
1358 return retval;
1359}
1360
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361/*
1362 * Write typed characters to script file.
zeertzjqea95f1a2024-03-29 10:11:42 +01001363 * If recording is on put the character in the record buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 */
1365 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001366gotchars(char_u *chars, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367{
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001368 char_u *s = chars;
zeertzjqea95f1a2024-03-29 10:11:42 +01001369 size_t i;
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001370 int todo = len;
zeertzjqacdfb8a2024-04-17 21:28:54 +02001371 static gotchars_state_T state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372
zeertzjqacdfb8a2024-04-17 21:28:54 +02001373 while (todo-- > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 {
zeertzjqacdfb8a2024-04-17 21:28:54 +02001375 if (!gotchars_add_byte(&state, *s++))
zeertzjqea95f1a2024-03-29 10:11:42 +01001376 continue;
1377
Bram Moolenaar30613902019-12-01 22:11:18 +01001378 // Handle one byte at a time; no translation to be done.
zeertzjqacdfb8a2024-04-17 21:28:54 +02001379 for (i = 0; i < state.buflen; ++i)
1380 updatescript(state.buf[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001382 if (reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 {
zeertzjqacdfb8a2024-04-17 21:28:54 +02001384 state.buf[state.buflen] = NUL;
1385 add_buff(&recordbuff, state.buf, (long)state.buflen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001386 // remember how many chars were last recorded
zeertzjqacdfb8a2024-04-17 21:28:54 +02001387 last_recorded_len += state.buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 }
zeertzjqacdfb8a2024-04-17 21:28:54 +02001389 state.buflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 }
zeertzjqea95f1a2024-03-29 10:11:42 +01001391
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 may_sync_undo();
1393
1394#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01001395 // output "debug mode" message next time in debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 debug_did_msg = FALSE;
1397#endif
1398
Bram Moolenaar30613902019-12-01 22:11:18 +01001399 // Since characters have been typed, consider the following to be in
1400 // another mapping. Search string will be kept in history.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 ++maptick;
1402}
1403
1404/*
zeertzjqbf321802024-01-28 19:03:00 +01001405 * Record an <Ignore> key.
zeertzjqbacc8302023-08-12 00:09:31 +02001406 */
1407 void
zeertzjqbf321802024-01-28 19:03:00 +01001408gotchars_ignore(void)
zeertzjqbacc8302023-08-12 00:09:31 +02001409{
zeertzjqbf321802024-01-28 19:03:00 +01001410 char_u nop_buf[3] = { K_SPECIAL, KS_EXTRA, KE_IGNORE };
zeertzjqbacc8302023-08-12 00:09:31 +02001411 gotchars(nop_buf, 3);
1412}
1413
1414/*
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001415 * Undo the last gotchars() for "len" bytes. To be used when putting a typed
1416 * character back into the typeahead buffer, thus gotchars() will be called
1417 * again.
1418 * Only affects recorded characters.
1419 */
1420 void
1421ungetchars(int len)
1422{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001423 if (reg_recording == 0)
1424 return;
1425
1426 delete_buff_tail(&recordbuff, len);
1427 last_recorded_len -= len;
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001428}
1429
1430/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 * Sync undo. Called when typed characters are obtained from the typeahead
1432 * buffer, or when a menu is used.
1433 * Do not sync:
1434 * - In Insert mode, unless cursor key has been used.
1435 * - While reading a script file.
1436 * - When no_u_sync is non-zero.
1437 */
1438 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001439may_sync_undo(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440{
Bram Moolenaar24959102022-05-07 20:01:16 +01001441 if ((!(State & (MODE_INSERT | MODE_CMDLINE)) || arrow_used)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001442 && scriptin[curscript] == NULL)
1443 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444}
1445
1446/*
1447 * Make "typebuf" empty and allocate new buffers.
1448 * Returns FAIL when out of memory.
1449 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001450 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001451alloc_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452{
1453 typebuf.tb_buf = alloc(TYPELEN_INIT);
1454 typebuf.tb_noremap = alloc(TYPELEN_INIT);
1455 if (typebuf.tb_buf == NULL || typebuf.tb_noremap == NULL)
1456 {
1457 free_typebuf();
1458 return FAIL;
1459 }
1460 typebuf.tb_buflen = TYPELEN_INIT;
Bram Moolenaar30613902019-12-01 22:11:18 +01001461 typebuf.tb_off = MAXMAPLEN + 4; // can insert without realloc
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 typebuf.tb_len = 0;
1463 typebuf.tb_maplen = 0;
1464 typebuf.tb_silent = 0;
1465 typebuf.tb_no_abbr_cnt = 0;
1466 if (++typebuf.tb_change_cnt == 0)
1467 typebuf.tb_change_cnt = 1;
Bram Moolenaare49b4bb2020-03-11 13:01:40 +01001468#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
1469 typebuf_was_filled = FALSE;
1470#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 return OK;
1472}
1473
1474/*
1475 * Free the buffers of "typebuf".
1476 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001477 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001478free_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479{
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001480 if (typebuf.tb_buf == typebuf_init)
Bram Moolenaar95f09602016-11-10 20:01:45 +01001481 internal_error("Free typebuf 1");
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001482 else
Bram Moolenaar0f1c6702019-09-28 15:24:00 +02001483 VIM_CLEAR(typebuf.tb_buf);
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00001484 if (typebuf.tb_noremap == noremapbuf_init)
Bram Moolenaar95f09602016-11-10 20:01:45 +01001485 internal_error("Free typebuf 2");
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001486 else
Bram Moolenaar0f1c6702019-09-28 15:24:00 +02001487 VIM_CLEAR(typebuf.tb_noremap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488}
1489
1490/*
1491 * When doing ":so! file", the current typeahead needs to be saved, and
1492 * restored when "file" has been read completely.
1493 */
1494static typebuf_T saved_typebuf[NSCRIPT];
1495
1496 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001497save_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498{
1499 init_typebuf();
1500 saved_typebuf[curscript] = typebuf;
Bram Moolenaar30613902019-12-01 22:11:18 +01001501 // If out of memory: restore typebuf and close file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 if (alloc_typebuf() == FAIL)
1503 {
1504 closescript();
1505 return FAIL;
1506 }
1507 return OK;
1508}
1509
Bram Moolenaar30613902019-12-01 22:11:18 +01001510static int old_char = -1; // character put back by vungetc()
1511static int old_mod_mask; // mod_mask for ungotten character
1512static int old_mouse_row; // mouse_row related to old_char
1513static int old_mouse_col; // mouse_col related to old_char
zeertzjq0f68e6c2022-04-05 13:17:01 +01001514static int old_KeyStuffed; // whether old_char was stuffed
1515
zeertzjq6d4e7252022-04-07 13:58:04 +01001516static int can_get_old_char(void)
zeertzjq0f68e6c2022-04-05 13:17:01 +01001517{
1518 // If the old character was not stuffed and characters have been added to
1519 // the stuff buffer, need to first get the stuffed characters instead.
1520 return old_char != -1 && (old_KeyStuffed || stuff_empty());
1521}
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001522
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523/*
1524 * Save all three kinds of typeahead, so that the user must type at a prompt.
1525 */
1526 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001527save_typeahead(tasave_T *tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528{
1529 tp->save_typebuf = typebuf;
1530 tp->typebuf_valid = (alloc_typebuf() == OK);
1531 if (!tp->typebuf_valid)
1532 typebuf = tp->save_typebuf;
1533
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001534 tp->old_char = old_char;
1535 tp->old_mod_mask = old_mod_mask;
1536 old_char = -1;
1537
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001538 tp->save_readbuf1 = readbuf1;
Bram Moolenaar285e3352018-04-18 23:01:13 +02001539 readbuf1.bh_first.b_next = NULL;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001540 tp->save_readbuf2 = readbuf2;
Bram Moolenaar285e3352018-04-18 23:01:13 +02001541 readbuf2.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542# ifdef USE_INPUT_BUF
1543 tp->save_inputbuf = get_input_buf();
1544# endif
1545}
1546
1547/*
1548 * Restore the typeahead to what it was before calling save_typeahead().
1549 * The allocated memory is freed, can only be called once!
Bram Moolenaarc41badb2021-06-07 22:04:52 +02001550 * When "overwrite" is FALSE input typed later is kept.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 */
1552 void
Bram Moolenaarc41badb2021-06-07 22:04:52 +02001553restore_typeahead(tasave_T *tp, int overwrite UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554{
1555 if (tp->typebuf_valid)
1556 {
1557 free_typebuf();
1558 typebuf = tp->save_typebuf;
1559 }
1560
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001561 old_char = tp->old_char;
1562 old_mod_mask = tp->old_mod_mask;
1563
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001564 free_buff(&readbuf1);
1565 readbuf1 = tp->save_readbuf1;
1566 free_buff(&readbuf2);
1567 readbuf2 = tp->save_readbuf2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568# ifdef USE_INPUT_BUF
Bram Moolenaarc41badb2021-06-07 22:04:52 +02001569 set_input_buf(tp->save_inputbuf, overwrite);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570# endif
1571}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572
1573/*
1574 * Open a new script file for the ":source!" command.
1575 */
1576 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001577openscript(
1578 char_u *name,
Bram Moolenaar30613902019-12-01 22:11:18 +01001579 int directly) // when TRUE execute directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580{
1581 if (curscript + 1 == NSCRIPT)
1582 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001583 emsg(_(e_scripts_nested_too_deep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 return;
1585 }
Bram Moolenaar53575522019-05-22 22:38:25 +02001586
1587 // Disallow sourcing a file in the sandbox, the commands would be executed
1588 // later, possibly outside of the sandbox.
1589 if (check_secure())
1590 return;
1591
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001592#ifdef FEAT_EVAL
1593 if (ignore_script)
Bram Moolenaar30613902019-12-01 22:11:18 +01001594 // Not reading from script, also don't open one. Warning message?
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001595 return;
1596#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597
Bram Moolenaar30613902019-12-01 22:11:18 +01001598 if (scriptin[curscript] != NULL) // already reading script
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 ++curscript;
Bram Moolenaar30613902019-12-01 22:11:18 +01001600 // use NameBuff for expanded name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 expand_env(name, NameBuff, MAXPATHL);
1602 if ((scriptin[curscript] = mch_fopen((char *)NameBuff, READBIN)) == NULL)
1603 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001604 semsg(_(e_cant_open_file_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 if (curscript)
1606 --curscript;
1607 return;
1608 }
1609 if (save_typebuf() == FAIL)
1610 return;
1611
1612 /*
1613 * Execute the commands from the file right now when using ":source!"
1614 * after ":global" or ":argdo" or in a loop. Also when another command
1615 * follows. This means the display won't be updated. Don't do this
1616 * always, "make test" would fail.
1617 */
1618 if (directly)
1619 {
1620 oparg_T oa;
1621 int oldcurscript;
1622 int save_State = State;
1623 int save_restart_edit = restart_edit;
1624 int save_insertmode = p_im;
1625 int save_finish_op = finish_op;
1626 int save_msg_scroll = msg_scroll;
1627
Bram Moolenaar24959102022-05-07 20:01:16 +01001628 State = MODE_NORMAL;
Bram Moolenaar30613902019-12-01 22:11:18 +01001629 msg_scroll = FALSE; // no msg scrolling in Normal mode
1630 restart_edit = 0; // don't go to Insert mode
1631 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 clear_oparg(&oa);
1633 finish_op = FALSE;
1634
1635 oldcurscript = curscript;
1636 do
1637 {
Bram Moolenaar386b43e2019-05-19 21:57:11 +02001638 update_topline_cursor(); // update cursor position and topline
1639 normal_cmd(&oa, FALSE); // execute one command
Bram Moolenaarae97b942020-07-09 19:16:35 +02001640 (void)vpeekc(); // check for end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 }
1642 while (scriptin[oldcurscript] != NULL);
1643
1644 State = save_State;
1645 msg_scroll = save_msg_scroll;
1646 restart_edit = save_restart_edit;
1647 p_im = save_insertmode;
1648 finish_op = save_finish_op;
1649 }
1650}
1651
1652/*
1653 * Close the currently active input script.
1654 */
1655 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001656closescript(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657{
1658 free_typebuf();
1659 typebuf = saved_typebuf[curscript];
1660
1661 fclose(scriptin[curscript]);
1662 scriptin[curscript] = NULL;
1663 if (curscript > 0)
1664 --curscript;
1665}
1666
Bram Moolenaarea408852005-06-25 22:49:46 +00001667#if defined(EXITFREE) || defined(PROTO)
1668 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001669close_all_scripts(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00001670{
1671 while (scriptin[0] != NULL)
1672 closescript();
1673}
1674#endif
1675
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676/*
1677 * Return TRUE when reading keys from a script file.
1678 */
1679 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001680using_script(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681{
1682 return scriptin[curscript] != NULL;
1683}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684
1685/*
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001686 * This function is called just before doing a blocking wait. Thus after
1687 * waiting 'updatetime' for a character to arrive.
1688 */
1689 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001690before_blocking(void)
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001691{
1692 updatescript(0);
1693#ifdef FEAT_EVAL
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001694 if (may_garbage_collect)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001695 garbage_collect(FALSE);
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001696#endif
1697}
1698
1699/*
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001700 * updatescript() is called when a character can be written into the script
1701 * file or when we have waited some time for a character (c == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 *
1703 * All the changed memfiles are synced if c == 0 or when the number of typed
1704 * characters reaches 'updatecount' and 'updatecount' is non-zero.
1705 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001706 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001707updatescript(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708{
1709 static int count = 0;
1710
1711 if (c && scriptout)
1712 putc(c, scriptout);
1713 if (c == 0 || (p_uc > 0 && ++count >= p_uc))
1714 {
1715 ml_sync_all(c == 0, TRUE);
1716 count = 0;
1717 }
1718}
1719
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720/*
RestorerZ68ebcee2023-05-31 17:12:14 +01001721 * Convert "c_arg" plus "modifiers" to merge the effect of modifyOtherKeys into
1722 * the character. Also for when the Kitty key protocol is used.
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001723 */
1724 int
Bram Moolenaar975a8802020-06-06 22:36:24 +02001725merge_modifyOtherKeys(int c_arg, int *modifiers)
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001726{
1727 int c = c_arg;
1728
Bram Moolenaarea83c192023-03-18 17:22:46 +00001729 // CTRL only uses the lower 5 bits of the character.
zeertzjqbad8a012022-04-29 16:44:00 +01001730 if (*modifiers & MOD_MASK_CTRL)
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001731 {
1732 if ((c >= '`' && c <= 0x7f) || (c >= '@' && c <= '_'))
zeertzjq17c95d92022-04-26 12:51:07 +01001733 {
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001734 c &= 0x1f;
zeertzjq17c95d92022-04-26 12:51:07 +01001735 if (c == NUL)
1736 c = K_ZERO;
1737 }
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001738 else if (c == '6')
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001739 // CTRL-6 is equivalent to CTRL-^
1740 c = 0x1e;
Bram Moolenaarf4ae6b22020-05-30 19:52:46 +02001741#ifdef FEAT_GUI_GTK
1742 // These mappings look arbitrary at the first glance, but in fact
1743 // resemble quite exactly the behaviour of the GTK+ 1.2 GUI on my
1744 // machine. The only difference is BS vs. DEL for CTRL-8 (makes
1745 // more sense and is consistent with usual terminal behaviour).
1746 else if (c == '2')
1747 c = NUL;
1748 else if (c >= '3' && c <= '7')
1749 c = c ^ 0x28;
1750 else if (c == '8')
1751 c = BS;
1752 else if (c == '?')
1753 c = DEL;
1754#endif
1755 if (c != c_arg)
Bram Moolenaar975a8802020-06-06 22:36:24 +02001756 *modifiers &= ~MOD_MASK_CTRL;
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001757 }
Bram Moolenaarea83c192023-03-18 17:22:46 +00001758
1759 // Alt/Meta sets the 8th bit of the character.
Bram Moolenaar975a8802020-06-06 22:36:24 +02001760 if ((*modifiers & (MOD_MASK_META | MOD_MASK_ALT))
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001761 && c >= 0 && c <= 127)
1762 {
Bram Moolenaarea83c192023-03-18 17:22:46 +00001763 // Some terminals (esp. Kitty) do not include Shift in the character.
1764 // Apply it here to get consistency across terminals. Only do ASCII
1765 // letters, for other characters it depends on the keyboard layout.
1766 if ((*modifiers & MOD_MASK_SHIFT) && c >= 'a' && c <= 'z')
1767 {
1768 c += 'a' - 'A';
1769 *modifiers &= ~MOD_MASK_SHIFT;
1770 }
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001771 c += 0x80;
Bram Moolenaara9a6b032023-02-05 18:00:42 +00001772 *modifiers &= ~(MOD_MASK_META | MOD_MASK_ALT);
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001773 }
Bram Moolenaarea83c192023-03-18 17:22:46 +00001774
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001775 return c;
1776}
1777
1778/*
zeertzjqacdfb8a2024-04-17 21:28:54 +02001779 * Add a single byte to 'showcmd' for a partially matched mapping.
1780 * Call add_to_showcmd() if a full key has been received.
1781 */
1782 static void
1783add_byte_to_showcmd(char_u byte)
1784{
1785 static gotchars_state_T state;
1786 char_u *ptr;
1787 int modifiers = 0;
1788 int c = NUL;
1789
1790 if (!p_sc || msg_silent != 0)
1791 return;
1792
1793 if (!gotchars_add_byte(&state, byte))
1794 return;
1795
1796 state.buf[state.buflen] = NUL;
1797 state.buflen = 0;
1798
1799 ptr = state.buf;
1800 if (ptr[0] == K_SPECIAL && ptr[1] == KS_MODIFIER && ptr[2] != NUL)
1801 {
1802 modifiers = ptr[2];
1803 ptr += 3;
1804 }
1805
1806 if (*ptr != NUL)
1807 {
1808 char_u *mb_ptr = mb_unescape(&ptr);
1809
1810 c = mb_ptr != NULL ? (*mb_ptr2char)(mb_ptr) : *ptr++;
1811 if (c <= 0x7f)
1812 {
1813 // Merge modifiers into the key to make the result more readable.
1814 int modifiers_after = modifiers;
1815 int mod_c = merge_modifyOtherKeys(c, &modifiers_after);
1816
1817 if (modifiers_after == 0)
1818 {
1819 modifiers = 0;
1820 c = mod_c;
1821 }
1822 }
1823 }
1824
1825 // TODO: is there a more readable and yet compact representation of
1826 // modifiers and special keys?
1827 if (modifiers != 0)
1828 {
1829 add_to_showcmd(K_SPECIAL);
1830 add_to_showcmd(KS_MODIFIER);
1831 add_to_showcmd(modifiers);
1832 }
1833 if (c != NUL)
1834 add_to_showcmd(c);
1835 while (*ptr != NUL)
1836 add_to_showcmd(*ptr++);
1837}
1838
1839/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 * Get the next input character.
1841 * Can return a special key or a multi-byte character.
1842 * Can return NUL when called recursively, use safe_vgetc() if that's not
1843 * wanted.
1844 * This translates escaped K_SPECIAL and CSI bytes to a K_SPECIAL or CSI byte.
1845 * Collects the bytes of a multibyte character into the whole character.
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00001846 * Returns the modifiers in the global "mod_mask".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 */
1848 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001849vgetc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850{
1851 int c, c2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001853 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001856#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01001857 // Do garbage collection when garbagecollect() was called previously and
1858 // we are now at the toplevel.
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001859 if (may_garbage_collect && want_garbage_collect)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001860 garbage_collect(FALSE);
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001861#endif
1862
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 /*
1864 * If a character was put back with vungetc, it was already processed.
1865 * Return it directly.
1866 */
zeertzjq0f68e6c2022-04-05 13:17:01 +01001867 if (can_get_old_char())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 {
1869 c = old_char;
1870 old_char = -1;
1871 mod_mask = old_mod_mask;
Bram Moolenaarb8978712013-03-16 21:42:16 +01001872 mouse_row = old_mouse_row;
1873 mouse_col = old_mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001875 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 {
zeertzjq81b46a62022-04-09 17:58:49 +01001877 // number of characters recorded from the last vgetc() call
Mike Williamsaca8f552024-04-07 18:26:22 +02001878 static size_t last_vgetc_recorded_len = 0;
zeertzjq81b46a62022-04-09 17:58:49 +01001879
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001880 mod_mask = 0;
1881 vgetc_mod_mask = 0;
1882 vgetc_char = 0;
zeertzjq81b46a62022-04-09 17:58:49 +01001883
1884 // last_recorded_len can be larger than last_vgetc_recorded_len
1885 // if peeking records more
1886 last_recorded_len -= last_vgetc_recorded_len;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001887
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001888 for (;;) // this is done twice if there are modifiers
1889 {
1890 int did_inc = FALSE;
Bram Moolenaar2455c4e2015-09-15 18:29:39 +02001891
Bram Moolenaar78aed952022-09-24 15:36:35 +01001892 // No mapping after modifier has been read, using an input method
1893 // and when a popup window has disabled mapping.
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001894 if (mod_mask
Bram Moolenaar39719052017-09-05 22:20:46 +02001895#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001896 || im_is_preediting()
Bram Moolenaar39719052017-09-05 22:20:46 +02001897#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001898#if defined(FEAT_PROP_POPUP)
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001899 || popup_no_mapping()
1900#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001901 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 {
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001903 ++no_mapping;
1904 ++allow_keys;
Bram Moolenaar78aed952022-09-24 15:36:35 +01001905 // mod_mask value may change, remember we did the increment
1906 did_inc = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 }
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01001908 c = vgetorpeek(TRUE);
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001909 if (did_inc)
1910 {
1911 --no_mapping;
1912 --allow_keys;
1913 }
1914
Christopher Plewright03193062022-11-22 12:58:27 +00001915 // Get two extra bytes for special keys, handle modifiers.
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001916 if (c == K_SPECIAL
Christopher Plewright03193062022-11-22 12:58:27 +00001917#ifdef FEAT_GUI
Christopher Plewright605d02a2022-10-19 11:54:46 +01001918 || c == CSI
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001919#endif
1920 )
1921 {
1922 int save_allow_keys = allow_keys;
1923
1924 ++no_mapping;
1925 allow_keys = 0; // make sure BS is not found
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01001926 c2 = vgetorpeek(TRUE); // no mapping for these chars
1927 c = vgetorpeek(TRUE);
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001928 --no_mapping;
1929 allow_keys = save_allow_keys;
1930 if (c2 == KS_MODIFIER)
1931 {
1932 mod_mask = c;
1933 continue;
1934 }
1935 c = TO_SPECIAL(c2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936
Bram Moolenaardffa6ea2022-11-29 20:33:20 +00001937 // K_ESC is used to avoid ambiguity with the single Esc
1938 // character that might be the start of an escape sequence.
1939 // Convert it back to a single Esc here.
1940 if (c == K_ESC)
1941 c = ESC;
1942
Bram Moolenaar4f974752019-02-17 17:44:42 +01001943#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001944 // Handle K_TEAROFF here, the caller of vgetc() doesn't need to
1945 // know that a menu was torn off
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001946 if (
1947# ifdef VIMDLL
1948 gui.in_use &&
1949# endif
1950 c == K_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 {
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001952 char_u name[200];
K.Takata54119102022-02-03 13:33:03 +00001953 int j;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001954
1955 // get menu path, it ends with a <CR>
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01001956 for (j = 0; (c = vgetorpeek(TRUE)) != '\r'; )
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001957 {
K.Takata54119102022-02-03 13:33:03 +00001958 name[j] = c;
1959 if (j < 199)
1960 ++j;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001961 }
K.Takata54119102022-02-03 13:33:03 +00001962 name[j] = NUL;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001963 gui_make_tearoff(name);
1964 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966#endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001967#if defined(FEAT_GUI) && defined(FEAT_GUI_GTK) && defined(FEAT_MENU)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001968 // GTK: <F10> normally selects the menu, but it's passed until
1969 // here to allow mapping it. Intercept and invoke the GTK
1970 // behavior if it's not mapped.
1971 if (c == K_F10 && gui.menubar != NULL)
1972 {
1973 gtk_menu_shell_select_first(
1974 GTK_MENU_SHELL(gui.menubar), FALSE);
1975 continue;
1976 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001977#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978#ifdef FEAT_GUI
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001979 // Handle focus event here, so that the caller doesn't need to
1980 // know about it. Return K_IGNORE so that we loop once (needed
1981 // if 'lazyredraw' is set).
1982 if (c == K_FOCUSGAINED || c == K_FOCUSLOST)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001983 {
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001984 ui_focus_change(c == K_FOCUSGAINED);
1985 c = K_IGNORE;
Bram Moolenaara9dd2d32019-04-29 21:58:41 +02001986 }
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001987
1988 // Translate K_CSI to CSI. The special key is only used to
1989 // avoid it being recognized as the start of a special key.
1990 if (c == K_CSI)
1991 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992#endif
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01001993#ifdef FEAT_EVAL
1994 if (c == K_SID)
1995 {
1996 int j;
1997
1998 // Handle <SID>{sid}; Do up to 20 digits for safety.
1999 last_used_sid = 0;
Keith Thompson184f71c2024-01-04 21:19:04 +01002000 for (j = 0; j < 20 && SAFE_isdigit(c = vgetorpeek(TRUE)); ++j)
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01002001 last_used_sid = last_used_sid * 10 + (c - '0');
2002 last_used_map = NULL;
2003 continue;
2004 }
2005#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002006 }
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01002007
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002008 // a keypad or special function key was not mapped, use it like
2009 // its ASCII equivalent
2010 switch (c)
2011 {
2012 case K_KPLUS: c = '+'; break;
2013 case K_KMINUS: c = '-'; break;
2014 case K_KDIVIDE: c = '/'; break;
2015 case K_KMULTIPLY: c = '*'; break;
2016 case K_KENTER: c = CAR; break;
2017 case K_KPOINT:
Bram Moolenaar4f974752019-02-17 17:44:42 +01002018#ifdef MSWIN
Bram Moolenaar8e85db02018-07-27 23:16:51 +02002019 // Can be either '.' or a ',',
2020 // depending on the type of keypad.
2021 c = MapVirtualKey(VK_DECIMAL, 2); break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002022#else
Bram Moolenaar8e85db02018-07-27 23:16:51 +02002023 c = '.'; break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002024#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002025 case K_K0: c = '0'; break;
2026 case K_K1: c = '1'; break;
2027 case K_K2: c = '2'; break;
2028 case K_K3: c = '3'; break;
2029 case K_K4: c = '4'; break;
2030 case K_K5: c = '5'; break;
2031 case K_K6: c = '6'; break;
2032 case K_K7: c = '7'; break;
2033 case K_K8: c = '8'; break;
2034 case K_K9: c = '9'; break;
Bram Moolenaara88d9682005-03-25 21:45:43 +00002035
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002036 case K_XHOME:
2037 case K_ZHOME: if (mod_mask == MOD_MASK_SHIFT)
Bram Moolenaara88d9682005-03-25 21:45:43 +00002038 {
2039 c = K_S_HOME;
2040 mod_mask = 0;
2041 }
2042 else if (mod_mask == MOD_MASK_CTRL)
2043 {
2044 c = K_C_HOME;
2045 mod_mask = 0;
2046 }
2047 else
2048 c = K_HOME;
2049 break;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002050 case K_XEND:
2051 case K_ZEND: if (mod_mask == MOD_MASK_SHIFT)
Bram Moolenaara88d9682005-03-25 21:45:43 +00002052 {
2053 c = K_S_END;
2054 mod_mask = 0;
2055 }
2056 else if (mod_mask == MOD_MASK_CTRL)
2057 {
2058 c = K_C_END;
2059 mod_mask = 0;
2060 }
2061 else
2062 c = K_END;
2063 break;
2064
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002065 case K_XUP: c = K_UP; break;
2066 case K_XDOWN: c = K_DOWN; break;
2067 case K_XLEFT: c = K_LEFT; break;
2068 case K_XRIGHT: c = K_RIGHT; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002071 // For a multi-byte character get all the bytes and return the
2072 // converted character.
2073 // Note: This will loop until enough bytes are received!
2074 if (has_mbyte && (n = MB_BYTE2LEN_CHECK(c)) > 1)
2075 {
2076 ++no_mapping;
2077 buf[0] = c;
2078 for (i = 1; i < n; ++i)
2079 {
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01002080 buf[i] = vgetorpeek(TRUE);
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002081 if (buf[i] == K_SPECIAL
2082#ifdef FEAT_GUI
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01002083 || (buf[i] == CSI)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002084#endif
2085 )
2086 {
2087 // Must be a K_SPECIAL - KS_SPECIAL - KE_FILLER
2088 // sequence, which represents a K_SPECIAL (0x80),
2089 // or a CSI - KS_EXTRA - KE_CSI sequence, which
2090 // represents a CSI (0x9B),
2091 // or a K_SPECIAL - KS_EXTRA - KE_CSI, which is CSI
2092 // too.
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01002093 c = vgetorpeek(TRUE);
2094 if (vgetorpeek(TRUE) == KE_CSI && c == KS_EXTRA)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002095 buf[i] = CSI;
2096 }
2097 }
2098 --no_mapping;
2099 c = (*mb_ptr2char)(buf);
2100 }
2101
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002102 if (vgetc_char == 0)
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02002103 {
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02002104 vgetc_mod_mask = mod_mask;
2105 vgetc_char = c;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02002106 }
Bram Moolenaar00eab7f2019-10-10 21:49:28 +02002107
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002108 break;
2109 }
zeertzjq81b46a62022-04-09 17:58:49 +01002110
2111 last_vgetc_recorded_len = last_recorded_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00002113
2114#ifdef FEAT_EVAL
2115 /*
2116 * In the main loop "may_garbage_collect" can be set to do garbage
2117 * collection in the first next vgetc(). It's disabled after that to
2118 * avoid internally used Lists and Dicts to be freed.
2119 */
2120 may_garbage_collect = FALSE;
2121#endif
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002122
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01002123#ifdef FEAT_BEVAL_TERM
Bram Moolenaarc2f50542019-07-05 23:24:56 +02002124 if (c != K_MOUSEMOVE && c != K_IGNORE && c != K_CURSORHOLD)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002125 {
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002126 // Don't trigger 'balloonexpr' unless only the mouse was moved.
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002127 bevalexpr_due_set = FALSE;
2128 ui_remove_balloon();
2129 }
2130#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002131#ifdef FEAT_PROP_POPUP
Bram Moolenaar4934ad02020-09-28 22:29:58 +02002132 // Only filter keys that do not come from ":normal". Keys from feedkeys()
2133 // are filtered.
2134 if ((!ex_normal_busy || in_feedkeys) && popup_do_filter(c))
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02002135 {
2136 if (c == Ctrl_C)
2137 got_int = FALSE; // avoid looping
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002138 c = K_IGNORE;
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02002139 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002140#endif
Bram Moolenaar9fecb462006-09-05 10:59:47 +00002141
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002142 // Need to process the character before we know it's safe to do something
2143 // else.
2144 if (c != K_IGNORE)
Bram Moolenaard103ee72019-09-18 21:15:31 +02002145 state_no_longer_safe("key typed");
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002146
Bram Moolenaar9fecb462006-09-05 10:59:47 +00002147 return c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148}
2149
2150/*
2151 * Like vgetc(), but never return a NUL when called recursively, get a key
2152 * directly from the user (ignoring typeahead).
2153 */
2154 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002155safe_vgetc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156{
2157 int c;
2158
2159 c = vgetc();
2160 if (c == NUL)
2161 c = get_keystroke();
2162 return c;
2163}
2164
2165/*
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002166 * Like safe_vgetc(), but loop to handle K_IGNORE.
2167 * Also ignore scrollbar events.
Bram Moolenaar78aed952022-09-24 15:36:35 +01002168 * Does not handle bracketed paste - do not use the result for commands.
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002169 */
Bram Moolenaar78aed952022-09-24 15:36:35 +01002170 static int
2171plain_vgetc_nopaste(void)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002172{
2173 int c;
2174
2175 do
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002176 c = safe_vgetc();
Bram Moolenaar3a006592021-05-05 19:58:17 +02002177 while (c == K_IGNORE
2178 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
2179 || c == K_MOUSEMOVE);
Bram Moolenaar78aed952022-09-24 15:36:35 +01002180 return c;
2181}
2182
2183/*
2184 * Like safe_vgetc(), but loop to handle K_IGNORE.
2185 * Also ignore scrollbar events.
2186 */
2187 int
2188plain_vgetc(void)
2189{
2190 int c = plain_vgetc_nopaste();
Bram Moolenaarec2da362017-01-21 20:04:22 +01002191
2192 if (c == K_PS)
Bram Moolenaar30613902019-12-01 22:11:18 +01002193 // Only handle the first pasted character. Drop the rest, since we
2194 // don't know what to do with it.
Bram Moolenaarec2da362017-01-21 20:04:22 +01002195 c = bracketed_paste(PASTE_ONE_CHAR, FALSE, NULL);
2196
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002197 return c;
2198}
2199
2200/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 * Check if a character is available, such that vgetc() will not block.
2202 * If the next character is a special character or multi-byte, the returned
2203 * character is not valid!.
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02002204 * Returns NUL if no character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 */
2206 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002207vpeekc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208{
zeertzjq0f68e6c2022-04-05 13:17:01 +01002209 if (can_get_old_char())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 return old_char;
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01002211 return vgetorpeek(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212}
2213
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01002214#if defined(FEAT_TERMRESPONSE) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215/*
2216 * Like vpeekc(), but don't allow mapping. Do allow checking for terminal
2217 * codes.
2218 */
2219 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002220vpeekc_nomap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221{
2222 int c;
2223
2224 ++no_mapping;
2225 ++allow_keys;
2226 c = vpeekc();
2227 --no_mapping;
2228 --allow_keys;
2229 return c;
2230}
2231#endif
2232
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233/*
2234 * Check if any character is available, also half an escape sequence.
2235 * Trick: when no typeahead found, but there is something in the typeahead
2236 * buffer, it must be an ESC that is recognized as the start of a key code.
2237 */
2238 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002239vpeekc_any(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240{
2241 int c;
2242
2243 c = vpeekc();
2244 if (c == NUL && typebuf.tb_len > 0)
2245 c = ESC;
2246 return c;
2247}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248
2249/*
2250 * Call vpeekc() without causing anything to be mapped.
2251 * Return TRUE if a character is available, FALSE otherwise.
2252 */
2253 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002254char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255{
2256 int retval;
2257
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01002258#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01002259 // When test_override("char_avail", 1) was called pretend there is no
2260 // typeahead.
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01002261 if (disable_char_avail_for_testing)
2262 return FALSE;
2263#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 ++no_mapping;
2265 retval = vpeekc();
2266 --no_mapping;
2267 return (retval != NUL);
2268}
2269
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002270#if defined(FEAT_EVAL) || defined(PROTO)
2271/*
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002272 * "getchar()" and "getcharstr()" functions
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002273 */
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002274 static void
2275getchar_common(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002276{
2277 varnumber_T n;
2278 int error = FALSE;
2279
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002280 if (in_vim9script() && check_for_opt_bool_arg(argvars, 0) == FAIL)
2281 return;
2282
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002283#ifdef MESSAGE_QUEUE
2284 // vpeekc() used to check for messages, but that caused problems, invoking
2285 // a callback where it was not expected. Some plugins use getchar(1) in a
2286 // loop to await a message, therefore make sure we check for messages here.
2287 parse_queued_messages();
2288#endif
2289
Bram Moolenaar30613902019-12-01 22:11:18 +01002290 // Position the cursor. Needed after a message that ends in a space.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002291 windgoto(msg_row, msg_col);
2292
2293 ++no_mapping;
2294 ++allow_keys;
2295 for (;;)
2296 {
2297 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar30613902019-12-01 22:11:18 +01002298 // getchar(): blocking wait.
Bram Moolenaar78aed952022-09-24 15:36:35 +01002299 n = plain_vgetc_nopaste();
Bram Moolenaarc08cc722020-09-05 17:51:23 +02002300 else if (tv_get_bool_chk(&argvars[0], &error))
Bram Moolenaar30613902019-12-01 22:11:18 +01002301 // getchar(1): only check if char avail
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002302 n = vpeekc_any();
2303 else if (error || vpeekc_any() == NUL)
Bram Moolenaar30613902019-12-01 22:11:18 +01002304 // illegal argument or getchar(0) and no char avail: return zero
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002305 n = 0;
2306 else
Bram Moolenaar15183b42020-09-05 19:59:39 +02002307 // getchar(0) and char avail() != NUL: get a character.
2308 // Note that vpeekc_any() returns K_SPECIAL for K_IGNORE.
2309 n = safe_vgetc();
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002310
Bram Moolenaar15183b42020-09-05 19:59:39 +02002311 if (n == K_IGNORE || n == K_MOUSEMOVE
2312 || n == K_VER_SCROLLBAR || n == K_HOR_SCROLLBAR)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002313 continue;
2314 break;
2315 }
2316 --no_mapping;
2317 --allow_keys;
2318
2319 set_vim_var_nr(VV_MOUSE_WIN, 0);
2320 set_vim_var_nr(VV_MOUSE_WINID, 0);
2321 set_vim_var_nr(VV_MOUSE_LNUM, 0);
2322 set_vim_var_nr(VV_MOUSE_COL, 0);
2323
2324 rettv->vval.v_number = n;
zeertzjqad6c45f2022-02-20 19:05:10 +00002325 if (n != 0 && (IS_SPECIAL(n) || mod_mask != 0))
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002326 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002327 char_u temp[10]; // modifier: 3, mbyte-char: 6, NUL: 1
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002328 int i = 0;
2329
Bram Moolenaar30613902019-12-01 22:11:18 +01002330 // Turn a special key into three bytes, plus modifier.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002331 if (mod_mask != 0)
2332 {
2333 temp[i++] = K_SPECIAL;
2334 temp[i++] = KS_MODIFIER;
2335 temp[i++] = mod_mask;
2336 }
2337 if (IS_SPECIAL(n))
2338 {
2339 temp[i++] = K_SPECIAL;
2340 temp[i++] = K_SECOND(n);
2341 temp[i++] = K_THIRD(n);
2342 }
2343 else if (has_mbyte)
2344 i += (*mb_char2bytes)(n, temp + i);
2345 else
2346 temp[i++] = n;
2347 temp[i++] = NUL;
2348 rettv->v_type = VAR_STRING;
2349 rettv->vval.v_string = vim_strsave(temp);
2350
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002351 if (is_mouse_key(n))
2352 {
2353 int row = mouse_row;
2354 int col = mouse_col;
2355 win_T *win;
2356 linenr_T lnum;
2357 win_T *wp;
2358 int winnr = 1;
2359
2360 if (row >= 0 && col >= 0)
2361 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002362 // Find the window at the mouse coordinates and compute the
2363 // text position.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002364 win = mouse_find_win(&row, &col, FIND_POPUP);
2365 if (win == NULL)
2366 return;
2367 (void)mouse_comp_pos(win, &row, &col, &lnum, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002368#ifdef FEAT_PROP_POPUP
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002369 if (WIN_IS_POPUP(win))
2370 winnr = 0;
2371 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002372#endif
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002373 for (wp = firstwin; wp != win && wp != NULL;
2374 wp = wp->w_next)
2375 ++winnr;
2376 set_vim_var_nr(VV_MOUSE_WIN, winnr);
2377 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
2378 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
2379 set_vim_var_nr(VV_MOUSE_COL, col + 1);
2380 }
2381 }
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002382 }
2383}
2384
2385/*
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002386 * "getchar()" function
2387 */
2388 void
2389f_getchar(typval_T *argvars, typval_T *rettv)
2390{
2391 getchar_common(argvars, rettv);
2392}
2393
2394/*
2395 * "getcharstr()" function
2396 */
2397 void
2398f_getcharstr(typval_T *argvars, typval_T *rettv)
2399{
2400 getchar_common(argvars, rettv);
2401
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00002402 if (rettv->v_type != VAR_NUMBER)
2403 return;
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002404
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00002405 char_u temp[7]; // mbyte-char: 6, NUL: 1
2406 varnumber_T n = rettv->vval.v_number;
2407 int i = 0;
2408
2409 if (n != 0)
2410 {
2411 if (has_mbyte)
2412 i += (*mb_char2bytes)(n, temp + i);
2413 else
2414 temp[i++] = n;
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002415 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00002416 temp[i++] = NUL;
2417 rettv->v_type = VAR_STRING;
2418 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002419}
2420
2421/*
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002422 * "getcharmod()" function
2423 */
2424 void
2425f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
2426{
2427 rettv->vval.v_number = mod_mask;
2428}
2429#endif // FEAT_EVAL
2430
2431#if defined(MESSAGE_QUEUE) || defined(PROTO)
2432# define MAX_REPEAT_PARSE 8
2433
2434/*
2435 * Process messages that have been queued for netbeans or clientserver.
2436 * Also check if any jobs have ended.
h-east965d2ed2021-10-04 21:51:57 +01002437 * These functions can call arbitrary Vim script and should only be called when
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002438 * it is safe to do so.
2439 */
2440 void
2441parse_queued_messages(void)
2442{
Bram Moolenaar1cac7092019-10-22 21:54:31 +02002443 int old_curwin_id;
2444 int old_curbuf_fnum;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002445 int i;
2446 int save_may_garbage_collect = may_garbage_collect;
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002447 static int entered = 0;
Bram Moolenaard103ee72019-09-18 21:15:31 +02002448 int was_safe = get_was_safe_state();
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002449
2450 // Do not handle messages while redrawing, because it may cause buffers to
2451 // change or be wiped while they are being redrawn.
Bram Moolenaar4778b4d2020-11-04 11:03:12 +01002452 // Also bail out when parsing messages was explicitly disabled.
2453 if (updating_screen || dont_parse_messages)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002454 return;
2455
Bram Moolenaar1cac7092019-10-22 21:54:31 +02002456 // If memory allocation fails during startup we'll exit but curbuf or
2457 // curwin could be NULL.
2458 if (curbuf == NULL || curwin == NULL)
2459 return;
2460
2461 old_curbuf_fnum = curbuf->b_fnum;
2462 old_curwin_id = curwin->w_id;
2463
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002464 ++entered;
2465
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002466 // may_garbage_collect is set in main_loop() to do garbage collection when
2467 // blocking to wait on a character. We don't want that while parsing
2468 // messages, a callback may invoke vgetc() while lists and dicts are in use
2469 // in the call stack.
2470 may_garbage_collect = FALSE;
2471
2472 // Loop when a job ended, but don't keep looping forever.
2473 for (i = 0; i < MAX_REPEAT_PARSE; ++i)
2474 {
2475 // For Win32 mch_breakcheck() does not check for input, do it here.
Bram Moolenaar80a8d382020-05-03 22:57:32 +02002476# if (defined(MSWIN) || defined(__HAIKU__)) && defined(FEAT_JOB_CHANNEL)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002477 channel_handle_events(FALSE);
2478# endif
2479
2480# ifdef FEAT_NETBEANS_INTG
2481 // Process the queued netbeans messages.
2482 netbeans_parse_messages();
2483# endif
2484# ifdef FEAT_JOB_CHANNEL
2485 // Write any buffer lines still to be written.
2486 channel_write_any_lines();
2487
2488 // Process the messages queued on channels.
2489 channel_parse_messages();
2490# endif
2491# if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
2492 // Process the queued clientserver messages.
2493 server_parse_messages();
2494# endif
2495# ifdef FEAT_JOB_CHANNEL
2496 // Check if any jobs have ended. If so, repeat the above to handle
2497 // changes, e.g. stdin may have been closed.
2498 if (job_check_ended())
2499 continue;
2500# endif
2501# ifdef FEAT_TERMINAL
2502 free_unused_terminals();
2503# endif
Yee Cheng Chin4314e4f2022-10-08 13:50:05 +01002504
2505# ifdef FEAT_SOUND_MACOSX
2506 process_cfrunloop();
2507# endif
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002508# ifdef FEAT_SOUND_CANBERRA
2509 if (has_sound_callback_in_queue())
2510 invoke_sound_callback();
2511# endif
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002512#ifdef SIGUSR1
2513 if (got_sigusr1)
2514 {
2515 apply_autocmds(EVENT_SIGUSR1, NULL, NULL, FALSE, curbuf);
2516 got_sigusr1 = FALSE;
2517 }
2518#endif
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002519 break;
2520 }
2521
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002522 // When not nested we'll go back to waiting for a typed character. If it
2523 // was safe before then this triggers a SafeStateAgain autocommand event.
Bram Moolenaard103ee72019-09-18 21:15:31 +02002524 if (entered == 1 && was_safe)
Bram Moolenaar37d18072019-09-17 20:28:38 +02002525 may_trigger_safestateagain();
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002526
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002527 may_garbage_collect = save_may_garbage_collect;
2528
2529 // If the current window or buffer changed we need to bail out of the
2530 // waiting loop. E.g. when a job exit callback closes the terminal window.
2531 if (curwin->w_id != old_curwin_id || curbuf->b_fnum != old_curbuf_fnum)
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02002532 ins_char_typebuf(K_IGNORE, 0);
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002533
2534 --entered;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002535}
2536#endif
2537
2538
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002539typedef enum {
2540 map_result_fail, // failed, break loop
2541 map_result_get, // get a character from typeahead
2542 map_result_retry, // try to map again
2543 map_result_nomatch // no matching mapping, get char
2544} map_result_T;
2545
2546/*
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002547 * Check if the bytes at the start of the typeahead buffer are a character used
zeertzjqee446032022-04-30 15:02:22 +01002548 * in Insert mode completion. This includes the form with a CTRL modifier.
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002549 */
2550 static int
zeertzjqee446032022-04-30 15:02:22 +01002551at_ins_compl_key(void)
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002552{
2553 char_u *p = typebuf.tb_buf + typebuf.tb_off;
2554 int c = *p;
2555
2556 if (typebuf.tb_len > 3
Bram Moolenaard979d642022-03-04 14:51:06 +00002557 && (c == K_SPECIAL || c == CSI) // CSI is used by the GUI
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002558 && p[1] == KS_MODIFIER
2559 && (p[2] & MOD_MASK_CTRL))
2560 c = p[3] & 0x1f;
zeertzjqee446032022-04-30 15:02:22 +01002561 return (ctrl_x_mode_not_default() && vim_is_ctrl_x_key(c))
2562 || (compl_status_local() && (c == Ctrl_N || c == Ctrl_P));
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002563}
2564
2565/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002566 * Check if typebuf.tb_buf[] contains a modifier plus key that can be changed
Bram Moolenaar975a8802020-06-06 22:36:24 +02002567 * into just a key, apply that.
2568 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
2569 * + "max_offset"].
zeertzjq12e21e32022-04-27 11:58:01 +01002570 * Return the length of the replaced bytes, 0 if nothing changed, -1 for error.
Bram Moolenaar975a8802020-06-06 22:36:24 +02002571 */
2572 static int
2573check_simplify_modifier(int max_offset)
2574{
2575 int offset;
2576 char_u *tp;
2577
2578 for (offset = 0; offset < max_offset; ++offset)
2579 {
2580 if (offset + 3 >= typebuf.tb_len)
2581 break;
2582 tp = typebuf.tb_buf + typebuf.tb_off + offset;
zeertzjqc47b16a2022-09-05 13:05:29 +01002583 if ((tp[0] == K_SPECIAL || tp[0] == CSI) && tp[1] == KS_MODIFIER)
Bram Moolenaar975a8802020-06-06 22:36:24 +02002584 {
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002585 // A modifier was not used for a mapping, apply it to ASCII keys.
2586 // Shift would already have been applied.
Bram Moolenaar975a8802020-06-06 22:36:24 +02002587 int modifier = tp[2];
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002588 int c = tp[3];
2589 int new_c = merge_modifyOtherKeys(c, &modifier);
Bram Moolenaar975a8802020-06-06 22:36:24 +02002590
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002591 if (new_c != c)
Bram Moolenaar975a8802020-06-06 22:36:24 +02002592 {
2593 char_u new_string[MB_MAXBYTES];
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002594 int len;
Bram Moolenaar975a8802020-06-06 22:36:24 +02002595
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002596 if (offset == 0)
2597 {
2598 // At the start: remember the character and mod_mask before
2599 // merging, in some cases, e.g. at the hit-return prompt,
2600 // they are put back in the typeahead buffer.
2601 vgetc_char = c;
2602 vgetc_mod_mask = tp[2];
2603 }
zeertzjq12e21e32022-04-27 11:58:01 +01002604 if (IS_SPECIAL(new_c))
2605 {
2606 new_string[0] = K_SPECIAL;
2607 new_string[1] = K_SECOND(new_c);
2608 new_string[2] = K_THIRD(new_c);
2609 len = 3;
2610 }
2611 else
2612 len = mb_char2bytes(new_c, new_string);
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002613 if (modifier == 0)
2614 {
2615 if (put_string_in_typebuf(offset, 4, new_string, len,
zeertzjqfc78a032022-04-26 22:11:38 +01002616 NULL, 0, NULL) == FAIL)
RestorerZ68ebcee2023-05-31 17:12:14 +01002617 return -1;
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002618 }
2619 else
2620 {
2621 tp[2] = modifier;
2622 if (put_string_in_typebuf(offset + 3, 1, new_string, len,
zeertzjqfc78a032022-04-26 22:11:38 +01002623 NULL, 0, NULL) == FAIL)
RestorerZ68ebcee2023-05-31 17:12:14 +01002624 return -1;
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002625 }
Bram Moolenaar975a8802020-06-06 22:36:24 +02002626 return len;
2627 }
2628 }
2629 }
2630 return 0;
2631}
2632
2633/*
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00002634 * Return TRUE if the terminal sends modifiers with various keys. This is when
2635 * modifyOtherKeys level 2 is enabled or the kitty keyboard protocol is
2636 * enabled.
2637 */
David Leadbeater67ec6552023-10-26 22:00:34 +02002638 int
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00002639key_protocol_enabled(void)
2640{
Bram Moolenaarc255b782022-11-26 19:16:48 +00002641 // If xterm has responded to XTQMODKEYS it overrules seenModifyOtherKeys.
2642 int using_mok = modify_otherkeys_state != MOKS_INITIAL
2643 ? modify_otherkeys_state == MOKS_ENABLED
2644 : seenModifyOtherKeys;
2645 return using_mok || kitty_protocol_state == KKPS_ENABLED;
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00002646}
2647
2648/*
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002649 * Handle mappings in the typeahead buffer.
2650 * - When something was mapped, return map_result_retry for recursive mappings.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002651 * - When nothing mapped and typeahead has a character: return map_result_get.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002652 * - When there is no match yet, return map_result_nomatch, need to get more
2653 * typeahead.
zeertzjq12e21e32022-04-27 11:58:01 +01002654 * - On failure (out of memory) return map_result_fail.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002655 */
2656 static int
2657handle_mapping(
2658 int *keylenp,
2659 int *timedout,
2660 int *mapdepth)
2661{
2662 mapblock_T *mp = NULL;
2663 mapblock_T *mp2;
2664 mapblock_T *mp_match;
2665 int mp_match_len = 0;
2666 int max_mlen = 0;
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002667 int want_termcode = 0; // 1 if termcode expected after max_mlen
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002668 int tb_c1;
2669 int mlen;
2670#ifdef FEAT_LANGMAP
2671 int nolmaplen;
2672#endif
2673 int keylen = *keylenp;
2674 int i;
2675 int local_State = get_real_state();
Bram Moolenaar1fc34222022-03-03 13:56:24 +00002676 int is_plug_map = FALSE;
2677
zeertzjqbb404f52022-07-23 06:25:29 +01002678 // If typeahead starts with <Plug> then remap, even for a "noremap" mapping.
Bram Moolenaaraf043e12022-07-02 12:08:16 +01002679 if (typebuf.tb_len >= 3
2680 && typebuf.tb_buf[typebuf.tb_off] == K_SPECIAL
Bram Moolenaar1fc34222022-03-03 13:56:24 +00002681 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_EXTRA
2682 && typebuf.tb_buf[typebuf.tb_off + 2] == KE_PLUG)
2683 is_plug_map = TRUE;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002684
2685 /*
2686 * Check for a mappable key sequence.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002687 * Walk through one maphash[] list until we find an entry that matches.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002688 *
2689 * Don't look for mappings if:
2690 * - no_mapping set: mapping disabled (e.g. for CTRL-V)
2691 * - maphash_valid not set: no mappings present.
2692 * - typebuf.tb_buf[typebuf.tb_off] should not be remapped
2693 * - in insert or cmdline mode and 'paste' option set
Bram Moolenaareda35f72019-08-03 14:59:44 +02002694 * - waiting for "hit return to continue" and CR or SPACE typed
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002695 * - waiting for a char with --more--
2696 * - in Ctrl-X mode, and we get a valid char for that mode
2697 */
2698 tb_c1 = typebuf.tb_buf[typebuf.tb_off];
2699 if (no_mapping == 0 && is_maphash_valid()
2700 && (no_zero_mapping == 0 || tb_c1 != '0')
Bram Moolenaar1fc34222022-03-03 13:56:24 +00002701 && (typebuf.tb_maplen == 0 || is_plug_map
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002702 || (p_remap
2703 && (typebuf.tb_noremap[typebuf.tb_off]
2704 & (RM_NONE|RM_ABBR)) == 0))
Bram Moolenaar24959102022-05-07 20:01:16 +01002705 && !(p_paste && (State & (MODE_INSERT | MODE_CMDLINE)))
2706 && !(State == MODE_HITRETURN && (tb_c1 == CAR || tb_c1 == ' '))
2707 && State != MODE_ASKMORE
2708 && State != MODE_CONFIRM
zeertzjqee446032022-04-30 15:02:22 +01002709 && !at_ins_compl_key())
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002710 {
Christopher Plewright03193062022-11-22 12:58:27 +00002711#ifdef FEAT_GUI
2712 if (gui.in_use && tb_c1 == CSI && typebuf.tb_len >= 2
2713 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_MODIFIER)
Bram Moolenaarc9983702020-05-28 21:03:53 +02002714 {
2715 // The GUI code sends CSI KS_MODIFIER {flags}, but mappings expect
2716 // K_SPECIAL KS_MODIFIER {flags}.
2717 tb_c1 = K_SPECIAL;
2718 }
2719#endif
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002720#ifdef FEAT_LANGMAP
2721 if (tb_c1 == K_SPECIAL)
2722 nolmaplen = 2;
2723 else
2724 {
Bram Moolenaar24959102022-05-07 20:01:16 +01002725 LANGMAP_ADJUST(tb_c1, (State & (MODE_CMDLINE | MODE_INSERT)) == 0
2726 && get_real_state() != MODE_SELECT);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002727 nolmaplen = 0;
2728 }
2729#endif
2730 // First try buffer-local mappings.
2731 mp = get_buf_maphash_list(local_State, tb_c1);
2732 mp2 = get_maphash_list(local_State, tb_c1);
2733 if (mp == NULL)
2734 {
2735 // There are no buffer-local mappings.
2736 mp = mp2;
2737 mp2 = NULL;
2738 }
Bram Moolenaareda35f72019-08-03 14:59:44 +02002739
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002740 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002741 * Loop until a partly matching mapping is found or all (local)
2742 * mappings have been checked.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002743 * The longest full match is remembered in "mp_match".
Bram Moolenaareda35f72019-08-03 14:59:44 +02002744 * A full match is only accepted if there is no partly match, so "aa"
2745 * and "aaa" can both be mapped.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002746 */
2747 mp_match = NULL;
2748 mp_match_len = 0;
2749 for ( ; mp != NULL;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002750 mp->m_next == NULL ? (mp = mp2, mp2 = NULL) : (mp = mp->m_next))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002751 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002752 // Only consider an entry if the first character matches and it is
2753 // for the current state.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002754 // Skip ":lmap" mappings if keys were mapped.
2755 if (mp->m_keys[0] == tb_c1
2756 && (mp->m_mode & local_State)
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00002757 && !(mp->m_simplified && key_protocol_enabled()
Bram Moolenaar46cd43b2020-06-04 22:22:11 +02002758 && typebuf.tb_maplen == 0)
Christopher Plewright4c366782022-10-20 13:11:15 +01002759 && ((mp->m_mode & MODE_LANGMAP) == 0
2760 || typebuf.tb_maplen == 0))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002761 {
2762#ifdef FEAT_LANGMAP
2763 int nomap = nolmaplen;
zeertzjq49660f52022-10-20 17:59:38 +01002764 int modifiers = 0;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002765#endif
2766 // find the match length of this mapping
2767 for (mlen = 1; mlen < typebuf.tb_len; ++mlen)
2768 {
zeertzjq49660f52022-10-20 17:59:38 +01002769 int c2 = typebuf.tb_buf[typebuf.tb_off + mlen];
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002770#ifdef FEAT_LANGMAP
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002771 if (nomap > 0)
zeertzjq49660f52022-10-20 17:59:38 +01002772 {
2773 if (nomap == 2 && c2 == KS_MODIFIER)
2774 modifiers = 1;
2775 else if (nomap == 1 && modifiers == 1)
2776 modifiers = c2;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002777 --nomap;
zeertzjq49660f52022-10-20 17:59:38 +01002778 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002779 else
zeertzjq49660f52022-10-20 17:59:38 +01002780 {
2781 if (c2 == K_SPECIAL)
2782 nomap = 2;
2783 else if (merge_modifyOtherKeys(c2, &modifiers) == c2)
2784 // Only apply 'langmap' if merging modifiers into
2785 // the key will not result in another character,
2786 // so that 'langmap' behaves consistently in
2787 // different terminals and GUIs.
2788 LANGMAP_ADJUST(c2, TRUE);
2789 modifiers = 0;
2790 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002791#endif
zeertzjq49660f52022-10-20 17:59:38 +01002792 if (mp->m_keys[mlen] != c2)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002793 break;
2794 }
2795
Bram Moolenaareda35f72019-08-03 14:59:44 +02002796 // Don't allow mapping the first byte(s) of a multi-byte char.
2797 // Happens when mapping <M-a> and then changing 'encoding'.
2798 // Beware that 0x80 is escaped.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002799 {
2800 char_u *p1 = mp->m_keys;
2801 char_u *p2 = mb_unescape(&p1);
2802
2803 if (has_mbyte && p2 != NULL
Bram Moolenaar1614a142019-10-06 22:00:13 +02002804 && MB_BYTE2LEN(tb_c1) > mb_ptr2len(p2))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002805 mlen = 0;
2806 }
2807
2808 // Check an entry whether it matches.
2809 // - Full match: mlen == keylen
2810 // - Partly match: mlen == typebuf.tb_len
2811 keylen = mp->m_keylen;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002812 if (mlen == keylen || (mlen == typebuf.tb_len
2813 && typebuf.tb_len < keylen))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002814 {
2815 char_u *s;
2816 int n;
2817
Bram Moolenaareda35f72019-08-03 14:59:44 +02002818 // If only script-local mappings are allowed, check if the
2819 // mapping starts with K_SNR.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002820 s = typebuf.tb_noremap + typebuf.tb_off;
2821 if (*s == RM_SCRIPT
2822 && (mp->m_keys[0] != K_SPECIAL
2823 || mp->m_keys[1] != KS_EXTRA
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01002824 || mp->m_keys[2] != KE_SNR))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002825 continue;
2826
Bram Moolenaareda35f72019-08-03 14:59:44 +02002827 // If one of the typed keys cannot be remapped, skip the
2828 // entry.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002829 for (n = mlen; --n >= 0; )
2830 if (*s++ & (RM_NONE|RM_ABBR))
2831 break;
Bram Moolenaar1fc34222022-03-03 13:56:24 +00002832 if (!is_plug_map && n >= 0)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002833 continue;
2834
2835 if (keylen > typebuf.tb_len)
2836 {
2837 if (!*timedout && !(mp_match != NULL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002838 && mp_match->m_nowait))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002839 {
2840 // break at a partly match
2841 keylen = KEYLEN_PART_MAP;
2842 break;
2843 }
2844 }
2845 else if (keylen > mp_match_len)
2846 {
2847 // found a longer match
2848 mp_match = mp;
2849 mp_match_len = keylen;
2850 }
2851 }
2852 else
Bram Moolenaareda35f72019-08-03 14:59:44 +02002853 // No match; may have to check for termcode at next
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002854 // character. If the first character that didn't match is
2855 // K_SPECIAL then check for a termcode. This isn't perfect
2856 // but should work in most cases.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002857 if (max_mlen < mlen)
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002858 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002859 max_mlen = mlen;
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002860 want_termcode = mp->m_keys[mlen] == K_SPECIAL;
2861 }
2862 else if (max_mlen == mlen && mp->m_keys[mlen] == K_SPECIAL)
2863 want_termcode = 1;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002864 }
2865 }
2866
Bram Moolenaareda35f72019-08-03 14:59:44 +02002867 // If no partly match found, use the longest full match.
Bram Moolenaar196c3852022-03-04 19:22:36 +00002868 if (keylen != KEYLEN_PART_MAP && mp_match != NULL)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002869 {
2870 mp = mp_match;
2871 keylen = mp_match_len;
2872 }
2873 }
2874
2875 /*
2876 * Check for match with 'pastetoggle'
2877 */
Bram Moolenaar24959102022-05-07 20:01:16 +01002878 if (*p_pt != NUL && mp == NULL && (State & (MODE_INSERT | MODE_NORMAL)))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002879 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002880 for (mlen = 0; mlen < typebuf.tb_len && p_pt[mlen]; ++mlen)
2881 if (p_pt[mlen] != typebuf.tb_buf[typebuf.tb_off + mlen])
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002882 break;
2883 if (p_pt[mlen] == NUL) // match
2884 {
2885 // write chars to script file(s)
2886 if (mlen > typebuf.tb_maplen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002887 gotchars(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_maplen,
2888 mlen - typebuf.tb_maplen);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002889
2890 del_typebuf(mlen, 0); // remove the chars
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002891 set_option_value_give_err((char_u *)"paste",
2892 (long)!p_paste, NULL, 0);
Bram Moolenaar24959102022-05-07 20:01:16 +01002893 if (!(State & MODE_INSERT))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002894 {
2895 msg_col = 0;
2896 msg_row = Rows - 1;
2897 msg_clr_eos(); // clear ruler
2898 }
2899 status_redraw_all();
2900 redraw_statuslines();
2901 showmode();
2902 setcursor();
2903 *keylenp = keylen;
2904 return map_result_retry;
2905 }
2906 // Need more chars for partly match.
2907 if (mlen == typebuf.tb_len)
2908 keylen = KEYLEN_PART_KEY;
2909 else if (max_mlen < mlen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002910 // no match, may have to check for termcode at next character
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002911 max_mlen = mlen + 1;
2912 }
2913
Bram Moolenaarbbf84e22022-03-12 13:48:39 +00002914 // May check for a terminal code when there is no mapping or only a partial
2915 // mapping. Also check if there is a full mapping with <Esc>, unless timed
2916 // out, since that is nearly always a partial match with a terminal code.
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002917 if ((mp == NULL || max_mlen + want_termcode > mp_match_len
Bram Moolenaarbbf84e22022-03-12 13:48:39 +00002918 || (mp_match_len == 1 && *mp->m_keys == ESC && !*timedout))
2919 && keylen != KEYLEN_PART_MAP)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002920 {
2921 int save_keylen = keylen;
2922
2923 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002924 * When no matching mapping found or found a non-matching mapping that
2925 * matches at least what the matching mapping matched:
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002926 * Check if we have a terminal code, when:
Bram Moolenaareda35f72019-08-03 14:59:44 +02002927 * - mapping is allowed,
2928 * - keys have not been mapped,
2929 * - and not an ESC sequence, not in insert mode or p_ek is on,
2930 * - and when not timed out,
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002931 */
zeertzjq68a573c2022-04-28 14:10:01 +01002932 if (no_mapping == 0 || allow_keys != 0)
2933 {
2934 if ((typebuf.tb_maplen == 0
RestorerZ68ebcee2023-05-31 17:12:14 +01002935 || (p_remap && typebuf.tb_noremap[
Bram Moolenaareda35f72019-08-03 14:59:44 +02002936 typebuf.tb_off] == RM_YES))
RestorerZ68ebcee2023-05-31 17:12:14 +01002937 && !*timedout)
zeertzjq68a573c2022-04-28 14:10:01 +01002938 keylen = check_termcode(max_mlen + 1, NULL, 0, NULL);
2939 else
2940 keylen = 0;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002941
Bram Moolenaar0684e362020-12-03 19:54:42 +01002942 // If no termcode matched but 'pastetoggle' matched partially
2943 // it's like an incomplete key sequence.
zeertzjq68a573c2022-04-28 14:10:01 +01002944 if (keylen == 0 && save_keylen == KEYLEN_PART_KEY && !*timedout)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002945 keylen = KEYLEN_PART_KEY;
2946
Bram Moolenaar975a8802020-06-06 22:36:24 +02002947 // If no termcode matched, try to include the modifier into the
zeertzjqa3f83fe2021-11-22 12:47:39 +00002948 // key. This is for when modifyOtherKeys is working.
Bram Moolenaarc896adb2022-11-19 19:02:40 +00002949#ifdef FEAT_TERMINAL
2950 check_no_reduce_keys(); // may update the no_reduce_keys flag
2951#endif
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002952 if (keylen == 0 && !no_reduce_keys)
zeertzjq12e21e32022-04-27 11:58:01 +01002953 {
Bram Moolenaar975a8802020-06-06 22:36:24 +02002954 keylen = check_simplify_modifier(max_mlen + 1);
zeertzjq12e21e32022-04-27 11:58:01 +01002955 if (keylen < 0)
2956 // ins_typebuf() failed
2957 return map_result_fail;
2958 }
Bram Moolenaar975a8802020-06-06 22:36:24 +02002959
Bram Moolenaareda35f72019-08-03 14:59:44 +02002960 // When getting a partial match, but the last characters were not
2961 // typed, don't wait for a typed character to complete the
2962 // termcode. This helps a lot when a ":normal" command ends in an
2963 // ESC.
2964 if (keylen < 0 && typebuf.tb_len == typebuf.tb_maplen)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002965 keylen = 0;
2966 }
2967 else
2968 keylen = 0;
2969 if (keylen == 0) // no matching terminal code
2970 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002971#ifdef AMIGA
2972 // check for window bounds report
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002973 if (typebuf.tb_maplen == 0 && (typebuf.tb_buf[
Bram Moolenaareda35f72019-08-03 14:59:44 +02002974 typebuf.tb_off] & 0xff) == CSI)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002975 {
2976 char_u *s;
2977
2978 for (s = typebuf.tb_buf + typebuf.tb_off + 1;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002979 s < typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len
2980 && (VIM_ISDIGIT(*s) || *s == ';' || *s == ' ');
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002981 ++s)
2982 ;
2983 if (*s == 'r' || *s == '|') // found one
2984 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002985 del_typebuf(
2986 (int)(s + 1 - (typebuf.tb_buf + typebuf.tb_off)), 0);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002987 // get size and redraw screen
2988 shell_resized();
2989 *keylenp = keylen;
2990 return map_result_retry;
2991 }
2992 if (*s == NUL) // need more characters
2993 keylen = KEYLEN_PART_KEY;
2994 }
2995 if (keylen >= 0)
2996#endif
Bram Moolenaareda35f72019-08-03 14:59:44 +02002997 // When there was a matching mapping and no termcode could be
2998 // replaced after another one, use that mapping (loop around).
2999 // If there was no mapping at all use the character from the
3000 // typeahead buffer right here.
3001 if (mp == NULL)
3002 {
3003 *keylenp = keylen;
Bram Moolenaar52797ba2021-12-16 14:45:13 +00003004 return map_result_get; // get character from typeahead
Bram Moolenaareda35f72019-08-03 14:59:44 +02003005 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003006 }
3007
3008 if (keylen > 0) // full matching terminal code
3009 {
3010#if defined(FEAT_GUI) && defined(FEAT_MENU)
3011 if (typebuf.tb_len >= 2
Bram Moolenaareda35f72019-08-03 14:59:44 +02003012 && typebuf.tb_buf[typebuf.tb_off] == K_SPECIAL
3013 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_MENU)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003014 {
3015 int idx;
3016
Bram Moolenaareda35f72019-08-03 14:59:44 +02003017 // Using a menu may cause a break in undo! It's like using
3018 // gotchars(), but without recording or writing to a script
3019 // file.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003020 may_sync_undo();
3021 del_typebuf(3, 0);
3022 idx = get_menu_index(current_menu, local_State);
3023 if (idx != MENU_INDEX_INVALID)
3024 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02003025 // In Select mode and a Visual mode menu is used: Switch
3026 // to Visual mode temporarily. Append K_SELECT to switch
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003027 // back to Select mode.
3028 if (VIsual_active && VIsual_select
Bram Moolenaar24959102022-05-07 20:01:16 +01003029 && (current_menu->modes & MODE_VISUAL))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003030 {
3031 VIsual_select = FALSE;
3032 (void)ins_typebuf(K_SELECT_STRING,
Bram Moolenaareda35f72019-08-03 14:59:44 +02003033 REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003034 }
3035 ins_typebuf(current_menu->strings[idx],
3036 current_menu->noremap[idx],
Bram Moolenaareda35f72019-08-03 14:59:44 +02003037 0, TRUE, current_menu->silent[idx]);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003038 }
3039 }
3040#endif // FEAT_GUI && FEAT_MENU
3041 *keylenp = keylen;
3042 return map_result_retry; // try mapping again
3043 }
3044
Bram Moolenaareda35f72019-08-03 14:59:44 +02003045 // Partial match: get some more characters. When a matching mapping
3046 // was found use that one.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003047 if (mp == NULL || keylen < 0)
3048 keylen = KEYLEN_PART_KEY;
3049 else
3050 keylen = mp_match_len;
3051 }
3052
3053 /*
3054 * complete match
3055 */
3056 if (keylen >= 0 && keylen <= typebuf.tb_len)
3057 {
3058 char_u *map_str;
3059
3060#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02003061 int save_m_expr;
3062 int save_m_noremap;
3063 int save_m_silent;
3064 char_u *save_m_keys;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003065#else
3066# define save_m_noremap mp->m_noremap
3067# define save_m_silent mp->m_silent
3068#endif
3069
3070 // write chars to script file(s)
3071 if (keylen > typebuf.tb_maplen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02003072 gotchars(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_maplen,
3073 keylen - typebuf.tb_maplen);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003074
3075 cmd_silent = (typebuf.tb_silent > 0);
3076 del_typebuf(keylen, 0); // remove the mapped keys
3077
3078 /*
3079 * Put the replacement string in front of mapstr.
3080 * The depth check catches ":map x y" and ":map y x".
3081 */
3082 if (++*mapdepth >= p_mmd)
3083 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00003084 emsg(_(e_recursive_mapping));
Bram Moolenaar24959102022-05-07 20:01:16 +01003085 if (State & MODE_CMDLINE)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003086 redrawcmdline();
3087 else
3088 setcursor();
3089 flush_buffers(FLUSH_MINIMAL);
Bram Moolenaar30613902019-12-01 22:11:18 +01003090 *mapdepth = 0; // for next one
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003091 *keylenp = keylen;
3092 return map_result_fail;
3093 }
3094
3095 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02003096 * In Select mode and a Visual mode mapping is used: Switch to Visual
3097 * mode temporarily. Append K_SELECT to switch back to Select mode.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003098 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003099 if (VIsual_active && VIsual_select && (mp->m_mode & MODE_VISUAL))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003100 {
3101 VIsual_select = FALSE;
Bram Moolenaareda35f72019-08-03 14:59:44 +02003102 (void)ins_typebuf(K_SELECT_STRING, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003103 }
3104
3105#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02003106 // Copy the values from *mp that are used, because evaluating the
3107 // expression may invoke a function that redefines the mapping, thereby
3108 // making *mp invalid.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003109 save_m_expr = mp->m_expr;
3110 save_m_noremap = mp->m_noremap;
3111 save_m_silent = mp->m_silent;
3112 save_m_keys = NULL; // only saved when needed
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003113
3114 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02003115 * Handle ":map <expr>": evaluate the {rhs} as an expression. Also
3116 * save and restore the command line for "normal :".
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003117 */
3118 if (mp->m_expr)
3119 {
3120 int save_vgetc_busy = vgetc_busy;
3121 int save_may_garbage_collect = may_garbage_collect;
Bram Moolenaar4ebe0e62019-11-22 20:55:40 +01003122 int was_screen_col = screen_cur_col;
3123 int was_screen_row = screen_cur_row;
Bram Moolenaar74a0a5b2022-02-10 14:07:41 +00003124 int prev_did_emsg = did_emsg;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003125
3126 vgetc_busy = 0;
3127 may_garbage_collect = FALSE;
3128
3129 save_m_keys = vim_strsave(mp->m_keys);
Bram Moolenaar19db9e62022-01-11 11:58:19 +00003130 map_str = eval_map_expr(mp, NUL);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003131
Bram Moolenaar4ebe0e62019-11-22 20:55:40 +01003132 // The mapping may do anything, but we expect it to take care of
3133 // redrawing. Do put the cursor back where it was.
3134 windgoto(was_screen_row, was_screen_col);
3135 out_flush();
3136
Bram Moolenaar74a0a5b2022-02-10 14:07:41 +00003137 // If an error was displayed and the expression returns an empty
3138 // string, generate a <Nop> to allow for a redraw.
3139 if (prev_did_emsg != did_emsg
3140 && (map_str == NULL || *map_str == NUL))
3141 {
3142 char_u buf[4];
3143
3144 vim_free(map_str);
3145 buf[0] = K_SPECIAL;
3146 buf[1] = KS_EXTRA;
3147 buf[2] = KE_IGNORE;
3148 buf[3] = NUL;
3149 map_str = vim_strsave(buf);
Bram Moolenaar24959102022-05-07 20:01:16 +01003150 if (State & MODE_CMDLINE)
Bram Moolenaar74a0a5b2022-02-10 14:07:41 +00003151 {
3152 // redraw the command below the error
3153 msg_didout = TRUE;
3154 if (msg_row < cmdline_row)
3155 msg_row = cmdline_row;
3156 redrawcmd();
3157 }
3158 }
3159
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003160 vgetc_busy = save_vgetc_busy;
3161 may_garbage_collect = save_may_garbage_collect;
3162 }
3163 else
3164#endif
3165 map_str = mp->m_str;
3166
3167 /*
3168 * Insert the 'to' part in the typebuf.tb_buf.
Bram Moolenaareda35f72019-08-03 14:59:44 +02003169 * If 'from' field is the same as the start of the 'to' field, don't
3170 * remap the first character (but do allow abbreviations).
3171 * If m_noremap is set, don't remap the whole 'to' part.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003172 */
3173 if (map_str == NULL)
3174 i = FAIL;
3175 else
3176 {
3177 int noremap;
3178
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01003179#ifdef FEAT_EVAL
3180 last_used_map = mp;
3181 last_used_sid = -1;
3182#endif
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003183 if (save_m_noremap != REMAP_YES)
3184 noremap = save_m_noremap;
3185 else if (
3186#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02003187 STRNCMP(map_str, save_m_keys != NULL ? save_m_keys : mp->m_keys,
3188 (size_t)keylen)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003189#else
3190 STRNCMP(map_str, mp->m_keys, (size_t)keylen)
3191#endif
3192 != 0)
3193 noremap = REMAP_YES;
3194 else
3195 noremap = REMAP_SKIP;
3196 i = ins_typebuf(map_str, noremap,
3197 0, TRUE, cmd_silent || save_m_silent);
3198#ifdef FEAT_EVAL
3199 if (save_m_expr)
3200 vim_free(map_str);
3201#endif
3202 }
3203#ifdef FEAT_EVAL
3204 vim_free(save_m_keys);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003205#endif
3206 *keylenp = keylen;
3207 if (i == FAIL)
3208 return map_result_fail;
3209 return map_result_retry;
3210 }
3211
3212 *keylenp = keylen;
3213 return map_result_nomatch;
3214}
3215
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003216/*
3217 * unget one character (can only be done once!)
zeertzjq6d4e7252022-04-07 13:58:04 +01003218 * If the character was stuffed, vgetc() will get it next time it is called.
zeertzjq0f68e6c2022-04-05 13:17:01 +01003219 * Otherwise vgetc() will only get it when the stuff buffer is empty.
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003220 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003222vungetc(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223{
3224 old_char = c;
3225 old_mod_mask = mod_mask;
Bram Moolenaarb8978712013-03-16 21:42:16 +01003226 old_mouse_row = mouse_row;
3227 old_mouse_col = mouse_col;
zeertzjq0f68e6c2022-04-05 13:17:01 +01003228 old_KeyStuffed = KeyStuffed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229}
3230
3231/*
zeertzjq6d4e7252022-04-07 13:58:04 +01003232 * When peeking and not getting a character, reg_executing cannot be cleared
3233 * yet, so set a flag to clear it later.
3234 */
3235 static void
3236check_end_reg_executing(int advance)
3237{
3238 if (reg_executing != 0 && (typebuf.tb_maplen == 0
3239 || pending_end_reg_executing))
3240 {
3241 if (advance)
3242 {
3243 reg_executing = 0;
3244 pending_end_reg_executing = FALSE;
3245 }
3246 else
3247 pending_end_reg_executing = TRUE;
3248 }
3249
3250}
3251
3252/*
Bram Moolenaar32526b32019-01-19 17:43:09 +01003253 * Get a byte:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 * 1. from the stuffbuffer
3255 * This is used for abbreviated commands like "D" -> "d$".
3256 * Also used to redo a command for ".".
3257 * 2. from the typeahead buffer
3258 * Stores text obtained previously but not used yet.
3259 * Also stores the result of mappings.
3260 * Also used for the ":normal" command.
3261 * 3. from the user
3262 * This may do a blocking wait if "advance" is TRUE.
3263 *
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003264 * if "advance" is TRUE (vgetc()):
Bram Moolenaard29459b2016-08-26 22:29:11 +02003265 * Really get the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 * KeyTyped is set to TRUE in the case the user typed the key.
3267 * KeyStuffed is TRUE if the character comes from the stuff buffer.
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003268 * if "advance" is FALSE (vpeekc()):
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02003269 * Just look whether there is a character available.
3270 * Return NUL if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 *
3272 * When "no_mapping" is zero, checks for mappings in the current mode.
3273 * Only returns one byte (of a multi-byte character).
3274 * K_SPECIAL and CSI may be escaped, need to get two more bytes then.
3275 */
3276 static int
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003277vgetorpeek(int advance)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278{
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003279 int c;
Bram Moolenaarb8329db2022-07-06 13:31:28 +01003280 int timedout = FALSE; // waited for more than 'timeoutlen'
3281 // for mapping to complete or
3282 // 'ttimeoutlen' for complete key code
Bram Moolenaar30613902019-12-01 22:11:18 +01003283 int mapdepth = 0; // check for recursive mapping
3284 int mode_deleted = FALSE; // set when mode has been deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 int new_wcol, new_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003287 int shape_changed = FALSE; // adjusted cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288#endif
3289 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 int old_wcol, old_wrow;
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003291 int wait_tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292
3293 /*
3294 * This function doesn't work very well when called recursively. This may
3295 * happen though, because of:
3296 * 1. The call to add_to_showcmd(). char_avail() is then used to check if
3297 * there is a character available, which calls this function. In that
3298 * case we must return NUL, to indicate no character is available.
3299 * 2. A GUI callback function writes to the screen, causing a
3300 * wait_return().
3301 * Using ":normal" can also do this, but it saves the typeahead buffer,
3302 * thus it should be OK. But don't get a key from the user then.
3303 */
Bram Moolenaare2c38102016-01-31 14:55:40 +01003304 if (vgetc_busy > 0 && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 return NUL;
3306
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003307 ++vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308
3309 if (advance)
Bram Moolenaar8d696372022-08-21 10:40:07 +01003310 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 KeyStuffed = FALSE;
Bram Moolenaar8d696372022-08-21 10:40:07 +01003312 typebuf_was_empty = FALSE;
3313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314
3315 init_typebuf();
3316 start_stuff();
zeertzjq6d4e7252022-04-07 13:58:04 +01003317 check_end_reg_executing(advance);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 do
3319 {
3320/*
3321 * get a character: 1. from the stuffbuffer
3322 */
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003323 if (typeahead_char != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 {
3325 c = typeahead_char;
3326 if (advance)
3327 typeahead_char = 0;
3328 }
3329 else
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01003330 c = read_readbuffers(advance);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 if (c != NUL && !got_int)
3332 {
3333 if (advance)
3334 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003335 // KeyTyped = FALSE; When the command that stuffed something
3336 // was typed, behave like the stuffed command was typed.
3337 // needed for CTRL-W CTRL-] to open a fold, for example.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 KeyStuffed = TRUE;
3339 }
3340 if (typebuf.tb_no_abbr_cnt == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003341 typebuf.tb_no_abbr_cnt = 1; // no abbreviations now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 }
3343 else
3344 {
3345 /*
3346 * Loop until we either find a matching mapped key, or we
3347 * are sure that it is not a mapped key.
3348 * If a mapped key sequence is found we go back to the start to
3349 * try re-mapping.
3350 */
3351 for (;;)
3352 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003353 long wait_time;
3354 int keylen = 0;
Bram Moolenaareda35f72019-08-03 14:59:44 +02003355 int showcmd_idx;
zeertzjq6d4e7252022-04-07 13:58:04 +01003356 check_end_reg_executing(advance);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 /*
3358 * ui_breakcheck() is slow, don't use it too often when
3359 * inside a mapping. But call it each time for typed
3360 * characters.
3361 */
3362 if (typebuf.tb_maplen)
3363 line_breakcheck();
3364 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003365 ui_breakcheck(); // check for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 if (got_int)
3367 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003368 // flush all input
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003369 c = inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 0L);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003370
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 /*
3372 * If inchar() returns TRUE (script file was active) or we
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003373 * are inside a mapping, get out of Insert mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 * Otherwise we behave like having gotten a CTRL-C.
3375 * As a result typing CTRL-C in insert mode will
3376 * really insert a CTRL-C.
3377 */
3378 if ((c || typebuf.tb_maplen)
Bram Moolenaar24959102022-05-07 20:01:16 +01003379 && (State & (MODE_INSERT | MODE_CMDLINE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 c = ESC;
3381 else
3382 c = Ctrl_C;
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02003383 flush_buffers(FLUSH_INPUT); // flush all typeahead
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384
Bram Moolenaard9dfd572006-10-03 13:36:13 +00003385 if (advance)
3386 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003387 // Also record this character, it might be needed to
3388 // get out of Insert mode.
Bram Moolenaard9dfd572006-10-03 13:36:13 +00003389 *typebuf.tb_buf = c;
3390 gotchars(typebuf.tb_buf, 1);
3391 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 cmd_silent = FALSE;
3393
3394 break;
3395 }
3396 else if (typebuf.tb_len > 0)
3397 {
3398 /*
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003399 * Check for a mapping in "typebuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 */
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003401 map_result_T result = handle_mapping(
3402 &keylen, &timedout, &mapdepth);
3403
3404 if (result == map_result_retry)
3405 // try mapping again
3406 continue;
3407 if (result == map_result_fail)
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003408 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003409 // failed, use the outer loop
3410 c = -1;
3411 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003413 if (result == map_result_get)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415/*
3416 * get a character: 2. from the typeahead buffer
3417 */
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003418 c = typebuf.tb_buf[typebuf.tb_off];
RestorerZ68ebcee2023-05-31 17:12:14 +01003419 if (advance) // remove chars from typebuf
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003420 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003421 cmd_silent = (typebuf.tb_silent > 0);
3422 if (typebuf.tb_maplen > 0)
3423 KeyTyped = FALSE;
3424 else
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003425 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003426 KeyTyped = TRUE;
Bram Moolenaar30613902019-12-01 22:11:18 +01003427 // write char to script file(s)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003428 gotchars(typebuf.tb_buf
3429 + typebuf.tb_off, 1);
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003430 }
RestorerZ68ebcee2023-05-31 17:12:14 +01003431 KeyNoremap = typebuf.tb_noremap[typebuf.tb_off];
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003432 del_typebuf(1, 0);
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003433 }
Bram Moolenaar52797ba2021-12-16 14:45:13 +00003434 break; // got character, break the for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 }
3436
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003437 // not enough characters, get more
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 }
3439
3440/*
3441 * get a character: 3. from the user - handle <Esc> in Insert mode
3442 */
3443 /*
Bram Moolenaard330e842022-11-24 20:23:24 +00003444 * Special case: if we get an <ESC> in Insert mode and there
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 * are no more characters at once, we pretend to go out of
Bram Moolenaard330e842022-11-24 20:23:24 +00003446 * Insert mode. This prevents the one second delay after
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 * typing an <ESC>. If we get something after all, we may
3448 * have to redisplay the mode. That the cursor is in the wrong
3449 * place does not matter.
Bram Moolenaard330e842022-11-24 20:23:24 +00003450 * Do not do this if the kitty keyboard protocol is used, every
3451 * <ESC> is the start of an escape sequence then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 */
3453 c = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 new_wcol = curwin->w_wcol;
3455 new_wrow = curwin->w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 if ( advance
3457 && typebuf.tb_len == 1
3458 && typebuf.tb_buf[typebuf.tb_off] == ESC
3459 && !no_mapping
Bram Moolenaard330e842022-11-24 20:23:24 +00003460 && kitty_protocol_state != KKPS_ENABLED
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 && ex_normal_busy == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 && typebuf.tb_maplen == 0
Bram Moolenaar24959102022-05-07 20:01:16 +01003463 && (State & MODE_INSERT)
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003464 && (p_timeout
3465 || (keylen == KEYLEN_PART_KEY && p_ttimeout))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 && (c = inchar(typebuf.tb_buf + typebuf.tb_off
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003467 + typebuf.tb_len, 3, 25L)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003469 colnr_T col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 char_u *ptr;
3471
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003472 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 {
3474 unshowmode(TRUE);
3475 mode_deleted = TRUE;
3476 }
3477#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003478 // may show a different cursor shape
Bram Moolenaar24959102022-05-07 20:01:16 +01003479 if (gui.in_use && State != MODE_NORMAL && !cmd_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 {
3481 int save_State;
3482
3483 save_State = State;
Bram Moolenaar24959102022-05-07 20:01:16 +01003484 State = MODE_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 gui_update_cursor(TRUE, FALSE);
3486 State = save_State;
3487 shape_changed = TRUE;
3488 }
3489#endif
3490 validate_cursor();
3491 old_wcol = curwin->w_wcol;
3492 old_wrow = curwin->w_wrow;
3493
Bram Moolenaar30613902019-12-01 22:11:18 +01003494 // move cursor left, if possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 if (curwin->w_cursor.col != 0)
3496 {
3497 if (curwin->w_wcol > 0)
3498 {
Bram Moolenaar0f843ef2023-01-25 17:34:41 +00003499 // After auto-indenting and no text is following,
3500 // we are expecting to truncate the trailing
3501 // white-space, so find the last non-white
3502 // character -- webb
3503 if (did_ai && *skipwhite(ml_get_curline()
3504 + curwin->w_cursor.col) == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003506 chartabsize_T cts;
3507
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003508 curwin->w_wcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 ptr = ml_get_curline();
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003510 init_chartabsize_arg(&cts, curwin,
3511 curwin->w_cursor.lnum, 0, ptr, ptr);
3512 while (cts.cts_ptr < ptr + curwin->w_cursor.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003514 if (!VIM_ISWHITE(*cts.cts_ptr))
3515 curwin->w_wcol = cts.cts_vcol;
3516 cts.cts_vcol += lbr_chartabsize(&cts);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 if (has_mbyte)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003518 cts.cts_ptr +=
3519 (*mb_ptr2len)(cts.cts_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 else
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003521 ++cts.cts_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003523 clear_chartabsize_arg(&cts);
3524
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 curwin->w_wrow = curwin->w_cline_row
Bram Moolenaar02631462017-09-22 15:20:32 +02003526 + curwin->w_wcol / curwin->w_width;
3527 curwin->w_wcol %= curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 curwin->w_wcol += curwin_col_off();
Bram Moolenaar30613902019-12-01 22:11:18 +01003529 col = 0; // no correction needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 }
3531 else
3532 {
3533 --curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 col = curwin->w_cursor.col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 }
3536 }
3537 else if (curwin->w_p_wrap && curwin->w_wrow)
3538 {
3539 --curwin->w_wrow;
Bram Moolenaar02631462017-09-22 15:20:32 +02003540 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 col = curwin->w_cursor.col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 if (has_mbyte && col > 0 && curwin->w_wcol > 0)
3544 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003545 // Correct when the cursor is on the right halve
3546 // of a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 ptr = ml_get_curline();
3548 col -= (*mb_head_off)(ptr, ptr + col);
3549 if ((*mb_ptr2cells)(ptr + col) > 1)
3550 --curwin->w_wcol;
3551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 }
3553 setcursor();
3554 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 new_wcol = curwin->w_wcol;
3556 new_wrow = curwin->w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 curwin->w_wcol = old_wcol;
3558 curwin->w_wrow = old_wrow;
3559 }
3560 if (c < 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003561 continue; // end of input script reached
Bram Moolenaar20c38922014-07-23 20:41:14 +02003562
Bram Moolenaar30613902019-12-01 22:11:18 +01003563 // Allow mapping for just typed characters. When we get here c
3564 // is the number of extra bytes and typebuf.tb_len is 1.
Bram Moolenaar20c38922014-07-23 20:41:14 +02003565 for (n = 1; n <= c; ++n)
3566 typebuf.tb_noremap[typebuf.tb_off + n] = RM_YES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 typebuf.tb_len += c;
3568
Bram Moolenaar30613902019-12-01 22:11:18 +01003569 // buffer full, don't map
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 if (typebuf.tb_len >= typebuf.tb_maplen + MAXMAPLEN)
3571 {
3572 timedout = TRUE;
3573 continue;
3574 }
3575
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 if (ex_normal_busy > 0)
3577 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 static int tc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579
Bram Moolenaar30613902019-12-01 22:11:18 +01003580 // No typeahead left and inside ":normal". Must return
3581 // something to avoid getting stuck. When an incomplete
3582 // mapping is present, behave like it timed out.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 if (typebuf.tb_len > 0)
3584 {
3585 timedout = TRUE;
3586 continue;
3587 }
Bram Moolenaar189832b2020-09-23 12:29:11 +02003588
Bram Moolenaar30613902019-12-01 22:11:18 +01003589 // When 'insertmode' is set, ESC just beeps in Insert
3590 // mode. Use CTRL-L to make edit() return.
3591 // For the command line only CTRL-C always breaks it.
3592 // For the cmdline window: Alternate between ESC and
3593 // CTRL-C: ESC for most situations and CTRL-C to close the
3594 // cmdline window.
Bram Moolenaar24959102022-05-07 20:01:16 +01003595 if (p_im && (State & MODE_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 c = Ctrl_L;
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003597#ifdef FEAT_TERMINAL
3598 else if (terminal_is_active())
3599 c = K_CANCEL;
3600#endif
Bram Moolenaar24959102022-05-07 20:01:16 +01003601 else if ((State & MODE_CMDLINE)
Martin Tournoij7904fa42022-10-04 16:28:45 +01003602 || (cmdwin_type > 0 && tc == ESC))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 c = Ctrl_C;
3604 else
3605 c = ESC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 tc = c;
Bram Moolenaar8d696372022-08-21 10:40:07 +01003607 // set a flag to indicate this wasn't a normal char
3608 if (advance)
3609 typebuf_was_empty = TRUE;
3610
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01003611 // return from main_loop()
3612 if (pending_exmode_active)
3613 exmode_active = EXMODE_NORMAL;
3614
Bram Moolenaarb37a65e2022-01-01 12:42:56 +00003615 // no chars to block abbreviation for
3616 typebuf.tb_no_abbr_cnt = 0;
3617
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 break;
3619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620
3621/*
3622 * get a character: 3. from the user - update display
3623 */
Bram Moolenaar30613902019-12-01 22:11:18 +01003624 // In insert mode a screen update is skipped when characters
3625 // are still available. But when those available characters
3626 // are part of a mapping, and we are going to do a blocking
3627 // wait here. Need to update the screen to display the
3628 // changed text so far. Also for when 'lazyredraw' is set and
3629 // redrawing was postponed because there was something in the
3630 // input buffer (e.g., termresponse).
Bram Moolenaar24959102022-05-07 20:01:16 +01003631 if (((State & MODE_INSERT) != 0 || p_lz)
3632 && (State & MODE_CMDLINE) == 0
3633 && advance && must_redraw != 0 && !need_wait_return)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 {
3635 update_screen(0);
Bram Moolenaar30613902019-12-01 22:11:18 +01003636 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 }
3638
3639 /*
3640 * If we have a partial match (and are going to wait for more
3641 * input from the user), show the partially matched characters
3642 * to the user with showcmd.
3643 */
Bram Moolenaareda35f72019-08-03 14:59:44 +02003644 showcmd_idx = 0;
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003645 int showing_partial = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 if (typebuf.tb_len > 0 && advance && !exmode_active)
3647 {
Bram Moolenaar24959102022-05-07 20:01:16 +01003648 if (((State & (MODE_NORMAL | MODE_INSERT))
3649 || State == MODE_LANGMAP)
3650 && State != MODE_HITRETURN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003652 // this looks nice when typing a dead character map
Bram Moolenaar24959102022-05-07 20:01:16 +01003653 if (State & MODE_INSERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
3655 + typebuf.tb_len - 1) == 1)
3656 {
3657 edit_putchar(typebuf.tb_buf[typebuf.tb_off
3658 + typebuf.tb_len - 1], FALSE);
Bram Moolenaar30613902019-12-01 22:11:18 +01003659 setcursor(); // put cursor back where it belongs
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003660 showing_partial = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003662 // need to use the col and row from above here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 old_wcol = curwin->w_wcol;
3664 old_wrow = curwin->w_wrow;
3665 curwin->w_wcol = new_wcol;
3666 curwin->w_wrow = new_wrow;
3667 push_showcmd();
3668 if (typebuf.tb_len > SHOWCMD_COLS)
Bram Moolenaareda35f72019-08-03 14:59:44 +02003669 showcmd_idx = typebuf.tb_len - SHOWCMD_COLS;
3670 while (showcmd_idx < typebuf.tb_len)
zeertzjqacdfb8a2024-04-17 21:28:54 +02003671 add_byte_to_showcmd(
Bram Moolenaareda35f72019-08-03 14:59:44 +02003672 typebuf.tb_buf[typebuf.tb_off + showcmd_idx++]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 curwin->w_wcol = old_wcol;
3674 curwin->w_wrow = old_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 }
3676
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003677 // This looks nice when typing a dead character map.
3678 // There is no actual command line for get_number().
Bram Moolenaar24959102022-05-07 20:01:16 +01003679 if ((State & MODE_CMDLINE)
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003680 && get_cmdline_info()->cmdbuff != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3682 && cmdline_star == 0
3683#endif
3684 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
3685 + typebuf.tb_len - 1) == 1)
3686 {
3687 putcmdline(typebuf.tb_buf[typebuf.tb_off
3688 + typebuf.tb_len - 1], FALSE);
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003689 showing_partial = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 }
3691 }
3692
3693/*
3694 * get a character: 3. from the user - get it
3695 */
Bram Moolenaar83f4cbd2018-06-12 21:35:40 +02003696 if (typebuf.tb_len == 0)
zeertzjq3760bfd2022-06-06 16:22:46 +01003697 // timedout may have been set if a mapping with empty RHS
3698 // fully matched while longer mappings timed out.
Bram Moolenaar83f4cbd2018-06-12 21:35:40 +02003699 timedout = FALSE;
3700
Bram Moolenaar652de232019-04-04 20:13:09 +02003701 if (advance)
3702 {
3703 if (typebuf.tb_len == 0
3704 || !(p_timeout
3705 || (p_ttimeout && keylen == KEYLEN_PART_KEY)))
3706 // blocking wait
3707 wait_time = -1L;
3708 else if (keylen == KEYLEN_PART_KEY && p_ttm >= 0)
3709 wait_time = p_ttm;
3710 else
3711 wait_time = p_tm;
3712 }
3713 else
3714 wait_time = 0;
3715
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003716 wait_tb_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 c = inchar(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len,
3718 typebuf.tb_buflen - typebuf.tb_off - typebuf.tb_len - 1,
Bram Moolenaar652de232019-04-04 20:13:09 +02003719 wait_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720
Bram Moolenaareda35f72019-08-03 14:59:44 +02003721 if (showcmd_idx != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 pop_showcmd();
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003723 if (showing_partial)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 {
Bram Moolenaar24959102022-05-07 20:01:16 +01003725 if (State & MODE_INSERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 edit_unputchar();
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003727 if ((State & MODE_CMDLINE)
3728 && get_cmdline_info()->cmdbuff != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 unputcmdline();
Bram Moolenaarbc256d92012-06-06 12:06:15 +02003730 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003731 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 }
3733
3734 if (c < 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003735 continue; // end of input script reached
3736 if (c == NUL) // no character available
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 {
3738 if (!advance)
3739 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01003740 if (wait_tb_len > 0) // timed out
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 {
3742 timedout = TRUE;
3743 continue;
3744 }
3745 }
3746 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003747 { // allow mapping for just typed characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 while (typebuf.tb_buf[typebuf.tb_off
3749 + typebuf.tb_len] != NUL)
3750 typebuf.tb_noremap[typebuf.tb_off
3751 + typebuf.tb_len++] = RM_YES;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003752#ifdef HAVE_INPUT_METHOD
Bram Moolenaar30613902019-12-01 22:11:18 +01003753 // Get IM status right after getting keys, not after the
3754 // timeout for a mapping (focus may be lost by then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 vgetc_im_active = im_get_status();
3756#endif
3757 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003758 } // for (;;)
3759 } // if (!character from stuffbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760
Bram Moolenaar30613902019-12-01 22:11:18 +01003761 // if advance is FALSE don't loop on NULs
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003762 } while ((c < 0 && c != K_CANCEL) || (advance && c == NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763
3764 /*
3765 * The "INSERT" message is taken care of here:
3766 * if we return an ESC to exit insert mode, the message is deleted
3767 * if we don't return an ESC but deleted the message before, redisplay it
3768 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003769 if (advance && p_smd && msg_silent == 0 && (State & MODE_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003771 if (c == ESC && !mode_deleted && !no_mapping && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 {
3773 if (typebuf.tb_len && !KeyTyped)
Bram Moolenaar30613902019-12-01 22:11:18 +01003774 redraw_cmdline = TRUE; // delete mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 else
3776 unshowmode(FALSE);
3777 }
3778 else if (c != ESC && mode_deleted)
3779 {
3780 if (typebuf.tb_len && !KeyTyped)
Bram Moolenaar30613902019-12-01 22:11:18 +01003781 redraw_cmdline = TRUE; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 else
3783 showmode();
3784 }
3785 }
3786#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003787 // may unshow different cursor shape
Bram Moolenaarf085f422017-06-07 20:39:47 +02003788 if (gui.in_use && shape_changed)
3789 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790#endif
Bram Moolenaar6edbbd82019-03-10 09:41:51 +01003791 if (timedout && c == ESC)
3792 {
zeertzjqbf321802024-01-28 19:03:00 +01003793 // When recording there will be no timeout. Add an <Ignore> after the
3794 // ESC to avoid that it forms a key code with following characters.
3795 gotchars_ignore();
Bram Moolenaar6edbbd82019-03-10 09:41:51 +01003796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003798 --vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799
3800 return c;
3801}
3802
3803/*
3804 * inchar() - get one character from
3805 * 1. a scriptfile
3806 * 2. the keyboard
3807 *
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003808 * As many characters as we can get (up to 'maxlen') are put in "buf" and
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 * NUL terminated (buffer length must be 'maxlen' + 1).
3810 * Minimum for "maxlen" is 3!!!!
3811 *
3812 * "tb_change_cnt" is the value of typebuf.tb_change_cnt if "buf" points into
3813 * it. When typebuf.tb_change_cnt changes (e.g., when a message is received
3814 * from a remote client) "buf" can no longer be used. "tb_change_cnt" is 0
3815 * otherwise.
3816 *
3817 * If we got an interrupt all input is read until none is available.
3818 *
3819 * If wait_time == 0 there is no waiting for the char.
3820 * If wait_time == n we wait for n msec for a character to arrive.
3821 * If wait_time == -1 we wait forever for a character to arrive.
3822 *
3823 * Return the number of obtained characters.
3824 * Return -1 when end of input script reached.
3825 */
Bram Moolenaarcda77642016-06-04 13:32:35 +02003826 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003827inchar(
3828 char_u *buf,
3829 int maxlen,
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003830 long wait_time) // milliseconds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831{
Bram Moolenaar30613902019-12-01 22:11:18 +01003832 int len = 0; // init for GCC
3833 int retesc = FALSE; // return ESC with gotint
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 int script_char;
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003835 int tb_change_cnt = typebuf.tb_change_cnt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836
Bram Moolenaar30613902019-12-01 22:11:18 +01003837 if (wait_time == -1L || wait_time > 100L) // flush output before waiting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 {
3839 cursor_on();
Bram Moolenaara338adc2018-01-31 20:51:47 +01003840 out_flush_cursor(FALSE, FALSE);
3841#if defined(FEAT_GUI) && defined(FEAT_MOUSESHAPE)
3842 if (gui.in_use && postponed_mouseshape)
3843 update_mouseshape(-1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844#endif
3845 }
3846
3847 /*
3848 * Don't reset these when at the hit-return prompt, otherwise a endless
3849 * recursive loop may result (write error in swapfile, hit-return, timeout
3850 * on char wait, flush swapfile, write error....).
3851 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003852 if (State != MODE_HITRETURN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003854 did_outofmem_msg = FALSE; // display out of memory message (again)
3855 did_swapwrite_msg = FALSE; // display swap file write error again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003857 undo_off = FALSE; // restart undo now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858
3859 /*
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00003860 * Get a character from a script file if there is one.
3861 * If interrupted: Stop reading script files, close them all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 */
3863 script_char = -1;
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00003864 while (scriptin[curscript] != NULL && script_char < 0
3865#ifdef FEAT_EVAL
3866 && !ignore_script
3867#endif
3868 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02003870#ifdef MESSAGE_QUEUE
3871 parse_queued_messages();
Bram Moolenaarf2330482008-06-24 20:19:36 +00003872#endif
3873
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 if (got_int || (script_char = getc(scriptin[curscript])) < 0)
3875 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003876 // Reached EOF.
3877 // Careful: closescript() frees typebuf.tb_buf[] and buf[] may
3878 // point inside typebuf.tb_buf[]. Don't use buf[] after this!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 closescript();
3880 /*
3881 * When reading script file is interrupted, return an ESC to get
3882 * back to normal mode.
3883 * Otherwise return -1, because typebuf.tb_buf[] has changed.
3884 */
3885 if (got_int)
3886 retesc = TRUE;
3887 else
3888 return -1;
3889 }
3890 else
3891 {
3892 buf[0] = script_char;
3893 len = 1;
3894 }
3895 }
3896
Bram Moolenaar30613902019-12-01 22:11:18 +01003897 if (script_char < 0) // did not get a character from script
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 {
3899 /*
3900 * If we got an interrupt, skip all previously typed characters and
3901 * return TRUE if quit reading script file.
3902 * Stop reading typeahead when a single CTRL-C was read,
3903 * fill_input_buf() returns this when not able to read from stdin.
3904 * Don't use buf[] here, closescript() may have freed typebuf.tb_buf[]
3905 * and buf may be pointing inside typebuf.tb_buf[].
3906 */
3907 if (got_int)
3908 {
kylo252ae6f1d82022-02-16 19:24:07 +00003909#define DUM_LEN (MAXMAPLEN * 3 + 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 char_u dum[DUM_LEN + 1];
3911
3912 for (;;)
3913 {
3914 len = ui_inchar(dum, DUM_LEN, 0L, 0);
zeertzjq17c95d92022-04-26 12:51:07 +01003915 if (len == 0 || (len == 1 && dum[0] == Ctrl_C))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 break;
3917 }
3918 return retesc;
3919 }
3920
3921 /*
3922 * Always flush the output characters when getting input characters
Bram Moolenaarcb574f42019-01-25 22:29:57 +01003923 * from the user and not just peeking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 */
Bram Moolenaarcb574f42019-01-25 22:29:57 +01003925 if (wait_time == -1L || wait_time > 10L)
3926 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927
3928 /*
3929 * Fill up to a third of the buffer, because each character may be
3930 * tripled below.
3931 */
3932 len = ui_inchar(buf, maxlen / 3, wait_time, tb_change_cnt);
3933 }
3934
Bram Moolenaar30613902019-12-01 22:11:18 +01003935 // If the typebuf was changed further down, it is like nothing was added by
3936 // this call.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 if (typebuf_changed(tb_change_cnt))
3938 return 0;
3939
Bram Moolenaar30613902019-12-01 22:11:18 +01003940 // Note the change in the typeahead buffer, this matters for when
3941 // vgetorpeek() is called recursively, e.g. using getchar(1) in a timer
3942 // function.
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003943 if (len > 0 && ++typebuf.tb_change_cnt == 0)
3944 typebuf.tb_change_cnt = 1;
3945
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003946 return fix_input_buffer(buf, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947}
3948
3949/*
3950 * Fix typed characters for use by vgetc() and check_termcode().
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003951 * "buf[]" must have room to triple the number of bytes!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 * Returns the new length.
3953 */
3954 int
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003955fix_input_buffer(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956{
3957 int i;
3958 char_u *p = buf;
3959
3960 /*
3961 * Two characters are special: NUL and K_SPECIAL.
3962 * When compiled With the GUI CSI is also special.
3963 * Replace NUL by K_SPECIAL KS_ZERO KE_FILLER
3964 * Replace K_SPECIAL by K_SPECIAL KS_SPECIAL KE_FILLER
3965 * Replace CSI by K_SPECIAL KS_EXTRA KE_CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 */
3967 for (i = len; --i >= 0; ++p)
3968 {
3969#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003970 // When the GUI is used any character can come after a CSI, don't
3971 // escape it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 if (gui.in_use && p[0] == CSI && i >= 2)
3973 {
3974 p += 2;
3975 i -= 2;
3976 }
zeertzjq646bb722022-02-16 17:51:47 +00003977# ifndef MSWIN
3978 // When not on MS-Windows and the GUI is not used CSI needs to be
3979 // escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 else if (!gui.in_use && p[0] == CSI)
3981 {
3982 mch_memmove(p + 3, p + 1, (size_t)i);
3983 *p++ = K_SPECIAL;
3984 *p++ = KS_EXTRA;
3985 *p = (int)KE_CSI;
3986 len += 2;
3987 }
zeertzjq646bb722022-02-16 17:51:47 +00003988# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 else
3990#endif
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003991 if (p[0] == NUL || (p[0] == K_SPECIAL
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003992 // timeout may generate K_CURSORHOLD
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003993 && (i < 2 || p[1] != KS_EXTRA || p[2] != (int)KE_CURSORHOLD)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003994#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003995 // Win32 console passes modifiers
3996 && (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003997# ifdef VIMDLL
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003998 gui.in_use ||
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003999# endif
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02004000 (i < 2 || p[1] != KS_MODIFIER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001#endif
4002 ))
4003 {
4004 mch_memmove(p + 3, p + 1, (size_t)i);
4005 p[2] = K_THIRD(p[0]);
4006 p[1] = K_SECOND(p[0]);
4007 p[0] = K_SPECIAL;
4008 p += 2;
4009 len += 2;
4010 }
4011 }
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02004012 *p = NUL; // add trailing NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 return len;
4014}
4015
4016#if defined(USE_INPUT_BUF) || defined(PROTO)
4017/*
4018 * Return TRUE when bytes are in the input buffer or in the typeahead buffer.
4019 * Normally the input buffer would be sufficient, but the server_to_input_buf()
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004020 * or feedkeys() may insert characters in the typeahead buffer while we are
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004021 * waiting for input to arrive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 */
4023 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004024input_available(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025{
4026 return (!vim_is_input_buf_empty()
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004027# if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
4028 || typebuf_was_filled
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029# endif
4030 );
4031}
4032#endif
Bram Moolenaar957cf672020-11-12 14:21:06 +01004033
4034/*
4035 * Function passed to do_cmdline() to get the command after a <Cmd> key from
4036 * typeahead.
4037 */
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004038 static char_u *
Bram Moolenaar957cf672020-11-12 14:21:06 +01004039getcmdkeycmd(
4040 int promptc UNUSED,
4041 void *cookie UNUSED,
4042 int indent UNUSED,
4043 getline_opt_T do_concat UNUSED)
4044{
4045 garray_T line_ga;
4046 int c1 = -1;
4047 int c2;
4048 int cmod = 0;
4049 int aborted = FALSE;
4050
4051 ga_init2(&line_ga, 1, 32);
4052
4053 // no mapping for these characters
4054 no_mapping++;
4055
4056 got_int = FALSE;
4057 while (c1 != NUL && !aborted)
4058 {
Yegappan Lakshmananfadc02a2023-01-27 21:03:12 +00004059 if (ga_grow(&line_ga, 32) == FAIL)
Bram Moolenaarca359cb2020-11-15 20:49:41 +01004060 {
4061 aborted = TRUE;
4062 break;
4063 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01004064
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01004065 if (vgetorpeek(FALSE) == NUL)
Bram Moolenaar957cf672020-11-12 14:21:06 +01004066 {
4067 // incomplete <Cmd> is an error, because there is not much the user
4068 // could do in this state.
4069 emsg(_(e_cmd_mapping_must_end_with_cr));
4070 aborted = TRUE;
4071 break;
4072 }
4073
4074 // Get one character at a time.
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01004075 c1 = vgetorpeek(TRUE);
4076
Bram Moolenaar957cf672020-11-12 14:21:06 +01004077 // Get two extra bytes for special keys
4078 if (c1 == K_SPECIAL)
4079 {
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01004080 c1 = vgetorpeek(TRUE);
4081 c2 = vgetorpeek(TRUE);
Bram Moolenaar957cf672020-11-12 14:21:06 +01004082 if (c1 == KS_MODIFIER)
4083 {
4084 cmod = c2;
4085 continue;
4086 }
4087 c1 = TO_SPECIAL(c1, c2);
Bram Moolenaardffa6ea2022-11-29 20:33:20 +00004088
4089 // K_ESC is used to avoid ambiguity with the single Esc character
4090 // that might be the start of an escape sequence. Convert it back
4091 // to a single Esc here.
4092 if (c1 == K_ESC)
4093 c1 = ESC;
Bram Moolenaar957cf672020-11-12 14:21:06 +01004094 }
4095
4096 if (got_int)
4097 aborted = TRUE;
4098 else if (c1 == '\r' || c1 == '\n')
4099 c1 = NUL; // end the line
4100 else if (c1 == ESC)
4101 aborted = TRUE;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004102 else if (c1 == K_COMMAND || c1 == K_SCRIPT_COMMAND)
Bram Moolenaar957cf672020-11-12 14:21:06 +01004103 {
4104 // give a nicer error message for this special case
4105 emsg(_(e_cmd_mapping_must_end_with_cr_before_second_cmd));
4106 aborted = TRUE;
4107 }
zeertzjq3ab3a862023-05-06 16:22:04 +01004108 else if (c1 == K_SNR)
Bram Moolenaar957cf672020-11-12 14:21:06 +01004109 {
zeertzjq3ab3a862023-05-06 16:22:04 +01004110 ga_concat(&line_ga, (char_u *)"<SNR>");
Bram Moolenaar957cf672020-11-12 14:21:06 +01004111 }
4112 else
zeertzjq3ab3a862023-05-06 16:22:04 +01004113 {
4114 if (cmod != 0)
4115 {
4116 ga_append(&line_ga, K_SPECIAL);
4117 ga_append(&line_ga, KS_MODIFIER);
4118 ga_append(&line_ga, cmod);
4119 }
4120 if (IS_SPECIAL(c1))
4121 {
4122 ga_append(&line_ga, K_SPECIAL);
4123 ga_append(&line_ga, K_SECOND(c1));
4124 ga_append(&line_ga, K_THIRD(c1));
4125 }
4126 else
4127 ga_append(&line_ga, c1);
4128 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01004129
4130 cmod = 0;
4131 }
4132
4133 no_mapping--;
4134
4135 if (aborted)
4136 ga_clear(&line_ga);
4137
4138 return (char_u *)line_ga.ga_data;
4139}
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004140
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004141#if defined(FEAT_EVAL) || defined(PROTO)
4142/*
Bram Moolenaar6b066c62023-02-20 18:44:33 +00004143 * If there was a mapping we get its SID. Otherwise, use "last_used_sid", it
4144 * is set when redo'ing.
4145 * Put this SID in the redo buffer, so that "." will use the same script
4146 * context.
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004147 */
4148 void
4149may_add_last_used_map_to_redobuff(void)
4150{
Bram Moolenaar6b066c62023-02-20 18:44:33 +00004151 char_u buf[3 + 20];
4152 int sid = -1;
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004153
Bram Moolenaar6b066c62023-02-20 18:44:33 +00004154 if (last_used_map != NULL)
4155 sid = last_used_map->m_script_ctx.sc_sid;
4156 if (sid < 0)
4157 sid = last_used_sid;
4158
4159 if (sid < 0)
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004160 return;
4161
4162 // <K_SID>{nr};
4163 buf[0] = K_SPECIAL;
4164 buf[1] = KS_EXTRA;
4165 buf[2] = KE_SID;
Bram Moolenaar6b066c62023-02-20 18:44:33 +00004166 vim_snprintf((char *)buf + 3, 20, "%d;", sid);
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004167 add_buff(&redobuff, buf, -1L);
4168}
4169#endif
4170
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004171 int
Bram Moolenaard4e2f502022-01-15 18:48:32 +00004172do_cmdkey_command(int key UNUSED, int flags)
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004173{
4174 int res;
4175#ifdef FEAT_EVAL
Bram Moolenaara9725222022-01-16 13:30:33 +00004176 sctx_T save_current_sctx = {-1, 0, 0, 0};
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004177
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004178 if (key == K_SCRIPT_COMMAND
4179 && (last_used_map != NULL || SCRIPT_ID_VALID(last_used_sid)))
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004180 {
4181 save_current_sctx = current_sctx;
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004182 if (last_used_map != NULL)
4183 current_sctx = last_used_map->m_script_ctx;
4184 else
4185 {
4186 current_sctx.sc_sid = last_used_sid;
4187 current_sctx.sc_lnum = 0;
4188 current_sctx.sc_version = SCRIPT_ITEM(last_used_sid)->sn_version;
4189 }
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004190 }
4191#endif
4192
4193 res = do_cmdline(NULL, getcmdkeycmd, NULL, flags);
4194
4195#ifdef FEAT_EVAL
Bram Moolenaara9725222022-01-16 13:30:33 +00004196 if (save_current_sctx.sc_sid >= 0)
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004197 current_sctx = save_current_sctx;
4198#endif
4199
4200 return res;
4201}
4202
4203#if defined(FEAT_EVAL) || defined(PROTO)
4204 void
Bram Moolenaarf61c89d2022-01-19 22:51:48 +00004205reset_last_used_map(mapblock_T *mp)
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004206{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00004207 if (last_used_map != mp)
4208 return;
4209
4210 last_used_map = NULL;
4211 last_used_sid = -1;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004212}
4213#endif