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