blob: 6817fa5b83590effdaa62a75697b9d90a8c394f7 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
zeertzjqa3f83fe2021-11-22 12:47:39 +000011 * getchar.c: Code related to getting a character from the user or a script
12 * file, manipulations with redo buffer and stuff buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013 */
14
15#include "vim.h"
16
17/*
18 * These buffers are used for storing:
19 * - stuffed characters: A command that is translated into another command.
20 * - redo characters: will redo the last change.
Bram Moolenaarf6f95d92009-11-11 15:23:37 +000021 * - recorded characters: for the "q" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022 *
23 * The bytes are stored like in the typeahead buffer:
24 * - K_SPECIAL introduces a special key (two more bytes follow). A literal
25 * K_SPECIAL is stored as K_SPECIAL KS_SPECIAL KE_FILLER.
26 * - CSI introduces a GUI termcap code (also when gui.in_use is FALSE,
27 * otherwise switching the GUI on would make mappings invalid).
28 * A literal CSI is stored as CSI KS_EXTRA KE_CSI.
29 * These translations are also done on multi-byte characters!
30 *
31 * Escaping CSI bytes is done by the system-specific input functions, called
32 * by ui_inchar().
33 * Escaping K_SPECIAL is done by inchar().
34 * Un-escaping is done by vgetc().
35 */
36
Bram Moolenaar30613902019-12-01 22:11:18 +010037#define MINIMAL_SIZE 20 // minimal size for b_str
Bram Moolenaar071d4272004-06-13 20:20:40 +000038
Bram Moolenaar285e3352018-04-18 23:01:13 +020039static buffheader_T redobuff = {{NULL, {NUL}}, NULL, 0, 0};
40static buffheader_T old_redobuff = {{NULL, {NUL}}, NULL, 0, 0};
41static buffheader_T recordbuff = {{NULL, {NUL}}, NULL, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +000042
Bram Moolenaar30613902019-12-01 22:11:18 +010043static int typeahead_char = 0; // typeahead char that's not flushed
Bram Moolenaar071d4272004-06-13 20:20:40 +000044
45/*
46 * when block_redo is TRUE redo buffer will not be changed
47 * used by edit() to repeat insertions and 'V' command for redoing
48 */
49static int block_redo = FALSE;
50
Bram Moolenaar459fd782019-10-13 16:43:39 +020051static int KeyNoremap = 0; // remapping flags
Bram Moolenaar071d4272004-06-13 20:20:40 +000052
53/*
Bram Moolenaarfd89d7e2016-06-04 20:25:05 +020054 * Variables used by vgetorpeek() and flush_buffers().
Bram Moolenaar071d4272004-06-13 20:20:40 +000055 *
56 * typebuf.tb_buf[] contains all characters that are not consumed yet.
57 * typebuf.tb_buf[typebuf.tb_off] is the first valid character.
58 * typebuf.tb_buf[typebuf.tb_off + typebuf.tb_len - 1] is the last valid char.
59 * typebuf.tb_buf[typebuf.tb_off + typebuf.tb_len] must be NUL.
60 * The head of the buffer may contain the result of mappings, abbreviations
61 * and @a commands. The length of this part is typebuf.tb_maplen.
62 * typebuf.tb_silent is the part where <silent> applies.
63 * After the head are characters that come from the terminal.
64 * typebuf.tb_no_abbr_cnt is the number of characters in typebuf.tb_buf that
65 * should not be considered for abbreviations.
66 * Some parts of typebuf.tb_buf may not be mapped. These parts are remembered
67 * in typebuf.tb_noremap[], which is the same length as typebuf.tb_buf and
68 * contains RM_NONE for the characters that are not to be remapped.
69 * typebuf.tb_noremap[typebuf.tb_off] is the first valid flag.
70 * (typebuf has been put in globals.h, because check_termcode() needs it).
71 */
Bram Moolenaar30613902019-12-01 22:11:18 +010072#define RM_YES 0 // tb_noremap: remap
73#define RM_NONE 1 // tb_noremap: don't remap
74#define RM_SCRIPT 2 // tb_noremap: remap local script mappings
75#define RM_ABBR 4 // tb_noremap: don't remap, do abbrev.
Bram Moolenaar071d4272004-06-13 20:20:40 +000076
Bram Moolenaar30613902019-12-01 22:11:18 +010077// typebuf.tb_buf has three parts: room in front (for result of mappings), the
78// middle for typeahead and room for new characters (which needs to be 3 *
Dominique Pelleaf4a61a2021-12-27 17:21:41 +000079// MAXMAPLEN for the Amiga).
Bram Moolenaar071d4272004-06-13 20:20:40 +000080#define TYPELEN_INIT (5 * (MAXMAPLEN + 3))
Bram Moolenaar30613902019-12-01 22:11:18 +010081static char_u typebuf_init[TYPELEN_INIT]; // initial typebuf.tb_buf
82static char_u noremapbuf_init[TYPELEN_INIT]; // initial typebuf.tb_noremap
Bram Moolenaar071d4272004-06-13 20:20:40 +000083
Bram Moolenaar30613902019-12-01 22:11:18 +010084static int last_recorded_len = 0; // number of last recorded chars
Bram Moolenaar071d4272004-06-13 20:20:40 +000085
Bram Moolenaare32c3c42022-01-15 18:26:04 +000086#ifdef FEAT_EVAL
87mapblock_T *last_used_map = NULL;
Bram Moolenaarddf7dba2022-09-05 16:53:21 +010088int last_used_sid = -1;
Bram Moolenaare32c3c42022-01-15 18:26:04 +000089#endif
90
Bram Moolenaard25c16e2016-01-29 22:13:30 +010091static int read_readbuf(buffheader_T *buf, int advance);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010092static void init_typebuf(void);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010093static void may_sync_undo(void);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020094static void free_typebuf(void);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010095static void closescript(void);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020096static void updatescript(int c);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010097static int vgetorpeek(int);
Bram Moolenaar0f0f2302017-08-30 18:52:56 +020098static int inchar(char_u *buf, int maxlen, long wait_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +000099
100/*
101 * Free and clear a buffer.
102 */
Bram Moolenaarbdff0122020-04-05 18:56:05 +0200103 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100104free_buff(buffheader_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100106 buffblock_T *p, *np;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107
Bram Moolenaar285e3352018-04-18 23:01:13 +0200108 for (p = buf->bh_first.b_next; p != NULL; p = np)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000109 {
110 np = p->b_next;
111 vim_free(p);
112 }
Bram Moolenaar285e3352018-04-18 23:01:13 +0200113 buf->bh_first.b_next = NULL;
Bram Moolenaar166788c2022-01-27 21:56:40 +0000114 buf->bh_curr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115}
116
117/*
118 * Return the contents of a buffer as a single string.
119 * K_SPECIAL and CSI in the returned string are escaped.
120 */
121 static char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100122get_buffcont(
123 buffheader_T *buffer,
Bram Moolenaar30613902019-12-01 22:11:18 +0100124 int dozero) // count == zero is not an error
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125{
126 long_u count = 0;
127 char_u *p = NULL;
128 char_u *p2;
129 char_u *str;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200130 buffblock_T *bp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131
Bram Moolenaar30613902019-12-01 22:11:18 +0100132 // compute the total length of the string
Bram Moolenaar285e3352018-04-18 23:01:13 +0200133 for (bp = buffer->bh_first.b_next; bp != NULL; bp = bp->b_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134 count += (long_u)STRLEN(bp->b_str);
135
Bram Moolenaarf39d9e92023-04-22 22:54:40 +0100136 if ((count > 0 || dozero) && (p = alloc(count + 1)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137 {
138 p2 = p;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200139 for (bp = buffer->bh_first.b_next; bp != NULL; bp = bp->b_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140 for (str = bp->b_str; *str; )
141 *p2++ = *str++;
142 *p2 = NUL;
143 }
Bram Moolenaarf39d9e92023-04-22 22:54:40 +0100144 return p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145}
146
147/*
148 * Return the contents of the record buffer as a single string
149 * and clear the record buffer.
150 * K_SPECIAL and CSI in the returned string are escaped.
151 */
152 char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100153get_recorded(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154{
155 char_u *p;
156 size_t len;
157
158 p = get_buffcont(&recordbuff, TRUE);
159 free_buff(&recordbuff);
160
161 /*
162 * Remove the characters that were added the last time, these must be the
163 * (possibly mapped) characters that stopped the recording.
164 */
165 len = STRLEN(p);
166 if ((int)len >= last_recorded_len)
167 {
168 len -= last_recorded_len;
169 p[len] = NUL;
170 }
171
172 /*
173 * When stopping recording from Insert mode with CTRL-O q, also remove the
174 * CTRL-O.
175 */
176 if (len > 0 && restart_edit != 0 && p[len - 1] == Ctrl_O)
177 p[len - 1] = NUL;
178
179 return (p);
180}
181
182/*
183 * Return the contents of the redo buffer as a single string.
184 * K_SPECIAL and CSI in the returned string are escaped.
185 */
186 char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100187get_inserted(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188{
Bram Moolenaarf2330482008-06-24 20:19:36 +0000189 return get_buffcont(&redobuff, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190}
191
192/*
Bram Moolenaar4e86cba2007-05-06 11:56:32 +0000193 * Add string "s" after the current block of buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194 * K_SPECIAL and CSI should have been escaped already.
195 */
196 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100197add_buff(
198 buffheader_T *buf,
199 char_u *s,
Bram Moolenaar30613902019-12-01 22:11:18 +0100200 long slen) // length of "s" or -1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100202 buffblock_T *p;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200203 long_u len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204
205 if (slen < 0)
206 slen = (long)STRLEN(s);
Bram Moolenaar30613902019-12-01 22:11:18 +0100207 if (slen == 0) // don't add empty strings
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208 return;
209
Bram Moolenaar30613902019-12-01 22:11:18 +0100210 if (buf->bh_first.b_next == NULL) // first add to list
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211 {
212 buf->bh_space = 0;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200213 buf->bh_curr = &(buf->bh_first);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214 }
Bram Moolenaar30613902019-12-01 22:11:18 +0100215 else if (buf->bh_curr == NULL) // buffer has already been read
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216 {
Bram Moolenaar6d057012021-12-31 18:49:43 +0000217 iemsg(_(e_add_to_internal_buffer_that_was_already_read_from));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218 return;
219 }
220 else if (buf->bh_index != 0)
Bram Moolenaar285e3352018-04-18 23:01:13 +0200221 mch_memmove(buf->bh_first.b_next->b_str,
222 buf->bh_first.b_next->b_str + buf->bh_index,
223 STRLEN(buf->bh_first.b_next->b_str + buf->bh_index) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224 buf->bh_index = 0;
225
226 if (buf->bh_space >= (int)slen)
227 {
228 len = (long_u)STRLEN(buf->bh_curr->b_str);
Bram Moolenaarb6356332005-07-18 21:40:44 +0000229 vim_strncpy(buf->bh_curr->b_str + len, s, (size_t)slen);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230 buf->bh_space -= slen;
231 }
232 else
233 {
234 if (slen < MINIMAL_SIZE)
235 len = MINIMAL_SIZE;
236 else
237 len = slen;
Bram Moolenaar47ed5532019-08-08 20:49:14 +0200238 p = alloc(offsetof(buffblock_T, b_str) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239 if (p == NULL)
Bram Moolenaar30613902019-12-01 22:11:18 +0100240 return; // no space, just forget it
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000241 buf->bh_space = (int)(len - slen);
Bram Moolenaarb6356332005-07-18 21:40:44 +0000242 vim_strncpy(p->b_str, s, (size_t)slen);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243
Bram Moolenaar285e3352018-04-18 23:01:13 +0200244 p->b_next = buf->bh_curr->b_next;
245 buf->bh_curr->b_next = p;
246 buf->bh_curr = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248}
249
250/*
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000251 * Delete "slen" bytes from the end of "buf".
252 * Only works when it was just added.
253 */
254 static void
255delete_buff_tail(buffheader_T *buf, int slen)
256{
Bram Moolenaara4bc2dd2022-01-27 19:27:16 +0000257 int len;
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000258
Bram Moolenaar37cf4132022-01-27 20:47:03 +0000259 if (buf->bh_curr == NULL)
Bram Moolenaara4bc2dd2022-01-27 19:27:16 +0000260 return; // nothing to delete
261 len = (int)STRLEN(buf->bh_curr->b_str);
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000262 if (len < slen)
263 return;
264
265 buf->bh_curr->b_str[len - slen] = NUL;
266 buf->bh_space += slen;
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000267}
268
269/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270 * Add number "n" to buffer "buf".
271 */
272 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100273add_num_buff(buffheader_T *buf, long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274{
275 char_u number[32];
276
277 sprintf((char *)number, "%ld", n);
278 add_buff(buf, number, -1L);
279}
280
281/*
282 * Add character 'c' to buffer "buf".
283 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
284 */
285 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100286add_char_buff(buffheader_T *buf, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288 char_u bytes[MB_MAXBYTES + 1];
289 int len;
290 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291 char_u temp[4];
292
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293 if (IS_SPECIAL(c))
294 len = 1;
295 else
296 len = (*mb_char2bytes)(c, bytes);
297 for (i = 0; i < len; ++i)
298 {
299 if (!IS_SPECIAL(c))
300 c = bytes[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301
302 if (IS_SPECIAL(c) || c == K_SPECIAL || c == NUL)
303 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100304 // translate special key code into three byte sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305 temp[0] = K_SPECIAL;
306 temp[1] = K_SECOND(c);
307 temp[2] = K_THIRD(c);
308 temp[3] = NUL;
309 }
310#ifdef FEAT_GUI
311 else if (c == CSI)
312 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100313 // Translate a CSI to a CSI - KS_EXTRA - KE_CSI sequence
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314 temp[0] = CSI;
315 temp[1] = KS_EXTRA;
316 temp[2] = (int)KE_CSI;
317 temp[3] = NUL;
318 }
319#endif
320 else
321 {
322 temp[0] = c;
323 temp[1] = NUL;
324 }
325 add_buff(buf, temp, -1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327}
328
Bram Moolenaar30613902019-12-01 22:11:18 +0100329// First read ahead buffer. Used for translated commands.
Bram Moolenaar285e3352018-04-18 23:01:13 +0200330static buffheader_T readbuf1 = {{NULL, {NUL}}, NULL, 0, 0};
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100331
Bram Moolenaar30613902019-12-01 22:11:18 +0100332// Second read ahead buffer. Used for redo.
Bram Moolenaar285e3352018-04-18 23:01:13 +0200333static buffheader_T readbuf2 = {{NULL, {NUL}}, NULL, 0, 0};
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100334
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335/*
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100336 * Get one byte from the read buffers. Use readbuf1 one first, use readbuf2
337 * if that one is empty.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338 * If advance == TRUE go to the next char.
339 * No translation is done K_SPECIAL and CSI are escaped.
340 */
341 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100342read_readbuffers(int advance)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100344 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100346 c = read_readbuf(&readbuf1, advance);
347 if (c == NUL)
348 c = read_readbuf(&readbuf2, advance);
349 return c;
350}
351
352 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100353read_readbuf(buffheader_T *buf, int advance)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100354{
355 char_u c;
356 buffblock_T *curr;
357
Bram Moolenaar30613902019-12-01 22:11:18 +0100358 if (buf->bh_first.b_next == NULL) // buffer is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359 return NUL;
360
Bram Moolenaar285e3352018-04-18 23:01:13 +0200361 curr = buf->bh_first.b_next;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100362 c = curr->b_str[buf->bh_index];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363
364 if (advance)
365 {
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100366 if (curr->b_str[++buf->bh_index] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367 {
Bram Moolenaar285e3352018-04-18 23:01:13 +0200368 buf->bh_first.b_next = curr->b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369 vim_free(curr);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100370 buf->bh_index = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371 }
372 }
373 return c;
374}
375
376/*
Bram Moolenaar06811f32014-02-15 16:17:07 +0100377 * Prepare the read buffers for reading (if they contain something).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378 */
379 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100380start_stuff(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381{
Bram Moolenaar285e3352018-04-18 23:01:13 +0200382 if (readbuf1.bh_first.b_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 {
Bram Moolenaar285e3352018-04-18 23:01:13 +0200384 readbuf1.bh_curr = &(readbuf1.bh_first);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100385 readbuf1.bh_space = 0;
386 }
Bram Moolenaar285e3352018-04-18 23:01:13 +0200387 if (readbuf2.bh_first.b_next != NULL)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100388 {
Bram Moolenaar285e3352018-04-18 23:01:13 +0200389 readbuf2.bh_curr = &(readbuf2.bh_first);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100390 readbuf2.bh_space = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391 }
392}
393
394/*
395 * Return TRUE if the stuff buffer is empty.
396 */
397 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100398stuff_empty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399{
Bram Moolenaar285e3352018-04-18 23:01:13 +0200400 return (readbuf1.bh_first.b_next == NULL
401 && readbuf2.bh_first.b_next == NULL);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100402}
403
Bram Moolenaar113e1072019-01-20 15:30:40 +0100404#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100405/*
406 * Return TRUE if readbuf1 is empty. There may still be redo characters in
407 * redbuf2.
408 */
409 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100410readbuf1_empty(void)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100411{
Bram Moolenaar285e3352018-04-18 23:01:13 +0200412 return (readbuf1.bh_first.b_next == NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100414#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415
416/*
417 * Set a typeahead character that won't be flushed.
418 */
419 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100420typeahead_noflush(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421{
422 typeahead_char = c;
423}
424
425/*
426 * Remove the contents of the stuff buffer and the mapped characters in the
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100427 * typeahead buffer (used in case of an error). If "flush_typeahead" is true,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428 * flush all typeahead characters (used when interrupted by a CTRL-C).
429 */
430 void
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200431flush_buffers(flush_buffers_T flush_typeahead)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432{
433 init_typebuf();
434
435 start_stuff();
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100436 while (read_readbuffers(TRUE) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437 ;
438
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200439 if (flush_typeahead == FLUSH_MINIMAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 {
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200441 // remove mapped characters at the start only
442 typebuf.tb_off += typebuf.tb_maplen;
443 typebuf.tb_len -= typebuf.tb_maplen;
Bram Moolenaare49b4bb2020-03-11 13:01:40 +0100444#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
445 if (typebuf.tb_len == 0)
446 typebuf_was_filled = FALSE;
447#endif
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200448 }
449 else
450 {
451 // remove typeahead
452 if (flush_typeahead == FLUSH_INPUT)
453 // We have to get all characters, because we may delete the first
454 // part of an escape sequence. In an xterm we get one char at a
455 // time and we have to get them all.
456 while (inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 10L) != 0)
457 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458 typebuf.tb_off = MAXMAPLEN;
459 typebuf.tb_len = 0;
Bram Moolenaar4eb65312017-06-24 18:49:00 +0200460#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar30613902019-12-01 22:11:18 +0100461 // Reset the flag that text received from a client or from feedkeys()
462 // was inserted in the typeahead buffer.
Bram Moolenaar4eb65312017-06-24 18:49:00 +0200463 typebuf_was_filled = FALSE;
464#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466 typebuf.tb_maplen = 0;
467 typebuf.tb_silent = 0;
468 cmd_silent = FALSE;
469 typebuf.tb_no_abbr_cnt = 0;
Bram Moolenaarb8d732e2020-08-05 22:07:26 +0200470 if (++typebuf.tb_change_cnt == 0)
471 typebuf.tb_change_cnt = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472}
473
474/*
475 * The previous contents of the redo buffer is kept in old_redobuffer.
476 * This is used for the CTRL-O <.> command in insert mode.
477 */
478 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100479ResetRedobuff(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000481 if (block_redo)
482 return;
483
484 free_buff(&old_redobuff);
485 old_redobuff = redobuff;
486 redobuff.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487}
488
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100489/*
490 * Discard the contents of the redo buffer and restore the previous redo
491 * buffer.
492 */
493 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100494CancelRedo(void)
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100495{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000496 if (block_redo)
497 return;
498
499 free_buff(&redobuff);
500 redobuff = old_redobuff;
501 old_redobuff.bh_first.b_next = NULL;
502 start_stuff();
503 while (read_readbuffers(TRUE) != NUL)
504 ;
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100505}
506
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507/*
508 * Save redobuff and old_redobuff to save_redobuff and save_old_redobuff.
509 * Used before executing autocommands and user functions.
510 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 void
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200512saveRedobuff(save_redo_T *save_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513{
514 char_u *s;
515
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200516 save_redo->sr_redobuff = redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200517 redobuff.bh_first.b_next = NULL;
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200518 save_redo->sr_old_redobuff = old_redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200519 old_redobuff.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520
Bram Moolenaar30613902019-12-01 22:11:18 +0100521 // Make a copy, so that ":normal ." in a function works.
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200522 s = get_buffcont(&save_redo->sr_redobuff, FALSE);
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000523 if (s == NULL)
524 return;
525
526 add_buff(&redobuff, s, -1L);
527 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528}
529
530/*
531 * Restore redobuff and old_redobuff from save_redobuff and save_old_redobuff.
532 * Used after executing autocommands and user functions.
533 */
534 void
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200535restoreRedobuff(save_redo_T *save_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536{
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200537 free_buff(&redobuff);
538 redobuff = save_redo->sr_redobuff;
539 free_buff(&old_redobuff);
540 old_redobuff = save_redo->sr_old_redobuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542
543/*
544 * Append "s" to the redo buffer.
545 * K_SPECIAL and CSI should already have been escaped.
546 */
547 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100548AppendToRedobuff(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549{
550 if (!block_redo)
551 add_buff(&redobuff, s, -1L);
552}
553
554/*
555 * Append to Redo buffer literally, escaping special characters with CTRL-V.
556 * K_SPECIAL and CSI are escaped as well.
557 */
558 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100559AppendToRedobuffLit(
560 char_u *str,
Bram Moolenaar30613902019-12-01 22:11:18 +0100561 int len) // length of "str" or -1 for up to the NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562{
Bram Moolenaarebefac62005-12-28 22:39:57 +0000563 char_u *s = str;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564 int c;
565 char_u *start;
566
567 if (block_redo)
568 return;
569
Bram Moolenaarebefac62005-12-28 22:39:57 +0000570 while (len < 0 ? *s != NUL : s - str < len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100572 // Put a string of normal characters in the redo buffer (that's
573 // faster).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 start = s;
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000575 while (*s >= ' ' && *s < DEL && (len < 0 || s - str < len))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 ++s;
577
Bram Moolenaar30613902019-12-01 22:11:18 +0100578 // Don't put '0' or '^' as last character, just in case a CTRL-D is
579 // typed next.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 if (*s == NUL && (s[-1] == '0' || s[-1] == '^'))
581 --s;
582 if (s > start)
583 add_buff(&redobuff, start, (long)(s - start));
584
Bram Moolenaarebefac62005-12-28 22:39:57 +0000585 if (*s == NUL || (len >= 0 && s - str >= len))
586 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587
Bram Moolenaar30613902019-12-01 22:11:18 +0100588 // Handle a special or multibyte character.
Bram Moolenaarebefac62005-12-28 22:39:57 +0000589 if (has_mbyte)
Bram Moolenaar30613902019-12-01 22:11:18 +0100590 // Handle composing chars separately.
Bram Moolenaarebefac62005-12-28 22:39:57 +0000591 c = mb_cptr2char_adv(&s);
592 else
Bram Moolenaarebefac62005-12-28 22:39:57 +0000593 c = *s++;
594 if (c < ' ' || c == DEL || (*s == NUL && (c == '0' || c == '^')))
595 add_char_buff(&redobuff, Ctrl_V);
596
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000597 // CTRL-V '0' must be inserted as CTRL-V 048
Bram Moolenaarebefac62005-12-28 22:39:57 +0000598 if (*s == NUL && c == '0')
Bram Moolenaarebefac62005-12-28 22:39:57 +0000599 add_buff(&redobuff, (char_u *)"048", 3L);
Bram Moolenaarebefac62005-12-28 22:39:57 +0000600 else
601 add_char_buff(&redobuff, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 }
603}
604
605/*
606 * Append a character to the redo buffer.
607 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
608 */
609 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100610AppendCharToRedobuff(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611{
612 if (!block_redo)
613 add_char_buff(&redobuff, c);
614}
615
616/*
617 * Append a number to the redo buffer.
618 */
619 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100620AppendNumberToRedobuff(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621{
622 if (!block_redo)
623 add_num_buff(&redobuff, n);
624}
625
626/*
627 * Append string "s" to the stuff buffer.
628 * CSI and K_SPECIAL must already have been escaped.
629 */
630 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100631stuffReadbuff(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100633 add_buff(&readbuf1, s, -1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634}
635
Bram Moolenaar4f5ce332014-07-30 16:00:58 +0200636/*
637 * Append string "s" to the redo stuff buffer.
638 * CSI and K_SPECIAL must already have been escaped.
639 */
640 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100641stuffRedoReadbuff(char_u *s)
Bram Moolenaar4f5ce332014-07-30 16:00:58 +0200642{
643 add_buff(&readbuf2, s, -1L);
644}
645
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200646 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100647stuffReadbuffLen(char_u *s, long len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100649 add_buff(&readbuf1, s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650}
651
652#if defined(FEAT_EVAL) || defined(PROTO)
653/*
654 * Stuff "s" into the stuff buffer, leaving special key codes unmodified and
655 * escaping other K_SPECIAL and CSI bytes.
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200656 * Change CR, LF and ESC into a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 */
658 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100659stuffReadbuffSpec(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660{
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200661 int c;
662
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 while (*s != NUL)
664 {
665 if (*s == K_SPECIAL && s[1] != NUL && s[2] != NUL)
666 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100667 // Insert special key literally.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 stuffReadbuffLen(s, 3L);
669 s += 3;
670 }
671 else
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200672 {
zeertzjqe3a529b2022-06-05 19:01:37 +0100673 c = mb_cptr2char_adv(&s);
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200674 if (c == CAR || c == NL || c == ESC)
675 c = ' ';
676 stuffcharReadbuff(c);
677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 }
679}
680#endif
681
682/*
683 * Append a character to the stuff buffer.
684 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
685 */
686 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100687stuffcharReadbuff(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100689 add_char_buff(&readbuf1, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690}
691
692/*
693 * Append a number to the stuff buffer.
694 */
695 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100696stuffnumReadbuff(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100698 add_num_buff(&readbuf1, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699}
700
701/*
Bram Moolenaar11abd092020-05-01 14:26:37 +0200702 * Stuff a string into the typeahead buffer, such that edit() will insert it
703 * literally ("literally" TRUE) or interpret is as typed characters.
704 */
705 void
706stuffescaped(char_u *arg, int literally)
707{
708 int c;
709 char_u *start;
710
711 while (*arg != NUL)
712 {
713 // Stuff a sequence of normal ASCII characters, that's fast. Also
714 // stuff K_SPECIAL to get the effect of a special key when "literally"
715 // is TRUE.
716 start = arg;
Bram Moolenaar424bcae2022-01-31 14:59:41 +0000717 while ((*arg >= ' ' && *arg < DEL)
Bram Moolenaar11abd092020-05-01 14:26:37 +0200718 || (*arg == K_SPECIAL && !literally))
719 ++arg;
720 if (arg > start)
721 stuffReadbuffLen(start, (long)(arg - start));
722
723 // stuff a single special character
724 if (*arg != NUL)
725 {
726 if (has_mbyte)
727 c = mb_cptr2char_adv(&arg);
728 else
729 c = *arg++;
730 if (literally && ((c < ' ' && c != TAB) || c == DEL))
731 stuffcharReadbuff(Ctrl_V);
732 stuffcharReadbuff(c);
733 }
734 }
735}
736
737/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 * Read a character from the redo buffer. Translates K_SPECIAL, CSI and
739 * multibyte characters.
740 * The redo buffer is left as it is.
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100741 * If init is TRUE, prepare for redo, return FAIL if nothing to redo, OK
742 * otherwise.
743 * If old is TRUE, use old_redobuff instead of redobuff.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 */
745 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100746read_redo(int init, int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100748 static buffblock_T *bp;
749 static char_u *p;
750 int c;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100751 int n;
752 char_u buf[MB_MAXBYTES + 1];
753 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754
755 if (init)
756 {
757 if (old_redo)
Bram Moolenaar285e3352018-04-18 23:01:13 +0200758 bp = old_redobuff.bh_first.b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 else
Bram Moolenaar285e3352018-04-18 23:01:13 +0200760 bp = redobuff.bh_first.b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 if (bp == NULL)
762 return FAIL;
763 p = bp->b_str;
764 return OK;
765 }
766 if ((c = *p) != NUL)
767 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100768 // Reverse the conversion done by add_char_buff()
769 // For a multi-byte character get all the bytes and return the
770 // converted character.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 if (has_mbyte && (c != K_SPECIAL || p[1] == KS_SPECIAL))
772 n = MB_BYTE2LEN_CHECK(c);
773 else
774 n = 1;
775 for (i = 0; ; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100777 if (c == K_SPECIAL) // special key or escaped K_SPECIAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 {
779 c = TO_SPECIAL(p[1], p[2]);
780 p += 2;
781 }
782#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +0100783 if (c == CSI) // escaped CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 p += 2;
785#endif
786 if (*++p == NUL && bp->b_next != NULL)
787 {
788 bp = bp->b_next;
789 p = bp->b_str;
790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 buf[i] = c;
Bram Moolenaar30613902019-12-01 22:11:18 +0100792 if (i == n - 1) // last byte of a character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 {
794 if (n != 1)
795 c = (*mb_ptr2char)(buf);
796 break;
797 }
798 c = *p;
Bram Moolenaar30613902019-12-01 22:11:18 +0100799 if (c == NUL) // cannot happen?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 }
802 }
803
804 return c;
805}
806
807/*
808 * Copy the rest of the redo buffer into the stuff buffer (in a slow way).
809 * If old_redo is TRUE, use old_redobuff instead of redobuff.
810 * The escaped K_SPECIAL and CSI are copied without translation.
811 */
812 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100813copy_redo(int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814{
815 int c;
816
817 while ((c = read_redo(FALSE, old_redo)) != NUL)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100818 add_char_buff(&readbuf2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819}
820
821/*
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100822 * Stuff the redo buffer into readbuf2.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 * Insert the redo count into the command.
824 * If "old_redo" is TRUE, the last but one command is repeated
825 * instead of the last command (inserting text). This is used for
826 * CTRL-O <.> in insert mode
827 *
828 * return FAIL for failure, OK otherwise
829 */
830 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100831start_redo(long count, int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832{
833 int c;
834
Bram Moolenaar30613902019-12-01 22:11:18 +0100835 // init the pointers; return if nothing to redo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 if (read_redo(TRUE, old_redo) == FAIL)
837 return FAIL;
838
839 c = read_redo(FALSE, old_redo);
840
Bram Moolenaarddf7dba2022-09-05 16:53:21 +0100841#ifdef FEAT_EVAL
842 if (c == K_SID)
843 {
844 // Copy the <SID>{sid}; sequence
845 add_char_buff(&readbuf2, c);
846 for (;;)
847 {
848 c = read_redo(FALSE, old_redo);
849 add_char_buff(&readbuf2, c);
850 if (!isdigit(c))
851 break;
852 }
853 c = read_redo(FALSE, old_redo);
854 }
855#endif
856
Bram Moolenaar30613902019-12-01 22:11:18 +0100857 // copy the buffer name, if present
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 if (c == '"')
859 {
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100860 add_buff(&readbuf2, (char_u *)"\"", 1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 c = read_redo(FALSE, old_redo);
862
Bram Moolenaar30613902019-12-01 22:11:18 +0100863 // if a numbered buffer is used, increment the number
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 if (c >= '1' && c < '9')
865 ++c;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100866 add_char_buff(&readbuf2, c);
Bram Moolenaar833093b2018-05-23 21:53:52 +0200867
Bram Moolenaar30613902019-12-01 22:11:18 +0100868 // the expression register should be re-evaluated
Bram Moolenaar833093b2018-05-23 21:53:52 +0200869 if (c == '=')
870 {
871 add_char_buff(&readbuf2, CAR);
872 cmd_silent = TRUE;
873 }
874
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 c = read_redo(FALSE, old_redo);
876 }
877
Bram Moolenaar30613902019-12-01 22:11:18 +0100878 if (c == 'v') // redo Visual
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 {
880 VIsual = curwin->w_cursor;
881 VIsual_active = TRUE;
882 VIsual_select = FALSE;
883 VIsual_reselect = TRUE;
884 redo_VIsual_busy = TRUE;
885 c = read_redo(FALSE, old_redo);
886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887
Bram Moolenaar30613902019-12-01 22:11:18 +0100888 // try to enter the count (in place of a previous count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 if (count)
890 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100891 while (VIM_ISDIGIT(c)) // skip "old" count
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 c = read_redo(FALSE, old_redo);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100893 add_num_buff(&readbuf2, count);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 }
895
Bram Moolenaarddf7dba2022-09-05 16:53:21 +0100896 // copy the rest from the redo buffer into the stuff buffer
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100897 add_char_buff(&readbuf2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 copy_redo(old_redo);
899 return OK;
900}
901
902/*
903 * Repeat the last insert (R, o, O, a, A, i or I command) by stuffing
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100904 * the redo buffer into readbuf2.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 * return FAIL for failure, OK otherwise
906 */
907 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100908start_redo_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909{
910 int c;
911
912 if (read_redo(TRUE, FALSE) == FAIL)
913 return FAIL;
914 start_stuff();
915
Bram Moolenaar30613902019-12-01 22:11:18 +0100916 // skip the count and the command character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 while ((c = read_redo(FALSE, FALSE)) != NUL)
918 {
919 if (vim_strchr((char_u *)"AaIiRrOo", c) != NULL)
920 {
921 if (c == 'O' || c == 'o')
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100922 add_buff(&readbuf2, NL_STR, -1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 break;
924 }
925 }
926
Bram Moolenaar30613902019-12-01 22:11:18 +0100927 // copy the typed text from the redo buffer into the stuff buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 copy_redo(FALSE);
929 block_redo = TRUE;
930 return OK;
931}
932
933 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100934stop_redo_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935{
936 block_redo = FALSE;
937}
938
939/*
940 * Initialize typebuf.tb_buf to point to typebuf_init.
941 * alloc() cannot be used here: In out-of-memory situations it would
942 * be impossible to type anything.
943 */
944 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100945init_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000947 if (typebuf.tb_buf != NULL)
948 return;
949
950 typebuf.tb_buf = typebuf_init;
951 typebuf.tb_noremap = noremapbuf_init;
952 typebuf.tb_buflen = TYPELEN_INIT;
953 typebuf.tb_len = 0;
954 typebuf.tb_off = MAXMAPLEN + 4;
955 typebuf.tb_change_cnt = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956}
957
958/*
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200959 * Returns TRUE when keys cannot be remapped.
960 */
961 int
962noremap_keys(void)
963{
964 return KeyNoremap & (RM_NONE|RM_SCRIPT);
965}
966
967/*
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100968 * Insert a string in position 'offset' in the typeahead buffer (for "@r"
969 * and ":normal" command, vgetorpeek() and check_termcode()).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 *
Bram Moolenaar88456cd2022-11-18 22:14:09 +0000971 * If "noremap" is REMAP_YES, new string can be mapped again.
972 * If "noremap" is REMAP_NONE, new string cannot be mapped again.
973 * If "noremap" is REMAP_SKIP, first char of new string cannot be mapped again,
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000974 * but abbreviations are allowed.
Bram Moolenaar88456cd2022-11-18 22:14:09 +0000975 * If "noremap" is REMAP_SCRIPT, new string cannot be mapped again, except for
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 * script-local mappings.
Bram Moolenaar88456cd2022-11-18 22:14:09 +0000977 * If "noremap" is > 0, that many characters of the new string cannot be mapped.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 *
Bram Moolenaar88456cd2022-11-18 22:14:09 +0000979 * If "nottyped" is TRUE, the string does not return KeyTyped (don't use when
980 * "offset" is non-zero!).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 *
Bram Moolenaar88456cd2022-11-18 22:14:09 +0000982 * If "silent" is TRUE, cmd_silent is set when the characters are obtained.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 *
984 * return FAIL for failure, OK otherwise
985 */
986 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100987ins_typebuf(
988 char_u *str,
989 int noremap,
990 int offset,
991 int nottyped,
992 int silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993{
994 char_u *s1, *s2;
995 int newlen;
996 int addlen;
997 int i;
998 int newoff;
999 int val;
1000 int nrm;
1001
1002 init_typebuf();
1003 if (++typebuf.tb_change_cnt == 0)
1004 typebuf.tb_change_cnt = 1;
Bram Moolenaard103ee72019-09-18 21:15:31 +02001005 state_no_longer_safe("ins_typebuf()");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006
1007 addlen = (int)STRLEN(str);
Bram Moolenaar4e86cba2007-05-06 11:56:32 +00001008
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 if (offset == 0 && addlen <= typebuf.tb_off)
1010 {
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001011 /*
1012 * Easy case: there is room in front of typebuf.tb_buf[typebuf.tb_off]
1013 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 typebuf.tb_off -= addlen;
1015 mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen);
1016 }
Bram Moolenaard34f9b12017-04-08 18:41:13 +02001017 else if (typebuf.tb_len == 0 && typebuf.tb_buflen
1018 >= addlen + 3 * (MAXMAPLEN + 4))
1019 {
1020 /*
1021 * Buffer is empty and string fits in the existing buffer.
1022 * Leave some space before and after, if possible.
1023 */
1024 typebuf.tb_off = (typebuf.tb_buflen - addlen - 3 * (MAXMAPLEN + 4)) / 2;
1025 mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen);
1026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 else
1028 {
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001029 int extra;
1030
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001031 /*
1032 * Need to allocate a new buffer.
Bram Moolenaard34f9b12017-04-08 18:41:13 +02001033 * In typebuf.tb_buf there must always be room for 3 * (MAXMAPLEN + 4)
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001034 * characters. We add some extra room to avoid having to allocate too
1035 * often.
1036 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 newoff = MAXMAPLEN + 4;
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001038 extra = addlen + newoff + 4 * (MAXMAPLEN + 4);
1039 if (typebuf.tb_len > 2147483647 - extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 {
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001041 // string is getting too long for a 32 bit int
Bram Moolenaar12f3c1b2021-12-05 21:46:34 +00001042 emsg(_(e_command_too_complex)); // also calls flush_buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 setcursor();
1044 return FAIL;
1045 }
Bram Moolenaar0d5a12e2021-11-14 14:05:18 +00001046 newlen = typebuf.tb_len + extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 s1 = alloc(newlen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001048 if (s1 == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 return FAIL;
1050 s2 = alloc(newlen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001051 if (s2 == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 {
1053 vim_free(s1);
1054 return FAIL;
1055 }
1056 typebuf.tb_buflen = newlen;
1057
Bram Moolenaar30613902019-12-01 22:11:18 +01001058 // copy the old chars, before the insertion point
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 mch_memmove(s1 + newoff, typebuf.tb_buf + typebuf.tb_off,
1060 (size_t)offset);
Bram Moolenaar30613902019-12-01 22:11:18 +01001061 // copy the new chars
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 mch_memmove(s1 + newoff + offset, str, (size_t)addlen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001063 // copy the old chars, after the insertion point, including the NUL at
1064 // the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 mch_memmove(s1 + newoff + offset + addlen,
1066 typebuf.tb_buf + typebuf.tb_off + offset,
1067 (size_t)(typebuf.tb_len - offset + 1));
1068 if (typebuf.tb_buf != typebuf_init)
1069 vim_free(typebuf.tb_buf);
1070 typebuf.tb_buf = s1;
1071
1072 mch_memmove(s2 + newoff, typebuf.tb_noremap + typebuf.tb_off,
1073 (size_t)offset);
1074 mch_memmove(s2 + newoff + offset + addlen,
1075 typebuf.tb_noremap + typebuf.tb_off + offset,
1076 (size_t)(typebuf.tb_len - offset));
1077 if (typebuf.tb_noremap != noremapbuf_init)
1078 vim_free(typebuf.tb_noremap);
1079 typebuf.tb_noremap = s2;
1080
1081 typebuf.tb_off = newoff;
1082 }
1083 typebuf.tb_len += addlen;
1084
Bram Moolenaar30613902019-12-01 22:11:18 +01001085 // If noremap == REMAP_SCRIPT: do remap script-local mappings.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 if (noremap == REMAP_SCRIPT)
1087 val = RM_SCRIPT;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001088 else if (noremap == REMAP_SKIP)
1089 val = RM_ABBR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 else
1091 val = RM_NONE;
1092
1093 /*
1094 * Adjust typebuf.tb_noremap[] for the new characters:
1095 * If noremap == REMAP_NONE or REMAP_SCRIPT: new characters are
1096 * (sometimes) not remappable
1097 * If noremap == REMAP_YES: all the new characters are mappable
1098 * If noremap > 0: "noremap" characters are not remappable, the rest
1099 * mappable
1100 */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001101 if (noremap == REMAP_SKIP)
1102 nrm = 1;
1103 else if (noremap < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 nrm = addlen;
1105 else
1106 nrm = noremap;
1107 for (i = 0; i < addlen; ++i)
1108 typebuf.tb_noremap[typebuf.tb_off + i + offset] =
1109 (--nrm >= 0) ? val : RM_YES;
1110
Bram Moolenaar30613902019-12-01 22:11:18 +01001111 // tb_maplen and tb_silent only remember the length of mapped and/or
1112 // silent mappings at the start of the buffer, assuming that a mapped
1113 // sequence doesn't result in typed characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 if (nottyped || typebuf.tb_maplen > offset)
1115 typebuf.tb_maplen += addlen;
1116 if (silent || typebuf.tb_silent > offset)
1117 {
1118 typebuf.tb_silent += addlen;
1119 cmd_silent = TRUE;
1120 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001121 if (typebuf.tb_no_abbr_cnt && offset == 0) // and not used for abbrev.s
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 typebuf.tb_no_abbr_cnt += addlen;
1123
1124 return OK;
1125}
1126
1127/*
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001128 * Put character "c" back into the typeahead buffer.
1129 * Can be used for a character obtained by vgetc() that needs to be put back.
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00001130 * Uses cmd_silent, KeyTyped and KeyNoremap to restore the flags belonging to
1131 * the char.
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001132 * Returns the length of what was inserted.
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001133 */
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001134 int
zeertzjq2e7cba32022-06-10 15:30:32 +01001135ins_char_typebuf(int c, int modifiers)
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001136{
zeertzjq6cac7702022-01-04 18:01:21 +00001137 char_u buf[MB_MAXBYTES * 3 + 4];
zeertzjq2e7cba32022-06-10 15:30:32 +01001138 int len = special_to_buf(c, modifiers, TRUE, buf);
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001139
zeertzjq2e7cba32022-06-10 15:30:32 +01001140 buf[len] = NUL;
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00001141 (void)ins_typebuf(buf, KeyNoremap, 0, !KeyTyped, cmd_silent);
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001142 return len;
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001143}
1144
1145/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 * Return TRUE if the typeahead buffer was changed (while waiting for a
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001147 * character to arrive). Happens when a message was received from a client or
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001148 * from feedkeys().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 * But check in a more generic way to avoid trouble: When "typebuf.tb_buf"
1150 * changed it was reallocated and the old pointer can no longer be used.
1151 * Or "typebuf.tb_off" may have been changed and we would overwrite characters
1152 * that was just added.
1153 */
1154 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001155typebuf_changed(
Bram Moolenaar30613902019-12-01 22:11:18 +01001156 int tb_change_cnt) // old value of typebuf.tb_change_cnt
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157{
1158 return (tb_change_cnt != 0 && (typebuf.tb_change_cnt != tb_change_cnt
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001159#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
1160 || typebuf_was_filled
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161#endif
1162 ));
1163}
1164
1165/*
1166 * Return TRUE if there are no characters in the typeahead buffer that have
1167 * not been typed (result from a mapping or come from ":normal").
1168 */
1169 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001170typebuf_typed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171{
1172 return typebuf.tb_maplen == 0;
1173}
1174
1175/*
1176 * Return the number of characters that are mapped (or not typed).
1177 */
1178 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001179typebuf_maplen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180{
1181 return typebuf.tb_maplen;
1182}
1183
1184/*
1185 * remove "len" characters from typebuf.tb_buf[typebuf.tb_off + offset]
1186 */
1187 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001188del_typebuf(int len, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189{
1190 int i;
1191
1192 if (len == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01001193 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194
1195 typebuf.tb_len -= len;
1196
1197 /*
1198 * Easy case: Just increase typebuf.tb_off.
1199 */
1200 if (offset == 0 && typebuf.tb_buflen - (typebuf.tb_off + len)
1201 >= 3 * MAXMAPLEN + 3)
1202 typebuf.tb_off += len;
1203 /*
1204 * Have to move the characters in typebuf.tb_buf[] and typebuf.tb_noremap[]
1205 */
1206 else
1207 {
1208 i = typebuf.tb_off + offset;
1209 /*
1210 * Leave some extra room at the end to avoid reallocation.
1211 */
1212 if (typebuf.tb_off > MAXMAPLEN)
1213 {
1214 mch_memmove(typebuf.tb_buf + MAXMAPLEN,
1215 typebuf.tb_buf + typebuf.tb_off, (size_t)offset);
1216 mch_memmove(typebuf.tb_noremap + MAXMAPLEN,
1217 typebuf.tb_noremap + typebuf.tb_off, (size_t)offset);
1218 typebuf.tb_off = MAXMAPLEN;
1219 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001220 // adjust typebuf.tb_buf (include the NUL at the end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset,
1222 typebuf.tb_buf + i + len,
1223 (size_t)(typebuf.tb_len - offset + 1));
Bram Moolenaar30613902019-12-01 22:11:18 +01001224 // adjust typebuf.tb_noremap[]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 mch_memmove(typebuf.tb_noremap + typebuf.tb_off + offset,
1226 typebuf.tb_noremap + i + len,
1227 (size_t)(typebuf.tb_len - offset));
1228 }
1229
Bram Moolenaar30613902019-12-01 22:11:18 +01001230 if (typebuf.tb_maplen > offset) // adjust tb_maplen
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 {
1232 if (typebuf.tb_maplen < offset + len)
1233 typebuf.tb_maplen = offset;
1234 else
1235 typebuf.tb_maplen -= len;
1236 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001237 if (typebuf.tb_silent > offset) // adjust tb_silent
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 {
1239 if (typebuf.tb_silent < offset + len)
1240 typebuf.tb_silent = offset;
1241 else
1242 typebuf.tb_silent -= len;
1243 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001244 if (typebuf.tb_no_abbr_cnt > offset) // adjust tb_no_abbr_cnt
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 {
1246 if (typebuf.tb_no_abbr_cnt < offset + len)
1247 typebuf.tb_no_abbr_cnt = offset;
1248 else
1249 typebuf.tb_no_abbr_cnt -= len;
1250 }
1251
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001252#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar30613902019-12-01 22:11:18 +01001253 // Reset the flag that text received from a client or from feedkeys()
1254 // was inserted in the typeahead buffer.
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001255 typebuf_was_filled = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256#endif
1257 if (++typebuf.tb_change_cnt == 0)
1258 typebuf.tb_change_cnt = 1;
1259}
1260
1261/*
1262 * Write typed characters to script file.
1263 * If recording is on put the character in the recordbuffer.
1264 */
1265 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001266gotchars(char_u *chars, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267{
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001268 char_u *s = chars;
1269 int i;
1270 static char_u buf[4];
1271 static int buflen = 0;
1272 int todo = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273
Bram Moolenaar4e86cba2007-05-06 11:56:32 +00001274 while (todo--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 {
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001276 buf[buflen++] = *s++;
1277
1278 // When receiving a special key sequence, store it until we have all
1279 // the bytes and we can decide what to do with it.
1280 if (buflen == 1 && buf[0] == K_SPECIAL)
1281 continue;
1282 if (buflen == 2)
1283 continue;
1284 if (buflen == 3 && buf[1] == KS_EXTRA
1285 && (buf[2] == KE_FOCUSGAINED || buf[2] == KE_FOCUSLOST))
1286 {
1287 // Drop K_FOCUSGAINED and K_FOCUSLOST, they are not useful in a
1288 // recording.
1289 buflen = 0;
1290 continue;
1291 }
1292
Bram Moolenaar30613902019-12-01 22:11:18 +01001293 // Handle one byte at a time; no translation to be done.
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001294 for (i = 0; i < buflen; ++i)
1295 updatescript(buf[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001297 if (reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 {
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001299 buf[buflen] = NUL;
1300 add_buff(&recordbuff, buf, (long)buflen);
Bram Moolenaar30613902019-12-01 22:11:18 +01001301 // remember how many chars were last recorded
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001302 last_recorded_len += buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 }
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001304 buflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 }
1306 may_sync_undo();
1307
1308#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01001309 // output "debug mode" message next time in debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 debug_did_msg = FALSE;
1311#endif
1312
Bram Moolenaar30613902019-12-01 22:11:18 +01001313 // Since characters have been typed, consider the following to be in
1314 // another mapping. Search string will be kept in history.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 ++maptick;
1316}
1317
1318/*
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001319 * Undo the last gotchars() for "len" bytes. To be used when putting a typed
1320 * character back into the typeahead buffer, thus gotchars() will be called
1321 * again.
1322 * Only affects recorded characters.
1323 */
1324 void
1325ungetchars(int len)
1326{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001327 if (reg_recording == 0)
1328 return;
1329
1330 delete_buff_tail(&recordbuff, len);
1331 last_recorded_len -= len;
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001332}
1333
1334/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 * Sync undo. Called when typed characters are obtained from the typeahead
1336 * buffer, or when a menu is used.
1337 * Do not sync:
1338 * - In Insert mode, unless cursor key has been used.
1339 * - While reading a script file.
1340 * - When no_u_sync is non-zero.
1341 */
1342 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001343may_sync_undo(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344{
Bram Moolenaar24959102022-05-07 20:01:16 +01001345 if ((!(State & (MODE_INSERT | MODE_CMDLINE)) || arrow_used)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001346 && scriptin[curscript] == NULL)
1347 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348}
1349
1350/*
1351 * Make "typebuf" empty and allocate new buffers.
1352 * Returns FAIL when out of memory.
1353 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001354 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001355alloc_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356{
1357 typebuf.tb_buf = alloc(TYPELEN_INIT);
1358 typebuf.tb_noremap = alloc(TYPELEN_INIT);
1359 if (typebuf.tb_buf == NULL || typebuf.tb_noremap == NULL)
1360 {
1361 free_typebuf();
1362 return FAIL;
1363 }
1364 typebuf.tb_buflen = TYPELEN_INIT;
Bram Moolenaar30613902019-12-01 22:11:18 +01001365 typebuf.tb_off = MAXMAPLEN + 4; // can insert without realloc
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 typebuf.tb_len = 0;
1367 typebuf.tb_maplen = 0;
1368 typebuf.tb_silent = 0;
1369 typebuf.tb_no_abbr_cnt = 0;
1370 if (++typebuf.tb_change_cnt == 0)
1371 typebuf.tb_change_cnt = 1;
Bram Moolenaare49b4bb2020-03-11 13:01:40 +01001372#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
1373 typebuf_was_filled = FALSE;
1374#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 return OK;
1376}
1377
1378/*
1379 * Free the buffers of "typebuf".
1380 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001381 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001382free_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383{
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001384 if (typebuf.tb_buf == typebuf_init)
Bram Moolenaar95f09602016-11-10 20:01:45 +01001385 internal_error("Free typebuf 1");
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001386 else
Bram Moolenaar0f1c6702019-09-28 15:24:00 +02001387 VIM_CLEAR(typebuf.tb_buf);
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00001388 if (typebuf.tb_noremap == noremapbuf_init)
Bram Moolenaar95f09602016-11-10 20:01:45 +01001389 internal_error("Free typebuf 2");
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001390 else
Bram Moolenaar0f1c6702019-09-28 15:24:00 +02001391 VIM_CLEAR(typebuf.tb_noremap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392}
1393
1394/*
1395 * When doing ":so! file", the current typeahead needs to be saved, and
1396 * restored when "file" has been read completely.
1397 */
1398static typebuf_T saved_typebuf[NSCRIPT];
1399
1400 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001401save_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402{
1403 init_typebuf();
1404 saved_typebuf[curscript] = typebuf;
Bram Moolenaar30613902019-12-01 22:11:18 +01001405 // If out of memory: restore typebuf and close file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 if (alloc_typebuf() == FAIL)
1407 {
1408 closescript();
1409 return FAIL;
1410 }
1411 return OK;
1412}
1413
Bram Moolenaar30613902019-12-01 22:11:18 +01001414static int old_char = -1; // character put back by vungetc()
1415static int old_mod_mask; // mod_mask for ungotten character
1416static int old_mouse_row; // mouse_row related to old_char
1417static int old_mouse_col; // mouse_col related to old_char
zeertzjq0f68e6c2022-04-05 13:17:01 +01001418static int old_KeyStuffed; // whether old_char was stuffed
1419
zeertzjq6d4e7252022-04-07 13:58:04 +01001420static int can_get_old_char(void)
zeertzjq0f68e6c2022-04-05 13:17:01 +01001421{
1422 // If the old character was not stuffed and characters have been added to
1423 // the stuff buffer, need to first get the stuffed characters instead.
1424 return old_char != -1 && (old_KeyStuffed || stuff_empty());
1425}
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001426
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427/*
1428 * Save all three kinds of typeahead, so that the user must type at a prompt.
1429 */
1430 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001431save_typeahead(tasave_T *tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432{
1433 tp->save_typebuf = typebuf;
1434 tp->typebuf_valid = (alloc_typebuf() == OK);
1435 if (!tp->typebuf_valid)
1436 typebuf = tp->save_typebuf;
1437
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001438 tp->old_char = old_char;
1439 tp->old_mod_mask = old_mod_mask;
1440 old_char = -1;
1441
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001442 tp->save_readbuf1 = readbuf1;
Bram Moolenaar285e3352018-04-18 23:01:13 +02001443 readbuf1.bh_first.b_next = NULL;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001444 tp->save_readbuf2 = readbuf2;
Bram Moolenaar285e3352018-04-18 23:01:13 +02001445 readbuf2.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446# ifdef USE_INPUT_BUF
1447 tp->save_inputbuf = get_input_buf();
1448# endif
1449}
1450
1451/*
1452 * Restore the typeahead to what it was before calling save_typeahead().
1453 * The allocated memory is freed, can only be called once!
Bram Moolenaarc41badb2021-06-07 22:04:52 +02001454 * When "overwrite" is FALSE input typed later is kept.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 */
1456 void
Bram Moolenaarc41badb2021-06-07 22:04:52 +02001457restore_typeahead(tasave_T *tp, int overwrite UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458{
1459 if (tp->typebuf_valid)
1460 {
1461 free_typebuf();
1462 typebuf = tp->save_typebuf;
1463 }
1464
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001465 old_char = tp->old_char;
1466 old_mod_mask = tp->old_mod_mask;
1467
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001468 free_buff(&readbuf1);
1469 readbuf1 = tp->save_readbuf1;
1470 free_buff(&readbuf2);
1471 readbuf2 = tp->save_readbuf2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472# ifdef USE_INPUT_BUF
Bram Moolenaarc41badb2021-06-07 22:04:52 +02001473 set_input_buf(tp->save_inputbuf, overwrite);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474# endif
1475}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476
1477/*
1478 * Open a new script file for the ":source!" command.
1479 */
1480 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001481openscript(
1482 char_u *name,
Bram Moolenaar30613902019-12-01 22:11:18 +01001483 int directly) // when TRUE execute directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484{
1485 if (curscript + 1 == NSCRIPT)
1486 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001487 emsg(_(e_scripts_nested_too_deep));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 return;
1489 }
Bram Moolenaar53575522019-05-22 22:38:25 +02001490
1491 // Disallow sourcing a file in the sandbox, the commands would be executed
1492 // later, possibly outside of the sandbox.
1493 if (check_secure())
1494 return;
1495
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001496#ifdef FEAT_EVAL
1497 if (ignore_script)
Bram Moolenaar30613902019-12-01 22:11:18 +01001498 // Not reading from script, also don't open one. Warning message?
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001499 return;
1500#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501
Bram Moolenaar30613902019-12-01 22:11:18 +01001502 if (scriptin[curscript] != NULL) // already reading script
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 ++curscript;
Bram Moolenaar30613902019-12-01 22:11:18 +01001504 // use NameBuff for expanded name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 expand_env(name, NameBuff, MAXPATHL);
1506 if ((scriptin[curscript] = mch_fopen((char *)NameBuff, READBIN)) == NULL)
1507 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001508 semsg(_(e_cant_open_file_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 if (curscript)
1510 --curscript;
1511 return;
1512 }
1513 if (save_typebuf() == FAIL)
1514 return;
1515
1516 /*
1517 * Execute the commands from the file right now when using ":source!"
1518 * after ":global" or ":argdo" or in a loop. Also when another command
1519 * follows. This means the display won't be updated. Don't do this
1520 * always, "make test" would fail.
1521 */
1522 if (directly)
1523 {
1524 oparg_T oa;
1525 int oldcurscript;
1526 int save_State = State;
1527 int save_restart_edit = restart_edit;
1528 int save_insertmode = p_im;
1529 int save_finish_op = finish_op;
1530 int save_msg_scroll = msg_scroll;
1531
Bram Moolenaar24959102022-05-07 20:01:16 +01001532 State = MODE_NORMAL;
Bram Moolenaar30613902019-12-01 22:11:18 +01001533 msg_scroll = FALSE; // no msg scrolling in Normal mode
1534 restart_edit = 0; // don't go to Insert mode
1535 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 clear_oparg(&oa);
1537 finish_op = FALSE;
1538
1539 oldcurscript = curscript;
1540 do
1541 {
Bram Moolenaar386b43e2019-05-19 21:57:11 +02001542 update_topline_cursor(); // update cursor position and topline
1543 normal_cmd(&oa, FALSE); // execute one command
Bram Moolenaarae97b942020-07-09 19:16:35 +02001544 (void)vpeekc(); // check for end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 }
1546 while (scriptin[oldcurscript] != NULL);
1547
1548 State = save_State;
1549 msg_scroll = save_msg_scroll;
1550 restart_edit = save_restart_edit;
1551 p_im = save_insertmode;
1552 finish_op = save_finish_op;
1553 }
1554}
1555
1556/*
1557 * Close the currently active input script.
1558 */
1559 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001560closescript(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561{
1562 free_typebuf();
1563 typebuf = saved_typebuf[curscript];
1564
1565 fclose(scriptin[curscript]);
1566 scriptin[curscript] = NULL;
1567 if (curscript > 0)
1568 --curscript;
1569}
1570
Bram Moolenaarea408852005-06-25 22:49:46 +00001571#if defined(EXITFREE) || defined(PROTO)
1572 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001573close_all_scripts(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00001574{
1575 while (scriptin[0] != NULL)
1576 closescript();
1577}
1578#endif
1579
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580/*
1581 * Return TRUE when reading keys from a script file.
1582 */
1583 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001584using_script(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585{
1586 return scriptin[curscript] != NULL;
1587}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588
1589/*
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001590 * This function is called just before doing a blocking wait. Thus after
1591 * waiting 'updatetime' for a character to arrive.
1592 */
1593 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001594before_blocking(void)
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001595{
1596 updatescript(0);
1597#ifdef FEAT_EVAL
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001598 if (may_garbage_collect)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001599 garbage_collect(FALSE);
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001600#endif
1601}
1602
1603/*
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001604 * updatescript() is called when a character can be written into the script
1605 * file or when we have waited some time for a character (c == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 *
1607 * All the changed memfiles are synced if c == 0 or when the number of typed
1608 * characters reaches 'updatecount' and 'updatecount' is non-zero.
1609 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001610 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001611updatescript(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612{
1613 static int count = 0;
1614
1615 if (c && scriptout)
1616 putc(c, scriptout);
1617 if (c == 0 || (p_uc > 0 && ++count >= p_uc))
1618 {
1619 ml_sync_all(c == 0, TRUE);
1620 count = 0;
1621 }
1622}
1623
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624/*
Bram Moolenaar975a8802020-06-06 22:36:24 +02001625 * Convert "c" plus "modifiers" to merge the effect of modifyOtherKeys into the
Bram Moolenaarea83c192023-03-18 17:22:46 +00001626 * character. Also for when the Kitty key protocol is used.
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001627 */
1628 int
Bram Moolenaar975a8802020-06-06 22:36:24 +02001629merge_modifyOtherKeys(int c_arg, int *modifiers)
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001630{
1631 int c = c_arg;
1632
Bram Moolenaarea83c192023-03-18 17:22:46 +00001633 // CTRL only uses the lower 5 bits of the character.
zeertzjqbad8a012022-04-29 16:44:00 +01001634 if (*modifiers & MOD_MASK_CTRL)
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001635 {
1636 if ((c >= '`' && c <= 0x7f) || (c >= '@' && c <= '_'))
zeertzjq17c95d92022-04-26 12:51:07 +01001637 {
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001638 c &= 0x1f;
zeertzjq17c95d92022-04-26 12:51:07 +01001639 if (c == NUL)
1640 c = K_ZERO;
1641 }
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001642 else if (c == '6')
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001643 // CTRL-6 is equivalent to CTRL-^
1644 c = 0x1e;
Bram Moolenaarf4ae6b22020-05-30 19:52:46 +02001645#ifdef FEAT_GUI_GTK
1646 // These mappings look arbitrary at the first glance, but in fact
1647 // resemble quite exactly the behaviour of the GTK+ 1.2 GUI on my
1648 // machine. The only difference is BS vs. DEL for CTRL-8 (makes
1649 // more sense and is consistent with usual terminal behaviour).
1650 else if (c == '2')
1651 c = NUL;
1652 else if (c >= '3' && c <= '7')
1653 c = c ^ 0x28;
1654 else if (c == '8')
1655 c = BS;
1656 else if (c == '?')
1657 c = DEL;
1658#endif
1659 if (c != c_arg)
Bram Moolenaar975a8802020-06-06 22:36:24 +02001660 *modifiers &= ~MOD_MASK_CTRL;
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001661 }
Bram Moolenaarea83c192023-03-18 17:22:46 +00001662
1663 // Alt/Meta sets the 8th bit of the character.
Bram Moolenaar975a8802020-06-06 22:36:24 +02001664 if ((*modifiers & (MOD_MASK_META | MOD_MASK_ALT))
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001665 && c >= 0 && c <= 127)
1666 {
Bram Moolenaarea83c192023-03-18 17:22:46 +00001667 // Some terminals (esp. Kitty) do not include Shift in the character.
1668 // Apply it here to get consistency across terminals. Only do ASCII
1669 // letters, for other characters it depends on the keyboard layout.
1670 if ((*modifiers & MOD_MASK_SHIFT) && c >= 'a' && c <= 'z')
1671 {
1672 c += 'a' - 'A';
1673 *modifiers &= ~MOD_MASK_SHIFT;
1674 }
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001675 c += 0x80;
Bram Moolenaara9a6b032023-02-05 18:00:42 +00001676 *modifiers &= ~(MOD_MASK_META | MOD_MASK_ALT);
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001677 }
Bram Moolenaarea83c192023-03-18 17:22:46 +00001678
Bram Moolenaarfc4ea2a2019-11-26 19:33:22 +01001679 return c;
1680}
1681
1682/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 * Get the next input character.
1684 * Can return a special key or a multi-byte character.
1685 * Can return NUL when called recursively, use safe_vgetc() if that's not
1686 * wanted.
1687 * This translates escaped K_SPECIAL and CSI bytes to a K_SPECIAL or CSI byte.
1688 * Collects the bytes of a multibyte character into the whole character.
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00001689 * Returns the modifiers in the global "mod_mask".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 */
1691 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001692vgetc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693{
1694 int c, c2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001696 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001699#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01001700 // Do garbage collection when garbagecollect() was called previously and
1701 // we are now at the toplevel.
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001702 if (may_garbage_collect && want_garbage_collect)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001703 garbage_collect(FALSE);
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001704#endif
1705
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 /*
1707 * If a character was put back with vungetc, it was already processed.
1708 * Return it directly.
1709 */
zeertzjq0f68e6c2022-04-05 13:17:01 +01001710 if (can_get_old_char())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 {
1712 c = old_char;
1713 old_char = -1;
1714 mod_mask = old_mod_mask;
Bram Moolenaarb8978712013-03-16 21:42:16 +01001715 mouse_row = old_mouse_row;
1716 mouse_col = old_mouse_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001718 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 {
zeertzjq81b46a62022-04-09 17:58:49 +01001720 // number of characters recorded from the last vgetc() call
1721 static int last_vgetc_recorded_len = 0;
1722
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001723 mod_mask = 0;
1724 vgetc_mod_mask = 0;
1725 vgetc_char = 0;
zeertzjq81b46a62022-04-09 17:58:49 +01001726
1727 // last_recorded_len can be larger than last_vgetc_recorded_len
1728 // if peeking records more
1729 last_recorded_len -= last_vgetc_recorded_len;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001730
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001731 for (;;) // this is done twice if there are modifiers
1732 {
1733 int did_inc = FALSE;
Bram Moolenaar2455c4e2015-09-15 18:29:39 +02001734
Bram Moolenaar78aed952022-09-24 15:36:35 +01001735 // No mapping after modifier has been read, using an input method
1736 // and when a popup window has disabled mapping.
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001737 if (mod_mask
Bram Moolenaar39719052017-09-05 22:20:46 +02001738#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001739 || im_is_preediting()
Bram Moolenaar39719052017-09-05 22:20:46 +02001740#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001741#if defined(FEAT_PROP_POPUP)
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001742 || popup_no_mapping()
1743#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001744 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 {
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001746 ++no_mapping;
1747 ++allow_keys;
Bram Moolenaar78aed952022-09-24 15:36:35 +01001748 // mod_mask value may change, remember we did the increment
1749 did_inc = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 }
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01001751 c = vgetorpeek(TRUE);
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001752 if (did_inc)
1753 {
1754 --no_mapping;
1755 --allow_keys;
1756 }
1757
Christopher Plewright03193062022-11-22 12:58:27 +00001758 // Get two extra bytes for special keys, handle modifiers.
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001759 if (c == K_SPECIAL
Christopher Plewright03193062022-11-22 12:58:27 +00001760#ifdef FEAT_GUI
Christopher Plewright605d02a2022-10-19 11:54:46 +01001761 || c == CSI
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001762#endif
1763 )
1764 {
1765 int save_allow_keys = allow_keys;
1766
1767 ++no_mapping;
1768 allow_keys = 0; // make sure BS is not found
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01001769 c2 = vgetorpeek(TRUE); // no mapping for these chars
1770 c = vgetorpeek(TRUE);
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001771 --no_mapping;
1772 allow_keys = save_allow_keys;
1773 if (c2 == KS_MODIFIER)
1774 {
1775 mod_mask = c;
1776 continue;
1777 }
1778 c = TO_SPECIAL(c2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779
Bram Moolenaardffa6ea2022-11-29 20:33:20 +00001780 // K_ESC is used to avoid ambiguity with the single Esc
1781 // character that might be the start of an escape sequence.
1782 // Convert it back to a single Esc here.
1783 if (c == K_ESC)
1784 c = ESC;
1785
Bram Moolenaar4f974752019-02-17 17:44:42 +01001786#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001787 // Handle K_TEAROFF here, the caller of vgetc() doesn't need to
1788 // know that a menu was torn off
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001789 if (
1790# ifdef VIMDLL
1791 gui.in_use &&
1792# endif
1793 c == K_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 {
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001795 char_u name[200];
K.Takata54119102022-02-03 13:33:03 +00001796 int j;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001797
1798 // get menu path, it ends with a <CR>
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01001799 for (j = 0; (c = vgetorpeek(TRUE)) != '\r'; )
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001800 {
K.Takata54119102022-02-03 13:33:03 +00001801 name[j] = c;
1802 if (j < 199)
1803 ++j;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001804 }
K.Takata54119102022-02-03 13:33:03 +00001805 name[j] = NUL;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001806 gui_make_tearoff(name);
1807 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809#endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001810#if defined(FEAT_GUI) && defined(FEAT_GUI_GTK) && defined(FEAT_MENU)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001811 // GTK: <F10> normally selects the menu, but it's passed until
1812 // here to allow mapping it. Intercept and invoke the GTK
1813 // behavior if it's not mapped.
1814 if (c == K_F10 && gui.menubar != NULL)
1815 {
1816 gtk_menu_shell_select_first(
1817 GTK_MENU_SHELL(gui.menubar), FALSE);
1818 continue;
1819 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001820#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821#ifdef FEAT_GUI
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001822 // Handle focus event here, so that the caller doesn't need to
1823 // know about it. Return K_IGNORE so that we loop once (needed
1824 // if 'lazyredraw' is set).
1825 if (c == K_FOCUSGAINED || c == K_FOCUSLOST)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001826 {
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001827 ui_focus_change(c == K_FOCUSGAINED);
1828 c = K_IGNORE;
Bram Moolenaara9dd2d32019-04-29 21:58:41 +02001829 }
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001830
1831 // Translate K_CSI to CSI. The special key is only used to
1832 // avoid it being recognized as the start of a special key.
1833 if (c == K_CSI)
1834 c = CSI;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835#endif
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01001836#ifdef FEAT_EVAL
1837 if (c == K_SID)
1838 {
1839 int j;
1840
1841 // Handle <SID>{sid}; Do up to 20 digits for safety.
1842 last_used_sid = 0;
1843 for (j = 0; j < 20 && isdigit(c = vgetorpeek(TRUE)); ++j)
1844 last_used_sid = last_used_sid * 10 + (c - '0');
1845 last_used_map = NULL;
1846 continue;
1847 }
1848#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001849 }
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01001850
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001851 // a keypad or special function key was not mapped, use it like
1852 // its ASCII equivalent
1853 switch (c)
1854 {
1855 case K_KPLUS: c = '+'; break;
1856 case K_KMINUS: c = '-'; break;
1857 case K_KDIVIDE: c = '/'; break;
1858 case K_KMULTIPLY: c = '*'; break;
1859 case K_KENTER: c = CAR; break;
1860 case K_KPOINT:
Bram Moolenaar4f974752019-02-17 17:44:42 +01001861#ifdef MSWIN
Bram Moolenaar8e85db02018-07-27 23:16:51 +02001862 // Can be either '.' or a ',',
1863 // depending on the type of keypad.
1864 c = MapVirtualKey(VK_DECIMAL, 2); break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001865#else
Bram Moolenaar8e85db02018-07-27 23:16:51 +02001866 c = '.'; break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001867#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001868 case K_K0: c = '0'; break;
1869 case K_K1: c = '1'; break;
1870 case K_K2: c = '2'; break;
1871 case K_K3: c = '3'; break;
1872 case K_K4: c = '4'; break;
1873 case K_K5: c = '5'; break;
1874 case K_K6: c = '6'; break;
1875 case K_K7: c = '7'; break;
1876 case K_K8: c = '8'; break;
1877 case K_K9: c = '9'; break;
Bram Moolenaara88d9682005-03-25 21:45:43 +00001878
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001879 case K_XHOME:
1880 case K_ZHOME: if (mod_mask == MOD_MASK_SHIFT)
Bram Moolenaara88d9682005-03-25 21:45:43 +00001881 {
1882 c = K_S_HOME;
1883 mod_mask = 0;
1884 }
1885 else if (mod_mask == MOD_MASK_CTRL)
1886 {
1887 c = K_C_HOME;
1888 mod_mask = 0;
1889 }
1890 else
1891 c = K_HOME;
1892 break;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001893 case K_XEND:
1894 case K_ZEND: if (mod_mask == MOD_MASK_SHIFT)
Bram Moolenaara88d9682005-03-25 21:45:43 +00001895 {
1896 c = K_S_END;
1897 mod_mask = 0;
1898 }
1899 else if (mod_mask == MOD_MASK_CTRL)
1900 {
1901 c = K_C_END;
1902 mod_mask = 0;
1903 }
1904 else
1905 c = K_END;
1906 break;
1907
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001908 case K_XUP: c = K_UP; break;
1909 case K_XDOWN: c = K_DOWN; break;
1910 case K_XLEFT: c = K_LEFT; break;
1911 case K_XRIGHT: c = K_RIGHT; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001914 // For a multi-byte character get all the bytes and return the
1915 // converted character.
1916 // Note: This will loop until enough bytes are received!
1917 if (has_mbyte && (n = MB_BYTE2LEN_CHECK(c)) > 1)
1918 {
1919 ++no_mapping;
1920 buf[0] = c;
1921 for (i = 1; i < n; ++i)
1922 {
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01001923 buf[i] = vgetorpeek(TRUE);
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001924 if (buf[i] == K_SPECIAL
1925#ifdef FEAT_GUI
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01001926 || (buf[i] == CSI)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001927#endif
1928 )
1929 {
1930 // Must be a K_SPECIAL - KS_SPECIAL - KE_FILLER
1931 // sequence, which represents a K_SPECIAL (0x80),
1932 // or a CSI - KS_EXTRA - KE_CSI sequence, which
1933 // represents a CSI (0x9B),
1934 // or a K_SPECIAL - KS_EXTRA - KE_CSI, which is CSI
1935 // too.
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01001936 c = vgetorpeek(TRUE);
1937 if (vgetorpeek(TRUE) == KE_CSI && c == KS_EXTRA)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001938 buf[i] = CSI;
1939 }
1940 }
1941 --no_mapping;
1942 c = (*mb_ptr2char)(buf);
1943 }
1944
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02001945 if (vgetc_char == 0)
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001946 {
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001947 vgetc_mod_mask = mod_mask;
1948 vgetc_char = c;
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02001949 }
Bram Moolenaar00eab7f2019-10-10 21:49:28 +02001950
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001951 break;
1952 }
zeertzjq81b46a62022-04-09 17:58:49 +01001953
1954 last_vgetc_recorded_len = last_recorded_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001956
1957#ifdef FEAT_EVAL
1958 /*
1959 * In the main loop "may_garbage_collect" can be set to do garbage
1960 * collection in the first next vgetc(). It's disabled after that to
1961 * avoid internally used Lists and Dicts to be freed.
1962 */
1963 may_garbage_collect = FALSE;
1964#endif
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001965
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001966#ifdef FEAT_BEVAL_TERM
Bram Moolenaarc2f50542019-07-05 23:24:56 +02001967 if (c != K_MOUSEMOVE && c != K_IGNORE && c != K_CURSORHOLD)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001968 {
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001969 // Don't trigger 'balloonexpr' unless only the mouse was moved.
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001970 bevalexpr_due_set = FALSE;
1971 ui_remove_balloon();
1972 }
1973#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001974#ifdef FEAT_PROP_POPUP
Bram Moolenaar4934ad02020-09-28 22:29:58 +02001975 // Only filter keys that do not come from ":normal". Keys from feedkeys()
1976 // are filtered.
1977 if ((!ex_normal_busy || in_feedkeys) && popup_do_filter(c))
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02001978 {
1979 if (c == Ctrl_C)
1980 got_int = FALSE; // avoid looping
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001981 c = K_IGNORE;
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02001982 }
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001983#endif
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001984
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001985 // Need to process the character before we know it's safe to do something
1986 // else.
1987 if (c != K_IGNORE)
Bram Moolenaard103ee72019-09-18 21:15:31 +02001988 state_no_longer_safe("key typed");
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001989
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001990 return c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991}
1992
1993/*
1994 * Like vgetc(), but never return a NUL when called recursively, get a key
1995 * directly from the user (ignoring typeahead).
1996 */
1997 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001998safe_vgetc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999{
2000 int c;
2001
2002 c = vgetc();
2003 if (c == NUL)
2004 c = get_keystroke();
2005 return c;
2006}
2007
2008/*
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002009 * Like safe_vgetc(), but loop to handle K_IGNORE.
2010 * Also ignore scrollbar events.
Bram Moolenaar78aed952022-09-24 15:36:35 +01002011 * Does not handle bracketed paste - do not use the result for commands.
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002012 */
Bram Moolenaar78aed952022-09-24 15:36:35 +01002013 static int
2014plain_vgetc_nopaste(void)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002015{
2016 int c;
2017
2018 do
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002019 c = safe_vgetc();
Bram Moolenaar3a006592021-05-05 19:58:17 +02002020 while (c == K_IGNORE
2021 || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR
2022 || c == K_MOUSEMOVE);
Bram Moolenaar78aed952022-09-24 15:36:35 +01002023 return c;
2024}
2025
2026/*
2027 * Like safe_vgetc(), but loop to handle K_IGNORE.
2028 * Also ignore scrollbar events.
2029 */
2030 int
2031plain_vgetc(void)
2032{
2033 int c = plain_vgetc_nopaste();
Bram Moolenaarec2da362017-01-21 20:04:22 +01002034
2035 if (c == K_PS)
Bram Moolenaar30613902019-12-01 22:11:18 +01002036 // Only handle the first pasted character. Drop the rest, since we
2037 // don't know what to do with it.
Bram Moolenaarec2da362017-01-21 20:04:22 +01002038 c = bracketed_paste(PASTE_ONE_CHAR, FALSE, NULL);
2039
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002040 return c;
2041}
2042
2043/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 * Check if a character is available, such that vgetc() will not block.
2045 * If the next character is a special character or multi-byte, the returned
2046 * character is not valid!.
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02002047 * Returns NUL if no character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 */
2049 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002050vpeekc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051{
zeertzjq0f68e6c2022-04-05 13:17:01 +01002052 if (can_get_old_char())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 return old_char;
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01002054 return vgetorpeek(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055}
2056
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01002057#if defined(FEAT_TERMRESPONSE) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058/*
2059 * Like vpeekc(), but don't allow mapping. Do allow checking for terminal
2060 * codes.
2061 */
2062 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002063vpeekc_nomap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064{
2065 int c;
2066
2067 ++no_mapping;
2068 ++allow_keys;
2069 c = vpeekc();
2070 --no_mapping;
2071 --allow_keys;
2072 return c;
2073}
2074#endif
2075
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076/*
2077 * Check if any character is available, also half an escape sequence.
2078 * Trick: when no typeahead found, but there is something in the typeahead
2079 * buffer, it must be an ESC that is recognized as the start of a key code.
2080 */
2081 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002082vpeekc_any(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083{
2084 int c;
2085
2086 c = vpeekc();
2087 if (c == NUL && typebuf.tb_len > 0)
2088 c = ESC;
2089 return c;
2090}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091
2092/*
2093 * Call vpeekc() without causing anything to be mapped.
2094 * Return TRUE if a character is available, FALSE otherwise.
2095 */
2096 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002097char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098{
2099 int retval;
2100
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01002101#ifdef FEAT_EVAL
Bram Moolenaar30613902019-12-01 22:11:18 +01002102 // When test_override("char_avail", 1) was called pretend there is no
2103 // typeahead.
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01002104 if (disable_char_avail_for_testing)
2105 return FALSE;
2106#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 ++no_mapping;
2108 retval = vpeekc();
2109 --no_mapping;
2110 return (retval != NUL);
2111}
2112
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002113#if defined(FEAT_EVAL) || defined(PROTO)
2114/*
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002115 * "getchar()" and "getcharstr()" functions
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002116 */
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002117 static void
2118getchar_common(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002119{
2120 varnumber_T n;
2121 int error = FALSE;
2122
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002123 if (in_vim9script() && check_for_opt_bool_arg(argvars, 0) == FAIL)
2124 return;
2125
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002126#ifdef MESSAGE_QUEUE
2127 // vpeekc() used to check for messages, but that caused problems, invoking
2128 // a callback where it was not expected. Some plugins use getchar(1) in a
2129 // loop to await a message, therefore make sure we check for messages here.
2130 parse_queued_messages();
2131#endif
2132
Bram Moolenaar30613902019-12-01 22:11:18 +01002133 // Position the cursor. Needed after a message that ends in a space.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002134 windgoto(msg_row, msg_col);
2135
2136 ++no_mapping;
2137 ++allow_keys;
2138 for (;;)
2139 {
2140 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar30613902019-12-01 22:11:18 +01002141 // getchar(): blocking wait.
Bram Moolenaar78aed952022-09-24 15:36:35 +01002142 n = plain_vgetc_nopaste();
Bram Moolenaarc08cc722020-09-05 17:51:23 +02002143 else if (tv_get_bool_chk(&argvars[0], &error))
Bram Moolenaar30613902019-12-01 22:11:18 +01002144 // getchar(1): only check if char avail
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002145 n = vpeekc_any();
2146 else if (error || vpeekc_any() == NUL)
Bram Moolenaar30613902019-12-01 22:11:18 +01002147 // illegal argument or getchar(0) and no char avail: return zero
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002148 n = 0;
2149 else
Bram Moolenaar15183b42020-09-05 19:59:39 +02002150 // getchar(0) and char avail() != NUL: get a character.
2151 // Note that vpeekc_any() returns K_SPECIAL for K_IGNORE.
2152 n = safe_vgetc();
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002153
Bram Moolenaar15183b42020-09-05 19:59:39 +02002154 if (n == K_IGNORE || n == K_MOUSEMOVE
2155 || n == K_VER_SCROLLBAR || n == K_HOR_SCROLLBAR)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002156 continue;
2157 break;
2158 }
2159 --no_mapping;
2160 --allow_keys;
2161
2162 set_vim_var_nr(VV_MOUSE_WIN, 0);
2163 set_vim_var_nr(VV_MOUSE_WINID, 0);
2164 set_vim_var_nr(VV_MOUSE_LNUM, 0);
2165 set_vim_var_nr(VV_MOUSE_COL, 0);
2166
2167 rettv->vval.v_number = n;
zeertzjqad6c45f2022-02-20 19:05:10 +00002168 if (n != 0 && (IS_SPECIAL(n) || mod_mask != 0))
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002169 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002170 char_u temp[10]; // modifier: 3, mbyte-char: 6, NUL: 1
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002171 int i = 0;
2172
Bram Moolenaar30613902019-12-01 22:11:18 +01002173 // Turn a special key into three bytes, plus modifier.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002174 if (mod_mask != 0)
2175 {
2176 temp[i++] = K_SPECIAL;
2177 temp[i++] = KS_MODIFIER;
2178 temp[i++] = mod_mask;
2179 }
2180 if (IS_SPECIAL(n))
2181 {
2182 temp[i++] = K_SPECIAL;
2183 temp[i++] = K_SECOND(n);
2184 temp[i++] = K_THIRD(n);
2185 }
2186 else if (has_mbyte)
2187 i += (*mb_char2bytes)(n, temp + i);
2188 else
2189 temp[i++] = n;
2190 temp[i++] = NUL;
2191 rettv->v_type = VAR_STRING;
2192 rettv->vval.v_string = vim_strsave(temp);
2193
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002194 if (is_mouse_key(n))
2195 {
2196 int row = mouse_row;
2197 int col = mouse_col;
2198 win_T *win;
2199 linenr_T lnum;
2200 win_T *wp;
2201 int winnr = 1;
2202
2203 if (row >= 0 && col >= 0)
2204 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002205 // Find the window at the mouse coordinates and compute the
2206 // text position.
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002207 win = mouse_find_win(&row, &col, FIND_POPUP);
2208 if (win == NULL)
2209 return;
2210 (void)mouse_comp_pos(win, &row, &col, &lnum, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002211#ifdef FEAT_PROP_POPUP
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002212 if (WIN_IS_POPUP(win))
2213 winnr = 0;
2214 else
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002215#endif
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002216 for (wp = firstwin; wp != win && wp != NULL;
2217 wp = wp->w_next)
2218 ++winnr;
2219 set_vim_var_nr(VV_MOUSE_WIN, winnr);
2220 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
2221 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
2222 set_vim_var_nr(VV_MOUSE_COL, col + 1);
2223 }
2224 }
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002225 }
2226}
2227
2228/*
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002229 * "getchar()" function
2230 */
2231 void
2232f_getchar(typval_T *argvars, typval_T *rettv)
2233{
2234 getchar_common(argvars, rettv);
2235}
2236
2237/*
2238 * "getcharstr()" function
2239 */
2240 void
2241f_getcharstr(typval_T *argvars, typval_T *rettv)
2242{
2243 getchar_common(argvars, rettv);
2244
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00002245 if (rettv->v_type != VAR_NUMBER)
2246 return;
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002247
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00002248 char_u temp[7]; // mbyte-char: 6, NUL: 1
2249 varnumber_T n = rettv->vval.v_number;
2250 int i = 0;
2251
2252 if (n != 0)
2253 {
2254 if (has_mbyte)
2255 i += (*mb_char2bytes)(n, temp + i);
2256 else
2257 temp[i++] = n;
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002258 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00002259 temp[i++] = NUL;
2260 rettv->v_type = VAR_STRING;
2261 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002262}
2263
2264/*
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002265 * "getcharmod()" function
2266 */
2267 void
2268f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
2269{
2270 rettv->vval.v_number = mod_mask;
2271}
2272#endif // FEAT_EVAL
2273
2274#if defined(MESSAGE_QUEUE) || defined(PROTO)
2275# define MAX_REPEAT_PARSE 8
2276
2277/*
2278 * Process messages that have been queued for netbeans or clientserver.
2279 * Also check if any jobs have ended.
h-east965d2ed2021-10-04 21:51:57 +01002280 * These functions can call arbitrary Vim script and should only be called when
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002281 * it is safe to do so.
2282 */
2283 void
2284parse_queued_messages(void)
2285{
Bram Moolenaar1cac7092019-10-22 21:54:31 +02002286 int old_curwin_id;
2287 int old_curbuf_fnum;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002288 int i;
2289 int save_may_garbage_collect = may_garbage_collect;
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002290 static int entered = 0;
Bram Moolenaard103ee72019-09-18 21:15:31 +02002291 int was_safe = get_was_safe_state();
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002292
2293 // Do not handle messages while redrawing, because it may cause buffers to
2294 // change or be wiped while they are being redrawn.
Bram Moolenaar4778b4d2020-11-04 11:03:12 +01002295 // Also bail out when parsing messages was explicitly disabled.
2296 if (updating_screen || dont_parse_messages)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002297 return;
2298
Bram Moolenaar1cac7092019-10-22 21:54:31 +02002299 // If memory allocation fails during startup we'll exit but curbuf or
2300 // curwin could be NULL.
2301 if (curbuf == NULL || curwin == NULL)
2302 return;
2303
2304 old_curbuf_fnum = curbuf->b_fnum;
2305 old_curwin_id = curwin->w_id;
2306
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002307 ++entered;
2308
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002309 // may_garbage_collect is set in main_loop() to do garbage collection when
2310 // blocking to wait on a character. We don't want that while parsing
2311 // messages, a callback may invoke vgetc() while lists and dicts are in use
2312 // in the call stack.
2313 may_garbage_collect = FALSE;
2314
2315 // Loop when a job ended, but don't keep looping forever.
2316 for (i = 0; i < MAX_REPEAT_PARSE; ++i)
2317 {
2318 // For Win32 mch_breakcheck() does not check for input, do it here.
Bram Moolenaar80a8d382020-05-03 22:57:32 +02002319# if (defined(MSWIN) || defined(__HAIKU__)) && defined(FEAT_JOB_CHANNEL)
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002320 channel_handle_events(FALSE);
2321# endif
2322
2323# ifdef FEAT_NETBEANS_INTG
2324 // Process the queued netbeans messages.
2325 netbeans_parse_messages();
2326# endif
2327# ifdef FEAT_JOB_CHANNEL
2328 // Write any buffer lines still to be written.
2329 channel_write_any_lines();
2330
2331 // Process the messages queued on channels.
2332 channel_parse_messages();
2333# endif
2334# if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
2335 // Process the queued clientserver messages.
2336 server_parse_messages();
2337# endif
2338# ifdef FEAT_JOB_CHANNEL
2339 // Check if any jobs have ended. If so, repeat the above to handle
2340 // changes, e.g. stdin may have been closed.
2341 if (job_check_ended())
2342 continue;
2343# endif
2344# ifdef FEAT_TERMINAL
2345 free_unused_terminals();
2346# endif
Yee Cheng Chin4314e4f2022-10-08 13:50:05 +01002347
2348# ifdef FEAT_SOUND_MACOSX
2349 process_cfrunloop();
2350# endif
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002351# ifdef FEAT_SOUND_CANBERRA
2352 if (has_sound_callback_in_queue())
2353 invoke_sound_callback();
2354# endif
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002355#ifdef SIGUSR1
2356 if (got_sigusr1)
2357 {
2358 apply_autocmds(EVENT_SIGUSR1, NULL, NULL, FALSE, curbuf);
2359 got_sigusr1 = FALSE;
2360 }
2361#endif
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002362 break;
2363 }
2364
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002365 // When not nested we'll go back to waiting for a typed character. If it
2366 // was safe before then this triggers a SafeStateAgain autocommand event.
Bram Moolenaard103ee72019-09-18 21:15:31 +02002367 if (entered == 1 && was_safe)
Bram Moolenaar37d18072019-09-17 20:28:38 +02002368 may_trigger_safestateagain();
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002369
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002370 may_garbage_collect = save_may_garbage_collect;
2371
2372 // If the current window or buffer changed we need to bail out of the
2373 // waiting loop. E.g. when a job exit callback closes the terminal window.
2374 if (curwin->w_id != old_curwin_id || curbuf->b_fnum != old_curbuf_fnum)
Bram Moolenaarb42c0d52020-05-29 22:41:41 +02002375 ins_char_typebuf(K_IGNORE, 0);
Bram Moolenaar69198cb2019-09-16 21:58:13 +02002376
2377 --entered;
Bram Moolenaar9c658c92019-09-15 21:00:54 +02002378}
2379#endif
2380
2381
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002382typedef enum {
2383 map_result_fail, // failed, break loop
2384 map_result_get, // get a character from typeahead
2385 map_result_retry, // try to map again
2386 map_result_nomatch // no matching mapping, get char
2387} map_result_T;
2388
2389/*
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002390 * Check if the bytes at the start of the typeahead buffer are a character used
zeertzjqee446032022-04-30 15:02:22 +01002391 * in Insert mode completion. This includes the form with a CTRL modifier.
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002392 */
2393 static int
zeertzjqee446032022-04-30 15:02:22 +01002394at_ins_compl_key(void)
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002395{
2396 char_u *p = typebuf.tb_buf + typebuf.tb_off;
2397 int c = *p;
2398
2399 if (typebuf.tb_len > 3
Bram Moolenaard979d642022-03-04 14:51:06 +00002400 && (c == K_SPECIAL || c == CSI) // CSI is used by the GUI
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002401 && p[1] == KS_MODIFIER
2402 && (p[2] & MOD_MASK_CTRL))
2403 c = p[3] & 0x1f;
zeertzjqee446032022-04-30 15:02:22 +01002404 return (ctrl_x_mode_not_default() && vim_is_ctrl_x_key(c))
2405 || (compl_status_local() && (c == Ctrl_N || c == Ctrl_P));
Bram Moolenaar88d3d092019-10-20 16:00:47 +02002406}
2407
2408/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002409 * Check if typebuf.tb_buf[] contains a modifier plus key that can be changed
Bram Moolenaar975a8802020-06-06 22:36:24 +02002410 * into just a key, apply that.
2411 * Check from typebuf.tb_buf[typebuf.tb_off] to typebuf.tb_buf[typebuf.tb_off
2412 * + "max_offset"].
zeertzjq12e21e32022-04-27 11:58:01 +01002413 * Return the length of the replaced bytes, 0 if nothing changed, -1 for error.
Bram Moolenaar975a8802020-06-06 22:36:24 +02002414 */
2415 static int
2416check_simplify_modifier(int max_offset)
2417{
2418 int offset;
2419 char_u *tp;
2420
2421 for (offset = 0; offset < max_offset; ++offset)
2422 {
2423 if (offset + 3 >= typebuf.tb_len)
2424 break;
2425 tp = typebuf.tb_buf + typebuf.tb_off + offset;
zeertzjqc47b16a2022-09-05 13:05:29 +01002426 if ((tp[0] == K_SPECIAL || tp[0] == CSI) && tp[1] == KS_MODIFIER)
Bram Moolenaar975a8802020-06-06 22:36:24 +02002427 {
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002428 // A modifier was not used for a mapping, apply it to ASCII keys.
2429 // Shift would already have been applied.
Bram Moolenaar975a8802020-06-06 22:36:24 +02002430 int modifier = tp[2];
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002431 int c = tp[3];
2432 int new_c = merge_modifyOtherKeys(c, &modifier);
Bram Moolenaar975a8802020-06-06 22:36:24 +02002433
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002434 if (new_c != c)
Bram Moolenaar975a8802020-06-06 22:36:24 +02002435 {
2436 char_u new_string[MB_MAXBYTES];
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002437 int len;
Bram Moolenaar975a8802020-06-06 22:36:24 +02002438
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002439 if (offset == 0)
2440 {
2441 // At the start: remember the character and mod_mask before
2442 // merging, in some cases, e.g. at the hit-return prompt,
2443 // they are put back in the typeahead buffer.
2444 vgetc_char = c;
2445 vgetc_mod_mask = tp[2];
2446 }
zeertzjq12e21e32022-04-27 11:58:01 +01002447 if (IS_SPECIAL(new_c))
2448 {
2449 new_string[0] = K_SPECIAL;
2450 new_string[1] = K_SECOND(new_c);
2451 new_string[2] = K_THIRD(new_c);
2452 len = 3;
2453 }
2454 else
2455 len = mb_char2bytes(new_c, new_string);
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002456 if (modifier == 0)
2457 {
2458 if (put_string_in_typebuf(offset, 4, new_string, len,
zeertzjqfc78a032022-04-26 22:11:38 +01002459 NULL, 0, NULL) == FAIL)
Bram Moolenaar975a8802020-06-06 22:36:24 +02002460 return -1;
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002461 }
2462 else
2463 {
2464 tp[2] = modifier;
2465 if (put_string_in_typebuf(offset + 3, 1, new_string, len,
zeertzjqfc78a032022-04-26 22:11:38 +01002466 NULL, 0, NULL) == FAIL)
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002467 return -1;
2468 }
Bram Moolenaar975a8802020-06-06 22:36:24 +02002469 return len;
2470 }
2471 }
2472 }
2473 return 0;
2474}
2475
2476/*
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00002477 * Return TRUE if the terminal sends modifiers with various keys. This is when
2478 * modifyOtherKeys level 2 is enabled or the kitty keyboard protocol is
2479 * enabled.
2480 */
2481 static int
2482key_protocol_enabled(void)
2483{
Bram Moolenaarc255b782022-11-26 19:16:48 +00002484 // If xterm has responded to XTQMODKEYS it overrules seenModifyOtherKeys.
2485 int using_mok = modify_otherkeys_state != MOKS_INITIAL
2486 ? modify_otherkeys_state == MOKS_ENABLED
2487 : seenModifyOtherKeys;
2488 return using_mok || kitty_protocol_state == KKPS_ENABLED;
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00002489}
2490
2491/*
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002492 * Handle mappings in the typeahead buffer.
2493 * - When something was mapped, return map_result_retry for recursive mappings.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002494 * - When nothing mapped and typeahead has a character: return map_result_get.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002495 * - When there is no match yet, return map_result_nomatch, need to get more
2496 * typeahead.
zeertzjq12e21e32022-04-27 11:58:01 +01002497 * - On failure (out of memory) return map_result_fail.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002498 */
2499 static int
2500handle_mapping(
2501 int *keylenp,
2502 int *timedout,
2503 int *mapdepth)
2504{
2505 mapblock_T *mp = NULL;
2506 mapblock_T *mp2;
2507 mapblock_T *mp_match;
2508 int mp_match_len = 0;
2509 int max_mlen = 0;
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002510 int want_termcode = 0; // 1 if termcode expected after max_mlen
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002511 int tb_c1;
2512 int mlen;
2513#ifdef FEAT_LANGMAP
2514 int nolmaplen;
2515#endif
2516 int keylen = *keylenp;
2517 int i;
2518 int local_State = get_real_state();
Bram Moolenaar1fc34222022-03-03 13:56:24 +00002519 int is_plug_map = FALSE;
2520
zeertzjqbb404f52022-07-23 06:25:29 +01002521 // If typeahead starts with <Plug> then remap, even for a "noremap" mapping.
Bram Moolenaaraf043e12022-07-02 12:08:16 +01002522 if (typebuf.tb_len >= 3
2523 && typebuf.tb_buf[typebuf.tb_off] == K_SPECIAL
Bram Moolenaar1fc34222022-03-03 13:56:24 +00002524 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_EXTRA
2525 && typebuf.tb_buf[typebuf.tb_off + 2] == KE_PLUG)
2526 is_plug_map = TRUE;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002527
2528 /*
2529 * Check for a mappable key sequence.
Bram Moolenaareda35f72019-08-03 14:59:44 +02002530 * Walk through one maphash[] list until we find an entry that matches.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002531 *
2532 * Don't look for mappings if:
2533 * - no_mapping set: mapping disabled (e.g. for CTRL-V)
2534 * - maphash_valid not set: no mappings present.
2535 * - typebuf.tb_buf[typebuf.tb_off] should not be remapped
2536 * - in insert or cmdline mode and 'paste' option set
Bram Moolenaareda35f72019-08-03 14:59:44 +02002537 * - waiting for "hit return to continue" and CR or SPACE typed
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002538 * - waiting for a char with --more--
2539 * - in Ctrl-X mode, and we get a valid char for that mode
2540 */
2541 tb_c1 = typebuf.tb_buf[typebuf.tb_off];
2542 if (no_mapping == 0 && is_maphash_valid()
2543 && (no_zero_mapping == 0 || tb_c1 != '0')
Bram Moolenaar1fc34222022-03-03 13:56:24 +00002544 && (typebuf.tb_maplen == 0 || is_plug_map
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002545 || (p_remap
2546 && (typebuf.tb_noremap[typebuf.tb_off]
2547 & (RM_NONE|RM_ABBR)) == 0))
Bram Moolenaar24959102022-05-07 20:01:16 +01002548 && !(p_paste && (State & (MODE_INSERT | MODE_CMDLINE)))
2549 && !(State == MODE_HITRETURN && (tb_c1 == CAR || tb_c1 == ' '))
2550 && State != MODE_ASKMORE
2551 && State != MODE_CONFIRM
zeertzjqee446032022-04-30 15:02:22 +01002552 && !at_ins_compl_key())
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002553 {
Christopher Plewright03193062022-11-22 12:58:27 +00002554#ifdef FEAT_GUI
2555 if (gui.in_use && tb_c1 == CSI && typebuf.tb_len >= 2
2556 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_MODIFIER)
Bram Moolenaarc9983702020-05-28 21:03:53 +02002557 {
2558 // The GUI code sends CSI KS_MODIFIER {flags}, but mappings expect
2559 // K_SPECIAL KS_MODIFIER {flags}.
2560 tb_c1 = K_SPECIAL;
2561 }
2562#endif
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002563#ifdef FEAT_LANGMAP
2564 if (tb_c1 == K_SPECIAL)
2565 nolmaplen = 2;
2566 else
2567 {
Bram Moolenaar24959102022-05-07 20:01:16 +01002568 LANGMAP_ADJUST(tb_c1, (State & (MODE_CMDLINE | MODE_INSERT)) == 0
2569 && get_real_state() != MODE_SELECT);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002570 nolmaplen = 0;
2571 }
2572#endif
2573 // First try buffer-local mappings.
2574 mp = get_buf_maphash_list(local_State, tb_c1);
2575 mp2 = get_maphash_list(local_State, tb_c1);
2576 if (mp == NULL)
2577 {
2578 // There are no buffer-local mappings.
2579 mp = mp2;
2580 mp2 = NULL;
2581 }
Bram Moolenaareda35f72019-08-03 14:59:44 +02002582
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002583 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002584 * Loop until a partly matching mapping is found or all (local)
2585 * mappings have been checked.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002586 * The longest full match is remembered in "mp_match".
Bram Moolenaareda35f72019-08-03 14:59:44 +02002587 * A full match is only accepted if there is no partly match, so "aa"
2588 * and "aaa" can both be mapped.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002589 */
2590 mp_match = NULL;
2591 mp_match_len = 0;
2592 for ( ; mp != NULL;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002593 mp->m_next == NULL ? (mp = mp2, mp2 = NULL) : (mp = mp->m_next))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002594 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002595 // Only consider an entry if the first character matches and it is
2596 // for the current state.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002597 // Skip ":lmap" mappings if keys were mapped.
2598 if (mp->m_keys[0] == tb_c1
2599 && (mp->m_mode & local_State)
Bram Moolenaar47f1fdc2022-11-24 13:27:36 +00002600 && !(mp->m_simplified && key_protocol_enabled()
Bram Moolenaar46cd43b2020-06-04 22:22:11 +02002601 && typebuf.tb_maplen == 0)
Christopher Plewright4c366782022-10-20 13:11:15 +01002602 && ((mp->m_mode & MODE_LANGMAP) == 0
2603 || typebuf.tb_maplen == 0))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002604 {
2605#ifdef FEAT_LANGMAP
2606 int nomap = nolmaplen;
zeertzjq49660f52022-10-20 17:59:38 +01002607 int modifiers = 0;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002608#endif
2609 // find the match length of this mapping
2610 for (mlen = 1; mlen < typebuf.tb_len; ++mlen)
2611 {
zeertzjq49660f52022-10-20 17:59:38 +01002612 int c2 = typebuf.tb_buf[typebuf.tb_off + mlen];
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002613#ifdef FEAT_LANGMAP
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002614 if (nomap > 0)
zeertzjq49660f52022-10-20 17:59:38 +01002615 {
2616 if (nomap == 2 && c2 == KS_MODIFIER)
2617 modifiers = 1;
2618 else if (nomap == 1 && modifiers == 1)
2619 modifiers = c2;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002620 --nomap;
zeertzjq49660f52022-10-20 17:59:38 +01002621 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002622 else
zeertzjq49660f52022-10-20 17:59:38 +01002623 {
2624 if (c2 == K_SPECIAL)
2625 nomap = 2;
2626 else if (merge_modifyOtherKeys(c2, &modifiers) == c2)
2627 // Only apply 'langmap' if merging modifiers into
2628 // the key will not result in another character,
2629 // so that 'langmap' behaves consistently in
2630 // different terminals and GUIs.
2631 LANGMAP_ADJUST(c2, TRUE);
2632 modifiers = 0;
2633 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002634#endif
zeertzjq49660f52022-10-20 17:59:38 +01002635 if (mp->m_keys[mlen] != c2)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002636 break;
2637 }
2638
Bram Moolenaareda35f72019-08-03 14:59:44 +02002639 // Don't allow mapping the first byte(s) of a multi-byte char.
2640 // Happens when mapping <M-a> and then changing 'encoding'.
2641 // Beware that 0x80 is escaped.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002642 {
2643 char_u *p1 = mp->m_keys;
2644 char_u *p2 = mb_unescape(&p1);
2645
2646 if (has_mbyte && p2 != NULL
Bram Moolenaar1614a142019-10-06 22:00:13 +02002647 && MB_BYTE2LEN(tb_c1) > mb_ptr2len(p2))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002648 mlen = 0;
2649 }
2650
2651 // Check an entry whether it matches.
2652 // - Full match: mlen == keylen
2653 // - Partly match: mlen == typebuf.tb_len
2654 keylen = mp->m_keylen;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002655 if (mlen == keylen || (mlen == typebuf.tb_len
2656 && typebuf.tb_len < keylen))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002657 {
2658 char_u *s;
2659 int n;
2660
Bram Moolenaareda35f72019-08-03 14:59:44 +02002661 // If only script-local mappings are allowed, check if the
2662 // mapping starts with K_SNR.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002663 s = typebuf.tb_noremap + typebuf.tb_off;
2664 if (*s == RM_SCRIPT
2665 && (mp->m_keys[0] != K_SPECIAL
2666 || mp->m_keys[1] != KS_EXTRA
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01002667 || mp->m_keys[2] != KE_SNR))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002668 continue;
2669
Bram Moolenaareda35f72019-08-03 14:59:44 +02002670 // If one of the typed keys cannot be remapped, skip the
2671 // entry.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002672 for (n = mlen; --n >= 0; )
2673 if (*s++ & (RM_NONE|RM_ABBR))
2674 break;
Bram Moolenaar1fc34222022-03-03 13:56:24 +00002675 if (!is_plug_map && n >= 0)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002676 continue;
2677
2678 if (keylen > typebuf.tb_len)
2679 {
2680 if (!*timedout && !(mp_match != NULL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002681 && mp_match->m_nowait))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002682 {
2683 // break at a partly match
2684 keylen = KEYLEN_PART_MAP;
2685 break;
2686 }
2687 }
2688 else if (keylen > mp_match_len)
2689 {
2690 // found a longer match
2691 mp_match = mp;
2692 mp_match_len = keylen;
2693 }
2694 }
2695 else
Bram Moolenaareda35f72019-08-03 14:59:44 +02002696 // No match; may have to check for termcode at next
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002697 // character. If the first character that didn't match is
2698 // K_SPECIAL then check for a termcode. This isn't perfect
2699 // but should work in most cases.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002700 if (max_mlen < mlen)
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002701 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002702 max_mlen = mlen;
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002703 want_termcode = mp->m_keys[mlen] == K_SPECIAL;
2704 }
2705 else if (max_mlen == mlen && mp->m_keys[mlen] == K_SPECIAL)
2706 want_termcode = 1;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002707 }
2708 }
2709
Bram Moolenaareda35f72019-08-03 14:59:44 +02002710 // If no partly match found, use the longest full match.
Bram Moolenaar196c3852022-03-04 19:22:36 +00002711 if (keylen != KEYLEN_PART_MAP && mp_match != NULL)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002712 {
2713 mp = mp_match;
2714 keylen = mp_match_len;
2715 }
2716 }
2717
2718 /*
2719 * Check for match with 'pastetoggle'
2720 */
Bram Moolenaar24959102022-05-07 20:01:16 +01002721 if (*p_pt != NUL && mp == NULL && (State & (MODE_INSERT | MODE_NORMAL)))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002722 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002723 for (mlen = 0; mlen < typebuf.tb_len && p_pt[mlen]; ++mlen)
2724 if (p_pt[mlen] != typebuf.tb_buf[typebuf.tb_off + mlen])
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002725 break;
2726 if (p_pt[mlen] == NUL) // match
2727 {
2728 // write chars to script file(s)
2729 if (mlen > typebuf.tb_maplen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002730 gotchars(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_maplen,
2731 mlen - typebuf.tb_maplen);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002732
2733 del_typebuf(mlen, 0); // remove the chars
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002734 set_option_value_give_err((char_u *)"paste",
2735 (long)!p_paste, NULL, 0);
Bram Moolenaar24959102022-05-07 20:01:16 +01002736 if (!(State & MODE_INSERT))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002737 {
2738 msg_col = 0;
2739 msg_row = Rows - 1;
2740 msg_clr_eos(); // clear ruler
2741 }
2742 status_redraw_all();
2743 redraw_statuslines();
2744 showmode();
2745 setcursor();
2746 *keylenp = keylen;
2747 return map_result_retry;
2748 }
2749 // Need more chars for partly match.
2750 if (mlen == typebuf.tb_len)
2751 keylen = KEYLEN_PART_KEY;
2752 else if (max_mlen < mlen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002753 // no match, may have to check for termcode at next character
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002754 max_mlen = mlen + 1;
2755 }
2756
Bram Moolenaarbbf84e22022-03-12 13:48:39 +00002757 // May check for a terminal code when there is no mapping or only a partial
2758 // mapping. Also check if there is a full mapping with <Esc>, unless timed
2759 // out, since that is nearly always a partial match with a terminal code.
Bram Moolenaarf35fd8e2022-03-18 15:41:17 +00002760 if ((mp == NULL || max_mlen + want_termcode > mp_match_len
Bram Moolenaarbbf84e22022-03-12 13:48:39 +00002761 || (mp_match_len == 1 && *mp->m_keys == ESC && !*timedout))
2762 && keylen != KEYLEN_PART_MAP)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002763 {
2764 int save_keylen = keylen;
2765
2766 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002767 * When no matching mapping found or found a non-matching mapping that
2768 * matches at least what the matching mapping matched:
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002769 * Check if we have a terminal code, when:
Bram Moolenaareda35f72019-08-03 14:59:44 +02002770 * - mapping is allowed,
2771 * - keys have not been mapped,
2772 * - and not an ESC sequence, not in insert mode or p_ek is on,
2773 * - and when not timed out,
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002774 */
zeertzjq68a573c2022-04-28 14:10:01 +01002775 if (no_mapping == 0 || allow_keys != 0)
2776 {
2777 if ((typebuf.tb_maplen == 0
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002778 || (p_remap && typebuf.tb_noremap[
Bram Moolenaareda35f72019-08-03 14:59:44 +02002779 typebuf.tb_off] == RM_YES))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002780 && !*timedout)
zeertzjq68a573c2022-04-28 14:10:01 +01002781 keylen = check_termcode(max_mlen + 1, NULL, 0, NULL);
2782 else
2783 keylen = 0;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002784
Bram Moolenaar0684e362020-12-03 19:54:42 +01002785 // If no termcode matched but 'pastetoggle' matched partially
2786 // it's like an incomplete key sequence.
zeertzjq68a573c2022-04-28 14:10:01 +01002787 if (keylen == 0 && save_keylen == KEYLEN_PART_KEY && !*timedout)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002788 keylen = KEYLEN_PART_KEY;
2789
Bram Moolenaar975a8802020-06-06 22:36:24 +02002790 // If no termcode matched, try to include the modifier into the
zeertzjqa3f83fe2021-11-22 12:47:39 +00002791 // key. This is for when modifyOtherKeys is working.
Bram Moolenaarc896adb2022-11-19 19:02:40 +00002792#ifdef FEAT_TERMINAL
2793 check_no_reduce_keys(); // may update the no_reduce_keys flag
2794#endif
Bram Moolenaar673fc3e2020-06-07 15:46:11 +02002795 if (keylen == 0 && !no_reduce_keys)
zeertzjq12e21e32022-04-27 11:58:01 +01002796 {
Bram Moolenaar975a8802020-06-06 22:36:24 +02002797 keylen = check_simplify_modifier(max_mlen + 1);
zeertzjq12e21e32022-04-27 11:58:01 +01002798 if (keylen < 0)
2799 // ins_typebuf() failed
2800 return map_result_fail;
2801 }
Bram Moolenaar975a8802020-06-06 22:36:24 +02002802
Bram Moolenaareda35f72019-08-03 14:59:44 +02002803 // When getting a partial match, but the last characters were not
2804 // typed, don't wait for a typed character to complete the
2805 // termcode. This helps a lot when a ":normal" command ends in an
2806 // ESC.
2807 if (keylen < 0 && typebuf.tb_len == typebuf.tb_maplen)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002808 keylen = 0;
2809 }
2810 else
2811 keylen = 0;
2812 if (keylen == 0) // no matching terminal code
2813 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002814#ifdef AMIGA
2815 // check for window bounds report
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002816 if (typebuf.tb_maplen == 0 && (typebuf.tb_buf[
Bram Moolenaareda35f72019-08-03 14:59:44 +02002817 typebuf.tb_off] & 0xff) == CSI)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002818 {
2819 char_u *s;
2820
2821 for (s = typebuf.tb_buf + typebuf.tb_off + 1;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002822 s < typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len
2823 && (VIM_ISDIGIT(*s) || *s == ';' || *s == ' ');
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002824 ++s)
2825 ;
2826 if (*s == 'r' || *s == '|') // found one
2827 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002828 del_typebuf(
2829 (int)(s + 1 - (typebuf.tb_buf + typebuf.tb_off)), 0);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002830 // get size and redraw screen
2831 shell_resized();
2832 *keylenp = keylen;
2833 return map_result_retry;
2834 }
2835 if (*s == NUL) // need more characters
2836 keylen = KEYLEN_PART_KEY;
2837 }
2838 if (keylen >= 0)
2839#endif
Bram Moolenaareda35f72019-08-03 14:59:44 +02002840 // When there was a matching mapping and no termcode could be
2841 // replaced after another one, use that mapping (loop around).
2842 // If there was no mapping at all use the character from the
2843 // typeahead buffer right here.
2844 if (mp == NULL)
2845 {
2846 *keylenp = keylen;
Bram Moolenaar52797ba2021-12-16 14:45:13 +00002847 return map_result_get; // get character from typeahead
Bram Moolenaareda35f72019-08-03 14:59:44 +02002848 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002849 }
2850
2851 if (keylen > 0) // full matching terminal code
2852 {
2853#if defined(FEAT_GUI) && defined(FEAT_MENU)
2854 if (typebuf.tb_len >= 2
Bram Moolenaareda35f72019-08-03 14:59:44 +02002855 && typebuf.tb_buf[typebuf.tb_off] == K_SPECIAL
2856 && typebuf.tb_buf[typebuf.tb_off + 1] == KS_MENU)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002857 {
2858 int idx;
2859
Bram Moolenaareda35f72019-08-03 14:59:44 +02002860 // Using a menu may cause a break in undo! It's like using
2861 // gotchars(), but without recording or writing to a script
2862 // file.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002863 may_sync_undo();
2864 del_typebuf(3, 0);
2865 idx = get_menu_index(current_menu, local_State);
2866 if (idx != MENU_INDEX_INVALID)
2867 {
Bram Moolenaareda35f72019-08-03 14:59:44 +02002868 // In Select mode and a Visual mode menu is used: Switch
2869 // to Visual mode temporarily. Append K_SELECT to switch
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002870 // back to Select mode.
2871 if (VIsual_active && VIsual_select
Bram Moolenaar24959102022-05-07 20:01:16 +01002872 && (current_menu->modes & MODE_VISUAL))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002873 {
2874 VIsual_select = FALSE;
2875 (void)ins_typebuf(K_SELECT_STRING,
Bram Moolenaareda35f72019-08-03 14:59:44 +02002876 REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002877 }
2878 ins_typebuf(current_menu->strings[idx],
2879 current_menu->noremap[idx],
Bram Moolenaareda35f72019-08-03 14:59:44 +02002880 0, TRUE, current_menu->silent[idx]);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002881 }
2882 }
2883#endif // FEAT_GUI && FEAT_MENU
2884 *keylenp = keylen;
2885 return map_result_retry; // try mapping again
2886 }
2887
Bram Moolenaareda35f72019-08-03 14:59:44 +02002888 // Partial match: get some more characters. When a matching mapping
2889 // was found use that one.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002890 if (mp == NULL || keylen < 0)
2891 keylen = KEYLEN_PART_KEY;
2892 else
2893 keylen = mp_match_len;
2894 }
2895
2896 /*
2897 * complete match
2898 */
2899 if (keylen >= 0 && keylen <= typebuf.tb_len)
2900 {
2901 char_u *map_str;
2902
2903#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002904 int save_m_expr;
2905 int save_m_noremap;
2906 int save_m_silent;
2907 char_u *save_m_keys;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002908#else
2909# define save_m_noremap mp->m_noremap
2910# define save_m_silent mp->m_silent
2911#endif
2912
2913 // write chars to script file(s)
2914 if (keylen > typebuf.tb_maplen)
Bram Moolenaareda35f72019-08-03 14:59:44 +02002915 gotchars(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_maplen,
2916 keylen - typebuf.tb_maplen);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002917
2918 cmd_silent = (typebuf.tb_silent > 0);
2919 del_typebuf(keylen, 0); // remove the mapped keys
2920
2921 /*
2922 * Put the replacement string in front of mapstr.
2923 * The depth check catches ":map x y" and ":map y x".
2924 */
2925 if (++*mapdepth >= p_mmd)
2926 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00002927 emsg(_(e_recursive_mapping));
Bram Moolenaar24959102022-05-07 20:01:16 +01002928 if (State & MODE_CMDLINE)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002929 redrawcmdline();
2930 else
2931 setcursor();
2932 flush_buffers(FLUSH_MINIMAL);
Bram Moolenaar30613902019-12-01 22:11:18 +01002933 *mapdepth = 0; // for next one
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002934 *keylenp = keylen;
2935 return map_result_fail;
2936 }
2937
2938 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002939 * In Select mode and a Visual mode mapping is used: Switch to Visual
2940 * mode temporarily. Append K_SELECT to switch back to Select mode.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002941 */
Bram Moolenaar24959102022-05-07 20:01:16 +01002942 if (VIsual_active && VIsual_select && (mp->m_mode & MODE_VISUAL))
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002943 {
2944 VIsual_select = FALSE;
Bram Moolenaareda35f72019-08-03 14:59:44 +02002945 (void)ins_typebuf(K_SELECT_STRING, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002946 }
2947
2948#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02002949 // Copy the values from *mp that are used, because evaluating the
2950 // expression may invoke a function that redefines the mapping, thereby
2951 // making *mp invalid.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002952 save_m_expr = mp->m_expr;
2953 save_m_noremap = mp->m_noremap;
2954 save_m_silent = mp->m_silent;
2955 save_m_keys = NULL; // only saved when needed
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002956
2957 /*
Bram Moolenaareda35f72019-08-03 14:59:44 +02002958 * Handle ":map <expr>": evaluate the {rhs} as an expression. Also
2959 * save and restore the command line for "normal :".
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002960 */
2961 if (mp->m_expr)
2962 {
2963 int save_vgetc_busy = vgetc_busy;
2964 int save_may_garbage_collect = may_garbage_collect;
Bram Moolenaar4ebe0e62019-11-22 20:55:40 +01002965 int was_screen_col = screen_cur_col;
2966 int was_screen_row = screen_cur_row;
Bram Moolenaar74a0a5b2022-02-10 14:07:41 +00002967 int prev_did_emsg = did_emsg;
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002968
2969 vgetc_busy = 0;
2970 may_garbage_collect = FALSE;
2971
2972 save_m_keys = vim_strsave(mp->m_keys);
Bram Moolenaar19db9e62022-01-11 11:58:19 +00002973 map_str = eval_map_expr(mp, NUL);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02002974
Bram Moolenaar4ebe0e62019-11-22 20:55:40 +01002975 // The mapping may do anything, but we expect it to take care of
2976 // redrawing. Do put the cursor back where it was.
2977 windgoto(was_screen_row, was_screen_col);
2978 out_flush();
2979
Bram Moolenaar74a0a5b2022-02-10 14:07:41 +00002980 // If an error was displayed and the expression returns an empty
2981 // string, generate a <Nop> to allow for a redraw.
2982 if (prev_did_emsg != did_emsg
2983 && (map_str == NULL || *map_str == NUL))
2984 {
2985 char_u buf[4];
2986
2987 vim_free(map_str);
2988 buf[0] = K_SPECIAL;
2989 buf[1] = KS_EXTRA;
2990 buf[2] = KE_IGNORE;
2991 buf[3] = NUL;
2992 map_str = vim_strsave(buf);
Bram Moolenaar24959102022-05-07 20:01:16 +01002993 if (State & MODE_CMDLINE)
Bram Moolenaar74a0a5b2022-02-10 14:07:41 +00002994 {
2995 // redraw the command below the error
2996 msg_didout = TRUE;
2997 if (msg_row < cmdline_row)
2998 msg_row = cmdline_row;
2999 redrawcmd();
3000 }
3001 }
3002
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003003 vgetc_busy = save_vgetc_busy;
3004 may_garbage_collect = save_may_garbage_collect;
3005 }
3006 else
3007#endif
3008 map_str = mp->m_str;
3009
3010 /*
3011 * Insert the 'to' part in the typebuf.tb_buf.
Bram Moolenaareda35f72019-08-03 14:59:44 +02003012 * If 'from' field is the same as the start of the 'to' field, don't
3013 * remap the first character (but do allow abbreviations).
3014 * If m_noremap is set, don't remap the whole 'to' part.
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003015 */
3016 if (map_str == NULL)
3017 i = FAIL;
3018 else
3019 {
3020 int noremap;
3021
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01003022#ifdef FEAT_EVAL
3023 last_used_map = mp;
3024 last_used_sid = -1;
3025#endif
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003026 if (save_m_noremap != REMAP_YES)
3027 noremap = save_m_noremap;
3028 else if (
3029#ifdef FEAT_EVAL
Bram Moolenaareda35f72019-08-03 14:59:44 +02003030 STRNCMP(map_str, save_m_keys != NULL ? save_m_keys : mp->m_keys,
3031 (size_t)keylen)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003032#else
3033 STRNCMP(map_str, mp->m_keys, (size_t)keylen)
3034#endif
3035 != 0)
3036 noremap = REMAP_YES;
3037 else
3038 noremap = REMAP_SKIP;
3039 i = ins_typebuf(map_str, noremap,
3040 0, TRUE, cmd_silent || save_m_silent);
3041#ifdef FEAT_EVAL
3042 if (save_m_expr)
3043 vim_free(map_str);
3044#endif
3045 }
3046#ifdef FEAT_EVAL
3047 vim_free(save_m_keys);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003048#endif
3049 *keylenp = keylen;
3050 if (i == FAIL)
3051 return map_result_fail;
3052 return map_result_retry;
3053 }
3054
3055 *keylenp = keylen;
3056 return map_result_nomatch;
3057}
3058
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003059/*
3060 * unget one character (can only be done once!)
zeertzjq6d4e7252022-04-07 13:58:04 +01003061 * If the character was stuffed, vgetc() will get it next time it is called.
zeertzjq0f68e6c2022-04-05 13:17:01 +01003062 * Otherwise vgetc() will only get it when the stuff buffer is empty.
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003063 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003065vungetc(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066{
3067 old_char = c;
3068 old_mod_mask = mod_mask;
Bram Moolenaarb8978712013-03-16 21:42:16 +01003069 old_mouse_row = mouse_row;
3070 old_mouse_col = mouse_col;
zeertzjq0f68e6c2022-04-05 13:17:01 +01003071 old_KeyStuffed = KeyStuffed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072}
3073
3074/*
zeertzjq6d4e7252022-04-07 13:58:04 +01003075 * When peeking and not getting a character, reg_executing cannot be cleared
3076 * yet, so set a flag to clear it later.
3077 */
3078 static void
3079check_end_reg_executing(int advance)
3080{
3081 if (reg_executing != 0 && (typebuf.tb_maplen == 0
3082 || pending_end_reg_executing))
3083 {
3084 if (advance)
3085 {
3086 reg_executing = 0;
3087 pending_end_reg_executing = FALSE;
3088 }
3089 else
3090 pending_end_reg_executing = TRUE;
3091 }
3092
3093}
3094
3095/*
Bram Moolenaar32526b32019-01-19 17:43:09 +01003096 * Get a byte:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 * 1. from the stuffbuffer
3098 * This is used for abbreviated commands like "D" -> "d$".
3099 * Also used to redo a command for ".".
3100 * 2. from the typeahead buffer
3101 * Stores text obtained previously but not used yet.
3102 * Also stores the result of mappings.
3103 * Also used for the ":normal" command.
3104 * 3. from the user
3105 * This may do a blocking wait if "advance" is TRUE.
3106 *
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003107 * if "advance" is TRUE (vgetc()):
Bram Moolenaard29459b2016-08-26 22:29:11 +02003108 * Really get the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 * KeyTyped is set to TRUE in the case the user typed the key.
3110 * KeyStuffed is TRUE if the character comes from the stuff buffer.
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003111 * if "advance" is FALSE (vpeekc()):
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02003112 * Just look whether there is a character available.
3113 * Return NUL if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 *
3115 * When "no_mapping" is zero, checks for mappings in the current mode.
3116 * Only returns one byte (of a multi-byte character).
3117 * K_SPECIAL and CSI may be escaped, need to get two more bytes then.
3118 */
3119 static int
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003120vgetorpeek(int advance)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121{
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003122 int c;
Bram Moolenaarb8329db2022-07-06 13:31:28 +01003123 int timedout = FALSE; // waited for more than 'timeoutlen'
3124 // for mapping to complete or
3125 // 'ttimeoutlen' for complete key code
Bram Moolenaar30613902019-12-01 22:11:18 +01003126 int mapdepth = 0; // check for recursive mapping
3127 int mode_deleted = FALSE; // set when mode has been deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 int new_wcol, new_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003130 int shape_changed = FALSE; // adjusted cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131#endif
3132 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 int old_wcol, old_wrow;
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003134 int wait_tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135
3136 /*
3137 * This function doesn't work very well when called recursively. This may
3138 * happen though, because of:
3139 * 1. The call to add_to_showcmd(). char_avail() is then used to check if
3140 * there is a character available, which calls this function. In that
3141 * case we must return NUL, to indicate no character is available.
3142 * 2. A GUI callback function writes to the screen, causing a
3143 * wait_return().
3144 * Using ":normal" can also do this, but it saves the typeahead buffer,
3145 * thus it should be OK. But don't get a key from the user then.
3146 */
Bram Moolenaare2c38102016-01-31 14:55:40 +01003147 if (vgetc_busy > 0 && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 return NUL;
3149
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003150 ++vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151
3152 if (advance)
Bram Moolenaar8d696372022-08-21 10:40:07 +01003153 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 KeyStuffed = FALSE;
Bram Moolenaar8d696372022-08-21 10:40:07 +01003155 typebuf_was_empty = FALSE;
3156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157
3158 init_typebuf();
3159 start_stuff();
zeertzjq6d4e7252022-04-07 13:58:04 +01003160 check_end_reg_executing(advance);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 do
3162 {
3163/*
3164 * get a character: 1. from the stuffbuffer
3165 */
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003166 if (typeahead_char != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 {
3168 c = typeahead_char;
3169 if (advance)
3170 typeahead_char = 0;
3171 }
3172 else
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01003173 c = read_readbuffers(advance);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 if (c != NUL && !got_int)
3175 {
3176 if (advance)
3177 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003178 // KeyTyped = FALSE; When the command that stuffed something
3179 // was typed, behave like the stuffed command was typed.
3180 // needed for CTRL-W CTRL-] to open a fold, for example.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 KeyStuffed = TRUE;
3182 }
3183 if (typebuf.tb_no_abbr_cnt == 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003184 typebuf.tb_no_abbr_cnt = 1; // no abbreviations now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 }
3186 else
3187 {
3188 /*
3189 * Loop until we either find a matching mapped key, or we
3190 * are sure that it is not a mapped key.
3191 * If a mapped key sequence is found we go back to the start to
3192 * try re-mapping.
3193 */
3194 for (;;)
3195 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003196 long wait_time;
3197 int keylen = 0;
Bram Moolenaareda35f72019-08-03 14:59:44 +02003198 int showcmd_idx;
zeertzjq6d4e7252022-04-07 13:58:04 +01003199 check_end_reg_executing(advance);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 /*
3201 * ui_breakcheck() is slow, don't use it too often when
3202 * inside a mapping. But call it each time for typed
3203 * characters.
3204 */
3205 if (typebuf.tb_maplen)
3206 line_breakcheck();
3207 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003208 ui_breakcheck(); // check for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 if (got_int)
3210 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003211 // flush all input
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003212 c = inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 0L);
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003213
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 /*
3215 * If inchar() returns TRUE (script file was active) or we
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003216 * are inside a mapping, get out of Insert mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 * Otherwise we behave like having gotten a CTRL-C.
3218 * As a result typing CTRL-C in insert mode will
3219 * really insert a CTRL-C.
3220 */
3221 if ((c || typebuf.tb_maplen)
Bram Moolenaar24959102022-05-07 20:01:16 +01003222 && (State & (MODE_INSERT | MODE_CMDLINE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 c = ESC;
3224 else
3225 c = Ctrl_C;
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02003226 flush_buffers(FLUSH_INPUT); // flush all typeahead
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227
Bram Moolenaard9dfd572006-10-03 13:36:13 +00003228 if (advance)
3229 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003230 // Also record this character, it might be needed to
3231 // get out of Insert mode.
Bram Moolenaard9dfd572006-10-03 13:36:13 +00003232 *typebuf.tb_buf = c;
3233 gotchars(typebuf.tb_buf, 1);
3234 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 cmd_silent = FALSE;
3236
3237 break;
3238 }
3239 else if (typebuf.tb_len > 0)
3240 {
3241 /*
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003242 * Check for a mapping in "typebuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 */
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003244 map_result_T result = handle_mapping(
3245 &keylen, &timedout, &mapdepth);
3246
3247 if (result == map_result_retry)
3248 // try mapping again
3249 continue;
3250 if (result == map_result_fail)
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003251 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003252 // failed, use the outer loop
3253 c = -1;
3254 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003256 if (result == map_result_get)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258/*
3259 * get a character: 2. from the typeahead buffer
3260 */
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003261 c = typebuf.tb_buf[typebuf.tb_off];
Bram Moolenaar30613902019-12-01 22:11:18 +01003262 if (advance) // remove chars from tb_buf
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003263 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003264 cmd_silent = (typebuf.tb_silent > 0);
3265 if (typebuf.tb_maplen > 0)
3266 KeyTyped = FALSE;
3267 else
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003268 {
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003269 KeyTyped = TRUE;
Bram Moolenaar30613902019-12-01 22:11:18 +01003270 // write char to script file(s)
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003271 gotchars(typebuf.tb_buf
3272 + typebuf.tb_off, 1);
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003273 }
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003274 KeyNoremap = typebuf.tb_noremap[
3275 typebuf.tb_off];
3276 del_typebuf(1, 0);
Bram Moolenaarf2d8b7a2019-08-02 22:46:11 +02003277 }
Bram Moolenaar52797ba2021-12-16 14:45:13 +00003278 break; // got character, break the for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 }
3280
Bram Moolenaaredd680f2019-08-03 14:23:48 +02003281 // not enough characters, get more
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 }
3283
3284/*
3285 * get a character: 3. from the user - handle <Esc> in Insert mode
3286 */
3287 /*
Bram Moolenaard330e842022-11-24 20:23:24 +00003288 * Special case: if we get an <ESC> in Insert mode and there
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 * are no more characters at once, we pretend to go out of
Bram Moolenaard330e842022-11-24 20:23:24 +00003290 * Insert mode. This prevents the one second delay after
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 * typing an <ESC>. If we get something after all, we may
3292 * have to redisplay the mode. That the cursor is in the wrong
3293 * place does not matter.
Bram Moolenaard330e842022-11-24 20:23:24 +00003294 * Do not do this if the kitty keyboard protocol is used, every
3295 * <ESC> is the start of an escape sequence then.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 */
3297 c = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 new_wcol = curwin->w_wcol;
3299 new_wrow = curwin->w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 if ( advance
3301 && typebuf.tb_len == 1
3302 && typebuf.tb_buf[typebuf.tb_off] == ESC
3303 && !no_mapping
Bram Moolenaard330e842022-11-24 20:23:24 +00003304 && kitty_protocol_state != KKPS_ENABLED
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 && ex_normal_busy == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 && typebuf.tb_maplen == 0
Bram Moolenaar24959102022-05-07 20:01:16 +01003307 && (State & MODE_INSERT)
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003308 && (p_timeout
3309 || (keylen == KEYLEN_PART_KEY && p_ttimeout))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 && (c = inchar(typebuf.tb_buf + typebuf.tb_off
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003311 + typebuf.tb_len, 3, 25L)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003313 colnr_T col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 char_u *ptr;
3315
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003316 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 {
3318 unshowmode(TRUE);
3319 mode_deleted = TRUE;
3320 }
3321#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003322 // may show a different cursor shape
Bram Moolenaar24959102022-05-07 20:01:16 +01003323 if (gui.in_use && State != MODE_NORMAL && !cmd_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 {
3325 int save_State;
3326
3327 save_State = State;
Bram Moolenaar24959102022-05-07 20:01:16 +01003328 State = MODE_NORMAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 gui_update_cursor(TRUE, FALSE);
3330 State = save_State;
3331 shape_changed = TRUE;
3332 }
3333#endif
3334 validate_cursor();
3335 old_wcol = curwin->w_wcol;
3336 old_wrow = curwin->w_wrow;
3337
Bram Moolenaar30613902019-12-01 22:11:18 +01003338 // move cursor left, if possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 if (curwin->w_cursor.col != 0)
3340 {
3341 if (curwin->w_wcol > 0)
3342 {
Bram Moolenaar0f843ef2023-01-25 17:34:41 +00003343 // After auto-indenting and no text is following,
3344 // we are expecting to truncate the trailing
3345 // white-space, so find the last non-white
3346 // character -- webb
3347 if (did_ai && *skipwhite(ml_get_curline()
3348 + curwin->w_cursor.col) == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003350 chartabsize_T cts;
3351
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003352 curwin->w_wcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 ptr = ml_get_curline();
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003354 init_chartabsize_arg(&cts, curwin,
3355 curwin->w_cursor.lnum, 0, ptr, ptr);
3356 while (cts.cts_ptr < ptr + curwin->w_cursor.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 {
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003358 if (!VIM_ISWHITE(*cts.cts_ptr))
3359 curwin->w_wcol = cts.cts_vcol;
3360 cts.cts_vcol += lbr_chartabsize(&cts);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 if (has_mbyte)
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003362 cts.cts_ptr +=
3363 (*mb_ptr2len)(cts.cts_ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 else
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003365 ++cts.cts_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 }
Bram Moolenaar7f9969c2022-07-25 18:13:54 +01003367 clear_chartabsize_arg(&cts);
3368
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 curwin->w_wrow = curwin->w_cline_row
Bram Moolenaar02631462017-09-22 15:20:32 +02003370 + curwin->w_wcol / curwin->w_width;
3371 curwin->w_wcol %= curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 curwin->w_wcol += curwin_col_off();
Bram Moolenaar30613902019-12-01 22:11:18 +01003373 col = 0; // no correction needed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 }
3375 else
3376 {
3377 --curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 col = curwin->w_cursor.col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 }
3380 }
3381 else if (curwin->w_p_wrap && curwin->w_wrow)
3382 {
3383 --curwin->w_wrow;
Bram Moolenaar02631462017-09-22 15:20:32 +02003384 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 col = curwin->w_cursor.col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 if (has_mbyte && col > 0 && curwin->w_wcol > 0)
3388 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003389 // Correct when the cursor is on the right halve
3390 // of a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 ptr = ml_get_curline();
3392 col -= (*mb_head_off)(ptr, ptr + col);
3393 if ((*mb_ptr2cells)(ptr + col) > 1)
3394 --curwin->w_wcol;
3395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 }
3397 setcursor();
3398 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 new_wcol = curwin->w_wcol;
3400 new_wrow = curwin->w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 curwin->w_wcol = old_wcol;
3402 curwin->w_wrow = old_wrow;
3403 }
3404 if (c < 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003405 continue; // end of input script reached
Bram Moolenaar20c38922014-07-23 20:41:14 +02003406
Bram Moolenaar30613902019-12-01 22:11:18 +01003407 // Allow mapping for just typed characters. When we get here c
3408 // is the number of extra bytes and typebuf.tb_len is 1.
Bram Moolenaar20c38922014-07-23 20:41:14 +02003409 for (n = 1; n <= c; ++n)
3410 typebuf.tb_noremap[typebuf.tb_off + n] = RM_YES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 typebuf.tb_len += c;
3412
Bram Moolenaar30613902019-12-01 22:11:18 +01003413 // buffer full, don't map
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 if (typebuf.tb_len >= typebuf.tb_maplen + MAXMAPLEN)
3415 {
3416 timedout = TRUE;
3417 continue;
3418 }
3419
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 if (ex_normal_busy > 0)
3421 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 static int tc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423
Bram Moolenaar30613902019-12-01 22:11:18 +01003424 // No typeahead left and inside ":normal". Must return
3425 // something to avoid getting stuck. When an incomplete
3426 // mapping is present, behave like it timed out.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 if (typebuf.tb_len > 0)
3428 {
3429 timedout = TRUE;
3430 continue;
3431 }
Bram Moolenaar189832b2020-09-23 12:29:11 +02003432
Bram Moolenaar30613902019-12-01 22:11:18 +01003433 // When 'insertmode' is set, ESC just beeps in Insert
3434 // mode. Use CTRL-L to make edit() return.
3435 // For the command line only CTRL-C always breaks it.
3436 // For the cmdline window: Alternate between ESC and
3437 // CTRL-C: ESC for most situations and CTRL-C to close the
3438 // cmdline window.
Bram Moolenaar24959102022-05-07 20:01:16 +01003439 if (p_im && (State & MODE_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 c = Ctrl_L;
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003441#ifdef FEAT_TERMINAL
3442 else if (terminal_is_active())
3443 c = K_CANCEL;
3444#endif
Bram Moolenaar24959102022-05-07 20:01:16 +01003445 else if ((State & MODE_CMDLINE)
Martin Tournoij7904fa42022-10-04 16:28:45 +01003446 || (cmdwin_type > 0 && tc == ESC))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 c = Ctrl_C;
3448 else
3449 c = ESC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 tc = c;
Bram Moolenaar8d696372022-08-21 10:40:07 +01003451 // set a flag to indicate this wasn't a normal char
3452 if (advance)
3453 typebuf_was_empty = TRUE;
3454
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01003455 // return from main_loop()
3456 if (pending_exmode_active)
3457 exmode_active = EXMODE_NORMAL;
3458
Bram Moolenaarb37a65e2022-01-01 12:42:56 +00003459 // no chars to block abbreviation for
3460 typebuf.tb_no_abbr_cnt = 0;
3461
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 break;
3463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464
3465/*
3466 * get a character: 3. from the user - update display
3467 */
Bram Moolenaar30613902019-12-01 22:11:18 +01003468 // In insert mode a screen update is skipped when characters
3469 // are still available. But when those available characters
3470 // are part of a mapping, and we are going to do a blocking
3471 // wait here. Need to update the screen to display the
3472 // changed text so far. Also for when 'lazyredraw' is set and
3473 // redrawing was postponed because there was something in the
3474 // input buffer (e.g., termresponse).
Bram Moolenaar24959102022-05-07 20:01:16 +01003475 if (((State & MODE_INSERT) != 0 || p_lz)
3476 && (State & MODE_CMDLINE) == 0
3477 && advance && must_redraw != 0 && !need_wait_return)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 {
3479 update_screen(0);
Bram Moolenaar30613902019-12-01 22:11:18 +01003480 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 }
3482
3483 /*
3484 * If we have a partial match (and are going to wait for more
3485 * input from the user), show the partially matched characters
3486 * to the user with showcmd.
3487 */
Bram Moolenaareda35f72019-08-03 14:59:44 +02003488 showcmd_idx = 0;
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003489 int showing_partial = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 if (typebuf.tb_len > 0 && advance && !exmode_active)
3491 {
Bram Moolenaar24959102022-05-07 20:01:16 +01003492 if (((State & (MODE_NORMAL | MODE_INSERT))
3493 || State == MODE_LANGMAP)
3494 && State != MODE_HITRETURN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003496 // this looks nice when typing a dead character map
Bram Moolenaar24959102022-05-07 20:01:16 +01003497 if (State & MODE_INSERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
3499 + typebuf.tb_len - 1) == 1)
3500 {
3501 edit_putchar(typebuf.tb_buf[typebuf.tb_off
3502 + typebuf.tb_len - 1], FALSE);
Bram Moolenaar30613902019-12-01 22:11:18 +01003503 setcursor(); // put cursor back where it belongs
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003504 showing_partial = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003506 // need to use the col and row from above here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 old_wcol = curwin->w_wcol;
3508 old_wrow = curwin->w_wrow;
3509 curwin->w_wcol = new_wcol;
3510 curwin->w_wrow = new_wrow;
3511 push_showcmd();
3512 if (typebuf.tb_len > SHOWCMD_COLS)
Bram Moolenaareda35f72019-08-03 14:59:44 +02003513 showcmd_idx = typebuf.tb_len - SHOWCMD_COLS;
3514 while (showcmd_idx < typebuf.tb_len)
3515 (void)add_to_showcmd(
3516 typebuf.tb_buf[typebuf.tb_off + showcmd_idx++]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 curwin->w_wcol = old_wcol;
3518 curwin->w_wrow = old_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 }
3520
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003521 // This looks nice when typing a dead character map.
3522 // There is no actual command line for get_number().
Bram Moolenaar24959102022-05-07 20:01:16 +01003523 if ((State & MODE_CMDLINE)
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003524 && get_cmdline_info()->cmdbuff != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3526 && cmdline_star == 0
3527#endif
3528 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
3529 + typebuf.tb_len - 1) == 1)
3530 {
3531 putcmdline(typebuf.tb_buf[typebuf.tb_off
3532 + typebuf.tb_len - 1], FALSE);
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003533 showing_partial = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 }
3535 }
3536
3537/*
3538 * get a character: 3. from the user - get it
3539 */
Bram Moolenaar83f4cbd2018-06-12 21:35:40 +02003540 if (typebuf.tb_len == 0)
zeertzjq3760bfd2022-06-06 16:22:46 +01003541 // timedout may have been set if a mapping with empty RHS
3542 // fully matched while longer mappings timed out.
Bram Moolenaar83f4cbd2018-06-12 21:35:40 +02003543 timedout = FALSE;
3544
Bram Moolenaar652de232019-04-04 20:13:09 +02003545 if (advance)
3546 {
3547 if (typebuf.tb_len == 0
3548 || !(p_timeout
3549 || (p_ttimeout && keylen == KEYLEN_PART_KEY)))
3550 // blocking wait
3551 wait_time = -1L;
3552 else if (keylen == KEYLEN_PART_KEY && p_ttm >= 0)
3553 wait_time = p_ttm;
3554 else
3555 wait_time = p_tm;
3556 }
3557 else
3558 wait_time = 0;
3559
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003560 wait_tb_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 c = inchar(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len,
3562 typebuf.tb_buflen - typebuf.tb_off - typebuf.tb_len - 1,
Bram Moolenaar652de232019-04-04 20:13:09 +02003563 wait_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564
Bram Moolenaareda35f72019-08-03 14:59:44 +02003565 if (showcmd_idx != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 pop_showcmd();
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003567 if (showing_partial)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 {
Bram Moolenaar24959102022-05-07 20:01:16 +01003569 if (State & MODE_INSERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 edit_unputchar();
Bram Moolenaar7ac50232023-03-07 21:05:04 +00003571 if ((State & MODE_CMDLINE)
3572 && get_cmdline_info()->cmdbuff != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 unputcmdline();
Bram Moolenaarbc256d92012-06-06 12:06:15 +02003574 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003575 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 }
3577
3578 if (c < 0)
Bram Moolenaar30613902019-12-01 22:11:18 +01003579 continue; // end of input script reached
3580 if (c == NUL) // no character available
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 {
3582 if (!advance)
3583 break;
Bram Moolenaar30613902019-12-01 22:11:18 +01003584 if (wait_tb_len > 0) // timed out
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 {
3586 timedout = TRUE;
3587 continue;
3588 }
3589 }
3590 else
Bram Moolenaar30613902019-12-01 22:11:18 +01003591 { // allow mapping for just typed characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 while (typebuf.tb_buf[typebuf.tb_off
3593 + typebuf.tb_len] != NUL)
3594 typebuf.tb_noremap[typebuf.tb_off
3595 + typebuf.tb_len++] = RM_YES;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003596#ifdef HAVE_INPUT_METHOD
Bram Moolenaar30613902019-12-01 22:11:18 +01003597 // Get IM status right after getting keys, not after the
3598 // timeout for a mapping (focus may be lost by then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 vgetc_im_active = im_get_status();
3600#endif
3601 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003602 } // for (;;)
3603 } // if (!character from stuffbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604
Bram Moolenaar30613902019-12-01 22:11:18 +01003605 // if advance is FALSE don't loop on NULs
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003606 } while ((c < 0 && c != K_CANCEL) || (advance && c == NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607
3608 /*
3609 * The "INSERT" message is taken care of here:
3610 * if we return an ESC to exit insert mode, the message is deleted
3611 * if we don't return an ESC but deleted the message before, redisplay it
3612 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003613 if (advance && p_smd && msg_silent == 0 && (State & MODE_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003615 if (c == ESC && !mode_deleted && !no_mapping && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 {
3617 if (typebuf.tb_len && !KeyTyped)
Bram Moolenaar30613902019-12-01 22:11:18 +01003618 redraw_cmdline = TRUE; // delete mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 else
3620 unshowmode(FALSE);
3621 }
3622 else if (c != ESC && mode_deleted)
3623 {
3624 if (typebuf.tb_len && !KeyTyped)
Bram Moolenaar30613902019-12-01 22:11:18 +01003625 redraw_cmdline = TRUE; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 else
3627 showmode();
3628 }
3629 }
3630#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003631 // may unshow different cursor shape
Bram Moolenaarf085f422017-06-07 20:39:47 +02003632 if (gui.in_use && shape_changed)
3633 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634#endif
Bram Moolenaar6edbbd82019-03-10 09:41:51 +01003635 if (timedout && c == ESC)
3636 {
3637 char_u nop_buf[3];
3638
3639 // When recording there will be no timeout. Add a <Nop> after the ESC
3640 // to avoid that it forms a key code with following characters.
3641 nop_buf[0] = K_SPECIAL;
3642 nop_buf[1] = KS_EXTRA;
3643 nop_buf[2] = KE_NOP;
3644 gotchars(nop_buf, 3);
3645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003647 --vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648
3649 return c;
3650}
3651
3652/*
3653 * inchar() - get one character from
3654 * 1. a scriptfile
3655 * 2. the keyboard
3656 *
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003657 * As many characters as we can get (up to 'maxlen') are put in "buf" and
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 * NUL terminated (buffer length must be 'maxlen' + 1).
3659 * Minimum for "maxlen" is 3!!!!
3660 *
3661 * "tb_change_cnt" is the value of typebuf.tb_change_cnt if "buf" points into
3662 * it. When typebuf.tb_change_cnt changes (e.g., when a message is received
3663 * from a remote client) "buf" can no longer be used. "tb_change_cnt" is 0
3664 * otherwise.
3665 *
3666 * If we got an interrupt all input is read until none is available.
3667 *
3668 * If wait_time == 0 there is no waiting for the char.
3669 * If wait_time == n we wait for n msec for a character to arrive.
3670 * If wait_time == -1 we wait forever for a character to arrive.
3671 *
3672 * Return the number of obtained characters.
3673 * Return -1 when end of input script reached.
3674 */
Bram Moolenaarcda77642016-06-04 13:32:35 +02003675 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003676inchar(
3677 char_u *buf,
3678 int maxlen,
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003679 long wait_time) // milliseconds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680{
Bram Moolenaar30613902019-12-01 22:11:18 +01003681 int len = 0; // init for GCC
3682 int retesc = FALSE; // return ESC with gotint
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 int script_char;
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003684 int tb_change_cnt = typebuf.tb_change_cnt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685
Bram Moolenaar30613902019-12-01 22:11:18 +01003686 if (wait_time == -1L || wait_time > 100L) // flush output before waiting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 {
3688 cursor_on();
Bram Moolenaara338adc2018-01-31 20:51:47 +01003689 out_flush_cursor(FALSE, FALSE);
3690#if defined(FEAT_GUI) && defined(FEAT_MOUSESHAPE)
3691 if (gui.in_use && postponed_mouseshape)
3692 update_mouseshape(-1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693#endif
3694 }
3695
3696 /*
3697 * Don't reset these when at the hit-return prompt, otherwise a endless
3698 * recursive loop may result (write error in swapfile, hit-return, timeout
3699 * on char wait, flush swapfile, write error....).
3700 */
Bram Moolenaar24959102022-05-07 20:01:16 +01003701 if (State != MODE_HITRETURN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003703 did_outofmem_msg = FALSE; // display out of memory message (again)
3704 did_swapwrite_msg = FALSE; // display swap file write error again
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 }
Bram Moolenaar30613902019-12-01 22:11:18 +01003706 undo_off = FALSE; // restart undo now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707
3708 /*
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00003709 * Get a character from a script file if there is one.
3710 * If interrupted: Stop reading script files, close them all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 */
3712 script_char = -1;
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00003713 while (scriptin[curscript] != NULL && script_char < 0
3714#ifdef FEAT_EVAL
3715 && !ignore_script
3716#endif
3717 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 {
Bram Moolenaar93c88e02015-09-15 14:12:05 +02003719#ifdef MESSAGE_QUEUE
3720 parse_queued_messages();
Bram Moolenaarf2330482008-06-24 20:19:36 +00003721#endif
3722
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 if (got_int || (script_char = getc(scriptin[curscript])) < 0)
3724 {
Bram Moolenaar30613902019-12-01 22:11:18 +01003725 // Reached EOF.
3726 // Careful: closescript() frees typebuf.tb_buf[] and buf[] may
3727 // point inside typebuf.tb_buf[]. Don't use buf[] after this!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 closescript();
3729 /*
3730 * When reading script file is interrupted, return an ESC to get
3731 * back to normal mode.
3732 * Otherwise return -1, because typebuf.tb_buf[] has changed.
3733 */
3734 if (got_int)
3735 retesc = TRUE;
3736 else
3737 return -1;
3738 }
3739 else
3740 {
3741 buf[0] = script_char;
3742 len = 1;
3743 }
3744 }
3745
Bram Moolenaar30613902019-12-01 22:11:18 +01003746 if (script_char < 0) // did not get a character from script
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 {
3748 /*
3749 * If we got an interrupt, skip all previously typed characters and
3750 * return TRUE if quit reading script file.
3751 * Stop reading typeahead when a single CTRL-C was read,
3752 * fill_input_buf() returns this when not able to read from stdin.
3753 * Don't use buf[] here, closescript() may have freed typebuf.tb_buf[]
3754 * and buf may be pointing inside typebuf.tb_buf[].
3755 */
3756 if (got_int)
3757 {
kylo252ae6f1d82022-02-16 19:24:07 +00003758#define DUM_LEN (MAXMAPLEN * 3 + 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 char_u dum[DUM_LEN + 1];
3760
3761 for (;;)
3762 {
3763 len = ui_inchar(dum, DUM_LEN, 0L, 0);
zeertzjq17c95d92022-04-26 12:51:07 +01003764 if (len == 0 || (len == 1 && dum[0] == Ctrl_C))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 break;
3766 }
3767 return retesc;
3768 }
3769
3770 /*
3771 * Always flush the output characters when getting input characters
Bram Moolenaarcb574f42019-01-25 22:29:57 +01003772 * from the user and not just peeking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 */
Bram Moolenaarcb574f42019-01-25 22:29:57 +01003774 if (wait_time == -1L || wait_time > 10L)
3775 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776
3777 /*
3778 * Fill up to a third of the buffer, because each character may be
3779 * tripled below.
3780 */
3781 len = ui_inchar(buf, maxlen / 3, wait_time, tb_change_cnt);
3782 }
3783
Bram Moolenaar30613902019-12-01 22:11:18 +01003784 // If the typebuf was changed further down, it is like nothing was added by
3785 // this call.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 if (typebuf_changed(tb_change_cnt))
3787 return 0;
3788
Bram Moolenaar30613902019-12-01 22:11:18 +01003789 // Note the change in the typeahead buffer, this matters for when
3790 // vgetorpeek() is called recursively, e.g. using getchar(1) in a timer
3791 // function.
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003792 if (len > 0 && ++typebuf.tb_change_cnt == 0)
3793 typebuf.tb_change_cnt = 1;
3794
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003795 return fix_input_buffer(buf, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796}
3797
3798/*
3799 * Fix typed characters for use by vgetc() and check_termcode().
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003800 * "buf[]" must have room to triple the number of bytes!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 * Returns the new length.
3802 */
3803 int
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003804fix_input_buffer(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805{
3806 int i;
3807 char_u *p = buf;
3808
3809 /*
3810 * Two characters are special: NUL and K_SPECIAL.
3811 * When compiled With the GUI CSI is also special.
3812 * Replace NUL by K_SPECIAL KS_ZERO KE_FILLER
3813 * Replace K_SPECIAL by K_SPECIAL KS_SPECIAL KE_FILLER
3814 * Replace CSI by K_SPECIAL KS_EXTRA KE_CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 */
3816 for (i = len; --i >= 0; ++p)
3817 {
3818#ifdef FEAT_GUI
Bram Moolenaar30613902019-12-01 22:11:18 +01003819 // When the GUI is used any character can come after a CSI, don't
3820 // escape it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 if (gui.in_use && p[0] == CSI && i >= 2)
3822 {
3823 p += 2;
3824 i -= 2;
3825 }
zeertzjq646bb722022-02-16 17:51:47 +00003826# ifndef MSWIN
3827 // When not on MS-Windows and the GUI is not used CSI needs to be
3828 // escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 else if (!gui.in_use && p[0] == CSI)
3830 {
3831 mch_memmove(p + 3, p + 1, (size_t)i);
3832 *p++ = K_SPECIAL;
3833 *p++ = KS_EXTRA;
3834 *p = (int)KE_CSI;
3835 len += 2;
3836 }
zeertzjq646bb722022-02-16 17:51:47 +00003837# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 else
3839#endif
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003840 if (p[0] == NUL || (p[0] == K_SPECIAL
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003841 // timeout may generate K_CURSORHOLD
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003842 && (i < 2 || p[1] != KS_EXTRA || p[2] != (int)KE_CURSORHOLD)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003843#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003844 // Win32 console passes modifiers
3845 && (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003846# ifdef VIMDLL
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003847 gui.in_use ||
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003848# endif
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003849 (i < 2 || p[1] != KS_MODIFIER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850#endif
3851 ))
3852 {
3853 mch_memmove(p + 3, p + 1, (size_t)i);
3854 p[2] = K_THIRD(p[0]);
3855 p[1] = K_SECOND(p[0]);
3856 p[0] = K_SPECIAL;
3857 p += 2;
3858 len += 2;
3859 }
3860 }
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003861 *p = NUL; // add trailing NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 return len;
3863}
3864
3865#if defined(USE_INPUT_BUF) || defined(PROTO)
3866/*
3867 * Return TRUE when bytes are in the input buffer or in the typeahead buffer.
3868 * Normally the input buffer would be sufficient, but the server_to_input_buf()
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003869 * or feedkeys() may insert characters in the typeahead buffer while we are
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003870 * waiting for input to arrive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 */
3872 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003873input_available(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874{
3875 return (!vim_is_input_buf_empty()
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003876# if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
3877 || typebuf_was_filled
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878# endif
3879 );
3880}
3881#endif
Bram Moolenaar957cf672020-11-12 14:21:06 +01003882
3883/*
3884 * Function passed to do_cmdline() to get the command after a <Cmd> key from
3885 * typeahead.
3886 */
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003887 static char_u *
Bram Moolenaar957cf672020-11-12 14:21:06 +01003888getcmdkeycmd(
3889 int promptc UNUSED,
3890 void *cookie UNUSED,
3891 int indent UNUSED,
3892 getline_opt_T do_concat UNUSED)
3893{
3894 garray_T line_ga;
3895 int c1 = -1;
3896 int c2;
3897 int cmod = 0;
3898 int aborted = FALSE;
3899
3900 ga_init2(&line_ga, 1, 32);
3901
3902 // no mapping for these characters
3903 no_mapping++;
3904
3905 got_int = FALSE;
3906 while (c1 != NUL && !aborted)
3907 {
Yegappan Lakshmananfadc02a2023-01-27 21:03:12 +00003908 if (ga_grow(&line_ga, 32) == FAIL)
Bram Moolenaarca359cb2020-11-15 20:49:41 +01003909 {
3910 aborted = TRUE;
3911 break;
3912 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003913
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003914 if (vgetorpeek(FALSE) == NUL)
Bram Moolenaar957cf672020-11-12 14:21:06 +01003915 {
3916 // incomplete <Cmd> is an error, because there is not much the user
3917 // could do in this state.
3918 emsg(_(e_cmd_mapping_must_end_with_cr));
3919 aborted = TRUE;
3920 break;
3921 }
3922
3923 // Get one character at a time.
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003924 c1 = vgetorpeek(TRUE);
3925
Bram Moolenaar957cf672020-11-12 14:21:06 +01003926 // Get two extra bytes for special keys
3927 if (c1 == K_SPECIAL)
3928 {
Bram Moolenaarca9d8d22022-04-04 22:09:30 +01003929 c1 = vgetorpeek(TRUE);
3930 c2 = vgetorpeek(TRUE);
Bram Moolenaar957cf672020-11-12 14:21:06 +01003931 if (c1 == KS_MODIFIER)
3932 {
3933 cmod = c2;
3934 continue;
3935 }
3936 c1 = TO_SPECIAL(c1, c2);
Bram Moolenaardffa6ea2022-11-29 20:33:20 +00003937
3938 // K_ESC is used to avoid ambiguity with the single Esc character
3939 // that might be the start of an escape sequence. Convert it back
3940 // to a single Esc here.
3941 if (c1 == K_ESC)
3942 c1 = ESC;
Bram Moolenaar957cf672020-11-12 14:21:06 +01003943 }
Bram Moolenaar4a441202020-11-28 14:43:26 +01003944 if (c1 == Ctrl_V)
3945 {
3946 // CTRL-V is followed by octal, hex or other characters, reverses
3947 // what AppendToRedobuffLit() does.
Bram Moolenaarc896adb2022-11-19 19:02:40 +00003948 ++no_reduce_keys; // don't merge modifyOtherKeys
Bram Moolenaar0684e362020-12-03 19:54:42 +01003949 c1 = get_literal(TRUE);
Bram Moolenaarc896adb2022-11-19 19:02:40 +00003950 --no_reduce_keys;
Bram Moolenaar4a441202020-11-28 14:43:26 +01003951 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003952
3953 if (got_int)
3954 aborted = TRUE;
3955 else if (c1 == '\r' || c1 == '\n')
3956 c1 = NUL; // end the line
3957 else if (c1 == ESC)
3958 aborted = TRUE;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003959 else if (c1 == K_COMMAND || c1 == K_SCRIPT_COMMAND)
Bram Moolenaar957cf672020-11-12 14:21:06 +01003960 {
3961 // give a nicer error message for this special case
3962 emsg(_(e_cmd_mapping_must_end_with_cr_before_second_cmd));
3963 aborted = TRUE;
3964 }
3965 else if (IS_SPECIAL(c1))
3966 {
3967 if (c1 == K_SNR)
Bram Moolenaarc77534c2020-11-18 11:34:37 +01003968 ga_concat(&line_ga, (char_u *)"<SNR>");
Bram Moolenaar957cf672020-11-12 14:21:06 +01003969 else
3970 {
dundargocc57b5bc2022-11-02 13:30:51 +00003971 semsg(e_cmd_mapping_must_not_include_str_key,
Bram Moolenaar957cf672020-11-12 14:21:06 +01003972 get_special_key_name(c1, cmod));
3973 aborted = TRUE;
3974 }
3975 }
3976 else
Bram Moolenaara9725222022-01-16 13:30:33 +00003977 ga_append(&line_ga, c1);
Bram Moolenaar957cf672020-11-12 14:21:06 +01003978
3979 cmod = 0;
3980 }
3981
3982 no_mapping--;
3983
3984 if (aborted)
3985 ga_clear(&line_ga);
3986
3987 return (char_u *)line_ga.ga_data;
3988}
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003989
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01003990#if defined(FEAT_EVAL) || defined(PROTO)
3991/*
Bram Moolenaar6b066c62023-02-20 18:44:33 +00003992 * If there was a mapping we get its SID. Otherwise, use "last_used_sid", it
3993 * is set when redo'ing.
3994 * Put this SID in the redo buffer, so that "." will use the same script
3995 * context.
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01003996 */
3997 void
3998may_add_last_used_map_to_redobuff(void)
3999{
Bram Moolenaar6b066c62023-02-20 18:44:33 +00004000 char_u buf[3 + 20];
4001 int sid = -1;
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004002
Bram Moolenaar6b066c62023-02-20 18:44:33 +00004003 if (last_used_map != NULL)
4004 sid = last_used_map->m_script_ctx.sc_sid;
4005 if (sid < 0)
4006 sid = last_used_sid;
4007
4008 if (sid < 0)
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004009 return;
4010
4011 // <K_SID>{nr};
4012 buf[0] = K_SPECIAL;
4013 buf[1] = KS_EXTRA;
4014 buf[2] = KE_SID;
Bram Moolenaar6b066c62023-02-20 18:44:33 +00004015 vim_snprintf((char *)buf + 3, 20, "%d;", sid);
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004016 add_buff(&redobuff, buf, -1L);
4017}
4018#endif
4019
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004020 int
Bram Moolenaard4e2f502022-01-15 18:48:32 +00004021do_cmdkey_command(int key UNUSED, int flags)
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004022{
4023 int res;
4024#ifdef FEAT_EVAL
Bram Moolenaara9725222022-01-16 13:30:33 +00004025 sctx_T save_current_sctx = {-1, 0, 0, 0};
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004026
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004027 if (key == K_SCRIPT_COMMAND
4028 && (last_used_map != NULL || SCRIPT_ID_VALID(last_used_sid)))
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004029 {
4030 save_current_sctx = current_sctx;
Bram Moolenaarddf7dba2022-09-05 16:53:21 +01004031 if (last_used_map != NULL)
4032 current_sctx = last_used_map->m_script_ctx;
4033 else
4034 {
4035 current_sctx.sc_sid = last_used_sid;
4036 current_sctx.sc_lnum = 0;
4037 current_sctx.sc_version = SCRIPT_ITEM(last_used_sid)->sn_version;
4038 }
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004039 }
4040#endif
4041
4042 res = do_cmdline(NULL, getcmdkeycmd, NULL, flags);
4043
4044#ifdef FEAT_EVAL
Bram Moolenaara9725222022-01-16 13:30:33 +00004045 if (save_current_sctx.sc_sid >= 0)
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004046 current_sctx = save_current_sctx;
4047#endif
4048
4049 return res;
4050}
4051
4052#if defined(FEAT_EVAL) || defined(PROTO)
4053 void
Bram Moolenaarf61c89d2022-01-19 22:51:48 +00004054reset_last_used_map(mapblock_T *mp)
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004055{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00004056 if (last_used_map != mp)
4057 return;
4058
4059 last_used_map = NULL;
4060 last_used_sid = -1;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00004061}
4062#endif