Bram Moolenaar | edf3f97 | 2016-08-29 22:49:24 +0200 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4 noet: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2 | * |
| 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 Moolenaar | f6f95d9 | 2009-11-11 15:23:37 +0000 | [diff] [blame] | 25 | * - recorded characters: for the "q" command. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 26 | * |
| 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 Moolenaar | 285e335 | 2018-04-18 23:01:13 +0200 | [diff] [blame] | 43 | static buffheader_T redobuff = {{NULL, {NUL}}, NULL, 0, 0}; |
| 44 | static buffheader_T old_redobuff = {{NULL, {NUL}}, NULL, 0, 0}; |
| 45 | static buffheader_T recordbuff = {{NULL, {NUL}}, NULL, 0, 0}; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 46 | |
| 47 | static 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 | */ |
| 53 | static int block_redo = FALSE; |
| 54 | |
Bram Moolenaar | 459fd78 | 2019-10-13 16:43:39 +0200 | [diff] [blame] | 55 | static int KeyNoremap = 0; // remapping flags |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 56 | |
| 57 | /* |
Bram Moolenaar | fd89d7e | 2016-06-04 20:25:05 +0200 | [diff] [blame] | 58 | * Variables used by vgetorpeek() and flush_buffers(). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 59 | * |
| 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 Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 79 | #define RM_ABBR 4 /* tb_noremap: don't remap, do abbrev. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 80 | |
| 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)) |
| 86 | static char_u typebuf_init[TYPELEN_INIT]; /* initial typebuf.tb_buf */ |
| 87 | static char_u noremapbuf_init[TYPELEN_INIT]; /* initial typebuf.tb_noremap */ |
| 88 | |
| 89 | static int last_recorded_len = 0; /* number of last recorded chars */ |
| 90 | |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 91 | static int read_readbuf(buffheader_T *buf, int advance); |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 92 | static void init_typebuf(void); |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 93 | static void may_sync_undo(void); |
Bram Moolenaar | 5843f5f | 2019-08-20 20:13:45 +0200 | [diff] [blame] | 94 | static void free_typebuf(void); |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 95 | static void closescript(void); |
Bram Moolenaar | 5843f5f | 2019-08-20 20:13:45 +0200 | [diff] [blame] | 96 | static void updatescript(int c); |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 97 | static int vgetorpeek(int); |
Bram Moolenaar | 0f0f230 | 2017-08-30 18:52:56 +0200 | [diff] [blame] | 98 | static int inchar(char_u *buf, int maxlen, long wait_time); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 99 | |
| 100 | /* |
| 101 | * Free and clear a buffer. |
| 102 | */ |
| 103 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 104 | free_buff(buffheader_T *buf) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 105 | { |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 106 | buffblock_T *p, *np; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 107 | |
Bram Moolenaar | 285e335 | 2018-04-18 23:01:13 +0200 | [diff] [blame] | 108 | for (p = buf->bh_first.b_next; p != NULL; p = np) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 109 | { |
| 110 | np = p->b_next; |
| 111 | vim_free(p); |
| 112 | } |
Bram Moolenaar | 285e335 | 2018-04-18 23:01:13 +0200 | [diff] [blame] | 113 | buf->bh_first.b_next = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | /* |
| 117 | * Return the contents of a buffer as a single string. |
| 118 | * K_SPECIAL and CSI in the returned string are escaped. |
| 119 | */ |
| 120 | static char_u * |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 121 | get_buffcont( |
| 122 | buffheader_T *buffer, |
| 123 | int dozero) /* count == zero is not an error */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 124 | { |
| 125 | long_u count = 0; |
| 126 | char_u *p = NULL; |
| 127 | char_u *p2; |
| 128 | char_u *str; |
Bram Moolenaar | 285e335 | 2018-04-18 23:01:13 +0200 | [diff] [blame] | 129 | buffblock_T *bp; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 130 | |
| 131 | /* compute the total length of the string */ |
Bram Moolenaar | 285e335 | 2018-04-18 23:01:13 +0200 | [diff] [blame] | 132 | for (bp = buffer->bh_first.b_next; bp != NULL; bp = bp->b_next) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 133 | count += (long_u)STRLEN(bp->b_str); |
| 134 | |
Bram Moolenaar | 18a4ba2 | 2019-05-24 19:39:03 +0200 | [diff] [blame] | 135 | if ((count || dozero) && (p = alloc(count + 1)) != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 136 | { |
| 137 | p2 = p; |
Bram Moolenaar | 285e335 | 2018-04-18 23:01:13 +0200 | [diff] [blame] | 138 | for (bp = buffer->bh_first.b_next; bp != NULL; bp = bp->b_next) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 139 | for (str = bp->b_str; *str; ) |
| 140 | *p2++ = *str++; |
| 141 | *p2 = NUL; |
| 142 | } |
| 143 | return (p); |
| 144 | } |
| 145 | |
| 146 | /* |
| 147 | * Return the contents of the record buffer as a single string |
| 148 | * and clear the record buffer. |
| 149 | * K_SPECIAL and CSI in the returned string are escaped. |
| 150 | */ |
| 151 | char_u * |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 152 | get_recorded(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 153 | { |
| 154 | char_u *p; |
| 155 | size_t len; |
| 156 | |
| 157 | p = get_buffcont(&recordbuff, TRUE); |
| 158 | free_buff(&recordbuff); |
| 159 | |
| 160 | /* |
| 161 | * Remove the characters that were added the last time, these must be the |
| 162 | * (possibly mapped) characters that stopped the recording. |
| 163 | */ |
| 164 | len = STRLEN(p); |
| 165 | if ((int)len >= last_recorded_len) |
| 166 | { |
| 167 | len -= last_recorded_len; |
| 168 | p[len] = NUL; |
| 169 | } |
| 170 | |
| 171 | /* |
| 172 | * When stopping recording from Insert mode with CTRL-O q, also remove the |
| 173 | * CTRL-O. |
| 174 | */ |
| 175 | if (len > 0 && restart_edit != 0 && p[len - 1] == Ctrl_O) |
| 176 | p[len - 1] = NUL; |
| 177 | |
| 178 | return (p); |
| 179 | } |
| 180 | |
| 181 | /* |
| 182 | * Return the contents of the redo buffer as a single string. |
| 183 | * K_SPECIAL and CSI in the returned string are escaped. |
| 184 | */ |
| 185 | char_u * |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 186 | get_inserted(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 187 | { |
Bram Moolenaar | f233048 | 2008-06-24 20:19:36 +0000 | [diff] [blame] | 188 | return get_buffcont(&redobuff, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | /* |
Bram Moolenaar | 4e86cba | 2007-05-06 11:56:32 +0000 | [diff] [blame] | 192 | * Add string "s" after the current block of buffer "buf". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 193 | * K_SPECIAL and CSI should have been escaped already. |
| 194 | */ |
| 195 | static void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 196 | add_buff( |
| 197 | buffheader_T *buf, |
| 198 | char_u *s, |
| 199 | long slen) /* length of "s" or -1 */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 200 | { |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 201 | buffblock_T *p; |
Bram Moolenaar | 285e335 | 2018-04-18 23:01:13 +0200 | [diff] [blame] | 202 | long_u len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 203 | |
| 204 | if (slen < 0) |
| 205 | slen = (long)STRLEN(s); |
| 206 | if (slen == 0) /* don't add empty strings */ |
| 207 | return; |
| 208 | |
Bram Moolenaar | 285e335 | 2018-04-18 23:01:13 +0200 | [diff] [blame] | 209 | if (buf->bh_first.b_next == NULL) /* first add to list */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 210 | { |
| 211 | buf->bh_space = 0; |
Bram Moolenaar | 285e335 | 2018-04-18 23:01:13 +0200 | [diff] [blame] | 212 | buf->bh_curr = &(buf->bh_first); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 213 | } |
| 214 | else if (buf->bh_curr == NULL) /* buffer has already been read */ |
| 215 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 216 | iemsg(_("E222: Add to read buffer")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 217 | return; |
| 218 | } |
| 219 | else if (buf->bh_index != 0) |
Bram Moolenaar | 285e335 | 2018-04-18 23:01:13 +0200 | [diff] [blame] | 220 | mch_memmove(buf->bh_first.b_next->b_str, |
| 221 | buf->bh_first.b_next->b_str + buf->bh_index, |
| 222 | STRLEN(buf->bh_first.b_next->b_str + buf->bh_index) + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 223 | buf->bh_index = 0; |
| 224 | |
| 225 | if (buf->bh_space >= (int)slen) |
| 226 | { |
| 227 | len = (long_u)STRLEN(buf->bh_curr->b_str); |
Bram Moolenaar | b635633 | 2005-07-18 21:40:44 +0000 | [diff] [blame] | 228 | vim_strncpy(buf->bh_curr->b_str + len, s, (size_t)slen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 229 | buf->bh_space -= slen; |
| 230 | } |
| 231 | else |
| 232 | { |
| 233 | if (slen < MINIMAL_SIZE) |
| 234 | len = MINIMAL_SIZE; |
| 235 | else |
| 236 | len = slen; |
Bram Moolenaar | 47ed553 | 2019-08-08 20:49:14 +0200 | [diff] [blame] | 237 | p = alloc(offsetof(buffblock_T, b_str) + len + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 238 | if (p == NULL) |
| 239 | return; /* no space, just forget it */ |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 240 | buf->bh_space = (int)(len - slen); |
Bram Moolenaar | b635633 | 2005-07-18 21:40:44 +0000 | [diff] [blame] | 241 | vim_strncpy(p->b_str, s, (size_t)slen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 242 | |
Bram Moolenaar | 285e335 | 2018-04-18 23:01:13 +0200 | [diff] [blame] | 243 | p->b_next = buf->bh_curr->b_next; |
| 244 | buf->bh_curr->b_next = p; |
| 245 | buf->bh_curr = p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 246 | } |
| 247 | return; |
| 248 | } |
| 249 | |
| 250 | /* |
| 251 | * Add number "n" to buffer "buf". |
| 252 | */ |
| 253 | static void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 254 | add_num_buff(buffheader_T *buf, long n) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 255 | { |
| 256 | char_u number[32]; |
| 257 | |
| 258 | sprintf((char *)number, "%ld", n); |
| 259 | add_buff(buf, number, -1L); |
| 260 | } |
| 261 | |
| 262 | /* |
| 263 | * Add character 'c' to buffer "buf". |
| 264 | * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters. |
| 265 | */ |
| 266 | static void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 267 | add_char_buff(buffheader_T *buf, int c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 268 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 269 | char_u bytes[MB_MAXBYTES + 1]; |
| 270 | int len; |
| 271 | int i; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 272 | char_u temp[4]; |
| 273 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 274 | if (IS_SPECIAL(c)) |
| 275 | len = 1; |
| 276 | else |
| 277 | len = (*mb_char2bytes)(c, bytes); |
| 278 | for (i = 0; i < len; ++i) |
| 279 | { |
| 280 | if (!IS_SPECIAL(c)) |
| 281 | c = bytes[i]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 282 | |
| 283 | if (IS_SPECIAL(c) || c == K_SPECIAL || c == NUL) |
| 284 | { |
| 285 | /* translate special key code into three byte sequence */ |
| 286 | temp[0] = K_SPECIAL; |
| 287 | temp[1] = K_SECOND(c); |
| 288 | temp[2] = K_THIRD(c); |
| 289 | temp[3] = NUL; |
| 290 | } |
| 291 | #ifdef FEAT_GUI |
| 292 | else if (c == CSI) |
| 293 | { |
| 294 | /* Translate a CSI to a CSI - KS_EXTRA - KE_CSI sequence */ |
| 295 | temp[0] = CSI; |
| 296 | temp[1] = KS_EXTRA; |
| 297 | temp[2] = (int)KE_CSI; |
| 298 | temp[3] = NUL; |
| 299 | } |
| 300 | #endif |
| 301 | else |
| 302 | { |
| 303 | temp[0] = c; |
| 304 | temp[1] = NUL; |
| 305 | } |
| 306 | add_buff(buf, temp, -1L); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 307 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 310 | /* First read ahead buffer. Used for translated commands. */ |
Bram Moolenaar | 285e335 | 2018-04-18 23:01:13 +0200 | [diff] [blame] | 311 | static buffheader_T readbuf1 = {{NULL, {NUL}}, NULL, 0, 0}; |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 312 | |
| 313 | /* Second read ahead buffer. Used for redo. */ |
Bram Moolenaar | 285e335 | 2018-04-18 23:01:13 +0200 | [diff] [blame] | 314 | static buffheader_T readbuf2 = {{NULL, {NUL}}, NULL, 0, 0}; |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 315 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 316 | /* |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 317 | * Get one byte from the read buffers. Use readbuf1 one first, use readbuf2 |
| 318 | * if that one is empty. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 319 | * If advance == TRUE go to the next char. |
| 320 | * No translation is done K_SPECIAL and CSI are escaped. |
| 321 | */ |
| 322 | static int |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 323 | read_readbuffers(int advance) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 324 | { |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 325 | int c; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 326 | |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 327 | c = read_readbuf(&readbuf1, advance); |
| 328 | if (c == NUL) |
| 329 | c = read_readbuf(&readbuf2, advance); |
| 330 | return c; |
| 331 | } |
| 332 | |
| 333 | static int |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 334 | read_readbuf(buffheader_T *buf, int advance) |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 335 | { |
| 336 | char_u c; |
| 337 | buffblock_T *curr; |
| 338 | |
Bram Moolenaar | 285e335 | 2018-04-18 23:01:13 +0200 | [diff] [blame] | 339 | if (buf->bh_first.b_next == NULL) /* buffer is empty */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 340 | return NUL; |
| 341 | |
Bram Moolenaar | 285e335 | 2018-04-18 23:01:13 +0200 | [diff] [blame] | 342 | curr = buf->bh_first.b_next; |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 343 | c = curr->b_str[buf->bh_index]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 344 | |
| 345 | if (advance) |
| 346 | { |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 347 | if (curr->b_str[++buf->bh_index] == NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 348 | { |
Bram Moolenaar | 285e335 | 2018-04-18 23:01:13 +0200 | [diff] [blame] | 349 | buf->bh_first.b_next = curr->b_next; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 350 | vim_free(curr); |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 351 | buf->bh_index = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 352 | } |
| 353 | } |
| 354 | return c; |
| 355 | } |
| 356 | |
| 357 | /* |
Bram Moolenaar | 06811f3 | 2014-02-15 16:17:07 +0100 | [diff] [blame] | 358 | * Prepare the read buffers for reading (if they contain something). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 359 | */ |
| 360 | static void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 361 | start_stuff(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 362 | { |
Bram Moolenaar | 285e335 | 2018-04-18 23:01:13 +0200 | [diff] [blame] | 363 | if (readbuf1.bh_first.b_next != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 364 | { |
Bram Moolenaar | 285e335 | 2018-04-18 23:01:13 +0200 | [diff] [blame] | 365 | readbuf1.bh_curr = &(readbuf1.bh_first); |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 366 | readbuf1.bh_space = 0; |
| 367 | } |
Bram Moolenaar | 285e335 | 2018-04-18 23:01:13 +0200 | [diff] [blame] | 368 | if (readbuf2.bh_first.b_next != NULL) |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 369 | { |
Bram Moolenaar | 285e335 | 2018-04-18 23:01:13 +0200 | [diff] [blame] | 370 | readbuf2.bh_curr = &(readbuf2.bh_first); |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 371 | readbuf2.bh_space = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 372 | } |
| 373 | } |
| 374 | |
| 375 | /* |
| 376 | * Return TRUE if the stuff buffer is empty. |
| 377 | */ |
| 378 | int |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 379 | stuff_empty(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 380 | { |
Bram Moolenaar | 285e335 | 2018-04-18 23:01:13 +0200 | [diff] [blame] | 381 | return (readbuf1.bh_first.b_next == NULL |
| 382 | && readbuf2.bh_first.b_next == NULL); |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 383 | } |
| 384 | |
Bram Moolenaar | 113e107 | 2019-01-20 15:30:40 +0100 | [diff] [blame] | 385 | #if defined(FEAT_EVAL) || defined(PROTO) |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 386 | /* |
| 387 | * Return TRUE if readbuf1 is empty. There may still be redo characters in |
| 388 | * redbuf2. |
| 389 | */ |
| 390 | int |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 391 | readbuf1_empty(void) |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 392 | { |
Bram Moolenaar | 285e335 | 2018-04-18 23:01:13 +0200 | [diff] [blame] | 393 | return (readbuf1.bh_first.b_next == NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 394 | } |
Bram Moolenaar | 113e107 | 2019-01-20 15:30:40 +0100 | [diff] [blame] | 395 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 396 | |
| 397 | /* |
| 398 | * Set a typeahead character that won't be flushed. |
| 399 | */ |
| 400 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 401 | typeahead_noflush(int c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 402 | { |
| 403 | typeahead_char = c; |
| 404 | } |
| 405 | |
| 406 | /* |
| 407 | * Remove the contents of the stuff buffer and the mapped characters in the |
Bram Moolenaar | 70b2a56 | 2012-01-10 22:26:17 +0100 | [diff] [blame] | 408 | * typeahead buffer (used in case of an error). If "flush_typeahead" is true, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 409 | * flush all typeahead characters (used when interrupted by a CTRL-C). |
| 410 | */ |
| 411 | void |
Bram Moolenaar | 6a2633b | 2018-10-07 23:16:36 +0200 | [diff] [blame] | 412 | flush_buffers(flush_buffers_T flush_typeahead) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 413 | { |
| 414 | init_typebuf(); |
| 415 | |
| 416 | start_stuff(); |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 417 | while (read_readbuffers(TRUE) != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 418 | ; |
| 419 | |
Bram Moolenaar | 6a2633b | 2018-10-07 23:16:36 +0200 | [diff] [blame] | 420 | if (flush_typeahead == FLUSH_MINIMAL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 421 | { |
Bram Moolenaar | 6a2633b | 2018-10-07 23:16:36 +0200 | [diff] [blame] | 422 | // remove mapped characters at the start only |
| 423 | typebuf.tb_off += typebuf.tb_maplen; |
| 424 | typebuf.tb_len -= typebuf.tb_maplen; |
| 425 | } |
| 426 | else |
| 427 | { |
| 428 | // remove typeahead |
| 429 | if (flush_typeahead == FLUSH_INPUT) |
| 430 | // We have to get all characters, because we may delete the first |
| 431 | // part of an escape sequence. In an xterm we get one char at a |
| 432 | // time and we have to get them all. |
| 433 | while (inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 10L) != 0) |
| 434 | ; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 435 | typebuf.tb_off = MAXMAPLEN; |
| 436 | typebuf.tb_len = 0; |
Bram Moolenaar | 4eb6531 | 2017-06-24 18:49:00 +0200 | [diff] [blame] | 437 | #if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL) |
| 438 | /* Reset the flag that text received from a client or from feedkeys() |
| 439 | * was inserted in the typeahead buffer. */ |
| 440 | typebuf_was_filled = FALSE; |
| 441 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 442 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 443 | typebuf.tb_maplen = 0; |
| 444 | typebuf.tb_silent = 0; |
| 445 | cmd_silent = FALSE; |
| 446 | typebuf.tb_no_abbr_cnt = 0; |
| 447 | } |
| 448 | |
| 449 | /* |
| 450 | * The previous contents of the redo buffer is kept in old_redobuffer. |
| 451 | * This is used for the CTRL-O <.> command in insert mode. |
| 452 | */ |
| 453 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 454 | ResetRedobuff(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 455 | { |
| 456 | if (!block_redo) |
| 457 | { |
| 458 | free_buff(&old_redobuff); |
| 459 | old_redobuff = redobuff; |
Bram Moolenaar | 285e335 | 2018-04-18 23:01:13 +0200 | [diff] [blame] | 460 | redobuff.bh_first.b_next = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 461 | } |
| 462 | } |
| 463 | |
Bram Moolenaar | be094a1 | 2012-02-05 01:18:48 +0100 | [diff] [blame] | 464 | /* |
| 465 | * Discard the contents of the redo buffer and restore the previous redo |
| 466 | * buffer. |
| 467 | */ |
| 468 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 469 | CancelRedo(void) |
Bram Moolenaar | be094a1 | 2012-02-05 01:18:48 +0100 | [diff] [blame] | 470 | { |
| 471 | if (!block_redo) |
| 472 | { |
| 473 | free_buff(&redobuff); |
| 474 | redobuff = old_redobuff; |
Bram Moolenaar | 285e335 | 2018-04-18 23:01:13 +0200 | [diff] [blame] | 475 | old_redobuff.bh_first.b_next = NULL; |
Bram Moolenaar | be094a1 | 2012-02-05 01:18:48 +0100 | [diff] [blame] | 476 | start_stuff(); |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 477 | while (read_readbuffers(TRUE) != NUL) |
Bram Moolenaar | be094a1 | 2012-02-05 01:18:48 +0100 | [diff] [blame] | 478 | ; |
| 479 | } |
| 480 | } |
| 481 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 482 | /* |
| 483 | * Save redobuff and old_redobuff to save_redobuff and save_old_redobuff. |
| 484 | * Used before executing autocommands and user functions. |
| 485 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 486 | void |
Bram Moolenaar | d4863aa | 2017-04-07 19:50:12 +0200 | [diff] [blame] | 487 | saveRedobuff(save_redo_T *save_redo) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 488 | { |
| 489 | char_u *s; |
| 490 | |
Bram Moolenaar | d4863aa | 2017-04-07 19:50:12 +0200 | [diff] [blame] | 491 | save_redo->sr_redobuff = redobuff; |
Bram Moolenaar | 285e335 | 2018-04-18 23:01:13 +0200 | [diff] [blame] | 492 | redobuff.bh_first.b_next = NULL; |
Bram Moolenaar | d4863aa | 2017-04-07 19:50:12 +0200 | [diff] [blame] | 493 | save_redo->sr_old_redobuff = old_redobuff; |
Bram Moolenaar | 285e335 | 2018-04-18 23:01:13 +0200 | [diff] [blame] | 494 | old_redobuff.bh_first.b_next = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 495 | |
Bram Moolenaar | d4863aa | 2017-04-07 19:50:12 +0200 | [diff] [blame] | 496 | /* Make a copy, so that ":normal ." in a function works. */ |
| 497 | s = get_buffcont(&save_redo->sr_redobuff, FALSE); |
| 498 | if (s != NULL) |
| 499 | { |
| 500 | add_buff(&redobuff, s, -1L); |
| 501 | vim_free(s); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 502 | } |
| 503 | } |
| 504 | |
| 505 | /* |
| 506 | * Restore redobuff and old_redobuff from save_redobuff and save_old_redobuff. |
| 507 | * Used after executing autocommands and user functions. |
| 508 | */ |
| 509 | void |
Bram Moolenaar | d4863aa | 2017-04-07 19:50:12 +0200 | [diff] [blame] | 510 | restoreRedobuff(save_redo_T *save_redo) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 511 | { |
Bram Moolenaar | d4863aa | 2017-04-07 19:50:12 +0200 | [diff] [blame] | 512 | free_buff(&redobuff); |
| 513 | redobuff = save_redo->sr_redobuff; |
| 514 | free_buff(&old_redobuff); |
| 515 | old_redobuff = save_redo->sr_old_redobuff; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 516 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 517 | |
| 518 | /* |
| 519 | * Append "s" to the redo buffer. |
| 520 | * K_SPECIAL and CSI should already have been escaped. |
| 521 | */ |
| 522 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 523 | AppendToRedobuff(char_u *s) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 524 | { |
| 525 | if (!block_redo) |
| 526 | add_buff(&redobuff, s, -1L); |
| 527 | } |
| 528 | |
| 529 | /* |
| 530 | * Append to Redo buffer literally, escaping special characters with CTRL-V. |
| 531 | * K_SPECIAL and CSI are escaped as well. |
| 532 | */ |
| 533 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 534 | AppendToRedobuffLit( |
| 535 | char_u *str, |
| 536 | int len) /* length of "str" or -1 for up to the NUL */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 537 | { |
Bram Moolenaar | ebefac6 | 2005-12-28 22:39:57 +0000 | [diff] [blame] | 538 | char_u *s = str; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 539 | int c; |
| 540 | char_u *start; |
| 541 | |
| 542 | if (block_redo) |
| 543 | return; |
| 544 | |
Bram Moolenaar | ebefac6 | 2005-12-28 22:39:57 +0000 | [diff] [blame] | 545 | while (len < 0 ? *s != NUL : s - str < len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 546 | { |
| 547 | /* Put a string of normal characters in the redo buffer (that's |
| 548 | * faster). */ |
| 549 | start = s; |
| 550 | while (*s >= ' ' |
| 551 | #ifndef EBCDIC |
| 552 | && *s < DEL /* EBCDIC: all chars above space are normal */ |
| 553 | #endif |
Bram Moolenaar | ebefac6 | 2005-12-28 22:39:57 +0000 | [diff] [blame] | 554 | && (len < 0 || s - str < len)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 555 | ++s; |
| 556 | |
| 557 | /* Don't put '0' or '^' as last character, just in case a CTRL-D is |
| 558 | * typed next. */ |
| 559 | if (*s == NUL && (s[-1] == '0' || s[-1] == '^')) |
| 560 | --s; |
| 561 | if (s > start) |
| 562 | add_buff(&redobuff, start, (long)(s - start)); |
| 563 | |
Bram Moolenaar | ebefac6 | 2005-12-28 22:39:57 +0000 | [diff] [blame] | 564 | if (*s == NUL || (len >= 0 && s - str >= len)) |
| 565 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 566 | |
Bram Moolenaar | ebefac6 | 2005-12-28 22:39:57 +0000 | [diff] [blame] | 567 | /* Handle a special or multibyte character. */ |
Bram Moolenaar | ebefac6 | 2005-12-28 22:39:57 +0000 | [diff] [blame] | 568 | if (has_mbyte) |
| 569 | /* Handle composing chars separately. */ |
| 570 | c = mb_cptr2char_adv(&s); |
| 571 | else |
Bram Moolenaar | ebefac6 | 2005-12-28 22:39:57 +0000 | [diff] [blame] | 572 | c = *s++; |
| 573 | if (c < ' ' || c == DEL || (*s == NUL && (c == '0' || c == '^'))) |
| 574 | add_char_buff(&redobuff, Ctrl_V); |
| 575 | |
| 576 | /* CTRL-V '0' must be inserted as CTRL-V 048 (EBCDIC: xf0) */ |
| 577 | if (*s == NUL && c == '0') |
| 578 | #ifdef EBCDIC |
| 579 | add_buff(&redobuff, (char_u *)"xf0", 3L); |
| 580 | #else |
| 581 | add_buff(&redobuff, (char_u *)"048", 3L); |
| 582 | #endif |
| 583 | else |
| 584 | add_char_buff(&redobuff, c); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 585 | } |
| 586 | } |
| 587 | |
| 588 | /* |
| 589 | * Append a character to the redo buffer. |
| 590 | * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters. |
| 591 | */ |
| 592 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 593 | AppendCharToRedobuff(int c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 594 | { |
| 595 | if (!block_redo) |
| 596 | add_char_buff(&redobuff, c); |
| 597 | } |
| 598 | |
| 599 | /* |
| 600 | * Append a number to the redo buffer. |
| 601 | */ |
| 602 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 603 | AppendNumberToRedobuff(long n) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 604 | { |
| 605 | if (!block_redo) |
| 606 | add_num_buff(&redobuff, n); |
| 607 | } |
| 608 | |
| 609 | /* |
| 610 | * Append string "s" to the stuff buffer. |
| 611 | * CSI and K_SPECIAL must already have been escaped. |
| 612 | */ |
| 613 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 614 | stuffReadbuff(char_u *s) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 615 | { |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 616 | add_buff(&readbuf1, s, -1L); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 617 | } |
| 618 | |
Bram Moolenaar | 4f5ce33 | 2014-07-30 16:00:58 +0200 | [diff] [blame] | 619 | /* |
| 620 | * Append string "s" to the redo stuff buffer. |
| 621 | * CSI and K_SPECIAL must already have been escaped. |
| 622 | */ |
| 623 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 624 | stuffRedoReadbuff(char_u *s) |
Bram Moolenaar | 4f5ce33 | 2014-07-30 16:00:58 +0200 | [diff] [blame] | 625 | { |
| 626 | add_buff(&readbuf2, s, -1L); |
| 627 | } |
| 628 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 629 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 630 | stuffReadbuffLen(char_u *s, long len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 631 | { |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 632 | add_buff(&readbuf1, s, len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 636 | /* |
| 637 | * Stuff "s" into the stuff buffer, leaving special key codes unmodified and |
| 638 | * escaping other K_SPECIAL and CSI bytes. |
Bram Moolenaar | 877b97b | 2011-04-28 17:30:09 +0200 | [diff] [blame] | 639 | * Change CR, LF and ESC into a space. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 640 | */ |
| 641 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 642 | stuffReadbuffSpec(char_u *s) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 643 | { |
Bram Moolenaar | 877b97b | 2011-04-28 17:30:09 +0200 | [diff] [blame] | 644 | int c; |
| 645 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 646 | while (*s != NUL) |
| 647 | { |
| 648 | if (*s == K_SPECIAL && s[1] != NUL && s[2] != NUL) |
| 649 | { |
| 650 | /* Insert special key literally. */ |
| 651 | stuffReadbuffLen(s, 3L); |
| 652 | s += 3; |
| 653 | } |
| 654 | else |
Bram Moolenaar | 877b97b | 2011-04-28 17:30:09 +0200 | [diff] [blame] | 655 | { |
Bram Moolenaar | 877b97b | 2011-04-28 17:30:09 +0200 | [diff] [blame] | 656 | c = mb_ptr2char_adv(&s); |
Bram Moolenaar | 877b97b | 2011-04-28 17:30:09 +0200 | [diff] [blame] | 657 | if (c == CAR || c == NL || c == ESC) |
| 658 | c = ' '; |
| 659 | stuffcharReadbuff(c); |
| 660 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 661 | } |
| 662 | } |
| 663 | #endif |
| 664 | |
| 665 | /* |
| 666 | * Append a character to the stuff buffer. |
| 667 | * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters. |
| 668 | */ |
| 669 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 670 | stuffcharReadbuff(int c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 671 | { |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 672 | add_char_buff(&readbuf1, c); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | /* |
| 676 | * Append a number to the stuff buffer. |
| 677 | */ |
| 678 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 679 | stuffnumReadbuff(long n) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 680 | { |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 681 | add_num_buff(&readbuf1, n); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | /* |
| 685 | * Read a character from the redo buffer. Translates K_SPECIAL, CSI and |
| 686 | * multibyte characters. |
| 687 | * The redo buffer is left as it is. |
Bram Moolenaar | be094a1 | 2012-02-05 01:18:48 +0100 | [diff] [blame] | 688 | * If init is TRUE, prepare for redo, return FAIL if nothing to redo, OK |
| 689 | * otherwise. |
| 690 | * If old is TRUE, use old_redobuff instead of redobuff. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 691 | */ |
| 692 | static int |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 693 | read_redo(int init, int old_redo) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 694 | { |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 695 | static buffblock_T *bp; |
| 696 | static char_u *p; |
| 697 | int c; |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 698 | int n; |
| 699 | char_u buf[MB_MAXBYTES + 1]; |
| 700 | int i; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 701 | |
| 702 | if (init) |
| 703 | { |
| 704 | if (old_redo) |
Bram Moolenaar | 285e335 | 2018-04-18 23:01:13 +0200 | [diff] [blame] | 705 | bp = old_redobuff.bh_first.b_next; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 706 | else |
Bram Moolenaar | 285e335 | 2018-04-18 23:01:13 +0200 | [diff] [blame] | 707 | bp = redobuff.bh_first.b_next; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 708 | if (bp == NULL) |
| 709 | return FAIL; |
| 710 | p = bp->b_str; |
| 711 | return OK; |
| 712 | } |
| 713 | if ((c = *p) != NUL) |
| 714 | { |
| 715 | /* Reverse the conversion done by add_char_buff() */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 716 | /* For a multi-byte character get all the bytes and return the |
| 717 | * converted character. */ |
| 718 | if (has_mbyte && (c != K_SPECIAL || p[1] == KS_SPECIAL)) |
| 719 | n = MB_BYTE2LEN_CHECK(c); |
| 720 | else |
| 721 | n = 1; |
| 722 | for (i = 0; ; ++i) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 723 | { |
| 724 | if (c == K_SPECIAL) /* special key or escaped K_SPECIAL */ |
| 725 | { |
| 726 | c = TO_SPECIAL(p[1], p[2]); |
| 727 | p += 2; |
| 728 | } |
| 729 | #ifdef FEAT_GUI |
| 730 | if (c == CSI) /* escaped CSI */ |
| 731 | p += 2; |
| 732 | #endif |
| 733 | if (*++p == NUL && bp->b_next != NULL) |
| 734 | { |
| 735 | bp = bp->b_next; |
| 736 | p = bp->b_str; |
| 737 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 738 | buf[i] = c; |
| 739 | if (i == n - 1) /* last byte of a character */ |
| 740 | { |
| 741 | if (n != 1) |
| 742 | c = (*mb_ptr2char)(buf); |
| 743 | break; |
| 744 | } |
| 745 | c = *p; |
| 746 | if (c == NUL) /* cannot happen? */ |
| 747 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 748 | } |
| 749 | } |
| 750 | |
| 751 | return c; |
| 752 | } |
| 753 | |
| 754 | /* |
| 755 | * Copy the rest of the redo buffer into the stuff buffer (in a slow way). |
| 756 | * If old_redo is TRUE, use old_redobuff instead of redobuff. |
| 757 | * The escaped K_SPECIAL and CSI are copied without translation. |
| 758 | */ |
| 759 | static void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 760 | copy_redo(int old_redo) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 761 | { |
| 762 | int c; |
| 763 | |
| 764 | while ((c = read_redo(FALSE, old_redo)) != NUL) |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 765 | add_char_buff(&readbuf2, c); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 766 | } |
| 767 | |
| 768 | /* |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 769 | * Stuff the redo buffer into readbuf2. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 770 | * Insert the redo count into the command. |
| 771 | * If "old_redo" is TRUE, the last but one command is repeated |
| 772 | * instead of the last command (inserting text). This is used for |
| 773 | * CTRL-O <.> in insert mode |
| 774 | * |
| 775 | * return FAIL for failure, OK otherwise |
| 776 | */ |
| 777 | int |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 778 | start_redo(long count, int old_redo) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 779 | { |
| 780 | int c; |
| 781 | |
| 782 | /* init the pointers; return if nothing to redo */ |
| 783 | if (read_redo(TRUE, old_redo) == FAIL) |
| 784 | return FAIL; |
| 785 | |
| 786 | c = read_redo(FALSE, old_redo); |
| 787 | |
| 788 | /* copy the buffer name, if present */ |
| 789 | if (c == '"') |
| 790 | { |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 791 | add_buff(&readbuf2, (char_u *)"\"", 1L); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 792 | c = read_redo(FALSE, old_redo); |
| 793 | |
| 794 | /* if a numbered buffer is used, increment the number */ |
| 795 | if (c >= '1' && c < '9') |
| 796 | ++c; |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 797 | add_char_buff(&readbuf2, c); |
Bram Moolenaar | 833093b | 2018-05-23 21:53:52 +0200 | [diff] [blame] | 798 | |
| 799 | /* the expression register should be re-evaluated */ |
| 800 | if (c == '=') |
| 801 | { |
| 802 | add_char_buff(&readbuf2, CAR); |
| 803 | cmd_silent = TRUE; |
| 804 | } |
| 805 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 806 | c = read_redo(FALSE, old_redo); |
| 807 | } |
| 808 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 809 | if (c == 'v') /* redo Visual */ |
| 810 | { |
| 811 | VIsual = curwin->w_cursor; |
| 812 | VIsual_active = TRUE; |
| 813 | VIsual_select = FALSE; |
| 814 | VIsual_reselect = TRUE; |
| 815 | redo_VIsual_busy = TRUE; |
| 816 | c = read_redo(FALSE, old_redo); |
| 817 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 818 | |
| 819 | /* try to enter the count (in place of a previous count) */ |
| 820 | if (count) |
| 821 | { |
| 822 | while (VIM_ISDIGIT(c)) /* skip "old" count */ |
| 823 | c = read_redo(FALSE, old_redo); |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 824 | add_num_buff(&readbuf2, count); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 825 | } |
| 826 | |
| 827 | /* copy from the redo buffer into the stuff buffer */ |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 828 | add_char_buff(&readbuf2, c); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 829 | copy_redo(old_redo); |
| 830 | return OK; |
| 831 | } |
| 832 | |
| 833 | /* |
| 834 | * Repeat the last insert (R, o, O, a, A, i or I command) by stuffing |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 835 | * the redo buffer into readbuf2. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 836 | * return FAIL for failure, OK otherwise |
| 837 | */ |
| 838 | int |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 839 | start_redo_ins(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 840 | { |
| 841 | int c; |
| 842 | |
| 843 | if (read_redo(TRUE, FALSE) == FAIL) |
| 844 | return FAIL; |
| 845 | start_stuff(); |
| 846 | |
| 847 | /* skip the count and the command character */ |
| 848 | while ((c = read_redo(FALSE, FALSE)) != NUL) |
| 849 | { |
| 850 | if (vim_strchr((char_u *)"AaIiRrOo", c) != NULL) |
| 851 | { |
| 852 | if (c == 'O' || c == 'o') |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 853 | add_buff(&readbuf2, NL_STR, -1L); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 854 | break; |
| 855 | } |
| 856 | } |
| 857 | |
| 858 | /* copy the typed text from the redo buffer into the stuff buffer */ |
| 859 | copy_redo(FALSE); |
| 860 | block_redo = TRUE; |
| 861 | return OK; |
| 862 | } |
| 863 | |
| 864 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 865 | stop_redo_ins(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 866 | { |
| 867 | block_redo = FALSE; |
| 868 | } |
| 869 | |
| 870 | /* |
| 871 | * Initialize typebuf.tb_buf to point to typebuf_init. |
| 872 | * alloc() cannot be used here: In out-of-memory situations it would |
| 873 | * be impossible to type anything. |
| 874 | */ |
| 875 | static void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 876 | init_typebuf(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 877 | { |
| 878 | if (typebuf.tb_buf == NULL) |
| 879 | { |
| 880 | typebuf.tb_buf = typebuf_init; |
| 881 | typebuf.tb_noremap = noremapbuf_init; |
| 882 | typebuf.tb_buflen = TYPELEN_INIT; |
| 883 | typebuf.tb_len = 0; |
Bram Moolenaar | d34f9b1 | 2017-04-08 18:41:13 +0200 | [diff] [blame] | 884 | typebuf.tb_off = MAXMAPLEN + 4; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 885 | typebuf.tb_change_cnt = 1; |
| 886 | } |
| 887 | } |
| 888 | |
| 889 | /* |
Bram Moolenaar | b66bab3 | 2019-08-01 14:28:24 +0200 | [diff] [blame] | 890 | * Returns TRUE when keys cannot be remapped. |
| 891 | */ |
| 892 | int |
| 893 | noremap_keys(void) |
| 894 | { |
| 895 | return KeyNoremap & (RM_NONE|RM_SCRIPT); |
| 896 | } |
| 897 | |
| 898 | /* |
Bram Moolenaar | e7fedb6 | 2015-12-31 19:07:19 +0100 | [diff] [blame] | 899 | * Insert a string in position 'offset' in the typeahead buffer (for "@r" |
| 900 | * and ":normal" command, vgetorpeek() and check_termcode()). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 901 | * |
| 902 | * If noremap is REMAP_YES, new string can be mapped again. |
| 903 | * If noremap is REMAP_NONE, new string cannot be mapped again. |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 904 | * If noremap is REMAP_SKIP, fist char of new string cannot be mapped again, |
| 905 | * but abbreviations are allowed. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 906 | * If noremap is REMAP_SCRIPT, new string cannot be mapped again, except for |
| 907 | * script-local mappings. |
| 908 | * If noremap is > 0, that many characters of the new string cannot be mapped. |
| 909 | * |
| 910 | * If nottyped is TRUE, the string does not return KeyTyped (don't use when |
| 911 | * offset is non-zero!). |
| 912 | * |
| 913 | * If silent is TRUE, cmd_silent is set when the characters are obtained. |
| 914 | * |
| 915 | * return FAIL for failure, OK otherwise |
| 916 | */ |
| 917 | int |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 918 | ins_typebuf( |
| 919 | char_u *str, |
| 920 | int noremap, |
| 921 | int offset, |
| 922 | int nottyped, |
| 923 | int silent) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 924 | { |
| 925 | char_u *s1, *s2; |
| 926 | int newlen; |
| 927 | int addlen; |
| 928 | int i; |
| 929 | int newoff; |
| 930 | int val; |
| 931 | int nrm; |
| 932 | |
| 933 | init_typebuf(); |
| 934 | if (++typebuf.tb_change_cnt == 0) |
| 935 | typebuf.tb_change_cnt = 1; |
Bram Moolenaar | d103ee7 | 2019-09-18 21:15:31 +0200 | [diff] [blame] | 936 | state_no_longer_safe("ins_typebuf()"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 937 | |
| 938 | addlen = (int)STRLEN(str); |
Bram Moolenaar | 4e86cba | 2007-05-06 11:56:32 +0000 | [diff] [blame] | 939 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 940 | if (offset == 0 && addlen <= typebuf.tb_off) |
| 941 | { |
Bram Moolenaar | caa55b6 | 2017-01-10 13:51:09 +0100 | [diff] [blame] | 942 | /* |
| 943 | * Easy case: there is room in front of typebuf.tb_buf[typebuf.tb_off] |
| 944 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 945 | typebuf.tb_off -= addlen; |
| 946 | mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen); |
| 947 | } |
Bram Moolenaar | d34f9b1 | 2017-04-08 18:41:13 +0200 | [diff] [blame] | 948 | else if (typebuf.tb_len == 0 && typebuf.tb_buflen |
| 949 | >= addlen + 3 * (MAXMAPLEN + 4)) |
| 950 | { |
| 951 | /* |
| 952 | * Buffer is empty and string fits in the existing buffer. |
| 953 | * Leave some space before and after, if possible. |
| 954 | */ |
| 955 | typebuf.tb_off = (typebuf.tb_buflen - addlen - 3 * (MAXMAPLEN + 4)) / 2; |
| 956 | mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen); |
| 957 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 958 | else |
| 959 | { |
Bram Moolenaar | caa55b6 | 2017-01-10 13:51:09 +0100 | [diff] [blame] | 960 | /* |
| 961 | * Need to allocate a new buffer. |
Bram Moolenaar | d34f9b1 | 2017-04-08 18:41:13 +0200 | [diff] [blame] | 962 | * In typebuf.tb_buf there must always be room for 3 * (MAXMAPLEN + 4) |
Bram Moolenaar | caa55b6 | 2017-01-10 13:51:09 +0100 | [diff] [blame] | 963 | * characters. We add some extra room to avoid having to allocate too |
| 964 | * often. |
| 965 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 966 | newoff = MAXMAPLEN + 4; |
| 967 | newlen = typebuf.tb_len + addlen + newoff + 4 * (MAXMAPLEN + 4); |
| 968 | if (newlen < 0) /* string is getting too long */ |
| 969 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 970 | emsg(_(e_toocompl)); /* also calls flush_buffers */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 971 | setcursor(); |
| 972 | return FAIL; |
| 973 | } |
| 974 | s1 = alloc(newlen); |
| 975 | if (s1 == NULL) /* out of memory */ |
| 976 | return FAIL; |
| 977 | s2 = alloc(newlen); |
| 978 | if (s2 == NULL) /* out of memory */ |
| 979 | { |
| 980 | vim_free(s1); |
| 981 | return FAIL; |
| 982 | } |
| 983 | typebuf.tb_buflen = newlen; |
| 984 | |
| 985 | /* copy the old chars, before the insertion point */ |
| 986 | mch_memmove(s1 + newoff, typebuf.tb_buf + typebuf.tb_off, |
| 987 | (size_t)offset); |
| 988 | /* copy the new chars */ |
| 989 | mch_memmove(s1 + newoff + offset, str, (size_t)addlen); |
| 990 | /* copy the old chars, after the insertion point, including the NUL at |
| 991 | * the end */ |
| 992 | mch_memmove(s1 + newoff + offset + addlen, |
| 993 | typebuf.tb_buf + typebuf.tb_off + offset, |
| 994 | (size_t)(typebuf.tb_len - offset + 1)); |
| 995 | if (typebuf.tb_buf != typebuf_init) |
| 996 | vim_free(typebuf.tb_buf); |
| 997 | typebuf.tb_buf = s1; |
| 998 | |
| 999 | mch_memmove(s2 + newoff, typebuf.tb_noremap + typebuf.tb_off, |
| 1000 | (size_t)offset); |
| 1001 | mch_memmove(s2 + newoff + offset + addlen, |
| 1002 | typebuf.tb_noremap + typebuf.tb_off + offset, |
| 1003 | (size_t)(typebuf.tb_len - offset)); |
| 1004 | if (typebuf.tb_noremap != noremapbuf_init) |
| 1005 | vim_free(typebuf.tb_noremap); |
| 1006 | typebuf.tb_noremap = s2; |
| 1007 | |
| 1008 | typebuf.tb_off = newoff; |
| 1009 | } |
| 1010 | typebuf.tb_len += addlen; |
| 1011 | |
| 1012 | /* If noremap == REMAP_SCRIPT: do remap script-local mappings. */ |
| 1013 | if (noremap == REMAP_SCRIPT) |
| 1014 | val = RM_SCRIPT; |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 1015 | else if (noremap == REMAP_SKIP) |
| 1016 | val = RM_ABBR; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1017 | else |
| 1018 | val = RM_NONE; |
| 1019 | |
| 1020 | /* |
| 1021 | * Adjust typebuf.tb_noremap[] for the new characters: |
| 1022 | * If noremap == REMAP_NONE or REMAP_SCRIPT: new characters are |
| 1023 | * (sometimes) not remappable |
| 1024 | * If noremap == REMAP_YES: all the new characters are mappable |
| 1025 | * If noremap > 0: "noremap" characters are not remappable, the rest |
| 1026 | * mappable |
| 1027 | */ |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 1028 | if (noremap == REMAP_SKIP) |
| 1029 | nrm = 1; |
| 1030 | else if (noremap < 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1031 | nrm = addlen; |
| 1032 | else |
| 1033 | nrm = noremap; |
| 1034 | for (i = 0; i < addlen; ++i) |
| 1035 | typebuf.tb_noremap[typebuf.tb_off + i + offset] = |
| 1036 | (--nrm >= 0) ? val : RM_YES; |
| 1037 | |
| 1038 | /* tb_maplen and tb_silent only remember the length of mapped and/or |
| 1039 | * silent mappings at the start of the buffer, assuming that a mapped |
| 1040 | * sequence doesn't result in typed characters. */ |
| 1041 | if (nottyped || typebuf.tb_maplen > offset) |
| 1042 | typebuf.tb_maplen += addlen; |
| 1043 | if (silent || typebuf.tb_silent > offset) |
| 1044 | { |
| 1045 | typebuf.tb_silent += addlen; |
| 1046 | cmd_silent = TRUE; |
| 1047 | } |
| 1048 | if (typebuf.tb_no_abbr_cnt && offset == 0) /* and not used for abbrev.s */ |
| 1049 | typebuf.tb_no_abbr_cnt += addlen; |
| 1050 | |
| 1051 | return OK; |
| 1052 | } |
| 1053 | |
| 1054 | /* |
Bram Moolenaar | d8fc5c0 | 2006-04-29 21:55:22 +0000 | [diff] [blame] | 1055 | * Put character "c" back into the typeahead buffer. |
| 1056 | * Can be used for a character obtained by vgetc() that needs to be put back. |
Bram Moolenaar | cf8e7d1 | 2006-12-05 20:43:17 +0000 | [diff] [blame] | 1057 | * Uses cmd_silent, KeyTyped and KeyNoremap to restore the flags belonging to |
| 1058 | * the char. |
Bram Moolenaar | d8fc5c0 | 2006-04-29 21:55:22 +0000 | [diff] [blame] | 1059 | */ |
| 1060 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 1061 | ins_char_typebuf(int c) |
Bram Moolenaar | d8fc5c0 | 2006-04-29 21:55:22 +0000 | [diff] [blame] | 1062 | { |
Bram Moolenaar | 9a920d8 | 2012-06-01 15:21:02 +0200 | [diff] [blame] | 1063 | char_u buf[MB_MAXBYTES + 1]; |
Bram Moolenaar | d8fc5c0 | 2006-04-29 21:55:22 +0000 | [diff] [blame] | 1064 | if (IS_SPECIAL(c)) |
| 1065 | { |
| 1066 | buf[0] = K_SPECIAL; |
| 1067 | buf[1] = K_SECOND(c); |
| 1068 | buf[2] = K_THIRD(c); |
| 1069 | buf[3] = NUL; |
| 1070 | } |
| 1071 | else |
Bram Moolenaar | d8fc5c0 | 2006-04-29 21:55:22 +0000 | [diff] [blame] | 1072 | buf[(*mb_char2bytes)(c, buf)] = NUL; |
Bram Moolenaar | cf8e7d1 | 2006-12-05 20:43:17 +0000 | [diff] [blame] | 1073 | (void)ins_typebuf(buf, KeyNoremap, 0, !KeyTyped, cmd_silent); |
Bram Moolenaar | d8fc5c0 | 2006-04-29 21:55:22 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
| 1076 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1077 | * Return TRUE if the typeahead buffer was changed (while waiting for a |
Bram Moolenaar | 4a85b41 | 2006-04-23 22:40:29 +0000 | [diff] [blame] | 1078 | * character to arrive). Happens when a message was received from a client or |
Bram Moolenaar | f9393ef | 2006-04-24 19:47:27 +0000 | [diff] [blame] | 1079 | * from feedkeys(). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1080 | * But check in a more generic way to avoid trouble: When "typebuf.tb_buf" |
| 1081 | * changed it was reallocated and the old pointer can no longer be used. |
| 1082 | * Or "typebuf.tb_off" may have been changed and we would overwrite characters |
| 1083 | * that was just added. |
| 1084 | */ |
| 1085 | int |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 1086 | typebuf_changed( |
| 1087 | int tb_change_cnt) /* old value of typebuf.tb_change_cnt */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1088 | { |
| 1089 | return (tb_change_cnt != 0 && (typebuf.tb_change_cnt != tb_change_cnt |
Bram Moolenaar | 4a85b41 | 2006-04-23 22:40:29 +0000 | [diff] [blame] | 1090 | #if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL) |
| 1091 | || typebuf_was_filled |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1092 | #endif |
| 1093 | )); |
| 1094 | } |
| 1095 | |
| 1096 | /* |
| 1097 | * Return TRUE if there are no characters in the typeahead buffer that have |
| 1098 | * not been typed (result from a mapping or come from ":normal"). |
| 1099 | */ |
| 1100 | int |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 1101 | typebuf_typed(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1102 | { |
| 1103 | return typebuf.tb_maplen == 0; |
| 1104 | } |
| 1105 | |
| 1106 | /* |
| 1107 | * Return the number of characters that are mapped (or not typed). |
| 1108 | */ |
| 1109 | int |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 1110 | typebuf_maplen(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1111 | { |
| 1112 | return typebuf.tb_maplen; |
| 1113 | } |
| 1114 | |
| 1115 | /* |
| 1116 | * remove "len" characters from typebuf.tb_buf[typebuf.tb_off + offset] |
| 1117 | */ |
| 1118 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 1119 | del_typebuf(int len, int offset) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1120 | { |
| 1121 | int i; |
| 1122 | |
| 1123 | if (len == 0) |
| 1124 | return; /* nothing to do */ |
| 1125 | |
| 1126 | typebuf.tb_len -= len; |
| 1127 | |
| 1128 | /* |
| 1129 | * Easy case: Just increase typebuf.tb_off. |
| 1130 | */ |
| 1131 | if (offset == 0 && typebuf.tb_buflen - (typebuf.tb_off + len) |
| 1132 | >= 3 * MAXMAPLEN + 3) |
| 1133 | typebuf.tb_off += len; |
| 1134 | /* |
| 1135 | * Have to move the characters in typebuf.tb_buf[] and typebuf.tb_noremap[] |
| 1136 | */ |
| 1137 | else |
| 1138 | { |
| 1139 | i = typebuf.tb_off + offset; |
| 1140 | /* |
| 1141 | * Leave some extra room at the end to avoid reallocation. |
| 1142 | */ |
| 1143 | if (typebuf.tb_off > MAXMAPLEN) |
| 1144 | { |
| 1145 | mch_memmove(typebuf.tb_buf + MAXMAPLEN, |
| 1146 | typebuf.tb_buf + typebuf.tb_off, (size_t)offset); |
| 1147 | mch_memmove(typebuf.tb_noremap + MAXMAPLEN, |
| 1148 | typebuf.tb_noremap + typebuf.tb_off, (size_t)offset); |
| 1149 | typebuf.tb_off = MAXMAPLEN; |
| 1150 | } |
| 1151 | /* adjust typebuf.tb_buf (include the NUL at the end) */ |
| 1152 | mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, |
| 1153 | typebuf.tb_buf + i + len, |
| 1154 | (size_t)(typebuf.tb_len - offset + 1)); |
| 1155 | /* adjust typebuf.tb_noremap[] */ |
| 1156 | mch_memmove(typebuf.tb_noremap + typebuf.tb_off + offset, |
| 1157 | typebuf.tb_noremap + i + len, |
| 1158 | (size_t)(typebuf.tb_len - offset)); |
| 1159 | } |
| 1160 | |
| 1161 | if (typebuf.tb_maplen > offset) /* adjust tb_maplen */ |
| 1162 | { |
| 1163 | if (typebuf.tb_maplen < offset + len) |
| 1164 | typebuf.tb_maplen = offset; |
| 1165 | else |
| 1166 | typebuf.tb_maplen -= len; |
| 1167 | } |
| 1168 | if (typebuf.tb_silent > offset) /* adjust tb_silent */ |
| 1169 | { |
| 1170 | if (typebuf.tb_silent < offset + len) |
| 1171 | typebuf.tb_silent = offset; |
| 1172 | else |
| 1173 | typebuf.tb_silent -= len; |
| 1174 | } |
| 1175 | if (typebuf.tb_no_abbr_cnt > offset) /* adjust tb_no_abbr_cnt */ |
| 1176 | { |
| 1177 | if (typebuf.tb_no_abbr_cnt < offset + len) |
| 1178 | typebuf.tb_no_abbr_cnt = offset; |
| 1179 | else |
| 1180 | typebuf.tb_no_abbr_cnt -= len; |
| 1181 | } |
| 1182 | |
Bram Moolenaar | 4a85b41 | 2006-04-23 22:40:29 +0000 | [diff] [blame] | 1183 | #if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL) |
Bram Moolenaar | f9393ef | 2006-04-24 19:47:27 +0000 | [diff] [blame] | 1184 | /* Reset the flag that text received from a client or from feedkeys() |
Bram Moolenaar | 4a85b41 | 2006-04-23 22:40:29 +0000 | [diff] [blame] | 1185 | * was inserted in the typeahead buffer. */ |
| 1186 | typebuf_was_filled = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1187 | #endif |
| 1188 | if (++typebuf.tb_change_cnt == 0) |
| 1189 | typebuf.tb_change_cnt = 1; |
| 1190 | } |
| 1191 | |
| 1192 | /* |
| 1193 | * Write typed characters to script file. |
| 1194 | * If recording is on put the character in the recordbuffer. |
| 1195 | */ |
| 1196 | static void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 1197 | gotchars(char_u *chars, int len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1198 | { |
Bram Moolenaar | 972bfdd | 2018-07-03 14:48:15 +0200 | [diff] [blame] | 1199 | char_u *s = chars; |
| 1200 | int i; |
| 1201 | static char_u buf[4]; |
| 1202 | static int buflen = 0; |
| 1203 | int todo = len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1204 | |
Bram Moolenaar | 4e86cba | 2007-05-06 11:56:32 +0000 | [diff] [blame] | 1205 | while (todo--) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1206 | { |
Bram Moolenaar | 972bfdd | 2018-07-03 14:48:15 +0200 | [diff] [blame] | 1207 | buf[buflen++] = *s++; |
| 1208 | |
| 1209 | // When receiving a special key sequence, store it until we have all |
| 1210 | // the bytes and we can decide what to do with it. |
| 1211 | if (buflen == 1 && buf[0] == K_SPECIAL) |
| 1212 | continue; |
| 1213 | if (buflen == 2) |
| 1214 | continue; |
| 1215 | if (buflen == 3 && buf[1] == KS_EXTRA |
| 1216 | && (buf[2] == KE_FOCUSGAINED || buf[2] == KE_FOCUSLOST)) |
| 1217 | { |
| 1218 | // Drop K_FOCUSGAINED and K_FOCUSLOST, they are not useful in a |
| 1219 | // recording. |
| 1220 | buflen = 0; |
| 1221 | continue; |
| 1222 | } |
| 1223 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1224 | /* Handle one byte at a time; no translation to be done. */ |
Bram Moolenaar | 972bfdd | 2018-07-03 14:48:15 +0200 | [diff] [blame] | 1225 | for (i = 0; i < buflen; ++i) |
| 1226 | updatescript(buf[i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1227 | |
Bram Moolenaar | 0b6d911 | 2018-05-22 20:35:17 +0200 | [diff] [blame] | 1228 | if (reg_recording != 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1229 | { |
Bram Moolenaar | 972bfdd | 2018-07-03 14:48:15 +0200 | [diff] [blame] | 1230 | buf[buflen] = NUL; |
| 1231 | add_buff(&recordbuff, buf, (long)buflen); |
| 1232 | /* remember how many chars were last recorded */ |
| 1233 | last_recorded_len += buflen; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1234 | } |
Bram Moolenaar | 972bfdd | 2018-07-03 14:48:15 +0200 | [diff] [blame] | 1235 | buflen = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1236 | } |
| 1237 | may_sync_undo(); |
| 1238 | |
| 1239 | #ifdef FEAT_EVAL |
| 1240 | /* output "debug mode" message next time in debug mode */ |
| 1241 | debug_did_msg = FALSE; |
| 1242 | #endif |
| 1243 | |
| 1244 | /* Since characters have been typed, consider the following to be in |
| 1245 | * another mapping. Search string will be kept in history. */ |
| 1246 | ++maptick; |
| 1247 | } |
| 1248 | |
| 1249 | /* |
| 1250 | * Sync undo. Called when typed characters are obtained from the typeahead |
| 1251 | * buffer, or when a menu is used. |
| 1252 | * Do not sync: |
| 1253 | * - In Insert mode, unless cursor key has been used. |
| 1254 | * - While reading a script file. |
| 1255 | * - When no_u_sync is non-zero. |
| 1256 | */ |
| 1257 | static void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 1258 | may_sync_undo(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1259 | { |
| 1260 | if ((!(State & (INSERT + CMDLINE)) || arrow_used) |
Bram Moolenaar | 779b74b | 2006-04-10 14:55:34 +0000 | [diff] [blame] | 1261 | && scriptin[curscript] == NULL) |
| 1262 | u_sync(FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1263 | } |
| 1264 | |
| 1265 | /* |
| 1266 | * Make "typebuf" empty and allocate new buffers. |
| 1267 | * Returns FAIL when out of memory. |
| 1268 | */ |
Bram Moolenaar | 5843f5f | 2019-08-20 20:13:45 +0200 | [diff] [blame] | 1269 | static int |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 1270 | alloc_typebuf(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1271 | { |
| 1272 | typebuf.tb_buf = alloc(TYPELEN_INIT); |
| 1273 | typebuf.tb_noremap = alloc(TYPELEN_INIT); |
| 1274 | if (typebuf.tb_buf == NULL || typebuf.tb_noremap == NULL) |
| 1275 | { |
| 1276 | free_typebuf(); |
| 1277 | return FAIL; |
| 1278 | } |
| 1279 | typebuf.tb_buflen = TYPELEN_INIT; |
Bram Moolenaar | d34f9b1 | 2017-04-08 18:41:13 +0200 | [diff] [blame] | 1280 | typebuf.tb_off = MAXMAPLEN + 4; /* can insert without realloc */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1281 | typebuf.tb_len = 0; |
| 1282 | typebuf.tb_maplen = 0; |
| 1283 | typebuf.tb_silent = 0; |
| 1284 | typebuf.tb_no_abbr_cnt = 0; |
| 1285 | if (++typebuf.tb_change_cnt == 0) |
| 1286 | typebuf.tb_change_cnt = 1; |
| 1287 | return OK; |
| 1288 | } |
| 1289 | |
| 1290 | /* |
| 1291 | * Free the buffers of "typebuf". |
| 1292 | */ |
Bram Moolenaar | 5843f5f | 2019-08-20 20:13:45 +0200 | [diff] [blame] | 1293 | static void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 1294 | free_typebuf(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1295 | { |
Bram Moolenaar | ee3f7a5 | 2008-01-01 13:17:56 +0000 | [diff] [blame] | 1296 | if (typebuf.tb_buf == typebuf_init) |
Bram Moolenaar | 95f0960 | 2016-11-10 20:01:45 +0100 | [diff] [blame] | 1297 | internal_error("Free typebuf 1"); |
Bram Moolenaar | ee3f7a5 | 2008-01-01 13:17:56 +0000 | [diff] [blame] | 1298 | else |
Bram Moolenaar | 0f1c670 | 2019-09-28 15:24:00 +0200 | [diff] [blame] | 1299 | VIM_CLEAR(typebuf.tb_buf); |
Bram Moolenaar | f6f95d9 | 2009-11-11 15:23:37 +0000 | [diff] [blame] | 1300 | if (typebuf.tb_noremap == noremapbuf_init) |
Bram Moolenaar | 95f0960 | 2016-11-10 20:01:45 +0100 | [diff] [blame] | 1301 | internal_error("Free typebuf 2"); |
Bram Moolenaar | ee3f7a5 | 2008-01-01 13:17:56 +0000 | [diff] [blame] | 1302 | else |
Bram Moolenaar | 0f1c670 | 2019-09-28 15:24:00 +0200 | [diff] [blame] | 1303 | VIM_CLEAR(typebuf.tb_noremap); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1304 | } |
| 1305 | |
| 1306 | /* |
| 1307 | * When doing ":so! file", the current typeahead needs to be saved, and |
| 1308 | * restored when "file" has been read completely. |
| 1309 | */ |
| 1310 | static typebuf_T saved_typebuf[NSCRIPT]; |
| 1311 | |
| 1312 | int |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 1313 | save_typebuf(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1314 | { |
| 1315 | init_typebuf(); |
| 1316 | saved_typebuf[curscript] = typebuf; |
| 1317 | /* If out of memory: restore typebuf and close file. */ |
| 1318 | if (alloc_typebuf() == FAIL) |
| 1319 | { |
| 1320 | closescript(); |
| 1321 | return FAIL; |
| 1322 | } |
| 1323 | return OK; |
| 1324 | } |
| 1325 | |
Bram Moolenaar | 13df0fe | 2009-07-09 16:24:19 +0000 | [diff] [blame] | 1326 | static int old_char = -1; /* character put back by vungetc() */ |
| 1327 | static int old_mod_mask; /* mod_mask for ungotten character */ |
Bram Moolenaar | b897871 | 2013-03-16 21:42:16 +0100 | [diff] [blame] | 1328 | static int old_mouse_row; /* mouse_row related to old_char */ |
| 1329 | static int old_mouse_col; /* mouse_col related to old_char */ |
Bram Moolenaar | 13df0fe | 2009-07-09 16:24:19 +0000 | [diff] [blame] | 1330 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1331 | /* |
| 1332 | * Save all three kinds of typeahead, so that the user must type at a prompt. |
| 1333 | */ |
| 1334 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 1335 | save_typeahead(tasave_T *tp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1336 | { |
| 1337 | tp->save_typebuf = typebuf; |
| 1338 | tp->typebuf_valid = (alloc_typebuf() == OK); |
| 1339 | if (!tp->typebuf_valid) |
| 1340 | typebuf = tp->save_typebuf; |
| 1341 | |
Bram Moolenaar | 13df0fe | 2009-07-09 16:24:19 +0000 | [diff] [blame] | 1342 | tp->old_char = old_char; |
| 1343 | tp->old_mod_mask = old_mod_mask; |
| 1344 | old_char = -1; |
| 1345 | |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 1346 | tp->save_readbuf1 = readbuf1; |
Bram Moolenaar | 285e335 | 2018-04-18 23:01:13 +0200 | [diff] [blame] | 1347 | readbuf1.bh_first.b_next = NULL; |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 1348 | tp->save_readbuf2 = readbuf2; |
Bram Moolenaar | 285e335 | 2018-04-18 23:01:13 +0200 | [diff] [blame] | 1349 | readbuf2.bh_first.b_next = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1350 | # ifdef USE_INPUT_BUF |
| 1351 | tp->save_inputbuf = get_input_buf(); |
| 1352 | # endif |
| 1353 | } |
| 1354 | |
| 1355 | /* |
| 1356 | * Restore the typeahead to what it was before calling save_typeahead(). |
| 1357 | * The allocated memory is freed, can only be called once! |
| 1358 | */ |
| 1359 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 1360 | restore_typeahead(tasave_T *tp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1361 | { |
| 1362 | if (tp->typebuf_valid) |
| 1363 | { |
| 1364 | free_typebuf(); |
| 1365 | typebuf = tp->save_typebuf; |
| 1366 | } |
| 1367 | |
Bram Moolenaar | 13df0fe | 2009-07-09 16:24:19 +0000 | [diff] [blame] | 1368 | old_char = tp->old_char; |
| 1369 | old_mod_mask = tp->old_mod_mask; |
| 1370 | |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 1371 | free_buff(&readbuf1); |
| 1372 | readbuf1 = tp->save_readbuf1; |
| 1373 | free_buff(&readbuf2); |
| 1374 | readbuf2 = tp->save_readbuf2; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1375 | # ifdef USE_INPUT_BUF |
| 1376 | set_input_buf(tp->save_inputbuf); |
| 1377 | # endif |
| 1378 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1379 | |
| 1380 | /* |
| 1381 | * Open a new script file for the ":source!" command. |
| 1382 | */ |
| 1383 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 1384 | openscript( |
| 1385 | char_u *name, |
| 1386 | int directly) /* when TRUE execute directly */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1387 | { |
| 1388 | if (curscript + 1 == NSCRIPT) |
| 1389 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 1390 | emsg(_(e_nesting)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1391 | return; |
| 1392 | } |
Bram Moolenaar | 5357552 | 2019-05-22 22:38:25 +0200 | [diff] [blame] | 1393 | |
| 1394 | // Disallow sourcing a file in the sandbox, the commands would be executed |
| 1395 | // later, possibly outside of the sandbox. |
| 1396 | if (check_secure()) |
| 1397 | return; |
| 1398 | |
Bram Moolenaar | ee3f7a5 | 2008-01-01 13:17:56 +0000 | [diff] [blame] | 1399 | #ifdef FEAT_EVAL |
| 1400 | if (ignore_script) |
| 1401 | /* Not reading from script, also don't open one. Warning message? */ |
| 1402 | return; |
| 1403 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1404 | |
| 1405 | if (scriptin[curscript] != NULL) /* already reading script */ |
| 1406 | ++curscript; |
| 1407 | /* use NameBuff for expanded name */ |
| 1408 | expand_env(name, NameBuff, MAXPATHL); |
| 1409 | if ((scriptin[curscript] = mch_fopen((char *)NameBuff, READBIN)) == NULL) |
| 1410 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 1411 | semsg(_(e_notopen), name); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1412 | if (curscript) |
| 1413 | --curscript; |
| 1414 | return; |
| 1415 | } |
| 1416 | if (save_typebuf() == FAIL) |
| 1417 | return; |
| 1418 | |
| 1419 | /* |
| 1420 | * Execute the commands from the file right now when using ":source!" |
| 1421 | * after ":global" or ":argdo" or in a loop. Also when another command |
| 1422 | * follows. This means the display won't be updated. Don't do this |
| 1423 | * always, "make test" would fail. |
| 1424 | */ |
| 1425 | if (directly) |
| 1426 | { |
| 1427 | oparg_T oa; |
| 1428 | int oldcurscript; |
| 1429 | int save_State = State; |
| 1430 | int save_restart_edit = restart_edit; |
| 1431 | int save_insertmode = p_im; |
| 1432 | int save_finish_op = finish_op; |
| 1433 | int save_msg_scroll = msg_scroll; |
| 1434 | |
| 1435 | State = NORMAL; |
| 1436 | msg_scroll = FALSE; /* no msg scrolling in Normal mode */ |
| 1437 | restart_edit = 0; /* don't go to Insert mode */ |
| 1438 | p_im = FALSE; /* don't use 'insertmode' */ |
| 1439 | clear_oparg(&oa); |
| 1440 | finish_op = FALSE; |
| 1441 | |
| 1442 | oldcurscript = curscript; |
| 1443 | do |
| 1444 | { |
Bram Moolenaar | 386b43e | 2019-05-19 21:57:11 +0200 | [diff] [blame] | 1445 | update_topline_cursor(); // update cursor position and topline |
| 1446 | normal_cmd(&oa, FALSE); // execute one command |
| 1447 | vpeekc(); // check for end of file |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1448 | } |
| 1449 | while (scriptin[oldcurscript] != NULL); |
| 1450 | |
| 1451 | State = save_State; |
| 1452 | msg_scroll = save_msg_scroll; |
| 1453 | restart_edit = save_restart_edit; |
| 1454 | p_im = save_insertmode; |
| 1455 | finish_op = save_finish_op; |
| 1456 | } |
| 1457 | } |
| 1458 | |
| 1459 | /* |
| 1460 | * Close the currently active input script. |
| 1461 | */ |
| 1462 | static void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 1463 | closescript(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1464 | { |
| 1465 | free_typebuf(); |
| 1466 | typebuf = saved_typebuf[curscript]; |
| 1467 | |
| 1468 | fclose(scriptin[curscript]); |
| 1469 | scriptin[curscript] = NULL; |
| 1470 | if (curscript > 0) |
| 1471 | --curscript; |
| 1472 | } |
| 1473 | |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 1474 | #if defined(EXITFREE) || defined(PROTO) |
| 1475 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 1476 | close_all_scripts(void) |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 1477 | { |
| 1478 | while (scriptin[0] != NULL) |
| 1479 | closescript(); |
| 1480 | } |
| 1481 | #endif |
| 1482 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1483 | /* |
| 1484 | * Return TRUE when reading keys from a script file. |
| 1485 | */ |
| 1486 | int |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 1487 | using_script(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1488 | { |
| 1489 | return scriptin[curscript] != NULL; |
| 1490 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1491 | |
| 1492 | /* |
Bram Moolenaar | 238f4fa | 2005-06-27 22:25:50 +0000 | [diff] [blame] | 1493 | * This function is called just before doing a blocking wait. Thus after |
| 1494 | * waiting 'updatetime' for a character to arrive. |
| 1495 | */ |
| 1496 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 1497 | before_blocking(void) |
Bram Moolenaar | 238f4fa | 2005-06-27 22:25:50 +0000 | [diff] [blame] | 1498 | { |
| 1499 | updatescript(0); |
| 1500 | #ifdef FEAT_EVAL |
Bram Moolenaar | 9fecb46 | 2006-09-05 10:59:47 +0000 | [diff] [blame] | 1501 | if (may_garbage_collect) |
Bram Moolenaar | ebf7dfa | 2016-04-14 12:46:51 +0200 | [diff] [blame] | 1502 | garbage_collect(FALSE); |
Bram Moolenaar | 238f4fa | 2005-06-27 22:25:50 +0000 | [diff] [blame] | 1503 | #endif |
| 1504 | } |
| 1505 | |
| 1506 | /* |
Bram Moolenaar | 32aa102 | 2019-11-02 22:54:41 +0100 | [diff] [blame] | 1507 | * updatescript() is called when a character can be written into the script file |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1508 | * or when we have waited some time for a character (c == 0) |
| 1509 | * |
| 1510 | * All the changed memfiles are synced if c == 0 or when the number of typed |
| 1511 | * characters reaches 'updatecount' and 'updatecount' is non-zero. |
| 1512 | */ |
Bram Moolenaar | 5843f5f | 2019-08-20 20:13:45 +0200 | [diff] [blame] | 1513 | static void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 1514 | updatescript(int c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1515 | { |
| 1516 | static int count = 0; |
| 1517 | |
| 1518 | if (c && scriptout) |
| 1519 | putc(c, scriptout); |
| 1520 | if (c == 0 || (p_uc > 0 && ++count >= p_uc)) |
| 1521 | { |
| 1522 | ml_sync_all(c == 0, TRUE); |
| 1523 | count = 0; |
| 1524 | } |
| 1525 | } |
| 1526 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1527 | /* |
| 1528 | * Get the next input character. |
| 1529 | * Can return a special key or a multi-byte character. |
| 1530 | * Can return NUL when called recursively, use safe_vgetc() if that's not |
| 1531 | * wanted. |
| 1532 | * This translates escaped K_SPECIAL and CSI bytes to a K_SPECIAL or CSI byte. |
| 1533 | * Collects the bytes of a multibyte character into the whole character. |
Bram Moolenaar | f6f95d9 | 2009-11-11 15:23:37 +0000 | [diff] [blame] | 1534 | * Returns the modifiers in the global "mod_mask". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1535 | */ |
| 1536 | int |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 1537 | vgetc(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1538 | { |
| 1539 | int c, c2; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1540 | int n; |
Bram Moolenaar | 9a920d8 | 2012-06-01 15:21:02 +0200 | [diff] [blame] | 1541 | char_u buf[MB_MAXBYTES + 1]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1542 | int i; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1543 | |
Bram Moolenaar | 9fecb46 | 2006-09-05 10:59:47 +0000 | [diff] [blame] | 1544 | #ifdef FEAT_EVAL |
| 1545 | /* Do garbage collection when garbagecollect() was called previously and |
| 1546 | * we are now at the toplevel. */ |
| 1547 | if (may_garbage_collect && want_garbage_collect) |
Bram Moolenaar | ebf7dfa | 2016-04-14 12:46:51 +0200 | [diff] [blame] | 1548 | garbage_collect(FALSE); |
Bram Moolenaar | 9fecb46 | 2006-09-05 10:59:47 +0000 | [diff] [blame] | 1549 | #endif |
| 1550 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1551 | /* |
| 1552 | * If a character was put back with vungetc, it was already processed. |
| 1553 | * Return it directly. |
| 1554 | */ |
| 1555 | if (old_char != -1) |
| 1556 | { |
| 1557 | c = old_char; |
| 1558 | old_char = -1; |
| 1559 | mod_mask = old_mod_mask; |
Bram Moolenaar | b897871 | 2013-03-16 21:42:16 +0100 | [diff] [blame] | 1560 | mouse_row = old_mouse_row; |
| 1561 | mouse_col = old_mouse_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1562 | } |
Bram Moolenaar | 9fecb46 | 2006-09-05 10:59:47 +0000 | [diff] [blame] | 1563 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1564 | { |
Bram Moolenaar | fd731b0 | 2019-03-09 11:23:58 +0100 | [diff] [blame] | 1565 | mod_mask = 0x0; |
| 1566 | last_recorded_len = 0; |
| 1567 | for (;;) // this is done twice if there are modifiers |
| 1568 | { |
| 1569 | int did_inc = FALSE; |
Bram Moolenaar | 2455c4e | 2015-09-15 18:29:39 +0200 | [diff] [blame] | 1570 | |
Bram Moolenaar | fd731b0 | 2019-03-09 11:23:58 +0100 | [diff] [blame] | 1571 | if (mod_mask |
Bram Moolenaar | 3971905 | 2017-09-05 22:20:46 +0200 | [diff] [blame] | 1572 | #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK) |
Bram Moolenaar | fd731b0 | 2019-03-09 11:23:58 +0100 | [diff] [blame] | 1573 | || im_is_preediting() |
Bram Moolenaar | 3971905 | 2017-09-05 22:20:46 +0200 | [diff] [blame] | 1574 | #endif |
Bram Moolenaar | 749fa0a | 2019-08-03 16:18:07 +0200 | [diff] [blame] | 1575 | #if defined(FEAT_TEXT_PROP) |
| 1576 | || popup_no_mapping() |
| 1577 | #endif |
Bram Moolenaar | fd731b0 | 2019-03-09 11:23:58 +0100 | [diff] [blame] | 1578 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1579 | { |
Bram Moolenaar | fd731b0 | 2019-03-09 11:23:58 +0100 | [diff] [blame] | 1580 | // no mapping after modifier has been read |
| 1581 | ++no_mapping; |
| 1582 | ++allow_keys; |
| 1583 | did_inc = TRUE; // mod_mask may change value |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1584 | } |
Bram Moolenaar | fd731b0 | 2019-03-09 11:23:58 +0100 | [diff] [blame] | 1585 | 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 Moolenaar | a9dd2d3 | 2019-04-29 21:58:41 +0200 | [diff] [blame] | 1595 | || (gui.in_use && c == CSI) |
Bram Moolenaar | fd731b0 | 2019-03-09 11:23:58 +0100 | [diff] [blame] | 1596 | #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 Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1613 | |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 1614 | #if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF) |
Bram Moolenaar | fd731b0 | 2019-03-09 11:23:58 +0100 | [diff] [blame] | 1615 | // Handle K_TEAROFF here, the caller of vgetc() doesn't need to |
| 1616 | // know that a menu was torn off |
Bram Moolenaar | afde13b | 2019-04-28 19:46:49 +0200 | [diff] [blame] | 1617 | if ( |
| 1618 | # ifdef VIMDLL |
| 1619 | gui.in_use && |
| 1620 | # endif |
| 1621 | c == K_TEAROFF) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1622 | { |
Bram Moolenaar | fd731b0 | 2019-03-09 11:23:58 +0100 | [diff] [blame] | 1623 | 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 Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1636 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1637 | #endif |
Bram Moolenaar | 0eda7ac | 2010-06-26 05:38:18 +0200 | [diff] [blame] | 1638 | #if defined(FEAT_GUI) && defined(FEAT_GUI_GTK) && defined(FEAT_MENU) |
Bram Moolenaar | fd731b0 | 2019-03-09 11:23:58 +0100 | [diff] [blame] | 1639 | // 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 Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 1648 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1649 | #ifdef FEAT_GUI |
Bram Moolenaar | a9dd2d3 | 2019-04-29 21:58:41 +0200 | [diff] [blame] | 1650 | if (gui.in_use) |
Bram Moolenaar | fd731b0 | 2019-03-09 11:23:58 +0100 | [diff] [blame] | 1651 | { |
Bram Moolenaar | a9dd2d3 | 2019-04-29 21:58:41 +0200 | [diff] [blame] | 1652 | // 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 Moolenaar | 9c8791f | 2007-09-05 19:47:23 +0000 | [diff] [blame] | 1660 | |
Bram Moolenaar | a9dd2d3 | 2019-04-29 21:58:41 +0200 | [diff] [blame] | 1661 | // 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 Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1667 | #endif |
Bram Moolenaar | fd731b0 | 2019-03-09 11:23:58 +0100 | [diff] [blame] | 1668 | } |
| 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 Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 1679 | #ifdef MSWIN |
Bram Moolenaar | 8e85db0 | 2018-07-27 23:16:51 +0200 | [diff] [blame] | 1680 | // Can be either '.' or a ',', |
| 1681 | // depending on the type of keypad. |
| 1682 | c = MapVirtualKey(VK_DECIMAL, 2); break; |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 1683 | #else |
Bram Moolenaar | 8e85db0 | 2018-07-27 23:16:51 +0200 | [diff] [blame] | 1684 | c = '.'; break; |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 1685 | #endif |
Bram Moolenaar | fd731b0 | 2019-03-09 11:23:58 +0100 | [diff] [blame] | 1686 | 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 Moolenaar | a88d968 | 2005-03-25 21:45:43 +0000 | [diff] [blame] | 1696 | |
Bram Moolenaar | fd731b0 | 2019-03-09 11:23:58 +0100 | [diff] [blame] | 1697 | case K_XHOME: |
| 1698 | case K_ZHOME: if (mod_mask == MOD_MASK_SHIFT) |
Bram Moolenaar | a88d968 | 2005-03-25 21:45:43 +0000 | [diff] [blame] | 1699 | { |
| 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 Moolenaar | fd731b0 | 2019-03-09 11:23:58 +0100 | [diff] [blame] | 1711 | case K_XEND: |
| 1712 | case K_ZEND: if (mod_mask == MOD_MASK_SHIFT) |
Bram Moolenaar | a88d968 | 2005-03-25 21:45:43 +0000 | [diff] [blame] | 1713 | { |
| 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 Moolenaar | fd731b0 | 2019-03-09 11:23:58 +0100 | [diff] [blame] | 1726 | 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 Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1730 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1731 | |
Bram Moolenaar | fd731b0 | 2019-03-09 11:23:58 +0100 | [diff] [blame] | 1732 | // 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 Moolenaar | 386b43e | 2019-05-19 21:57:11 +0200 | [diff] [blame] | 1744 | || ( |
| 1745 | # ifdef VIMDLL |
| 1746 | gui.in_use && |
| 1747 | # endif |
| 1748 | buf[i] == CSI) |
Bram Moolenaar | fd731b0 | 2019-03-09 11:23:58 +0100 | [diff] [blame] | 1749 | #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 | |
Bram Moolenaar | 00eab7f | 2019-10-10 21:49:28 +0200 | [diff] [blame] | 1767 | if (!no_reduce_keys) |
| 1768 | { |
| 1769 | // A modifier was not used for a mapping, apply it to ASCII |
Bram Moolenaar | 459fd78 | 2019-10-13 16:43:39 +0200 | [diff] [blame] | 1770 | // keys. Shift would already have been applied. |
Bram Moolenaar | 828ffd5 | 2019-11-21 23:24:00 +0100 | [diff] [blame] | 1771 | if (mod_mask & MOD_MASK_CTRL) |
Bram Moolenaar | 00eab7f | 2019-10-10 21:49:28 +0200 | [diff] [blame] | 1772 | { |
Bram Moolenaar | 828ffd5 | 2019-11-21 23:24:00 +0100 | [diff] [blame] | 1773 | if ((c >= '`' && c <= 0x7f) || (c >= '@' && c <= '_')) |
| 1774 | { |
| 1775 | c &= 0x1f; |
| 1776 | mod_mask &= ~MOD_MASK_CTRL; |
| 1777 | } |
| 1778 | else if (c == '6') |
| 1779 | { |
| 1780 | // CTRL-6 is equivalent to CTRL-^ |
| 1781 | c = 0x1e; |
| 1782 | mod_mask &= ~MOD_MASK_CTRL; |
| 1783 | } |
Bram Moolenaar | 00eab7f | 2019-10-10 21:49:28 +0200 | [diff] [blame] | 1784 | } |
| 1785 | if ((mod_mask & (MOD_MASK_META | MOD_MASK_ALT)) |
| 1786 | && c >= 0 && c <= 127) |
| 1787 | { |
| 1788 | c += 0x80; |
| 1789 | mod_mask &= ~(MOD_MASK_META|MOD_MASK_ALT); |
| 1790 | } |
| 1791 | } |
| 1792 | |
Bram Moolenaar | fd731b0 | 2019-03-09 11:23:58 +0100 | [diff] [blame] | 1793 | break; |
| 1794 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1795 | } |
Bram Moolenaar | 9fecb46 | 2006-09-05 10:59:47 +0000 | [diff] [blame] | 1796 | |
| 1797 | #ifdef FEAT_EVAL |
| 1798 | /* |
| 1799 | * In the main loop "may_garbage_collect" can be set to do garbage |
| 1800 | * collection in the first next vgetc(). It's disabled after that to |
| 1801 | * avoid internally used Lists and Dicts to be freed. |
| 1802 | */ |
| 1803 | may_garbage_collect = FALSE; |
| 1804 | #endif |
Bram Moolenaar | 69198cb | 2019-09-16 21:58:13 +0200 | [diff] [blame] | 1805 | |
Bram Moolenaar | c3719bd | 2017-11-18 22:13:31 +0100 | [diff] [blame] | 1806 | #ifdef FEAT_BEVAL_TERM |
Bram Moolenaar | c2f5054 | 2019-07-05 23:24:56 +0200 | [diff] [blame] | 1807 | if (c != K_MOUSEMOVE && c != K_IGNORE && c != K_CURSORHOLD) |
Bram Moolenaar | 51b0f37 | 2017-11-18 18:52:04 +0100 | [diff] [blame] | 1808 | { |
Bram Moolenaar | 69198cb | 2019-09-16 21:58:13 +0200 | [diff] [blame] | 1809 | // Don't trigger 'balloonexpr' unless only the mouse was moved. |
Bram Moolenaar | 51b0f37 | 2017-11-18 18:52:04 +0100 | [diff] [blame] | 1810 | bevalexpr_due_set = FALSE; |
| 1811 | ui_remove_balloon(); |
| 1812 | } |
| 1813 | #endif |
Bram Moolenaar | bf0eff0 | 2019-06-01 17:13:36 +0200 | [diff] [blame] | 1814 | #ifdef FEAT_TEXT_PROP |
| 1815 | if (popup_do_filter(c)) |
Bram Moolenaar | e8a7dfe | 2019-10-03 22:35:52 +0200 | [diff] [blame] | 1816 | { |
| 1817 | if (c == Ctrl_C) |
| 1818 | got_int = FALSE; // avoid looping |
Bram Moolenaar | bf0eff0 | 2019-06-01 17:13:36 +0200 | [diff] [blame] | 1819 | c = K_IGNORE; |
Bram Moolenaar | e8a7dfe | 2019-10-03 22:35:52 +0200 | [diff] [blame] | 1820 | } |
Bram Moolenaar | bf0eff0 | 2019-06-01 17:13:36 +0200 | [diff] [blame] | 1821 | #endif |
Bram Moolenaar | 9fecb46 | 2006-09-05 10:59:47 +0000 | [diff] [blame] | 1822 | |
Bram Moolenaar | 69198cb | 2019-09-16 21:58:13 +0200 | [diff] [blame] | 1823 | // Need to process the character before we know it's safe to do something |
| 1824 | // else. |
| 1825 | if (c != K_IGNORE) |
Bram Moolenaar | d103ee7 | 2019-09-18 21:15:31 +0200 | [diff] [blame] | 1826 | state_no_longer_safe("key typed"); |
Bram Moolenaar | 69198cb | 2019-09-16 21:58:13 +0200 | [diff] [blame] | 1827 | |
Bram Moolenaar | 9fecb46 | 2006-09-05 10:59:47 +0000 | [diff] [blame] | 1828 | return c; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1829 | } |
| 1830 | |
| 1831 | /* |
| 1832 | * Like vgetc(), but never return a NUL when called recursively, get a key |
| 1833 | * directly from the user (ignoring typeahead). |
| 1834 | */ |
| 1835 | int |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 1836 | safe_vgetc(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1837 | { |
| 1838 | int c; |
| 1839 | |
| 1840 | c = vgetc(); |
| 1841 | if (c == NUL) |
| 1842 | c = get_keystroke(); |
| 1843 | return c; |
| 1844 | } |
| 1845 | |
| 1846 | /* |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 1847 | * Like safe_vgetc(), but loop to handle K_IGNORE. |
| 1848 | * Also ignore scrollbar events. |
| 1849 | */ |
| 1850 | int |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 1851 | plain_vgetc(void) |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 1852 | { |
| 1853 | int c; |
| 1854 | |
| 1855 | do |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 1856 | c = safe_vgetc(); |
Bram Moolenaar | abab0b0 | 2019-03-30 18:47:01 +0100 | [diff] [blame] | 1857 | while (c == K_IGNORE || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR); |
Bram Moolenaar | ec2da36 | 2017-01-21 20:04:22 +0100 | [diff] [blame] | 1858 | |
| 1859 | if (c == K_PS) |
| 1860 | /* Only handle the first pasted character. Drop the rest, since we |
| 1861 | * don't know what to do with it. */ |
| 1862 | c = bracketed_paste(PASTE_ONE_CHAR, FALSE, NULL); |
| 1863 | |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 1864 | return c; |
| 1865 | } |
| 1866 | |
| 1867 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1868 | * Check if a character is available, such that vgetc() will not block. |
| 1869 | * If the next character is a special character or multi-byte, the returned |
| 1870 | * character is not valid!. |
Bram Moolenaar | 6a2633b | 2018-10-07 23:16:36 +0200 | [diff] [blame] | 1871 | * Returns NUL if no character is available. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1872 | */ |
| 1873 | int |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 1874 | vpeekc(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1875 | { |
| 1876 | if (old_char != -1) |
| 1877 | return old_char; |
| 1878 | return vgetorpeek(FALSE); |
| 1879 | } |
| 1880 | |
Bram Moolenaar | c8bcfe7 | 2018-02-27 16:29:28 +0100 | [diff] [blame] | 1881 | #if defined(FEAT_TERMRESPONSE) || defined(FEAT_TERMINAL) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1882 | /* |
| 1883 | * Like vpeekc(), but don't allow mapping. Do allow checking for terminal |
| 1884 | * codes. |
| 1885 | */ |
| 1886 | int |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 1887 | vpeekc_nomap(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1888 | { |
| 1889 | int c; |
| 1890 | |
| 1891 | ++no_mapping; |
| 1892 | ++allow_keys; |
| 1893 | c = vpeekc(); |
| 1894 | --no_mapping; |
| 1895 | --allow_keys; |
| 1896 | return c; |
| 1897 | } |
| 1898 | #endif |
| 1899 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1900 | /* |
| 1901 | * Check if any character is available, also half an escape sequence. |
| 1902 | * Trick: when no typeahead found, but there is something in the typeahead |
| 1903 | * buffer, it must be an ESC that is recognized as the start of a key code. |
| 1904 | */ |
| 1905 | int |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 1906 | vpeekc_any(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1907 | { |
| 1908 | int c; |
| 1909 | |
| 1910 | c = vpeekc(); |
| 1911 | if (c == NUL && typebuf.tb_len > 0) |
| 1912 | c = ESC; |
| 1913 | return c; |
| 1914 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1915 | |
| 1916 | /* |
| 1917 | * Call vpeekc() without causing anything to be mapped. |
| 1918 | * Return TRUE if a character is available, FALSE otherwise. |
| 1919 | */ |
| 1920 | int |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 1921 | char_avail(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1922 | { |
| 1923 | int retval; |
| 1924 | |
Bram Moolenaar | 2ab375e | 2016-02-10 22:23:06 +0100 | [diff] [blame] | 1925 | #ifdef FEAT_EVAL |
Bram Moolenaar | d057301 | 2017-10-28 21:11:06 +0200 | [diff] [blame] | 1926 | /* When test_override("char_avail", 1) was called pretend there is no |
Bram Moolenaar | 2ab375e | 2016-02-10 22:23:06 +0100 | [diff] [blame] | 1927 | * typeahead. */ |
| 1928 | if (disable_char_avail_for_testing) |
| 1929 | return FALSE; |
| 1930 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1931 | ++no_mapping; |
| 1932 | retval = vpeekc(); |
| 1933 | --no_mapping; |
| 1934 | return (retval != NUL); |
| 1935 | } |
| 1936 | |
Bram Moolenaar | 9c658c9 | 2019-09-15 21:00:54 +0200 | [diff] [blame] | 1937 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 1938 | /* |
| 1939 | * "getchar()" function |
| 1940 | */ |
| 1941 | void |
| 1942 | f_getchar(typval_T *argvars, typval_T *rettv) |
| 1943 | { |
| 1944 | varnumber_T n; |
| 1945 | int error = FALSE; |
| 1946 | |
| 1947 | #ifdef MESSAGE_QUEUE |
| 1948 | // vpeekc() used to check for messages, but that caused problems, invoking |
| 1949 | // a callback where it was not expected. Some plugins use getchar(1) in a |
| 1950 | // loop to await a message, therefore make sure we check for messages here. |
| 1951 | parse_queued_messages(); |
| 1952 | #endif |
| 1953 | |
| 1954 | /* Position the cursor. Needed after a message that ends in a space. */ |
| 1955 | windgoto(msg_row, msg_col); |
| 1956 | |
| 1957 | ++no_mapping; |
| 1958 | ++allow_keys; |
| 1959 | for (;;) |
| 1960 | { |
| 1961 | if (argvars[0].v_type == VAR_UNKNOWN) |
| 1962 | /* getchar(): blocking wait. */ |
| 1963 | n = plain_vgetc(); |
| 1964 | else if (tv_get_number_chk(&argvars[0], &error) == 1) |
| 1965 | /* getchar(1): only check if char avail */ |
| 1966 | n = vpeekc_any(); |
| 1967 | else if (error || vpeekc_any() == NUL) |
| 1968 | /* illegal argument or getchar(0) and no char avail: return zero */ |
| 1969 | n = 0; |
| 1970 | else |
| 1971 | /* getchar(0) and char avail: return char */ |
| 1972 | n = plain_vgetc(); |
| 1973 | |
| 1974 | if (n == K_IGNORE) |
| 1975 | continue; |
| 1976 | break; |
| 1977 | } |
| 1978 | --no_mapping; |
| 1979 | --allow_keys; |
| 1980 | |
| 1981 | set_vim_var_nr(VV_MOUSE_WIN, 0); |
| 1982 | set_vim_var_nr(VV_MOUSE_WINID, 0); |
| 1983 | set_vim_var_nr(VV_MOUSE_LNUM, 0); |
| 1984 | set_vim_var_nr(VV_MOUSE_COL, 0); |
| 1985 | |
| 1986 | rettv->vval.v_number = n; |
| 1987 | if (IS_SPECIAL(n) || mod_mask != 0) |
| 1988 | { |
| 1989 | char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */ |
| 1990 | int i = 0; |
| 1991 | |
| 1992 | /* Turn a special key into three bytes, plus modifier. */ |
| 1993 | if (mod_mask != 0) |
| 1994 | { |
| 1995 | temp[i++] = K_SPECIAL; |
| 1996 | temp[i++] = KS_MODIFIER; |
| 1997 | temp[i++] = mod_mask; |
| 1998 | } |
| 1999 | if (IS_SPECIAL(n)) |
| 2000 | { |
| 2001 | temp[i++] = K_SPECIAL; |
| 2002 | temp[i++] = K_SECOND(n); |
| 2003 | temp[i++] = K_THIRD(n); |
| 2004 | } |
| 2005 | else if (has_mbyte) |
| 2006 | i += (*mb_char2bytes)(n, temp + i); |
| 2007 | else |
| 2008 | temp[i++] = n; |
| 2009 | temp[i++] = NUL; |
| 2010 | rettv->v_type = VAR_STRING; |
| 2011 | rettv->vval.v_string = vim_strsave(temp); |
| 2012 | |
Bram Moolenaar | 9c658c9 | 2019-09-15 21:00:54 +0200 | [diff] [blame] | 2013 | if (is_mouse_key(n)) |
| 2014 | { |
| 2015 | int row = mouse_row; |
| 2016 | int col = mouse_col; |
| 2017 | win_T *win; |
| 2018 | linenr_T lnum; |
| 2019 | win_T *wp; |
| 2020 | int winnr = 1; |
| 2021 | |
| 2022 | if (row >= 0 && col >= 0) |
| 2023 | { |
| 2024 | /* Find the window at the mouse coordinates and compute the |
| 2025 | * text position. */ |
| 2026 | win = mouse_find_win(&row, &col, FIND_POPUP); |
| 2027 | if (win == NULL) |
| 2028 | return; |
| 2029 | (void)mouse_comp_pos(win, &row, &col, &lnum, NULL); |
Bram Moolenaar | a1cb1d1 | 2019-10-17 23:00:07 +0200 | [diff] [blame] | 2030 | #ifdef FEAT_TEXT_PROP |
Bram Moolenaar | 9c658c9 | 2019-09-15 21:00:54 +0200 | [diff] [blame] | 2031 | if (WIN_IS_POPUP(win)) |
| 2032 | winnr = 0; |
| 2033 | else |
Bram Moolenaar | a1cb1d1 | 2019-10-17 23:00:07 +0200 | [diff] [blame] | 2034 | #endif |
Bram Moolenaar | 9c658c9 | 2019-09-15 21:00:54 +0200 | [diff] [blame] | 2035 | for (wp = firstwin; wp != win && wp != NULL; |
| 2036 | wp = wp->w_next) |
| 2037 | ++winnr; |
| 2038 | set_vim_var_nr(VV_MOUSE_WIN, winnr); |
| 2039 | set_vim_var_nr(VV_MOUSE_WINID, win->w_id); |
| 2040 | set_vim_var_nr(VV_MOUSE_LNUM, lnum); |
| 2041 | set_vim_var_nr(VV_MOUSE_COL, col + 1); |
| 2042 | } |
| 2043 | } |
Bram Moolenaar | 9c658c9 | 2019-09-15 21:00:54 +0200 | [diff] [blame] | 2044 | } |
| 2045 | } |
| 2046 | |
| 2047 | /* |
| 2048 | * "getcharmod()" function |
| 2049 | */ |
| 2050 | void |
| 2051 | f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv) |
| 2052 | { |
| 2053 | rettv->vval.v_number = mod_mask; |
| 2054 | } |
| 2055 | #endif // FEAT_EVAL |
| 2056 | |
| 2057 | #if defined(MESSAGE_QUEUE) || defined(PROTO) |
| 2058 | # define MAX_REPEAT_PARSE 8 |
| 2059 | |
| 2060 | /* |
| 2061 | * Process messages that have been queued for netbeans or clientserver. |
| 2062 | * Also check if any jobs have ended. |
| 2063 | * These functions can call arbitrary vimscript and should only be called when |
| 2064 | * it is safe to do so. |
| 2065 | */ |
| 2066 | void |
| 2067 | parse_queued_messages(void) |
| 2068 | { |
Bram Moolenaar | 1cac709 | 2019-10-22 21:54:31 +0200 | [diff] [blame] | 2069 | int old_curwin_id; |
| 2070 | int old_curbuf_fnum; |
Bram Moolenaar | 9c658c9 | 2019-09-15 21:00:54 +0200 | [diff] [blame] | 2071 | int i; |
| 2072 | int save_may_garbage_collect = may_garbage_collect; |
Bram Moolenaar | 69198cb | 2019-09-16 21:58:13 +0200 | [diff] [blame] | 2073 | static int entered = 0; |
Bram Moolenaar | d103ee7 | 2019-09-18 21:15:31 +0200 | [diff] [blame] | 2074 | int was_safe = get_was_safe_state(); |
Bram Moolenaar | 9c658c9 | 2019-09-15 21:00:54 +0200 | [diff] [blame] | 2075 | |
| 2076 | // Do not handle messages while redrawing, because it may cause buffers to |
| 2077 | // change or be wiped while they are being redrawn. |
| 2078 | if (updating_screen) |
| 2079 | return; |
| 2080 | |
Bram Moolenaar | 1cac709 | 2019-10-22 21:54:31 +0200 | [diff] [blame] | 2081 | // If memory allocation fails during startup we'll exit but curbuf or |
| 2082 | // curwin could be NULL. |
| 2083 | if (curbuf == NULL || curwin == NULL) |
| 2084 | return; |
| 2085 | |
| 2086 | old_curbuf_fnum = curbuf->b_fnum; |
| 2087 | old_curwin_id = curwin->w_id; |
| 2088 | |
Bram Moolenaar | 69198cb | 2019-09-16 21:58:13 +0200 | [diff] [blame] | 2089 | ++entered; |
| 2090 | |
Bram Moolenaar | 9c658c9 | 2019-09-15 21:00:54 +0200 | [diff] [blame] | 2091 | // may_garbage_collect is set in main_loop() to do garbage collection when |
| 2092 | // blocking to wait on a character. We don't want that while parsing |
| 2093 | // messages, a callback may invoke vgetc() while lists and dicts are in use |
| 2094 | // in the call stack. |
| 2095 | may_garbage_collect = FALSE; |
| 2096 | |
| 2097 | // Loop when a job ended, but don't keep looping forever. |
| 2098 | for (i = 0; i < MAX_REPEAT_PARSE; ++i) |
| 2099 | { |
| 2100 | // For Win32 mch_breakcheck() does not check for input, do it here. |
| 2101 | # if defined(MSWIN) && defined(FEAT_JOB_CHANNEL) |
| 2102 | channel_handle_events(FALSE); |
| 2103 | # endif |
| 2104 | |
| 2105 | # ifdef FEAT_NETBEANS_INTG |
| 2106 | // Process the queued netbeans messages. |
| 2107 | netbeans_parse_messages(); |
| 2108 | # endif |
| 2109 | # ifdef FEAT_JOB_CHANNEL |
| 2110 | // Write any buffer lines still to be written. |
| 2111 | channel_write_any_lines(); |
| 2112 | |
| 2113 | // Process the messages queued on channels. |
| 2114 | channel_parse_messages(); |
| 2115 | # endif |
| 2116 | # if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11) |
| 2117 | // Process the queued clientserver messages. |
| 2118 | server_parse_messages(); |
| 2119 | # endif |
| 2120 | # ifdef FEAT_JOB_CHANNEL |
| 2121 | // Check if any jobs have ended. If so, repeat the above to handle |
| 2122 | // changes, e.g. stdin may have been closed. |
| 2123 | if (job_check_ended()) |
| 2124 | continue; |
| 2125 | # endif |
| 2126 | # ifdef FEAT_TERMINAL |
| 2127 | free_unused_terminals(); |
| 2128 | # endif |
| 2129 | # ifdef FEAT_SOUND_CANBERRA |
| 2130 | if (has_sound_callback_in_queue()) |
| 2131 | invoke_sound_callback(); |
| 2132 | # endif |
| 2133 | break; |
| 2134 | } |
| 2135 | |
Bram Moolenaar | 69198cb | 2019-09-16 21:58:13 +0200 | [diff] [blame] | 2136 | // When not nested we'll go back to waiting for a typed character. If it |
| 2137 | // was safe before then this triggers a SafeStateAgain autocommand event. |
Bram Moolenaar | d103ee7 | 2019-09-18 21:15:31 +0200 | [diff] [blame] | 2138 | if (entered == 1 && was_safe) |
Bram Moolenaar | 37d1807 | 2019-09-17 20:28:38 +0200 | [diff] [blame] | 2139 | may_trigger_safestateagain(); |
Bram Moolenaar | 69198cb | 2019-09-16 21:58:13 +0200 | [diff] [blame] | 2140 | |
Bram Moolenaar | 9c658c9 | 2019-09-15 21:00:54 +0200 | [diff] [blame] | 2141 | may_garbage_collect = save_may_garbage_collect; |
| 2142 | |
| 2143 | // If the current window or buffer changed we need to bail out of the |
| 2144 | // waiting loop. E.g. when a job exit callback closes the terminal window. |
| 2145 | if (curwin->w_id != old_curwin_id || curbuf->b_fnum != old_curbuf_fnum) |
| 2146 | ins_char_typebuf(K_IGNORE); |
Bram Moolenaar | 69198cb | 2019-09-16 21:58:13 +0200 | [diff] [blame] | 2147 | |
| 2148 | --entered; |
Bram Moolenaar | 9c658c9 | 2019-09-15 21:00:54 +0200 | [diff] [blame] | 2149 | } |
| 2150 | #endif |
| 2151 | |
| 2152 | |
Bram Moolenaar | edd680f | 2019-08-03 14:23:48 +0200 | [diff] [blame] | 2153 | typedef enum { |
| 2154 | map_result_fail, // failed, break loop |
| 2155 | map_result_get, // get a character from typeahead |
| 2156 | map_result_retry, // try to map again |
| 2157 | map_result_nomatch // no matching mapping, get char |
| 2158 | } map_result_T; |
| 2159 | |
| 2160 | /* |
Bram Moolenaar | 88d3d09 | 2019-10-20 16:00:47 +0200 | [diff] [blame] | 2161 | * Check if the bytes at the start of the typeahead buffer are a character used |
| 2162 | * in CTRL-X mode. This includes the form with a CTRL modifier. |
| 2163 | */ |
| 2164 | static int |
| 2165 | at_ctrl_x_key(void) |
| 2166 | { |
| 2167 | char_u *p = typebuf.tb_buf + typebuf.tb_off; |
| 2168 | int c = *p; |
| 2169 | |
| 2170 | if (typebuf.tb_len > 3 |
| 2171 | && c == K_SPECIAL |
| 2172 | && p[1] == KS_MODIFIER |
| 2173 | && (p[2] & MOD_MASK_CTRL)) |
| 2174 | c = p[3] & 0x1f; |
| 2175 | return vim_is_ctrl_x_key(c); |
| 2176 | } |
| 2177 | |
| 2178 | /* |
Bram Moolenaar | edd680f | 2019-08-03 14:23:48 +0200 | [diff] [blame] | 2179 | * Handle mappings in the typeahead buffer. |
| 2180 | * - When something was mapped, return map_result_retry for recursive mappings. |
Bram Moolenaar | eda35f7 | 2019-08-03 14:59:44 +0200 | [diff] [blame] | 2181 | * - When nothing mapped and typeahead has a character: return map_result_get. |
Bram Moolenaar | edd680f | 2019-08-03 14:23:48 +0200 | [diff] [blame] | 2182 | * - When there is no match yet, return map_result_nomatch, need to get more |
| 2183 | * typeahead. |
| 2184 | */ |
| 2185 | static int |
| 2186 | handle_mapping( |
| 2187 | int *keylenp, |
| 2188 | int *timedout, |
| 2189 | int *mapdepth) |
| 2190 | { |
| 2191 | mapblock_T *mp = NULL; |
| 2192 | mapblock_T *mp2; |
| 2193 | mapblock_T *mp_match; |
| 2194 | int mp_match_len = 0; |
| 2195 | int max_mlen = 0; |
| 2196 | int tb_c1; |
| 2197 | int mlen; |
| 2198 | #ifdef FEAT_LANGMAP |
| 2199 | int nolmaplen; |
| 2200 | #endif |
| 2201 | int keylen = *keylenp; |
| 2202 | int i; |
| 2203 | int local_State = get_real_state(); |
| 2204 | |
| 2205 | /* |
| 2206 | * Check for a mappable key sequence. |
Bram Moolenaar | eda35f7 | 2019-08-03 14:59:44 +0200 | [diff] [blame] | 2207 | * Walk through one maphash[] list until we find an entry that matches. |
Bram Moolenaar | edd680f | 2019-08-03 14:23:48 +0200 | [diff] [blame] | 2208 | * |
| 2209 | * Don't look for mappings if: |
| 2210 | * - no_mapping set: mapping disabled (e.g. for CTRL-V) |
| 2211 | * - maphash_valid not set: no mappings present. |
| 2212 | * - typebuf.tb_buf[typebuf.tb_off] should not be remapped |
| 2213 | * - in insert or cmdline mode and 'paste' option set |
Bram Moolenaar | eda35f7 | 2019-08-03 14:59:44 +0200 | [diff] [blame] | 2214 | * - waiting for "hit return to continue" and CR or SPACE typed |
Bram Moolenaar | edd680f | 2019-08-03 14:23:48 +0200 | [diff] [blame] | 2215 | * - waiting for a char with --more-- |
| 2216 | * - in Ctrl-X mode, and we get a valid char for that mode |
| 2217 | */ |
| 2218 | tb_c1 = typebuf.tb_buf[typebuf.tb_off]; |
| 2219 | if (no_mapping == 0 && is_maphash_valid() |
| 2220 | && (no_zero_mapping == 0 || tb_c1 != '0') |
| 2221 | && (typebuf.tb_maplen == 0 |
| 2222 | || (p_remap |
| 2223 | && (typebuf.tb_noremap[typebuf.tb_off] |
| 2224 | & (RM_NONE|RM_ABBR)) == 0)) |
| 2225 | && !(p_paste && (State & (INSERT + CMDLINE))) |
| 2226 | && !(State == HITRETURN && (tb_c1 == CAR || tb_c1 == ' ')) |
| 2227 | && State != ASKMORE |
| 2228 | && State != CONFIRM |
Bram Moolenaar | 88d3d09 | 2019-10-20 16:00:47 +0200 | [diff] [blame] | 2229 | && !((ctrl_x_mode_not_default() && at_ctrl_x_key()) |
Bram Moolenaar | edd680f | 2019-08-03 14:23:48 +0200 | [diff] [blame] | 2230 | || ((compl_cont_status & CONT_LOCAL) |
Bram Moolenaar | e2c453d | 2019-08-21 14:37:09 +0200 | [diff] [blame] | 2231 | && (tb_c1 == Ctrl_N || tb_c1 == Ctrl_P)))) |
Bram Moolenaar | edd680f | 2019-08-03 14:23:48 +0200 | [diff] [blame] | 2232 | { |
| 2233 | #ifdef FEAT_LANGMAP |
| 2234 | if (tb_c1 == K_SPECIAL) |
| 2235 | nolmaplen = 2; |
| 2236 | else |
| 2237 | { |
Bram Moolenaar | eda35f7 | 2019-08-03 14:59:44 +0200 | [diff] [blame] | 2238 | LANGMAP_ADJUST(tb_c1, (State & (CMDLINE | INSERT)) == 0 |
| 2239 | && get_real_state() != SELECTMODE); |
Bram Moolenaar | edd680f | 2019-08-03 14:23:48 +0200 | [diff] [blame] | 2240 | nolmaplen = 0; |
| 2241 | } |
| 2242 | #endif |
| 2243 | // First try buffer-local mappings. |
| 2244 | mp = get_buf_maphash_list(local_State, tb_c1); |
| 2245 | mp2 = get_maphash_list(local_State, tb_c1); |
| 2246 | if (mp == NULL) |
| 2247 | { |
| 2248 | // There are no buffer-local mappings. |
| 2249 | mp = mp2; |
| 2250 | mp2 = NULL; |
| 2251 | } |
Bram Moolenaar | eda35f7 | 2019-08-03 14:59:44 +0200 | [diff] [blame] | 2252 | |
Bram Moolenaar | edd680f | 2019-08-03 14:23:48 +0200 | [diff] [blame] | 2253 | /* |
Bram Moolenaar | eda35f7 | 2019-08-03 14:59:44 +0200 | [diff] [blame] | 2254 | * Loop until a partly matching mapping is found or all (local) |
| 2255 | * mappings have been checked. |
Bram Moolenaar | edd680f | 2019-08-03 14:23:48 +0200 | [diff] [blame] | 2256 | * The longest full match is remembered in "mp_match". |
Bram Moolenaar | eda35f7 | 2019-08-03 14:59:44 +0200 | [diff] [blame] | 2257 | * A full match is only accepted if there is no partly match, so "aa" |
| 2258 | * and "aaa" can both be mapped. |
Bram Moolenaar | edd680f | 2019-08-03 14:23:48 +0200 | [diff] [blame] | 2259 | */ |
| 2260 | mp_match = NULL; |
| 2261 | mp_match_len = 0; |
| 2262 | for ( ; mp != NULL; |
Bram Moolenaar | eda35f7 | 2019-08-03 14:59:44 +0200 | [diff] [blame] | 2263 | mp->m_next == NULL ? (mp = mp2, mp2 = NULL) : (mp = mp->m_next)) |
Bram Moolenaar | edd680f | 2019-08-03 14:23:48 +0200 | [diff] [blame] | 2264 | { |
Bram Moolenaar | eda35f7 | 2019-08-03 14:59:44 +0200 | [diff] [blame] | 2265 | // Only consider an entry if the first character matches and it is |
| 2266 | // for the current state. |
Bram Moolenaar | edd680f | 2019-08-03 14:23:48 +0200 | [diff] [blame] | 2267 | // Skip ":lmap" mappings if keys were mapped. |
| 2268 | if (mp->m_keys[0] == tb_c1 |
| 2269 | && (mp->m_mode & local_State) |
Bram Moolenaar | 459fd78 | 2019-10-13 16:43:39 +0200 | [diff] [blame] | 2270 | && !(mp->m_simplified && seenModifyOtherKeys) |
Bram Moolenaar | eda35f7 | 2019-08-03 14:59:44 +0200 | [diff] [blame] | 2271 | && ((mp->m_mode & LANGMAP) == 0 || typebuf.tb_maplen == 0)) |
Bram Moolenaar | edd680f | 2019-08-03 14:23:48 +0200 | [diff] [blame] | 2272 | { |
| 2273 | #ifdef FEAT_LANGMAP |
| 2274 | int nomap = nolmaplen; |
| 2275 | int c2; |
| 2276 | #endif |
| 2277 | // find the match length of this mapping |
| 2278 | for (mlen = 1; mlen < typebuf.tb_len; ++mlen) |
| 2279 | { |
| 2280 | #ifdef FEAT_LANGMAP |
| 2281 | c2 = typebuf.tb_buf[typebuf.tb_off + mlen]; |
| 2282 | if (nomap > 0) |
| 2283 | --nomap; |
| 2284 | else if (c2 == K_SPECIAL) |
| 2285 | nomap = 2; |
| 2286 | else |
| 2287 | LANGMAP_ADJUST(c2, TRUE); |
| 2288 | if (mp->m_keys[mlen] != c2) |
| 2289 | #else |
| 2290 | if (mp->m_keys[mlen] != |
| 2291 | typebuf.tb_buf[typebuf.tb_off + mlen]) |
| 2292 | #endif |
| 2293 | break; |
| 2294 | } |
| 2295 | |
Bram Moolenaar | eda35f7 | 2019-08-03 14:59:44 +0200 | [diff] [blame] | 2296 | // Don't allow mapping the first byte(s) of a multi-byte char. |
| 2297 | // Happens when mapping <M-a> and then changing 'encoding'. |
| 2298 | // Beware that 0x80 is escaped. |
Bram Moolenaar | edd680f | 2019-08-03 14:23:48 +0200 | [diff] [blame] | 2299 | { |
| 2300 | char_u *p1 = mp->m_keys; |
| 2301 | char_u *p2 = mb_unescape(&p1); |
| 2302 | |
| 2303 | if (has_mbyte && p2 != NULL |
Bram Moolenaar | 1614a14 | 2019-10-06 22:00:13 +0200 | [diff] [blame] | 2304 | && MB_BYTE2LEN(tb_c1) > mb_ptr2len(p2)) |
Bram Moolenaar | edd680f | 2019-08-03 14:23:48 +0200 | [diff] [blame] | 2305 | mlen = 0; |
| 2306 | } |
| 2307 | |
| 2308 | // Check an entry whether it matches. |
| 2309 | // - Full match: mlen == keylen |
| 2310 | // - Partly match: mlen == typebuf.tb_len |
| 2311 | keylen = mp->m_keylen; |
Bram Moolenaar | eda35f7 | 2019-08-03 14:59:44 +0200 | [diff] [blame] | 2312 | if (mlen == keylen || (mlen == typebuf.tb_len |
| 2313 | && typebuf.tb_len < keylen)) |
Bram Moolenaar | edd680f | 2019-08-03 14:23:48 +0200 | [diff] [blame] | 2314 | { |
| 2315 | char_u *s; |
| 2316 | int n; |
| 2317 | |
Bram Moolenaar | eda35f7 | 2019-08-03 14:59:44 +0200 | [diff] [blame] | 2318 | // If only script-local mappings are allowed, check if the |
| 2319 | // mapping starts with K_SNR. |
Bram Moolenaar | edd680f | 2019-08-03 14:23:48 +0200 | [diff] [blame] | 2320 | s = typebuf.tb_noremap + typebuf.tb_off; |
| 2321 | if (*s == RM_SCRIPT |
| 2322 | && (mp->m_keys[0] != K_SPECIAL |
| 2323 | || mp->m_keys[1] != KS_EXTRA |
Bram Moolenaar | eda35f7 | 2019-08-03 14:59:44 +0200 | [diff] [blame] | 2324 | || mp->m_keys[2] != (int)KE_SNR)) |
Bram Moolenaar | edd680f | 2019-08-03 14:23:48 +0200 | [diff] [blame] | 2325 | continue; |
| 2326 | |
Bram Moolenaar | eda35f7 | 2019-08-03 14:59:44 +0200 | [diff] [blame] | 2327 | // If one of the typed keys cannot be remapped, skip the |
| 2328 | // entry. |
Bram Moolenaar | edd680f | 2019-08-03 14:23:48 +0200 | [diff] [blame] | 2329 | for (n = mlen; --n >= 0; ) |
| 2330 | if (*s++ & (RM_NONE|RM_ABBR)) |
| 2331 | break; |
| 2332 | if (n >= 0) |
| 2333 | continue; |
| 2334 | |
| 2335 | if (keylen > typebuf.tb_len) |
| 2336 | { |
| 2337 | if (!*timedout && !(mp_match != NULL |
Bram Moolenaar | eda35f7 | 2019-08-03 14:59:44 +0200 | [diff] [blame] | 2338 | && mp_match->m_nowait)) |
Bram Moolenaar | edd680f | 2019-08-03 14:23:48 +0200 | [diff] [blame] | 2339 | { |
| 2340 | // break at a partly match |
| 2341 | keylen = KEYLEN_PART_MAP; |
| 2342 | break; |
| 2343 | } |
| 2344 | } |
| 2345 | else if (keylen > mp_match_len) |
| 2346 | { |
| 2347 | // found a longer match |
| 2348 | mp_match = mp; |
| 2349 | mp_match_len = keylen; |
| 2350 | } |
| 2351 | } |
| 2352 | else |
Bram Moolenaar | eda35f7 | 2019-08-03 14:59:44 +0200 | [diff] [blame] | 2353 | // No match; may have to check for termcode at next |
| 2354 | // character. |
Bram Moolenaar | edd680f | 2019-08-03 14:23:48 +0200 | [diff] [blame] | 2355 | if (max_mlen < mlen) |
| 2356 | max_mlen = mlen; |
| 2357 | } |
| 2358 | } |
| 2359 | |
Bram Moolenaar | eda35f7 | 2019-08-03 14:59:44 +0200 | [diff] [blame] | 2360 | // If no partly match found, use the longest full match. |
Bram Moolenaar | edd680f | 2019-08-03 14:23:48 +0200 | [diff] [blame] | 2361 | if (keylen != KEYLEN_PART_MAP) |
| 2362 | { |
| 2363 | mp = mp_match; |
| 2364 | keylen = mp_match_len; |
| 2365 | } |
| 2366 | } |
| 2367 | |
| 2368 | /* |
| 2369 | * Check for match with 'pastetoggle' |
| 2370 | */ |
| 2371 | if (*p_pt != NUL && mp == NULL && (State & (INSERT|NORMAL))) |
| 2372 | { |
Bram Moolenaar | eda35f7 | 2019-08-03 14:59:44 +0200 | [diff] [blame] | 2373 | for (mlen = 0; mlen < typebuf.tb_len && p_pt[mlen]; ++mlen) |
| 2374 | if (p_pt[mlen] != typebuf.tb_buf[typebuf.tb_off + mlen]) |
Bram Moolenaar | edd680f | 2019-08-03 14:23:48 +0200 | [diff] [blame] | 2375 | break; |
| 2376 | if (p_pt[mlen] == NUL) // match |
| 2377 | { |
| 2378 | // write chars to script file(s) |
| 2379 | if (mlen > typebuf.tb_maplen) |
Bram Moolenaar | eda35f7 | 2019-08-03 14:59:44 +0200 | [diff] [blame] | 2380 | gotchars(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_maplen, |
| 2381 | mlen - typebuf.tb_maplen); |
Bram Moolenaar | edd680f | 2019-08-03 14:23:48 +0200 | [diff] [blame] | 2382 | |
| 2383 | del_typebuf(mlen, 0); // remove the chars |
Bram Moolenaar | eda35f7 | 2019-08-03 14:59:44 +0200 | [diff] [blame] | 2384 | set_option_value((char_u *)"paste", (long)!p_paste, NULL, 0); |
Bram Moolenaar | edd680f | 2019-08-03 14:23:48 +0200 | [diff] [blame] | 2385 | if (!(State & INSERT)) |
| 2386 | { |
| 2387 | msg_col = 0; |
| 2388 | msg_row = Rows - 1; |
| 2389 | msg_clr_eos(); // clear ruler |
| 2390 | } |
| 2391 | status_redraw_all(); |
| 2392 | redraw_statuslines(); |
| 2393 | showmode(); |
| 2394 | setcursor(); |
| 2395 | *keylenp = keylen; |
| 2396 | return map_result_retry; |
| 2397 | } |
| 2398 | // Need more chars for partly match. |
| 2399 | if (mlen == typebuf.tb_len) |
| 2400 | keylen = KEYLEN_PART_KEY; |
| 2401 | else if (max_mlen < mlen) |
Bram Moolenaar | eda35f7 | 2019-08-03 14:59:44 +0200 | [diff] [blame] | 2402 | // no match, may have to check for termcode at next character |
Bram Moolenaar | edd680f | 2019-08-03 14:23:48 +0200 | [diff] [blame] | 2403 | max_mlen = mlen + 1; |
| 2404 | } |
| 2405 | |
Bram Moolenaar | eda35f7 | 2019-08-03 14:59:44 +0200 | [diff] [blame] | 2406 | if ((mp == NULL || max_mlen >= mp_match_len) && keylen != KEYLEN_PART_MAP) |
Bram Moolenaar | edd680f | 2019-08-03 14:23:48 +0200 | [diff] [blame] | 2407 | { |
| 2408 | int save_keylen = keylen; |
| 2409 | |
| 2410 | /* |
Bram Moolenaar | eda35f7 | 2019-08-03 14:59:44 +0200 | [diff] [blame] | 2411 | * When no matching mapping found or found a non-matching mapping that |
| 2412 | * matches at least what the matching mapping matched: |
Bram Moolenaar | edd680f | 2019-08-03 14:23:48 +0200 | [diff] [blame] | 2413 | * Check if we have a terminal code, when: |
Bram Moolenaar | eda35f7 | 2019-08-03 14:59:44 +0200 | [diff] [blame] | 2414 | * - mapping is allowed, |
| 2415 | * - keys have not been mapped, |
| 2416 | * - and not an ESC sequence, not in insert mode or p_ek is on, |
| 2417 | * - and when not timed out, |
Bram Moolenaar | edd680f | 2019-08-03 14:23:48 +0200 | [diff] [blame] | 2418 | */ |
| 2419 | if ((no_mapping == 0 || allow_keys != 0) |
| 2420 | && (typebuf.tb_maplen == 0 |
| 2421 | || (p_remap && typebuf.tb_noremap[ |
Bram Moolenaar | eda35f7 | 2019-08-03 14:59:44 +0200 | [diff] [blame] | 2422 | typebuf.tb_off] == RM_YES)) |
Bram Moolenaar | edd680f | 2019-08-03 14:23:48 +0200 | [diff] [blame] | 2423 | && !*timedout) |
| 2424 | { |
| 2425 | keylen = check_termcode(max_mlen + 1, |
| 2426 | NULL, 0, NULL); |
| 2427 | |
Bram Moolenaar | eda35f7 | 2019-08-03 14:59:44 +0200 | [diff] [blame] | 2428 | // If no termcode matched but 'pastetoggle' matched partially it's |
| 2429 | // like an incomplete key sequence. |
Bram Moolenaar | edd680f | 2019-08-03 14:23:48 +0200 | [diff] [blame] | 2430 | if (keylen == 0 && save_keylen == KEYLEN_PART_KEY) |
| 2431 | keylen = KEYLEN_PART_KEY; |
| 2432 | |
Bram Moolenaar | eda35f7 | 2019-08-03 14:59:44 +0200 | [diff] [blame] | 2433 | // When getting a partial match, but the last characters were not |
| 2434 | // typed, don't wait for a typed character to complete the |
| 2435 | // termcode. This helps a lot when a ":normal" command ends in an |
| 2436 | // ESC. |
| 2437 | if (keylen < 0 && typebuf.tb_len == typebuf.tb_maplen) |
Bram Moolenaar | edd680f | 2019-08-03 14:23:48 +0200 | [diff] [blame] | 2438 | keylen = 0; |
| 2439 | } |
| 2440 | else |
| 2441 | keylen = 0; |
| 2442 | if (keylen == 0) // no matching terminal code |
| 2443 | { |
Bram Moolenaar | eda35f7 | 2019-08-03 14:59:44 +0200 | [diff] [blame] | 2444 | #ifdef AMIGA |
| 2445 | // check for window bounds report |
Bram Moolenaar | edd680f | 2019-08-03 14:23:48 +0200 | [diff] [blame] | 2446 | if (typebuf.tb_maplen == 0 && (typebuf.tb_buf[ |
Bram Moolenaar | eda35f7 | 2019-08-03 14:59:44 +0200 | [diff] [blame] | 2447 | typebuf.tb_off] & 0xff) == CSI) |
Bram Moolenaar | edd680f | 2019-08-03 14:23:48 +0200 | [diff] [blame] | 2448 | { |
| 2449 | char_u *s; |
| 2450 | |
| 2451 | for (s = typebuf.tb_buf + typebuf.tb_off + 1; |
Bram Moolenaar | eda35f7 | 2019-08-03 14:59:44 +0200 | [diff] [blame] | 2452 | s < typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len |
| 2453 | && (VIM_ISDIGIT(*s) || *s == ';' || *s == ' '); |
Bram Moolenaar | edd680f | 2019-08-03 14:23:48 +0200 | [diff] [blame] | 2454 | ++s) |
| 2455 | ; |
| 2456 | if (*s == 'r' || *s == '|') // found one |
| 2457 | { |
Bram Moolenaar | eda35f7 | 2019-08-03 14:59:44 +0200 | [diff] [blame] | 2458 | del_typebuf( |
| 2459 | (int)(s + 1 - (typebuf.tb_buf + typebuf.tb_off)), 0); |
Bram Moolenaar | edd680f | 2019-08-03 14:23:48 +0200 | [diff] [blame] | 2460 | // get size and redraw screen |
| 2461 | shell_resized(); |
| 2462 | *keylenp = keylen; |
| 2463 | return map_result_retry; |
| 2464 | } |
| 2465 | if (*s == NUL) // need more characters |
| 2466 | keylen = KEYLEN_PART_KEY; |
| 2467 | } |
| 2468 | if (keylen >= 0) |
| 2469 | #endif |
Bram Moolenaar | eda35f7 | 2019-08-03 14:59:44 +0200 | [diff] [blame] | 2470 | // When there was a matching mapping and no termcode could be |
| 2471 | // replaced after another one, use that mapping (loop around). |
| 2472 | // If there was no mapping at all use the character from the |
| 2473 | // typeahead buffer right here. |
| 2474 | if (mp == NULL) |
| 2475 | { |
| 2476 | *keylenp = keylen; |
| 2477 | return map_result_get; // got character, break for loop |
| 2478 | } |
Bram Moolenaar | edd680f | 2019-08-03 14:23:48 +0200 | [diff] [blame] | 2479 | } |
| 2480 | |
| 2481 | if (keylen > 0) // full matching terminal code |
| 2482 | { |
| 2483 | #if defined(FEAT_GUI) && defined(FEAT_MENU) |
| 2484 | if (typebuf.tb_len >= 2 |
Bram Moolenaar | eda35f7 | 2019-08-03 14:59:44 +0200 | [diff] [blame] | 2485 | && typebuf.tb_buf[typebuf.tb_off] == K_SPECIAL |
| 2486 | && typebuf.tb_buf[typebuf.tb_off + 1] == KS_MENU) |
Bram Moolenaar | edd680f | 2019-08-03 14:23:48 +0200 | [diff] [blame] | 2487 | { |
| 2488 | int idx; |
| 2489 | |
Bram Moolenaar | eda35f7 | 2019-08-03 14:59:44 +0200 | [diff] [blame] | 2490 | // Using a menu may cause a break in undo! It's like using |
| 2491 | // gotchars(), but without recording or writing to a script |
| 2492 | // file. |
Bram Moolenaar | edd680f | 2019-08-03 14:23:48 +0200 | [diff] [blame] | 2493 | may_sync_undo(); |
| 2494 | del_typebuf(3, 0); |
| 2495 | idx = get_menu_index(current_menu, local_State); |
| 2496 | if (idx != MENU_INDEX_INVALID) |
| 2497 | { |
Bram Moolenaar | eda35f7 | 2019-08-03 14:59:44 +0200 | [diff] [blame] | 2498 | // In Select mode and a Visual mode menu is used: Switch |
| 2499 | // to Visual mode temporarily. Append K_SELECT to switch |
Bram Moolenaar | edd680f | 2019-08-03 14:23:48 +0200 | [diff] [blame] | 2500 | // back to Select mode. |
| 2501 | if (VIsual_active && VIsual_select |
Bram Moolenaar | eda35f7 | 2019-08-03 14:59:44 +0200 | [diff] [blame] | 2502 | && (current_menu->modes & VISUAL)) |
Bram Moolenaar | edd680f | 2019-08-03 14:23:48 +0200 | [diff] [blame] | 2503 | { |
| 2504 | VIsual_select = FALSE; |
| 2505 | (void)ins_typebuf(K_SELECT_STRING, |
Bram Moolenaar | eda35f7 | 2019-08-03 14:59:44 +0200 | [diff] [blame] | 2506 | REMAP_NONE, 0, TRUE, FALSE); |
Bram Moolenaar | edd680f | 2019-08-03 14:23:48 +0200 | [diff] [blame] | 2507 | } |
| 2508 | ins_typebuf(current_menu->strings[idx], |
| 2509 | current_menu->noremap[idx], |
Bram Moolenaar | eda35f7 | 2019-08-03 14:59:44 +0200 | [diff] [blame] | 2510 | 0, TRUE, current_menu->silent[idx]); |
Bram Moolenaar | edd680f | 2019-08-03 14:23:48 +0200 | [diff] [blame] | 2511 | } |
| 2512 | } |
| 2513 | #endif // FEAT_GUI && FEAT_MENU |
| 2514 | *keylenp = keylen; |
| 2515 | return map_result_retry; // try mapping again |
| 2516 | } |
| 2517 | |
Bram Moolenaar | eda35f7 | 2019-08-03 14:59:44 +0200 | [diff] [blame] | 2518 | // Partial match: get some more characters. When a matching mapping |
| 2519 | // was found use that one. |
Bram Moolenaar | edd680f | 2019-08-03 14:23:48 +0200 | [diff] [blame] | 2520 | if (mp == NULL || keylen < 0) |
| 2521 | keylen = KEYLEN_PART_KEY; |
| 2522 | else |
| 2523 | keylen = mp_match_len; |
| 2524 | } |
| 2525 | |
| 2526 | /* |
| 2527 | * complete match |
| 2528 | */ |
| 2529 | if (keylen >= 0 && keylen <= typebuf.tb_len) |
| 2530 | { |
| 2531 | char_u *map_str; |
| 2532 | |
| 2533 | #ifdef FEAT_EVAL |
Bram Moolenaar | eda35f7 | 2019-08-03 14:59:44 +0200 | [diff] [blame] | 2534 | int save_m_expr; |
| 2535 | int save_m_noremap; |
| 2536 | int save_m_silent; |
| 2537 | char_u *save_m_keys; |
| 2538 | char_u *save_m_str; |
Bram Moolenaar | edd680f | 2019-08-03 14:23:48 +0200 | [diff] [blame] | 2539 | #else |
| 2540 | # define save_m_noremap mp->m_noremap |
| 2541 | # define save_m_silent mp->m_silent |
| 2542 | #endif |
| 2543 | |
| 2544 | // write chars to script file(s) |
| 2545 | if (keylen > typebuf.tb_maplen) |
Bram Moolenaar | eda35f7 | 2019-08-03 14:59:44 +0200 | [diff] [blame] | 2546 | gotchars(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_maplen, |
| 2547 | keylen - typebuf.tb_maplen); |
Bram Moolenaar | edd680f | 2019-08-03 14:23:48 +0200 | [diff] [blame] | 2548 | |
| 2549 | cmd_silent = (typebuf.tb_silent > 0); |
| 2550 | del_typebuf(keylen, 0); // remove the mapped keys |
| 2551 | |
| 2552 | /* |
| 2553 | * Put the replacement string in front of mapstr. |
| 2554 | * The depth check catches ":map x y" and ":map y x". |
| 2555 | */ |
| 2556 | if (++*mapdepth >= p_mmd) |
| 2557 | { |
| 2558 | emsg(_("E223: recursive mapping")); |
| 2559 | if (State & CMDLINE) |
| 2560 | redrawcmdline(); |
| 2561 | else |
| 2562 | setcursor(); |
| 2563 | flush_buffers(FLUSH_MINIMAL); |
| 2564 | *mapdepth = 0; /* for next one */ |
| 2565 | *keylenp = keylen; |
| 2566 | return map_result_fail; |
| 2567 | } |
| 2568 | |
| 2569 | /* |
Bram Moolenaar | eda35f7 | 2019-08-03 14:59:44 +0200 | [diff] [blame] | 2570 | * In Select mode and a Visual mode mapping is used: Switch to Visual |
| 2571 | * mode temporarily. Append K_SELECT to switch back to Select mode. |
Bram Moolenaar | edd680f | 2019-08-03 14:23:48 +0200 | [diff] [blame] | 2572 | */ |
Bram Moolenaar | eda35f7 | 2019-08-03 14:59:44 +0200 | [diff] [blame] | 2573 | if (VIsual_active && VIsual_select && (mp->m_mode & VISUAL)) |
Bram Moolenaar | edd680f | 2019-08-03 14:23:48 +0200 | [diff] [blame] | 2574 | { |
| 2575 | VIsual_select = FALSE; |
Bram Moolenaar | eda35f7 | 2019-08-03 14:59:44 +0200 | [diff] [blame] | 2576 | (void)ins_typebuf(K_SELECT_STRING, REMAP_NONE, 0, TRUE, FALSE); |
Bram Moolenaar | edd680f | 2019-08-03 14:23:48 +0200 | [diff] [blame] | 2577 | } |
| 2578 | |
| 2579 | #ifdef FEAT_EVAL |
Bram Moolenaar | eda35f7 | 2019-08-03 14:59:44 +0200 | [diff] [blame] | 2580 | // Copy the values from *mp that are used, because evaluating the |
| 2581 | // expression may invoke a function that redefines the mapping, thereby |
| 2582 | // making *mp invalid. |
Bram Moolenaar | edd680f | 2019-08-03 14:23:48 +0200 | [diff] [blame] | 2583 | save_m_expr = mp->m_expr; |
| 2584 | save_m_noremap = mp->m_noremap; |
| 2585 | save_m_silent = mp->m_silent; |
| 2586 | save_m_keys = NULL; // only saved when needed |
| 2587 | save_m_str = NULL; // only saved when needed |
| 2588 | |
| 2589 | /* |
Bram Moolenaar | eda35f7 | 2019-08-03 14:59:44 +0200 | [diff] [blame] | 2590 | * Handle ":map <expr>": evaluate the {rhs} as an expression. Also |
| 2591 | * save and restore the command line for "normal :". |
Bram Moolenaar | edd680f | 2019-08-03 14:23:48 +0200 | [diff] [blame] | 2592 | */ |
| 2593 | if (mp->m_expr) |
| 2594 | { |
| 2595 | int save_vgetc_busy = vgetc_busy; |
| 2596 | int save_may_garbage_collect = may_garbage_collect; |
| 2597 | |
| 2598 | vgetc_busy = 0; |
| 2599 | may_garbage_collect = FALSE; |
| 2600 | |
| 2601 | save_m_keys = vim_strsave(mp->m_keys); |
| 2602 | save_m_str = vim_strsave(mp->m_str); |
| 2603 | map_str = eval_map_expr(save_m_str, NUL); |
| 2604 | |
| 2605 | vgetc_busy = save_vgetc_busy; |
| 2606 | may_garbage_collect = save_may_garbage_collect; |
| 2607 | } |
| 2608 | else |
| 2609 | #endif |
| 2610 | map_str = mp->m_str; |
| 2611 | |
| 2612 | /* |
| 2613 | * Insert the 'to' part in the typebuf.tb_buf. |
Bram Moolenaar | eda35f7 | 2019-08-03 14:59:44 +0200 | [diff] [blame] | 2614 | * If 'from' field is the same as the start of the 'to' field, don't |
| 2615 | * remap the first character (but do allow abbreviations). |
| 2616 | * If m_noremap is set, don't remap the whole 'to' part. |
Bram Moolenaar | edd680f | 2019-08-03 14:23:48 +0200 | [diff] [blame] | 2617 | */ |
| 2618 | if (map_str == NULL) |
| 2619 | i = FAIL; |
| 2620 | else |
| 2621 | { |
| 2622 | int noremap; |
| 2623 | |
| 2624 | if (save_m_noremap != REMAP_YES) |
| 2625 | noremap = save_m_noremap; |
| 2626 | else if ( |
| 2627 | #ifdef FEAT_EVAL |
Bram Moolenaar | eda35f7 | 2019-08-03 14:59:44 +0200 | [diff] [blame] | 2628 | STRNCMP(map_str, save_m_keys != NULL ? save_m_keys : mp->m_keys, |
| 2629 | (size_t)keylen) |
Bram Moolenaar | edd680f | 2019-08-03 14:23:48 +0200 | [diff] [blame] | 2630 | #else |
| 2631 | STRNCMP(map_str, mp->m_keys, (size_t)keylen) |
| 2632 | #endif |
| 2633 | != 0) |
| 2634 | noremap = REMAP_YES; |
| 2635 | else |
| 2636 | noremap = REMAP_SKIP; |
| 2637 | i = ins_typebuf(map_str, noremap, |
| 2638 | 0, TRUE, cmd_silent || save_m_silent); |
| 2639 | #ifdef FEAT_EVAL |
| 2640 | if (save_m_expr) |
| 2641 | vim_free(map_str); |
| 2642 | #endif |
| 2643 | } |
| 2644 | #ifdef FEAT_EVAL |
| 2645 | vim_free(save_m_keys); |
| 2646 | vim_free(save_m_str); |
| 2647 | #endif |
| 2648 | *keylenp = keylen; |
| 2649 | if (i == FAIL) |
| 2650 | return map_result_fail; |
| 2651 | return map_result_retry; |
| 2652 | } |
| 2653 | |
| 2654 | *keylenp = keylen; |
| 2655 | return map_result_nomatch; |
| 2656 | } |
| 2657 | |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 2658 | /* |
| 2659 | * unget one character (can only be done once!) |
| 2660 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2661 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 2662 | vungetc(int c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2663 | { |
| 2664 | old_char = c; |
| 2665 | old_mod_mask = mod_mask; |
Bram Moolenaar | b897871 | 2013-03-16 21:42:16 +0100 | [diff] [blame] | 2666 | old_mouse_row = mouse_row; |
| 2667 | old_mouse_col = mouse_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2668 | } |
| 2669 | |
| 2670 | /* |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 2671 | * Get a byte: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2672 | * 1. from the stuffbuffer |
| 2673 | * This is used for abbreviated commands like "D" -> "d$". |
| 2674 | * Also used to redo a command for ".". |
| 2675 | * 2. from the typeahead buffer |
| 2676 | * Stores text obtained previously but not used yet. |
| 2677 | * Also stores the result of mappings. |
| 2678 | * Also used for the ":normal" command. |
| 2679 | * 3. from the user |
| 2680 | * This may do a blocking wait if "advance" is TRUE. |
| 2681 | * |
| 2682 | * if "advance" is TRUE (vgetc()): |
Bram Moolenaar | d29459b | 2016-08-26 22:29:11 +0200 | [diff] [blame] | 2683 | * Really get the character. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2684 | * KeyTyped is set to TRUE in the case the user typed the key. |
| 2685 | * KeyStuffed is TRUE if the character comes from the stuff buffer. |
| 2686 | * if "advance" is FALSE (vpeekc()): |
Bram Moolenaar | 6a2633b | 2018-10-07 23:16:36 +0200 | [diff] [blame] | 2687 | * Just look whether there is a character available. |
| 2688 | * Return NUL if not. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2689 | * |
| 2690 | * When "no_mapping" is zero, checks for mappings in the current mode. |
| 2691 | * Only returns one byte (of a multi-byte character). |
| 2692 | * K_SPECIAL and CSI may be escaped, need to get two more bytes then. |
| 2693 | */ |
| 2694 | static int |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 2695 | vgetorpeek(int advance) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2696 | { |
| 2697 | int c, c1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2698 | int timedout = FALSE; /* waited for more than 1 second |
| 2699 | for mapping to complete */ |
| 2700 | int mapdepth = 0; /* check for recursive mapping */ |
| 2701 | int mode_deleted = FALSE; /* set when mode has been deleted */ |
Bram Moolenaar | 4e42719 | 2006-03-10 21:34:27 +0000 | [diff] [blame] | 2702 | #ifdef FEAT_CMDL_INFO |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2703 | int new_wcol, new_wrow; |
| 2704 | #endif |
| 2705 | #ifdef FEAT_GUI |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2706 | int shape_changed = FALSE; /* adjusted cursor shape */ |
| 2707 | #endif |
| 2708 | int n; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2709 | int old_wcol, old_wrow; |
Bram Moolenaar | 28a37ff | 2005-03-15 22:28:00 +0000 | [diff] [blame] | 2710 | int wait_tb_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2711 | |
| 2712 | /* |
| 2713 | * This function doesn't work very well when called recursively. This may |
| 2714 | * happen though, because of: |
| 2715 | * 1. The call to add_to_showcmd(). char_avail() is then used to check if |
| 2716 | * there is a character available, which calls this function. In that |
| 2717 | * case we must return NUL, to indicate no character is available. |
| 2718 | * 2. A GUI callback function writes to the screen, causing a |
| 2719 | * wait_return(). |
| 2720 | * Using ":normal" can also do this, but it saves the typeahead buffer, |
| 2721 | * thus it should be OK. But don't get a key from the user then. |
| 2722 | */ |
Bram Moolenaar | e2c3810 | 2016-01-31 14:55:40 +0100 | [diff] [blame] | 2723 | if (vgetc_busy > 0 && ex_normal_busy == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2724 | return NUL; |
| 2725 | |
Bram Moolenaar | 5555acc | 2006-04-07 21:33:12 +0000 | [diff] [blame] | 2726 | ++vgetc_busy; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2727 | |
| 2728 | if (advance) |
| 2729 | KeyStuffed = FALSE; |
| 2730 | |
| 2731 | init_typebuf(); |
| 2732 | start_stuff(); |
| 2733 | if (advance && typebuf.tb_maplen == 0) |
Bram Moolenaar | 0b6d911 | 2018-05-22 20:35:17 +0200 | [diff] [blame] | 2734 | reg_executing = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2735 | do |
| 2736 | { |
| 2737 | /* |
| 2738 | * get a character: 1. from the stuffbuffer |
| 2739 | */ |
| 2740 | if (typeahead_char != 0) |
| 2741 | { |
| 2742 | c = typeahead_char; |
| 2743 | if (advance) |
| 2744 | typeahead_char = 0; |
| 2745 | } |
| 2746 | else |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 2747 | c = read_readbuffers(advance); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2748 | if (c != NUL && !got_int) |
| 2749 | { |
| 2750 | if (advance) |
| 2751 | { |
| 2752 | /* KeyTyped = FALSE; When the command that stuffed something |
| 2753 | * was typed, behave like the stuffed command was typed. |
Bram Moolenaar | caa55b6 | 2017-01-10 13:51:09 +0100 | [diff] [blame] | 2754 | * needed for CTRL-W CTRL-] to open a fold, for example. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2755 | KeyStuffed = TRUE; |
| 2756 | } |
| 2757 | if (typebuf.tb_no_abbr_cnt == 0) |
| 2758 | typebuf.tb_no_abbr_cnt = 1; /* no abbreviations now */ |
| 2759 | } |
| 2760 | else |
| 2761 | { |
| 2762 | /* |
| 2763 | * Loop until we either find a matching mapped key, or we |
| 2764 | * are sure that it is not a mapped key. |
| 2765 | * If a mapped key sequence is found we go back to the start to |
| 2766 | * try re-mapping. |
| 2767 | */ |
| 2768 | for (;;) |
| 2769 | { |
Bram Moolenaar | edd680f | 2019-08-03 14:23:48 +0200 | [diff] [blame] | 2770 | long wait_time; |
| 2771 | int keylen = 0; |
Bram Moolenaar | eda35f7 | 2019-08-03 14:59:44 +0200 | [diff] [blame] | 2772 | #ifdef FEAT_CMDL_INFO |
| 2773 | int showcmd_idx; |
| 2774 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2775 | /* |
| 2776 | * ui_breakcheck() is slow, don't use it too often when |
| 2777 | * inside a mapping. But call it each time for typed |
| 2778 | * characters. |
| 2779 | */ |
| 2780 | if (typebuf.tb_maplen) |
| 2781 | line_breakcheck(); |
| 2782 | else |
| 2783 | ui_breakcheck(); /* check for CTRL-C */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2784 | if (got_int) |
| 2785 | { |
| 2786 | /* flush all input */ |
Bram Moolenaar | 0f0f230 | 2017-08-30 18:52:56 +0200 | [diff] [blame] | 2787 | c = inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 0L); |
Bram Moolenaar | edd680f | 2019-08-03 14:23:48 +0200 | [diff] [blame] | 2788 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2789 | /* |
| 2790 | * If inchar() returns TRUE (script file was active) or we |
Bram Moolenaar | b2ac14c | 2018-05-01 18:47:59 +0200 | [diff] [blame] | 2791 | * are inside a mapping, get out of Insert mode. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2792 | * Otherwise we behave like having gotten a CTRL-C. |
| 2793 | * As a result typing CTRL-C in insert mode will |
| 2794 | * really insert a CTRL-C. |
| 2795 | */ |
| 2796 | if ((c || typebuf.tb_maplen) |
| 2797 | && (State & (INSERT + CMDLINE))) |
| 2798 | c = ESC; |
| 2799 | else |
| 2800 | c = Ctrl_C; |
Bram Moolenaar | 6a2633b | 2018-10-07 23:16:36 +0200 | [diff] [blame] | 2801 | flush_buffers(FLUSH_INPUT); // flush all typeahead |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2802 | |
Bram Moolenaar | d9dfd57 | 2006-10-03 13:36:13 +0000 | [diff] [blame] | 2803 | if (advance) |
| 2804 | { |
| 2805 | /* Also record this character, it might be needed to |
| 2806 | * get out of Insert mode. */ |
| 2807 | *typebuf.tb_buf = c; |
| 2808 | gotchars(typebuf.tb_buf, 1); |
| 2809 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2810 | cmd_silent = FALSE; |
| 2811 | |
| 2812 | break; |
| 2813 | } |
| 2814 | else if (typebuf.tb_len > 0) |
| 2815 | { |
| 2816 | /* |
Bram Moolenaar | edd680f | 2019-08-03 14:23:48 +0200 | [diff] [blame] | 2817 | * Check for a mapping in "typebuf". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2818 | */ |
Bram Moolenaar | edd680f | 2019-08-03 14:23:48 +0200 | [diff] [blame] | 2819 | map_result_T result = handle_mapping( |
| 2820 | &keylen, &timedout, &mapdepth); |
| 2821 | |
| 2822 | if (result == map_result_retry) |
| 2823 | // try mapping again |
| 2824 | continue; |
| 2825 | if (result == map_result_fail) |
Bram Moolenaar | f2d8b7a | 2019-08-02 22:46:11 +0200 | [diff] [blame] | 2826 | { |
Bram Moolenaar | edd680f | 2019-08-03 14:23:48 +0200 | [diff] [blame] | 2827 | // failed, use the outer loop |
| 2828 | c = -1; |
| 2829 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2830 | } |
Bram Moolenaar | edd680f | 2019-08-03 14:23:48 +0200 | [diff] [blame] | 2831 | if (result == map_result_get) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2832 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2833 | /* |
| 2834 | * get a character: 2. from the typeahead buffer |
| 2835 | */ |
Bram Moolenaar | edd680f | 2019-08-03 14:23:48 +0200 | [diff] [blame] | 2836 | c = typebuf.tb_buf[typebuf.tb_off] & 255; |
| 2837 | if (advance) /* remove chars from tb_buf */ |
Bram Moolenaar | f2d8b7a | 2019-08-02 22:46:11 +0200 | [diff] [blame] | 2838 | { |
Bram Moolenaar | edd680f | 2019-08-03 14:23:48 +0200 | [diff] [blame] | 2839 | cmd_silent = (typebuf.tb_silent > 0); |
| 2840 | if (typebuf.tb_maplen > 0) |
| 2841 | KeyTyped = FALSE; |
| 2842 | else |
Bram Moolenaar | f2d8b7a | 2019-08-02 22:46:11 +0200 | [diff] [blame] | 2843 | { |
Bram Moolenaar | edd680f | 2019-08-03 14:23:48 +0200 | [diff] [blame] | 2844 | KeyTyped = TRUE; |
| 2845 | /* write char to script file(s) */ |
| 2846 | gotchars(typebuf.tb_buf |
| 2847 | + typebuf.tb_off, 1); |
Bram Moolenaar | f2d8b7a | 2019-08-02 22:46:11 +0200 | [diff] [blame] | 2848 | } |
Bram Moolenaar | edd680f | 2019-08-03 14:23:48 +0200 | [diff] [blame] | 2849 | KeyNoremap = typebuf.tb_noremap[ |
| 2850 | typebuf.tb_off]; |
| 2851 | del_typebuf(1, 0); |
Bram Moolenaar | f2d8b7a | 2019-08-02 22:46:11 +0200 | [diff] [blame] | 2852 | } |
Bram Moolenaar | edd680f | 2019-08-03 14:23:48 +0200 | [diff] [blame] | 2853 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2854 | } |
| 2855 | |
Bram Moolenaar | edd680f | 2019-08-03 14:23:48 +0200 | [diff] [blame] | 2856 | // not enough characters, get more |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2857 | } |
| 2858 | |
| 2859 | /* |
| 2860 | * get a character: 3. from the user - handle <Esc> in Insert mode |
| 2861 | */ |
| 2862 | /* |
Bram Moolenaar | f085f42 | 2017-06-07 20:39:47 +0200 | [diff] [blame] | 2863 | * Special case: if we get an <ESC> in insert mode and there |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2864 | * are no more characters at once, we pretend to go out of |
| 2865 | * insert mode. This prevents the one second delay after |
| 2866 | * typing an <ESC>. If we get something after all, we may |
| 2867 | * have to redisplay the mode. That the cursor is in the wrong |
| 2868 | * place does not matter. |
| 2869 | */ |
| 2870 | c = 0; |
| 2871 | #ifdef FEAT_CMDL_INFO |
| 2872 | new_wcol = curwin->w_wcol; |
| 2873 | new_wrow = curwin->w_wrow; |
| 2874 | #endif |
| 2875 | if ( advance |
| 2876 | && typebuf.tb_len == 1 |
| 2877 | && typebuf.tb_buf[typebuf.tb_off] == ESC |
| 2878 | && !no_mapping |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2879 | && ex_normal_busy == 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2880 | && typebuf.tb_maplen == 0 |
| 2881 | && (State & INSERT) |
Bram Moolenaar | 946ffd4 | 2010-12-30 12:30:31 +0100 | [diff] [blame] | 2882 | && (p_timeout |
| 2883 | || (keylen == KEYLEN_PART_KEY && p_ttimeout)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2884 | && (c = inchar(typebuf.tb_buf + typebuf.tb_off |
Bram Moolenaar | 0f0f230 | 2017-08-30 18:52:56 +0200 | [diff] [blame] | 2885 | + typebuf.tb_len, 3, 25L)) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2886 | { |
| 2887 | colnr_T col = 0, vcol; |
| 2888 | char_u *ptr; |
| 2889 | |
Bram Moolenaar | 28c258f | 2006-01-25 22:02:51 +0000 | [diff] [blame] | 2890 | if (mode_displayed) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2891 | { |
| 2892 | unshowmode(TRUE); |
| 2893 | mode_deleted = TRUE; |
| 2894 | } |
| 2895 | #ifdef FEAT_GUI |
Bram Moolenaar | f085f42 | 2017-06-07 20:39:47 +0200 | [diff] [blame] | 2896 | /* may show a different cursor shape */ |
| 2897 | if (gui.in_use && State != NORMAL && !cmd_silent) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2898 | { |
| 2899 | int save_State; |
| 2900 | |
| 2901 | save_State = State; |
| 2902 | State = NORMAL; |
| 2903 | gui_update_cursor(TRUE, FALSE); |
| 2904 | State = save_State; |
| 2905 | shape_changed = TRUE; |
| 2906 | } |
| 2907 | #endif |
| 2908 | validate_cursor(); |
| 2909 | old_wcol = curwin->w_wcol; |
| 2910 | old_wrow = curwin->w_wrow; |
| 2911 | |
| 2912 | /* move cursor left, if possible */ |
| 2913 | if (curwin->w_cursor.col != 0) |
| 2914 | { |
| 2915 | if (curwin->w_wcol > 0) |
| 2916 | { |
| 2917 | if (did_ai) |
| 2918 | { |
| 2919 | /* |
| 2920 | * We are expecting to truncate the trailing |
| 2921 | * white-space, so find the last non-white |
| 2922 | * character -- webb |
| 2923 | */ |
| 2924 | col = vcol = curwin->w_wcol = 0; |
| 2925 | ptr = ml_get_curline(); |
| 2926 | while (col < curwin->w_cursor.col) |
| 2927 | { |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 2928 | if (!VIM_ISWHITE(ptr[col])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2929 | curwin->w_wcol = vcol; |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2930 | vcol += lbr_chartabsize(ptr, ptr + col, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2931 | (colnr_T)vcol); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2932 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2933 | col += (*mb_ptr2len)(ptr + col); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2934 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2935 | ++col; |
| 2936 | } |
| 2937 | curwin->w_wrow = curwin->w_cline_row |
Bram Moolenaar | 0263146 | 2017-09-22 15:20:32 +0200 | [diff] [blame] | 2938 | + curwin->w_wcol / curwin->w_width; |
| 2939 | curwin->w_wcol %= curwin->w_width; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2940 | curwin->w_wcol += curwin_col_off(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2941 | col = 0; /* no correction needed */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2942 | } |
| 2943 | else |
| 2944 | { |
| 2945 | --curwin->w_wcol; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2946 | col = curwin->w_cursor.col - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2947 | } |
| 2948 | } |
| 2949 | else if (curwin->w_p_wrap && curwin->w_wrow) |
| 2950 | { |
| 2951 | --curwin->w_wrow; |
Bram Moolenaar | 0263146 | 2017-09-22 15:20:32 +0200 | [diff] [blame] | 2952 | curwin->w_wcol = curwin->w_width - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2953 | col = curwin->w_cursor.col - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2954 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2955 | if (has_mbyte && col > 0 && curwin->w_wcol > 0) |
| 2956 | { |
| 2957 | /* Correct when the cursor is on the right halve |
| 2958 | * of a double-wide character. */ |
| 2959 | ptr = ml_get_curline(); |
| 2960 | col -= (*mb_head_off)(ptr, ptr + col); |
| 2961 | if ((*mb_ptr2cells)(ptr + col) > 1) |
| 2962 | --curwin->w_wcol; |
| 2963 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2964 | } |
| 2965 | setcursor(); |
| 2966 | out_flush(); |
| 2967 | #ifdef FEAT_CMDL_INFO |
| 2968 | new_wcol = curwin->w_wcol; |
| 2969 | new_wrow = curwin->w_wrow; |
| 2970 | #endif |
| 2971 | curwin->w_wcol = old_wcol; |
| 2972 | curwin->w_wrow = old_wrow; |
| 2973 | } |
| 2974 | if (c < 0) |
| 2975 | continue; /* end of input script reached */ |
Bram Moolenaar | 20c3892 | 2014-07-23 20:41:14 +0200 | [diff] [blame] | 2976 | |
| 2977 | /* Allow mapping for just typed characters. When we get here c |
| 2978 | * is the number of extra bytes and typebuf.tb_len is 1. */ |
| 2979 | for (n = 1; n <= c; ++n) |
| 2980 | typebuf.tb_noremap[typebuf.tb_off + n] = RM_YES; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2981 | typebuf.tb_len += c; |
| 2982 | |
| 2983 | /* buffer full, don't map */ |
| 2984 | if (typebuf.tb_len >= typebuf.tb_maplen + MAXMAPLEN) |
| 2985 | { |
| 2986 | timedout = TRUE; |
| 2987 | continue; |
| 2988 | } |
| 2989 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2990 | if (ex_normal_busy > 0) |
| 2991 | { |
Bram Moolenaar | e2c3810 | 2016-01-31 14:55:40 +0100 | [diff] [blame] | 2992 | #ifdef FEAT_CMDWIN |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2993 | static int tc = 0; |
Bram Moolenaar | e2c3810 | 2016-01-31 14:55:40 +0100 | [diff] [blame] | 2994 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2995 | |
| 2996 | /* No typeahead left and inside ":normal". Must return |
| 2997 | * something to avoid getting stuck. When an incomplete |
| 2998 | * mapping is present, behave like it timed out. */ |
| 2999 | if (typebuf.tb_len > 0) |
| 3000 | { |
| 3001 | timedout = TRUE; |
| 3002 | continue; |
| 3003 | } |
| 3004 | /* When 'insertmode' is set, ESC just beeps in Insert |
| 3005 | * mode. Use CTRL-L to make edit() return. |
| 3006 | * For the command line only CTRL-C always breaks it. |
| 3007 | * For the cmdline window: Alternate between ESC and |
| 3008 | * CTRL-C: ESC for most situations and CTRL-C to close the |
| 3009 | * cmdline window. */ |
| 3010 | if (p_im && (State & INSERT)) |
| 3011 | c = Ctrl_L; |
Bram Moolenaar | b2ac14c | 2018-05-01 18:47:59 +0200 | [diff] [blame] | 3012 | #ifdef FEAT_TERMINAL |
| 3013 | else if (terminal_is_active()) |
| 3014 | c = K_CANCEL; |
| 3015 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3016 | else if ((State & CMDLINE) |
Bram Moolenaar | e2c3810 | 2016-01-31 14:55:40 +0100 | [diff] [blame] | 3017 | #ifdef FEAT_CMDWIN |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3018 | || (cmdwin_type > 0 && tc == ESC) |
Bram Moolenaar | e2c3810 | 2016-01-31 14:55:40 +0100 | [diff] [blame] | 3019 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3020 | ) |
| 3021 | c = Ctrl_C; |
| 3022 | else |
| 3023 | c = ESC; |
Bram Moolenaar | e2c3810 | 2016-01-31 14:55:40 +0100 | [diff] [blame] | 3024 | #ifdef FEAT_CMDWIN |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3025 | tc = c; |
Bram Moolenaar | e2c3810 | 2016-01-31 14:55:40 +0100 | [diff] [blame] | 3026 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3027 | break; |
| 3028 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3029 | |
| 3030 | /* |
| 3031 | * get a character: 3. from the user - update display |
| 3032 | */ |
| 3033 | /* In insert mode a screen update is skipped when characters |
| 3034 | * are still available. But when those available characters |
| 3035 | * are part of a mapping, and we are going to do a blocking |
| 3036 | * wait here. Need to update the screen to display the |
Bram Moolenaar | 6eb634e | 2011-03-03 15:04:08 +0100 | [diff] [blame] | 3037 | * changed text so far. Also for when 'lazyredraw' is set and |
| 3038 | * redrawing was postponed because there was something in the |
| 3039 | * input buffer (e.g., termresponse). */ |
Bram Moolenaar | fd30cd4 | 2011-03-22 13:07:26 +0100 | [diff] [blame] | 3040 | if (((State & INSERT) != 0 || p_lz) && (State & CMDLINE) == 0 |
| 3041 | && advance && must_redraw != 0 && !need_wait_return) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3042 | { |
| 3043 | update_screen(0); |
| 3044 | setcursor(); /* put cursor back where it belongs */ |
| 3045 | } |
| 3046 | |
| 3047 | /* |
| 3048 | * If we have a partial match (and are going to wait for more |
| 3049 | * input from the user), show the partially matched characters |
| 3050 | * to the user with showcmd. |
| 3051 | */ |
| 3052 | #ifdef FEAT_CMDL_INFO |
Bram Moolenaar | eda35f7 | 2019-08-03 14:59:44 +0200 | [diff] [blame] | 3053 | showcmd_idx = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3054 | #endif |
| 3055 | c1 = 0; |
| 3056 | if (typebuf.tb_len > 0 && advance && !exmode_active) |
| 3057 | { |
| 3058 | if (((State & (NORMAL | INSERT)) || State == LANGMAP) |
| 3059 | && State != HITRETURN) |
| 3060 | { |
| 3061 | /* this looks nice when typing a dead character map */ |
| 3062 | if (State & INSERT |
| 3063 | && ptr2cells(typebuf.tb_buf + typebuf.tb_off |
| 3064 | + typebuf.tb_len - 1) == 1) |
| 3065 | { |
| 3066 | edit_putchar(typebuf.tb_buf[typebuf.tb_off |
| 3067 | + typebuf.tb_len - 1], FALSE); |
| 3068 | setcursor(); /* put cursor back where it belongs */ |
| 3069 | c1 = 1; |
| 3070 | } |
| 3071 | #ifdef FEAT_CMDL_INFO |
| 3072 | /* need to use the col and row from above here */ |
| 3073 | old_wcol = curwin->w_wcol; |
| 3074 | old_wrow = curwin->w_wrow; |
| 3075 | curwin->w_wcol = new_wcol; |
| 3076 | curwin->w_wrow = new_wrow; |
| 3077 | push_showcmd(); |
| 3078 | if (typebuf.tb_len > SHOWCMD_COLS) |
Bram Moolenaar | eda35f7 | 2019-08-03 14:59:44 +0200 | [diff] [blame] | 3079 | showcmd_idx = typebuf.tb_len - SHOWCMD_COLS; |
| 3080 | while (showcmd_idx < typebuf.tb_len) |
| 3081 | (void)add_to_showcmd( |
| 3082 | typebuf.tb_buf[typebuf.tb_off + showcmd_idx++]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3083 | curwin->w_wcol = old_wcol; |
| 3084 | curwin->w_wrow = old_wrow; |
| 3085 | #endif |
| 3086 | } |
| 3087 | |
| 3088 | /* this looks nice when typing a dead character map */ |
| 3089 | if ((State & CMDLINE) |
| 3090 | #if defined(FEAT_CRYPT) || defined(FEAT_EVAL) |
| 3091 | && cmdline_star == 0 |
| 3092 | #endif |
| 3093 | && ptr2cells(typebuf.tb_buf + typebuf.tb_off |
| 3094 | + typebuf.tb_len - 1) == 1) |
| 3095 | { |
| 3096 | putcmdline(typebuf.tb_buf[typebuf.tb_off |
| 3097 | + typebuf.tb_len - 1], FALSE); |
| 3098 | c1 = 1; |
| 3099 | } |
| 3100 | } |
| 3101 | |
| 3102 | /* |
| 3103 | * get a character: 3. from the user - get it |
| 3104 | */ |
Bram Moolenaar | 83f4cbd | 2018-06-12 21:35:40 +0200 | [diff] [blame] | 3105 | if (typebuf.tb_len == 0) |
| 3106 | // timedout may have been set while waiting for a mapping |
| 3107 | // that has a <Nop> RHS. |
| 3108 | timedout = FALSE; |
| 3109 | |
Bram Moolenaar | 652de23 | 2019-04-04 20:13:09 +0200 | [diff] [blame] | 3110 | if (advance) |
| 3111 | { |
| 3112 | if (typebuf.tb_len == 0 |
| 3113 | || !(p_timeout |
| 3114 | || (p_ttimeout && keylen == KEYLEN_PART_KEY))) |
| 3115 | // blocking wait |
| 3116 | wait_time = -1L; |
| 3117 | else if (keylen == KEYLEN_PART_KEY && p_ttm >= 0) |
| 3118 | wait_time = p_ttm; |
| 3119 | else |
| 3120 | wait_time = p_tm; |
| 3121 | } |
| 3122 | else |
| 3123 | wait_time = 0; |
| 3124 | |
Bram Moolenaar | 28a37ff | 2005-03-15 22:28:00 +0000 | [diff] [blame] | 3125 | wait_tb_len = typebuf.tb_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3126 | c = inchar(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len, |
| 3127 | typebuf.tb_buflen - typebuf.tb_off - typebuf.tb_len - 1, |
Bram Moolenaar | 652de23 | 2019-04-04 20:13:09 +0200 | [diff] [blame] | 3128 | wait_time); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3129 | |
| 3130 | #ifdef FEAT_CMDL_INFO |
Bram Moolenaar | eda35f7 | 2019-08-03 14:59:44 +0200 | [diff] [blame] | 3131 | if (showcmd_idx != 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3132 | pop_showcmd(); |
| 3133 | #endif |
| 3134 | if (c1 == 1) |
| 3135 | { |
| 3136 | if (State & INSERT) |
| 3137 | edit_unputchar(); |
| 3138 | if (State & CMDLINE) |
| 3139 | unputcmdline(); |
Bram Moolenaar | bc256d9 | 2012-06-06 12:06:15 +0200 | [diff] [blame] | 3140 | else |
| 3141 | setcursor(); /* put cursor back where it belongs */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3142 | } |
| 3143 | |
| 3144 | if (c < 0) |
| 3145 | continue; /* end of input script reached */ |
| 3146 | if (c == NUL) /* no character available */ |
| 3147 | { |
| 3148 | if (!advance) |
| 3149 | break; |
Bram Moolenaar | 28a37ff | 2005-03-15 22:28:00 +0000 | [diff] [blame] | 3150 | if (wait_tb_len > 0) /* timed out */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3151 | { |
| 3152 | timedout = TRUE; |
| 3153 | continue; |
| 3154 | } |
| 3155 | } |
| 3156 | else |
| 3157 | { /* allow mapping for just typed characters */ |
| 3158 | while (typebuf.tb_buf[typebuf.tb_off |
| 3159 | + typebuf.tb_len] != NUL) |
| 3160 | typebuf.tb_noremap[typebuf.tb_off |
| 3161 | + typebuf.tb_len++] = RM_YES; |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 3162 | #ifdef HAVE_INPUT_METHOD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3163 | /* Get IM status right after getting keys, not after the |
| 3164 | * timeout for a mapping (focus may be lost by then). */ |
| 3165 | vgetc_im_active = im_get_status(); |
| 3166 | #endif |
| 3167 | } |
| 3168 | } /* for (;;) */ |
| 3169 | } /* if (!character from stuffbuf) */ |
| 3170 | |
Bram Moolenaar | b2ac14c | 2018-05-01 18:47:59 +0200 | [diff] [blame] | 3171 | /* if advance is FALSE don't loop on NULs */ |
| 3172 | } while ((c < 0 && c != K_CANCEL) || (advance && c == NUL)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3173 | |
| 3174 | /* |
| 3175 | * The "INSERT" message is taken care of here: |
| 3176 | * if we return an ESC to exit insert mode, the message is deleted |
| 3177 | * if we don't return an ESC but deleted the message before, redisplay it |
| 3178 | */ |
Bram Moolenaar | 09df312 | 2006-01-23 22:23:09 +0000 | [diff] [blame] | 3179 | if (advance && p_smd && msg_silent == 0 && (State & INSERT)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3180 | { |
Bram Moolenaar | 28c258f | 2006-01-25 22:02:51 +0000 | [diff] [blame] | 3181 | if (c == ESC && !mode_deleted && !no_mapping && mode_displayed) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3182 | { |
| 3183 | if (typebuf.tb_len && !KeyTyped) |
| 3184 | redraw_cmdline = TRUE; /* delete mode later */ |
| 3185 | else |
| 3186 | unshowmode(FALSE); |
| 3187 | } |
| 3188 | else if (c != ESC && mode_deleted) |
| 3189 | { |
| 3190 | if (typebuf.tb_len && !KeyTyped) |
| 3191 | redraw_cmdline = TRUE; /* show mode later */ |
| 3192 | else |
| 3193 | showmode(); |
| 3194 | } |
| 3195 | } |
| 3196 | #ifdef FEAT_GUI |
| 3197 | /* may unshow different cursor shape */ |
Bram Moolenaar | f085f42 | 2017-06-07 20:39:47 +0200 | [diff] [blame] | 3198 | if (gui.in_use && shape_changed) |
| 3199 | gui_update_cursor(TRUE, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3200 | #endif |
Bram Moolenaar | 6edbbd8 | 2019-03-10 09:41:51 +0100 | [diff] [blame] | 3201 | if (timedout && c == ESC) |
| 3202 | { |
| 3203 | char_u nop_buf[3]; |
| 3204 | |
| 3205 | // When recording there will be no timeout. Add a <Nop> after the ESC |
| 3206 | // to avoid that it forms a key code with following characters. |
| 3207 | nop_buf[0] = K_SPECIAL; |
| 3208 | nop_buf[1] = KS_EXTRA; |
| 3209 | nop_buf[2] = KE_NOP; |
| 3210 | gotchars(nop_buf, 3); |
| 3211 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3212 | |
Bram Moolenaar | 5555acc | 2006-04-07 21:33:12 +0000 | [diff] [blame] | 3213 | --vgetc_busy; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3214 | |
| 3215 | return c; |
| 3216 | } |
| 3217 | |
| 3218 | /* |
| 3219 | * inchar() - get one character from |
| 3220 | * 1. a scriptfile |
| 3221 | * 2. the keyboard |
| 3222 | * |
| 3223 | * As much characters as we can get (upto 'maxlen') are put in "buf" and |
| 3224 | * NUL terminated (buffer length must be 'maxlen' + 1). |
| 3225 | * Minimum for "maxlen" is 3!!!! |
| 3226 | * |
| 3227 | * "tb_change_cnt" is the value of typebuf.tb_change_cnt if "buf" points into |
| 3228 | * it. When typebuf.tb_change_cnt changes (e.g., when a message is received |
| 3229 | * from a remote client) "buf" can no longer be used. "tb_change_cnt" is 0 |
| 3230 | * otherwise. |
| 3231 | * |
| 3232 | * If we got an interrupt all input is read until none is available. |
| 3233 | * |
| 3234 | * If wait_time == 0 there is no waiting for the char. |
| 3235 | * If wait_time == n we wait for n msec for a character to arrive. |
| 3236 | * If wait_time == -1 we wait forever for a character to arrive. |
| 3237 | * |
| 3238 | * Return the number of obtained characters. |
| 3239 | * Return -1 when end of input script reached. |
| 3240 | */ |
Bram Moolenaar | cda7764 | 2016-06-04 13:32:35 +0200 | [diff] [blame] | 3241 | static int |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 3242 | inchar( |
| 3243 | char_u *buf, |
| 3244 | int maxlen, |
Bram Moolenaar | 0f0f230 | 2017-08-30 18:52:56 +0200 | [diff] [blame] | 3245 | long wait_time) /* milli seconds */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3246 | { |
| 3247 | int len = 0; /* init for GCC */ |
| 3248 | int retesc = FALSE; /* return ESC with gotint */ |
| 3249 | int script_char; |
Bram Moolenaar | 0f0f230 | 2017-08-30 18:52:56 +0200 | [diff] [blame] | 3250 | int tb_change_cnt = typebuf.tb_change_cnt; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3251 | |
| 3252 | if (wait_time == -1L || wait_time > 100L) /* flush output before waiting */ |
| 3253 | { |
| 3254 | cursor_on(); |
Bram Moolenaar | a338adc | 2018-01-31 20:51:47 +0100 | [diff] [blame] | 3255 | out_flush_cursor(FALSE, FALSE); |
| 3256 | #if defined(FEAT_GUI) && defined(FEAT_MOUSESHAPE) |
| 3257 | if (gui.in_use && postponed_mouseshape) |
| 3258 | update_mouseshape(-1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3259 | #endif |
| 3260 | } |
| 3261 | |
| 3262 | /* |
| 3263 | * Don't reset these when at the hit-return prompt, otherwise a endless |
| 3264 | * recursive loop may result (write error in swapfile, hit-return, timeout |
| 3265 | * on char wait, flush swapfile, write error....). |
| 3266 | */ |
| 3267 | if (State != HITRETURN) |
| 3268 | { |
| 3269 | did_outofmem_msg = FALSE; /* display out of memory message (again) */ |
| 3270 | did_swapwrite_msg = FALSE; /* display swap file write error again */ |
| 3271 | } |
| 3272 | undo_off = FALSE; /* restart undo now */ |
| 3273 | |
| 3274 | /* |
Bram Moolenaar | ee3f7a5 | 2008-01-01 13:17:56 +0000 | [diff] [blame] | 3275 | * Get a character from a script file if there is one. |
| 3276 | * If interrupted: Stop reading script files, close them all. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3277 | */ |
| 3278 | script_char = -1; |
Bram Moolenaar | ee3f7a5 | 2008-01-01 13:17:56 +0000 | [diff] [blame] | 3279 | while (scriptin[curscript] != NULL && script_char < 0 |
| 3280 | #ifdef FEAT_EVAL |
| 3281 | && !ignore_script |
| 3282 | #endif |
| 3283 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3284 | { |
Bram Moolenaar | 93c88e0 | 2015-09-15 14:12:05 +0200 | [diff] [blame] | 3285 | #ifdef MESSAGE_QUEUE |
| 3286 | parse_queued_messages(); |
Bram Moolenaar | f233048 | 2008-06-24 20:19:36 +0000 | [diff] [blame] | 3287 | #endif |
| 3288 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3289 | if (got_int || (script_char = getc(scriptin[curscript])) < 0) |
| 3290 | { |
| 3291 | /* Reached EOF. |
| 3292 | * Careful: closescript() frees typebuf.tb_buf[] and buf[] may |
| 3293 | * point inside typebuf.tb_buf[]. Don't use buf[] after this! */ |
| 3294 | closescript(); |
| 3295 | /* |
| 3296 | * When reading script file is interrupted, return an ESC to get |
| 3297 | * back to normal mode. |
| 3298 | * Otherwise return -1, because typebuf.tb_buf[] has changed. |
| 3299 | */ |
| 3300 | if (got_int) |
| 3301 | retesc = TRUE; |
| 3302 | else |
| 3303 | return -1; |
| 3304 | } |
| 3305 | else |
| 3306 | { |
| 3307 | buf[0] = script_char; |
| 3308 | len = 1; |
| 3309 | } |
| 3310 | } |
| 3311 | |
| 3312 | if (script_char < 0) /* did not get a character from script */ |
| 3313 | { |
| 3314 | /* |
| 3315 | * If we got an interrupt, skip all previously typed characters and |
| 3316 | * return TRUE if quit reading script file. |
| 3317 | * Stop reading typeahead when a single CTRL-C was read, |
| 3318 | * fill_input_buf() returns this when not able to read from stdin. |
| 3319 | * Don't use buf[] here, closescript() may have freed typebuf.tb_buf[] |
| 3320 | * and buf may be pointing inside typebuf.tb_buf[]. |
| 3321 | */ |
| 3322 | if (got_int) |
| 3323 | { |
| 3324 | #define DUM_LEN MAXMAPLEN * 3 + 3 |
| 3325 | char_u dum[DUM_LEN + 1]; |
| 3326 | |
| 3327 | for (;;) |
| 3328 | { |
| 3329 | len = ui_inchar(dum, DUM_LEN, 0L, 0); |
| 3330 | if (len == 0 || (len == 1 && dum[0] == 3)) |
| 3331 | break; |
| 3332 | } |
| 3333 | return retesc; |
| 3334 | } |
| 3335 | |
| 3336 | /* |
| 3337 | * Always flush the output characters when getting input characters |
Bram Moolenaar | cb574f4 | 2019-01-25 22:29:57 +0100 | [diff] [blame] | 3338 | * from the user and not just peeking. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3339 | */ |
Bram Moolenaar | cb574f4 | 2019-01-25 22:29:57 +0100 | [diff] [blame] | 3340 | if (wait_time == -1L || wait_time > 10L) |
| 3341 | out_flush(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3342 | |
| 3343 | /* |
| 3344 | * Fill up to a third of the buffer, because each character may be |
| 3345 | * tripled below. |
| 3346 | */ |
| 3347 | len = ui_inchar(buf, maxlen / 3, wait_time, tb_change_cnt); |
| 3348 | } |
| 3349 | |
Bram Moolenaar | 0f0f230 | 2017-08-30 18:52:56 +0200 | [diff] [blame] | 3350 | /* If the typebuf was changed further down, it is like nothing was added by |
| 3351 | * this call. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3352 | if (typebuf_changed(tb_change_cnt)) |
| 3353 | return 0; |
| 3354 | |
Bram Moolenaar | 0f0f230 | 2017-08-30 18:52:56 +0200 | [diff] [blame] | 3355 | /* Note the change in the typeahead buffer, this matters for when |
| 3356 | * vgetorpeek() is called recursively, e.g. using getchar(1) in a timer |
| 3357 | * function. */ |
| 3358 | if (len > 0 && ++typebuf.tb_change_cnt == 0) |
| 3359 | typebuf.tb_change_cnt = 1; |
| 3360 | |
Bram Moolenaar | 6bff02e | 2016-08-16 22:50:55 +0200 | [diff] [blame] | 3361 | return fix_input_buffer(buf, len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3362 | } |
| 3363 | |
| 3364 | /* |
| 3365 | * Fix typed characters for use by vgetc() and check_termcode(). |
Bram Moolenaar | ed5ab2a | 2019-05-04 20:00:00 +0200 | [diff] [blame] | 3366 | * "buf[]" must have room to triple the number of bytes! |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3367 | * Returns the new length. |
| 3368 | */ |
| 3369 | int |
Bram Moolenaar | 6bff02e | 2016-08-16 22:50:55 +0200 | [diff] [blame] | 3370 | fix_input_buffer(char_u *buf, int len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3371 | { |
| 3372 | int i; |
| 3373 | char_u *p = buf; |
| 3374 | |
| 3375 | /* |
| 3376 | * Two characters are special: NUL and K_SPECIAL. |
| 3377 | * When compiled With the GUI CSI is also special. |
| 3378 | * Replace NUL by K_SPECIAL KS_ZERO KE_FILLER |
| 3379 | * Replace K_SPECIAL by K_SPECIAL KS_SPECIAL KE_FILLER |
| 3380 | * Replace CSI by K_SPECIAL KS_EXTRA KE_CSI |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3381 | */ |
| 3382 | for (i = len; --i >= 0; ++p) |
| 3383 | { |
| 3384 | #ifdef FEAT_GUI |
| 3385 | /* When the GUI is used any character can come after a CSI, don't |
| 3386 | * escape it. */ |
| 3387 | if (gui.in_use && p[0] == CSI && i >= 2) |
| 3388 | { |
| 3389 | p += 2; |
| 3390 | i -= 2; |
| 3391 | } |
Bram Moolenaar | afde13b | 2019-04-28 19:46:49 +0200 | [diff] [blame] | 3392 | # ifndef MSWIN |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3393 | /* When the GUI is not used CSI needs to be escaped. */ |
| 3394 | else if (!gui.in_use && p[0] == CSI) |
| 3395 | { |
| 3396 | mch_memmove(p + 3, p + 1, (size_t)i); |
| 3397 | *p++ = K_SPECIAL; |
| 3398 | *p++ = KS_EXTRA; |
| 3399 | *p = (int)KE_CSI; |
| 3400 | len += 2; |
| 3401 | } |
Bram Moolenaar | afde13b | 2019-04-28 19:46:49 +0200 | [diff] [blame] | 3402 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3403 | else |
| 3404 | #endif |
Bram Moolenaar | 6bff02e | 2016-08-16 22:50:55 +0200 | [diff] [blame] | 3405 | if (p[0] == NUL || (p[0] == K_SPECIAL |
Bram Moolenaar | ed5ab2a | 2019-05-04 20:00:00 +0200 | [diff] [blame] | 3406 | // timeout may generate K_CURSORHOLD |
Bram Moolenaar | 28a37ff | 2005-03-15 22:28:00 +0000 | [diff] [blame] | 3407 | && (i < 2 || p[1] != KS_EXTRA || p[2] != (int)KE_CURSORHOLD) |
Bram Moolenaar | afde13b | 2019-04-28 19:46:49 +0200 | [diff] [blame] | 3408 | #if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL)) |
Bram Moolenaar | ed5ab2a | 2019-05-04 20:00:00 +0200 | [diff] [blame] | 3409 | // Win32 console passes modifiers |
| 3410 | && ( |
Bram Moolenaar | afde13b | 2019-04-28 19:46:49 +0200 | [diff] [blame] | 3411 | # ifdef VIMDLL |
Bram Moolenaar | ed5ab2a | 2019-05-04 20:00:00 +0200 | [diff] [blame] | 3412 | gui.in_use || |
Bram Moolenaar | afde13b | 2019-04-28 19:46:49 +0200 | [diff] [blame] | 3413 | # endif |
Bram Moolenaar | ed5ab2a | 2019-05-04 20:00:00 +0200 | [diff] [blame] | 3414 | (i < 2 || p[1] != KS_MODIFIER)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3415 | #endif |
| 3416 | )) |
| 3417 | { |
| 3418 | mch_memmove(p + 3, p + 1, (size_t)i); |
| 3419 | p[2] = K_THIRD(p[0]); |
| 3420 | p[1] = K_SECOND(p[0]); |
| 3421 | p[0] = K_SPECIAL; |
| 3422 | p += 2; |
| 3423 | len += 2; |
| 3424 | } |
| 3425 | } |
Bram Moolenaar | ed5ab2a | 2019-05-04 20:00:00 +0200 | [diff] [blame] | 3426 | *p = NUL; // add trailing NUL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3427 | return len; |
| 3428 | } |
| 3429 | |
| 3430 | #if defined(USE_INPUT_BUF) || defined(PROTO) |
| 3431 | /* |
| 3432 | * Return TRUE when bytes are in the input buffer or in the typeahead buffer. |
| 3433 | * Normally the input buffer would be sufficient, but the server_to_input_buf() |
Bram Moolenaar | f9393ef | 2006-04-24 19:47:27 +0000 | [diff] [blame] | 3434 | * or feedkeys() may insert characters in the typeahead buffer while we are |
Bram Moolenaar | 4a85b41 | 2006-04-23 22:40:29 +0000 | [diff] [blame] | 3435 | * waiting for input to arrive. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3436 | */ |
| 3437 | int |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 3438 | input_available(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3439 | { |
| 3440 | return (!vim_is_input_buf_empty() |
Bram Moolenaar | 4a85b41 | 2006-04-23 22:40:29 +0000 | [diff] [blame] | 3441 | # if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL) |
| 3442 | || typebuf_was_filled |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3443 | # endif |
| 3444 | ); |
| 3445 | } |
| 3446 | #endif |