blob: 179c50c8fa876e7852fe2e9e29a8470950414f78 [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
Bram Moolenaar30613902019-12-01 22:11:18 +010084static int last_recorded_len = 0; // number of last recorded chars
Bram Moolenaar071d4272004-06-13 20:20:40 +000085
Bram Moolenaare32c3c42022-01-15 18:26:04 +000086#ifdef FEAT_EVAL
87mapblock_T *last_used_map = NULL;
Bram Moolenaarddf7dba2022-09-05 16:53:21 +010088int last_used_sid = -1;
Bram Moolenaare32c3c42022-01-15 18:26:04 +000089#endif
90
Bram Moolenaard25c16e2016-01-29 22:13:30 +010091static int read_readbuf(buffheader_T *buf, int advance);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010092static void init_typebuf(void);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010093static void may_sync_undo(void);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020094static void free_typebuf(void);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010095static void closescript(void);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020096static void updatescript(int c);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010097static int vgetorpeek(int);
Bram Moolenaar0f0f2302017-08-30 18:52:56 +020098static int inchar(char_u *buf, int maxlen, long wait_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +000099
100/*
101 * Free and clear a buffer.
102 */
Bram Moolenaarbdff0122020-04-05 18:56:05 +0200103 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100104free_buff(buffheader_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100106 buffblock_T *p, *np;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107
Bram Moolenaar285e3352018-04-18 23:01:13 +0200108 for (p = buf->bh_first.b_next; p != NULL; p = np)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000109 {
110 np = p->b_next;
111 vim_free(p);
112 }
Bram Moolenaar285e3352018-04-18 23:01:13 +0200113 buf->bh_first.b_next = NULL;
Bram Moolenaar166788c2022-01-27 21:56:40 +0000114 buf->bh_curr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115}
116
117/*
118 * Return the contents of a buffer as a single string.
119 * K_SPECIAL and CSI in the returned string are escaped.
120 */
121 static char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100122get_buffcont(
123 buffheader_T *buffer,
Bram Moolenaar30613902019-12-01 22:11:18 +0100124 int dozero) // count == zero is not an error
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125{
126 long_u count = 0;
127 char_u *p = NULL;
128 char_u *p2;
129 char_u *str;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200130 buffblock_T *bp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131
Bram Moolenaar30613902019-12-01 22:11:18 +0100132 // compute the total length of the string
Bram Moolenaar285e3352018-04-18 23:01:13 +0200133 for (bp = buffer->bh_first.b_next; bp != NULL; bp = bp->b_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134 count += (long_u)STRLEN(bp->b_str);
135
Bram Moolenaarf39d9e92023-04-22 22:54:40 +0100136 if ((count > 0 || dozero) && (p = alloc(count + 1)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137 {
138 p2 = p;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200139 for (bp = buffer->bh_first.b_next; bp != NULL; bp = bp->b_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140 for (str = bp->b_str; *str; )
141 *p2++ = *str++;
142 *p2 = NUL;
143 }
Bram Moolenaarf39d9e92023-04-22 22:54:40 +0100144 return p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145}
146
147/*
148 * Return the contents of the record buffer as a single string
149 * and clear the record buffer.
150 * K_SPECIAL and CSI in the returned string are escaped.
151 */
152 char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100153get_recorded(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154{
155 char_u *p;
156 size_t len;
157
158 p = get_buffcont(&recordbuff, TRUE);
159 free_buff(&recordbuff);
160
161 /*
162 * Remove the characters that were added the last time, these must be the
163 * (possibly mapped) characters that stopped the recording.
164 */
165 len = STRLEN(p);
166 if ((int)len >= last_recorded_len)
167 {
168 len -= last_recorded_len;
169 p[len] = NUL;
170 }
171
172 /*
173 * When stopping recording from Insert mode with CTRL-O q, also remove the
174 * CTRL-O.
175 */
176 if (len > 0 && restart_edit != 0 && p[len - 1] == Ctrl_O)
177 p[len - 1] = NUL;
178
179 return (p);
180}
181
182/*
183 * Return the contents of the redo buffer as a single string.
184 * K_SPECIAL and CSI in the returned string are escaped.
185 */
186 char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100187get_inserted(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188{
Bram Moolenaarf2330482008-06-24 20:19:36 +0000189 return get_buffcont(&redobuff, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190}
191
192/*
Bram Moolenaar4e86cba2007-05-06 11:56:32 +0000193 * Add string "s" after the current block of buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194 * K_SPECIAL and CSI should have been escaped already.
195 */
196 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100197add_buff(
198 buffheader_T *buf,
199 char_u *s,
Bram Moolenaar30613902019-12-01 22:11:18 +0100200 long slen) // length of "s" or -1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100202 buffblock_T *p;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200203 long_u len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204
205 if (slen < 0)
206 slen = (long)STRLEN(s);
Bram Moolenaar30613902019-12-01 22:11:18 +0100207 if (slen == 0) // don't add empty strings
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208 return;
209
Bram Moolenaar30613902019-12-01 22:11:18 +0100210 if (buf->bh_first.b_next == NULL) // first add to list
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211 {
212 buf->bh_space = 0;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200213 buf->bh_curr = &(buf->bh_first);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214 }
Bram Moolenaar30613902019-12-01 22:11:18 +0100215 else if (buf->bh_curr == NULL) // buffer has already been read
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216 {
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
1284/*
1285 * Write typed characters to script file.
zeertzjqea95f1a2024-03-29 10:11:42 +01001286 * If recording is on put the character in the record buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 */
1288 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001289gotchars(char_u *chars, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290{
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001291 char_u *s = chars;
zeertzjqea95f1a2024-03-29 10:11:42 +01001292 size_t i;
1293 int c = NUL;
1294 static int prev_c = NUL;
1295 static char_u buf[MB_MAXBYTES * 3 + 4];
1296 static size_t buflen = 0;
1297 static unsigned pending = 0;
1298 static int in_special = FALSE;
1299 static int in_mbyte = FALSE;
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001300 int todo = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301
zeertzjqea95f1a2024-03-29 10:11:42 +01001302 for (; todo--; prev_c = c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 {
zeertzjqea95f1a2024-03-29 10:11:42 +01001304 c = buf[buflen++] = *s++;
1305 if (pending > 0)
1306 pending--;
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001307
1308 // When receiving a special key sequence, store it until we have all
1309 // the bytes and we can decide what to do with it.
zeertzjqea95f1a2024-03-29 10:11:42 +01001310 if ((pending == 0 || in_mbyte)
1311 && (c == K_SPECIAL
1312#ifdef FEAT_GUI
1313 || c == CSI
1314#endif
1315 ))
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001316 {
zeertzjqea95f1a2024-03-29 10:11:42 +01001317 pending += 2;
1318 if (!in_mbyte)
1319 in_special = TRUE;
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001320 }
1321
zeertzjqea95f1a2024-03-29 10:11:42 +01001322 if (pending > 0)
1323 continue;
1324
1325 if (!in_mbyte)
1326 {
1327 if (in_special)
1328 {
1329 in_special = FALSE;
1330 if (prev_c == KS_MODIFIER)
1331 // When receiving a modifier, wait for the modified key.
1332 continue;
1333 c = TO_SPECIAL(prev_c, c);
1334 if (c == K_FOCUSGAINED || c == K_FOCUSLOST)
1335 // Drop K_FOCUSGAINED and K_FOCUSLOST, they are not useful
1336 // in a recording.
1337 buflen = 0;
1338 }
1339 // When receiving a multibyte character, store it until we have all
1340 // the bytes, so that it won't be split between two buffer blocks,
1341 // and delete_buff_tail() will work properly.
1342 pending = MB_BYTE2LEN_CHECK(c) - 1;
1343 if (pending > 0)
1344 {
1345 in_mbyte = TRUE;
1346 continue;
1347 }
1348 }
1349 else
1350 // Stored all bytes of a multibyte character.
1351 in_mbyte = FALSE;
1352
Bram Moolenaar30613902019-12-01 22:11:18 +01001353 // Handle one byte at a time; no translation to be done.
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001354 for (i = 0; i < buflen; ++i)
1355 updatescript(buf[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001357 if (reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 {
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001359 buf[buflen] = NUL;
1360 add_buff(&recordbuff, buf, (long)buflen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001361 // remember how many chars were last recorded
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001362 last_recorded_len += buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 }
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001364 buflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 }
zeertzjqea95f1a2024-03-29 10:11:42 +01001366
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 may_sync_undo();
1368
1369#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01001370 // output "debug mode" message next time in debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 debug_did_msg = FALSE;
1372#endif
1373
Bram Moolenaar30613902019-12-01 22:11:18 +01001374 // Since characters have been typed, consider the following to be in
1375 // another mapping. Search string will be kept in history.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 ++maptick;
1377}
1378
1379/*
zeertzjqbf321802024-01-28 19:03:00 +01001380 * Record an <Ignore> key.
zeertzjqbacc8302023-08-12 00:09:31 +02001381 */
1382 void
zeertzjqbf321802024-01-28 19:03:00 +01001383gotchars_ignore(void)
zeertzjqbacc8302023-08-12 00:09:31 +02001384{
zeertzjqbf321802024-01-28 19:03:00 +01001385 char_u nop_buf[3] = { K_SPECIAL, KS_EXTRA, KE_IGNORE };
zeertzjqbacc8302023-08-12 00:09:31 +02001386 gotchars(nop_buf, 3);
1387}
1388
1389/*
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001390 * Undo the last gotchars() for "len" bytes. To be used when putting a typed
1391 * character back into the typeahead buffer, thus gotchars() will be called
1392 * again.
1393 * Only affects recorded characters.
1394 */
1395 void
1396ungetchars(int len)
1397{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001398 if (reg_recording == 0)
1399 return;
1400
1401 delete_buff_tail(&recordbuff, len);
1402 last_recorded_len -= len;
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001403}
1404
1405/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 * Sync undo. Called when typed characters are obtained from the typeahead
1407 * buffer, or when a menu is used.
1408 * Do not sync:
1409 * - In Insert mode, unless cursor key has been used.
1410 * - While reading a script file.
1411 * - When no_u_sync is non-zero.
1412 */
1413 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001414may_sync_undo(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415{
Bram Moolenaar24959102022-05-07 20:01:16 +01001416 if ((!(State & (MODE_INSERT | MODE_CMDLINE)) || arrow_used)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001417 && scriptin[curscript] == NULL)
1418 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419}
1420
1421/*
1422 * Make "typebuf" empty and allocate new buffers.
1423 * Returns FAIL when out of memory.
1424 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001425 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001426alloc_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427{
1428 typebuf.tb_buf = alloc(TYPELEN_INIT);
1429 typebuf.tb_noremap = alloc(TYPELEN_INIT);
1430 if (typebuf.tb_buf == NULL || typebuf.tb_noremap == NULL)
1431 {
1432 free_typebuf();
1433 return FAIL;
1434 }
1435 typebuf.tb_buflen = TYPELEN_INIT;
Bram Moolenaar30613902019-12-01 22:11:18 +01001436 typebuf.tb_off = MAXMAPLEN + 4; // can insert without realloc
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 typebuf.tb_len = 0;
1438 typebuf.tb_maplen = 0;
1439 typebuf.tb_silent = 0;
1440 typebuf.tb_no_abbr_cnt = 0;
1441 if (++typebuf.tb_change_cnt == 0)
1442 typebuf.tb_change_cnt = 1;
Bram Moolenaare49b4bb2020-03-11 13:01:40 +01001443#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
1444 typebuf_was_filled = FALSE;
1445#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 return OK;
1447}
1448
1449/*
1450 * Free the buffers of "typebuf".
1451 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001452 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001453free_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454{
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001455 if (typebuf.tb_buf == typebuf_init)
Bram Moolenaar95f09602016-11-10 20:01:45 +01001456 internal_error("Free typebuf 1");
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001457 else
Bram Moolenaar0f1c6702019-09-28 15:24:00 +02001458 VIM_CLEAR(typebuf.tb_buf);
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00001459 if (typebuf.tb_noremap == noremapbuf_init)
Bram Moolenaar95f09602016-11-10 20:01:45 +01001460 internal_error("Free typebuf 2");
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001461 else
Bram Moolenaar0f1c6702019-09-28 15:24:00 +02001462 VIM_CLEAR(typebuf.tb_noremap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463}
1464
1465/*
1466 * When doing ":so! file", the current typeahead needs to be saved, and
1467 * restored when "file" has been read completely.
1468 */
1469static typebuf_T saved_typebuf[NSCRIPT];
1470
1471 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001472save_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473{
1474 init_typebuf();
1475 saved_typebuf[curscript] = typebuf;
Bram Moolenaar30613902019-12-01 22:11:18 +01001476 // If out of memory: restore typebuf and close file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 if (alloc_typebuf() == FAIL)
1478 {
1479 closescript();
1480 return FAIL;
1481 }
1482 return OK;
1483}
1484
Bram Moolenaar30613902019-12-01 22:11:18 +01001485static int old_char = -1; // character put back by vungetc()
1486static int old_mod_mask; // mod_mask for ungotten character
1487static int old_mouse_row; // mouse_row related to old_char
1488static int old_mouse_col; // mouse_col related to old_char
zeertzjq0f68e6c2022-04-05 13:17:01 +01001489static int old_KeyStuffed; // whether old_char was stuffed
1490
zeertzjq6d4e7252022-04-07 13:58:04 +01001491static int can_get_old_char(void)
zeertzjq0f68e6c2022-04-05 13:17:01 +01001492{
1493 // If the old character was not stuffed and characters have been added to
1494 // the stuff buffer, need to first get the stuffed characters instead.
1495 return old_char != -1 && (old_KeyStuffed || stuff_empty());
1496}
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001497
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498/*
1499 * Save all three kinds of typeahead, so that the user must type at a prompt.
1500 */
1501 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001502save_typeahead(tasave_T *tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503{
1504 tp->save_typebuf = typebuf;
1505 tp->typebuf_valid = (alloc_typebuf() == OK);
1506 if (!tp->typebuf_valid)
1507 typebuf = tp->save_typebuf;
1508
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001509 tp->old_char = old_char;
1510 tp->old_mod_mask = old_mod_mask;
1511 old_char = -1;
1512
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001513 tp->save_readbuf1 = readbuf1;
Bram Moolenaar285e3352018-04-18 23:01:13 +02001514 readbuf1.bh_first.b_next = NULL;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001515 tp->save_readbuf2 = readbuf2;
Bram Moolenaar285e3352018-04-18 23:01:13 +02001516 readbuf2.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517# ifdef USE_INPUT_BUF
1518 tp->save_inputbuf = get_input_buf();
1519# endif
1520}
1521
1522/*
1523 * Restore the typeahead to what it was before calling save_typeahead().
1524 * The allocated memory is freed, can only be called once!
Bram Moolenaarc41badb2021-06-07 22:04:52 +02001525 * When "overwrite" is FALSE input typed later is kept.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 */
1527 void
Bram Moolenaarc41badb2021-06-07 22:04:52 +02001528restore_typeahead(tasave_T *tp, int overwrite UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529{
1530 if (tp->typebuf_valid)
1531 {
1532 free_typebuf();
1533 typebuf = tp->save_typebuf;
1534 }
1535
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001536 old_char = tp->old_char;
1537 old_mod_mask = tp->old_mod_mask;
1538
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001539 free_buff(&readbuf1);
1540 readbuf1 = tp->save_readbuf1;
1541 free_buff(&readbuf2);
1542 readbuf2 = tp->save_readbuf2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543# ifdef USE_INPUT_BUF
Bram Moolenaarc41badb2021-06-07 22:04:52 +02001544 set_input_buf(tp->save_inputbuf, overwrite);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545# endif
1546}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547
1548/*
1549 * Open a new script file for the ":source!" command.
1550 */
1551 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001552openscript(
1553 char_u *name,
Bram Moolenaar30613902019-12-01 22:11:18 +01001554 int directly) // when TRUE execute directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555{
1556 if (curscript + 1 == NSCRIPT)
1557 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001558 emsg(_(e_scripts_nested_too_deep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 return;
1560 }
Bram Moolenaar53575522019-05-22 22:38:25 +02001561
1562 // Disallow sourcing a file in the sandbox, the commands would be executed
1563 // later, possibly outside of the sandbox.
1564 if (check_secure())
1565 return;
1566
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001567#ifdef FEAT_EVAL
1568 if (ignore_script)
Bram Moolenaar30613902019-12-01 22:11:18 +01001569 // Not reading from script, also don't open one. Warning message?
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001570 return;
1571#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572
Bram Moolenaar30613902019-12-01 22:11:18 +01001573 if (scriptin[curscript] != NULL) // already reading script
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 ++curscript;
Bram Moolenaar30613902019-12-01 22:11:18 +01001575 // use NameBuff for expanded name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 expand_env(name, NameBuff, MAXPATHL);
1577 if ((scriptin[curscript] = mch_fopen((char *)NameBuff, READBIN)) == NULL)
1578 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001579 semsg(_(e_cant_open_file_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 if (curscript)
1581 --curscript;
1582 return;
1583 }
1584 if (save_typebuf() == FAIL)
1585 return;
1586
1587 /*
1588 * Execute the commands from the file right now when using ":source!"
1589 * after ":global" or ":argdo" or in a loop. Also when another command
1590 * follows. This means the display won't be updated. Don't do this
1591 * always, "make test" would fail.
1592 */
1593 if (directly)
1594 {
1595 oparg_T oa;
1596 int oldcurscript;
1597 int save_State = State;
1598 int save_restart_edit = restart_edit;
1599 int save_insertmode = p_im;
1600 int save_finish_op = finish_op;
1601 int save_msg_scroll = msg_scroll;
1602
Bram Moolenaar24959102022-05-07 20:01:16 +01001603 State = MODE_NORMAL;
Bram Moolenaar30613902019-12-01 22:11:18 +01001604 msg_scroll = FALSE; // no msg scrolling in Normal mode
1605 restart_edit = 0; // don't go to Insert mode
1606 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 clear_oparg(&oa);
1608 finish_op = FALSE;
1609
1610 oldcurscript = curscript;
1611 do
1612 {
Bram Moolenaar386b43e2019-05-19 21:57:11 +02001613 update_topline_cursor(); // update cursor position and topline
1614 normal_cmd(&oa, FALSE); // execute one command
Bram Moolenaarae97b942020-07-09 19:16:35 +02001615 (void)vpeekc(); // check for end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 }
1617 while (scriptin[oldcurscript] != NULL);
1618
1619 State = save_State;
1620 msg_scroll = save_msg_scroll;
1621 restart_edit = save_restart_edit;
1622 p_im = save_insertmode;
1623 finish_op = save_finish_op;
1624 }
1625}
1626
1627/*
1628 * Close the currently active input script.
1629 */
1630 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001631closescript(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632{
1633 free_typebuf();
1634 typebuf = saved_typebuf[curscript];
1635
1636 fclose(scriptin[curscript]);
1637 scriptin[curscript] = NULL;
1638 if (curscript > 0)
1639 --curscript;
1640}
1641
Bram Moolenaarea408852005-06-25 22:49:46 +00001642#if defined(EXITFREE) || defined(PROTO)
1643 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001644close_all_scripts(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00001645{
1646 while (scriptin[0] != NULL)
1647 closescript();
1648}
1649#endif
1650
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651/*
1652 * Return TRUE when reading keys from a script file.
1653 */
1654 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001655using_script(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656{
1657 return scriptin[curscript] != NULL;
1658}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659
1660/*
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001661 * This function is called just before doing a blocking wait. Thus after
1662 * waiting 'updatetime' for a character to arrive.
1663 */
1664 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001665before_blocking(void)
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001666{
1667 updatescript(0);
1668#ifdef FEAT_EVAL
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001669 if (may_garbage_collect)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001670 garbage_collect(FALSE);
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001671#endif
1672}
1673
1674/*
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001675 * updatescript() is called when a character can be written into the script
1676 * file or when we have waited some time for a character (c == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 *
1678 * All the changed memfiles are synced if c == 0 or when the number of typed
1679 * characters reaches 'updatecount' and 'updatecount' is non-zero.
1680 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001681 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001682updatescript(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683{
1684 static int count = 0;
1685
1686 if (c && scriptout)
1687 putc(c, scriptout);
1688 if (c == 0 || (p_uc > 0 && ++count >= p_uc))
1689 {
1690 ml_sync_all(c == 0, TRUE);
1691 count = 0;
1692 }
1693}
1694
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695/*
RestorerZ68ebcee2023-05-31 17:12:14 +01001696 * Convert "c_arg" plus "modifiers" to merge the effect of modifyOtherKeys into
1697 * the character. Also for when the Kitty key protocol is used.
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001698 */
1699 int
Bram Moolenaar975a8802020-06-06 22:36:24 +02001700merge_modifyOtherKeys(int c_arg, int *modifiers)
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001701{
1702 int c = c_arg;
1703
Bram Moolenaarea83c192023-03-18 17:22:46 +00001704 // CTRL only uses the lower 5 bits of the character.
zeertzjqbad8a012022-04-29 16:44:00 +01001705 if (*modifiers & MOD_MASK_CTRL)
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001706 {
1707 if ((c >= '`' && c <= 0x7f) || (c >= '@' && c <= '_'))
zeertzjq17c95d92022-04-26 12:51:07 +01001708 {
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001709 c &= 0x1f;
zeertzjq17c95d92022-04-26 12:51:07 +01001710 if (c == NUL)
1711 c = K_ZERO;
1712 }
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001713 else if (c == '6')
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001714 // CTRL-6 is equivalent to CTRL-^
1715 c = 0x1e;
Bram Moolenaarf4ae6b22020-05-30 19:52:46 +02001716#ifdef FEAT_GUI_GTK
1717 // These mappings look arbitrary at the first glance, but in fact
1718 // resemble quite exactly the behaviour of the GTK+ 1.2 GUI on my
1719 // machine. The only difference is BS vs. DEL for CTRL-8 (makes
1720 // more sense and is consistent with usual terminal behaviour).
1721 else if (c == '2')
1722 c = NUL;
1723 else if (c >= '3' && c <= '7')
1724 c = c ^ 0x28;
1725 else if (c == '8')
1726 c = BS;
1727 else if (c == '?')
1728 c = DEL;
1729#endif
1730 if (c != c_arg)
Bram Moolenaar975a8802020-06-06 22:36:24 +02001731 *modifiers &= ~MOD_MASK_CTRL;
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001732 }
Bram Moolenaarea83c192023-03-18 17:22:46 +00001733
1734 // Alt/Meta sets the 8th bit of the character.
Bram Moolenaar975a8802020-06-06 22:36:24 +02001735 if ((*modifiers & (MOD_MASK_META | MOD_MASK_ALT))
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001736 && c >= 0 && c <= 127)
1737 {
Bram Moolenaarea83c192023-03-18 17:22:46 +00001738 // Some terminals (esp. Kitty) do not include Shift in the character.
1739 // Apply it here to get consistency across terminals. Only do ASCII
1740 // letters, for other characters it depends on the keyboard layout.
1741 if ((*modifiers & MOD_MASK_SHIFT) && c >= 'a' && c <= 'z')
1742 {
1743 c += 'a' - 'A';
1744 *modifiers &= ~MOD_MASK_SHIFT;
1745 }
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001746 c += 0x80;
Bram Moolenaara9a6b032023-02-05 18:00:42 +00001747 *modifiers &= ~(MOD_MASK_META | MOD_MASK_ALT);
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001748 }
Bram Moolenaarea83c192023-03-18 17:22:46 +00001749
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001750 return c;
1751}
1752
1753/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 * Get the next input character.
1755 * Can return a special key or a multi-byte character.
1756 * Can return NUL when called recursively, use safe_vgetc() if that's not
1757 * wanted.
1758 * This translates escaped K_SPECIAL and CSI bytes to a K_SPECIAL or CSI byte.
1759 * Collects the bytes of a multibyte character into the whole character.
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00001760 * Returns the modifiers in the global "mod_mask".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 */
1762 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001763vgetc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764{
1765 int c, c2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001767 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001770#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01001771 // Do garbage collection when garbagecollect() was called previously and
1772 // we are now at the toplevel.
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001773 if (may_garbage_collect && want_garbage_collect)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001774 garbage_collect(FALSE);
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001775#endif
1776
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 /*
1778 * If a character was put back with vungetc, it was already processed.
1779 * Return it directly.
1780 */
zeertzjq0f68e6c2022-04-05 13:17:01 +01001781 if (can_get_old_char())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 {
1783 c = old_char;
1784 old_char = -1;
1785 mod_mask = old_mod_mask;
Bram Moolenaarb8978712013-03-16 21:42:16 +01001786 mouse_row = old_mouse_row;
1787 mouse_col = old_mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001789 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 {
zeertzjq81b46a62022-04-09 17:58:49 +01001791 // number of characters recorded from the last vgetc() call
1792 static int last_vgetc_recorded_len = 0;
1793
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001794 mod_mask = 0;
1795 vgetc_mod_mask = 0;
1796 vgetc_char = 0;
zeertzjq81b46a62022-04-09 17:58:49 +01001797
1798 // last_recorded_len can be larger than last_vgetc_recorded_len
1799 // if peeking records more
1800 last_recorded_len -= last_vgetc_recorded_len;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001801
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001802 for (;;) // this is done twice if there are modifiers
1803 {
1804 int did_inc = FALSE;
Bram Moolenaar2455c4e2015-09-15 18:29:39 +02001805
Bram Moolenaar78aed952022-09-24 15:36:35 +01001806 // No mapping after modifier has been read, using an input method
1807 // and when a popup window has disabled mapping.
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001808 if (mod_mask
Bram Moolenaar39719052017-09-05 22:20:46 +02001809#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001810 || im_is_preediting()
Bram Moolenaar39719052017-09-05 22:20:46 +02001811#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001812#if defined(FEAT_PROP_POPUP)
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001813 || popup_no_mapping()
1814#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001815 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 {
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001817 ++no_mapping;
1818 ++allow_keys;
Bram Moolenaar78aed952022-09-24 15:36:35 +01001819 // mod_mask value may change, remember we did the increment
1820 did_inc = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 }
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01001822 c = vgetorpeek(TRUE);
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001823 if (did_inc)
1824 {
1825 --no_mapping;
1826 --allow_keys;
1827 }
1828
Christopher Plewright03193062022-11-22 12:58:27 +00001829 // Get two extra bytes for special keys, handle modifiers.
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001830 if (c == K_SPECIAL
Christopher Plewright03193062022-11-22 12:58:27 +00001831#ifdef FEAT_GUI
Christopher Plewright605d02a2022-10-19 11:54:46 +01001832 || c == CSI
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001833#endif
1834 )
1835 {
1836 int save_allow_keys = allow_keys;
1837
1838 ++no_mapping;
1839 allow_keys = 0; // make sure BS is not found
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01001840 c2 = vgetorpeek(TRUE); // no mapping for these chars
1841 c = vgetorpeek(TRUE);
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001842 --no_mapping;
1843 allow_keys = save_allow_keys;
1844 if (c2 == KS_MODIFIER)
1845 {
1846 mod_mask = c;
1847 continue;
1848 }
1849 c = TO_SPECIAL(c2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850
Bram Moolenaardffa6ea2022-11-29 20:33:20 +00001851 // K_ESC is used to avoid ambiguity with the single Esc
1852 // character that might be the start of an escape sequence.
1853 // Convert it back to a single Esc here.
1854 if (c == K_ESC)
1855 c = ESC;
1856
Bram Moolenaar4f974752019-02-17 17:44:42 +01001857#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001858 // Handle K_TEAROFF here, the caller of vgetc() doesn't need to
1859 // know that a menu was torn off
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001860 if (
1861# ifdef VIMDLL
1862 gui.in_use &&
1863# endif
1864 c == K_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 {
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001866 char_u name[200];
K.Takata54119102022-02-03 13:33:03 +00001867 int j;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001868
1869 // get menu path, it ends with a <CR>
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01001870 for (j = 0; (c = vgetorpeek(TRUE)) != '\r'; )
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001871 {
K.Takata54119102022-02-03 13:33:03 +00001872 name[j] = c;
1873 if (j < 199)
1874 ++j;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001875 }
K.Takata54119102022-02-03 13:33:03 +00001876 name[j] = NUL;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001877 gui_make_tearoff(name);
1878 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880#endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001881#if defined(FEAT_GUI) && defined(FEAT_GUI_GTK) && defined(FEAT_MENU)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001882 // GTK: <F10> normally selects the menu, but it's passed until
1883 // here to allow mapping it. Intercept and invoke the GTK
1884 // behavior if it's not mapped.
1885 if (c == K_F10 && gui.menubar != NULL)
1886 {
1887 gtk_menu_shell_select_first(
1888 GTK_MENU_SHELL(gui.menubar), FALSE);
1889 continue;
1890 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001891#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892#ifdef FEAT_GUI
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001893 // Handle focus event here, so that the caller doesn't need to
1894 // know about it. Return K_IGNORE so that we loop once (needed
1895 // if 'lazyredraw' is set).
1896 if (c == K_FOCUSGAINED || c == K_FOCUSLOST)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001897 {
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001898 ui_focus_change(c == K_FOCUSGAINED);
1899 c = K_IGNORE;
Bram Moolenaara9dd2d32019-04-29 21:58:41 +02001900 }
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001901
1902 // Translate K_CSI to CSI. The special key is only used to
1903 // avoid it being recognized as the start of a special key.
1904 if (c == K_CSI)
1905 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906#endif
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01001907#ifdef FEAT_EVAL
1908 if (c == K_SID)
1909 {
1910 int j;
1911
1912 // Handle <SID>{sid}; Do up to 20 digits for safety.
1913 last_used_sid = 0;
Keith Thompson184f71c2024-01-04 21:19:04 +01001914 for (j = 0; j < 20 && SAFE_isdigit(c = vgetorpeek(TRUE)); ++j)
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01001915 last_used_sid = last_used_sid * 10 + (c - '0');
1916 last_used_map = NULL;
1917 continue;
1918 }
1919#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001920 }
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01001921
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001922 // a keypad or special function key was not mapped, use it like
1923 // its ASCII equivalent
1924 switch (c)
1925 {
1926 case K_KPLUS: c = '+'; break;
1927 case K_KMINUS: c = '-'; break;
1928 case K_KDIVIDE: c = '/'; break;
1929 case K_KMULTIPLY: c = '*'; break;
1930 case K_KENTER: c = CAR; break;
1931 case K_KPOINT:
Bram Moolenaar4f974752019-02-17 17:44:42 +01001932#ifdef MSWIN
Bram Moolenaar8e85db02018-07-27 23:16:51 +02001933 // Can be either '.' or a ',',
1934 // depending on the type of keypad.
1935 c = MapVirtualKey(VK_DECIMAL, 2); break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001936#else
Bram Moolenaar8e85db02018-07-27 23:16:51 +02001937 c = '.'; break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001938#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001939 case K_K0: c = '0'; break;
1940 case K_K1: c = '1'; break;
1941 case K_K2: c = '2'; break;
1942 case K_K3: c = '3'; break;
1943 case K_K4: c = '4'; break;
1944 case K_K5: c = '5'; break;
1945 case K_K6: c = '6'; break;
1946 case K_K7: c = '7'; break;
1947 case K_K8: c = '8'; break;
1948 case K_K9: c = '9'; break;
Bram Moolenaara88d9682005-03-25 21:45:43 +00001949
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001950 case K_XHOME:
1951 case K_ZHOME: if (mod_mask == MOD_MASK_SHIFT)
Bram Moolenaara88d9682005-03-25 21:45:43 +00001952 {
1953 c = K_S_HOME;
1954 mod_mask = 0;
1955 }
1956 else if (mod_mask == MOD_MASK_CTRL)
1957 {
1958 c = K_C_HOME;
1959 mod_mask = 0;
1960 }
1961 else
1962 c = K_HOME;
1963 break;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001964 case K_XEND:
1965 case K_ZEND: if (mod_mask == MOD_MASK_SHIFT)
Bram Moolenaara88d9682005-03-25 21:45:43 +00001966 {
1967 c = K_S_END;
1968 mod_mask = 0;
1969 }
1970 else if (mod_mask == MOD_MASK_CTRL)
1971 {
1972 c = K_C_END;
1973 mod_mask = 0;
1974 }
1975 else
1976 c = K_END;
1977 break;
1978
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001979 case K_XUP: c = K_UP; break;
1980 case K_XDOWN: c = K_DOWN; break;
1981 case K_XLEFT: c = K_LEFT; break;
1982 case K_XRIGHT: c = K_RIGHT; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001985 // For a multi-byte character get all the bytes and return the
1986 // converted character.
1987 // Note: This will loop until enough bytes are received!
1988 if (has_mbyte && (n = MB_BYTE2LEN_CHECK(c)) > 1)
1989 {
1990 ++no_mapping;
1991 buf[0] = c;
1992 for (i = 1; i < n; ++i)
1993 {
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01001994 buf[i] = vgetorpeek(TRUE);
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001995 if (buf[i] == K_SPECIAL
1996#ifdef FEAT_GUI
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001997 || (buf[i] == CSI)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001998#endif
1999 )
2000 {
2001 // Must be a K_SPECIAL - KS_SPECIAL - KE_FILLER
2002 // sequence, which represents a K_SPECIAL (0x80),
2003 // or a CSI - KS_EXTRA - KE_CSI sequence, which
2004 // represents a CSI (0x9B),
2005 // or a K_SPECIAL - KS_EXTRA - KE_CSI, which is CSI
2006 // too.
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01002007 c = vgetorpeek(TRUE);
2008 if (vgetorpeek(TRUE) == KE_CSI && c == KS_EXTRA)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002009 buf[i] = CSI;
2010 }
2011 }
2012 --no_mapping;
2013 c = (*mb_ptr2char)(buf);
2014 }
2015
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002016 if (vgetc_char == 0)
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02002017 {
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02002018 vgetc_mod_mask = mod_mask;
2019 vgetc_char = c;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02002020 }
Bram Moolenaar00eab7f2019-10-10 21:49:28 +02002021
Bram Moolenaarfd731b02019-03-09 11:23:58 +01002022 break;
2023 }
zeertzjq81b46a62022-04-09 17:58:49 +01002024
2025 last_vgetc_recorded_len = last_recorded_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00002027
2028#ifdef FEAT_EVAL
2029 /*
2030 * In the main loop "may_garbage_collect" can be set to do garbage
2031 * collection in the first next vgetc(). It's disabled after that to
2032 * avoid internally used Lists and Dicts to be freed.
2033 */
2034 may_garbage_collect = FALSE;
2035#endif
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002036
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01002037#ifdef FEAT_BEVAL_TERM
Bram Moolenaarc2f50542019-07-05 23:24:56 +02002038 if (c != K_MOUSEMOVE && c != K_IGNORE && c != K_CURSORHOLD)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002039 {
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002040 // Don't trigger 'balloonexpr' unless only the mouse was moved.
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002041 bevalexpr_due_set = FALSE;
2042 ui_remove_balloon();
2043 }
2044#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002045#ifdef FEAT_PROP_POPUP
Bram Moolenaar4934ad02020-09-28 22:29:58 +02002046 // Only filter keys that do not come from ":normal". Keys from feedkeys()
2047 // are filtered.
2048 if ((!ex_normal_busy || in_feedkeys) && popup_do_filter(c))
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02002049 {
2050 if (c == Ctrl_C)
2051 got_int = FALSE; // avoid looping
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002052 c = K_IGNORE;
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02002053 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02002054#endif
Bram Moolenaar9fecb462006-09-05 10:59:47 +00002055
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002056 // Need to process the character before we know it's safe to do something
2057 // else.
2058 if (c != K_IGNORE)
Bram Moolenaard103ee72019-09-18 21:15:31 +02002059 state_no_longer_safe("key typed");
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002060
Bram Moolenaar9fecb462006-09-05 10:59:47 +00002061 return c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062}
2063
2064/*
2065 * Like vgetc(), but never return a NUL when called recursively, get a key
2066 * directly from the user (ignoring typeahead).
2067 */
2068 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002069safe_vgetc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070{
2071 int c;
2072
2073 c = vgetc();
2074 if (c == NUL)
2075 c = get_keystroke();
2076 return c;
2077}
2078
2079/*
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002080 * Like safe_vgetc(), but loop to handle K_IGNORE.
2081 * Also ignore scrollbar events.
Bram Moolenaar78aed952022-09-24 15:36:35 +01002082 * Does not handle bracketed paste - do not use the result for commands.
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002083 */
Bram Moolenaar78aed952022-09-24 15:36:35 +01002084 static int
2085plain_vgetc_nopaste(void)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002086{
2087 int c;
2088
2089 do
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002090 c = safe_vgetc();
Bram Moolenaar3a006592021-05-05 19:58:17 +02002091 while (c == K_IGNORE
2092 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
2093 || c == K_MOUSEMOVE);
Bram Moolenaar78aed952022-09-24 15:36:35 +01002094 return c;
2095}
2096
2097/*
2098 * Like safe_vgetc(), but loop to handle K_IGNORE.
2099 * Also ignore scrollbar events.
2100 */
2101 int
2102plain_vgetc(void)
2103{
2104 int c = plain_vgetc_nopaste();
Bram Moolenaarec2da362017-01-21 20:04:22 +01002105
2106 if (c == K_PS)
Bram Moolenaar30613902019-12-01 22:11:18 +01002107 // Only handle the first pasted character. Drop the rest, since we
2108 // don't know what to do with it.
Bram Moolenaarec2da362017-01-21 20:04:22 +01002109 c = bracketed_paste(PASTE_ONE_CHAR, FALSE, NULL);
2110
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002111 return c;
2112}
2113
2114/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 * Check if a character is available, such that vgetc() will not block.
2116 * If the next character is a special character or multi-byte, the returned
2117 * character is not valid!.
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02002118 * Returns NUL if no character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 */
2120 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002121vpeekc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122{
zeertzjq0f68e6c2022-04-05 13:17:01 +01002123 if (can_get_old_char())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 return old_char;
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01002125 return vgetorpeek(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126}
2127
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01002128#if defined(FEAT_TERMRESPONSE) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129/*
2130 * Like vpeekc(), but don't allow mapping. Do allow checking for terminal
2131 * codes.
2132 */
2133 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002134vpeekc_nomap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135{
2136 int c;
2137
2138 ++no_mapping;
2139 ++allow_keys;
2140 c = vpeekc();
2141 --no_mapping;
2142 --allow_keys;
2143 return c;
2144}
2145#endif
2146
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147/*
2148 * Check if any character is available, also half an escape sequence.
2149 * Trick: when no typeahead found, but there is something in the typeahead
2150 * buffer, it must be an ESC that is recognized as the start of a key code.
2151 */
2152 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002153vpeekc_any(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154{
2155 int c;
2156
2157 c = vpeekc();
2158 if (c == NUL && typebuf.tb_len > 0)
2159 c = ESC;
2160 return c;
2161}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162
2163/*
2164 * Call vpeekc() without causing anything to be mapped.
2165 * Return TRUE if a character is available, FALSE otherwise.
2166 */
2167 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002168char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169{
2170 int retval;
2171
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01002172#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01002173 // When test_override("char_avail", 1) was called pretend there is no
2174 // typeahead.
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01002175 if (disable_char_avail_for_testing)
2176 return FALSE;
2177#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 ++no_mapping;
2179 retval = vpeekc();
2180 --no_mapping;
2181 return (retval != NUL);
2182}
2183
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002184#if defined(FEAT_EVAL) || defined(PROTO)
2185/*
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002186 * "getchar()" and "getcharstr()" functions
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002187 */
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002188 static void
2189getchar_common(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002190{
2191 varnumber_T n;
2192 int error = FALSE;
2193
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002194 if (in_vim9script() && check_for_opt_bool_arg(argvars, 0) == FAIL)
2195 return;
2196
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002197#ifdef MESSAGE_QUEUE
2198 // vpeekc() used to check for messages, but that caused problems, invoking
2199 // a callback where it was not expected. Some plugins use getchar(1) in a
2200 // loop to await a message, therefore make sure we check for messages here.
2201 parse_queued_messages();
2202#endif
2203
Bram Moolenaar30613902019-12-01 22:11:18 +01002204 // Position the cursor. Needed after a message that ends in a space.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002205 windgoto(msg_row, msg_col);
2206
2207 ++no_mapping;
2208 ++allow_keys;
2209 for (;;)
2210 {
2211 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar30613902019-12-01 22:11:18 +01002212 // getchar(): blocking wait.
Bram Moolenaar78aed952022-09-24 15:36:35 +01002213 n = plain_vgetc_nopaste();
Bram Moolenaarc08cc722020-09-05 17:51:23 +02002214 else if (tv_get_bool_chk(&argvars[0], &error))
Bram Moolenaar30613902019-12-01 22:11:18 +01002215 // getchar(1): only check if char avail
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002216 n = vpeekc_any();
2217 else if (error || vpeekc_any() == NUL)
Bram Moolenaar30613902019-12-01 22:11:18 +01002218 // illegal argument or getchar(0) and no char avail: return zero
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002219 n = 0;
2220 else
Bram Moolenaar15183b42020-09-05 19:59:39 +02002221 // getchar(0) and char avail() != NUL: get a character.
2222 // Note that vpeekc_any() returns K_SPECIAL for K_IGNORE.
2223 n = safe_vgetc();
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002224
Bram Moolenaar15183b42020-09-05 19:59:39 +02002225 if (n == K_IGNORE || n == K_MOUSEMOVE
2226 || n == K_VER_SCROLLBAR || n == K_HOR_SCROLLBAR)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002227 continue;
2228 break;
2229 }
2230 --no_mapping;
2231 --allow_keys;
2232
2233 set_vim_var_nr(VV_MOUSE_WIN, 0);
2234 set_vim_var_nr(VV_MOUSE_WINID, 0);
2235 set_vim_var_nr(VV_MOUSE_LNUM, 0);
2236 set_vim_var_nr(VV_MOUSE_COL, 0);
2237
2238 rettv->vval.v_number = n;
zeertzjqad6c45f2022-02-20 19:05:10 +00002239 if (n != 0 && (IS_SPECIAL(n) || mod_mask != 0))
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002240 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002241 char_u temp[10]; // modifier: 3, mbyte-char: 6, NUL: 1
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002242 int i = 0;
2243
Bram Moolenaar30613902019-12-01 22:11:18 +01002244 // Turn a special key into three bytes, plus modifier.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002245 if (mod_mask != 0)
2246 {
2247 temp[i++] = K_SPECIAL;
2248 temp[i++] = KS_MODIFIER;
2249 temp[i++] = mod_mask;
2250 }
2251 if (IS_SPECIAL(n))
2252 {
2253 temp[i++] = K_SPECIAL;
2254 temp[i++] = K_SECOND(n);
2255 temp[i++] = K_THIRD(n);
2256 }
2257 else if (has_mbyte)
2258 i += (*mb_char2bytes)(n, temp + i);
2259 else
2260 temp[i++] = n;
2261 temp[i++] = NUL;
2262 rettv->v_type = VAR_STRING;
2263 rettv->vval.v_string = vim_strsave(temp);
2264
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002265 if (is_mouse_key(n))
2266 {
2267 int row = mouse_row;
2268 int col = mouse_col;
2269 win_T *win;
2270 linenr_T lnum;
2271 win_T *wp;
2272 int winnr = 1;
2273
2274 if (row >= 0 && col >= 0)
2275 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002276 // Find the window at the mouse coordinates and compute the
2277 // text position.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002278 win = mouse_find_win(&row, &col, FIND_POPUP);
2279 if (win == NULL)
2280 return;
2281 (void)mouse_comp_pos(win, &row, &col, &lnum, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002282#ifdef FEAT_PROP_POPUP
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002283 if (WIN_IS_POPUP(win))
2284 winnr = 0;
2285 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002286#endif
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002287 for (wp = firstwin; wp != win && wp != NULL;
2288 wp = wp->w_next)
2289 ++winnr;
2290 set_vim_var_nr(VV_MOUSE_WIN, winnr);
2291 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
2292 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
2293 set_vim_var_nr(VV_MOUSE_COL, col + 1);
2294 }
2295 }
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002296 }
2297}
2298
2299/*
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002300 * "getchar()" function
2301 */
2302 void
2303f_getchar(typval_T *argvars, typval_T *rettv)
2304{
2305 getchar_common(argvars, rettv);
2306}
2307
2308/*
2309 * "getcharstr()" function
2310 */
2311 void
2312f_getcharstr(typval_T *argvars, typval_T *rettv)
2313{
2314 getchar_common(argvars, rettv);
2315
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00002316 if (rettv->v_type != VAR_NUMBER)
2317 return;
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002318
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00002319 char_u temp[7]; // mbyte-char: 6, NUL: 1
2320 varnumber_T n = rettv->vval.v_number;
2321 int i = 0;
2322
2323 if (n != 0)
2324 {
2325 if (has_mbyte)
2326 i += (*mb_char2bytes)(n, temp + i);
2327 else
2328 temp[i++] = n;
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002329 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00002330 temp[i++] = NUL;
2331 rettv->v_type = VAR_STRING;
2332 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002333}
2334
2335/*
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002336 * "getcharmod()" function
2337 */
2338 void
2339f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
2340{
2341 rettv->vval.v_number = mod_mask;
2342}
2343#endif // FEAT_EVAL
2344
2345#if defined(MESSAGE_QUEUE) || defined(PROTO)
2346# define MAX_REPEAT_PARSE 8
2347
2348/*
2349 * Process messages that have been queued for netbeans or clientserver.
2350 * Also check if any jobs have ended.
h-east965d2ed2021-10-04 21:51:57 +01002351 * These functions can call arbitrary Vim script and should only be called when
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002352 * it is safe to do so.
2353 */
2354 void
2355parse_queued_messages(void)
2356{
Bram Moolenaar1cac7092019-10-22 21:54:31 +02002357 int old_curwin_id;
2358 int old_curbuf_fnum;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002359 int i;
2360 int save_may_garbage_collect = may_garbage_collect;
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002361 static int entered = 0;
Bram Moolenaard103ee72019-09-18 21:15:31 +02002362 int was_safe = get_was_safe_state();
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002363
2364 // Do not handle messages while redrawing, because it may cause buffers to
2365 // change or be wiped while they are being redrawn.
Bram Moolenaar4778b4d2020-11-04 11:03:12 +01002366 // Also bail out when parsing messages was explicitly disabled.
2367 if (updating_screen || dont_parse_messages)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002368 return;
2369
Bram Moolenaar1cac7092019-10-22 21:54:31 +02002370 // If memory allocation fails during startup we'll exit but curbuf or
2371 // curwin could be NULL.
2372 if (curbuf == NULL || curwin == NULL)
2373 return;
2374
2375 old_curbuf_fnum = curbuf->b_fnum;
2376 old_curwin_id = curwin->w_id;
2377
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002378 ++entered;
2379
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002380 // may_garbage_collect is set in main_loop() to do garbage collection when
2381 // blocking to wait on a character. We don't want that while parsing
2382 // messages, a callback may invoke vgetc() while lists and dicts are in use
2383 // in the call stack.
2384 may_garbage_collect = FALSE;
2385
2386 // Loop when a job ended, but don't keep looping forever.
2387 for (i = 0; i < MAX_REPEAT_PARSE; ++i)
2388 {
2389 // For Win32 mch_breakcheck() does not check for input, do it here.
Bram Moolenaar80a8d382020-05-03 22:57:32 +02002390# if (defined(MSWIN) || defined(__HAIKU__)) && defined(FEAT_JOB_CHANNEL)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002391 channel_handle_events(FALSE);
2392# endif
2393
2394# ifdef FEAT_NETBEANS_INTG
2395 // Process the queued netbeans messages.
2396 netbeans_parse_messages();
2397# endif
2398# ifdef FEAT_JOB_CHANNEL
2399 // Write any buffer lines still to be written.
2400 channel_write_any_lines();
2401
2402 // Process the messages queued on channels.
2403 channel_parse_messages();
2404# endif
2405# if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
2406 // Process the queued clientserver messages.
2407 server_parse_messages();
2408# endif
2409# ifdef FEAT_JOB_CHANNEL
2410 // Check if any jobs have ended. If so, repeat the above to handle
2411 // changes, e.g. stdin may have been closed.
2412 if (job_check_ended())
2413 continue;
2414# endif
2415# ifdef FEAT_TERMINAL
2416 free_unused_terminals();
2417# endif
Yee Cheng Chin4314e4f2022-10-08 13:50:05 +01002418
2419# ifdef FEAT_SOUND_MACOSX
2420 process_cfrunloop();
2421# endif
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002422# ifdef FEAT_SOUND_CANBERRA
2423 if (has_sound_callback_in_queue())
2424 invoke_sound_callback();
2425# endif
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002426#ifdef SIGUSR1
2427 if (got_sigusr1)
2428 {
2429 apply_autocmds(EVENT_SIGUSR1, NULL, NULL, FALSE, curbuf);
2430 got_sigusr1 = FALSE;
2431 }
2432#endif
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002433 break;
2434 }
2435
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002436 // When not nested we'll go back to waiting for a typed character. If it
2437 // was safe before then this triggers a SafeStateAgain autocommand event.
Bram Moolenaard103ee72019-09-18 21:15:31 +02002438 if (entered == 1 && was_safe)
Bram Moolenaar37d18072019-09-17 20:28:38 +02002439 may_trigger_safestateagain();
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002440
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002441 may_garbage_collect = save_may_garbage_collect;
2442
2443 // If the current window or buffer changed we need to bail out of the
2444 // waiting loop. E.g. when a job exit callback closes the terminal window.
2445 if (curwin->w_id != old_curwin_id || curbuf->b_fnum != old_curbuf_fnum)
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02002446 ins_char_typebuf(K_IGNORE, 0);
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002447
2448 --entered;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002449}
2450#endif
2451
2452
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002453typedef enum {
2454 map_result_fail, // failed, break loop
2455 map_result_get, // get a character from typeahead
2456 map_result_retry, // try to map again
2457 map_result_nomatch // no matching mapping, get char
2458} map_result_T;
2459
2460/*
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002461 * Check if the bytes at the start of the typeahead buffer are a character used
zeertzjqee446032022-04-30 15:02:22 +01002462 * in Insert mode completion. This includes the form with a CTRL modifier.
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002463 */
2464 static int
zeertzjqee446032022-04-30 15:02:22 +01002465at_ins_compl_key(void)
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002466{
2467 char_u *p = typebuf.tb_buf + typebuf.tb_off;
2468 int c = *p;
2469
2470 if (typebuf.tb_len > 3
Bram Moolenaard979d642022-03-04 14:51:06 +00002471 && (c == K_SPECIAL || c == CSI) // CSI is used by the GUI
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002472 && p[1] == KS_MODIFIER
2473 && (p[2] & MOD_MASK_CTRL))
2474 c = p[3] & 0x1f;
zeertzjqee446032022-04-30 15:02:22 +01002475 return (ctrl_x_mode_not_default() && vim_is_ctrl_x_key(c))
2476 || (compl_status_local() && (c == Ctrl_N || c == Ctrl_P));
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002477}
2478
2479/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002480 * Check if typebuf.tb_buf[] contains a modifier plus key that can be changed
Bram Moolenaar975a8802020-06-06 22:36:24 +02002481 * into just a key, apply that.
2482 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
2483 * + "max_offset"].
zeertzjq12e21e32022-04-27 11:58:01 +01002484 * Return the length of the replaced bytes, 0 if nothing changed, -1 for error.
Bram Moolenaar975a8802020-06-06 22:36:24 +02002485 */
2486 static int
2487check_simplify_modifier(int max_offset)
2488{
2489 int offset;
2490 char_u *tp;
2491
2492 for (offset = 0; offset < max_offset; ++offset)
2493 {
2494 if (offset + 3 >= typebuf.tb_len)
2495 break;
2496 tp = typebuf.tb_buf + typebuf.tb_off + offset;
zeertzjqc47b16a2022-09-05 13:05:29 +01002497 if ((tp[0] == K_SPECIAL || tp[0] == CSI) && tp[1] == KS_MODIFIER)
Bram Moolenaar975a8802020-06-06 22:36:24 +02002498 {
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002499 // A modifier was not used for a mapping, apply it to ASCII keys.
2500 // Shift would already have been applied.
Bram Moolenaar975a8802020-06-06 22:36:24 +02002501 int modifier = tp[2];
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002502 int c = tp[3];
2503 int new_c = merge_modifyOtherKeys(c, &modifier);
Bram Moolenaar975a8802020-06-06 22:36:24 +02002504
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002505 if (new_c != c)
Bram Moolenaar975a8802020-06-06 22:36:24 +02002506 {
2507 char_u new_string[MB_MAXBYTES];
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002508 int len;
Bram Moolenaar975a8802020-06-06 22:36:24 +02002509
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002510 if (offset == 0)
2511 {
2512 // At the start: remember the character and mod_mask before
2513 // merging, in some cases, e.g. at the hit-return prompt,
2514 // they are put back in the typeahead buffer.
2515 vgetc_char = c;
2516 vgetc_mod_mask = tp[2];
2517 }
zeertzjq12e21e32022-04-27 11:58:01 +01002518 if (IS_SPECIAL(new_c))
2519 {
2520 new_string[0] = K_SPECIAL;
2521 new_string[1] = K_SECOND(new_c);
2522 new_string[2] = K_THIRD(new_c);
2523 len = 3;
2524 }
2525 else
2526 len = mb_char2bytes(new_c, new_string);
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002527 if (modifier == 0)
2528 {
2529 if (put_string_in_typebuf(offset, 4, new_string, len,
zeertzjqfc78a032022-04-26 22:11:38 +01002530 NULL, 0, NULL) == FAIL)
RestorerZ68ebcee2023-05-31 17:12:14 +01002531 return -1;
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002532 }
2533 else
2534 {
2535 tp[2] = modifier;
2536 if (put_string_in_typebuf(offset + 3, 1, new_string, len,
zeertzjqfc78a032022-04-26 22:11:38 +01002537 NULL, 0, NULL) == FAIL)
RestorerZ68ebcee2023-05-31 17:12:14 +01002538 return -1;
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002539 }
Bram Moolenaar975a8802020-06-06 22:36:24 +02002540 return len;
2541 }
2542 }
2543 }
2544 return 0;
2545}
2546
2547/*
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00002548 * Return TRUE if the terminal sends modifiers with various keys. This is when
2549 * modifyOtherKeys level 2 is enabled or the kitty keyboard protocol is
2550 * enabled.
2551 */
David Leadbeater67ec6552023-10-26 22:00:34 +02002552 int
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00002553key_protocol_enabled(void)
2554{
Bram Moolenaarc255b782022-11-26 19:16:48 +00002555 // If xterm has responded to XTQMODKEYS it overrules seenModifyOtherKeys.
2556 int using_mok = modify_otherkeys_state != MOKS_INITIAL
2557 ? modify_otherkeys_state == MOKS_ENABLED
2558 : seenModifyOtherKeys;
2559 return using_mok || kitty_protocol_state == KKPS_ENABLED;
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00002560}
2561
2562/*
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002563 * Handle mappings in the typeahead buffer.
2564 * - When something was mapped, return map_result_retry for recursive mappings.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002565 * - When nothing mapped and typeahead has a character: return map_result_get.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002566 * - When there is no match yet, return map_result_nomatch, need to get more
2567 * typeahead.
zeertzjq12e21e32022-04-27 11:58:01 +01002568 * - On failure (out of memory) return map_result_fail.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002569 */
2570 static int
2571handle_mapping(
2572 int *keylenp,
2573 int *timedout,
2574 int *mapdepth)
2575{
2576 mapblock_T *mp = NULL;
2577 mapblock_T *mp2;
2578 mapblock_T *mp_match;
2579 int mp_match_len = 0;
2580 int max_mlen = 0;
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002581 int want_termcode = 0; // 1 if termcode expected after max_mlen
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002582 int tb_c1;
2583 int mlen;
2584#ifdef FEAT_LANGMAP
2585 int nolmaplen;
2586#endif
2587 int keylen = *keylenp;
2588 int i;
2589 int local_State = get_real_state();
Bram Moolenaar1fc34222022-03-03 13:56:24 +00002590 int is_plug_map = FALSE;
2591
zeertzjqbb404f52022-07-23 06:25:29 +01002592 // If typeahead starts with <Plug> then remap, even for a "noremap" mapping.
Bram Moolenaaraf043e12022-07-02 12:08:16 +01002593 if (typebuf.tb_len >= 3
2594 && typebuf.tb_buf[typebuf.tb_off] == K_SPECIAL
Bram Moolenaar1fc34222022-03-03 13:56:24 +00002595 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_EXTRA
2596 && typebuf.tb_buf[typebuf.tb_off + 2] == KE_PLUG)
2597 is_plug_map = TRUE;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002598
2599 /*
2600 * Check for a mappable key sequence.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002601 * Walk through one maphash[] list until we find an entry that matches.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002602 *
2603 * Don't look for mappings if:
2604 * - no_mapping set: mapping disabled (e.g. for CTRL-V)
2605 * - maphash_valid not set: no mappings present.
2606 * - typebuf.tb_buf[typebuf.tb_off] should not be remapped
2607 * - in insert or cmdline mode and 'paste' option set
Bram Moolenaareda35f72019-08-03 14:59:44 +02002608 * - waiting for "hit return to continue" and CR or SPACE typed
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002609 * - waiting for a char with --more--
2610 * - in Ctrl-X mode, and we get a valid char for that mode
2611 */
2612 tb_c1 = typebuf.tb_buf[typebuf.tb_off];
2613 if (no_mapping == 0 && is_maphash_valid()
2614 && (no_zero_mapping == 0 || tb_c1 != '0')
Bram Moolenaar1fc34222022-03-03 13:56:24 +00002615 && (typebuf.tb_maplen == 0 || is_plug_map
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002616 || (p_remap
2617 && (typebuf.tb_noremap[typebuf.tb_off]
2618 & (RM_NONE|RM_ABBR)) == 0))
Bram Moolenaar24959102022-05-07 20:01:16 +01002619 && !(p_paste && (State & (MODE_INSERT | MODE_CMDLINE)))
2620 && !(State == MODE_HITRETURN && (tb_c1 == CAR || tb_c1 == ' '))
2621 && State != MODE_ASKMORE
2622 && State != MODE_CONFIRM
zeertzjqee446032022-04-30 15:02:22 +01002623 && !at_ins_compl_key())
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002624 {
Christopher Plewright03193062022-11-22 12:58:27 +00002625#ifdef FEAT_GUI
2626 if (gui.in_use && tb_c1 == CSI && typebuf.tb_len >= 2
2627 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_MODIFIER)
Bram Moolenaarc9983702020-05-28 21:03:53 +02002628 {
2629 // The GUI code sends CSI KS_MODIFIER {flags}, but mappings expect
2630 // K_SPECIAL KS_MODIFIER {flags}.
2631 tb_c1 = K_SPECIAL;
2632 }
2633#endif
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002634#ifdef FEAT_LANGMAP
2635 if (tb_c1 == K_SPECIAL)
2636 nolmaplen = 2;
2637 else
2638 {
Bram Moolenaar24959102022-05-07 20:01:16 +01002639 LANGMAP_ADJUST(tb_c1, (State & (MODE_CMDLINE | MODE_INSERT)) == 0
2640 && get_real_state() != MODE_SELECT);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002641 nolmaplen = 0;
2642 }
2643#endif
2644 // First try buffer-local mappings.
2645 mp = get_buf_maphash_list(local_State, tb_c1);
2646 mp2 = get_maphash_list(local_State, tb_c1);
2647 if (mp == NULL)
2648 {
2649 // There are no buffer-local mappings.
2650 mp = mp2;
2651 mp2 = NULL;
2652 }
Bram Moolenaareda35f72019-08-03 14:59:44 +02002653
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002654 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002655 * Loop until a partly matching mapping is found or all (local)
2656 * mappings have been checked.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002657 * The longest full match is remembered in "mp_match".
Bram Moolenaareda35f72019-08-03 14:59:44 +02002658 * A full match is only accepted if there is no partly match, so "aa"
2659 * and "aaa" can both be mapped.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002660 */
2661 mp_match = NULL;
2662 mp_match_len = 0;
2663 for ( ; mp != NULL;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002664 mp->m_next == NULL ? (mp = mp2, mp2 = NULL) : (mp = mp->m_next))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002665 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002666 // Only consider an entry if the first character matches and it is
2667 // for the current state.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002668 // Skip ":lmap" mappings if keys were mapped.
2669 if (mp->m_keys[0] == tb_c1
2670 && (mp->m_mode & local_State)
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00002671 && !(mp->m_simplified && key_protocol_enabled()
Bram Moolenaar46cd43b2020-06-04 22:22:11 +02002672 && typebuf.tb_maplen == 0)
Christopher Plewright4c366782022-10-20 13:11:15 +01002673 && ((mp->m_mode & MODE_LANGMAP) == 0
2674 || typebuf.tb_maplen == 0))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002675 {
2676#ifdef FEAT_LANGMAP
2677 int nomap = nolmaplen;
zeertzjq49660f52022-10-20 17:59:38 +01002678 int modifiers = 0;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002679#endif
2680 // find the match length of this mapping
2681 for (mlen = 1; mlen < typebuf.tb_len; ++mlen)
2682 {
zeertzjq49660f52022-10-20 17:59:38 +01002683 int c2 = typebuf.tb_buf[typebuf.tb_off + mlen];
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002684#ifdef FEAT_LANGMAP
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002685 if (nomap > 0)
zeertzjq49660f52022-10-20 17:59:38 +01002686 {
2687 if (nomap == 2 && c2 == KS_MODIFIER)
2688 modifiers = 1;
2689 else if (nomap == 1 && modifiers == 1)
2690 modifiers = c2;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002691 --nomap;
zeertzjq49660f52022-10-20 17:59:38 +01002692 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002693 else
zeertzjq49660f52022-10-20 17:59:38 +01002694 {
2695 if (c2 == K_SPECIAL)
2696 nomap = 2;
2697 else if (merge_modifyOtherKeys(c2, &modifiers) == c2)
2698 // Only apply 'langmap' if merging modifiers into
2699 // the key will not result in another character,
2700 // so that 'langmap' behaves consistently in
2701 // different terminals and GUIs.
2702 LANGMAP_ADJUST(c2, TRUE);
2703 modifiers = 0;
2704 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002705#endif
zeertzjq49660f52022-10-20 17:59:38 +01002706 if (mp->m_keys[mlen] != c2)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002707 break;
2708 }
2709
Bram Moolenaareda35f72019-08-03 14:59:44 +02002710 // Don't allow mapping the first byte(s) of a multi-byte char.
2711 // Happens when mapping <M-a> and then changing 'encoding'.
2712 // Beware that 0x80 is escaped.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002713 {
2714 char_u *p1 = mp->m_keys;
2715 char_u *p2 = mb_unescape(&p1);
2716
2717 if (has_mbyte && p2 != NULL
Bram Moolenaar1614a142019-10-06 22:00:13 +02002718 && MB_BYTE2LEN(tb_c1) > mb_ptr2len(p2))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002719 mlen = 0;
2720 }
2721
2722 // Check an entry whether it matches.
2723 // - Full match: mlen == keylen
2724 // - Partly match: mlen == typebuf.tb_len
2725 keylen = mp->m_keylen;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002726 if (mlen == keylen || (mlen == typebuf.tb_len
2727 && typebuf.tb_len < keylen))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002728 {
2729 char_u *s;
2730 int n;
2731
Bram Moolenaareda35f72019-08-03 14:59:44 +02002732 // If only script-local mappings are allowed, check if the
2733 // mapping starts with K_SNR.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002734 s = typebuf.tb_noremap + typebuf.tb_off;
2735 if (*s == RM_SCRIPT
2736 && (mp->m_keys[0] != K_SPECIAL
2737 || mp->m_keys[1] != KS_EXTRA
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01002738 || mp->m_keys[2] != KE_SNR))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002739 continue;
2740
Bram Moolenaareda35f72019-08-03 14:59:44 +02002741 // If one of the typed keys cannot be remapped, skip the
2742 // entry.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002743 for (n = mlen; --n >= 0; )
2744 if (*s++ & (RM_NONE|RM_ABBR))
2745 break;
Bram Moolenaar1fc34222022-03-03 13:56:24 +00002746 if (!is_plug_map && n >= 0)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002747 continue;
2748
2749 if (keylen > typebuf.tb_len)
2750 {
2751 if (!*timedout && !(mp_match != NULL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002752 && mp_match->m_nowait))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002753 {
2754 // break at a partly match
2755 keylen = KEYLEN_PART_MAP;
2756 break;
2757 }
2758 }
2759 else if (keylen > mp_match_len)
2760 {
2761 // found a longer match
2762 mp_match = mp;
2763 mp_match_len = keylen;
2764 }
2765 }
2766 else
Bram Moolenaareda35f72019-08-03 14:59:44 +02002767 // No match; may have to check for termcode at next
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002768 // character. If the first character that didn't match is
2769 // K_SPECIAL then check for a termcode. This isn't perfect
2770 // but should work in most cases.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002771 if (max_mlen < mlen)
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002772 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002773 max_mlen = mlen;
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002774 want_termcode = mp->m_keys[mlen] == K_SPECIAL;
2775 }
2776 else if (max_mlen == mlen && mp->m_keys[mlen] == K_SPECIAL)
2777 want_termcode = 1;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002778 }
2779 }
2780
Bram Moolenaareda35f72019-08-03 14:59:44 +02002781 // If no partly match found, use the longest full match.
Bram Moolenaar196c3852022-03-04 19:22:36 +00002782 if (keylen != KEYLEN_PART_MAP && mp_match != NULL)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002783 {
2784 mp = mp_match;
2785 keylen = mp_match_len;
2786 }
2787 }
2788
2789 /*
2790 * Check for match with 'pastetoggle'
2791 */
Bram Moolenaar24959102022-05-07 20:01:16 +01002792 if (*p_pt != NUL && mp == NULL && (State & (MODE_INSERT | MODE_NORMAL)))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002793 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002794 for (mlen = 0; mlen < typebuf.tb_len && p_pt[mlen]; ++mlen)
2795 if (p_pt[mlen] != typebuf.tb_buf[typebuf.tb_off + mlen])
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002796 break;
2797 if (p_pt[mlen] == NUL) // match
2798 {
2799 // write chars to script file(s)
2800 if (mlen > typebuf.tb_maplen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002801 gotchars(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_maplen,
2802 mlen - typebuf.tb_maplen);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002803
2804 del_typebuf(mlen, 0); // remove the chars
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002805 set_option_value_give_err((char_u *)"paste",
2806 (long)!p_paste, NULL, 0);
Bram Moolenaar24959102022-05-07 20:01:16 +01002807 if (!(State & MODE_INSERT))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002808 {
2809 msg_col = 0;
2810 msg_row = Rows - 1;
2811 msg_clr_eos(); // clear ruler
2812 }
2813 status_redraw_all();
2814 redraw_statuslines();
2815 showmode();
2816 setcursor();
2817 *keylenp = keylen;
2818 return map_result_retry;
2819 }
2820 // Need more chars for partly match.
2821 if (mlen == typebuf.tb_len)
2822 keylen = KEYLEN_PART_KEY;
2823 else if (max_mlen < mlen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002824 // no match, may have to check for termcode at next character
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002825 max_mlen = mlen + 1;
2826 }
2827
Bram Moolenaarbbf84e22022-03-12 13:48:39 +00002828 // May check for a terminal code when there is no mapping or only a partial
2829 // mapping. Also check if there is a full mapping with <Esc>, unless timed
2830 // out, since that is nearly always a partial match with a terminal code.
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002831 if ((mp == NULL || max_mlen + want_termcode > mp_match_len
Bram Moolenaarbbf84e22022-03-12 13:48:39 +00002832 || (mp_match_len == 1 && *mp->m_keys == ESC && !*timedout))
2833 && keylen != KEYLEN_PART_MAP)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002834 {
2835 int save_keylen = keylen;
2836
2837 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002838 * When no matching mapping found or found a non-matching mapping that
2839 * matches at least what the matching mapping matched:
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002840 * Check if we have a terminal code, when:
Bram Moolenaareda35f72019-08-03 14:59:44 +02002841 * - mapping is allowed,
2842 * - keys have not been mapped,
2843 * - and not an ESC sequence, not in insert mode or p_ek is on,
2844 * - and when not timed out,
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002845 */
zeertzjq68a573c2022-04-28 14:10:01 +01002846 if (no_mapping == 0 || allow_keys != 0)
2847 {
2848 if ((typebuf.tb_maplen == 0
RestorerZ68ebcee2023-05-31 17:12:14 +01002849 || (p_remap && typebuf.tb_noremap[
Bram Moolenaareda35f72019-08-03 14:59:44 +02002850 typebuf.tb_off] == RM_YES))
RestorerZ68ebcee2023-05-31 17:12:14 +01002851 && !*timedout)
zeertzjq68a573c2022-04-28 14:10:01 +01002852 keylen = check_termcode(max_mlen + 1, NULL, 0, NULL);
2853 else
2854 keylen = 0;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002855
Bram Moolenaar0684e362020-12-03 19:54:42 +01002856 // If no termcode matched but 'pastetoggle' matched partially
2857 // it's like an incomplete key sequence.
zeertzjq68a573c2022-04-28 14:10:01 +01002858 if (keylen == 0 && save_keylen == KEYLEN_PART_KEY && !*timedout)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002859 keylen = KEYLEN_PART_KEY;
2860
Bram Moolenaar975a8802020-06-06 22:36:24 +02002861 // If no termcode matched, try to include the modifier into the
zeertzjqa3f83fe2021-11-22 12:47:39 +00002862 // key. This is for when modifyOtherKeys is working.
Bram Moolenaarc896adb2022-11-19 19:02:40 +00002863#ifdef FEAT_TERMINAL
2864 check_no_reduce_keys(); // may update the no_reduce_keys flag
2865#endif
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002866 if (keylen == 0 && !no_reduce_keys)
zeertzjq12e21e32022-04-27 11:58:01 +01002867 {
Bram Moolenaar975a8802020-06-06 22:36:24 +02002868 keylen = check_simplify_modifier(max_mlen + 1);
zeertzjq12e21e32022-04-27 11:58:01 +01002869 if (keylen < 0)
2870 // ins_typebuf() failed
2871 return map_result_fail;
2872 }
Bram Moolenaar975a8802020-06-06 22:36:24 +02002873
Bram Moolenaareda35f72019-08-03 14:59:44 +02002874 // When getting a partial match, but the last characters were not
2875 // typed, don't wait for a typed character to complete the
2876 // termcode. This helps a lot when a ":normal" command ends in an
2877 // ESC.
2878 if (keylen < 0 && typebuf.tb_len == typebuf.tb_maplen)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002879 keylen = 0;
2880 }
2881 else
2882 keylen = 0;
2883 if (keylen == 0) // no matching terminal code
2884 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002885#ifdef AMIGA
2886 // check for window bounds report
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002887 if (typebuf.tb_maplen == 0 && (typebuf.tb_buf[
Bram Moolenaareda35f72019-08-03 14:59:44 +02002888 typebuf.tb_off] & 0xff) == CSI)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002889 {
2890 char_u *s;
2891
2892 for (s = typebuf.tb_buf + typebuf.tb_off + 1;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002893 s < typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len
2894 && (VIM_ISDIGIT(*s) || *s == ';' || *s == ' ');
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002895 ++s)
2896 ;
2897 if (*s == 'r' || *s == '|') // found one
2898 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002899 del_typebuf(
2900 (int)(s + 1 - (typebuf.tb_buf + typebuf.tb_off)), 0);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002901 // get size and redraw screen
2902 shell_resized();
2903 *keylenp = keylen;
2904 return map_result_retry;
2905 }
2906 if (*s == NUL) // need more characters
2907 keylen = KEYLEN_PART_KEY;
2908 }
2909 if (keylen >= 0)
2910#endif
Bram Moolenaareda35f72019-08-03 14:59:44 +02002911 // When there was a matching mapping and no termcode could be
2912 // replaced after another one, use that mapping (loop around).
2913 // If there was no mapping at all use the character from the
2914 // typeahead buffer right here.
2915 if (mp == NULL)
2916 {
2917 *keylenp = keylen;
Bram Moolenaar52797ba2021-12-16 14:45:13 +00002918 return map_result_get; // get character from typeahead
Bram Moolenaareda35f72019-08-03 14:59:44 +02002919 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002920 }
2921
2922 if (keylen > 0) // full matching terminal code
2923 {
2924#if defined(FEAT_GUI) && defined(FEAT_MENU)
2925 if (typebuf.tb_len >= 2
Bram Moolenaareda35f72019-08-03 14:59:44 +02002926 && typebuf.tb_buf[typebuf.tb_off] == K_SPECIAL
2927 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_MENU)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002928 {
2929 int idx;
2930
Bram Moolenaareda35f72019-08-03 14:59:44 +02002931 // Using a menu may cause a break in undo! It's like using
2932 // gotchars(), but without recording or writing to a script
2933 // file.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002934 may_sync_undo();
2935 del_typebuf(3, 0);
2936 idx = get_menu_index(current_menu, local_State);
2937 if (idx != MENU_INDEX_INVALID)
2938 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002939 // In Select mode and a Visual mode menu is used: Switch
2940 // to Visual mode temporarily. Append K_SELECT to switch
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002941 // back to Select mode.
2942 if (VIsual_active && VIsual_select
Bram Moolenaar24959102022-05-07 20:01:16 +01002943 && (current_menu->modes & MODE_VISUAL))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002944 {
2945 VIsual_select = FALSE;
2946 (void)ins_typebuf(K_SELECT_STRING,
Bram Moolenaareda35f72019-08-03 14:59:44 +02002947 REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002948 }
2949 ins_typebuf(current_menu->strings[idx],
2950 current_menu->noremap[idx],
Bram Moolenaareda35f72019-08-03 14:59:44 +02002951 0, TRUE, current_menu->silent[idx]);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002952 }
2953 }
2954#endif // FEAT_GUI && FEAT_MENU
2955 *keylenp = keylen;
2956 return map_result_retry; // try mapping again
2957 }
2958
Bram Moolenaareda35f72019-08-03 14:59:44 +02002959 // Partial match: get some more characters. When a matching mapping
2960 // was found use that one.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002961 if (mp == NULL || keylen < 0)
2962 keylen = KEYLEN_PART_KEY;
2963 else
2964 keylen = mp_match_len;
2965 }
2966
2967 /*
2968 * complete match
2969 */
2970 if (keylen >= 0 && keylen <= typebuf.tb_len)
2971 {
2972 char_u *map_str;
2973
2974#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002975 int save_m_expr;
2976 int save_m_noremap;
2977 int save_m_silent;
2978 char_u *save_m_keys;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002979#else
2980# define save_m_noremap mp->m_noremap
2981# define save_m_silent mp->m_silent
2982#endif
2983
2984 // write chars to script file(s)
2985 if (keylen > typebuf.tb_maplen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002986 gotchars(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_maplen,
2987 keylen - typebuf.tb_maplen);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002988
2989 cmd_silent = (typebuf.tb_silent > 0);
2990 del_typebuf(keylen, 0); // remove the mapped keys
2991
2992 /*
2993 * Put the replacement string in front of mapstr.
2994 * The depth check catches ":map x y" and ":map y x".
2995 */
2996 if (++*mapdepth >= p_mmd)
2997 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00002998 emsg(_(e_recursive_mapping));
Bram Moolenaar24959102022-05-07 20:01:16 +01002999 if (State & MODE_CMDLINE)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003000 redrawcmdline();
3001 else
3002 setcursor();
3003 flush_buffers(FLUSH_MINIMAL);
Bram Moolenaar30613902019-12-01 22:11:18 +01003004 *mapdepth = 0; // for next one
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003005 *keylenp = keylen;
3006 return map_result_fail;
3007 }
3008
3009 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02003010 * In Select mode and a Visual mode mapping is used: Switch to Visual
3011 * mode temporarily. Append K_SELECT to switch back to Select mode.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003012 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003013 if (VIsual_active && VIsual_select && (mp->m_mode & MODE_VISUAL))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003014 {
3015 VIsual_select = FALSE;
Bram Moolenaareda35f72019-08-03 14:59:44 +02003016 (void)ins_typebuf(K_SELECT_STRING, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003017 }
3018
3019#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02003020 // Copy the values from *mp that are used, because evaluating the
3021 // expression may invoke a function that redefines the mapping, thereby
3022 // making *mp invalid.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003023 save_m_expr = mp->m_expr;
3024 save_m_noremap = mp->m_noremap;
3025 save_m_silent = mp->m_silent;
3026 save_m_keys = NULL; // only saved when needed
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003027
3028 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02003029 * Handle ":map <expr>": evaluate the {rhs} as an expression. Also
3030 * save and restore the command line for "normal :".
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003031 */
3032 if (mp->m_expr)
3033 {
3034 int save_vgetc_busy = vgetc_busy;
3035 int save_may_garbage_collect = may_garbage_collect;
Bram Moolenaar4ebe0e62019-11-22 20:55:40 +01003036 int was_screen_col = screen_cur_col;
3037 int was_screen_row = screen_cur_row;
Bram Moolenaar74a0a5b2022-02-10 14:07:41 +00003038 int prev_did_emsg = did_emsg;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003039
3040 vgetc_busy = 0;
3041 may_garbage_collect = FALSE;
3042
3043 save_m_keys = vim_strsave(mp->m_keys);
Bram Moolenaar19db9e62022-01-11 11:58:19 +00003044 map_str = eval_map_expr(mp, NUL);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003045
Bram Moolenaar4ebe0e62019-11-22 20:55:40 +01003046 // The mapping may do anything, but we expect it to take care of
3047 // redrawing. Do put the cursor back where it was.
3048 windgoto(was_screen_row, was_screen_col);
3049 out_flush();
3050
Bram Moolenaar74a0a5b2022-02-10 14:07:41 +00003051 // If an error was displayed and the expression returns an empty
3052 // string, generate a <Nop> to allow for a redraw.
3053 if (prev_did_emsg != did_emsg
3054 && (map_str == NULL || *map_str == NUL))
3055 {
3056 char_u buf[4];
3057
3058 vim_free(map_str);
3059 buf[0] = K_SPECIAL;
3060 buf[1] = KS_EXTRA;
3061 buf[2] = KE_IGNORE;
3062 buf[3] = NUL;
3063 map_str = vim_strsave(buf);
Bram Moolenaar24959102022-05-07 20:01:16 +01003064 if (State & MODE_CMDLINE)
Bram Moolenaar74a0a5b2022-02-10 14:07:41 +00003065 {
3066 // redraw the command below the error
3067 msg_didout = TRUE;
3068 if (msg_row < cmdline_row)
3069 msg_row = cmdline_row;
3070 redrawcmd();
3071 }
3072 }
3073
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003074 vgetc_busy = save_vgetc_busy;
3075 may_garbage_collect = save_may_garbage_collect;
3076 }
3077 else
3078#endif
3079 map_str = mp->m_str;
3080
3081 /*
3082 * Insert the 'to' part in the typebuf.tb_buf.
Bram Moolenaareda35f72019-08-03 14:59:44 +02003083 * If 'from' field is the same as the start of the 'to' field, don't
3084 * remap the first character (but do allow abbreviations).
3085 * If m_noremap is set, don't remap the whole 'to' part.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003086 */
3087 if (map_str == NULL)
3088 i = FAIL;
3089 else
3090 {
3091 int noremap;
3092
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01003093#ifdef FEAT_EVAL
3094 last_used_map = mp;
3095 last_used_sid = -1;
3096#endif
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003097 if (save_m_noremap != REMAP_YES)
3098 noremap = save_m_noremap;
3099 else if (
3100#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02003101 STRNCMP(map_str, save_m_keys != NULL ? save_m_keys : mp->m_keys,
3102 (size_t)keylen)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003103#else
3104 STRNCMP(map_str, mp->m_keys, (size_t)keylen)
3105#endif
3106 != 0)
3107 noremap = REMAP_YES;
3108 else
3109 noremap = REMAP_SKIP;
3110 i = ins_typebuf(map_str, noremap,
3111 0, TRUE, cmd_silent || save_m_silent);
3112#ifdef FEAT_EVAL
3113 if (save_m_expr)
3114 vim_free(map_str);
3115#endif
3116 }
3117#ifdef FEAT_EVAL
3118 vim_free(save_m_keys);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003119#endif
3120 *keylenp = keylen;
3121 if (i == FAIL)
3122 return map_result_fail;
3123 return map_result_retry;
3124 }
3125
3126 *keylenp = keylen;
3127 return map_result_nomatch;
3128}
3129
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003130/*
3131 * unget one character (can only be done once!)
zeertzjq6d4e7252022-04-07 13:58:04 +01003132 * If the character was stuffed, vgetc() will get it next time it is called.
zeertzjq0f68e6c2022-04-05 13:17:01 +01003133 * Otherwise vgetc() will only get it when the stuff buffer is empty.
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003134 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003136vungetc(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137{
3138 old_char = c;
3139 old_mod_mask = mod_mask;
Bram Moolenaarb8978712013-03-16 21:42:16 +01003140 old_mouse_row = mouse_row;
3141 old_mouse_col = mouse_col;
zeertzjq0f68e6c2022-04-05 13:17:01 +01003142 old_KeyStuffed = KeyStuffed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143}
3144
3145/*
zeertzjq6d4e7252022-04-07 13:58:04 +01003146 * When peeking and not getting a character, reg_executing cannot be cleared
3147 * yet, so set a flag to clear it later.
3148 */
3149 static void
3150check_end_reg_executing(int advance)
3151{
3152 if (reg_executing != 0 && (typebuf.tb_maplen == 0
3153 || pending_end_reg_executing))
3154 {
3155 if (advance)
3156 {
3157 reg_executing = 0;
3158 pending_end_reg_executing = FALSE;
3159 }
3160 else
3161 pending_end_reg_executing = TRUE;
3162 }
3163
3164}
3165
3166/*
Bram Moolenaar32526b32019-01-19 17:43:09 +01003167 * Get a byte:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 * 1. from the stuffbuffer
3169 * This is used for abbreviated commands like "D" -> "d$".
3170 * Also used to redo a command for ".".
3171 * 2. from the typeahead buffer
3172 * Stores text obtained previously but not used yet.
3173 * Also stores the result of mappings.
3174 * Also used for the ":normal" command.
3175 * 3. from the user
3176 * This may do a blocking wait if "advance" is TRUE.
3177 *
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003178 * if "advance" is TRUE (vgetc()):
Bram Moolenaard29459b2016-08-26 22:29:11 +02003179 * Really get the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 * KeyTyped is set to TRUE in the case the user typed the key.
3181 * KeyStuffed is TRUE if the character comes from the stuff buffer.
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003182 * if "advance" is FALSE (vpeekc()):
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02003183 * Just look whether there is a character available.
3184 * Return NUL if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 *
3186 * When "no_mapping" is zero, checks for mappings in the current mode.
3187 * Only returns one byte (of a multi-byte character).
3188 * K_SPECIAL and CSI may be escaped, need to get two more bytes then.
3189 */
3190 static int
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003191vgetorpeek(int advance)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192{
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003193 int c;
Bram Moolenaarb8329db2022-07-06 13:31:28 +01003194 int timedout = FALSE; // waited for more than 'timeoutlen'
3195 // for mapping to complete or
3196 // 'ttimeoutlen' for complete key code
Bram Moolenaar30613902019-12-01 22:11:18 +01003197 int mapdepth = 0; // check for recursive mapping
3198 int mode_deleted = FALSE; // set when mode has been deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 int new_wcol, new_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003201 int shape_changed = FALSE; // adjusted cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202#endif
3203 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 int old_wcol, old_wrow;
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003205 int wait_tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206
3207 /*
3208 * This function doesn't work very well when called recursively. This may
3209 * happen though, because of:
3210 * 1. The call to add_to_showcmd(). char_avail() is then used to check if
3211 * there is a character available, which calls this function. In that
3212 * case we must return NUL, to indicate no character is available.
3213 * 2. A GUI callback function writes to the screen, causing a
3214 * wait_return().
3215 * Using ":normal" can also do this, but it saves the typeahead buffer,
3216 * thus it should be OK. But don't get a key from the user then.
3217 */
Bram Moolenaare2c38102016-01-31 14:55:40 +01003218 if (vgetc_busy > 0 && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 return NUL;
3220
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003221 ++vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222
3223 if (advance)
Bram Moolenaar8d696372022-08-21 10:40:07 +01003224 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 KeyStuffed = FALSE;
Bram Moolenaar8d696372022-08-21 10:40:07 +01003226 typebuf_was_empty = FALSE;
3227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228
3229 init_typebuf();
3230 start_stuff();
zeertzjq6d4e7252022-04-07 13:58:04 +01003231 check_end_reg_executing(advance);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 do
3233 {
3234/*
3235 * get a character: 1. from the stuffbuffer
3236 */
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003237 if (typeahead_char != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 {
3239 c = typeahead_char;
3240 if (advance)
3241 typeahead_char = 0;
3242 }
3243 else
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01003244 c = read_readbuffers(advance);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 if (c != NUL && !got_int)
3246 {
3247 if (advance)
3248 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003249 // KeyTyped = FALSE; When the command that stuffed something
3250 // was typed, behave like the stuffed command was typed.
3251 // needed for CTRL-W CTRL-] to open a fold, for example.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 KeyStuffed = TRUE;
3253 }
3254 if (typebuf.tb_no_abbr_cnt == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003255 typebuf.tb_no_abbr_cnt = 1; // no abbreviations now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 }
3257 else
3258 {
3259 /*
3260 * Loop until we either find a matching mapped key, or we
3261 * are sure that it is not a mapped key.
3262 * If a mapped key sequence is found we go back to the start to
3263 * try re-mapping.
3264 */
3265 for (;;)
3266 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003267 long wait_time;
3268 int keylen = 0;
Bram Moolenaareda35f72019-08-03 14:59:44 +02003269 int showcmd_idx;
zeertzjq6d4e7252022-04-07 13:58:04 +01003270 check_end_reg_executing(advance);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 /*
3272 * ui_breakcheck() is slow, don't use it too often when
3273 * inside a mapping. But call it each time for typed
3274 * characters.
3275 */
3276 if (typebuf.tb_maplen)
3277 line_breakcheck();
3278 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003279 ui_breakcheck(); // check for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 if (got_int)
3281 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003282 // flush all input
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003283 c = inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 0L);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003284
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 /*
3286 * If inchar() returns TRUE (script file was active) or we
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003287 * are inside a mapping, get out of Insert mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 * Otherwise we behave like having gotten a CTRL-C.
3289 * As a result typing CTRL-C in insert mode will
3290 * really insert a CTRL-C.
3291 */
3292 if ((c || typebuf.tb_maplen)
Bram Moolenaar24959102022-05-07 20:01:16 +01003293 && (State & (MODE_INSERT | MODE_CMDLINE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 c = ESC;
3295 else
3296 c = Ctrl_C;
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02003297 flush_buffers(FLUSH_INPUT); // flush all typeahead
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298
Bram Moolenaard9dfd572006-10-03 13:36:13 +00003299 if (advance)
3300 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003301 // Also record this character, it might be needed to
3302 // get out of Insert mode.
Bram Moolenaard9dfd572006-10-03 13:36:13 +00003303 *typebuf.tb_buf = c;
3304 gotchars(typebuf.tb_buf, 1);
3305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 cmd_silent = FALSE;
3307
3308 break;
3309 }
3310 else if (typebuf.tb_len > 0)
3311 {
3312 /*
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003313 * Check for a mapping in "typebuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 */
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003315 map_result_T result = handle_mapping(
3316 &keylen, &timedout, &mapdepth);
3317
3318 if (result == map_result_retry)
3319 // try mapping again
3320 continue;
3321 if (result == map_result_fail)
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003322 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003323 // failed, use the outer loop
3324 c = -1;
3325 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003327 if (result == map_result_get)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329/*
3330 * get a character: 2. from the typeahead buffer
3331 */
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003332 c = typebuf.tb_buf[typebuf.tb_off];
RestorerZ68ebcee2023-05-31 17:12:14 +01003333 if (advance) // remove chars from typebuf
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003334 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003335 cmd_silent = (typebuf.tb_silent > 0);
3336 if (typebuf.tb_maplen > 0)
3337 KeyTyped = FALSE;
3338 else
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003339 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003340 KeyTyped = TRUE;
Bram Moolenaar30613902019-12-01 22:11:18 +01003341 // write char to script file(s)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003342 gotchars(typebuf.tb_buf
3343 + typebuf.tb_off, 1);
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003344 }
RestorerZ68ebcee2023-05-31 17:12:14 +01003345 KeyNoremap = typebuf.tb_noremap[typebuf.tb_off];
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003346 del_typebuf(1, 0);
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003347 }
Bram Moolenaar52797ba2021-12-16 14:45:13 +00003348 break; // got character, break the for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 }
3350
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003351 // not enough characters, get more
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 }
3353
3354/*
3355 * get a character: 3. from the user - handle <Esc> in Insert mode
3356 */
3357 /*
Bram Moolenaard330e842022-11-24 20:23:24 +00003358 * Special case: if we get an <ESC> in Insert mode and there
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 * are no more characters at once, we pretend to go out of
Bram Moolenaard330e842022-11-24 20:23:24 +00003360 * Insert mode. This prevents the one second delay after
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 * typing an <ESC>. If we get something after all, we may
3362 * have to redisplay the mode. That the cursor is in the wrong
3363 * place does not matter.
Bram Moolenaard330e842022-11-24 20:23:24 +00003364 * Do not do this if the kitty keyboard protocol is used, every
3365 * <ESC> is the start of an escape sequence then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 */
3367 c = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 new_wcol = curwin->w_wcol;
3369 new_wrow = curwin->w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 if ( advance
3371 && typebuf.tb_len == 1
3372 && typebuf.tb_buf[typebuf.tb_off] == ESC
3373 && !no_mapping
Bram Moolenaard330e842022-11-24 20:23:24 +00003374 && kitty_protocol_state != KKPS_ENABLED
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 && ex_normal_busy == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 && typebuf.tb_maplen == 0
Bram Moolenaar24959102022-05-07 20:01:16 +01003377 && (State & MODE_INSERT)
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003378 && (p_timeout
3379 || (keylen == KEYLEN_PART_KEY && p_ttimeout))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 && (c = inchar(typebuf.tb_buf + typebuf.tb_off
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003381 + typebuf.tb_len, 3, 25L)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003383 colnr_T col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 char_u *ptr;
3385
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003386 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 {
3388 unshowmode(TRUE);
3389 mode_deleted = TRUE;
3390 }
3391#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003392 // may show a different cursor shape
Bram Moolenaar24959102022-05-07 20:01:16 +01003393 if (gui.in_use && State != MODE_NORMAL && !cmd_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 {
3395 int save_State;
3396
3397 save_State = State;
Bram Moolenaar24959102022-05-07 20:01:16 +01003398 State = MODE_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 gui_update_cursor(TRUE, FALSE);
3400 State = save_State;
3401 shape_changed = TRUE;
3402 }
3403#endif
3404 validate_cursor();
3405 old_wcol = curwin->w_wcol;
3406 old_wrow = curwin->w_wrow;
3407
Bram Moolenaar30613902019-12-01 22:11:18 +01003408 // move cursor left, if possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 if (curwin->w_cursor.col != 0)
3410 {
3411 if (curwin->w_wcol > 0)
3412 {
Bram Moolenaar0f843ef2023-01-25 17:34:41 +00003413 // After auto-indenting and no text is following,
3414 // we are expecting to truncate the trailing
3415 // white-space, so find the last non-white
3416 // character -- webb
3417 if (did_ai && *skipwhite(ml_get_curline()
3418 + curwin->w_cursor.col) == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003420 chartabsize_T cts;
3421
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003422 curwin->w_wcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 ptr = ml_get_curline();
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003424 init_chartabsize_arg(&cts, curwin,
3425 curwin->w_cursor.lnum, 0, ptr, ptr);
3426 while (cts.cts_ptr < ptr + curwin->w_cursor.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003428 if (!VIM_ISWHITE(*cts.cts_ptr))
3429 curwin->w_wcol = cts.cts_vcol;
3430 cts.cts_vcol += lbr_chartabsize(&cts);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 if (has_mbyte)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003432 cts.cts_ptr +=
3433 (*mb_ptr2len)(cts.cts_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 else
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003435 ++cts.cts_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003437 clear_chartabsize_arg(&cts);
3438
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 curwin->w_wrow = curwin->w_cline_row
Bram Moolenaar02631462017-09-22 15:20:32 +02003440 + curwin->w_wcol / curwin->w_width;
3441 curwin->w_wcol %= curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 curwin->w_wcol += curwin_col_off();
Bram Moolenaar30613902019-12-01 22:11:18 +01003443 col = 0; // no correction needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 }
3445 else
3446 {
3447 --curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 col = curwin->w_cursor.col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 }
3450 }
3451 else if (curwin->w_p_wrap && curwin->w_wrow)
3452 {
3453 --curwin->w_wrow;
Bram Moolenaar02631462017-09-22 15:20:32 +02003454 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 col = curwin->w_cursor.col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 if (has_mbyte && col > 0 && curwin->w_wcol > 0)
3458 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003459 // Correct when the cursor is on the right halve
3460 // of a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 ptr = ml_get_curline();
3462 col -= (*mb_head_off)(ptr, ptr + col);
3463 if ((*mb_ptr2cells)(ptr + col) > 1)
3464 --curwin->w_wcol;
3465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 }
3467 setcursor();
3468 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 new_wcol = curwin->w_wcol;
3470 new_wrow = curwin->w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 curwin->w_wcol = old_wcol;
3472 curwin->w_wrow = old_wrow;
3473 }
3474 if (c < 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003475 continue; // end of input script reached
Bram Moolenaar20c38922014-07-23 20:41:14 +02003476
Bram Moolenaar30613902019-12-01 22:11:18 +01003477 // Allow mapping for just typed characters. When we get here c
3478 // is the number of extra bytes and typebuf.tb_len is 1.
Bram Moolenaar20c38922014-07-23 20:41:14 +02003479 for (n = 1; n <= c; ++n)
3480 typebuf.tb_noremap[typebuf.tb_off + n] = RM_YES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 typebuf.tb_len += c;
3482
Bram Moolenaar30613902019-12-01 22:11:18 +01003483 // buffer full, don't map
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 if (typebuf.tb_len >= typebuf.tb_maplen + MAXMAPLEN)
3485 {
3486 timedout = TRUE;
3487 continue;
3488 }
3489
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 if (ex_normal_busy > 0)
3491 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 static int tc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493
Bram Moolenaar30613902019-12-01 22:11:18 +01003494 // No typeahead left and inside ":normal". Must return
3495 // something to avoid getting stuck. When an incomplete
3496 // mapping is present, behave like it timed out.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 if (typebuf.tb_len > 0)
3498 {
3499 timedout = TRUE;
3500 continue;
3501 }
Bram Moolenaar189832b2020-09-23 12:29:11 +02003502
Bram Moolenaar30613902019-12-01 22:11:18 +01003503 // When 'insertmode' is set, ESC just beeps in Insert
3504 // mode. Use CTRL-L to make edit() return.
3505 // For the command line only CTRL-C always breaks it.
3506 // For the cmdline window: Alternate between ESC and
3507 // CTRL-C: ESC for most situations and CTRL-C to close the
3508 // cmdline window.
Bram Moolenaar24959102022-05-07 20:01:16 +01003509 if (p_im && (State & MODE_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 c = Ctrl_L;
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003511#ifdef FEAT_TERMINAL
3512 else if (terminal_is_active())
3513 c = K_CANCEL;
3514#endif
Bram Moolenaar24959102022-05-07 20:01:16 +01003515 else if ((State & MODE_CMDLINE)
Martin Tournoij7904fa42022-10-04 16:28:45 +01003516 || (cmdwin_type > 0 && tc == ESC))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 c = Ctrl_C;
3518 else
3519 c = ESC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 tc = c;
Bram Moolenaar8d696372022-08-21 10:40:07 +01003521 // set a flag to indicate this wasn't a normal char
3522 if (advance)
3523 typebuf_was_empty = TRUE;
3524
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01003525 // return from main_loop()
3526 if (pending_exmode_active)
3527 exmode_active = EXMODE_NORMAL;
3528
Bram Moolenaarb37a65e2022-01-01 12:42:56 +00003529 // no chars to block abbreviation for
3530 typebuf.tb_no_abbr_cnt = 0;
3531
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 break;
3533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534
3535/*
3536 * get a character: 3. from the user - update display
3537 */
Bram Moolenaar30613902019-12-01 22:11:18 +01003538 // In insert mode a screen update is skipped when characters
3539 // are still available. But when those available characters
3540 // are part of a mapping, and we are going to do a blocking
3541 // wait here. Need to update the screen to display the
3542 // changed text so far. Also for when 'lazyredraw' is set and
3543 // redrawing was postponed because there was something in the
3544 // input buffer (e.g., termresponse).
Bram Moolenaar24959102022-05-07 20:01:16 +01003545 if (((State & MODE_INSERT) != 0 || p_lz)
3546 && (State & MODE_CMDLINE) == 0
3547 && advance && must_redraw != 0 && !need_wait_return)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 {
3549 update_screen(0);
Bram Moolenaar30613902019-12-01 22:11:18 +01003550 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 }
3552
3553 /*
3554 * If we have a partial match (and are going to wait for more
3555 * input from the user), show the partially matched characters
3556 * to the user with showcmd.
3557 */
Bram Moolenaareda35f72019-08-03 14:59:44 +02003558 showcmd_idx = 0;
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003559 int showing_partial = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 if (typebuf.tb_len > 0 && advance && !exmode_active)
3561 {
Bram Moolenaar24959102022-05-07 20:01:16 +01003562 if (((State & (MODE_NORMAL | MODE_INSERT))
3563 || State == MODE_LANGMAP)
3564 && State != MODE_HITRETURN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003566 // this looks nice when typing a dead character map
Bram Moolenaar24959102022-05-07 20:01:16 +01003567 if (State & MODE_INSERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
3569 + typebuf.tb_len - 1) == 1)
3570 {
3571 edit_putchar(typebuf.tb_buf[typebuf.tb_off
3572 + typebuf.tb_len - 1], FALSE);
Bram Moolenaar30613902019-12-01 22:11:18 +01003573 setcursor(); // put cursor back where it belongs
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003574 showing_partial = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003576 // need to use the col and row from above here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 old_wcol = curwin->w_wcol;
3578 old_wrow = curwin->w_wrow;
3579 curwin->w_wcol = new_wcol;
3580 curwin->w_wrow = new_wrow;
3581 push_showcmd();
3582 if (typebuf.tb_len > SHOWCMD_COLS)
Bram Moolenaareda35f72019-08-03 14:59:44 +02003583 showcmd_idx = typebuf.tb_len - SHOWCMD_COLS;
3584 while (showcmd_idx < typebuf.tb_len)
3585 (void)add_to_showcmd(
3586 typebuf.tb_buf[typebuf.tb_off + showcmd_idx++]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 curwin->w_wcol = old_wcol;
3588 curwin->w_wrow = old_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 }
3590
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003591 // This looks nice when typing a dead character map.
3592 // There is no actual command line for get_number().
Bram Moolenaar24959102022-05-07 20:01:16 +01003593 if ((State & MODE_CMDLINE)
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003594 && get_cmdline_info()->cmdbuff != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3596 && cmdline_star == 0
3597#endif
3598 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
3599 + typebuf.tb_len - 1) == 1)
3600 {
3601 putcmdline(typebuf.tb_buf[typebuf.tb_off
3602 + typebuf.tb_len - 1], FALSE);
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003603 showing_partial = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 }
3605 }
3606
3607/*
3608 * get a character: 3. from the user - get it
3609 */
Bram Moolenaar83f4cbd2018-06-12 21:35:40 +02003610 if (typebuf.tb_len == 0)
zeertzjq3760bfd2022-06-06 16:22:46 +01003611 // timedout may have been set if a mapping with empty RHS
3612 // fully matched while longer mappings timed out.
Bram Moolenaar83f4cbd2018-06-12 21:35:40 +02003613 timedout = FALSE;
3614
Bram Moolenaar652de232019-04-04 20:13:09 +02003615 if (advance)
3616 {
3617 if (typebuf.tb_len == 0
3618 || !(p_timeout
3619 || (p_ttimeout && keylen == KEYLEN_PART_KEY)))
3620 // blocking wait
3621 wait_time = -1L;
3622 else if (keylen == KEYLEN_PART_KEY && p_ttm >= 0)
3623 wait_time = p_ttm;
3624 else
3625 wait_time = p_tm;
3626 }
3627 else
3628 wait_time = 0;
3629
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003630 wait_tb_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 c = inchar(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len,
3632 typebuf.tb_buflen - typebuf.tb_off - typebuf.tb_len - 1,
Bram Moolenaar652de232019-04-04 20:13:09 +02003633 wait_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634
Bram Moolenaareda35f72019-08-03 14:59:44 +02003635 if (showcmd_idx != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 pop_showcmd();
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003637 if (showing_partial)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 {
Bram Moolenaar24959102022-05-07 20:01:16 +01003639 if (State & MODE_INSERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 edit_unputchar();
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003641 if ((State & MODE_CMDLINE)
3642 && get_cmdline_info()->cmdbuff != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 unputcmdline();
Bram Moolenaarbc256d92012-06-06 12:06:15 +02003644 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003645 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 }
3647
3648 if (c < 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003649 continue; // end of input script reached
3650 if (c == NUL) // no character available
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 {
3652 if (!advance)
3653 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01003654 if (wait_tb_len > 0) // timed out
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 {
3656 timedout = TRUE;
3657 continue;
3658 }
3659 }
3660 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003661 { // allow mapping for just typed characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 while (typebuf.tb_buf[typebuf.tb_off
3663 + typebuf.tb_len] != NUL)
3664 typebuf.tb_noremap[typebuf.tb_off
3665 + typebuf.tb_len++] = RM_YES;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003666#ifdef HAVE_INPUT_METHOD
Bram Moolenaar30613902019-12-01 22:11:18 +01003667 // Get IM status right after getting keys, not after the
3668 // timeout for a mapping (focus may be lost by then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 vgetc_im_active = im_get_status();
3670#endif
3671 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003672 } // for (;;)
3673 } // if (!character from stuffbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674
Bram Moolenaar30613902019-12-01 22:11:18 +01003675 // if advance is FALSE don't loop on NULs
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003676 } while ((c < 0 && c != K_CANCEL) || (advance && c == NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677
3678 /*
3679 * The "INSERT" message is taken care of here:
3680 * if we return an ESC to exit insert mode, the message is deleted
3681 * if we don't return an ESC but deleted the message before, redisplay it
3682 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003683 if (advance && p_smd && msg_silent == 0 && (State & MODE_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003685 if (c == ESC && !mode_deleted && !no_mapping && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 {
3687 if (typebuf.tb_len && !KeyTyped)
Bram Moolenaar30613902019-12-01 22:11:18 +01003688 redraw_cmdline = TRUE; // delete mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 else
3690 unshowmode(FALSE);
3691 }
3692 else if (c != ESC && mode_deleted)
3693 {
3694 if (typebuf.tb_len && !KeyTyped)
Bram Moolenaar30613902019-12-01 22:11:18 +01003695 redraw_cmdline = TRUE; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 else
3697 showmode();
3698 }
3699 }
3700#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003701 // may unshow different cursor shape
Bram Moolenaarf085f422017-06-07 20:39:47 +02003702 if (gui.in_use && shape_changed)
3703 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704#endif
Bram Moolenaar6edbbd82019-03-10 09:41:51 +01003705 if (timedout && c == ESC)
3706 {
zeertzjqbf321802024-01-28 19:03:00 +01003707 // When recording there will be no timeout. Add an <Ignore> after the
3708 // ESC to avoid that it forms a key code with following characters.
3709 gotchars_ignore();
Bram Moolenaar6edbbd82019-03-10 09:41:51 +01003710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003712 --vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713
3714 return c;
3715}
3716
3717/*
3718 * inchar() - get one character from
3719 * 1. a scriptfile
3720 * 2. the keyboard
3721 *
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003722 * As many characters as we can get (up to 'maxlen') are put in "buf" and
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 * NUL terminated (buffer length must be 'maxlen' + 1).
3724 * Minimum for "maxlen" is 3!!!!
3725 *
3726 * "tb_change_cnt" is the value of typebuf.tb_change_cnt if "buf" points into
3727 * it. When typebuf.tb_change_cnt changes (e.g., when a message is received
3728 * from a remote client) "buf" can no longer be used. "tb_change_cnt" is 0
3729 * otherwise.
3730 *
3731 * If we got an interrupt all input is read until none is available.
3732 *
3733 * If wait_time == 0 there is no waiting for the char.
3734 * If wait_time == n we wait for n msec for a character to arrive.
3735 * If wait_time == -1 we wait forever for a character to arrive.
3736 *
3737 * Return the number of obtained characters.
3738 * Return -1 when end of input script reached.
3739 */
Bram Moolenaarcda77642016-06-04 13:32:35 +02003740 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003741inchar(
3742 char_u *buf,
3743 int maxlen,
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003744 long wait_time) // milliseconds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745{
Bram Moolenaar30613902019-12-01 22:11:18 +01003746 int len = 0; // init for GCC
3747 int retesc = FALSE; // return ESC with gotint
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 int script_char;
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003749 int tb_change_cnt = typebuf.tb_change_cnt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750
Bram Moolenaar30613902019-12-01 22:11:18 +01003751 if (wait_time == -1L || wait_time > 100L) // flush output before waiting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 {
3753 cursor_on();
Bram Moolenaara338adc2018-01-31 20:51:47 +01003754 out_flush_cursor(FALSE, FALSE);
3755#if defined(FEAT_GUI) && defined(FEAT_MOUSESHAPE)
3756 if (gui.in_use && postponed_mouseshape)
3757 update_mouseshape(-1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758#endif
3759 }
3760
3761 /*
3762 * Don't reset these when at the hit-return prompt, otherwise a endless
3763 * recursive loop may result (write error in swapfile, hit-return, timeout
3764 * on char wait, flush swapfile, write error....).
3765 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003766 if (State != MODE_HITRETURN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003768 did_outofmem_msg = FALSE; // display out of memory message (again)
3769 did_swapwrite_msg = FALSE; // display swap file write error again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003771 undo_off = FALSE; // restart undo now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772
3773 /*
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00003774 * Get a character from a script file if there is one.
3775 * If interrupted: Stop reading script files, close them all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 */
3777 script_char = -1;
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00003778 while (scriptin[curscript] != NULL && script_char < 0
3779#ifdef FEAT_EVAL
3780 && !ignore_script
3781#endif
3782 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02003784#ifdef MESSAGE_QUEUE
3785 parse_queued_messages();
Bram Moolenaarf2330482008-06-24 20:19:36 +00003786#endif
3787
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 if (got_int || (script_char = getc(scriptin[curscript])) < 0)
3789 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003790 // Reached EOF.
3791 // Careful: closescript() frees typebuf.tb_buf[] and buf[] may
3792 // point inside typebuf.tb_buf[]. Don't use buf[] after this!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 closescript();
3794 /*
3795 * When reading script file is interrupted, return an ESC to get
3796 * back to normal mode.
3797 * Otherwise return -1, because typebuf.tb_buf[] has changed.
3798 */
3799 if (got_int)
3800 retesc = TRUE;
3801 else
3802 return -1;
3803 }
3804 else
3805 {
3806 buf[0] = script_char;
3807 len = 1;
3808 }
3809 }
3810
Bram Moolenaar30613902019-12-01 22:11:18 +01003811 if (script_char < 0) // did not get a character from script
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 {
3813 /*
3814 * If we got an interrupt, skip all previously typed characters and
3815 * return TRUE if quit reading script file.
3816 * Stop reading typeahead when a single CTRL-C was read,
3817 * fill_input_buf() returns this when not able to read from stdin.
3818 * Don't use buf[] here, closescript() may have freed typebuf.tb_buf[]
3819 * and buf may be pointing inside typebuf.tb_buf[].
3820 */
3821 if (got_int)
3822 {
kylo252ae6f1d82022-02-16 19:24:07 +00003823#define DUM_LEN (MAXMAPLEN * 3 + 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 char_u dum[DUM_LEN + 1];
3825
3826 for (;;)
3827 {
3828 len = ui_inchar(dum, DUM_LEN, 0L, 0);
zeertzjq17c95d92022-04-26 12:51:07 +01003829 if (len == 0 || (len == 1 && dum[0] == Ctrl_C))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 break;
3831 }
3832 return retesc;
3833 }
3834
3835 /*
3836 * Always flush the output characters when getting input characters
Bram Moolenaarcb574f42019-01-25 22:29:57 +01003837 * from the user and not just peeking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 */
Bram Moolenaarcb574f42019-01-25 22:29:57 +01003839 if (wait_time == -1L || wait_time > 10L)
3840 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841
3842 /*
3843 * Fill up to a third of the buffer, because each character may be
3844 * tripled below.
3845 */
3846 len = ui_inchar(buf, maxlen / 3, wait_time, tb_change_cnt);
3847 }
3848
Bram Moolenaar30613902019-12-01 22:11:18 +01003849 // If the typebuf was changed further down, it is like nothing was added by
3850 // this call.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 if (typebuf_changed(tb_change_cnt))
3852 return 0;
3853
Bram Moolenaar30613902019-12-01 22:11:18 +01003854 // Note the change in the typeahead buffer, this matters for when
3855 // vgetorpeek() is called recursively, e.g. using getchar(1) in a timer
3856 // function.
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003857 if (len > 0 && ++typebuf.tb_change_cnt == 0)
3858 typebuf.tb_change_cnt = 1;
3859
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003860 return fix_input_buffer(buf, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861}
3862
3863/*
3864 * Fix typed characters for use by vgetc() and check_termcode().
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003865 * "buf[]" must have room to triple the number of bytes!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 * Returns the new length.
3867 */
3868 int
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003869fix_input_buffer(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870{
3871 int i;
3872 char_u *p = buf;
3873
3874 /*
3875 * Two characters are special: NUL and K_SPECIAL.
3876 * When compiled With the GUI CSI is also special.
3877 * Replace NUL by K_SPECIAL KS_ZERO KE_FILLER
3878 * Replace K_SPECIAL by K_SPECIAL KS_SPECIAL KE_FILLER
3879 * Replace CSI by K_SPECIAL KS_EXTRA KE_CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 */
3881 for (i = len; --i >= 0; ++p)
3882 {
3883#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003884 // When the GUI is used any character can come after a CSI, don't
3885 // escape it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 if (gui.in_use && p[0] == CSI && i >= 2)
3887 {
3888 p += 2;
3889 i -= 2;
3890 }
zeertzjq646bb722022-02-16 17:51:47 +00003891# ifndef MSWIN
3892 // When not on MS-Windows and the GUI is not used CSI needs to be
3893 // escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 else if (!gui.in_use && p[0] == CSI)
3895 {
3896 mch_memmove(p + 3, p + 1, (size_t)i);
3897 *p++ = K_SPECIAL;
3898 *p++ = KS_EXTRA;
3899 *p = (int)KE_CSI;
3900 len += 2;
3901 }
zeertzjq646bb722022-02-16 17:51:47 +00003902# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 else
3904#endif
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003905 if (p[0] == NUL || (p[0] == K_SPECIAL
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003906 // timeout may generate K_CURSORHOLD
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003907 && (i < 2 || p[1] != KS_EXTRA || p[2] != (int)KE_CURSORHOLD)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003908#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003909 // Win32 console passes modifiers
3910 && (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003911# ifdef VIMDLL
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003912 gui.in_use ||
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003913# endif
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003914 (i < 2 || p[1] != KS_MODIFIER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915#endif
3916 ))
3917 {
3918 mch_memmove(p + 3, p + 1, (size_t)i);
3919 p[2] = K_THIRD(p[0]);
3920 p[1] = K_SECOND(p[0]);
3921 p[0] = K_SPECIAL;
3922 p += 2;
3923 len += 2;
3924 }
3925 }
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003926 *p = NUL; // add trailing NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 return len;
3928}
3929
3930#if defined(USE_INPUT_BUF) || defined(PROTO)
3931/*
3932 * Return TRUE when bytes are in the input buffer or in the typeahead buffer.
3933 * Normally the input buffer would be sufficient, but the server_to_input_buf()
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003934 * or feedkeys() may insert characters in the typeahead buffer while we are
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003935 * waiting for input to arrive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 */
3937 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003938input_available(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939{
3940 return (!vim_is_input_buf_empty()
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003941# if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
3942 || typebuf_was_filled
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943# endif
3944 );
3945}
3946#endif
Bram Moolenaar957cf672020-11-12 14:21:06 +01003947
3948/*
3949 * Function passed to do_cmdline() to get the command after a <Cmd> key from
3950 * typeahead.
3951 */
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003952 static char_u *
Bram Moolenaar957cf672020-11-12 14:21:06 +01003953getcmdkeycmd(
3954 int promptc UNUSED,
3955 void *cookie UNUSED,
3956 int indent UNUSED,
3957 getline_opt_T do_concat UNUSED)
3958{
3959 garray_T line_ga;
3960 int c1 = -1;
3961 int c2;
3962 int cmod = 0;
3963 int aborted = FALSE;
3964
3965 ga_init2(&line_ga, 1, 32);
3966
3967 // no mapping for these characters
3968 no_mapping++;
3969
3970 got_int = FALSE;
3971 while (c1 != NUL && !aborted)
3972 {
Yegappan Lakshmananfadc02a2023-01-27 21:03:12 +00003973 if (ga_grow(&line_ga, 32) == FAIL)
Bram Moolenaarca359cb2020-11-15 20:49:41 +01003974 {
3975 aborted = TRUE;
3976 break;
3977 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003978
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003979 if (vgetorpeek(FALSE) == NUL)
Bram Moolenaar957cf672020-11-12 14:21:06 +01003980 {
3981 // incomplete <Cmd> is an error, because there is not much the user
3982 // could do in this state.
3983 emsg(_(e_cmd_mapping_must_end_with_cr));
3984 aborted = TRUE;
3985 break;
3986 }
3987
3988 // Get one character at a time.
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003989 c1 = vgetorpeek(TRUE);
3990
Bram Moolenaar957cf672020-11-12 14:21:06 +01003991 // Get two extra bytes for special keys
3992 if (c1 == K_SPECIAL)
3993 {
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003994 c1 = vgetorpeek(TRUE);
3995 c2 = vgetorpeek(TRUE);
Bram Moolenaar957cf672020-11-12 14:21:06 +01003996 if (c1 == KS_MODIFIER)
3997 {
3998 cmod = c2;
3999 continue;
4000 }
4001 c1 = TO_SPECIAL(c1, c2);
Bram Moolenaardffa6ea2022-11-29 20:33:20 +00004002
4003 // K_ESC is used to avoid ambiguity with the single Esc character
4004 // that might be the start of an escape sequence. Convert it back
4005 // to a single Esc here.
4006 if (c1 == K_ESC)
4007 c1 = ESC;
Bram Moolenaar957cf672020-11-12 14:21:06 +01004008 }
4009
4010 if (got_int)
4011 aborted = TRUE;
4012 else if (c1 == '\r' || c1 == '\n')
4013 c1 = NUL; // end the line
4014 else if (c1 == ESC)
4015 aborted = TRUE;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004016 else if (c1 == K_COMMAND || c1 == K_SCRIPT_COMMAND)
Bram Moolenaar957cf672020-11-12 14:21:06 +01004017 {
4018 // give a nicer error message for this special case
4019 emsg(_(e_cmd_mapping_must_end_with_cr_before_second_cmd));
4020 aborted = TRUE;
4021 }
zeertzjq3ab3a862023-05-06 16:22:04 +01004022 else if (c1 == K_SNR)
Bram Moolenaar957cf672020-11-12 14:21:06 +01004023 {
zeertzjq3ab3a862023-05-06 16:22:04 +01004024 ga_concat(&line_ga, (char_u *)"<SNR>");
Bram Moolenaar957cf672020-11-12 14:21:06 +01004025 }
4026 else
zeertzjq3ab3a862023-05-06 16:22:04 +01004027 {
4028 if (cmod != 0)
4029 {
4030 ga_append(&line_ga, K_SPECIAL);
4031 ga_append(&line_ga, KS_MODIFIER);
4032 ga_append(&line_ga, cmod);
4033 }
4034 if (IS_SPECIAL(c1))
4035 {
4036 ga_append(&line_ga, K_SPECIAL);
4037 ga_append(&line_ga, K_SECOND(c1));
4038 ga_append(&line_ga, K_THIRD(c1));
4039 }
4040 else
4041 ga_append(&line_ga, c1);
4042 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01004043
4044 cmod = 0;
4045 }
4046
4047 no_mapping--;
4048
4049 if (aborted)
4050 ga_clear(&line_ga);
4051
4052 return (char_u *)line_ga.ga_data;
4053}
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004054
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004055#if defined(FEAT_EVAL) || defined(PROTO)
4056/*
Bram Moolenaar6b066c62023-02-20 18:44:33 +00004057 * If there was a mapping we get its SID. Otherwise, use "last_used_sid", it
4058 * is set when redo'ing.
4059 * Put this SID in the redo buffer, so that "." will use the same script
4060 * context.
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004061 */
4062 void
4063may_add_last_used_map_to_redobuff(void)
4064{
Bram Moolenaar6b066c62023-02-20 18:44:33 +00004065 char_u buf[3 + 20];
4066 int sid = -1;
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004067
Bram Moolenaar6b066c62023-02-20 18:44:33 +00004068 if (last_used_map != NULL)
4069 sid = last_used_map->m_script_ctx.sc_sid;
4070 if (sid < 0)
4071 sid = last_used_sid;
4072
4073 if (sid < 0)
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004074 return;
4075
4076 // <K_SID>{nr};
4077 buf[0] = K_SPECIAL;
4078 buf[1] = KS_EXTRA;
4079 buf[2] = KE_SID;
Bram Moolenaar6b066c62023-02-20 18:44:33 +00004080 vim_snprintf((char *)buf + 3, 20, "%d;", sid);
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004081 add_buff(&redobuff, buf, -1L);
4082}
4083#endif
4084
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004085 int
Bram Moolenaard4e2f502022-01-15 18:48:32 +00004086do_cmdkey_command(int key UNUSED, int flags)
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004087{
4088 int res;
4089#ifdef FEAT_EVAL
Bram Moolenaara9725222022-01-16 13:30:33 +00004090 sctx_T save_current_sctx = {-1, 0, 0, 0};
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004091
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004092 if (key == K_SCRIPT_COMMAND
4093 && (last_used_map != NULL || SCRIPT_ID_VALID(last_used_sid)))
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004094 {
4095 save_current_sctx = current_sctx;
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004096 if (last_used_map != NULL)
4097 current_sctx = last_used_map->m_script_ctx;
4098 else
4099 {
4100 current_sctx.sc_sid = last_used_sid;
4101 current_sctx.sc_lnum = 0;
4102 current_sctx.sc_version = SCRIPT_ITEM(last_used_sid)->sn_version;
4103 }
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004104 }
4105#endif
4106
4107 res = do_cmdline(NULL, getcmdkeycmd, NULL, flags);
4108
4109#ifdef FEAT_EVAL
Bram Moolenaara9725222022-01-16 13:30:33 +00004110 if (save_current_sctx.sc_sid >= 0)
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004111 current_sctx = save_current_sctx;
4112#endif
4113
4114 return res;
4115}
4116
4117#if defined(FEAT_EVAL) || defined(PROTO)
4118 void
Bram Moolenaarf61c89d2022-01-19 22:51:48 +00004119reset_last_used_map(mapblock_T *mp)
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004120{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00004121 if (last_used_map != mp)
4122 return;
4123
4124 last_used_map = NULL;
4125 last_used_sid = -1;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004126}
4127#endif