blob: ea628cb08256c7af232f3892b19b6412148086e1 [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/*
11 * getchar.c
12 *
13 * functions related with getting a character from the user/mapping/redo/...
14 *
15 * manipulations with redo buffer and stuff buffer
16 * mappings and abbreviations
17 */
18
19#include "vim.h"
20
21/*
22 * These buffers are used for storing:
23 * - stuffed characters: A command that is translated into another command.
24 * - redo characters: will redo the last change.
Bram Moolenaarf6f95d92009-11-11 15:23:37 +000025 * - recorded characters: for the "q" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026 *
27 * The bytes are stored like in the typeahead buffer:
28 * - K_SPECIAL introduces a special key (two more bytes follow). A literal
29 * K_SPECIAL is stored as K_SPECIAL KS_SPECIAL KE_FILLER.
30 * - CSI introduces a GUI termcap code (also when gui.in_use is FALSE,
31 * otherwise switching the GUI on would make mappings invalid).
32 * A literal CSI is stored as CSI KS_EXTRA KE_CSI.
33 * These translations are also done on multi-byte characters!
34 *
35 * Escaping CSI bytes is done by the system-specific input functions, called
36 * by ui_inchar().
37 * Escaping K_SPECIAL is done by inchar().
38 * Un-escaping is done by vgetc().
39 */
40
41#define MINIMAL_SIZE 20 /* minimal size for b_str */
42
Bram Moolenaar285e3352018-04-18 23:01:13 +020043static buffheader_T redobuff = {{NULL, {NUL}}, NULL, 0, 0};
44static buffheader_T old_redobuff = {{NULL, {NUL}}, NULL, 0, 0};
45static buffheader_T recordbuff = {{NULL, {NUL}}, NULL, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +000046
47static int typeahead_char = 0; /* typeahead char that's not flushed */
48
49/*
50 * when block_redo is TRUE redo buffer will not be changed
51 * used by edit() to repeat insertions and 'V' command for redoing
52 */
53static int block_redo = FALSE;
54
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +000055static int KeyNoremap = 0; /* remapping flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000056
57/*
Bram Moolenaarfd89d7e2016-06-04 20:25:05 +020058 * Variables used by vgetorpeek() and flush_buffers().
Bram Moolenaar071d4272004-06-13 20:20:40 +000059 *
60 * typebuf.tb_buf[] contains all characters that are not consumed yet.
61 * typebuf.tb_buf[typebuf.tb_off] is the first valid character.
62 * typebuf.tb_buf[typebuf.tb_off + typebuf.tb_len - 1] is the last valid char.
63 * typebuf.tb_buf[typebuf.tb_off + typebuf.tb_len] must be NUL.
64 * The head of the buffer may contain the result of mappings, abbreviations
65 * and @a commands. The length of this part is typebuf.tb_maplen.
66 * typebuf.tb_silent is the part where <silent> applies.
67 * After the head are characters that come from the terminal.
68 * typebuf.tb_no_abbr_cnt is the number of characters in typebuf.tb_buf that
69 * should not be considered for abbreviations.
70 * Some parts of typebuf.tb_buf may not be mapped. These parts are remembered
71 * in typebuf.tb_noremap[], which is the same length as typebuf.tb_buf and
72 * contains RM_NONE for the characters that are not to be remapped.
73 * typebuf.tb_noremap[typebuf.tb_off] is the first valid flag.
74 * (typebuf has been put in globals.h, because check_termcode() needs it).
75 */
76#define RM_YES 0 /* tb_noremap: remap */
77#define RM_NONE 1 /* tb_noremap: don't remap */
78#define RM_SCRIPT 2 /* tb_noremap: remap local script mappings */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000079#define RM_ABBR 4 /* tb_noremap: don't remap, do abbrev. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000080
81/* typebuf.tb_buf has three parts: room in front (for result of mappings), the
82 * middle for typeahead and room for new characters (which needs to be 3 *
83 * MAXMAPLEN) for the Amiga).
84 */
85#define TYPELEN_INIT (5 * (MAXMAPLEN + 3))
86static char_u typebuf_init[TYPELEN_INIT]; /* initial typebuf.tb_buf */
87static char_u noremapbuf_init[TYPELEN_INIT]; /* initial typebuf.tb_noremap */
88
89static int last_recorded_len = 0; /* number of last recorded chars */
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);
94static void closescript(void);
95static int vgetorpeek(int);
Bram Moolenaar0f0f2302017-08-30 18:52:56 +020096static int inchar(char_u *buf, int maxlen, long wait_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +000097
98/*
99 * Free and clear a buffer.
100 */
101 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100102free_buff(buffheader_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100104 buffblock_T *p, *np;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105
Bram Moolenaar285e3352018-04-18 23:01:13 +0200106 for (p = buf->bh_first.b_next; p != NULL; p = np)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107 {
108 np = p->b_next;
109 vim_free(p);
110 }
Bram Moolenaar285e3352018-04-18 23:01:13 +0200111 buf->bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112}
113
114/*
115 * Return the contents of a buffer as a single string.
116 * K_SPECIAL and CSI in the returned string are escaped.
117 */
118 static char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100119get_buffcont(
120 buffheader_T *buffer,
121 int dozero) /* count == zero is not an error */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122{
123 long_u count = 0;
124 char_u *p = NULL;
125 char_u *p2;
126 char_u *str;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200127 buffblock_T *bp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128
129 /* compute the total length of the string */
Bram Moolenaar285e3352018-04-18 23:01:13 +0200130 for (bp = buffer->bh_first.b_next; bp != NULL; bp = bp->b_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131 count += (long_u)STRLEN(bp->b_str);
132
Bram Moolenaar18a4ba22019-05-24 19:39:03 +0200133 if ((count || dozero) && (p = alloc(count + 1)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134 {
135 p2 = p;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200136 for (bp = buffer->bh_first.b_next; bp != NULL; bp = bp->b_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137 for (str = bp->b_str; *str; )
138 *p2++ = *str++;
139 *p2 = NUL;
140 }
141 return (p);
142}
143
144/*
145 * Return the contents of the record buffer as a single string
146 * and clear the record buffer.
147 * K_SPECIAL and CSI in the returned string are escaped.
148 */
149 char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100150get_recorded(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151{
152 char_u *p;
153 size_t len;
154
155 p = get_buffcont(&recordbuff, TRUE);
156 free_buff(&recordbuff);
157
158 /*
159 * Remove the characters that were added the last time, these must be the
160 * (possibly mapped) characters that stopped the recording.
161 */
162 len = STRLEN(p);
163 if ((int)len >= last_recorded_len)
164 {
165 len -= last_recorded_len;
166 p[len] = NUL;
167 }
168
169 /*
170 * When stopping recording from Insert mode with CTRL-O q, also remove the
171 * CTRL-O.
172 */
173 if (len > 0 && restart_edit != 0 && p[len - 1] == Ctrl_O)
174 p[len - 1] = NUL;
175
176 return (p);
177}
178
179/*
180 * Return the contents of the redo buffer as a single string.
181 * K_SPECIAL and CSI in the returned string are escaped.
182 */
183 char_u *
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100184get_inserted(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185{
Bram Moolenaarf2330482008-06-24 20:19:36 +0000186 return get_buffcont(&redobuff, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187}
188
189/*
Bram Moolenaar4e86cba2007-05-06 11:56:32 +0000190 * Add string "s" after the current block of buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191 * K_SPECIAL and CSI should have been escaped already.
192 */
193 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100194add_buff(
195 buffheader_T *buf,
196 char_u *s,
197 long slen) /* length of "s" or -1 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100199 buffblock_T *p;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200200 long_u len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201
202 if (slen < 0)
203 slen = (long)STRLEN(s);
204 if (slen == 0) /* don't add empty strings */
205 return;
206
Bram Moolenaar285e3352018-04-18 23:01:13 +0200207 if (buf->bh_first.b_next == NULL) /* first add to list */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208 {
209 buf->bh_space = 0;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200210 buf->bh_curr = &(buf->bh_first);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211 }
212 else if (buf->bh_curr == NULL) /* buffer has already been read */
213 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100214 iemsg(_("E222: Add to read buffer"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215 return;
216 }
217 else if (buf->bh_index != 0)
Bram Moolenaar285e3352018-04-18 23:01:13 +0200218 mch_memmove(buf->bh_first.b_next->b_str,
219 buf->bh_first.b_next->b_str + buf->bh_index,
220 STRLEN(buf->bh_first.b_next->b_str + buf->bh_index) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221 buf->bh_index = 0;
222
223 if (buf->bh_space >= (int)slen)
224 {
225 len = (long_u)STRLEN(buf->bh_curr->b_str);
Bram Moolenaarb6356332005-07-18 21:40:44 +0000226 vim_strncpy(buf->bh_curr->b_str + len, s, (size_t)slen);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227 buf->bh_space -= slen;
228 }
229 else
230 {
231 if (slen < MINIMAL_SIZE)
232 len = MINIMAL_SIZE;
233 else
234 len = slen;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200235 p = alloc(sizeof(buffblock_T) + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236 if (p == NULL)
237 return; /* no space, just forget it */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000238 buf->bh_space = (int)(len - slen);
Bram Moolenaarb6356332005-07-18 21:40:44 +0000239 vim_strncpy(p->b_str, s, (size_t)slen);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240
Bram Moolenaar285e3352018-04-18 23:01:13 +0200241 p->b_next = buf->bh_curr->b_next;
242 buf->bh_curr->b_next = p;
243 buf->bh_curr = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244 }
245 return;
246}
247
248/*
249 * Add number "n" to buffer "buf".
250 */
251 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100252add_num_buff(buffheader_T *buf, long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253{
254 char_u number[32];
255
256 sprintf((char *)number, "%ld", n);
257 add_buff(buf, number, -1L);
258}
259
260/*
261 * Add character 'c' to buffer "buf".
262 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
263 */
264 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100265add_char_buff(buffheader_T *buf, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267 char_u bytes[MB_MAXBYTES + 1];
268 int len;
269 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270 char_u temp[4];
271
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272 if (IS_SPECIAL(c))
273 len = 1;
274 else
275 len = (*mb_char2bytes)(c, bytes);
276 for (i = 0; i < len; ++i)
277 {
278 if (!IS_SPECIAL(c))
279 c = bytes[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280
281 if (IS_SPECIAL(c) || c == K_SPECIAL || c == NUL)
282 {
283 /* translate special key code into three byte sequence */
284 temp[0] = K_SPECIAL;
285 temp[1] = K_SECOND(c);
286 temp[2] = K_THIRD(c);
287 temp[3] = NUL;
288 }
289#ifdef FEAT_GUI
290 else if (c == CSI)
291 {
292 /* Translate a CSI to a CSI - KS_EXTRA - KE_CSI sequence */
293 temp[0] = CSI;
294 temp[1] = KS_EXTRA;
295 temp[2] = (int)KE_CSI;
296 temp[3] = NUL;
297 }
298#endif
299 else
300 {
301 temp[0] = c;
302 temp[1] = NUL;
303 }
304 add_buff(buf, temp, -1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306}
307
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100308/* First read ahead buffer. Used for translated commands. */
Bram Moolenaar285e3352018-04-18 23:01:13 +0200309static buffheader_T readbuf1 = {{NULL, {NUL}}, NULL, 0, 0};
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100310
311/* Second read ahead buffer. Used for redo. */
Bram Moolenaar285e3352018-04-18 23:01:13 +0200312static buffheader_T readbuf2 = {{NULL, {NUL}}, NULL, 0, 0};
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100313
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314/*
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100315 * Get one byte from the read buffers. Use readbuf1 one first, use readbuf2
316 * if that one is empty.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317 * If advance == TRUE go to the next char.
318 * No translation is done K_SPECIAL and CSI are escaped.
319 */
320 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100321read_readbuffers(int advance)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100323 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100325 c = read_readbuf(&readbuf1, advance);
326 if (c == NUL)
327 c = read_readbuf(&readbuf2, advance);
328 return c;
329}
330
331 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100332read_readbuf(buffheader_T *buf, int advance)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100333{
334 char_u c;
335 buffblock_T *curr;
336
Bram Moolenaar285e3352018-04-18 23:01:13 +0200337 if (buf->bh_first.b_next == NULL) /* buffer is empty */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338 return NUL;
339
Bram Moolenaar285e3352018-04-18 23:01:13 +0200340 curr = buf->bh_first.b_next;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100341 c = curr->b_str[buf->bh_index];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342
343 if (advance)
344 {
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100345 if (curr->b_str[++buf->bh_index] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346 {
Bram Moolenaar285e3352018-04-18 23:01:13 +0200347 buf->bh_first.b_next = curr->b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348 vim_free(curr);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100349 buf->bh_index = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 }
351 }
352 return c;
353}
354
355/*
Bram Moolenaar06811f32014-02-15 16:17:07 +0100356 * Prepare the read buffers for reading (if they contain something).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357 */
358 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100359start_stuff(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360{
Bram Moolenaar285e3352018-04-18 23:01:13 +0200361 if (readbuf1.bh_first.b_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362 {
Bram Moolenaar285e3352018-04-18 23:01:13 +0200363 readbuf1.bh_curr = &(readbuf1.bh_first);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100364 readbuf1.bh_space = 0;
365 }
Bram Moolenaar285e3352018-04-18 23:01:13 +0200366 if (readbuf2.bh_first.b_next != NULL)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100367 {
Bram Moolenaar285e3352018-04-18 23:01:13 +0200368 readbuf2.bh_curr = &(readbuf2.bh_first);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100369 readbuf2.bh_space = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370 }
371}
372
373/*
374 * Return TRUE if the stuff buffer is empty.
375 */
376 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100377stuff_empty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378{
Bram Moolenaar285e3352018-04-18 23:01:13 +0200379 return (readbuf1.bh_first.b_next == NULL
380 && readbuf2.bh_first.b_next == NULL);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100381}
382
Bram Moolenaar113e1072019-01-20 15:30:40 +0100383#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100384/*
385 * Return TRUE if readbuf1 is empty. There may still be redo characters in
386 * redbuf2.
387 */
388 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100389readbuf1_empty(void)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100390{
Bram Moolenaar285e3352018-04-18 23:01:13 +0200391 return (readbuf1.bh_first.b_next == NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100393#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394
395/*
396 * Set a typeahead character that won't be flushed.
397 */
398 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100399typeahead_noflush(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400{
401 typeahead_char = c;
402}
403
404/*
405 * Remove the contents of the stuff buffer and the mapped characters in the
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100406 * typeahead buffer (used in case of an error). If "flush_typeahead" is true,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407 * flush all typeahead characters (used when interrupted by a CTRL-C).
408 */
409 void
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200410flush_buffers(flush_buffers_T flush_typeahead)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411{
412 init_typebuf();
413
414 start_stuff();
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100415 while (read_readbuffers(TRUE) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416 ;
417
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200418 if (flush_typeahead == FLUSH_MINIMAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419 {
Bram Moolenaar6a2633b2018-10-07 23:16:36 +0200420 // remove mapped characters at the start only
421 typebuf.tb_off += typebuf.tb_maplen;
422 typebuf.tb_len -= typebuf.tb_maplen;
423 }
424 else
425 {
426 // remove typeahead
427 if (flush_typeahead == FLUSH_INPUT)
428 // We have to get all characters, because we may delete the first
429 // part of an escape sequence. In an xterm we get one char at a
430 // time and we have to get them all.
431 while (inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 10L) != 0)
432 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433 typebuf.tb_off = MAXMAPLEN;
434 typebuf.tb_len = 0;
Bram Moolenaar4eb65312017-06-24 18:49:00 +0200435#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
436 /* Reset the flag that text received from a client or from feedkeys()
437 * was inserted in the typeahead buffer. */
438 typebuf_was_filled = FALSE;
439#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441 typebuf.tb_maplen = 0;
442 typebuf.tb_silent = 0;
443 cmd_silent = FALSE;
444 typebuf.tb_no_abbr_cnt = 0;
445}
446
447/*
448 * The previous contents of the redo buffer is kept in old_redobuffer.
449 * This is used for the CTRL-O <.> command in insert mode.
450 */
451 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100452ResetRedobuff(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453{
454 if (!block_redo)
455 {
456 free_buff(&old_redobuff);
457 old_redobuff = redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200458 redobuff.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459 }
460}
461
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100462/*
463 * Discard the contents of the redo buffer and restore the previous redo
464 * buffer.
465 */
466 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100467CancelRedo(void)
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100468{
469 if (!block_redo)
470 {
471 free_buff(&redobuff);
472 redobuff = old_redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200473 old_redobuff.bh_first.b_next = NULL;
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100474 start_stuff();
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100475 while (read_readbuffers(TRUE) != NUL)
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100476 ;
477 }
478}
479
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480/*
481 * Save redobuff and old_redobuff to save_redobuff and save_old_redobuff.
482 * Used before executing autocommands and user functions.
483 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 void
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200485saveRedobuff(save_redo_T *save_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486{
487 char_u *s;
488
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200489 save_redo->sr_redobuff = redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200490 redobuff.bh_first.b_next = NULL;
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200491 save_redo->sr_old_redobuff = old_redobuff;
Bram Moolenaar285e3352018-04-18 23:01:13 +0200492 old_redobuff.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200494 /* Make a copy, so that ":normal ." in a function works. */
495 s = get_buffcont(&save_redo->sr_redobuff, FALSE);
496 if (s != NULL)
497 {
498 add_buff(&redobuff, s, -1L);
499 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500 }
501}
502
503/*
504 * Restore redobuff and old_redobuff from save_redobuff and save_old_redobuff.
505 * Used after executing autocommands and user functions.
506 */
507 void
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200508restoreRedobuff(save_redo_T *save_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509{
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200510 free_buff(&redobuff);
511 redobuff = save_redo->sr_redobuff;
512 free_buff(&old_redobuff);
513 old_redobuff = save_redo->sr_old_redobuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515
516/*
517 * Append "s" to the redo buffer.
518 * K_SPECIAL and CSI should already have been escaped.
519 */
520 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100521AppendToRedobuff(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522{
523 if (!block_redo)
524 add_buff(&redobuff, s, -1L);
525}
526
527/*
528 * Append to Redo buffer literally, escaping special characters with CTRL-V.
529 * K_SPECIAL and CSI are escaped as well.
530 */
531 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100532AppendToRedobuffLit(
533 char_u *str,
534 int len) /* length of "str" or -1 for up to the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535{
Bram Moolenaarebefac62005-12-28 22:39:57 +0000536 char_u *s = str;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537 int c;
538 char_u *start;
539
540 if (block_redo)
541 return;
542
Bram Moolenaarebefac62005-12-28 22:39:57 +0000543 while (len < 0 ? *s != NUL : s - str < len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 {
545 /* Put a string of normal characters in the redo buffer (that's
546 * faster). */
547 start = s;
548 while (*s >= ' '
549#ifndef EBCDIC
550 && *s < DEL /* EBCDIC: all chars above space are normal */
551#endif
Bram Moolenaarebefac62005-12-28 22:39:57 +0000552 && (len < 0 || s - str < len))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 ++s;
554
555 /* Don't put '0' or '^' as last character, just in case a CTRL-D is
556 * typed next. */
557 if (*s == NUL && (s[-1] == '0' || s[-1] == '^'))
558 --s;
559 if (s > start)
560 add_buff(&redobuff, start, (long)(s - start));
561
Bram Moolenaarebefac62005-12-28 22:39:57 +0000562 if (*s == NUL || (len >= 0 && s - str >= len))
563 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564
Bram Moolenaarebefac62005-12-28 22:39:57 +0000565 /* Handle a special or multibyte character. */
Bram Moolenaarebefac62005-12-28 22:39:57 +0000566 if (has_mbyte)
567 /* Handle composing chars separately. */
568 c = mb_cptr2char_adv(&s);
569 else
Bram Moolenaarebefac62005-12-28 22:39:57 +0000570 c = *s++;
571 if (c < ' ' || c == DEL || (*s == NUL && (c == '0' || c == '^')))
572 add_char_buff(&redobuff, Ctrl_V);
573
574 /* CTRL-V '0' must be inserted as CTRL-V 048 (EBCDIC: xf0) */
575 if (*s == NUL && c == '0')
576#ifdef EBCDIC
577 add_buff(&redobuff, (char_u *)"xf0", 3L);
578#else
579 add_buff(&redobuff, (char_u *)"048", 3L);
580#endif
581 else
582 add_char_buff(&redobuff, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 }
584}
585
586/*
587 * Append a character to the redo buffer.
588 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
589 */
590 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100591AppendCharToRedobuff(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592{
593 if (!block_redo)
594 add_char_buff(&redobuff, c);
595}
596
597/*
598 * Append a number to the redo buffer.
599 */
600 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100601AppendNumberToRedobuff(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602{
603 if (!block_redo)
604 add_num_buff(&redobuff, n);
605}
606
607/*
608 * Append string "s" to the stuff buffer.
609 * CSI and K_SPECIAL must already have been escaped.
610 */
611 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100612stuffReadbuff(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100614 add_buff(&readbuf1, s, -1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615}
616
Bram Moolenaar4f5ce332014-07-30 16:00:58 +0200617/*
618 * Append string "s" to the redo stuff buffer.
619 * CSI and K_SPECIAL must already have been escaped.
620 */
621 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100622stuffRedoReadbuff(char_u *s)
Bram Moolenaar4f5ce332014-07-30 16:00:58 +0200623{
624 add_buff(&readbuf2, s, -1L);
625}
626
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100628stuffReadbuffLen(char_u *s, long len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100630 add_buff(&readbuf1, s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631}
632
633#if defined(FEAT_EVAL) || defined(PROTO)
634/*
635 * Stuff "s" into the stuff buffer, leaving special key codes unmodified and
636 * escaping other K_SPECIAL and CSI bytes.
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200637 * Change CR, LF and ESC into a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 */
639 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100640stuffReadbuffSpec(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641{
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200642 int c;
643
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 while (*s != NUL)
645 {
646 if (*s == K_SPECIAL && s[1] != NUL && s[2] != NUL)
647 {
648 /* Insert special key literally. */
649 stuffReadbuffLen(s, 3L);
650 s += 3;
651 }
652 else
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200653 {
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200654 c = mb_ptr2char_adv(&s);
Bram Moolenaar877b97b2011-04-28 17:30:09 +0200655 if (c == CAR || c == NL || c == ESC)
656 c = ' ';
657 stuffcharReadbuff(c);
658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 }
660}
661#endif
662
663/*
664 * Append a character to the stuff buffer.
665 * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters.
666 */
667 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100668stuffcharReadbuff(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100670 add_char_buff(&readbuf1, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671}
672
673/*
674 * Append a number to the stuff buffer.
675 */
676 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100677stuffnumReadbuff(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100679 add_num_buff(&readbuf1, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680}
681
682/*
683 * Read a character from the redo buffer. Translates K_SPECIAL, CSI and
684 * multibyte characters.
685 * The redo buffer is left as it is.
Bram Moolenaarbe094a12012-02-05 01:18:48 +0100686 * If init is TRUE, prepare for redo, return FAIL if nothing to redo, OK
687 * otherwise.
688 * If old is TRUE, use old_redobuff instead of redobuff.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 */
690 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100691read_redo(int init, int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692{
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100693 static buffblock_T *bp;
694 static char_u *p;
695 int c;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100696 int n;
697 char_u buf[MB_MAXBYTES + 1];
698 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699
700 if (init)
701 {
702 if (old_redo)
Bram Moolenaar285e3352018-04-18 23:01:13 +0200703 bp = old_redobuff.bh_first.b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 else
Bram Moolenaar285e3352018-04-18 23:01:13 +0200705 bp = redobuff.bh_first.b_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 if (bp == NULL)
707 return FAIL;
708 p = bp->b_str;
709 return OK;
710 }
711 if ((c = *p) != NUL)
712 {
713 /* Reverse the conversion done by add_char_buff() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 /* For a multi-byte character get all the bytes and return the
715 * converted character. */
716 if (has_mbyte && (c != K_SPECIAL || p[1] == KS_SPECIAL))
717 n = MB_BYTE2LEN_CHECK(c);
718 else
719 n = 1;
720 for (i = 0; ; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 {
722 if (c == K_SPECIAL) /* special key or escaped K_SPECIAL */
723 {
724 c = TO_SPECIAL(p[1], p[2]);
725 p += 2;
726 }
727#ifdef FEAT_GUI
728 if (c == CSI) /* escaped CSI */
729 p += 2;
730#endif
731 if (*++p == NUL && bp->b_next != NULL)
732 {
733 bp = bp->b_next;
734 p = bp->b_str;
735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 buf[i] = c;
737 if (i == n - 1) /* last byte of a character */
738 {
739 if (n != 1)
740 c = (*mb_ptr2char)(buf);
741 break;
742 }
743 c = *p;
744 if (c == NUL) /* cannot happen? */
745 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 }
747 }
748
749 return c;
750}
751
752/*
753 * Copy the rest of the redo buffer into the stuff buffer (in a slow way).
754 * If old_redo is TRUE, use old_redobuff instead of redobuff.
755 * The escaped K_SPECIAL and CSI are copied without translation.
756 */
757 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100758copy_redo(int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759{
760 int c;
761
762 while ((c = read_redo(FALSE, old_redo)) != NUL)
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100763 add_char_buff(&readbuf2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764}
765
766/*
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100767 * Stuff the redo buffer into readbuf2.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 * Insert the redo count into the command.
769 * If "old_redo" is TRUE, the last but one command is repeated
770 * instead of the last command (inserting text). This is used for
771 * CTRL-O <.> in insert mode
772 *
773 * return FAIL for failure, OK otherwise
774 */
775 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100776start_redo(long count, int old_redo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777{
778 int c;
779
780 /* init the pointers; return if nothing to redo */
781 if (read_redo(TRUE, old_redo) == FAIL)
782 return FAIL;
783
784 c = read_redo(FALSE, old_redo);
785
786 /* copy the buffer name, if present */
787 if (c == '"')
788 {
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100789 add_buff(&readbuf2, (char_u *)"\"", 1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 c = read_redo(FALSE, old_redo);
791
792 /* if a numbered buffer is used, increment the number */
793 if (c >= '1' && c < '9')
794 ++c;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100795 add_char_buff(&readbuf2, c);
Bram Moolenaar833093b2018-05-23 21:53:52 +0200796
797 /* the expression register should be re-evaluated */
798 if (c == '=')
799 {
800 add_char_buff(&readbuf2, CAR);
801 cmd_silent = TRUE;
802 }
803
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 c = read_redo(FALSE, old_redo);
805 }
806
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 if (c == 'v') /* redo Visual */
808 {
809 VIsual = curwin->w_cursor;
810 VIsual_active = TRUE;
811 VIsual_select = FALSE;
812 VIsual_reselect = TRUE;
813 redo_VIsual_busy = TRUE;
814 c = read_redo(FALSE, old_redo);
815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816
817 /* try to enter the count (in place of a previous count) */
818 if (count)
819 {
820 while (VIM_ISDIGIT(c)) /* skip "old" count */
821 c = read_redo(FALSE, old_redo);
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100822 add_num_buff(&readbuf2, count);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 }
824
825 /* copy from the redo buffer into the stuff buffer */
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100826 add_char_buff(&readbuf2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 copy_redo(old_redo);
828 return OK;
829}
830
831/*
832 * Repeat the last insert (R, o, O, a, A, i or I command) by stuffing
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100833 * the redo buffer into readbuf2.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 * return FAIL for failure, OK otherwise
835 */
836 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100837start_redo_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838{
839 int c;
840
841 if (read_redo(TRUE, FALSE) == FAIL)
842 return FAIL;
843 start_stuff();
844
845 /* skip the count and the command character */
846 while ((c = read_redo(FALSE, FALSE)) != NUL)
847 {
848 if (vim_strchr((char_u *)"AaIiRrOo", c) != NULL)
849 {
850 if (c == 'O' || c == 'o')
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100851 add_buff(&readbuf2, NL_STR, -1L);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 break;
853 }
854 }
855
856 /* copy the typed text from the redo buffer into the stuff buffer */
857 copy_redo(FALSE);
858 block_redo = TRUE;
859 return OK;
860}
861
862 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100863stop_redo_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864{
865 block_redo = FALSE;
866}
867
868/*
869 * Initialize typebuf.tb_buf to point to typebuf_init.
870 * alloc() cannot be used here: In out-of-memory situations it would
871 * be impossible to type anything.
872 */
873 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100874init_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875{
876 if (typebuf.tb_buf == NULL)
877 {
878 typebuf.tb_buf = typebuf_init;
879 typebuf.tb_noremap = noremapbuf_init;
880 typebuf.tb_buflen = TYPELEN_INIT;
881 typebuf.tb_len = 0;
Bram Moolenaard34f9b12017-04-08 18:41:13 +0200882 typebuf.tb_off = MAXMAPLEN + 4;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 typebuf.tb_change_cnt = 1;
884 }
885}
886
887/*
Bram Moolenaarb66bab32019-08-01 14:28:24 +0200888 * Returns TRUE when keys cannot be remapped.
889 */
890 int
891noremap_keys(void)
892{
893 return KeyNoremap & (RM_NONE|RM_SCRIPT);
894}
895
896/*
Bram Moolenaare7fedb62015-12-31 19:07:19 +0100897 * Insert a string in position 'offset' in the typeahead buffer (for "@r"
898 * and ":normal" command, vgetorpeek() and check_termcode()).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 *
900 * If noremap is REMAP_YES, new string can be mapped again.
901 * If noremap is REMAP_NONE, new string cannot be mapped again.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000902 * If noremap is REMAP_SKIP, fist char of new string cannot be mapped again,
903 * but abbreviations are allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 * If noremap is REMAP_SCRIPT, new string cannot be mapped again, except for
905 * script-local mappings.
906 * If noremap is > 0, that many characters of the new string cannot be mapped.
907 *
908 * If nottyped is TRUE, the string does not return KeyTyped (don't use when
909 * offset is non-zero!).
910 *
911 * If silent is TRUE, cmd_silent is set when the characters are obtained.
912 *
913 * return FAIL for failure, OK otherwise
914 */
915 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100916ins_typebuf(
917 char_u *str,
918 int noremap,
919 int offset,
920 int nottyped,
921 int silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922{
923 char_u *s1, *s2;
924 int newlen;
925 int addlen;
926 int i;
927 int newoff;
928 int val;
929 int nrm;
930
931 init_typebuf();
932 if (++typebuf.tb_change_cnt == 0)
933 typebuf.tb_change_cnt = 1;
934
935 addlen = (int)STRLEN(str);
Bram Moolenaar4e86cba2007-05-06 11:56:32 +0000936
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 if (offset == 0 && addlen <= typebuf.tb_off)
938 {
Bram Moolenaarcaa55b62017-01-10 13:51:09 +0100939 /*
940 * Easy case: there is room in front of typebuf.tb_buf[typebuf.tb_off]
941 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 typebuf.tb_off -= addlen;
943 mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen);
944 }
Bram Moolenaard34f9b12017-04-08 18:41:13 +0200945 else if (typebuf.tb_len == 0 && typebuf.tb_buflen
946 >= addlen + 3 * (MAXMAPLEN + 4))
947 {
948 /*
949 * Buffer is empty and string fits in the existing buffer.
950 * Leave some space before and after, if possible.
951 */
952 typebuf.tb_off = (typebuf.tb_buflen - addlen - 3 * (MAXMAPLEN + 4)) / 2;
953 mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen);
954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 else
956 {
Bram Moolenaarcaa55b62017-01-10 13:51:09 +0100957 /*
958 * Need to allocate a new buffer.
Bram Moolenaard34f9b12017-04-08 18:41:13 +0200959 * In typebuf.tb_buf there must always be room for 3 * (MAXMAPLEN + 4)
Bram Moolenaarcaa55b62017-01-10 13:51:09 +0100960 * characters. We add some extra room to avoid having to allocate too
961 * often.
962 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 newoff = MAXMAPLEN + 4;
964 newlen = typebuf.tb_len + addlen + newoff + 4 * (MAXMAPLEN + 4);
965 if (newlen < 0) /* string is getting too long */
966 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100967 emsg(_(e_toocompl)); /* also calls flush_buffers */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 setcursor();
969 return FAIL;
970 }
971 s1 = alloc(newlen);
972 if (s1 == NULL) /* out of memory */
973 return FAIL;
974 s2 = alloc(newlen);
975 if (s2 == NULL) /* out of memory */
976 {
977 vim_free(s1);
978 return FAIL;
979 }
980 typebuf.tb_buflen = newlen;
981
982 /* copy the old chars, before the insertion point */
983 mch_memmove(s1 + newoff, typebuf.tb_buf + typebuf.tb_off,
984 (size_t)offset);
985 /* copy the new chars */
986 mch_memmove(s1 + newoff + offset, str, (size_t)addlen);
987 /* copy the old chars, after the insertion point, including the NUL at
988 * the end */
989 mch_memmove(s1 + newoff + offset + addlen,
990 typebuf.tb_buf + typebuf.tb_off + offset,
991 (size_t)(typebuf.tb_len - offset + 1));
992 if (typebuf.tb_buf != typebuf_init)
993 vim_free(typebuf.tb_buf);
994 typebuf.tb_buf = s1;
995
996 mch_memmove(s2 + newoff, typebuf.tb_noremap + typebuf.tb_off,
997 (size_t)offset);
998 mch_memmove(s2 + newoff + offset + addlen,
999 typebuf.tb_noremap + typebuf.tb_off + offset,
1000 (size_t)(typebuf.tb_len - offset));
1001 if (typebuf.tb_noremap != noremapbuf_init)
1002 vim_free(typebuf.tb_noremap);
1003 typebuf.tb_noremap = s2;
1004
1005 typebuf.tb_off = newoff;
1006 }
1007 typebuf.tb_len += addlen;
1008
1009 /* If noremap == REMAP_SCRIPT: do remap script-local mappings. */
1010 if (noremap == REMAP_SCRIPT)
1011 val = RM_SCRIPT;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001012 else if (noremap == REMAP_SKIP)
1013 val = RM_ABBR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 else
1015 val = RM_NONE;
1016
1017 /*
1018 * Adjust typebuf.tb_noremap[] for the new characters:
1019 * If noremap == REMAP_NONE or REMAP_SCRIPT: new characters are
1020 * (sometimes) not remappable
1021 * If noremap == REMAP_YES: all the new characters are mappable
1022 * If noremap > 0: "noremap" characters are not remappable, the rest
1023 * mappable
1024 */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00001025 if (noremap == REMAP_SKIP)
1026 nrm = 1;
1027 else if (noremap < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 nrm = addlen;
1029 else
1030 nrm = noremap;
1031 for (i = 0; i < addlen; ++i)
1032 typebuf.tb_noremap[typebuf.tb_off + i + offset] =
1033 (--nrm >= 0) ? val : RM_YES;
1034
1035 /* tb_maplen and tb_silent only remember the length of mapped and/or
1036 * silent mappings at the start of the buffer, assuming that a mapped
1037 * sequence doesn't result in typed characters. */
1038 if (nottyped || typebuf.tb_maplen > offset)
1039 typebuf.tb_maplen += addlen;
1040 if (silent || typebuf.tb_silent > offset)
1041 {
1042 typebuf.tb_silent += addlen;
1043 cmd_silent = TRUE;
1044 }
1045 if (typebuf.tb_no_abbr_cnt && offset == 0) /* and not used for abbrev.s */
1046 typebuf.tb_no_abbr_cnt += addlen;
1047
1048 return OK;
1049}
1050
1051/*
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001052 * Put character "c" back into the typeahead buffer.
1053 * Can be used for a character obtained by vgetc() that needs to be put back.
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00001054 * Uses cmd_silent, KeyTyped and KeyNoremap to restore the flags belonging to
1055 * the char.
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001056 */
1057 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001058ins_char_typebuf(int c)
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001059{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001060 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001061 if (IS_SPECIAL(c))
1062 {
1063 buf[0] = K_SPECIAL;
1064 buf[1] = K_SECOND(c);
1065 buf[2] = K_THIRD(c);
1066 buf[3] = NUL;
1067 }
1068 else
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001069 buf[(*mb_char2bytes)(c, buf)] = NUL;
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +00001070 (void)ins_typebuf(buf, KeyNoremap, 0, !KeyTyped, cmd_silent);
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00001071}
1072
1073/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 * Return TRUE if the typeahead buffer was changed (while waiting for a
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001075 * character to arrive). Happens when a message was received from a client or
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001076 * from feedkeys().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 * But check in a more generic way to avoid trouble: When "typebuf.tb_buf"
1078 * changed it was reallocated and the old pointer can no longer be used.
1079 * Or "typebuf.tb_off" may have been changed and we would overwrite characters
1080 * that was just added.
1081 */
1082 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001083typebuf_changed(
1084 int tb_change_cnt) /* old value of typebuf.tb_change_cnt */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085{
1086 return (tb_change_cnt != 0 && (typebuf.tb_change_cnt != tb_change_cnt
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001087#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
1088 || typebuf_was_filled
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089#endif
1090 ));
1091}
1092
1093/*
1094 * Return TRUE if there are no characters in the typeahead buffer that have
1095 * not been typed (result from a mapping or come from ":normal").
1096 */
1097 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001098typebuf_typed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099{
1100 return typebuf.tb_maplen == 0;
1101}
1102
1103/*
1104 * Return the number of characters that are mapped (or not typed).
1105 */
1106 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001107typebuf_maplen(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108{
1109 return typebuf.tb_maplen;
1110}
1111
1112/*
1113 * remove "len" characters from typebuf.tb_buf[typebuf.tb_off + offset]
1114 */
1115 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001116del_typebuf(int len, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117{
1118 int i;
1119
1120 if (len == 0)
1121 return; /* nothing to do */
1122
1123 typebuf.tb_len -= len;
1124
1125 /*
1126 * Easy case: Just increase typebuf.tb_off.
1127 */
1128 if (offset == 0 && typebuf.tb_buflen - (typebuf.tb_off + len)
1129 >= 3 * MAXMAPLEN + 3)
1130 typebuf.tb_off += len;
1131 /*
1132 * Have to move the characters in typebuf.tb_buf[] and typebuf.tb_noremap[]
1133 */
1134 else
1135 {
1136 i = typebuf.tb_off + offset;
1137 /*
1138 * Leave some extra room at the end to avoid reallocation.
1139 */
1140 if (typebuf.tb_off > MAXMAPLEN)
1141 {
1142 mch_memmove(typebuf.tb_buf + MAXMAPLEN,
1143 typebuf.tb_buf + typebuf.tb_off, (size_t)offset);
1144 mch_memmove(typebuf.tb_noremap + MAXMAPLEN,
1145 typebuf.tb_noremap + typebuf.tb_off, (size_t)offset);
1146 typebuf.tb_off = MAXMAPLEN;
1147 }
1148 /* adjust typebuf.tb_buf (include the NUL at the end) */
1149 mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset,
1150 typebuf.tb_buf + i + len,
1151 (size_t)(typebuf.tb_len - offset + 1));
1152 /* adjust typebuf.tb_noremap[] */
1153 mch_memmove(typebuf.tb_noremap + typebuf.tb_off + offset,
1154 typebuf.tb_noremap + i + len,
1155 (size_t)(typebuf.tb_len - offset));
1156 }
1157
1158 if (typebuf.tb_maplen > offset) /* adjust tb_maplen */
1159 {
1160 if (typebuf.tb_maplen < offset + len)
1161 typebuf.tb_maplen = offset;
1162 else
1163 typebuf.tb_maplen -= len;
1164 }
1165 if (typebuf.tb_silent > offset) /* adjust tb_silent */
1166 {
1167 if (typebuf.tb_silent < offset + len)
1168 typebuf.tb_silent = offset;
1169 else
1170 typebuf.tb_silent -= len;
1171 }
1172 if (typebuf.tb_no_abbr_cnt > offset) /* adjust tb_no_abbr_cnt */
1173 {
1174 if (typebuf.tb_no_abbr_cnt < offset + len)
1175 typebuf.tb_no_abbr_cnt = offset;
1176 else
1177 typebuf.tb_no_abbr_cnt -= len;
1178 }
1179
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001180#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001181 /* Reset the flag that text received from a client or from feedkeys()
Bram Moolenaar4a85b412006-04-23 22:40:29 +00001182 * was inserted in the typeahead buffer. */
1183 typebuf_was_filled = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184#endif
1185 if (++typebuf.tb_change_cnt == 0)
1186 typebuf.tb_change_cnt = 1;
1187}
1188
1189/*
1190 * Write typed characters to script file.
1191 * If recording is on put the character in the recordbuffer.
1192 */
1193 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001194gotchars(char_u *chars, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195{
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001196 char_u *s = chars;
1197 int i;
1198 static char_u buf[4];
1199 static int buflen = 0;
1200 int todo = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201
Bram Moolenaar4e86cba2007-05-06 11:56:32 +00001202 while (todo--)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 {
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001204 buf[buflen++] = *s++;
1205
1206 // When receiving a special key sequence, store it until we have all
1207 // the bytes and we can decide what to do with it.
1208 if (buflen == 1 && buf[0] == K_SPECIAL)
1209 continue;
1210 if (buflen == 2)
1211 continue;
1212 if (buflen == 3 && buf[1] == KS_EXTRA
1213 && (buf[2] == KE_FOCUSGAINED || buf[2] == KE_FOCUSLOST))
1214 {
1215 // Drop K_FOCUSGAINED and K_FOCUSLOST, they are not useful in a
1216 // recording.
1217 buflen = 0;
1218 continue;
1219 }
1220
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 /* Handle one byte at a time; no translation to be done. */
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001222 for (i = 0; i < buflen; ++i)
1223 updatescript(buf[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001225 if (reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 {
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001227 buf[buflen] = NUL;
1228 add_buff(&recordbuff, buf, (long)buflen);
1229 /* remember how many chars were last recorded */
1230 last_recorded_len += buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 }
Bram Moolenaar972bfdd2018-07-03 14:48:15 +02001232 buflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 }
1234 may_sync_undo();
1235
1236#ifdef FEAT_EVAL
1237 /* output "debug mode" message next time in debug mode */
1238 debug_did_msg = FALSE;
1239#endif
1240
1241 /* Since characters have been typed, consider the following to be in
1242 * another mapping. Search string will be kept in history. */
1243 ++maptick;
1244}
1245
1246/*
1247 * Sync undo. Called when typed characters are obtained from the typeahead
1248 * buffer, or when a menu is used.
1249 * Do not sync:
1250 * - In Insert mode, unless cursor key has been used.
1251 * - While reading a script file.
1252 * - When no_u_sync is non-zero.
1253 */
1254 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001255may_sync_undo(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256{
1257 if ((!(State & (INSERT + CMDLINE)) || arrow_used)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001258 && scriptin[curscript] == NULL)
1259 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260}
1261
1262/*
1263 * Make "typebuf" empty and allocate new buffers.
1264 * Returns FAIL when out of memory.
1265 */
1266 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001267alloc_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268{
1269 typebuf.tb_buf = alloc(TYPELEN_INIT);
1270 typebuf.tb_noremap = alloc(TYPELEN_INIT);
1271 if (typebuf.tb_buf == NULL || typebuf.tb_noremap == NULL)
1272 {
1273 free_typebuf();
1274 return FAIL;
1275 }
1276 typebuf.tb_buflen = TYPELEN_INIT;
Bram Moolenaard34f9b12017-04-08 18:41:13 +02001277 typebuf.tb_off = MAXMAPLEN + 4; /* can insert without realloc */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 typebuf.tb_len = 0;
1279 typebuf.tb_maplen = 0;
1280 typebuf.tb_silent = 0;
1281 typebuf.tb_no_abbr_cnt = 0;
1282 if (++typebuf.tb_change_cnt == 0)
1283 typebuf.tb_change_cnt = 1;
1284 return OK;
1285}
1286
1287/*
1288 * Free the buffers of "typebuf".
1289 */
1290 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001291free_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292{
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001293 if (typebuf.tb_buf == typebuf_init)
Bram Moolenaar95f09602016-11-10 20:01:45 +01001294 internal_error("Free typebuf 1");
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001295 else
1296 vim_free(typebuf.tb_buf);
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00001297 if (typebuf.tb_noremap == noremapbuf_init)
Bram Moolenaar95f09602016-11-10 20:01:45 +01001298 internal_error("Free typebuf 2");
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001299 else
1300 vim_free(typebuf.tb_noremap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301}
1302
1303/*
1304 * When doing ":so! file", the current typeahead needs to be saved, and
1305 * restored when "file" has been read completely.
1306 */
1307static typebuf_T saved_typebuf[NSCRIPT];
1308
1309 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001310save_typebuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311{
1312 init_typebuf();
1313 saved_typebuf[curscript] = typebuf;
1314 /* If out of memory: restore typebuf and close file. */
1315 if (alloc_typebuf() == FAIL)
1316 {
1317 closescript();
1318 return FAIL;
1319 }
1320 return OK;
1321}
1322
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001323static int old_char = -1; /* character put back by vungetc() */
1324static int old_mod_mask; /* mod_mask for ungotten character */
Bram Moolenaarb8978712013-03-16 21:42:16 +01001325#ifdef FEAT_MOUSE
1326static int old_mouse_row; /* mouse_row related to old_char */
1327static int old_mouse_col; /* mouse_col related to old_char */
1328#endif
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001329
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330/*
1331 * Save all three kinds of typeahead, so that the user must type at a prompt.
1332 */
1333 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001334save_typeahead(tasave_T *tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335{
1336 tp->save_typebuf = typebuf;
1337 tp->typebuf_valid = (alloc_typebuf() == OK);
1338 if (!tp->typebuf_valid)
1339 typebuf = tp->save_typebuf;
1340
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001341 tp->old_char = old_char;
1342 tp->old_mod_mask = old_mod_mask;
1343 old_char = -1;
1344
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001345 tp->save_readbuf1 = readbuf1;
Bram Moolenaar285e3352018-04-18 23:01:13 +02001346 readbuf1.bh_first.b_next = NULL;
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001347 tp->save_readbuf2 = readbuf2;
Bram Moolenaar285e3352018-04-18 23:01:13 +02001348 readbuf2.bh_first.b_next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349# ifdef USE_INPUT_BUF
1350 tp->save_inputbuf = get_input_buf();
1351# endif
1352}
1353
1354/*
1355 * Restore the typeahead to what it was before calling save_typeahead().
1356 * The allocated memory is freed, can only be called once!
1357 */
1358 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001359restore_typeahead(tasave_T *tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360{
1361 if (tp->typebuf_valid)
1362 {
1363 free_typebuf();
1364 typebuf = tp->save_typebuf;
1365 }
1366
Bram Moolenaar13df0fe2009-07-09 16:24:19 +00001367 old_char = tp->old_char;
1368 old_mod_mask = tp->old_mod_mask;
1369
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001370 free_buff(&readbuf1);
1371 readbuf1 = tp->save_readbuf1;
1372 free_buff(&readbuf2);
1373 readbuf2 = tp->save_readbuf2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374# ifdef USE_INPUT_BUF
1375 set_input_buf(tp->save_inputbuf);
1376# endif
1377}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378
1379/*
1380 * Open a new script file for the ":source!" command.
1381 */
1382 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001383openscript(
1384 char_u *name,
1385 int directly) /* when TRUE execute directly */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386{
1387 if (curscript + 1 == NSCRIPT)
1388 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001389 emsg(_(e_nesting));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 return;
1391 }
Bram Moolenaar53575522019-05-22 22:38:25 +02001392
1393 // Disallow sourcing a file in the sandbox, the commands would be executed
1394 // later, possibly outside of the sandbox.
1395 if (check_secure())
1396 return;
1397
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00001398#ifdef FEAT_EVAL
1399 if (ignore_script)
1400 /* Not reading from script, also don't open one. Warning message? */
1401 return;
1402#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403
1404 if (scriptin[curscript] != NULL) /* already reading script */
1405 ++curscript;
1406 /* use NameBuff for expanded name */
1407 expand_env(name, NameBuff, MAXPATHL);
1408 if ((scriptin[curscript] = mch_fopen((char *)NameBuff, READBIN)) == NULL)
1409 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001410 semsg(_(e_notopen), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 if (curscript)
1412 --curscript;
1413 return;
1414 }
1415 if (save_typebuf() == FAIL)
1416 return;
1417
1418 /*
1419 * Execute the commands from the file right now when using ":source!"
1420 * after ":global" or ":argdo" or in a loop. Also when another command
1421 * follows. This means the display won't be updated. Don't do this
1422 * always, "make test" would fail.
1423 */
1424 if (directly)
1425 {
1426 oparg_T oa;
1427 int oldcurscript;
1428 int save_State = State;
1429 int save_restart_edit = restart_edit;
1430 int save_insertmode = p_im;
1431 int save_finish_op = finish_op;
1432 int save_msg_scroll = msg_scroll;
1433
1434 State = NORMAL;
1435 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
1436 restart_edit = 0; /* don't go to Insert mode */
1437 p_im = FALSE; /* don't use 'insertmode' */
1438 clear_oparg(&oa);
1439 finish_op = FALSE;
1440
1441 oldcurscript = curscript;
1442 do
1443 {
Bram Moolenaar386b43e2019-05-19 21:57:11 +02001444 update_topline_cursor(); // update cursor position and topline
1445 normal_cmd(&oa, FALSE); // execute one command
1446 vpeekc(); // check for end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 }
1448 while (scriptin[oldcurscript] != NULL);
1449
1450 State = save_State;
1451 msg_scroll = save_msg_scroll;
1452 restart_edit = save_restart_edit;
1453 p_im = save_insertmode;
1454 finish_op = save_finish_op;
1455 }
1456}
1457
1458/*
1459 * Close the currently active input script.
1460 */
1461 static void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001462closescript(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463{
1464 free_typebuf();
1465 typebuf = saved_typebuf[curscript];
1466
1467 fclose(scriptin[curscript]);
1468 scriptin[curscript] = NULL;
1469 if (curscript > 0)
1470 --curscript;
1471}
1472
Bram Moolenaarea408852005-06-25 22:49:46 +00001473#if defined(EXITFREE) || defined(PROTO)
1474 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001475close_all_scripts(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00001476{
1477 while (scriptin[0] != NULL)
1478 closescript();
1479}
1480#endif
1481
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1483/*
1484 * Return TRUE when reading keys from a script file.
1485 */
1486 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001487using_script(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488{
1489 return scriptin[curscript] != NULL;
1490}
1491#endif
1492
1493/*
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001494 * This function is called just before doing a blocking wait. Thus after
1495 * waiting 'updatetime' for a character to arrive.
1496 */
1497 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001498before_blocking(void)
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001499{
1500 updatescript(0);
1501#ifdef FEAT_EVAL
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001502 if (may_garbage_collect)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001503 garbage_collect(FALSE);
Bram Moolenaar238f4fa2005-06-27 22:25:50 +00001504#endif
1505}
1506
1507/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 * updatescipt() is called when a character can be written into the script file
1509 * or when we have waited some time for a character (c == 0)
1510 *
1511 * All the changed memfiles are synced if c == 0 or when the number of typed
1512 * characters reaches 'updatecount' and 'updatecount' is non-zero.
1513 */
1514 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001515updatescript(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516{
1517 static int count = 0;
1518
1519 if (c && scriptout)
1520 putc(c, scriptout);
1521 if (c == 0 || (p_uc > 0 && ++count >= p_uc))
1522 {
1523 ml_sync_all(c == 0, TRUE);
1524 count = 0;
1525 }
1526}
1527
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528/*
1529 * Get the next input character.
1530 * Can return a special key or a multi-byte character.
1531 * Can return NUL when called recursively, use safe_vgetc() if that's not
1532 * wanted.
1533 * This translates escaped K_SPECIAL and CSI bytes to a K_SPECIAL or CSI byte.
1534 * Collects the bytes of a multibyte character into the whole character.
Bram Moolenaarf6f95d92009-11-11 15:23:37 +00001535 * Returns the modifiers in the global "mod_mask".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 */
1537 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001538vgetc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539{
1540 int c, c2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02001542 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001545#ifdef FEAT_EVAL
1546 /* Do garbage collection when garbagecollect() was called previously and
1547 * we are now at the toplevel. */
1548 if (may_garbage_collect && want_garbage_collect)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001549 garbage_collect(FALSE);
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001550#endif
1551
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 /*
1553 * If a character was put back with vungetc, it was already processed.
1554 * Return it directly.
1555 */
1556 if (old_char != -1)
1557 {
1558 c = old_char;
1559 old_char = -1;
1560 mod_mask = old_mod_mask;
Bram Moolenaarb8978712013-03-16 21:42:16 +01001561#ifdef FEAT_MOUSE
1562 mouse_row = old_mouse_row;
1563 mouse_col = old_mouse_col;
1564#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001566 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 {
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001568 mod_mask = 0x0;
1569 last_recorded_len = 0;
1570 for (;;) // this is done twice if there are modifiers
1571 {
1572 int did_inc = FALSE;
Bram Moolenaar2455c4e2015-09-15 18:29:39 +02001573
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001574 if (mod_mask
Bram Moolenaar39719052017-09-05 22:20:46 +02001575#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001576 || im_is_preediting()
Bram Moolenaar39719052017-09-05 22:20:46 +02001577#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001578 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 {
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001580 // no mapping after modifier has been read
1581 ++no_mapping;
1582 ++allow_keys;
1583 did_inc = TRUE; // mod_mask may change value
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 }
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001585 c = vgetorpeek(TRUE);
1586 if (did_inc)
1587 {
1588 --no_mapping;
1589 --allow_keys;
1590 }
1591
1592 // Get two extra bytes for special keys
1593 if (c == K_SPECIAL
1594#ifdef FEAT_GUI
Bram Moolenaara9dd2d32019-04-29 21:58:41 +02001595 || (gui.in_use && c == CSI)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001596#endif
1597 )
1598 {
1599 int save_allow_keys = allow_keys;
1600
1601 ++no_mapping;
1602 allow_keys = 0; // make sure BS is not found
1603 c2 = vgetorpeek(TRUE); // no mapping for these chars
1604 c = vgetorpeek(TRUE);
1605 --no_mapping;
1606 allow_keys = save_allow_keys;
1607 if (c2 == KS_MODIFIER)
1608 {
1609 mod_mask = c;
1610 continue;
1611 }
1612 c = TO_SPECIAL(c2, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613
Bram Moolenaar4f974752019-02-17 17:44:42 +01001614#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001615 // Handle K_TEAROFF here, the caller of vgetc() doesn't need to
1616 // know that a menu was torn off
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001617 if (
1618# ifdef VIMDLL
1619 gui.in_use &&
1620# endif
1621 c == K_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 {
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001623 char_u name[200];
1624 int i;
1625
1626 // get menu path, it ends with a <CR>
1627 for (i = 0; (c = vgetorpeek(TRUE)) != '\r'; )
1628 {
1629 name[i] = c;
1630 if (i < 199)
1631 ++i;
1632 }
1633 name[i] = NUL;
1634 gui_make_tearoff(name);
1635 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637#endif
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02001638#if defined(FEAT_GUI) && defined(FEAT_GUI_GTK) && defined(FEAT_MENU)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001639 // GTK: <F10> normally selects the menu, but it's passed until
1640 // here to allow mapping it. Intercept and invoke the GTK
1641 // behavior if it's not mapped.
1642 if (c == K_F10 && gui.menubar != NULL)
1643 {
1644 gtk_menu_shell_select_first(
1645 GTK_MENU_SHELL(gui.menubar), FALSE);
1646 continue;
1647 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001648#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649#ifdef FEAT_GUI
Bram Moolenaara9dd2d32019-04-29 21:58:41 +02001650 if (gui.in_use)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001651 {
Bram Moolenaara9dd2d32019-04-29 21:58:41 +02001652 // Handle focus event here, so that the caller doesn't
1653 // need to know about it. Return K_IGNORE so that we loop
1654 // once (needed if 'lazyredraw' is set).
1655 if (c == K_FOCUSGAINED || c == K_FOCUSLOST)
1656 {
1657 ui_focus_change(c == K_FOCUSGAINED);
1658 c = K_IGNORE;
1659 }
Bram Moolenaar9c8791f2007-09-05 19:47:23 +00001660
Bram Moolenaara9dd2d32019-04-29 21:58:41 +02001661 // Translate K_CSI to CSI. The special key is only used
1662 // to avoid it being recognized as the start of a special
1663 // key.
1664 if (c == K_CSI)
1665 c = CSI;
1666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001668 }
1669 // a keypad or special function key was not mapped, use it like
1670 // its ASCII equivalent
1671 switch (c)
1672 {
1673 case K_KPLUS: c = '+'; break;
1674 case K_KMINUS: c = '-'; break;
1675 case K_KDIVIDE: c = '/'; break;
1676 case K_KMULTIPLY: c = '*'; break;
1677 case K_KENTER: c = CAR; break;
1678 case K_KPOINT:
Bram Moolenaar4f974752019-02-17 17:44:42 +01001679#ifdef MSWIN
Bram Moolenaar8e85db02018-07-27 23:16:51 +02001680 // Can be either '.' or a ',',
1681 // depending on the type of keypad.
1682 c = MapVirtualKey(VK_DECIMAL, 2); break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001683#else
Bram Moolenaar8e85db02018-07-27 23:16:51 +02001684 c = '.'; break;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001685#endif
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001686 case K_K0: c = '0'; break;
1687 case K_K1: c = '1'; break;
1688 case K_K2: c = '2'; break;
1689 case K_K3: c = '3'; break;
1690 case K_K4: c = '4'; break;
1691 case K_K5: c = '5'; break;
1692 case K_K6: c = '6'; break;
1693 case K_K7: c = '7'; break;
1694 case K_K8: c = '8'; break;
1695 case K_K9: c = '9'; break;
Bram Moolenaara88d9682005-03-25 21:45:43 +00001696
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001697 case K_XHOME:
1698 case K_ZHOME: if (mod_mask == MOD_MASK_SHIFT)
Bram Moolenaara88d9682005-03-25 21:45:43 +00001699 {
1700 c = K_S_HOME;
1701 mod_mask = 0;
1702 }
1703 else if (mod_mask == MOD_MASK_CTRL)
1704 {
1705 c = K_C_HOME;
1706 mod_mask = 0;
1707 }
1708 else
1709 c = K_HOME;
1710 break;
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001711 case K_XEND:
1712 case K_ZEND: if (mod_mask == MOD_MASK_SHIFT)
Bram Moolenaara88d9682005-03-25 21:45:43 +00001713 {
1714 c = K_S_END;
1715 mod_mask = 0;
1716 }
1717 else if (mod_mask == MOD_MASK_CTRL)
1718 {
1719 c = K_C_END;
1720 mod_mask = 0;
1721 }
1722 else
1723 c = K_END;
1724 break;
1725
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001726 case K_XUP: c = K_UP; break;
1727 case K_XDOWN: c = K_DOWN; break;
1728 case K_XLEFT: c = K_LEFT; break;
1729 case K_XRIGHT: c = K_RIGHT; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001732 // For a multi-byte character get all the bytes and return the
1733 // converted character.
1734 // Note: This will loop until enough bytes are received!
1735 if (has_mbyte && (n = MB_BYTE2LEN_CHECK(c)) > 1)
1736 {
1737 ++no_mapping;
1738 buf[0] = c;
1739 for (i = 1; i < n; ++i)
1740 {
1741 buf[i] = vgetorpeek(TRUE);
1742 if (buf[i] == K_SPECIAL
1743#ifdef FEAT_GUI
Bram Moolenaar386b43e2019-05-19 21:57:11 +02001744 || (
1745# ifdef VIMDLL
1746 gui.in_use &&
1747# endif
1748 buf[i] == CSI)
Bram Moolenaarfd731b02019-03-09 11:23:58 +01001749#endif
1750 )
1751 {
1752 // Must be a K_SPECIAL - KS_SPECIAL - KE_FILLER
1753 // sequence, which represents a K_SPECIAL (0x80),
1754 // or a CSI - KS_EXTRA - KE_CSI sequence, which
1755 // represents a CSI (0x9B),
1756 // or a K_SPECIAL - KS_EXTRA - KE_CSI, which is CSI
1757 // too.
1758 c = vgetorpeek(TRUE);
1759 if (vgetorpeek(TRUE) == (int)KE_CSI && c == KS_EXTRA)
1760 buf[i] = CSI;
1761 }
1762 }
1763 --no_mapping;
1764 c = (*mb_ptr2char)(buf);
1765 }
1766
1767 break;
1768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001770
1771#ifdef FEAT_EVAL
1772 /*
1773 * In the main loop "may_garbage_collect" can be set to do garbage
1774 * collection in the first next vgetc(). It's disabled after that to
1775 * avoid internally used Lists and Dicts to be freed.
1776 */
1777 may_garbage_collect = FALSE;
1778#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001779#ifdef FEAT_BEVAL_TERM
Bram Moolenaarc2f50542019-07-05 23:24:56 +02001780 if (c != K_MOUSEMOVE && c != K_IGNORE && c != K_CURSORHOLD)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001781 {
1782 /* Don't trigger 'balloonexpr' unless only the mouse was moved. */
1783 bevalexpr_due_set = FALSE;
1784 ui_remove_balloon();
1785 }
1786#endif
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001787#ifdef FEAT_TEXT_PROP
1788 if (popup_do_filter(c))
1789 c = K_IGNORE;
1790#endif
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001791
1792 return c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793}
1794
1795/*
1796 * Like vgetc(), but never return a NUL when called recursively, get a key
1797 * directly from the user (ignoring typeahead).
1798 */
1799 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001800safe_vgetc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801{
1802 int c;
1803
1804 c = vgetc();
1805 if (c == NUL)
1806 c = get_keystroke();
1807 return c;
1808}
1809
1810/*
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001811 * Like safe_vgetc(), but loop to handle K_IGNORE.
1812 * Also ignore scrollbar events.
1813 */
1814 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001815plain_vgetc(void)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001816{
1817 int c;
1818
1819 do
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001820 c = safe_vgetc();
Bram Moolenaarabab0b02019-03-30 18:47:01 +01001821 while (c == K_IGNORE || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR);
Bram Moolenaarec2da362017-01-21 20:04:22 +01001822
1823 if (c == K_PS)
1824 /* Only handle the first pasted character. Drop the rest, since we
1825 * don't know what to do with it. */
1826 c = bracketed_paste(PASTE_ONE_CHAR, FALSE, NULL);
1827
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001828 return c;
1829}
1830
1831/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 * Check if a character is available, such that vgetc() will not block.
1833 * If the next character is a special character or multi-byte, the returned
1834 * character is not valid!.
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02001835 * Returns NUL if no character is available.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 */
1837 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001838vpeekc(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839{
1840 if (old_char != -1)
1841 return old_char;
1842 return vgetorpeek(FALSE);
1843}
1844
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01001845#if defined(FEAT_TERMRESPONSE) || defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846/*
1847 * Like vpeekc(), but don't allow mapping. Do allow checking for terminal
1848 * codes.
1849 */
1850 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001851vpeekc_nomap(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852{
1853 int c;
1854
1855 ++no_mapping;
1856 ++allow_keys;
1857 c = vpeekc();
1858 --no_mapping;
1859 --allow_keys;
1860 return c;
1861}
1862#endif
1863
Bram Moolenaar9a665ba2014-05-22 18:59:58 +02001864#if defined(FEAT_INS_EXPAND) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865/*
1866 * Check if any character is available, also half an escape sequence.
1867 * Trick: when no typeahead found, but there is something in the typeahead
1868 * buffer, it must be an ESC that is recognized as the start of a key code.
1869 */
1870 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001871vpeekc_any(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872{
1873 int c;
1874
1875 c = vpeekc();
1876 if (c == NUL && typebuf.tb_len > 0)
1877 c = ESC;
1878 return c;
1879}
1880#endif
1881
1882/*
1883 * Call vpeekc() without causing anything to be mapped.
1884 * Return TRUE if a character is available, FALSE otherwise.
1885 */
1886 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01001887char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888{
1889 int retval;
1890
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01001891#ifdef FEAT_EVAL
Bram Moolenaard0573012017-10-28 21:11:06 +02001892 /* When test_override("char_avail", 1) was called pretend there is no
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01001893 * typeahead. */
1894 if (disable_char_avail_for_testing)
1895 return FALSE;
1896#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 ++no_mapping;
1898 retval = vpeekc();
1899 --no_mapping;
1900 return (retval != NUL);
1901}
1902
Bram Moolenaardd000352019-08-02 21:35:33 +02001903typedef enum {
1904 map_result_fail, // failed, break loop
1905 map_result_get, // get a character from typeahead
1906 map_result_retry, // try to map again
1907 map_result_nomatch // no matching mapping, get char
1908} map_result_T;
1909
1910/*
1911 * Handle mappings in the typeahead buffer.
1912 * - When something was mapped, return map_result_retry for recursive mappings.
1913 * - When nothing mapped and typeahead has a character return map_result_get.
1914 * - When there is no match yet, return map_result_nomatch, need to get more
1915 * typeahead.
1916 */
1917 static int
1918handle_mapping(
1919 int *keylenp,
1920 int *timedout,
1921 int *mapdepth)
1922{
1923 mapblock_T *mp = NULL;
1924 mapblock_T *mp2;
1925 mapblock_T *mp_match;
1926 int mp_match_len = 0;
1927 int max_mlen = 0;
1928 int tb_c1;
1929 int mlen;
1930#ifdef FEAT_LANGMAP
1931 int nolmaplen;
1932#endif
1933 int keylen;
1934 int i;
1935 int local_State = get_real_state();
1936
1937 /*
1938 * Check for a mappable key sequence.
1939 * Walk through one maphash[] list until we find an
1940 * entry that matches.
1941 *
1942 * Don't look for mappings if:
1943 * - no_mapping set: mapping disabled (e.g. for CTRL-V)
1944 * - maphash_valid not set: no mappings present.
1945 * - typebuf.tb_buf[typebuf.tb_off] should not be remapped
1946 * - in insert or cmdline mode and 'paste' option set
1947 * - waiting for "hit return to continue" and CR or SPACE
1948 * typed
1949 * - waiting for a char with --more--
1950 * - in Ctrl-X mode, and we get a valid char for that mode
1951 */
1952 tb_c1 = typebuf.tb_buf[typebuf.tb_off];
1953 if (no_mapping == 0 && is_maphash_valid()
1954 && (no_zero_mapping == 0 || tb_c1 != '0')
1955 && (typebuf.tb_maplen == 0
1956 || (p_remap
1957 && (typebuf.tb_noremap[typebuf.tb_off]
1958 & (RM_NONE|RM_ABBR)) == 0))
1959 && !(p_paste && (State & (INSERT + CMDLINE)))
1960 && !(State == HITRETURN && (tb_c1 == CAR || tb_c1 == ' '))
1961 && State != ASKMORE
1962 && State != CONFIRM
1963#ifdef FEAT_INS_EXPAND
1964 && !((ctrl_x_mode_not_default()
1965 && vim_is_ctrl_x_key(tb_c1))
1966 || ((compl_cont_status & CONT_LOCAL)
1967 && (tb_c1 == Ctrl_N || tb_c1 == Ctrl_P)))
1968#endif
1969 )
1970 {
1971#ifdef FEAT_LANGMAP
1972 if (tb_c1 == K_SPECIAL)
1973 nolmaplen = 2;
1974 else
1975 {
1976 LANGMAP_ADJUST(tb_c1,
1977 (State & (CMDLINE | INSERT)) == 0
1978 && get_real_state() != SELECTMODE);
1979 nolmaplen = 0;
1980 }
1981#endif
1982 // First try buffer-local mappings.
1983 mp = get_buf_maphash_list(local_State, tb_c1);
1984 mp2 = get_maphash_list(local_State, tb_c1);
1985 if (mp == NULL)
1986 {
1987 // There are no buffer-local mappings.
1988 mp = mp2;
1989 mp2 = NULL;
1990 }
1991 /*
1992 * Loop until a partly matching mapping is found or
1993 * all (local) mappings have been checked.
1994 * The longest full match is remembered in "mp_match".
1995 * A full match is only accepted if there is no partly
1996 * match, so "aa" and "aaa" can both be mapped.
1997 */
1998 mp_match = NULL;
1999 mp_match_len = 0;
2000 for ( ; mp != NULL;
2001 mp->m_next == NULL ? (mp = mp2, mp2 = NULL)
2002 : (mp = mp->m_next))
2003 {
2004 // Only consider an entry if the first character
2005 // matches and it is for the current state.
2006 // Skip ":lmap" mappings if keys were mapped.
2007 if (mp->m_keys[0] == tb_c1
2008 && (mp->m_mode & local_State)
2009 && ((mp->m_mode & LANGMAP) == 0
2010 || typebuf.tb_maplen == 0))
2011 {
2012#ifdef FEAT_LANGMAP
2013 int nomap = nolmaplen;
2014 int c2;
2015#endif
2016 // find the match length of this mapping
2017 for (mlen = 1; mlen < typebuf.tb_len; ++mlen)
2018 {
2019#ifdef FEAT_LANGMAP
2020 c2 = typebuf.tb_buf[typebuf.tb_off + mlen];
2021 if (nomap > 0)
2022 --nomap;
2023 else if (c2 == K_SPECIAL)
2024 nomap = 2;
2025 else
2026 LANGMAP_ADJUST(c2, TRUE);
2027 if (mp->m_keys[mlen] != c2)
2028#else
2029 if (mp->m_keys[mlen] !=
2030 typebuf.tb_buf[typebuf.tb_off + mlen])
2031#endif
2032 break;
2033 }
2034
2035 // Don't allow mapping the first byte(s) of a
2036 // multi-byte char. Happens when mapping
2037 // <M-a> and then changing 'encoding'. Beware
2038 // that 0x80 is escaped.
2039 {
2040 char_u *p1 = mp->m_keys;
2041 char_u *p2 = mb_unescape(&p1);
2042
2043 if (has_mbyte && p2 != NULL
2044 && MB_BYTE2LEN(tb_c1) > MB_PTR2LEN(p2))
2045 mlen = 0;
2046 }
2047
2048 // Check an entry whether it matches.
2049 // - Full match: mlen == keylen
2050 // - Partly match: mlen == typebuf.tb_len
2051 keylen = mp->m_keylen;
2052 if (mlen == keylen
2053 || (mlen == typebuf.tb_len
2054 && typebuf.tb_len < keylen))
2055 {
2056 char_u *s;
2057 int n;
2058
2059 // If only script-local mappings are
2060 // allowed, check if the mapping starts
2061 // with K_SNR.
2062 s = typebuf.tb_noremap + typebuf.tb_off;
2063 if (*s == RM_SCRIPT
2064 && (mp->m_keys[0] != K_SPECIAL
2065 || mp->m_keys[1] != KS_EXTRA
2066 || mp->m_keys[2]
2067 != (int)KE_SNR))
2068 continue;
2069
2070 // If one of the typed keys cannot be
2071 // remapped, skip the entry.
2072 for (n = mlen; --n >= 0; )
2073 if (*s++ & (RM_NONE|RM_ABBR))
2074 break;
2075 if (n >= 0)
2076 continue;
2077
2078 if (keylen > typebuf.tb_len)
2079 {
2080 if (!*timedout && !(mp_match != NULL
2081 && mp_match->m_nowait))
2082 {
2083 // break at a partly match
2084 keylen = KEYLEN_PART_MAP;
2085 break;
2086 }
2087 }
2088 else if (keylen > mp_match_len)
2089 {
2090 // found a longer match
2091 mp_match = mp;
2092 mp_match_len = keylen;
2093 }
2094 }
2095 else
2096 // No match; may have to check for
2097 // termcode at next character.
2098 if (max_mlen < mlen)
2099 max_mlen = mlen;
2100 }
2101 }
2102
2103 // If no partly match found, use the longest full
2104 // match.
2105 if (keylen != KEYLEN_PART_MAP)
2106 {
2107 mp = mp_match;
2108 keylen = mp_match_len;
2109 }
2110 }
2111
2112 /*
2113 * Check for match with 'pastetoggle'
2114 */
2115 if (*p_pt != NUL && mp == NULL && (State & (INSERT|NORMAL)))
2116 {
2117 for (mlen = 0; mlen < typebuf.tb_len && p_pt[mlen];
2118 ++mlen)
2119 if (p_pt[mlen] != typebuf.tb_buf[typebuf.tb_off
2120 + mlen])
2121 break;
2122 if (p_pt[mlen] == NUL) // match
2123 {
2124 // write chars to script file(s)
2125 if (mlen > typebuf.tb_maplen)
2126 gotchars(typebuf.tb_buf + typebuf.tb_off
2127 + typebuf.tb_maplen,
2128 mlen - typebuf.tb_maplen);
2129
2130 del_typebuf(mlen, 0); // remove the chars
2131 set_option_value((char_u *)"paste",
2132 (long)!p_paste, NULL, 0);
2133 if (!(State & INSERT))
2134 {
2135 msg_col = 0;
2136 msg_row = Rows - 1;
2137 msg_clr_eos(); // clear ruler
2138 }
2139 status_redraw_all();
2140 redraw_statuslines();
2141 showmode();
2142 setcursor();
2143 *keylenp = keylen;
2144 return map_result_retry;
2145 }
2146 // Need more chars for partly match.
2147 if (mlen == typebuf.tb_len)
2148 keylen = KEYLEN_PART_KEY;
2149 else if (max_mlen < mlen)
2150 // no match, may have to check for termcode at
2151 // next character
2152 max_mlen = mlen + 1;
2153 }
2154
2155 if ((mp == NULL || max_mlen >= mp_match_len)
2156 && keylen != KEYLEN_PART_MAP)
2157 {
2158 int save_keylen = keylen;
2159
2160 /*
2161 * When no matching mapping found or found a
2162 * non-matching mapping that matches at least what the
2163 * matching mapping matched:
2164 * Check if we have a terminal code, when:
2165 * mapping is allowed,
2166 * keys have not been mapped,
2167 * and not an ESC sequence, not in insert mode or
2168 * p_ek is on,
2169 * and when not timed out,
2170 */
2171 if ((no_mapping == 0 || allow_keys != 0)
2172 && (typebuf.tb_maplen == 0
2173 || (p_remap && typebuf.tb_noremap[
2174 typebuf.tb_off] == RM_YES))
2175 && !*timedout)
2176 {
2177 keylen = check_termcode(max_mlen + 1,
2178 NULL, 0, NULL);
2179
2180 // If no termcode matched but 'pastetoggle'
2181 // matched partially it's like an incomplete key
2182 // sequence.
2183 if (keylen == 0 && save_keylen == KEYLEN_PART_KEY)
2184 keylen = KEYLEN_PART_KEY;
2185
2186 // When getting a partial match, but the last
2187 // characters were not typed, don't wait for a
2188 // typed character to complete the termcode.
2189 // This helps a lot when a ":normal" command ends
2190 // in an ESC.
2191 if (keylen < 0
2192 && typebuf.tb_len == typebuf.tb_maplen)
2193 keylen = 0;
2194 }
2195 else
2196 keylen = 0;
2197 if (keylen == 0) // no matching terminal code
2198 {
2199#ifdef AMIGA // check for window bounds report
2200 if (typebuf.tb_maplen == 0 && (typebuf.tb_buf[
2201 typebuf.tb_off] & 0xff) == CSI)
2202 {
2203 char_u *s;
2204
2205 for (s = typebuf.tb_buf + typebuf.tb_off + 1;
2206 s < typebuf.tb_buf + typebuf.tb_off
2207 + typebuf.tb_len
2208 && (VIM_ISDIGIT(*s) || *s == ';'
2209 || *s == ' ');
2210 ++s)
2211 ;
2212 if (*s == 'r' || *s == '|') // found one
2213 {
2214 del_typebuf((int)(s + 1 -
2215 (typebuf.tb_buf + typebuf.tb_off)), 0);
2216 // get size and redraw screen
2217 shell_resized();
2218 *keylenp = keylen;
2219 return map_result_retry;
2220 }
2221 if (*s == NUL) // need more characters
2222 keylen = KEYLEN_PART_KEY;
2223 }
2224 if (keylen >= 0)
2225#endif
2226 // When there was a matching mapping and no
2227 // termcode could be replaced after another one,
2228 // use that mapping (loop around). If there was
2229 // no mapping at all use the character from the
2230 // typeahead buffer right here.
2231 if (mp == NULL)
2232 {
2233 *keylenp = keylen;
2234 return map_result_get; // got character, break for loop
2235 }
2236 }
2237
2238 if (keylen > 0) // full matching terminal code
2239 {
2240#if defined(FEAT_GUI) && defined(FEAT_MENU)
2241 if (typebuf.tb_len >= 2
2242 && typebuf.tb_buf[typebuf.tb_off] == K_SPECIAL
2243 && typebuf.tb_buf[typebuf.tb_off + 1]
2244 == KS_MENU)
2245 {
2246 int idx;
2247
2248 // Using a menu may cause a break in undo!
2249 // It's like using gotchars(), but without
2250 // recording or writing to a script file.
2251 may_sync_undo();
2252 del_typebuf(3, 0);
2253 idx = get_menu_index(current_menu, local_State);
2254 if (idx != MENU_INDEX_INVALID)
2255 {
2256 // In Select mode and a Visual mode menu
2257 // is used: Switch to Visual mode
2258 // temporarily. Append K_SELECT to switch
2259 // back to Select mode.
2260 if (VIsual_active && VIsual_select
2261 && (current_menu->modes & VISUAL))
2262 {
2263 VIsual_select = FALSE;
2264 (void)ins_typebuf(K_SELECT_STRING,
2265 REMAP_NONE, 0, TRUE, FALSE);
2266 }
2267 ins_typebuf(current_menu->strings[idx],
2268 current_menu->noremap[idx],
2269 0, TRUE,
2270 current_menu->silent[idx]);
2271 }
2272 }
2273#endif // FEAT_GUI && FEAT_MENU
2274 *keylenp = keylen;
2275 return map_result_retry; // try mapping again
2276 }
2277
2278 // Partial match: get some more characters. When a
2279 // matching mapping was found use that one.
2280 if (mp == NULL || keylen < 0)
2281 keylen = KEYLEN_PART_KEY;
2282 else
2283 keylen = mp_match_len;
2284 }
2285
2286 /*
2287 * complete match
2288 */
2289 if (keylen >= 0 && keylen <= typebuf.tb_len)
2290 {
2291 char_u *map_str;
2292
2293#ifdef FEAT_EVAL
2294 int save_m_expr;
2295 int save_m_noremap;
2296 int save_m_silent;
2297 char_u *save_m_keys;
2298 char_u *save_m_str;
2299#else
2300# define save_m_noremap mp->m_noremap
2301# define save_m_silent mp->m_silent
2302#endif
2303
2304 // write chars to script file(s)
2305 if (keylen > typebuf.tb_maplen)
2306 gotchars(typebuf.tb_buf + typebuf.tb_off
2307 + typebuf.tb_maplen,
2308 keylen - typebuf.tb_maplen);
2309
2310 cmd_silent = (typebuf.tb_silent > 0);
2311 del_typebuf(keylen, 0); // remove the mapped keys
2312
2313 /*
2314 * Put the replacement string in front of mapstr.
2315 * The depth check catches ":map x y" and ":map y x".
2316 */
2317 if (++*mapdepth >= p_mmd)
2318 {
2319 emsg(_("E223: recursive mapping"));
2320 if (State & CMDLINE)
2321 redrawcmdline();
2322 else
2323 setcursor();
2324 flush_buffers(FLUSH_MINIMAL);
2325 *mapdepth = 0; /* for next one */
2326 *keylenp = keylen;
2327 return map_result_fail;
2328 }
2329
2330 /*
2331 * In Select mode and a Visual mode mapping is used:
2332 * Switch to Visual mode temporarily. Append K_SELECT
2333 * to switch back to Select mode.
2334 */
2335 if (VIsual_active && VIsual_select
2336 && (mp->m_mode & VISUAL))
2337 {
2338 VIsual_select = FALSE;
2339 (void)ins_typebuf(K_SELECT_STRING, REMAP_NONE,
2340 0, TRUE, FALSE);
2341 }
2342
2343#ifdef FEAT_EVAL
2344 // Copy the values from *mp that are used, because
2345 // evaluating the expression may invoke a function
2346 // that redefines the mapping, thereby making *mp
2347 // invalid.
2348 save_m_expr = mp->m_expr;
2349 save_m_noremap = mp->m_noremap;
2350 save_m_silent = mp->m_silent;
2351 save_m_keys = NULL; // only saved when needed
2352 save_m_str = NULL; // only saved when needed
2353
2354 /*
2355 * Handle ":map <expr>": evaluate the {rhs} as an
2356 * expression. Also save and restore the command line
2357 * for "normal :".
2358 */
2359 if (mp->m_expr)
2360 {
2361 int save_vgetc_busy = vgetc_busy;
2362 int save_may_garbage_collect = may_garbage_collect;
2363
2364 vgetc_busy = 0;
2365 may_garbage_collect = FALSE;
2366
2367 save_m_keys = vim_strsave(mp->m_keys);
2368 save_m_str = vim_strsave(mp->m_str);
2369 map_str = eval_map_expr(save_m_str, NUL);
2370
2371 vgetc_busy = save_vgetc_busy;
2372 may_garbage_collect = save_may_garbage_collect;
2373 }
2374 else
2375#endif
2376 map_str = mp->m_str;
2377
2378 /*
2379 * Insert the 'to' part in the typebuf.tb_buf.
2380 * If 'from' field is the same as the start of the
2381 * 'to' field, don't remap the first character (but do
2382 * allow abbreviations).
2383 * If m_noremap is set, don't remap the whole 'to'
2384 * part.
2385 */
2386 if (map_str == NULL)
2387 i = FAIL;
2388 else
2389 {
2390 int noremap;
2391
2392 if (save_m_noremap != REMAP_YES)
2393 noremap = save_m_noremap;
2394 else if (
2395#ifdef FEAT_EVAL
2396 STRNCMP(map_str, save_m_keys != NULL
2397 ? save_m_keys : mp->m_keys,
2398 (size_t)keylen)
2399#else
2400 STRNCMP(map_str, mp->m_keys, (size_t)keylen)
2401#endif
2402 != 0)
2403 noremap = REMAP_YES;
2404 else
2405 noremap = REMAP_SKIP;
2406 i = ins_typebuf(map_str, noremap,
2407 0, TRUE, cmd_silent || save_m_silent);
2408#ifdef FEAT_EVAL
2409 if (save_m_expr)
2410 vim_free(map_str);
2411#endif
2412 }
2413#ifdef FEAT_EVAL
2414 vim_free(save_m_keys);
2415 vim_free(save_m_str);
2416#endif
2417 *keylenp = keylen;
2418 if (i == FAIL)
2419 return map_result_fail;
2420 return map_result_retry;
2421 }
2422
2423 *keylenp = keylen;
2424 return map_result_nomatch;
2425}
2426
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002427/*
2428 * unget one character (can only be done once!)
2429 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002431vungetc(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432{
2433 old_char = c;
2434 old_mod_mask = mod_mask;
Bram Moolenaarb8978712013-03-16 21:42:16 +01002435#ifdef FEAT_MOUSE
2436 old_mouse_row = mouse_row;
2437 old_mouse_col = mouse_col;
2438#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439}
2440
2441/*
Bram Moolenaar32526b32019-01-19 17:43:09 +01002442 * Get a byte:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 * 1. from the stuffbuffer
2444 * This is used for abbreviated commands like "D" -> "d$".
2445 * Also used to redo a command for ".".
2446 * 2. from the typeahead buffer
2447 * Stores text obtained previously but not used yet.
2448 * Also stores the result of mappings.
2449 * Also used for the ":normal" command.
2450 * 3. from the user
2451 * This may do a blocking wait if "advance" is TRUE.
2452 *
2453 * if "advance" is TRUE (vgetc()):
Bram Moolenaard29459b2016-08-26 22:29:11 +02002454 * Really get the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 * KeyTyped is set to TRUE in the case the user typed the key.
2456 * KeyStuffed is TRUE if the character comes from the stuff buffer.
2457 * if "advance" is FALSE (vpeekc()):
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02002458 * Just look whether there is a character available.
2459 * Return NUL if not.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 *
2461 * When "no_mapping" is zero, checks for mappings in the current mode.
2462 * Only returns one byte (of a multi-byte character).
2463 * K_SPECIAL and CSI may be escaped, need to get two more bytes then.
2464 */
2465 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002466vgetorpeek(int advance)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467{
2468 int c, c1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 int timedout = FALSE; /* waited for more than 1 second
2470 for mapping to complete */
2471 int mapdepth = 0; /* check for recursive mapping */
2472 int mode_deleted = FALSE; /* set when mode has been deleted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 int i;
Bram Moolenaar4e427192006-03-10 21:34:27 +00002474#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 int new_wcol, new_wrow;
2476#endif
2477#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 int shape_changed = FALSE; /* adjusted cursor shape */
2479#endif
2480 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 int old_wcol, old_wrow;
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00002482 int wait_tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483
2484 /*
2485 * This function doesn't work very well when called recursively. This may
2486 * happen though, because of:
2487 * 1. The call to add_to_showcmd(). char_avail() is then used to check if
2488 * there is a character available, which calls this function. In that
2489 * case we must return NUL, to indicate no character is available.
2490 * 2. A GUI callback function writes to the screen, causing a
2491 * wait_return().
2492 * Using ":normal" can also do this, but it saves the typeahead buffer,
2493 * thus it should be OK. But don't get a key from the user then.
2494 */
Bram Moolenaare2c38102016-01-31 14:55:40 +01002495 if (vgetc_busy > 0 && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 return NUL;
2497
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002498 ++vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499
2500 if (advance)
2501 KeyStuffed = FALSE;
2502
2503 init_typebuf();
2504 start_stuff();
2505 if (advance && typebuf.tb_maplen == 0)
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02002506 reg_executing = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 do
2508 {
2509/*
2510 * get a character: 1. from the stuffbuffer
2511 */
2512 if (typeahead_char != 0)
2513 {
2514 c = typeahead_char;
2515 if (advance)
2516 typeahead_char = 0;
2517 }
2518 else
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01002519 c = read_readbuffers(advance);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 if (c != NUL && !got_int)
2521 {
2522 if (advance)
2523 {
2524 /* KeyTyped = FALSE; When the command that stuffed something
2525 * was typed, behave like the stuffed command was typed.
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01002526 * needed for CTRL-W CTRL-] to open a fold, for example. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 KeyStuffed = TRUE;
2528 }
2529 if (typebuf.tb_no_abbr_cnt == 0)
2530 typebuf.tb_no_abbr_cnt = 1; /* no abbreviations now */
2531 }
2532 else
2533 {
2534 /*
2535 * Loop until we either find a matching mapped key, or we
2536 * are sure that it is not a mapped key.
2537 * If a mapped key sequence is found we go back to the start to
2538 * try re-mapping.
2539 */
2540 for (;;)
2541 {
Bram Moolenaardd000352019-08-02 21:35:33 +02002542 long wait_time;
2543 int keylen = 0;
Bram Moolenaar652de232019-04-04 20:13:09 +02002544
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 /*
2546 * ui_breakcheck() is slow, don't use it too often when
2547 * inside a mapping. But call it each time for typed
2548 * characters.
2549 */
2550 if (typebuf.tb_maplen)
2551 line_breakcheck();
2552 else
2553 ui_breakcheck(); /* check for CTRL-C */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 if (got_int)
2555 {
2556 /* flush all input */
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02002557 c = inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 0L);
Bram Moolenaardd000352019-08-02 21:35:33 +02002558
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 /*
2560 * If inchar() returns TRUE (script file was active) or we
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002561 * are inside a mapping, get out of Insert mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 * Otherwise we behave like having gotten a CTRL-C.
2563 * As a result typing CTRL-C in insert mode will
2564 * really insert a CTRL-C.
2565 */
2566 if ((c || typebuf.tb_maplen)
2567 && (State & (INSERT + CMDLINE)))
2568 c = ESC;
2569 else
2570 c = Ctrl_C;
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02002571 flush_buffers(FLUSH_INPUT); // flush all typeahead
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572
Bram Moolenaard9dfd572006-10-03 13:36:13 +00002573 if (advance)
2574 {
2575 /* Also record this character, it might be needed to
2576 * get out of Insert mode. */
2577 *typebuf.tb_buf = c;
2578 gotchars(typebuf.tb_buf, 1);
2579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 cmd_silent = FALSE;
2581
2582 break;
2583 }
2584 else if (typebuf.tb_len > 0)
2585 {
2586 /*
Bram Moolenaardd000352019-08-02 21:35:33 +02002587 * Check for a mapping in "typebuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 */
Bram Moolenaardd000352019-08-02 21:35:33 +02002589 map_result_T result = handle_mapping(
2590 &keylen, &timedout, &mapdepth);
2591
2592 if (result == map_result_retry)
2593 // try mapping again
2594 continue;
2595 if (result == map_result_fail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 {
Bram Moolenaardd000352019-08-02 21:35:33 +02002597 // failed, use the outer loop
2598 c = -1;
2599 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 }
Bram Moolenaardd000352019-08-02 21:35:33 +02002601 if (result == map_result_get)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603/*
2604 * get a character: 2. from the typeahead buffer
2605 */
Bram Moolenaardd000352019-08-02 21:35:33 +02002606 c = typebuf.tb_buf[typebuf.tb_off] & 255;
2607 if (advance) /* remove chars from tb_buf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 {
Bram Moolenaardd000352019-08-02 21:35:33 +02002609 cmd_silent = (typebuf.tb_silent > 0);
2610 if (typebuf.tb_maplen > 0)
2611 KeyTyped = FALSE;
2612 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 {
Bram Moolenaardd000352019-08-02 21:35:33 +02002614 KeyTyped = TRUE;
2615 /* write char to script file(s) */
2616 gotchars(typebuf.tb_buf
2617 + typebuf.tb_off, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 }
Bram Moolenaardd000352019-08-02 21:35:33 +02002619 KeyNoremap = typebuf.tb_noremap[
2620 typebuf.tb_off];
2621 del_typebuf(1, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 }
Bram Moolenaardd000352019-08-02 21:35:33 +02002623 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 }
2625
Bram Moolenaardd000352019-08-02 21:35:33 +02002626 // not enough characters, get more
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 }
2628
2629/*
2630 * get a character: 3. from the user - handle <Esc> in Insert mode
2631 */
2632 /*
Bram Moolenaarf085f422017-06-07 20:39:47 +02002633 * Special case: if we get an <ESC> in insert mode and there
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 * are no more characters at once, we pretend to go out of
2635 * insert mode. This prevents the one second delay after
2636 * typing an <ESC>. If we get something after all, we may
2637 * have to redisplay the mode. That the cursor is in the wrong
2638 * place does not matter.
2639 */
2640 c = 0;
2641#ifdef FEAT_CMDL_INFO
2642 new_wcol = curwin->w_wcol;
2643 new_wrow = curwin->w_wrow;
2644#endif
2645 if ( advance
2646 && typebuf.tb_len == 1
2647 && typebuf.tb_buf[typebuf.tb_off] == ESC
2648 && !no_mapping
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 && ex_normal_busy == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 && typebuf.tb_maplen == 0
2651 && (State & INSERT)
Bram Moolenaar946ffd42010-12-30 12:30:31 +01002652 && (p_timeout
2653 || (keylen == KEYLEN_PART_KEY && p_ttimeout))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 && (c = inchar(typebuf.tb_buf + typebuf.tb_off
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02002655 + typebuf.tb_len, 3, 25L)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 {
2657 colnr_T col = 0, vcol;
2658 char_u *ptr;
2659
Bram Moolenaar28c258f2006-01-25 22:02:51 +00002660 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 {
2662 unshowmode(TRUE);
2663 mode_deleted = TRUE;
2664 }
2665#ifdef FEAT_GUI
Bram Moolenaarf085f422017-06-07 20:39:47 +02002666 /* may show a different cursor shape */
2667 if (gui.in_use && State != NORMAL && !cmd_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 {
2669 int save_State;
2670
2671 save_State = State;
2672 State = NORMAL;
2673 gui_update_cursor(TRUE, FALSE);
2674 State = save_State;
2675 shape_changed = TRUE;
2676 }
2677#endif
2678 validate_cursor();
2679 old_wcol = curwin->w_wcol;
2680 old_wrow = curwin->w_wrow;
2681
2682 /* move cursor left, if possible */
2683 if (curwin->w_cursor.col != 0)
2684 {
2685 if (curwin->w_wcol > 0)
2686 {
2687 if (did_ai)
2688 {
2689 /*
2690 * We are expecting to truncate the trailing
2691 * white-space, so find the last non-white
2692 * character -- webb
2693 */
2694 col = vcol = curwin->w_wcol = 0;
2695 ptr = ml_get_curline();
2696 while (col < curwin->w_cursor.col)
2697 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002698 if (!VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 curwin->w_wcol = vcol;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002700 vcol += lbr_chartabsize(ptr, ptr + col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002703 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 ++col;
2706 }
2707 curwin->w_wrow = curwin->w_cline_row
Bram Moolenaar02631462017-09-22 15:20:32 +02002708 + curwin->w_wcol / curwin->w_width;
2709 curwin->w_wcol %= curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 curwin->w_wcol += curwin_col_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 col = 0; /* no correction needed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 }
2713 else
2714 {
2715 --curwin->w_wcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 col = curwin->w_cursor.col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 }
2718 }
2719 else if (curwin->w_p_wrap && curwin->w_wrow)
2720 {
2721 --curwin->w_wrow;
Bram Moolenaar02631462017-09-22 15:20:32 +02002722 curwin->w_wcol = curwin->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 col = curwin->w_cursor.col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 if (has_mbyte && col > 0 && curwin->w_wcol > 0)
2726 {
2727 /* Correct when the cursor is on the right halve
2728 * of a double-wide character. */
2729 ptr = ml_get_curline();
2730 col -= (*mb_head_off)(ptr, ptr + col);
2731 if ((*mb_ptr2cells)(ptr + col) > 1)
2732 --curwin->w_wcol;
2733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 }
2735 setcursor();
2736 out_flush();
2737#ifdef FEAT_CMDL_INFO
2738 new_wcol = curwin->w_wcol;
2739 new_wrow = curwin->w_wrow;
2740#endif
2741 curwin->w_wcol = old_wcol;
2742 curwin->w_wrow = old_wrow;
2743 }
2744 if (c < 0)
2745 continue; /* end of input script reached */
Bram Moolenaar20c38922014-07-23 20:41:14 +02002746
2747 /* Allow mapping for just typed characters. When we get here c
2748 * is the number of extra bytes and typebuf.tb_len is 1. */
2749 for (n = 1; n <= c; ++n)
2750 typebuf.tb_noremap[typebuf.tb_off + n] = RM_YES;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 typebuf.tb_len += c;
2752
2753 /* buffer full, don't map */
2754 if (typebuf.tb_len >= typebuf.tb_maplen + MAXMAPLEN)
2755 {
2756 timedout = TRUE;
2757 continue;
2758 }
2759
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 if (ex_normal_busy > 0)
2761 {
Bram Moolenaare2c38102016-01-31 14:55:40 +01002762#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 static int tc = 0;
Bram Moolenaare2c38102016-01-31 14:55:40 +01002764#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765
2766 /* No typeahead left and inside ":normal". Must return
2767 * something to avoid getting stuck. When an incomplete
2768 * mapping is present, behave like it timed out. */
2769 if (typebuf.tb_len > 0)
2770 {
2771 timedout = TRUE;
2772 continue;
2773 }
2774 /* When 'insertmode' is set, ESC just beeps in Insert
2775 * mode. Use CTRL-L to make edit() return.
2776 * For the command line only CTRL-C always breaks it.
2777 * For the cmdline window: Alternate between ESC and
2778 * CTRL-C: ESC for most situations and CTRL-C to close the
2779 * cmdline window. */
2780 if (p_im && (State & INSERT))
2781 c = Ctrl_L;
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002782#ifdef FEAT_TERMINAL
2783 else if (terminal_is_active())
2784 c = K_CANCEL;
2785#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 else if ((State & CMDLINE)
Bram Moolenaare2c38102016-01-31 14:55:40 +01002787#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 || (cmdwin_type > 0 && tc == ESC)
Bram Moolenaare2c38102016-01-31 14:55:40 +01002789#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 )
2791 c = Ctrl_C;
2792 else
2793 c = ESC;
Bram Moolenaare2c38102016-01-31 14:55:40 +01002794#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 tc = c;
Bram Moolenaare2c38102016-01-31 14:55:40 +01002796#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 break;
2798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799
2800/*
2801 * get a character: 3. from the user - update display
2802 */
2803 /* In insert mode a screen update is skipped when characters
2804 * are still available. But when those available characters
2805 * are part of a mapping, and we are going to do a blocking
2806 * wait here. Need to update the screen to display the
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01002807 * changed text so far. Also for when 'lazyredraw' is set and
2808 * redrawing was postponed because there was something in the
2809 * input buffer (e.g., termresponse). */
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01002810 if (((State & INSERT) != 0 || p_lz) && (State & CMDLINE) == 0
2811 && advance && must_redraw != 0 && !need_wait_return)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 {
2813 update_screen(0);
2814 setcursor(); /* put cursor back where it belongs */
2815 }
2816
2817 /*
2818 * If we have a partial match (and are going to wait for more
2819 * input from the user), show the partially matched characters
2820 * to the user with showcmd.
2821 */
2822#ifdef FEAT_CMDL_INFO
2823 i = 0;
2824#endif
2825 c1 = 0;
2826 if (typebuf.tb_len > 0 && advance && !exmode_active)
2827 {
2828 if (((State & (NORMAL | INSERT)) || State == LANGMAP)
2829 && State != HITRETURN)
2830 {
2831 /* this looks nice when typing a dead character map */
2832 if (State & INSERT
2833 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
2834 + typebuf.tb_len - 1) == 1)
2835 {
2836 edit_putchar(typebuf.tb_buf[typebuf.tb_off
2837 + typebuf.tb_len - 1], FALSE);
2838 setcursor(); /* put cursor back where it belongs */
2839 c1 = 1;
2840 }
2841#ifdef FEAT_CMDL_INFO
2842 /* need to use the col and row from above here */
2843 old_wcol = curwin->w_wcol;
2844 old_wrow = curwin->w_wrow;
2845 curwin->w_wcol = new_wcol;
2846 curwin->w_wrow = new_wrow;
2847 push_showcmd();
2848 if (typebuf.tb_len > SHOWCMD_COLS)
2849 i = typebuf.tb_len - SHOWCMD_COLS;
2850 while (i < typebuf.tb_len)
2851 (void)add_to_showcmd(typebuf.tb_buf[typebuf.tb_off
2852 + i++]);
2853 curwin->w_wcol = old_wcol;
2854 curwin->w_wrow = old_wrow;
2855#endif
2856 }
2857
2858 /* this looks nice when typing a dead character map */
2859 if ((State & CMDLINE)
2860#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2861 && cmdline_star == 0
2862#endif
2863 && ptr2cells(typebuf.tb_buf + typebuf.tb_off
2864 + typebuf.tb_len - 1) == 1)
2865 {
2866 putcmdline(typebuf.tb_buf[typebuf.tb_off
2867 + typebuf.tb_len - 1], FALSE);
2868 c1 = 1;
2869 }
2870 }
2871
2872/*
2873 * get a character: 3. from the user - get it
2874 */
Bram Moolenaar83f4cbd2018-06-12 21:35:40 +02002875 if (typebuf.tb_len == 0)
2876 // timedout may have been set while waiting for a mapping
2877 // that has a <Nop> RHS.
2878 timedout = FALSE;
2879
Bram Moolenaar652de232019-04-04 20:13:09 +02002880 if (advance)
2881 {
2882 if (typebuf.tb_len == 0
2883 || !(p_timeout
2884 || (p_ttimeout && keylen == KEYLEN_PART_KEY)))
2885 // blocking wait
2886 wait_time = -1L;
2887 else if (keylen == KEYLEN_PART_KEY && p_ttm >= 0)
2888 wait_time = p_ttm;
2889 else
2890 wait_time = p_tm;
2891 }
2892 else
2893 wait_time = 0;
2894
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00002895 wait_tb_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 c = inchar(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len,
2897 typebuf.tb_buflen - typebuf.tb_off - typebuf.tb_len - 1,
Bram Moolenaar652de232019-04-04 20:13:09 +02002898 wait_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899
2900#ifdef FEAT_CMDL_INFO
2901 if (i != 0)
2902 pop_showcmd();
2903#endif
2904 if (c1 == 1)
2905 {
2906 if (State & INSERT)
2907 edit_unputchar();
2908 if (State & CMDLINE)
2909 unputcmdline();
Bram Moolenaarbc256d92012-06-06 12:06:15 +02002910 else
2911 setcursor(); /* put cursor back where it belongs */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 }
2913
2914 if (c < 0)
2915 continue; /* end of input script reached */
2916 if (c == NUL) /* no character available */
2917 {
2918 if (!advance)
2919 break;
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00002920 if (wait_tb_len > 0) /* timed out */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 {
2922 timedout = TRUE;
2923 continue;
2924 }
2925 }
2926 else
2927 { /* allow mapping for just typed characters */
2928 while (typebuf.tb_buf[typebuf.tb_off
2929 + typebuf.tb_len] != NUL)
2930 typebuf.tb_noremap[typebuf.tb_off
2931 + typebuf.tb_len++] = RM_YES;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002932#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 /* Get IM status right after getting keys, not after the
2934 * timeout for a mapping (focus may be lost by then). */
2935 vgetc_im_active = im_get_status();
2936#endif
2937 }
2938 } /* for (;;) */
2939 } /* if (!character from stuffbuf) */
2940
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002941 /* if advance is FALSE don't loop on NULs */
2942 } while ((c < 0 && c != K_CANCEL) || (advance && c == NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943
2944 /*
2945 * The "INSERT" message is taken care of here:
2946 * if we return an ESC to exit insert mode, the message is deleted
2947 * if we don't return an ESC but deleted the message before, redisplay it
2948 */
Bram Moolenaar09df3122006-01-23 22:23:09 +00002949 if (advance && p_smd && msg_silent == 0 && (State & INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00002951 if (c == ESC && !mode_deleted && !no_mapping && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 {
2953 if (typebuf.tb_len && !KeyTyped)
2954 redraw_cmdline = TRUE; /* delete mode later */
2955 else
2956 unshowmode(FALSE);
2957 }
2958 else if (c != ESC && mode_deleted)
2959 {
2960 if (typebuf.tb_len && !KeyTyped)
2961 redraw_cmdline = TRUE; /* show mode later */
2962 else
2963 showmode();
2964 }
2965 }
2966#ifdef FEAT_GUI
2967 /* may unshow different cursor shape */
Bram Moolenaarf085f422017-06-07 20:39:47 +02002968 if (gui.in_use && shape_changed)
2969 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970#endif
Bram Moolenaar6edbbd82019-03-10 09:41:51 +01002971 if (timedout && c == ESC)
2972 {
2973 char_u nop_buf[3];
2974
2975 // When recording there will be no timeout. Add a <Nop> after the ESC
2976 // to avoid that it forms a key code with following characters.
2977 nop_buf[0] = K_SPECIAL;
2978 nop_buf[1] = KS_EXTRA;
2979 nop_buf[2] = KE_NOP;
2980 gotchars(nop_buf, 3);
2981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002983 --vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984
2985 return c;
2986}
2987
2988/*
2989 * inchar() - get one character from
2990 * 1. a scriptfile
2991 * 2. the keyboard
2992 *
2993 * As much characters as we can get (upto 'maxlen') are put in "buf" and
2994 * NUL terminated (buffer length must be 'maxlen' + 1).
2995 * Minimum for "maxlen" is 3!!!!
2996 *
2997 * "tb_change_cnt" is the value of typebuf.tb_change_cnt if "buf" points into
2998 * it. When typebuf.tb_change_cnt changes (e.g., when a message is received
2999 * from a remote client) "buf" can no longer be used. "tb_change_cnt" is 0
3000 * otherwise.
3001 *
3002 * If we got an interrupt all input is read until none is available.
3003 *
3004 * If wait_time == 0 there is no waiting for the char.
3005 * If wait_time == n we wait for n msec for a character to arrive.
3006 * If wait_time == -1 we wait forever for a character to arrive.
3007 *
3008 * Return the number of obtained characters.
3009 * Return -1 when end of input script reached.
3010 */
Bram Moolenaarcda77642016-06-04 13:32:35 +02003011 static int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003012inchar(
3013 char_u *buf,
3014 int maxlen,
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003015 long wait_time) /* milli seconds */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016{
3017 int len = 0; /* init for GCC */
3018 int retesc = FALSE; /* return ESC with gotint */
3019 int script_char;
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003020 int tb_change_cnt = typebuf.tb_change_cnt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021
3022 if (wait_time == -1L || wait_time > 100L) /* flush output before waiting */
3023 {
3024 cursor_on();
Bram Moolenaara338adc2018-01-31 20:51:47 +01003025 out_flush_cursor(FALSE, FALSE);
3026#if defined(FEAT_GUI) && defined(FEAT_MOUSESHAPE)
3027 if (gui.in_use && postponed_mouseshape)
3028 update_mouseshape(-1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029#endif
3030 }
3031
3032 /*
3033 * Don't reset these when at the hit-return prompt, otherwise a endless
3034 * recursive loop may result (write error in swapfile, hit-return, timeout
3035 * on char wait, flush swapfile, write error....).
3036 */
3037 if (State != HITRETURN)
3038 {
3039 did_outofmem_msg = FALSE; /* display out of memory message (again) */
3040 did_swapwrite_msg = FALSE; /* display swap file write error again */
3041 }
3042 undo_off = FALSE; /* restart undo now */
3043
3044 /*
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00003045 * Get a character from a script file if there is one.
3046 * If interrupted: Stop reading script files, close them all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 */
3048 script_char = -1;
Bram Moolenaaree3f7a52008-01-01 13:17:56 +00003049 while (scriptin[curscript] != NULL && script_char < 0
3050#ifdef FEAT_EVAL
3051 && !ignore_script
3052#endif
3053 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00003055
Bram Moolenaar93c88e02015-09-15 14:12:05 +02003056#ifdef MESSAGE_QUEUE
3057 parse_queued_messages();
Bram Moolenaarf2330482008-06-24 20:19:36 +00003058#endif
3059
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 if (got_int || (script_char = getc(scriptin[curscript])) < 0)
3061 {
3062 /* Reached EOF.
3063 * Careful: closescript() frees typebuf.tb_buf[] and buf[] may
3064 * point inside typebuf.tb_buf[]. Don't use buf[] after this! */
3065 closescript();
3066 /*
3067 * When reading script file is interrupted, return an ESC to get
3068 * back to normal mode.
3069 * Otherwise return -1, because typebuf.tb_buf[] has changed.
3070 */
3071 if (got_int)
3072 retesc = TRUE;
3073 else
3074 return -1;
3075 }
3076 else
3077 {
3078 buf[0] = script_char;
3079 len = 1;
3080 }
3081 }
3082
3083 if (script_char < 0) /* did not get a character from script */
3084 {
3085 /*
3086 * If we got an interrupt, skip all previously typed characters and
3087 * return TRUE if quit reading script file.
3088 * Stop reading typeahead when a single CTRL-C was read,
3089 * fill_input_buf() returns this when not able to read from stdin.
3090 * Don't use buf[] here, closescript() may have freed typebuf.tb_buf[]
3091 * and buf may be pointing inside typebuf.tb_buf[].
3092 */
3093 if (got_int)
3094 {
3095#define DUM_LEN MAXMAPLEN * 3 + 3
3096 char_u dum[DUM_LEN + 1];
3097
3098 for (;;)
3099 {
3100 len = ui_inchar(dum, DUM_LEN, 0L, 0);
3101 if (len == 0 || (len == 1 && dum[0] == 3))
3102 break;
3103 }
3104 return retesc;
3105 }
3106
3107 /*
3108 * Always flush the output characters when getting input characters
Bram Moolenaarcb574f42019-01-25 22:29:57 +01003109 * from the user and not just peeking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 */
Bram Moolenaarcb574f42019-01-25 22:29:57 +01003111 if (wait_time == -1L || wait_time > 10L)
3112 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113
3114 /*
3115 * Fill up to a third of the buffer, because each character may be
3116 * tripled below.
3117 */
3118 len = ui_inchar(buf, maxlen / 3, wait_time, tb_change_cnt);
3119 }
3120
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003121 /* If the typebuf was changed further down, it is like nothing was added by
3122 * this call. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 if (typebuf_changed(tb_change_cnt))
3124 return 0;
3125
Bram Moolenaar0f0f2302017-08-30 18:52:56 +02003126 /* Note the change in the typeahead buffer, this matters for when
3127 * vgetorpeek() is called recursively, e.g. using getchar(1) in a timer
3128 * function. */
3129 if (len > 0 && ++typebuf.tb_change_cnt == 0)
3130 typebuf.tb_change_cnt = 1;
3131
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003132 return fix_input_buffer(buf, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133}
3134
3135/*
3136 * Fix typed characters for use by vgetc() and check_termcode().
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003137 * "buf[]" must have room to triple the number of bytes!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 * Returns the new length.
3139 */
3140 int
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003141fix_input_buffer(char_u *buf, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142{
3143 int i;
3144 char_u *p = buf;
3145
3146 /*
3147 * Two characters are special: NUL and K_SPECIAL.
3148 * When compiled With the GUI CSI is also special.
3149 * Replace NUL by K_SPECIAL KS_ZERO KE_FILLER
3150 * Replace K_SPECIAL by K_SPECIAL KS_SPECIAL KE_FILLER
3151 * Replace CSI by K_SPECIAL KS_EXTRA KE_CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 */
3153 for (i = len; --i >= 0; ++p)
3154 {
3155#ifdef FEAT_GUI
3156 /* When the GUI is used any character can come after a CSI, don't
3157 * escape it. */
3158 if (gui.in_use && p[0] == CSI && i >= 2)
3159 {
3160 p += 2;
3161 i -= 2;
3162 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003163# ifndef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 /* When the GUI is not used CSI needs to be escaped. */
3165 else if (!gui.in_use && p[0] == CSI)
3166 {
3167 mch_memmove(p + 3, p + 1, (size_t)i);
3168 *p++ = K_SPECIAL;
3169 *p++ = KS_EXTRA;
3170 *p = (int)KE_CSI;
3171 len += 2;
3172 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003173# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 else
3175#endif
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003176 if (p[0] == NUL || (p[0] == K_SPECIAL
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003177 // timeout may generate K_CURSORHOLD
Bram Moolenaar28a37ff2005-03-15 22:28:00 +00003178 && (i < 2 || p[1] != KS_EXTRA || p[2] != (int)KE_CURSORHOLD)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003179#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003180 // Win32 console passes modifiers
3181 && (
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003182# ifdef VIMDLL
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003183 gui.in_use ||
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003184# endif
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003185 (i < 2 || p[1] != KS_MODIFIER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186#endif
3187 ))
3188 {
3189 mch_memmove(p + 3, p + 1, (size_t)i);
3190 p[2] = K_THIRD(p[0]);
3191 p[1] = K_SECOND(p[0]);
3192 p[0] = K_SPECIAL;
3193 p += 2;
3194 len += 2;
3195 }
3196 }
Bram Moolenaared5ab2a2019-05-04 20:00:00 +02003197 *p = NUL; // add trailing NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 return len;
3199}
3200
3201#if defined(USE_INPUT_BUF) || defined(PROTO)
3202/*
3203 * Return TRUE when bytes are in the input buffer or in the typeahead buffer.
3204 * Normally the input buffer would be sufficient, but the server_to_input_buf()
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003205 * or feedkeys() may insert characters in the typeahead buffer while we are
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003206 * waiting for input to arrive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 */
3208 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003209input_available(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210{
3211 return (!vim_is_input_buf_empty()
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003212# if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
3213 || typebuf_was_filled
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214# endif
3215 );
3216}
3217#endif