Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4: |
| 2 | * |
| 3 | * VIM - Vi IMproved by Bram Moolenaar |
| 4 | * |
| 5 | * Do ":help uganda" in Vim to read copying and usage conditions. |
| 6 | * Do ":help credits" in Vim to see a list of people who contributed. |
| 7 | * See README.txt for an overview of the Vim source code. |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * getchar.c |
| 12 | * |
| 13 | * functions related with getting a character from the user/mapping/redo/... |
| 14 | * |
| 15 | * manipulations with redo buffer and stuff buffer |
| 16 | * mappings and abbreviations |
| 17 | */ |
| 18 | |
| 19 | #include "vim.h" |
| 20 | |
| 21 | /* |
| 22 | * These buffers are used for storing: |
| 23 | * - stuffed characters: A command that is translated into another command. |
| 24 | * - redo characters: will redo the last change. |
Bram Moolenaar | f6f95d9 | 2009-11-11 15:23:37 +0000 | [diff] [blame] | 25 | * - recorded characters: for the "q" command. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 26 | * |
| 27 | * The bytes are stored like in the typeahead buffer: |
| 28 | * - K_SPECIAL introduces a special key (two more bytes follow). A literal |
| 29 | * K_SPECIAL is stored as K_SPECIAL KS_SPECIAL KE_FILLER. |
| 30 | * - CSI introduces a GUI termcap code (also when gui.in_use is FALSE, |
| 31 | * otherwise switching the GUI on would make mappings invalid). |
| 32 | * A literal CSI is stored as CSI KS_EXTRA KE_CSI. |
| 33 | * These translations are also done on multi-byte characters! |
| 34 | * |
| 35 | * Escaping CSI bytes is done by the system-specific input functions, called |
| 36 | * by ui_inchar(). |
| 37 | * Escaping K_SPECIAL is done by inchar(). |
| 38 | * Un-escaping is done by vgetc(). |
| 39 | */ |
| 40 | |
| 41 | #define MINIMAL_SIZE 20 /* minimal size for b_str */ |
| 42 | |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 43 | static buffheader_T redobuff = {{NULL, {NUL}}, NULL, 0, 0}; |
| 44 | static buffheader_T old_redobuff = {{NULL, {NUL}}, NULL, 0, 0}; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 45 | #if defined(FEAT_AUTOCMD) || defined(FEAT_EVAL) || defined(PROTO) |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 46 | static buffheader_T save_redobuff = {{NULL, {NUL}}, NULL, 0, 0}; |
| 47 | static buffheader_T save_old_redobuff = {{NULL, {NUL}}, NULL, 0, 0}; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 48 | #endif |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 49 | static buffheader_T recordbuff = {{NULL, {NUL}}, NULL, 0, 0}; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 50 | |
| 51 | static int typeahead_char = 0; /* typeahead char that's not flushed */ |
| 52 | |
| 53 | /* |
| 54 | * when block_redo is TRUE redo buffer will not be changed |
| 55 | * used by edit() to repeat insertions and 'V' command for redoing |
| 56 | */ |
| 57 | static int block_redo = FALSE; |
| 58 | |
| 59 | /* |
| 60 | * Make a hash value for a mapping. |
| 61 | * "mode" is the lower 4 bits of the State for the mapping. |
| 62 | * "c1" is the first character of the "lhs". |
| 63 | * Returns a value between 0 and 255, index in maphash. |
| 64 | * Put Normal/Visual mode mappings mostly separately from Insert/Cmdline mode. |
| 65 | */ |
Bram Moolenaar | 371d540 | 2006-03-20 21:47:49 +0000 | [diff] [blame] | 66 | #define MAP_HASH(mode, c1) (((mode) & (NORMAL + VISUAL + SELECTMODE + OP_PENDING)) ? (c1) : ((c1) ^ 0x80)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 67 | |
| 68 | /* |
| 69 | * Each mapping is put in one of the 256 hash lists, to speed up finding it. |
| 70 | */ |
| 71 | static mapblock_T *(maphash[256]); |
| 72 | static int maphash_valid = FALSE; |
| 73 | |
| 74 | /* |
| 75 | * List used for abbreviations. |
| 76 | */ |
| 77 | static mapblock_T *first_abbr = NULL; /* first entry in abbrlist */ |
| 78 | |
Bram Moolenaar | cf8e7d1 | 2006-12-05 20:43:17 +0000 | [diff] [blame] | 79 | static int KeyNoremap = 0; /* remapping flags */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 80 | |
| 81 | /* |
| 82 | * variables used by vgetorpeek() and flush_buffers() |
| 83 | * |
| 84 | * typebuf.tb_buf[] contains all characters that are not consumed yet. |
| 85 | * typebuf.tb_buf[typebuf.tb_off] is the first valid character. |
| 86 | * typebuf.tb_buf[typebuf.tb_off + typebuf.tb_len - 1] is the last valid char. |
| 87 | * typebuf.tb_buf[typebuf.tb_off + typebuf.tb_len] must be NUL. |
| 88 | * The head of the buffer may contain the result of mappings, abbreviations |
| 89 | * and @a commands. The length of this part is typebuf.tb_maplen. |
| 90 | * typebuf.tb_silent is the part where <silent> applies. |
| 91 | * After the head are characters that come from the terminal. |
| 92 | * typebuf.tb_no_abbr_cnt is the number of characters in typebuf.tb_buf that |
| 93 | * should not be considered for abbreviations. |
| 94 | * Some parts of typebuf.tb_buf may not be mapped. These parts are remembered |
| 95 | * in typebuf.tb_noremap[], which is the same length as typebuf.tb_buf and |
| 96 | * contains RM_NONE for the characters that are not to be remapped. |
| 97 | * typebuf.tb_noremap[typebuf.tb_off] is the first valid flag. |
| 98 | * (typebuf has been put in globals.h, because check_termcode() needs it). |
| 99 | */ |
| 100 | #define RM_YES 0 /* tb_noremap: remap */ |
| 101 | #define RM_NONE 1 /* tb_noremap: don't remap */ |
| 102 | #define RM_SCRIPT 2 /* tb_noremap: remap local script mappings */ |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 103 | #define RM_ABBR 4 /* tb_noremap: don't remap, do abbrev. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 104 | |
| 105 | /* typebuf.tb_buf has three parts: room in front (for result of mappings), the |
| 106 | * middle for typeahead and room for new characters (which needs to be 3 * |
| 107 | * MAXMAPLEN) for the Amiga). |
| 108 | */ |
| 109 | #define TYPELEN_INIT (5 * (MAXMAPLEN + 3)) |
| 110 | static char_u typebuf_init[TYPELEN_INIT]; /* initial typebuf.tb_buf */ |
| 111 | static char_u noremapbuf_init[TYPELEN_INIT]; /* initial typebuf.tb_noremap */ |
| 112 | |
| 113 | static int last_recorded_len = 0; /* number of last recorded chars */ |
| 114 | |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 115 | static char_u *get_buffcont(buffheader_T *, int); |
| 116 | static void add_buff(buffheader_T *, char_u *, long n); |
| 117 | static void add_num_buff(buffheader_T *, long); |
| 118 | static void add_char_buff(buffheader_T *, int); |
| 119 | static int read_readbuffers(int advance); |
| 120 | static int read_readbuf(buffheader_T *buf, int advance); |
| 121 | static void start_stuff(void); |
| 122 | static int read_redo(int, int); |
| 123 | static void copy_redo(int); |
| 124 | static void init_typebuf(void); |
| 125 | static void gotchars(char_u *, int); |
| 126 | static void may_sync_undo(void); |
| 127 | static void closescript(void); |
| 128 | static int vgetorpeek(int); |
| 129 | static void map_free(mapblock_T **); |
| 130 | static void validate_maphash(void); |
| 131 | static void showmap(mapblock_T *mp, int local); |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 132 | #ifdef FEAT_EVAL |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 133 | static char_u *eval_map_expr(char_u *str, int c); |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 134 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 135 | |
| 136 | /* |
| 137 | * Free and clear a buffer. |
| 138 | */ |
| 139 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 140 | free_buff(buffheader_T *buf) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 141 | { |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 142 | buffblock_T *p, *np; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 143 | |
| 144 | for (p = buf->bh_first.b_next; p != NULL; p = np) |
| 145 | { |
| 146 | np = p->b_next; |
| 147 | vim_free(p); |
| 148 | } |
| 149 | buf->bh_first.b_next = NULL; |
| 150 | } |
| 151 | |
| 152 | /* |
| 153 | * Return the contents of a buffer as a single string. |
| 154 | * K_SPECIAL and CSI in the returned string are escaped. |
| 155 | */ |
| 156 | static char_u * |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 157 | get_buffcont( |
| 158 | buffheader_T *buffer, |
| 159 | int dozero) /* count == zero is not an error */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 160 | { |
| 161 | long_u count = 0; |
| 162 | char_u *p = NULL; |
| 163 | char_u *p2; |
| 164 | char_u *str; |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 165 | buffblock_T *bp; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 166 | |
| 167 | /* compute the total length of the string */ |
| 168 | for (bp = buffer->bh_first.b_next; bp != NULL; bp = bp->b_next) |
| 169 | count += (long_u)STRLEN(bp->b_str); |
| 170 | |
| 171 | if ((count || dozero) && (p = lalloc(count + 1, TRUE)) != NULL) |
| 172 | { |
| 173 | p2 = p; |
| 174 | for (bp = buffer->bh_first.b_next; bp != NULL; bp = bp->b_next) |
| 175 | for (str = bp->b_str; *str; ) |
| 176 | *p2++ = *str++; |
| 177 | *p2 = NUL; |
| 178 | } |
| 179 | return (p); |
| 180 | } |
| 181 | |
| 182 | /* |
| 183 | * Return the contents of the record buffer as a single string |
| 184 | * and clear the record buffer. |
| 185 | * K_SPECIAL and CSI in the returned string are escaped. |
| 186 | */ |
| 187 | char_u * |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 188 | get_recorded(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 189 | { |
| 190 | char_u *p; |
| 191 | size_t len; |
| 192 | |
| 193 | p = get_buffcont(&recordbuff, TRUE); |
| 194 | free_buff(&recordbuff); |
| 195 | |
| 196 | /* |
| 197 | * Remove the characters that were added the last time, these must be the |
| 198 | * (possibly mapped) characters that stopped the recording. |
| 199 | */ |
| 200 | len = STRLEN(p); |
| 201 | if ((int)len >= last_recorded_len) |
| 202 | { |
| 203 | len -= last_recorded_len; |
| 204 | p[len] = NUL; |
| 205 | } |
| 206 | |
| 207 | /* |
| 208 | * When stopping recording from Insert mode with CTRL-O q, also remove the |
| 209 | * CTRL-O. |
| 210 | */ |
| 211 | if (len > 0 && restart_edit != 0 && p[len - 1] == Ctrl_O) |
| 212 | p[len - 1] = NUL; |
| 213 | |
| 214 | return (p); |
| 215 | } |
| 216 | |
| 217 | /* |
| 218 | * Return the contents of the redo buffer as a single string. |
| 219 | * K_SPECIAL and CSI in the returned string are escaped. |
| 220 | */ |
| 221 | char_u * |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 222 | get_inserted(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 223 | { |
Bram Moolenaar | f233048 | 2008-06-24 20:19:36 +0000 | [diff] [blame] | 224 | return get_buffcont(&redobuff, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | /* |
Bram Moolenaar | 4e86cba | 2007-05-06 11:56:32 +0000 | [diff] [blame] | 228 | * Add string "s" after the current block of buffer "buf". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 229 | * K_SPECIAL and CSI should have been escaped already. |
| 230 | */ |
| 231 | static void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 232 | add_buff( |
| 233 | buffheader_T *buf, |
| 234 | char_u *s, |
| 235 | long slen) /* length of "s" or -1 */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 236 | { |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 237 | buffblock_T *p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 238 | long_u len; |
| 239 | |
| 240 | if (slen < 0) |
| 241 | slen = (long)STRLEN(s); |
| 242 | if (slen == 0) /* don't add empty strings */ |
| 243 | return; |
| 244 | |
| 245 | if (buf->bh_first.b_next == NULL) /* first add to list */ |
| 246 | { |
| 247 | buf->bh_space = 0; |
| 248 | buf->bh_curr = &(buf->bh_first); |
| 249 | } |
| 250 | else if (buf->bh_curr == NULL) /* buffer has already been read */ |
| 251 | { |
| 252 | EMSG(_("E222: Add to read buffer")); |
| 253 | return; |
| 254 | } |
| 255 | else if (buf->bh_index != 0) |
Bram Moolenaar | c3b7307 | 2007-12-07 16:30:42 +0000 | [diff] [blame] | 256 | mch_memmove(buf->bh_first.b_next->b_str, |
| 257 | buf->bh_first.b_next->b_str + buf->bh_index, |
| 258 | STRLEN(buf->bh_first.b_next->b_str + buf->bh_index) + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 259 | buf->bh_index = 0; |
| 260 | |
| 261 | if (buf->bh_space >= (int)slen) |
| 262 | { |
| 263 | len = (long_u)STRLEN(buf->bh_curr->b_str); |
Bram Moolenaar | b635633 | 2005-07-18 21:40:44 +0000 | [diff] [blame] | 264 | vim_strncpy(buf->bh_curr->b_str + len, s, (size_t)slen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 265 | buf->bh_space -= slen; |
| 266 | } |
| 267 | else |
| 268 | { |
| 269 | if (slen < MINIMAL_SIZE) |
| 270 | len = MINIMAL_SIZE; |
| 271 | else |
| 272 | len = slen; |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 273 | p = (buffblock_T *)lalloc((long_u)(sizeof(buffblock_T) + len), |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 274 | TRUE); |
| 275 | if (p == NULL) |
| 276 | return; /* no space, just forget it */ |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 277 | buf->bh_space = (int)(len - slen); |
Bram Moolenaar | b635633 | 2005-07-18 21:40:44 +0000 | [diff] [blame] | 278 | vim_strncpy(p->b_str, s, (size_t)slen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 279 | |
| 280 | p->b_next = buf->bh_curr->b_next; |
| 281 | buf->bh_curr->b_next = p; |
| 282 | buf->bh_curr = p; |
| 283 | } |
| 284 | return; |
| 285 | } |
| 286 | |
| 287 | /* |
| 288 | * Add number "n" to buffer "buf". |
| 289 | */ |
| 290 | static void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 291 | add_num_buff(buffheader_T *buf, long n) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 292 | { |
| 293 | char_u number[32]; |
| 294 | |
| 295 | sprintf((char *)number, "%ld", n); |
| 296 | add_buff(buf, number, -1L); |
| 297 | } |
| 298 | |
| 299 | /* |
| 300 | * Add character 'c' to buffer "buf". |
| 301 | * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters. |
| 302 | */ |
| 303 | static void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 304 | add_char_buff(buffheader_T *buf, int c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 305 | { |
| 306 | #ifdef FEAT_MBYTE |
| 307 | char_u bytes[MB_MAXBYTES + 1]; |
| 308 | int len; |
| 309 | int i; |
| 310 | #endif |
| 311 | char_u temp[4]; |
| 312 | |
| 313 | #ifdef FEAT_MBYTE |
| 314 | if (IS_SPECIAL(c)) |
| 315 | len = 1; |
| 316 | else |
| 317 | len = (*mb_char2bytes)(c, bytes); |
| 318 | for (i = 0; i < len; ++i) |
| 319 | { |
| 320 | if (!IS_SPECIAL(c)) |
| 321 | c = bytes[i]; |
| 322 | #endif |
| 323 | |
| 324 | if (IS_SPECIAL(c) || c == K_SPECIAL || c == NUL) |
| 325 | { |
| 326 | /* translate special key code into three byte sequence */ |
| 327 | temp[0] = K_SPECIAL; |
| 328 | temp[1] = K_SECOND(c); |
| 329 | temp[2] = K_THIRD(c); |
| 330 | temp[3] = NUL; |
| 331 | } |
| 332 | #ifdef FEAT_GUI |
| 333 | else if (c == CSI) |
| 334 | { |
| 335 | /* Translate a CSI to a CSI - KS_EXTRA - KE_CSI sequence */ |
| 336 | temp[0] = CSI; |
| 337 | temp[1] = KS_EXTRA; |
| 338 | temp[2] = (int)KE_CSI; |
| 339 | temp[3] = NUL; |
| 340 | } |
| 341 | #endif |
| 342 | else |
| 343 | { |
| 344 | temp[0] = c; |
| 345 | temp[1] = NUL; |
| 346 | } |
| 347 | add_buff(buf, temp, -1L); |
| 348 | #ifdef FEAT_MBYTE |
| 349 | } |
| 350 | #endif |
| 351 | } |
| 352 | |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 353 | /* First read ahead buffer. Used for translated commands. */ |
| 354 | static buffheader_T readbuf1 = {{NULL, {NUL}}, NULL, 0, 0}; |
| 355 | |
| 356 | /* Second read ahead buffer. Used for redo. */ |
| 357 | static buffheader_T readbuf2 = {{NULL, {NUL}}, NULL, 0, 0}; |
| 358 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 359 | /* |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 360 | * Get one byte from the read buffers. Use readbuf1 one first, use readbuf2 |
| 361 | * if that one is empty. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 362 | * If advance == TRUE go to the next char. |
| 363 | * No translation is done K_SPECIAL and CSI are escaped. |
| 364 | */ |
| 365 | static int |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 366 | read_readbuffers(int advance) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 367 | { |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 368 | int c; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 369 | |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 370 | c = read_readbuf(&readbuf1, advance); |
| 371 | if (c == NUL) |
| 372 | c = read_readbuf(&readbuf2, advance); |
| 373 | return c; |
| 374 | } |
| 375 | |
| 376 | static int |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 377 | read_readbuf(buffheader_T *buf, int advance) |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 378 | { |
| 379 | char_u c; |
| 380 | buffblock_T *curr; |
| 381 | |
| 382 | if (buf->bh_first.b_next == NULL) /* buffer is empty */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 383 | return NUL; |
| 384 | |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 385 | curr = buf->bh_first.b_next; |
| 386 | c = curr->b_str[buf->bh_index]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 387 | |
| 388 | if (advance) |
| 389 | { |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 390 | if (curr->b_str[++buf->bh_index] == NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 391 | { |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 392 | buf->bh_first.b_next = curr->b_next; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 393 | vim_free(curr); |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 394 | buf->bh_index = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 395 | } |
| 396 | } |
| 397 | return c; |
| 398 | } |
| 399 | |
| 400 | /* |
Bram Moolenaar | 06811f3 | 2014-02-15 16:17:07 +0100 | [diff] [blame] | 401 | * Prepare the read buffers for reading (if they contain something). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 402 | */ |
| 403 | static void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 404 | start_stuff(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 405 | { |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 406 | if (readbuf1.bh_first.b_next != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 407 | { |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 408 | readbuf1.bh_curr = &(readbuf1.bh_first); |
| 409 | readbuf1.bh_space = 0; |
| 410 | } |
| 411 | if (readbuf2.bh_first.b_next != NULL) |
| 412 | { |
| 413 | readbuf2.bh_curr = &(readbuf2.bh_first); |
| 414 | readbuf2.bh_space = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 415 | } |
| 416 | } |
| 417 | |
| 418 | /* |
| 419 | * Return TRUE if the stuff buffer is empty. |
| 420 | */ |
| 421 | int |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 422 | stuff_empty(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 423 | { |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 424 | return (readbuf1.bh_first.b_next == NULL |
| 425 | && readbuf2.bh_first.b_next == NULL); |
| 426 | } |
| 427 | |
| 428 | /* |
| 429 | * Return TRUE if readbuf1 is empty. There may still be redo characters in |
| 430 | * redbuf2. |
| 431 | */ |
| 432 | int |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 433 | readbuf1_empty(void) |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 434 | { |
| 435 | return (readbuf1.bh_first.b_next == NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | /* |
| 439 | * Set a typeahead character that won't be flushed. |
| 440 | */ |
| 441 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 442 | typeahead_noflush(int c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 443 | { |
| 444 | typeahead_char = c; |
| 445 | } |
| 446 | |
| 447 | /* |
| 448 | * 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] | 449 | * 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] | 450 | * flush all typeahead characters (used when interrupted by a CTRL-C). |
| 451 | */ |
| 452 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 453 | flush_buffers(int flush_typeahead) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 454 | { |
| 455 | init_typebuf(); |
| 456 | |
| 457 | start_stuff(); |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 458 | while (read_readbuffers(TRUE) != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 459 | ; |
| 460 | |
Bram Moolenaar | 70b2a56 | 2012-01-10 22:26:17 +0100 | [diff] [blame] | 461 | if (flush_typeahead) /* remove all typeahead */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 462 | { |
| 463 | /* |
| 464 | * We have to get all characters, because we may delete the first part |
| 465 | * of an escape sequence. |
| 466 | * In an xterm we get one char at a time and we have to get them all. |
| 467 | */ |
| 468 | while (inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 10L, |
| 469 | typebuf.tb_change_cnt) != 0) |
| 470 | ; |
| 471 | typebuf.tb_off = MAXMAPLEN; |
| 472 | typebuf.tb_len = 0; |
| 473 | } |
Bram Moolenaar | 687a29c | 2013-04-15 15:47:12 +0200 | [diff] [blame] | 474 | else /* remove mapped characters at the start only */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 475 | { |
| 476 | typebuf.tb_off += typebuf.tb_maplen; |
| 477 | typebuf.tb_len -= typebuf.tb_maplen; |
| 478 | } |
| 479 | typebuf.tb_maplen = 0; |
| 480 | typebuf.tb_silent = 0; |
| 481 | cmd_silent = FALSE; |
| 482 | typebuf.tb_no_abbr_cnt = 0; |
| 483 | } |
| 484 | |
| 485 | /* |
| 486 | * The previous contents of the redo buffer is kept in old_redobuffer. |
| 487 | * This is used for the CTRL-O <.> command in insert mode. |
| 488 | */ |
| 489 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 490 | ResetRedobuff(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 491 | { |
| 492 | if (!block_redo) |
| 493 | { |
| 494 | free_buff(&old_redobuff); |
| 495 | old_redobuff = redobuff; |
| 496 | redobuff.bh_first.b_next = NULL; |
| 497 | } |
| 498 | } |
| 499 | |
Bram Moolenaar | be094a1 | 2012-02-05 01:18:48 +0100 | [diff] [blame] | 500 | /* |
| 501 | * Discard the contents of the redo buffer and restore the previous redo |
| 502 | * buffer. |
| 503 | */ |
| 504 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 505 | CancelRedo(void) |
Bram Moolenaar | be094a1 | 2012-02-05 01:18:48 +0100 | [diff] [blame] | 506 | { |
| 507 | if (!block_redo) |
| 508 | { |
| 509 | free_buff(&redobuff); |
| 510 | redobuff = old_redobuff; |
| 511 | old_redobuff.bh_first.b_next = NULL; |
| 512 | start_stuff(); |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 513 | while (read_readbuffers(TRUE) != NUL) |
Bram Moolenaar | be094a1 | 2012-02-05 01:18:48 +0100 | [diff] [blame] | 514 | ; |
| 515 | } |
| 516 | } |
| 517 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 518 | #if defined(FEAT_AUTOCMD) || defined(FEAT_EVAL) || defined(PROTO) |
| 519 | /* |
| 520 | * Save redobuff and old_redobuff to save_redobuff and save_old_redobuff. |
| 521 | * Used before executing autocommands and user functions. |
| 522 | */ |
| 523 | static int save_level = 0; |
| 524 | |
| 525 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 526 | saveRedobuff(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 527 | { |
| 528 | char_u *s; |
| 529 | |
| 530 | if (save_level++ == 0) |
| 531 | { |
| 532 | save_redobuff = redobuff; |
| 533 | redobuff.bh_first.b_next = NULL; |
| 534 | save_old_redobuff = old_redobuff; |
| 535 | old_redobuff.bh_first.b_next = NULL; |
| 536 | |
| 537 | /* Make a copy, so that ":normal ." in a function works. */ |
| 538 | s = get_buffcont(&save_redobuff, FALSE); |
| 539 | if (s != NULL) |
| 540 | { |
| 541 | add_buff(&redobuff, s, -1L); |
| 542 | vim_free(s); |
| 543 | } |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | /* |
| 548 | * Restore redobuff and old_redobuff from save_redobuff and save_old_redobuff. |
| 549 | * Used after executing autocommands and user functions. |
| 550 | */ |
| 551 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 552 | restoreRedobuff(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 553 | { |
| 554 | if (--save_level == 0) |
| 555 | { |
| 556 | free_buff(&redobuff); |
| 557 | redobuff = save_redobuff; |
| 558 | free_buff(&old_redobuff); |
| 559 | old_redobuff = save_old_redobuff; |
| 560 | } |
| 561 | } |
| 562 | #endif |
| 563 | |
| 564 | /* |
| 565 | * Append "s" to the redo buffer. |
| 566 | * K_SPECIAL and CSI should already have been escaped. |
| 567 | */ |
| 568 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 569 | AppendToRedobuff(char_u *s) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 570 | { |
| 571 | if (!block_redo) |
| 572 | add_buff(&redobuff, s, -1L); |
| 573 | } |
| 574 | |
| 575 | /* |
| 576 | * Append to Redo buffer literally, escaping special characters with CTRL-V. |
| 577 | * K_SPECIAL and CSI are escaped as well. |
| 578 | */ |
| 579 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 580 | AppendToRedobuffLit( |
| 581 | char_u *str, |
| 582 | int len) /* length of "str" or -1 for up to the NUL */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 583 | { |
Bram Moolenaar | ebefac6 | 2005-12-28 22:39:57 +0000 | [diff] [blame] | 584 | char_u *s = str; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 585 | int c; |
| 586 | char_u *start; |
| 587 | |
| 588 | if (block_redo) |
| 589 | return; |
| 590 | |
Bram Moolenaar | ebefac6 | 2005-12-28 22:39:57 +0000 | [diff] [blame] | 591 | while (len < 0 ? *s != NUL : s - str < len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 592 | { |
| 593 | /* Put a string of normal characters in the redo buffer (that's |
| 594 | * faster). */ |
| 595 | start = s; |
| 596 | while (*s >= ' ' |
| 597 | #ifndef EBCDIC |
| 598 | && *s < DEL /* EBCDIC: all chars above space are normal */ |
| 599 | #endif |
Bram Moolenaar | ebefac6 | 2005-12-28 22:39:57 +0000 | [diff] [blame] | 600 | && (len < 0 || s - str < len)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 601 | ++s; |
| 602 | |
| 603 | /* Don't put '0' or '^' as last character, just in case a CTRL-D is |
| 604 | * typed next. */ |
| 605 | if (*s == NUL && (s[-1] == '0' || s[-1] == '^')) |
| 606 | --s; |
| 607 | if (s > start) |
| 608 | add_buff(&redobuff, start, (long)(s - start)); |
| 609 | |
Bram Moolenaar | ebefac6 | 2005-12-28 22:39:57 +0000 | [diff] [blame] | 610 | if (*s == NUL || (len >= 0 && s - str >= len)) |
| 611 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 612 | |
Bram Moolenaar | ebefac6 | 2005-12-28 22:39:57 +0000 | [diff] [blame] | 613 | /* Handle a special or multibyte character. */ |
| 614 | #ifdef FEAT_MBYTE |
| 615 | if (has_mbyte) |
| 616 | /* Handle composing chars separately. */ |
| 617 | c = mb_cptr2char_adv(&s); |
| 618 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 619 | #endif |
Bram Moolenaar | ebefac6 | 2005-12-28 22:39:57 +0000 | [diff] [blame] | 620 | c = *s++; |
| 621 | if (c < ' ' || c == DEL || (*s == NUL && (c == '0' || c == '^'))) |
| 622 | add_char_buff(&redobuff, Ctrl_V); |
| 623 | |
| 624 | /* CTRL-V '0' must be inserted as CTRL-V 048 (EBCDIC: xf0) */ |
| 625 | if (*s == NUL && c == '0') |
| 626 | #ifdef EBCDIC |
| 627 | add_buff(&redobuff, (char_u *)"xf0", 3L); |
| 628 | #else |
| 629 | add_buff(&redobuff, (char_u *)"048", 3L); |
| 630 | #endif |
| 631 | else |
| 632 | add_char_buff(&redobuff, c); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 633 | } |
| 634 | } |
| 635 | |
| 636 | /* |
| 637 | * Append a character to the redo buffer. |
| 638 | * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters. |
| 639 | */ |
| 640 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 641 | AppendCharToRedobuff(int c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 642 | { |
| 643 | if (!block_redo) |
| 644 | add_char_buff(&redobuff, c); |
| 645 | } |
| 646 | |
| 647 | /* |
| 648 | * Append a number to the redo buffer. |
| 649 | */ |
| 650 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 651 | AppendNumberToRedobuff(long n) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 652 | { |
| 653 | if (!block_redo) |
| 654 | add_num_buff(&redobuff, n); |
| 655 | } |
| 656 | |
| 657 | /* |
| 658 | * Append string "s" to the stuff buffer. |
| 659 | * CSI and K_SPECIAL must already have been escaped. |
| 660 | */ |
| 661 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 662 | stuffReadbuff(char_u *s) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 663 | { |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 664 | add_buff(&readbuf1, s, -1L); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 665 | } |
| 666 | |
Bram Moolenaar | 4f5ce33 | 2014-07-30 16:00:58 +0200 | [diff] [blame] | 667 | /* |
| 668 | * Append string "s" to the redo stuff buffer. |
| 669 | * CSI and K_SPECIAL must already have been escaped. |
| 670 | */ |
| 671 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 672 | stuffRedoReadbuff(char_u *s) |
Bram Moolenaar | 4f5ce33 | 2014-07-30 16:00:58 +0200 | [diff] [blame] | 673 | { |
| 674 | add_buff(&readbuf2, s, -1L); |
| 675 | } |
| 676 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 677 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 678 | stuffReadbuffLen(char_u *s, long len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 679 | { |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 680 | add_buff(&readbuf1, s, len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 684 | /* |
| 685 | * Stuff "s" into the stuff buffer, leaving special key codes unmodified and |
| 686 | * escaping other K_SPECIAL and CSI bytes. |
Bram Moolenaar | 877b97b | 2011-04-28 17:30:09 +0200 | [diff] [blame] | 687 | * Change CR, LF and ESC into a space. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 688 | */ |
| 689 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 690 | stuffReadbuffSpec(char_u *s) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 691 | { |
Bram Moolenaar | 877b97b | 2011-04-28 17:30:09 +0200 | [diff] [blame] | 692 | int c; |
| 693 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 694 | while (*s != NUL) |
| 695 | { |
| 696 | if (*s == K_SPECIAL && s[1] != NUL && s[2] != NUL) |
| 697 | { |
| 698 | /* Insert special key literally. */ |
| 699 | stuffReadbuffLen(s, 3L); |
| 700 | s += 3; |
| 701 | } |
| 702 | else |
Bram Moolenaar | 877b97b | 2011-04-28 17:30:09 +0200 | [diff] [blame] | 703 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 704 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 877b97b | 2011-04-28 17:30:09 +0200 | [diff] [blame] | 705 | c = mb_ptr2char_adv(&s); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 706 | #else |
Bram Moolenaar | 877b97b | 2011-04-28 17:30:09 +0200 | [diff] [blame] | 707 | c = *s++; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 708 | #endif |
Bram Moolenaar | 877b97b | 2011-04-28 17:30:09 +0200 | [diff] [blame] | 709 | if (c == CAR || c == NL || c == ESC) |
| 710 | c = ' '; |
| 711 | stuffcharReadbuff(c); |
| 712 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 713 | } |
| 714 | } |
| 715 | #endif |
| 716 | |
| 717 | /* |
| 718 | * Append a character to the stuff buffer. |
| 719 | * Translates special keys, NUL, CSI, K_SPECIAL and multibyte characters. |
| 720 | */ |
| 721 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 722 | stuffcharReadbuff(int c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 723 | { |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 724 | add_char_buff(&readbuf1, c); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | /* |
| 728 | * Append a number to the stuff buffer. |
| 729 | */ |
| 730 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 731 | stuffnumReadbuff(long n) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 732 | { |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 733 | add_num_buff(&readbuf1, n); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | /* |
| 737 | * Read a character from the redo buffer. Translates K_SPECIAL, CSI and |
| 738 | * multibyte characters. |
| 739 | * The redo buffer is left as it is. |
Bram Moolenaar | be094a1 | 2012-02-05 01:18:48 +0100 | [diff] [blame] | 740 | * If init is TRUE, prepare for redo, return FAIL if nothing to redo, OK |
| 741 | * otherwise. |
| 742 | * If old is TRUE, use old_redobuff instead of redobuff. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 743 | */ |
| 744 | static int |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 745 | read_redo(int init, int old_redo) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 746 | { |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 747 | static buffblock_T *bp; |
| 748 | static char_u *p; |
| 749 | int c; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 750 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 751 | int n; |
| 752 | char_u buf[MB_MAXBYTES + 1]; |
| 753 | int i; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 754 | #endif |
| 755 | |
| 756 | if (init) |
| 757 | { |
| 758 | if (old_redo) |
| 759 | bp = old_redobuff.bh_first.b_next; |
| 760 | else |
| 761 | bp = redobuff.bh_first.b_next; |
| 762 | if (bp == NULL) |
| 763 | return FAIL; |
| 764 | p = bp->b_str; |
| 765 | return OK; |
| 766 | } |
| 767 | if ((c = *p) != NUL) |
| 768 | { |
| 769 | /* Reverse the conversion done by add_char_buff() */ |
| 770 | #ifdef FEAT_MBYTE |
| 771 | /* For a multi-byte character get all the bytes and return the |
| 772 | * converted character. */ |
| 773 | if (has_mbyte && (c != K_SPECIAL || p[1] == KS_SPECIAL)) |
| 774 | n = MB_BYTE2LEN_CHECK(c); |
| 775 | else |
| 776 | n = 1; |
| 777 | for (i = 0; ; ++i) |
| 778 | #endif |
| 779 | { |
| 780 | if (c == K_SPECIAL) /* special key or escaped K_SPECIAL */ |
| 781 | { |
| 782 | c = TO_SPECIAL(p[1], p[2]); |
| 783 | p += 2; |
| 784 | } |
| 785 | #ifdef FEAT_GUI |
| 786 | if (c == CSI) /* escaped CSI */ |
| 787 | p += 2; |
| 788 | #endif |
| 789 | if (*++p == NUL && bp->b_next != NULL) |
| 790 | { |
| 791 | bp = bp->b_next; |
| 792 | p = bp->b_str; |
| 793 | } |
| 794 | #ifdef FEAT_MBYTE |
| 795 | buf[i] = c; |
| 796 | if (i == n - 1) /* last byte of a character */ |
| 797 | { |
| 798 | if (n != 1) |
| 799 | c = (*mb_ptr2char)(buf); |
| 800 | break; |
| 801 | } |
| 802 | c = *p; |
| 803 | if (c == NUL) /* cannot happen? */ |
| 804 | break; |
| 805 | #endif |
| 806 | } |
| 807 | } |
| 808 | |
| 809 | return c; |
| 810 | } |
| 811 | |
| 812 | /* |
| 813 | * Copy the rest of the redo buffer into the stuff buffer (in a slow way). |
| 814 | * If old_redo is TRUE, use old_redobuff instead of redobuff. |
| 815 | * The escaped K_SPECIAL and CSI are copied without translation. |
| 816 | */ |
| 817 | static void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 818 | copy_redo(int old_redo) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 819 | { |
| 820 | int c; |
| 821 | |
| 822 | while ((c = read_redo(FALSE, old_redo)) != NUL) |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 823 | add_char_buff(&readbuf2, c); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | /* |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 827 | * Stuff the redo buffer into readbuf2. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 828 | * Insert the redo count into the command. |
| 829 | * If "old_redo" is TRUE, the last but one command is repeated |
| 830 | * instead of the last command (inserting text). This is used for |
| 831 | * CTRL-O <.> in insert mode |
| 832 | * |
| 833 | * return FAIL for failure, OK otherwise |
| 834 | */ |
| 835 | int |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 836 | start_redo(long count, int old_redo) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 837 | { |
| 838 | int c; |
| 839 | |
| 840 | /* init the pointers; return if nothing to redo */ |
| 841 | if (read_redo(TRUE, old_redo) == FAIL) |
| 842 | return FAIL; |
| 843 | |
| 844 | c = read_redo(FALSE, old_redo); |
| 845 | |
| 846 | /* copy the buffer name, if present */ |
| 847 | if (c == '"') |
| 848 | { |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 849 | add_buff(&readbuf2, (char_u *)"\"", 1L); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 850 | c = read_redo(FALSE, old_redo); |
| 851 | |
| 852 | /* if a numbered buffer is used, increment the number */ |
| 853 | if (c >= '1' && c < '9') |
| 854 | ++c; |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 855 | add_char_buff(&readbuf2, c); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 856 | c = read_redo(FALSE, old_redo); |
| 857 | } |
| 858 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 859 | if (c == 'v') /* redo Visual */ |
| 860 | { |
| 861 | VIsual = curwin->w_cursor; |
| 862 | VIsual_active = TRUE; |
| 863 | VIsual_select = FALSE; |
| 864 | VIsual_reselect = TRUE; |
| 865 | redo_VIsual_busy = TRUE; |
| 866 | c = read_redo(FALSE, old_redo); |
| 867 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 868 | |
| 869 | /* try to enter the count (in place of a previous count) */ |
| 870 | if (count) |
| 871 | { |
| 872 | while (VIM_ISDIGIT(c)) /* skip "old" count */ |
| 873 | c = read_redo(FALSE, old_redo); |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 874 | add_num_buff(&readbuf2, count); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | /* copy from the redo buffer into the stuff buffer */ |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 878 | add_char_buff(&readbuf2, c); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 879 | copy_redo(old_redo); |
| 880 | return OK; |
| 881 | } |
| 882 | |
| 883 | /* |
| 884 | * 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] | 885 | * the redo buffer into readbuf2. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 886 | * return FAIL for failure, OK otherwise |
| 887 | */ |
| 888 | int |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 889 | start_redo_ins(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 890 | { |
| 891 | int c; |
| 892 | |
| 893 | if (read_redo(TRUE, FALSE) == FAIL) |
| 894 | return FAIL; |
| 895 | start_stuff(); |
| 896 | |
| 897 | /* skip the count and the command character */ |
| 898 | while ((c = read_redo(FALSE, FALSE)) != NUL) |
| 899 | { |
| 900 | if (vim_strchr((char_u *)"AaIiRrOo", c) != NULL) |
| 901 | { |
| 902 | if (c == 'O' || c == 'o') |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 903 | add_buff(&readbuf2, NL_STR, -1L); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 904 | break; |
| 905 | } |
| 906 | } |
| 907 | |
| 908 | /* copy the typed text from the redo buffer into the stuff buffer */ |
| 909 | copy_redo(FALSE); |
| 910 | block_redo = TRUE; |
| 911 | return OK; |
| 912 | } |
| 913 | |
| 914 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 915 | stop_redo_ins(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 916 | { |
| 917 | block_redo = FALSE; |
| 918 | } |
| 919 | |
| 920 | /* |
| 921 | * Initialize typebuf.tb_buf to point to typebuf_init. |
| 922 | * alloc() cannot be used here: In out-of-memory situations it would |
| 923 | * be impossible to type anything. |
| 924 | */ |
| 925 | static void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 926 | init_typebuf(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 927 | { |
| 928 | if (typebuf.tb_buf == NULL) |
| 929 | { |
| 930 | typebuf.tb_buf = typebuf_init; |
| 931 | typebuf.tb_noremap = noremapbuf_init; |
| 932 | typebuf.tb_buflen = TYPELEN_INIT; |
| 933 | typebuf.tb_len = 0; |
| 934 | typebuf.tb_off = 0; |
| 935 | typebuf.tb_change_cnt = 1; |
| 936 | } |
| 937 | } |
| 938 | |
| 939 | /* |
Bram Moolenaar | e7fedb6 | 2015-12-31 19:07:19 +0100 | [diff] [blame] | 940 | * Insert a string in position 'offset' in the typeahead buffer (for "@r" |
| 941 | * and ":normal" command, vgetorpeek() and check_termcode()). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 942 | * |
| 943 | * If noremap is REMAP_YES, new string can be mapped again. |
| 944 | * If noremap is REMAP_NONE, new string cannot be mapped again. |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 945 | * If noremap is REMAP_SKIP, fist char of new string cannot be mapped again, |
| 946 | * but abbreviations are allowed. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 947 | * If noremap is REMAP_SCRIPT, new string cannot be mapped again, except for |
| 948 | * script-local mappings. |
| 949 | * If noremap is > 0, that many characters of the new string cannot be mapped. |
| 950 | * |
| 951 | * If nottyped is TRUE, the string does not return KeyTyped (don't use when |
| 952 | * offset is non-zero!). |
| 953 | * |
| 954 | * If silent is TRUE, cmd_silent is set when the characters are obtained. |
| 955 | * |
| 956 | * return FAIL for failure, OK otherwise |
| 957 | */ |
| 958 | int |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 959 | ins_typebuf( |
| 960 | char_u *str, |
| 961 | int noremap, |
| 962 | int offset, |
| 963 | int nottyped, |
| 964 | int silent) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 965 | { |
| 966 | char_u *s1, *s2; |
| 967 | int newlen; |
| 968 | int addlen; |
| 969 | int i; |
| 970 | int newoff; |
| 971 | int val; |
| 972 | int nrm; |
| 973 | |
| 974 | init_typebuf(); |
| 975 | if (++typebuf.tb_change_cnt == 0) |
| 976 | typebuf.tb_change_cnt = 1; |
| 977 | |
| 978 | addlen = (int)STRLEN(str); |
Bram Moolenaar | 4e86cba | 2007-05-06 11:56:32 +0000 | [diff] [blame] | 979 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 980 | /* |
| 981 | * Easy case: there is room in front of typebuf.tb_buf[typebuf.tb_off] |
| 982 | */ |
| 983 | if (offset == 0 && addlen <= typebuf.tb_off) |
| 984 | { |
| 985 | typebuf.tb_off -= addlen; |
| 986 | mch_memmove(typebuf.tb_buf + typebuf.tb_off, str, (size_t)addlen); |
| 987 | } |
Bram Moolenaar | 4e86cba | 2007-05-06 11:56:32 +0000 | [diff] [blame] | 988 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 989 | /* |
Bram Moolenaar | 4e86cba | 2007-05-06 11:56:32 +0000 | [diff] [blame] | 990 | * Need to allocate a new buffer. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 991 | * In typebuf.tb_buf there must always be room for 3 * MAXMAPLEN + 4 |
| 992 | * characters. We add some extra room to avoid having to allocate too |
| 993 | * often. |
| 994 | */ |
| 995 | else |
| 996 | { |
| 997 | newoff = MAXMAPLEN + 4; |
| 998 | newlen = typebuf.tb_len + addlen + newoff + 4 * (MAXMAPLEN + 4); |
| 999 | if (newlen < 0) /* string is getting too long */ |
| 1000 | { |
| 1001 | EMSG(_(e_toocompl)); /* also calls flush_buffers */ |
| 1002 | setcursor(); |
| 1003 | return FAIL; |
| 1004 | } |
| 1005 | s1 = alloc(newlen); |
| 1006 | if (s1 == NULL) /* out of memory */ |
| 1007 | return FAIL; |
| 1008 | s2 = alloc(newlen); |
| 1009 | if (s2 == NULL) /* out of memory */ |
| 1010 | { |
| 1011 | vim_free(s1); |
| 1012 | return FAIL; |
| 1013 | } |
| 1014 | typebuf.tb_buflen = newlen; |
| 1015 | |
| 1016 | /* copy the old chars, before the insertion point */ |
| 1017 | mch_memmove(s1 + newoff, typebuf.tb_buf + typebuf.tb_off, |
| 1018 | (size_t)offset); |
| 1019 | /* copy the new chars */ |
| 1020 | mch_memmove(s1 + newoff + offset, str, (size_t)addlen); |
| 1021 | /* copy the old chars, after the insertion point, including the NUL at |
| 1022 | * the end */ |
| 1023 | mch_memmove(s1 + newoff + offset + addlen, |
| 1024 | typebuf.tb_buf + typebuf.tb_off + offset, |
| 1025 | (size_t)(typebuf.tb_len - offset + 1)); |
| 1026 | if (typebuf.tb_buf != typebuf_init) |
| 1027 | vim_free(typebuf.tb_buf); |
| 1028 | typebuf.tb_buf = s1; |
| 1029 | |
| 1030 | mch_memmove(s2 + newoff, typebuf.tb_noremap + typebuf.tb_off, |
| 1031 | (size_t)offset); |
| 1032 | mch_memmove(s2 + newoff + offset + addlen, |
| 1033 | typebuf.tb_noremap + typebuf.tb_off + offset, |
| 1034 | (size_t)(typebuf.tb_len - offset)); |
| 1035 | if (typebuf.tb_noremap != noremapbuf_init) |
| 1036 | vim_free(typebuf.tb_noremap); |
| 1037 | typebuf.tb_noremap = s2; |
| 1038 | |
| 1039 | typebuf.tb_off = newoff; |
| 1040 | } |
| 1041 | typebuf.tb_len += addlen; |
| 1042 | |
| 1043 | /* If noremap == REMAP_SCRIPT: do remap script-local mappings. */ |
| 1044 | if (noremap == REMAP_SCRIPT) |
| 1045 | val = RM_SCRIPT; |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 1046 | else if (noremap == REMAP_SKIP) |
| 1047 | val = RM_ABBR; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1048 | else |
| 1049 | val = RM_NONE; |
| 1050 | |
| 1051 | /* |
| 1052 | * Adjust typebuf.tb_noremap[] for the new characters: |
| 1053 | * If noremap == REMAP_NONE or REMAP_SCRIPT: new characters are |
| 1054 | * (sometimes) not remappable |
| 1055 | * If noremap == REMAP_YES: all the new characters are mappable |
| 1056 | * If noremap > 0: "noremap" characters are not remappable, the rest |
| 1057 | * mappable |
| 1058 | */ |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 1059 | if (noremap == REMAP_SKIP) |
| 1060 | nrm = 1; |
| 1061 | else if (noremap < 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1062 | nrm = addlen; |
| 1063 | else |
| 1064 | nrm = noremap; |
| 1065 | for (i = 0; i < addlen; ++i) |
| 1066 | typebuf.tb_noremap[typebuf.tb_off + i + offset] = |
| 1067 | (--nrm >= 0) ? val : RM_YES; |
| 1068 | |
| 1069 | /* tb_maplen and tb_silent only remember the length of mapped and/or |
| 1070 | * silent mappings at the start of the buffer, assuming that a mapped |
| 1071 | * sequence doesn't result in typed characters. */ |
| 1072 | if (nottyped || typebuf.tb_maplen > offset) |
| 1073 | typebuf.tb_maplen += addlen; |
| 1074 | if (silent || typebuf.tb_silent > offset) |
| 1075 | { |
| 1076 | typebuf.tb_silent += addlen; |
| 1077 | cmd_silent = TRUE; |
| 1078 | } |
| 1079 | if (typebuf.tb_no_abbr_cnt && offset == 0) /* and not used for abbrev.s */ |
| 1080 | typebuf.tb_no_abbr_cnt += addlen; |
| 1081 | |
| 1082 | return OK; |
| 1083 | } |
| 1084 | |
| 1085 | /* |
Bram Moolenaar | d8fc5c0 | 2006-04-29 21:55:22 +0000 | [diff] [blame] | 1086 | * Put character "c" back into the typeahead buffer. |
| 1087 | * 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] | 1088 | * Uses cmd_silent, KeyTyped and KeyNoremap to restore the flags belonging to |
| 1089 | * the char. |
Bram Moolenaar | d8fc5c0 | 2006-04-29 21:55:22 +0000 | [diff] [blame] | 1090 | */ |
| 1091 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 1092 | ins_char_typebuf(int c) |
Bram Moolenaar | d8fc5c0 | 2006-04-29 21:55:22 +0000 | [diff] [blame] | 1093 | { |
| 1094 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 9a920d8 | 2012-06-01 15:21:02 +0200 | [diff] [blame] | 1095 | char_u buf[MB_MAXBYTES + 1]; |
Bram Moolenaar | d8fc5c0 | 2006-04-29 21:55:22 +0000 | [diff] [blame] | 1096 | #else |
| 1097 | char_u buf[4]; |
| 1098 | #endif |
| 1099 | if (IS_SPECIAL(c)) |
| 1100 | { |
| 1101 | buf[0] = K_SPECIAL; |
| 1102 | buf[1] = K_SECOND(c); |
| 1103 | buf[2] = K_THIRD(c); |
| 1104 | buf[3] = NUL; |
| 1105 | } |
| 1106 | else |
| 1107 | { |
| 1108 | #ifdef FEAT_MBYTE |
| 1109 | buf[(*mb_char2bytes)(c, buf)] = NUL; |
| 1110 | #else |
| 1111 | buf[0] = c; |
| 1112 | buf[1] = NUL; |
| 1113 | #endif |
| 1114 | } |
Bram Moolenaar | cf8e7d1 | 2006-12-05 20:43:17 +0000 | [diff] [blame] | 1115 | (void)ins_typebuf(buf, KeyNoremap, 0, !KeyTyped, cmd_silent); |
Bram Moolenaar | d8fc5c0 | 2006-04-29 21:55:22 +0000 | [diff] [blame] | 1116 | } |
| 1117 | |
| 1118 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1119 | * Return TRUE if the typeahead buffer was changed (while waiting for a |
Bram Moolenaar | 4a85b41 | 2006-04-23 22:40:29 +0000 | [diff] [blame] | 1120 | * 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] | 1121 | * from feedkeys(). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1122 | * But check in a more generic way to avoid trouble: When "typebuf.tb_buf" |
| 1123 | * changed it was reallocated and the old pointer can no longer be used. |
| 1124 | * Or "typebuf.tb_off" may have been changed and we would overwrite characters |
| 1125 | * that was just added. |
| 1126 | */ |
| 1127 | int |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 1128 | typebuf_changed( |
| 1129 | int tb_change_cnt) /* old value of typebuf.tb_change_cnt */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1130 | { |
| 1131 | return (tb_change_cnt != 0 && (typebuf.tb_change_cnt != tb_change_cnt |
Bram Moolenaar | 4a85b41 | 2006-04-23 22:40:29 +0000 | [diff] [blame] | 1132 | #if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL) |
| 1133 | || typebuf_was_filled |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1134 | #endif |
| 1135 | )); |
| 1136 | } |
| 1137 | |
| 1138 | /* |
| 1139 | * Return TRUE if there are no characters in the typeahead buffer that have |
| 1140 | * not been typed (result from a mapping or come from ":normal"). |
| 1141 | */ |
| 1142 | int |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 1143 | typebuf_typed(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1144 | { |
| 1145 | return typebuf.tb_maplen == 0; |
| 1146 | } |
| 1147 | |
| 1148 | /* |
| 1149 | * Return the number of characters that are mapped (or not typed). |
| 1150 | */ |
| 1151 | int |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 1152 | typebuf_maplen(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1153 | { |
| 1154 | return typebuf.tb_maplen; |
| 1155 | } |
| 1156 | |
| 1157 | /* |
| 1158 | * remove "len" characters from typebuf.tb_buf[typebuf.tb_off + offset] |
| 1159 | */ |
| 1160 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 1161 | del_typebuf(int len, int offset) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1162 | { |
| 1163 | int i; |
| 1164 | |
| 1165 | if (len == 0) |
| 1166 | return; /* nothing to do */ |
| 1167 | |
| 1168 | typebuf.tb_len -= len; |
| 1169 | |
| 1170 | /* |
| 1171 | * Easy case: Just increase typebuf.tb_off. |
| 1172 | */ |
| 1173 | if (offset == 0 && typebuf.tb_buflen - (typebuf.tb_off + len) |
| 1174 | >= 3 * MAXMAPLEN + 3) |
| 1175 | typebuf.tb_off += len; |
| 1176 | /* |
| 1177 | * Have to move the characters in typebuf.tb_buf[] and typebuf.tb_noremap[] |
| 1178 | */ |
| 1179 | else |
| 1180 | { |
| 1181 | i = typebuf.tb_off + offset; |
| 1182 | /* |
| 1183 | * Leave some extra room at the end to avoid reallocation. |
| 1184 | */ |
| 1185 | if (typebuf.tb_off > MAXMAPLEN) |
| 1186 | { |
| 1187 | mch_memmove(typebuf.tb_buf + MAXMAPLEN, |
| 1188 | typebuf.tb_buf + typebuf.tb_off, (size_t)offset); |
| 1189 | mch_memmove(typebuf.tb_noremap + MAXMAPLEN, |
| 1190 | typebuf.tb_noremap + typebuf.tb_off, (size_t)offset); |
| 1191 | typebuf.tb_off = MAXMAPLEN; |
| 1192 | } |
| 1193 | /* adjust typebuf.tb_buf (include the NUL at the end) */ |
| 1194 | mch_memmove(typebuf.tb_buf + typebuf.tb_off + offset, |
| 1195 | typebuf.tb_buf + i + len, |
| 1196 | (size_t)(typebuf.tb_len - offset + 1)); |
| 1197 | /* adjust typebuf.tb_noremap[] */ |
| 1198 | mch_memmove(typebuf.tb_noremap + typebuf.tb_off + offset, |
| 1199 | typebuf.tb_noremap + i + len, |
| 1200 | (size_t)(typebuf.tb_len - offset)); |
| 1201 | } |
| 1202 | |
| 1203 | if (typebuf.tb_maplen > offset) /* adjust tb_maplen */ |
| 1204 | { |
| 1205 | if (typebuf.tb_maplen < offset + len) |
| 1206 | typebuf.tb_maplen = offset; |
| 1207 | else |
| 1208 | typebuf.tb_maplen -= len; |
| 1209 | } |
| 1210 | if (typebuf.tb_silent > offset) /* adjust tb_silent */ |
| 1211 | { |
| 1212 | if (typebuf.tb_silent < offset + len) |
| 1213 | typebuf.tb_silent = offset; |
| 1214 | else |
| 1215 | typebuf.tb_silent -= len; |
| 1216 | } |
| 1217 | if (typebuf.tb_no_abbr_cnt > offset) /* adjust tb_no_abbr_cnt */ |
| 1218 | { |
| 1219 | if (typebuf.tb_no_abbr_cnt < offset + len) |
| 1220 | typebuf.tb_no_abbr_cnt = offset; |
| 1221 | else |
| 1222 | typebuf.tb_no_abbr_cnt -= len; |
| 1223 | } |
| 1224 | |
Bram Moolenaar | 4a85b41 | 2006-04-23 22:40:29 +0000 | [diff] [blame] | 1225 | #if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL) |
Bram Moolenaar | f9393ef | 2006-04-24 19:47:27 +0000 | [diff] [blame] | 1226 | /* Reset the flag that text received from a client or from feedkeys() |
Bram Moolenaar | 4a85b41 | 2006-04-23 22:40:29 +0000 | [diff] [blame] | 1227 | * was inserted in the typeahead buffer. */ |
| 1228 | typebuf_was_filled = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1229 | #endif |
| 1230 | if (++typebuf.tb_change_cnt == 0) |
| 1231 | typebuf.tb_change_cnt = 1; |
| 1232 | } |
| 1233 | |
| 1234 | /* |
| 1235 | * Write typed characters to script file. |
| 1236 | * If recording is on put the character in the recordbuffer. |
| 1237 | */ |
| 1238 | static void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 1239 | gotchars(char_u *chars, int len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1240 | { |
Bram Moolenaar | 4e86cba | 2007-05-06 11:56:32 +0000 | [diff] [blame] | 1241 | char_u *s = chars; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1242 | int c; |
| 1243 | char_u buf[2]; |
Bram Moolenaar | 4e86cba | 2007-05-06 11:56:32 +0000 | [diff] [blame] | 1244 | int todo = len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1245 | |
| 1246 | /* remember how many chars were last recorded */ |
| 1247 | if (Recording) |
| 1248 | last_recorded_len += len; |
| 1249 | |
| 1250 | buf[1] = NUL; |
Bram Moolenaar | 4e86cba | 2007-05-06 11:56:32 +0000 | [diff] [blame] | 1251 | while (todo--) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1252 | { |
| 1253 | /* Handle one byte at a time; no translation to be done. */ |
| 1254 | c = *s++; |
| 1255 | updatescript(c); |
| 1256 | |
| 1257 | if (Recording) |
| 1258 | { |
| 1259 | buf[0] = c; |
| 1260 | add_buff(&recordbuff, buf, 1L); |
| 1261 | } |
| 1262 | } |
| 1263 | may_sync_undo(); |
| 1264 | |
| 1265 | #ifdef FEAT_EVAL |
| 1266 | /* output "debug mode" message next time in debug mode */ |
| 1267 | debug_did_msg = FALSE; |
| 1268 | #endif |
| 1269 | |
| 1270 | /* Since characters have been typed, consider the following to be in |
| 1271 | * another mapping. Search string will be kept in history. */ |
| 1272 | ++maptick; |
| 1273 | } |
| 1274 | |
| 1275 | /* |
| 1276 | * Sync undo. Called when typed characters are obtained from the typeahead |
| 1277 | * buffer, or when a menu is used. |
| 1278 | * Do not sync: |
| 1279 | * - In Insert mode, unless cursor key has been used. |
| 1280 | * - While reading a script file. |
| 1281 | * - When no_u_sync is non-zero. |
| 1282 | */ |
| 1283 | static void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 1284 | may_sync_undo(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1285 | { |
| 1286 | if ((!(State & (INSERT + CMDLINE)) || arrow_used) |
Bram Moolenaar | 779b74b | 2006-04-10 14:55:34 +0000 | [diff] [blame] | 1287 | && scriptin[curscript] == NULL) |
| 1288 | u_sync(FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1289 | } |
| 1290 | |
| 1291 | /* |
| 1292 | * Make "typebuf" empty and allocate new buffers. |
| 1293 | * Returns FAIL when out of memory. |
| 1294 | */ |
| 1295 | int |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 1296 | alloc_typebuf(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1297 | { |
| 1298 | typebuf.tb_buf = alloc(TYPELEN_INIT); |
| 1299 | typebuf.tb_noremap = alloc(TYPELEN_INIT); |
| 1300 | if (typebuf.tb_buf == NULL || typebuf.tb_noremap == NULL) |
| 1301 | { |
| 1302 | free_typebuf(); |
| 1303 | return FAIL; |
| 1304 | } |
| 1305 | typebuf.tb_buflen = TYPELEN_INIT; |
| 1306 | typebuf.tb_off = 0; |
| 1307 | typebuf.tb_len = 0; |
| 1308 | typebuf.tb_maplen = 0; |
| 1309 | typebuf.tb_silent = 0; |
| 1310 | typebuf.tb_no_abbr_cnt = 0; |
| 1311 | if (++typebuf.tb_change_cnt == 0) |
| 1312 | typebuf.tb_change_cnt = 1; |
| 1313 | return OK; |
| 1314 | } |
| 1315 | |
| 1316 | /* |
| 1317 | * Free the buffers of "typebuf". |
| 1318 | */ |
| 1319 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 1320 | free_typebuf(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1321 | { |
Bram Moolenaar | ee3f7a5 | 2008-01-01 13:17:56 +0000 | [diff] [blame] | 1322 | if (typebuf.tb_buf == typebuf_init) |
| 1323 | EMSG2(_(e_intern2), "Free typebuf 1"); |
| 1324 | else |
| 1325 | vim_free(typebuf.tb_buf); |
Bram Moolenaar | f6f95d9 | 2009-11-11 15:23:37 +0000 | [diff] [blame] | 1326 | if (typebuf.tb_noremap == noremapbuf_init) |
Bram Moolenaar | ee3f7a5 | 2008-01-01 13:17:56 +0000 | [diff] [blame] | 1327 | EMSG2(_(e_intern2), "Free typebuf 2"); |
| 1328 | else |
| 1329 | vim_free(typebuf.tb_noremap); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1330 | } |
| 1331 | |
| 1332 | /* |
| 1333 | * When doing ":so! file", the current typeahead needs to be saved, and |
| 1334 | * restored when "file" has been read completely. |
| 1335 | */ |
| 1336 | static typebuf_T saved_typebuf[NSCRIPT]; |
| 1337 | |
| 1338 | int |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 1339 | save_typebuf(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1340 | { |
| 1341 | init_typebuf(); |
| 1342 | saved_typebuf[curscript] = typebuf; |
| 1343 | /* If out of memory: restore typebuf and close file. */ |
| 1344 | if (alloc_typebuf() == FAIL) |
| 1345 | { |
| 1346 | closescript(); |
| 1347 | return FAIL; |
| 1348 | } |
| 1349 | return OK; |
| 1350 | } |
| 1351 | |
Bram Moolenaar | 13df0fe | 2009-07-09 16:24:19 +0000 | [diff] [blame] | 1352 | static int old_char = -1; /* character put back by vungetc() */ |
| 1353 | static int old_mod_mask; /* mod_mask for ungotten character */ |
Bram Moolenaar | b897871 | 2013-03-16 21:42:16 +0100 | [diff] [blame] | 1354 | #ifdef FEAT_MOUSE |
| 1355 | static int old_mouse_row; /* mouse_row related to old_char */ |
| 1356 | static int old_mouse_col; /* mouse_col related to old_char */ |
| 1357 | #endif |
Bram Moolenaar | 13df0fe | 2009-07-09 16:24:19 +0000 | [diff] [blame] | 1358 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1359 | #if defined(FEAT_EVAL) || defined(FEAT_EX_EXTRA) || defined(PROTO) |
| 1360 | |
| 1361 | /* |
| 1362 | * Save all three kinds of typeahead, so that the user must type at a prompt. |
| 1363 | */ |
| 1364 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 1365 | save_typeahead(tasave_T *tp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1366 | { |
| 1367 | tp->save_typebuf = typebuf; |
| 1368 | tp->typebuf_valid = (alloc_typebuf() == OK); |
| 1369 | if (!tp->typebuf_valid) |
| 1370 | typebuf = tp->save_typebuf; |
| 1371 | |
Bram Moolenaar | 13df0fe | 2009-07-09 16:24:19 +0000 | [diff] [blame] | 1372 | tp->old_char = old_char; |
| 1373 | tp->old_mod_mask = old_mod_mask; |
| 1374 | old_char = -1; |
| 1375 | |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 1376 | tp->save_readbuf1 = readbuf1; |
| 1377 | readbuf1.bh_first.b_next = NULL; |
| 1378 | tp->save_readbuf2 = readbuf2; |
| 1379 | readbuf2.bh_first.b_next = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1380 | # ifdef USE_INPUT_BUF |
| 1381 | tp->save_inputbuf = get_input_buf(); |
| 1382 | # endif |
| 1383 | } |
| 1384 | |
| 1385 | /* |
| 1386 | * Restore the typeahead to what it was before calling save_typeahead(). |
| 1387 | * The allocated memory is freed, can only be called once! |
| 1388 | */ |
| 1389 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 1390 | restore_typeahead(tasave_T *tp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1391 | { |
| 1392 | if (tp->typebuf_valid) |
| 1393 | { |
| 1394 | free_typebuf(); |
| 1395 | typebuf = tp->save_typebuf; |
| 1396 | } |
| 1397 | |
Bram Moolenaar | 13df0fe | 2009-07-09 16:24:19 +0000 | [diff] [blame] | 1398 | old_char = tp->old_char; |
| 1399 | old_mod_mask = tp->old_mod_mask; |
| 1400 | |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 1401 | free_buff(&readbuf1); |
| 1402 | readbuf1 = tp->save_readbuf1; |
| 1403 | free_buff(&readbuf2); |
| 1404 | readbuf2 = tp->save_readbuf2; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1405 | # ifdef USE_INPUT_BUF |
| 1406 | set_input_buf(tp->save_inputbuf); |
| 1407 | # endif |
| 1408 | } |
| 1409 | #endif |
| 1410 | |
| 1411 | /* |
| 1412 | * Open a new script file for the ":source!" command. |
| 1413 | */ |
| 1414 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 1415 | openscript( |
| 1416 | char_u *name, |
| 1417 | int directly) /* when TRUE execute directly */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1418 | { |
| 1419 | if (curscript + 1 == NSCRIPT) |
| 1420 | { |
| 1421 | EMSG(_(e_nesting)); |
| 1422 | return; |
| 1423 | } |
Bram Moolenaar | ee3f7a5 | 2008-01-01 13:17:56 +0000 | [diff] [blame] | 1424 | #ifdef FEAT_EVAL |
| 1425 | if (ignore_script) |
| 1426 | /* Not reading from script, also don't open one. Warning message? */ |
| 1427 | return; |
| 1428 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1429 | |
| 1430 | if (scriptin[curscript] != NULL) /* already reading script */ |
| 1431 | ++curscript; |
| 1432 | /* use NameBuff for expanded name */ |
| 1433 | expand_env(name, NameBuff, MAXPATHL); |
| 1434 | if ((scriptin[curscript] = mch_fopen((char *)NameBuff, READBIN)) == NULL) |
| 1435 | { |
| 1436 | EMSG2(_(e_notopen), name); |
| 1437 | if (curscript) |
| 1438 | --curscript; |
| 1439 | return; |
| 1440 | } |
| 1441 | if (save_typebuf() == FAIL) |
| 1442 | return; |
| 1443 | |
| 1444 | /* |
| 1445 | * Execute the commands from the file right now when using ":source!" |
| 1446 | * after ":global" or ":argdo" or in a loop. Also when another command |
| 1447 | * follows. This means the display won't be updated. Don't do this |
| 1448 | * always, "make test" would fail. |
| 1449 | */ |
| 1450 | if (directly) |
| 1451 | { |
| 1452 | oparg_T oa; |
| 1453 | int oldcurscript; |
| 1454 | int save_State = State; |
| 1455 | int save_restart_edit = restart_edit; |
| 1456 | int save_insertmode = p_im; |
| 1457 | int save_finish_op = finish_op; |
| 1458 | int save_msg_scroll = msg_scroll; |
| 1459 | |
| 1460 | State = NORMAL; |
| 1461 | msg_scroll = FALSE; /* no msg scrolling in Normal mode */ |
| 1462 | restart_edit = 0; /* don't go to Insert mode */ |
| 1463 | p_im = FALSE; /* don't use 'insertmode' */ |
| 1464 | clear_oparg(&oa); |
| 1465 | finish_op = FALSE; |
| 1466 | |
| 1467 | oldcurscript = curscript; |
| 1468 | do |
| 1469 | { |
| 1470 | update_topline_cursor(); /* update cursor position and topline */ |
| 1471 | normal_cmd(&oa, FALSE); /* execute one command */ |
| 1472 | vpeekc(); /* check for end of file */ |
| 1473 | } |
| 1474 | while (scriptin[oldcurscript] != NULL); |
| 1475 | |
| 1476 | State = save_State; |
| 1477 | msg_scroll = save_msg_scroll; |
| 1478 | restart_edit = save_restart_edit; |
| 1479 | p_im = save_insertmode; |
| 1480 | finish_op = save_finish_op; |
| 1481 | } |
| 1482 | } |
| 1483 | |
| 1484 | /* |
| 1485 | * Close the currently active input script. |
| 1486 | */ |
| 1487 | static void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 1488 | closescript(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1489 | { |
| 1490 | free_typebuf(); |
| 1491 | typebuf = saved_typebuf[curscript]; |
| 1492 | |
| 1493 | fclose(scriptin[curscript]); |
| 1494 | scriptin[curscript] = NULL; |
| 1495 | if (curscript > 0) |
| 1496 | --curscript; |
| 1497 | } |
| 1498 | |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 1499 | #if defined(EXITFREE) || defined(PROTO) |
| 1500 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 1501 | close_all_scripts(void) |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 1502 | { |
| 1503 | while (scriptin[0] != NULL) |
| 1504 | closescript(); |
| 1505 | } |
| 1506 | #endif |
| 1507 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1508 | #if defined(FEAT_INS_EXPAND) || defined(PROTO) |
| 1509 | /* |
| 1510 | * Return TRUE when reading keys from a script file. |
| 1511 | */ |
| 1512 | int |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 1513 | using_script(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1514 | { |
| 1515 | return scriptin[curscript] != NULL; |
| 1516 | } |
| 1517 | #endif |
| 1518 | |
| 1519 | /* |
Bram Moolenaar | 238f4fa | 2005-06-27 22:25:50 +0000 | [diff] [blame] | 1520 | * This function is called just before doing a blocking wait. Thus after |
| 1521 | * waiting 'updatetime' for a character to arrive. |
| 1522 | */ |
| 1523 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 1524 | before_blocking(void) |
Bram Moolenaar | 238f4fa | 2005-06-27 22:25:50 +0000 | [diff] [blame] | 1525 | { |
| 1526 | updatescript(0); |
| 1527 | #ifdef FEAT_EVAL |
Bram Moolenaar | 9fecb46 | 2006-09-05 10:59:47 +0000 | [diff] [blame] | 1528 | if (may_garbage_collect) |
| 1529 | garbage_collect(); |
Bram Moolenaar | 238f4fa | 2005-06-27 22:25:50 +0000 | [diff] [blame] | 1530 | #endif |
| 1531 | } |
| 1532 | |
| 1533 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1534 | * updatescipt() is called when a character can be written into the script file |
| 1535 | * or when we have waited some time for a character (c == 0) |
| 1536 | * |
| 1537 | * All the changed memfiles are synced if c == 0 or when the number of typed |
| 1538 | * characters reaches 'updatecount' and 'updatecount' is non-zero. |
| 1539 | */ |
| 1540 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 1541 | updatescript(int c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1542 | { |
| 1543 | static int count = 0; |
| 1544 | |
| 1545 | if (c && scriptout) |
| 1546 | putc(c, scriptout); |
| 1547 | if (c == 0 || (p_uc > 0 && ++count >= p_uc)) |
| 1548 | { |
| 1549 | ml_sync_all(c == 0, TRUE); |
| 1550 | count = 0; |
| 1551 | } |
| 1552 | } |
| 1553 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1554 | /* |
| 1555 | * Get the next input character. |
| 1556 | * Can return a special key or a multi-byte character. |
| 1557 | * Can return NUL when called recursively, use safe_vgetc() if that's not |
| 1558 | * wanted. |
| 1559 | * This translates escaped K_SPECIAL and CSI bytes to a K_SPECIAL or CSI byte. |
| 1560 | * Collects the bytes of a multibyte character into the whole character. |
Bram Moolenaar | f6f95d9 | 2009-11-11 15:23:37 +0000 | [diff] [blame] | 1561 | * Returns the modifiers in the global "mod_mask". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1562 | */ |
| 1563 | int |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 1564 | vgetc(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1565 | { |
| 1566 | int c, c2; |
| 1567 | #ifdef FEAT_MBYTE |
| 1568 | int n; |
Bram Moolenaar | 9a920d8 | 2012-06-01 15:21:02 +0200 | [diff] [blame] | 1569 | char_u buf[MB_MAXBYTES + 1]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1570 | int i; |
| 1571 | #endif |
| 1572 | |
Bram Moolenaar | 9fecb46 | 2006-09-05 10:59:47 +0000 | [diff] [blame] | 1573 | #ifdef FEAT_EVAL |
| 1574 | /* Do garbage collection when garbagecollect() was called previously and |
| 1575 | * we are now at the toplevel. */ |
| 1576 | if (may_garbage_collect && want_garbage_collect) |
| 1577 | garbage_collect(); |
| 1578 | #endif |
| 1579 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1580 | /* |
| 1581 | * If a character was put back with vungetc, it was already processed. |
| 1582 | * Return it directly. |
| 1583 | */ |
| 1584 | if (old_char != -1) |
| 1585 | { |
| 1586 | c = old_char; |
| 1587 | old_char = -1; |
| 1588 | mod_mask = old_mod_mask; |
Bram Moolenaar | b897871 | 2013-03-16 21:42:16 +0100 | [diff] [blame] | 1589 | #ifdef FEAT_MOUSE |
| 1590 | mouse_row = old_mouse_row; |
| 1591 | mouse_col = old_mouse_col; |
| 1592 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1593 | } |
Bram Moolenaar | 9fecb46 | 2006-09-05 10:59:47 +0000 | [diff] [blame] | 1594 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1595 | { |
Bram Moolenaar | 9fecb46 | 2006-09-05 10:59:47 +0000 | [diff] [blame] | 1596 | mod_mask = 0x0; |
| 1597 | last_recorded_len = 0; |
| 1598 | for (;;) /* this is done twice if there are modifiers */ |
| 1599 | { |
Bram Moolenaar | 2455c4e | 2015-09-15 18:29:39 +0200 | [diff] [blame] | 1600 | int did_inc = FALSE; |
| 1601 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1602 | if (mod_mask) /* no mapping after modifier has been read */ |
| 1603 | { |
| 1604 | ++no_mapping; |
| 1605 | ++allow_keys; |
Bram Moolenaar | 2455c4e | 2015-09-15 18:29:39 +0200 | [diff] [blame] | 1606 | did_inc = TRUE; /* mod_mask may change value */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1607 | } |
| 1608 | c = vgetorpeek(TRUE); |
Bram Moolenaar | 2455c4e | 2015-09-15 18:29:39 +0200 | [diff] [blame] | 1609 | if (did_inc) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1610 | { |
| 1611 | --no_mapping; |
| 1612 | --allow_keys; |
| 1613 | } |
| 1614 | |
| 1615 | /* Get two extra bytes for special keys */ |
| 1616 | if (c == K_SPECIAL |
| 1617 | #ifdef FEAT_GUI |
| 1618 | || c == CSI |
| 1619 | #endif |
| 1620 | ) |
| 1621 | { |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 1622 | int save_allow_keys = allow_keys; |
| 1623 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1624 | ++no_mapping; |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 1625 | allow_keys = 0; /* make sure BS is not found */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1626 | c2 = vgetorpeek(TRUE); /* no mapping for these chars */ |
| 1627 | c = vgetorpeek(TRUE); |
| 1628 | --no_mapping; |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 1629 | allow_keys = save_allow_keys; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1630 | if (c2 == KS_MODIFIER) |
| 1631 | { |
| 1632 | mod_mask = c; |
| 1633 | continue; |
| 1634 | } |
| 1635 | c = TO_SPECIAL(c2, c); |
| 1636 | |
| 1637 | #if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF) |
| 1638 | /* Handle K_TEAROFF here, the caller of vgetc() doesn't need to |
| 1639 | * know that a menu was torn off */ |
| 1640 | if (c == K_TEAROFF) |
| 1641 | { |
| 1642 | char_u name[200]; |
| 1643 | int i; |
| 1644 | |
| 1645 | /* get menu path, it ends with a <CR> */ |
| 1646 | for (i = 0; (c = vgetorpeek(TRUE)) != '\r'; ) |
| 1647 | { |
| 1648 | name[i] = c; |
| 1649 | if (i < 199) |
| 1650 | ++i; |
| 1651 | } |
| 1652 | name[i] = NUL; |
| 1653 | gui_make_tearoff(name); |
| 1654 | continue; |
| 1655 | } |
| 1656 | #endif |
Bram Moolenaar | 0eda7ac | 2010-06-26 05:38:18 +0200 | [diff] [blame] | 1657 | #if defined(FEAT_GUI) && defined(FEAT_GUI_GTK) && defined(FEAT_MENU) |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 1658 | /* GTK: <F10> normally selects the menu, but it's passed until |
| 1659 | * here to allow mapping it. Intercept and invoke the GTK |
| 1660 | * behavior if it's not mapped. */ |
| 1661 | if (c == K_F10 && gui.menubar != NULL) |
| 1662 | { |
| 1663 | gtk_menu_shell_select_first(GTK_MENU_SHELL(gui.menubar), FALSE); |
| 1664 | continue; |
| 1665 | } |
| 1666 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1667 | #ifdef FEAT_GUI |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 1668 | /* Handle focus event here, so that the caller doesn't need to |
| 1669 | * know about it. Return K_IGNORE so that we loop once (needed if |
| 1670 | * 'lazyredraw' is set). */ |
Bram Moolenaar | 9c8791f | 2007-09-05 19:47:23 +0000 | [diff] [blame] | 1671 | if (c == K_FOCUSGAINED || c == K_FOCUSLOST) |
| 1672 | { |
| 1673 | ui_focus_change(c == K_FOCUSGAINED); |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 1674 | c = K_IGNORE; |
Bram Moolenaar | 9c8791f | 2007-09-05 19:47:23 +0000 | [diff] [blame] | 1675 | } |
| 1676 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1677 | /* Translate K_CSI to CSI. The special key is only used to avoid |
| 1678 | * it being recognized as the start of a special key. */ |
| 1679 | if (c == K_CSI) |
| 1680 | c = CSI; |
| 1681 | #endif |
| 1682 | } |
| 1683 | #ifdef MSDOS |
| 1684 | /* |
| 1685 | * If K_NUL was typed, it is replaced by K_NUL, 3 in mch_inchar(). |
| 1686 | * Delete the 3 here. |
| 1687 | */ |
| 1688 | else if (c == K_NUL && vpeekc() == 3) |
| 1689 | (void)vgetorpeek(TRUE); |
| 1690 | #endif |
| 1691 | |
Bram Moolenaar | a88d968 | 2005-03-25 21:45:43 +0000 | [diff] [blame] | 1692 | /* a keypad or special function key was not mapped, use it like |
| 1693 | * its ASCII equivalent */ |
| 1694 | switch (c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1695 | { |
Bram Moolenaar | a88d968 | 2005-03-25 21:45:43 +0000 | [diff] [blame] | 1696 | case K_KPLUS: c = '+'; break; |
| 1697 | case K_KMINUS: c = '-'; break; |
| 1698 | case K_KDIVIDE: c = '/'; break; |
| 1699 | case K_KMULTIPLY: c = '*'; break; |
| 1700 | case K_KENTER: c = CAR; break; |
| 1701 | case K_KPOINT: |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 1702 | #ifdef WIN32 |
Bram Moolenaar | a88d968 | 2005-03-25 21:45:43 +0000 | [diff] [blame] | 1703 | /* Can be either '.' or a ',', * |
| 1704 | * depending on the type of keypad. */ |
| 1705 | c = MapVirtualKey(VK_DECIMAL, 2); break; |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 1706 | #else |
Bram Moolenaar | a88d968 | 2005-03-25 21:45:43 +0000 | [diff] [blame] | 1707 | c = '.'; break; |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 1708 | #endif |
Bram Moolenaar | a88d968 | 2005-03-25 21:45:43 +0000 | [diff] [blame] | 1709 | case K_K0: c = '0'; break; |
| 1710 | case K_K1: c = '1'; break; |
| 1711 | case K_K2: c = '2'; break; |
| 1712 | case K_K3: c = '3'; break; |
| 1713 | case K_K4: c = '4'; break; |
| 1714 | case K_K5: c = '5'; break; |
| 1715 | case K_K6: c = '6'; break; |
| 1716 | case K_K7: c = '7'; break; |
| 1717 | case K_K8: c = '8'; break; |
| 1718 | case K_K9: c = '9'; break; |
| 1719 | |
| 1720 | case K_XHOME: |
| 1721 | case K_ZHOME: if (mod_mask == MOD_MASK_SHIFT) |
| 1722 | { |
| 1723 | c = K_S_HOME; |
| 1724 | mod_mask = 0; |
| 1725 | } |
| 1726 | else if (mod_mask == MOD_MASK_CTRL) |
| 1727 | { |
| 1728 | c = K_C_HOME; |
| 1729 | mod_mask = 0; |
| 1730 | } |
| 1731 | else |
| 1732 | c = K_HOME; |
| 1733 | break; |
| 1734 | case K_XEND: |
| 1735 | case K_ZEND: if (mod_mask == MOD_MASK_SHIFT) |
| 1736 | { |
| 1737 | c = K_S_END; |
| 1738 | mod_mask = 0; |
| 1739 | } |
| 1740 | else if (mod_mask == MOD_MASK_CTRL) |
| 1741 | { |
| 1742 | c = K_C_END; |
| 1743 | mod_mask = 0; |
| 1744 | } |
| 1745 | else |
| 1746 | c = K_END; |
| 1747 | break; |
| 1748 | |
| 1749 | case K_XUP: c = K_UP; break; |
| 1750 | case K_XDOWN: c = K_DOWN; break; |
| 1751 | case K_XLEFT: c = K_LEFT; break; |
| 1752 | case K_XRIGHT: c = K_RIGHT; break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1753 | } |
| 1754 | |
| 1755 | #ifdef FEAT_MBYTE |
| 1756 | /* For a multi-byte character get all the bytes and return the |
| 1757 | * converted character. |
| 1758 | * Note: This will loop until enough bytes are received! |
| 1759 | */ |
| 1760 | if (has_mbyte && (n = MB_BYTE2LEN_CHECK(c)) > 1) |
| 1761 | { |
| 1762 | ++no_mapping; |
| 1763 | buf[0] = c; |
| 1764 | for (i = 1; i < n; ++i) |
| 1765 | { |
| 1766 | buf[i] = vgetorpeek(TRUE); |
| 1767 | if (buf[i] == K_SPECIAL |
| 1768 | #ifdef FEAT_GUI |
| 1769 | || buf[i] == CSI |
| 1770 | #endif |
| 1771 | ) |
| 1772 | { |
| 1773 | /* Must be a K_SPECIAL - KS_SPECIAL - KE_FILLER sequence, |
| 1774 | * which represents a K_SPECIAL (0x80), |
| 1775 | * or a CSI - KS_EXTRA - KE_CSI sequence, which represents |
| 1776 | * a CSI (0x9B), |
| 1777 | * of a K_SPECIAL - KS_EXTRA - KE_CSI, which is CSI too. */ |
| 1778 | c = vgetorpeek(TRUE); |
| 1779 | if (vgetorpeek(TRUE) == (int)KE_CSI && c == KS_EXTRA) |
| 1780 | buf[i] = CSI; |
| 1781 | } |
| 1782 | } |
| 1783 | --no_mapping; |
| 1784 | c = (*mb_ptr2char)(buf); |
| 1785 | } |
| 1786 | #endif |
| 1787 | |
Bram Moolenaar | 9fecb46 | 2006-09-05 10:59:47 +0000 | [diff] [blame] | 1788 | break; |
| 1789 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1790 | } |
Bram Moolenaar | 9fecb46 | 2006-09-05 10:59:47 +0000 | [diff] [blame] | 1791 | |
| 1792 | #ifdef FEAT_EVAL |
| 1793 | /* |
| 1794 | * In the main loop "may_garbage_collect" can be set to do garbage |
| 1795 | * collection in the first next vgetc(). It's disabled after that to |
| 1796 | * avoid internally used Lists and Dicts to be freed. |
| 1797 | */ |
| 1798 | may_garbage_collect = FALSE; |
| 1799 | #endif |
| 1800 | |
| 1801 | return c; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1802 | } |
| 1803 | |
| 1804 | /* |
| 1805 | * Like vgetc(), but never return a NUL when called recursively, get a key |
| 1806 | * directly from the user (ignoring typeahead). |
| 1807 | */ |
| 1808 | int |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 1809 | safe_vgetc(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1810 | { |
| 1811 | int c; |
| 1812 | |
| 1813 | c = vgetc(); |
| 1814 | if (c == NUL) |
| 1815 | c = get_keystroke(); |
| 1816 | return c; |
| 1817 | } |
| 1818 | |
| 1819 | /* |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 1820 | * Like safe_vgetc(), but loop to handle K_IGNORE. |
| 1821 | * Also ignore scrollbar events. |
| 1822 | */ |
| 1823 | int |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 1824 | plain_vgetc(void) |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 1825 | { |
| 1826 | int c; |
| 1827 | |
| 1828 | do |
| 1829 | { |
| 1830 | c = safe_vgetc(); |
| 1831 | } while (c == K_IGNORE || c == K_VER_SCROLLBAR || c == K_HOR_SCROLLBAR); |
| 1832 | return c; |
| 1833 | } |
| 1834 | |
| 1835 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1836 | * Check if a character is available, such that vgetc() will not block. |
| 1837 | * If the next character is a special character or multi-byte, the returned |
| 1838 | * character is not valid!. |
| 1839 | */ |
| 1840 | int |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 1841 | vpeekc(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1842 | { |
| 1843 | if (old_char != -1) |
| 1844 | return old_char; |
| 1845 | return vgetorpeek(FALSE); |
| 1846 | } |
| 1847 | |
| 1848 | #if defined(FEAT_TERMRESPONSE) || defined(PROTO) |
| 1849 | /* |
| 1850 | * Like vpeekc(), but don't allow mapping. Do allow checking for terminal |
| 1851 | * codes. |
| 1852 | */ |
| 1853 | int |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 1854 | vpeekc_nomap(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1855 | { |
| 1856 | int c; |
| 1857 | |
| 1858 | ++no_mapping; |
| 1859 | ++allow_keys; |
| 1860 | c = vpeekc(); |
| 1861 | --no_mapping; |
| 1862 | --allow_keys; |
| 1863 | return c; |
| 1864 | } |
| 1865 | #endif |
| 1866 | |
Bram Moolenaar | 9a665ba | 2014-05-22 18:59:58 +0200 | [diff] [blame] | 1867 | #if defined(FEAT_INS_EXPAND) || defined(FEAT_EVAL) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1868 | /* |
| 1869 | * Check if any character is available, also half an escape sequence. |
| 1870 | * Trick: when no typeahead found, but there is something in the typeahead |
| 1871 | * buffer, it must be an ESC that is recognized as the start of a key code. |
| 1872 | */ |
| 1873 | int |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 1874 | vpeekc_any(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1875 | { |
| 1876 | int c; |
| 1877 | |
| 1878 | c = vpeekc(); |
| 1879 | if (c == NUL && typebuf.tb_len > 0) |
| 1880 | c = ESC; |
| 1881 | return c; |
| 1882 | } |
| 1883 | #endif |
| 1884 | |
| 1885 | /* |
| 1886 | * Call vpeekc() without causing anything to be mapped. |
| 1887 | * Return TRUE if a character is available, FALSE otherwise. |
| 1888 | */ |
| 1889 | int |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 1890 | char_avail(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1891 | { |
| 1892 | int retval; |
| 1893 | |
| 1894 | ++no_mapping; |
| 1895 | retval = vpeekc(); |
| 1896 | --no_mapping; |
| 1897 | return (retval != NUL); |
| 1898 | } |
| 1899 | |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 1900 | /* |
| 1901 | * unget one character (can only be done once!) |
| 1902 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1903 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 1904 | vungetc(int c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1905 | { |
| 1906 | old_char = c; |
| 1907 | old_mod_mask = mod_mask; |
Bram Moolenaar | b897871 | 2013-03-16 21:42:16 +0100 | [diff] [blame] | 1908 | #ifdef FEAT_MOUSE |
| 1909 | old_mouse_row = mouse_row; |
| 1910 | old_mouse_col = mouse_col; |
| 1911 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1912 | } |
| 1913 | |
| 1914 | /* |
| 1915 | * get a character: |
| 1916 | * 1. from the stuffbuffer |
| 1917 | * This is used for abbreviated commands like "D" -> "d$". |
| 1918 | * Also used to redo a command for ".". |
| 1919 | * 2. from the typeahead buffer |
| 1920 | * Stores text obtained previously but not used yet. |
| 1921 | * Also stores the result of mappings. |
| 1922 | * Also used for the ":normal" command. |
| 1923 | * 3. from the user |
| 1924 | * This may do a blocking wait if "advance" is TRUE. |
| 1925 | * |
| 1926 | * if "advance" is TRUE (vgetc()): |
| 1927 | * really get the character. |
| 1928 | * KeyTyped is set to TRUE in the case the user typed the key. |
| 1929 | * KeyStuffed is TRUE if the character comes from the stuff buffer. |
| 1930 | * if "advance" is FALSE (vpeekc()): |
| 1931 | * just look whether there is a character available. |
| 1932 | * |
| 1933 | * When "no_mapping" is zero, checks for mappings in the current mode. |
| 1934 | * Only returns one byte (of a multi-byte character). |
| 1935 | * K_SPECIAL and CSI may be escaped, need to get two more bytes then. |
| 1936 | */ |
| 1937 | static int |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 1938 | vgetorpeek(int advance) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1939 | { |
| 1940 | int c, c1; |
| 1941 | int keylen; |
| 1942 | char_u *s; |
| 1943 | mapblock_T *mp; |
| 1944 | #ifdef FEAT_LOCALMAP |
| 1945 | mapblock_T *mp2; |
| 1946 | #endif |
| 1947 | mapblock_T *mp_match; |
| 1948 | int mp_match_len = 0; |
| 1949 | int timedout = FALSE; /* waited for more than 1 second |
| 1950 | for mapping to complete */ |
| 1951 | int mapdepth = 0; /* check for recursive mapping */ |
| 1952 | int mode_deleted = FALSE; /* set when mode has been deleted */ |
| 1953 | int local_State; |
| 1954 | int mlen; |
| 1955 | int max_mlen; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1956 | int i; |
Bram Moolenaar | 4e42719 | 2006-03-10 21:34:27 +0000 | [diff] [blame] | 1957 | #ifdef FEAT_CMDL_INFO |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1958 | int new_wcol, new_wrow; |
| 1959 | #endif |
| 1960 | #ifdef FEAT_GUI |
| 1961 | # ifdef FEAT_MENU |
| 1962 | int idx; |
| 1963 | # endif |
| 1964 | int shape_changed = FALSE; /* adjusted cursor shape */ |
| 1965 | #endif |
| 1966 | int n; |
| 1967 | #ifdef FEAT_LANGMAP |
| 1968 | int nolmaplen; |
| 1969 | #endif |
| 1970 | int old_wcol, old_wrow; |
Bram Moolenaar | 28a37ff | 2005-03-15 22:28:00 +0000 | [diff] [blame] | 1971 | int wait_tb_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1972 | |
| 1973 | /* |
| 1974 | * This function doesn't work very well when called recursively. This may |
| 1975 | * happen though, because of: |
| 1976 | * 1. The call to add_to_showcmd(). char_avail() is then used to check if |
| 1977 | * there is a character available, which calls this function. In that |
| 1978 | * case we must return NUL, to indicate no character is available. |
| 1979 | * 2. A GUI callback function writes to the screen, causing a |
| 1980 | * wait_return(). |
| 1981 | * Using ":normal" can also do this, but it saves the typeahead buffer, |
| 1982 | * thus it should be OK. But don't get a key from the user then. |
| 1983 | */ |
Bram Moolenaar | 5555acc | 2006-04-07 21:33:12 +0000 | [diff] [blame] | 1984 | if (vgetc_busy > 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1985 | #ifdef FEAT_EX_EXTRA |
| 1986 | && ex_normal_busy == 0 |
| 1987 | #endif |
| 1988 | ) |
| 1989 | return NUL; |
| 1990 | |
| 1991 | local_State = get_real_state(); |
| 1992 | |
Bram Moolenaar | 5555acc | 2006-04-07 21:33:12 +0000 | [diff] [blame] | 1993 | ++vgetc_busy; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1994 | |
| 1995 | if (advance) |
| 1996 | KeyStuffed = FALSE; |
| 1997 | |
| 1998 | init_typebuf(); |
| 1999 | start_stuff(); |
| 2000 | if (advance && typebuf.tb_maplen == 0) |
| 2001 | Exec_reg = FALSE; |
| 2002 | do |
| 2003 | { |
| 2004 | /* |
| 2005 | * get a character: 1. from the stuffbuffer |
| 2006 | */ |
| 2007 | if (typeahead_char != 0) |
| 2008 | { |
| 2009 | c = typeahead_char; |
| 2010 | if (advance) |
| 2011 | typeahead_char = 0; |
| 2012 | } |
| 2013 | else |
Bram Moolenaar | 0a36fec | 2014-02-11 15:10:43 +0100 | [diff] [blame] | 2014 | c = read_readbuffers(advance); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2015 | if (c != NUL && !got_int) |
| 2016 | { |
| 2017 | if (advance) |
| 2018 | { |
| 2019 | /* KeyTyped = FALSE; When the command that stuffed something |
| 2020 | * was typed, behave like the stuffed command was typed. |
| 2021 | * needed for CTRL-W CTRl-] to open a fold, for example. */ |
| 2022 | KeyStuffed = TRUE; |
| 2023 | } |
| 2024 | if (typebuf.tb_no_abbr_cnt == 0) |
| 2025 | typebuf.tb_no_abbr_cnt = 1; /* no abbreviations now */ |
| 2026 | } |
| 2027 | else |
| 2028 | { |
| 2029 | /* |
| 2030 | * Loop until we either find a matching mapped key, or we |
| 2031 | * are sure that it is not a mapped key. |
| 2032 | * If a mapped key sequence is found we go back to the start to |
| 2033 | * try re-mapping. |
| 2034 | */ |
| 2035 | for (;;) |
| 2036 | { |
| 2037 | /* |
| 2038 | * ui_breakcheck() is slow, don't use it too often when |
| 2039 | * inside a mapping. But call it each time for typed |
| 2040 | * characters. |
| 2041 | */ |
| 2042 | if (typebuf.tb_maplen) |
| 2043 | line_breakcheck(); |
| 2044 | else |
| 2045 | ui_breakcheck(); /* check for CTRL-C */ |
| 2046 | keylen = 0; |
| 2047 | if (got_int) |
| 2048 | { |
| 2049 | /* flush all input */ |
| 2050 | c = inchar(typebuf.tb_buf, typebuf.tb_buflen - 1, 0L, |
| 2051 | typebuf.tb_change_cnt); |
| 2052 | /* |
| 2053 | * If inchar() returns TRUE (script file was active) or we |
| 2054 | * are inside a mapping, get out of insert mode. |
| 2055 | * Otherwise we behave like having gotten a CTRL-C. |
| 2056 | * As a result typing CTRL-C in insert mode will |
| 2057 | * really insert a CTRL-C. |
| 2058 | */ |
| 2059 | if ((c || typebuf.tb_maplen) |
| 2060 | && (State & (INSERT + CMDLINE))) |
| 2061 | c = ESC; |
| 2062 | else |
| 2063 | c = Ctrl_C; |
| 2064 | flush_buffers(TRUE); /* flush all typeahead */ |
| 2065 | |
Bram Moolenaar | d9dfd57 | 2006-10-03 13:36:13 +0000 | [diff] [blame] | 2066 | if (advance) |
| 2067 | { |
| 2068 | /* Also record this character, it might be needed to |
| 2069 | * get out of Insert mode. */ |
| 2070 | *typebuf.tb_buf = c; |
| 2071 | gotchars(typebuf.tb_buf, 1); |
| 2072 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2073 | cmd_silent = FALSE; |
| 2074 | |
| 2075 | break; |
| 2076 | } |
| 2077 | else if (typebuf.tb_len > 0) |
| 2078 | { |
| 2079 | /* |
| 2080 | * Check for a mappable key sequence. |
| 2081 | * Walk through one maphash[] list until we find an |
| 2082 | * entry that matches. |
| 2083 | * |
| 2084 | * Don't look for mappings if: |
| 2085 | * - no_mapping set: mapping disabled (e.g. for CTRL-V) |
| 2086 | * - maphash_valid not set: no mappings present. |
| 2087 | * - typebuf.tb_buf[typebuf.tb_off] should not be remapped |
| 2088 | * - in insert or cmdline mode and 'paste' option set |
| 2089 | * - waiting for "hit return to continue" and CR or SPACE |
| 2090 | * typed |
| 2091 | * - waiting for a char with --more-- |
| 2092 | * - in Ctrl-X mode, and we get a valid char for that mode |
| 2093 | */ |
| 2094 | mp = NULL; |
| 2095 | max_mlen = 0; |
| 2096 | c1 = typebuf.tb_buf[typebuf.tb_off]; |
| 2097 | if (no_mapping == 0 && maphash_valid |
| 2098 | && (no_zero_mapping == 0 || c1 != '0') |
| 2099 | && (typebuf.tb_maplen == 0 |
| 2100 | || (p_remap |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 2101 | && (typebuf.tb_noremap[typebuf.tb_off] |
| 2102 | & (RM_NONE|RM_ABBR)) == 0)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2103 | && !(p_paste && (State & (INSERT + CMDLINE))) |
| 2104 | && !(State == HITRETURN && (c1 == CAR || c1 == ' ')) |
| 2105 | && State != ASKMORE |
| 2106 | && State != CONFIRM |
| 2107 | #ifdef FEAT_INS_EXPAND |
| 2108 | && !((ctrl_x_mode != 0 && vim_is_ctrl_x_key(c1)) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2109 | || ((compl_cont_status & CONT_LOCAL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2110 | && (c1 == Ctrl_N || c1 == Ctrl_P))) |
| 2111 | #endif |
| 2112 | ) |
| 2113 | { |
| 2114 | #ifdef FEAT_LANGMAP |
| 2115 | if (c1 == K_SPECIAL) |
| 2116 | nolmaplen = 2; |
| 2117 | else |
| 2118 | { |
Bram Moolenaar | ae94b77 | 2015-07-10 17:19:30 +0200 | [diff] [blame] | 2119 | LANGMAP_ADJUST(c1, |
Bram Moolenaar | 2528163 | 2016-01-21 23:32:32 +0100 | [diff] [blame] | 2120 | (State & (CMDLINE | INSERT)) == 0 |
| 2121 | && get_real_state() != SELECTMODE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2122 | nolmaplen = 0; |
| 2123 | } |
| 2124 | #endif |
| 2125 | #ifdef FEAT_LOCALMAP |
| 2126 | /* First try buffer-local mappings. */ |
| 2127 | mp = curbuf->b_maphash[MAP_HASH(local_State, c1)]; |
| 2128 | mp2 = maphash[MAP_HASH(local_State, c1)]; |
| 2129 | if (mp == NULL) |
| 2130 | { |
Bram Moolenaar | 72179e1 | 2013-06-29 13:58:31 +0200 | [diff] [blame] | 2131 | /* There are no buffer-local mappings. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2132 | mp = mp2; |
| 2133 | mp2 = NULL; |
| 2134 | } |
| 2135 | #else |
| 2136 | mp = maphash[MAP_HASH(local_State, c1)]; |
| 2137 | #endif |
| 2138 | /* |
| 2139 | * Loop until a partly matching mapping is found or |
| 2140 | * all (local) mappings have been checked. |
| 2141 | * The longest full match is remembered in "mp_match". |
| 2142 | * A full match is only accepted if there is no partly |
| 2143 | * match, so "aa" and "aaa" can both be mapped. |
| 2144 | */ |
| 2145 | mp_match = NULL; |
| 2146 | mp_match_len = 0; |
| 2147 | for ( ; mp != NULL; |
| 2148 | #ifdef FEAT_LOCALMAP |
| 2149 | mp->m_next == NULL ? (mp = mp2, mp2 = NULL) : |
| 2150 | #endif |
| 2151 | (mp = mp->m_next)) |
| 2152 | { |
| 2153 | /* |
| 2154 | * Only consider an entry if the first character |
| 2155 | * matches and it is for the current state. |
| 2156 | * Skip ":lmap" mappings if keys were mapped. |
| 2157 | */ |
| 2158 | if (mp->m_keys[0] == c1 |
| 2159 | && (mp->m_mode & local_State) |
| 2160 | && ((mp->m_mode & LANGMAP) == 0 |
| 2161 | || typebuf.tb_maplen == 0)) |
| 2162 | { |
| 2163 | #ifdef FEAT_LANGMAP |
| 2164 | int nomap = nolmaplen; |
| 2165 | int c2; |
| 2166 | #endif |
| 2167 | /* find the match length of this mapping */ |
| 2168 | for (mlen = 1; mlen < typebuf.tb_len; ++mlen) |
| 2169 | { |
| 2170 | #ifdef FEAT_LANGMAP |
| 2171 | c2 = typebuf.tb_buf[typebuf.tb_off + mlen]; |
| 2172 | if (nomap > 0) |
| 2173 | --nomap; |
| 2174 | else if (c2 == K_SPECIAL) |
| 2175 | nomap = 2; |
| 2176 | else |
| 2177 | LANGMAP_ADJUST(c2, TRUE); |
| 2178 | if (mp->m_keys[mlen] != c2) |
| 2179 | #else |
| 2180 | if (mp->m_keys[mlen] != |
| 2181 | typebuf.tb_buf[typebuf.tb_off + mlen]) |
| 2182 | #endif |
| 2183 | break; |
| 2184 | } |
| 2185 | |
| 2186 | #ifdef FEAT_MBYTE |
| 2187 | /* Don't allow mapping the first byte(s) of a |
| 2188 | * multi-byte char. Happens when mapping |
Bram Moolenaar | 1d9ff43 | 2014-03-12 20:17:51 +0100 | [diff] [blame] | 2189 | * <M-a> and then changing 'encoding'. Beware |
| 2190 | * that 0x80 is escaped. */ |
| 2191 | { |
| 2192 | char_u *p1 = mp->m_keys; |
| 2193 | char_u *p2 = mb_unescape(&p1); |
| 2194 | |
| 2195 | if (has_mbyte && p2 != NULL |
| 2196 | && MB_BYTE2LEN(c1) > MB_PTR2LEN(p2)) |
| 2197 | mlen = 0; |
| 2198 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2199 | #endif |
| 2200 | /* |
| 2201 | * Check an entry whether it matches. |
| 2202 | * - Full match: mlen == keylen |
| 2203 | * - Partly match: mlen == typebuf.tb_len |
| 2204 | */ |
| 2205 | keylen = mp->m_keylen; |
| 2206 | if (mlen == keylen |
| 2207 | || (mlen == typebuf.tb_len |
| 2208 | && typebuf.tb_len < keylen)) |
| 2209 | { |
| 2210 | /* |
| 2211 | * If only script-local mappings are |
| 2212 | * allowed, check if the mapping starts |
| 2213 | * with K_SNR. |
| 2214 | */ |
| 2215 | s = typebuf.tb_noremap + typebuf.tb_off; |
| 2216 | if (*s == RM_SCRIPT |
| 2217 | && (mp->m_keys[0] != K_SPECIAL |
| 2218 | || mp->m_keys[1] != KS_EXTRA |
| 2219 | || mp->m_keys[2] |
| 2220 | != (int)KE_SNR)) |
| 2221 | continue; |
| 2222 | /* |
| 2223 | * If one of the typed keys cannot be |
| 2224 | * remapped, skip the entry. |
| 2225 | */ |
| 2226 | for (n = mlen; --n >= 0; ) |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 2227 | if (*s++ & (RM_NONE|RM_ABBR)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2228 | break; |
| 2229 | if (n >= 0) |
| 2230 | continue; |
| 2231 | |
| 2232 | if (keylen > typebuf.tb_len) |
| 2233 | { |
Bram Moolenaar | 72179e1 | 2013-06-29 13:58:31 +0200 | [diff] [blame] | 2234 | if (!timedout && !(mp_match != NULL |
| 2235 | && mp_match->m_nowait)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2236 | { |
| 2237 | /* break at a partly match */ |
Bram Moolenaar | 946ffd4 | 2010-12-30 12:30:31 +0100 | [diff] [blame] | 2238 | keylen = KEYLEN_PART_MAP; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2239 | break; |
| 2240 | } |
| 2241 | } |
| 2242 | else if (keylen > mp_match_len) |
| 2243 | { |
| 2244 | /* found a longer match */ |
| 2245 | mp_match = mp; |
| 2246 | mp_match_len = keylen; |
| 2247 | } |
| 2248 | } |
| 2249 | else |
| 2250 | /* No match; may have to check for |
| 2251 | * termcode at next character. */ |
| 2252 | if (max_mlen < mlen) |
| 2253 | max_mlen = mlen; |
| 2254 | } |
| 2255 | } |
| 2256 | |
| 2257 | /* If no partly match found, use the longest full |
| 2258 | * match. */ |
Bram Moolenaar | 946ffd4 | 2010-12-30 12:30:31 +0100 | [diff] [blame] | 2259 | if (keylen != KEYLEN_PART_MAP) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2260 | { |
| 2261 | mp = mp_match; |
| 2262 | keylen = mp_match_len; |
| 2263 | } |
| 2264 | } |
| 2265 | |
| 2266 | /* Check for match with 'pastetoggle' */ |
| 2267 | if (*p_pt != NUL && mp == NULL && (State & (INSERT|NORMAL))) |
| 2268 | { |
| 2269 | for (mlen = 0; mlen < typebuf.tb_len && p_pt[mlen]; |
| 2270 | ++mlen) |
| 2271 | if (p_pt[mlen] != typebuf.tb_buf[typebuf.tb_off |
| 2272 | + mlen]) |
| 2273 | break; |
| 2274 | if (p_pt[mlen] == NUL) /* match */ |
| 2275 | { |
| 2276 | /* write chars to script file(s) */ |
| 2277 | if (mlen > typebuf.tb_maplen) |
| 2278 | gotchars(typebuf.tb_buf + typebuf.tb_off |
| 2279 | + typebuf.tb_maplen, |
| 2280 | mlen - typebuf.tb_maplen); |
| 2281 | |
| 2282 | del_typebuf(mlen, 0); /* remove the chars */ |
| 2283 | set_option_value((char_u *)"paste", |
| 2284 | (long)!p_paste, NULL, 0); |
| 2285 | if (!(State & INSERT)) |
| 2286 | { |
| 2287 | msg_col = 0; |
| 2288 | msg_row = Rows - 1; |
| 2289 | msg_clr_eos(); /* clear ruler */ |
| 2290 | } |
Bram Moolenaar | 06811f3 | 2014-02-15 16:17:07 +0100 | [diff] [blame] | 2291 | #ifdef FEAT_WINDOWS |
| 2292 | status_redraw_all(); |
| 2293 | redraw_statuslines(); |
| 2294 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2295 | showmode(); |
| 2296 | setcursor(); |
| 2297 | continue; |
| 2298 | } |
| 2299 | /* Need more chars for partly match. */ |
| 2300 | if (mlen == typebuf.tb_len) |
Bram Moolenaar | 946ffd4 | 2010-12-30 12:30:31 +0100 | [diff] [blame] | 2301 | keylen = KEYLEN_PART_KEY; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2302 | else if (max_mlen < mlen) |
| 2303 | /* no match, may have to check for termcode at |
| 2304 | * next character */ |
| 2305 | max_mlen = mlen + 1; |
| 2306 | } |
| 2307 | |
| 2308 | if ((mp == NULL || max_mlen >= mp_match_len) |
Bram Moolenaar | 946ffd4 | 2010-12-30 12:30:31 +0100 | [diff] [blame] | 2309 | && keylen != KEYLEN_PART_MAP) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2310 | { |
Bram Moolenaar | f233048 | 2008-06-24 20:19:36 +0000 | [diff] [blame] | 2311 | int save_keylen = keylen; |
| 2312 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2313 | /* |
| 2314 | * When no matching mapping found or found a |
| 2315 | * non-matching mapping that matches at least what the |
| 2316 | * matching mapping matched: |
| 2317 | * Check if we have a terminal code, when: |
| 2318 | * mapping is allowed, |
| 2319 | * keys have not been mapped, |
| 2320 | * and not an ESC sequence, not in insert mode or |
| 2321 | * p_ek is on, |
| 2322 | * and when not timed out, |
| 2323 | */ |
| 2324 | if ((no_mapping == 0 || allow_keys != 0) |
| 2325 | && (typebuf.tb_maplen == 0 |
| 2326 | || (p_remap && typebuf.tb_noremap[ |
| 2327 | typebuf.tb_off] == RM_YES)) |
| 2328 | && !timedout) |
| 2329 | { |
Bram Moolenaar | a8c8a68 | 2012-02-05 22:05:48 +0100 | [diff] [blame] | 2330 | keylen = check_termcode(max_mlen + 1, |
| 2331 | NULL, 0, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2332 | |
Bram Moolenaar | f233048 | 2008-06-24 20:19:36 +0000 | [diff] [blame] | 2333 | /* If no termcode matched but 'pastetoggle' |
| 2334 | * matched partially it's like an incomplete key |
| 2335 | * sequence. */ |
Bram Moolenaar | 946ffd4 | 2010-12-30 12:30:31 +0100 | [diff] [blame] | 2336 | if (keylen == 0 && save_keylen == KEYLEN_PART_KEY) |
| 2337 | keylen = KEYLEN_PART_KEY; |
Bram Moolenaar | f233048 | 2008-06-24 20:19:36 +0000 | [diff] [blame] | 2338 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2339 | /* |
| 2340 | * When getting a partial match, but the last |
| 2341 | * characters were not typed, don't wait for a |
| 2342 | * typed character to complete the termcode. |
| 2343 | * This helps a lot when a ":normal" command ends |
| 2344 | * in an ESC. |
| 2345 | */ |
| 2346 | if (keylen < 0 |
| 2347 | && typebuf.tb_len == typebuf.tb_maplen) |
| 2348 | keylen = 0; |
| 2349 | } |
| 2350 | else |
| 2351 | keylen = 0; |
| 2352 | if (keylen == 0) /* no matching terminal code */ |
| 2353 | { |
| 2354 | #ifdef AMIGA /* check for window bounds report */ |
| 2355 | if (typebuf.tb_maplen == 0 && (typebuf.tb_buf[ |
| 2356 | typebuf.tb_off] & 0xff) == CSI) |
| 2357 | { |
| 2358 | for (s = typebuf.tb_buf + typebuf.tb_off + 1; |
| 2359 | s < typebuf.tb_buf + typebuf.tb_off |
| 2360 | + typebuf.tb_len |
| 2361 | && (VIM_ISDIGIT(*s) || *s == ';' |
| 2362 | || *s == ' '); |
| 2363 | ++s) |
| 2364 | ; |
| 2365 | if (*s == 'r' || *s == '|') /* found one */ |
| 2366 | { |
| 2367 | del_typebuf((int)(s + 1 - |
| 2368 | (typebuf.tb_buf + typebuf.tb_off)), 0); |
| 2369 | /* get size and redraw screen */ |
| 2370 | shell_resized(); |
| 2371 | continue; |
| 2372 | } |
| 2373 | if (*s == NUL) /* need more characters */ |
Bram Moolenaar | 946ffd4 | 2010-12-30 12:30:31 +0100 | [diff] [blame] | 2374 | keylen = KEYLEN_PART_KEY; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2375 | } |
| 2376 | if (keylen >= 0) |
| 2377 | #endif |
| 2378 | /* When there was a matching mapping and no |
| 2379 | * termcode could be replaced after another one, |
Bram Moolenaar | f233048 | 2008-06-24 20:19:36 +0000 | [diff] [blame] | 2380 | * use that mapping (loop around). If there was |
| 2381 | * no mapping use the character from the |
| 2382 | * typeahead buffer right here. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2383 | if (mp == NULL) |
| 2384 | { |
| 2385 | /* |
| 2386 | * get a character: 2. from the typeahead buffer |
| 2387 | */ |
| 2388 | c = typebuf.tb_buf[typebuf.tb_off] & 255; |
| 2389 | if (advance) /* remove chars from tb_buf */ |
| 2390 | { |
| 2391 | cmd_silent = (typebuf.tb_silent > 0); |
| 2392 | if (typebuf.tb_maplen > 0) |
| 2393 | KeyTyped = FALSE; |
| 2394 | else |
| 2395 | { |
| 2396 | KeyTyped = TRUE; |
| 2397 | /* write char to script file(s) */ |
| 2398 | gotchars(typebuf.tb_buf |
| 2399 | + typebuf.tb_off, 1); |
| 2400 | } |
Bram Moolenaar | cf8e7d1 | 2006-12-05 20:43:17 +0000 | [diff] [blame] | 2401 | KeyNoremap = typebuf.tb_noremap[ |
| 2402 | typebuf.tb_off]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2403 | del_typebuf(1, 0); |
| 2404 | } |
| 2405 | break; /* got character, break for loop */ |
| 2406 | } |
| 2407 | } |
| 2408 | if (keylen > 0) /* full matching terminal code */ |
| 2409 | { |
| 2410 | #if defined(FEAT_GUI) && defined(FEAT_MENU) |
Bram Moolenaar | 946ffd4 | 2010-12-30 12:30:31 +0100 | [diff] [blame] | 2411 | if (typebuf.tb_len >= 2 |
| 2412 | && typebuf.tb_buf[typebuf.tb_off] == K_SPECIAL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2413 | && typebuf.tb_buf[typebuf.tb_off + 1] |
| 2414 | == KS_MENU) |
| 2415 | { |
| 2416 | /* |
| 2417 | * Using a menu may cause a break in undo! |
| 2418 | * It's like using gotchars(), but without |
| 2419 | * recording or writing to a script file. |
| 2420 | */ |
| 2421 | may_sync_undo(); |
| 2422 | del_typebuf(3, 0); |
| 2423 | idx = get_menu_index(current_menu, local_State); |
| 2424 | if (idx != MENU_INDEX_INVALID) |
| 2425 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2426 | /* |
Bram Moolenaar | 371d540 | 2006-03-20 21:47:49 +0000 | [diff] [blame] | 2427 | * In Select mode and a Visual mode menu |
| 2428 | * is used: Switch to Visual mode |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2429 | * temporarily. Append K_SELECT to switch |
| 2430 | * back to Select mode. |
| 2431 | */ |
Bram Moolenaar | 371d540 | 2006-03-20 21:47:49 +0000 | [diff] [blame] | 2432 | if (VIsual_active && VIsual_select |
| 2433 | && (current_menu->modes & VISUAL)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2434 | { |
| 2435 | VIsual_select = FALSE; |
| 2436 | (void)ins_typebuf(K_SELECT_STRING, |
| 2437 | REMAP_NONE, 0, TRUE, FALSE); |
| 2438 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2439 | ins_typebuf(current_menu->strings[idx], |
| 2440 | current_menu->noremap[idx], |
| 2441 | 0, TRUE, |
| 2442 | current_menu->silent[idx]); |
| 2443 | } |
| 2444 | } |
Bram Moolenaar | ee3f7a5 | 2008-01-01 13:17:56 +0000 | [diff] [blame] | 2445 | #endif /* FEAT_GUI && FEAT_MENU */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2446 | continue; /* try mapping again */ |
| 2447 | } |
| 2448 | |
| 2449 | /* Partial match: get some more characters. When a |
| 2450 | * matching mapping was found use that one. */ |
| 2451 | if (mp == NULL || keylen < 0) |
Bram Moolenaar | 946ffd4 | 2010-12-30 12:30:31 +0100 | [diff] [blame] | 2452 | keylen = KEYLEN_PART_KEY; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2453 | else |
| 2454 | keylen = mp_match_len; |
| 2455 | } |
| 2456 | |
| 2457 | /* complete match */ |
| 2458 | if (keylen >= 0 && keylen <= typebuf.tb_len) |
| 2459 | { |
Bram Moolenaar | 0dbf720 | 2010-01-27 17:31:43 +0100 | [diff] [blame] | 2460 | #ifdef FEAT_EVAL |
| 2461 | int save_m_expr; |
| 2462 | int save_m_noremap; |
| 2463 | int save_m_silent; |
| 2464 | char_u *save_m_keys; |
| 2465 | char_u *save_m_str; |
| 2466 | #else |
| 2467 | # define save_m_noremap mp->m_noremap |
| 2468 | # define save_m_silent mp->m_silent |
| 2469 | #endif |
| 2470 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2471 | /* write chars to script file(s) */ |
| 2472 | if (keylen > typebuf.tb_maplen) |
| 2473 | gotchars(typebuf.tb_buf + typebuf.tb_off |
| 2474 | + typebuf.tb_maplen, |
| 2475 | keylen - typebuf.tb_maplen); |
| 2476 | |
| 2477 | cmd_silent = (typebuf.tb_silent > 0); |
| 2478 | del_typebuf(keylen, 0); /* remove the mapped keys */ |
| 2479 | |
| 2480 | /* |
| 2481 | * Put the replacement string in front of mapstr. |
| 2482 | * The depth check catches ":map x y" and ":map y x". |
| 2483 | */ |
| 2484 | if (++mapdepth >= p_mmd) |
| 2485 | { |
| 2486 | EMSG(_("E223: recursive mapping")); |
| 2487 | if (State & CMDLINE) |
| 2488 | redrawcmdline(); |
| 2489 | else |
| 2490 | setcursor(); |
| 2491 | flush_buffers(FALSE); |
| 2492 | mapdepth = 0; /* for next one */ |
| 2493 | c = -1; |
| 2494 | break; |
| 2495 | } |
| 2496 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2497 | /* |
Bram Moolenaar | 371d540 | 2006-03-20 21:47:49 +0000 | [diff] [blame] | 2498 | * In Select mode and a Visual mode mapping is used: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2499 | * Switch to Visual mode temporarily. Append K_SELECT |
| 2500 | * to switch back to Select mode. |
| 2501 | */ |
Bram Moolenaar | 371d540 | 2006-03-20 21:47:49 +0000 | [diff] [blame] | 2502 | if (VIsual_active && VIsual_select |
| 2503 | && (mp->m_mode & VISUAL)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2504 | { |
| 2505 | VIsual_select = FALSE; |
| 2506 | (void)ins_typebuf(K_SELECT_STRING, REMAP_NONE, |
| 2507 | 0, TRUE, FALSE); |
| 2508 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2509 | |
Bram Moolenaar | 4e42719 | 2006-03-10 21:34:27 +0000 | [diff] [blame] | 2510 | #ifdef FEAT_EVAL |
Bram Moolenaar | 0dbf720 | 2010-01-27 17:31:43 +0100 | [diff] [blame] | 2511 | /* Copy the values from *mp that are used, because |
| 2512 | * evaluating the expression may invoke a function |
| 2513 | * that redefines the mapping, thereby making *mp |
| 2514 | * invalid. */ |
| 2515 | save_m_expr = mp->m_expr; |
| 2516 | save_m_noremap = mp->m_noremap; |
| 2517 | save_m_silent = mp->m_silent; |
| 2518 | save_m_keys = NULL; /* only saved when needed */ |
| 2519 | save_m_str = NULL; /* only saved when needed */ |
| 2520 | |
Bram Moolenaar | 4e42719 | 2006-03-10 21:34:27 +0000 | [diff] [blame] | 2521 | /* |
| 2522 | * Handle ":map <expr>": evaluate the {rhs} as an |
Bram Moolenaar | b3479bd | 2011-10-12 22:02:14 +0200 | [diff] [blame] | 2523 | * expression. Also save and restore the command line |
| 2524 | * for "normal :". |
Bram Moolenaar | 4e42719 | 2006-03-10 21:34:27 +0000 | [diff] [blame] | 2525 | */ |
| 2526 | if (mp->m_expr) |
Bram Moolenaar | 5555acc | 2006-04-07 21:33:12 +0000 | [diff] [blame] | 2527 | { |
Bram Moolenaar | 5555acc | 2006-04-07 21:33:12 +0000 | [diff] [blame] | 2528 | int save_vgetc_busy = vgetc_busy; |
| 2529 | |
Bram Moolenaar | b3479bd | 2011-10-12 22:02:14 +0200 | [diff] [blame] | 2530 | vgetc_busy = 0; |
| 2531 | save_m_keys = vim_strsave(mp->m_keys); |
| 2532 | save_m_str = vim_strsave(mp->m_str); |
| 2533 | s = eval_map_expr(save_m_str, NUL); |
| 2534 | vgetc_busy = save_vgetc_busy; |
Bram Moolenaar | 5555acc | 2006-04-07 21:33:12 +0000 | [diff] [blame] | 2535 | } |
Bram Moolenaar | 4e42719 | 2006-03-10 21:34:27 +0000 | [diff] [blame] | 2536 | else |
| 2537 | #endif |
| 2538 | s = mp->m_str; |
| 2539 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2540 | /* |
| 2541 | * Insert the 'to' part in the typebuf.tb_buf. |
| 2542 | * If 'from' field is the same as the start of the |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 2543 | * 'to' field, don't remap the first character (but do |
| 2544 | * allow abbreviations). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2545 | * If m_noremap is set, don't remap the whole 'to' |
| 2546 | * part. |
| 2547 | */ |
Bram Moolenaar | 4e42719 | 2006-03-10 21:34:27 +0000 | [diff] [blame] | 2548 | if (s == NULL) |
| 2549 | i = FAIL; |
| 2550 | else |
| 2551 | { |
Bram Moolenaar | d21d9a6 | 2010-01-28 22:58:16 +0100 | [diff] [blame] | 2552 | int noremap; |
| 2553 | |
| 2554 | if (save_m_noremap != REMAP_YES) |
| 2555 | noremap = save_m_noremap; |
| 2556 | else if ( |
Bram Moolenaar | 0dbf720 | 2010-01-27 17:31:43 +0100 | [diff] [blame] | 2557 | #ifdef FEAT_EVAL |
Bram Moolenaar | d21d9a6 | 2010-01-28 22:58:16 +0100 | [diff] [blame] | 2558 | STRNCMP(s, save_m_keys != NULL |
| 2559 | ? save_m_keys : mp->m_keys, |
| 2560 | (size_t)keylen) |
| 2561 | #else |
| 2562 | STRNCMP(s, mp->m_keys, (size_t)keylen) |
Bram Moolenaar | 0dbf720 | 2010-01-27 17:31:43 +0100 | [diff] [blame] | 2563 | #endif |
Bram Moolenaar | d21d9a6 | 2010-01-28 22:58:16 +0100 | [diff] [blame] | 2564 | != 0) |
| 2565 | noremap = REMAP_YES; |
| 2566 | else |
| 2567 | noremap = REMAP_SKIP; |
| 2568 | i = ins_typebuf(s, noremap, |
| 2569 | 0, TRUE, cmd_silent || save_m_silent); |
Bram Moolenaar | 4e42719 | 2006-03-10 21:34:27 +0000 | [diff] [blame] | 2570 | #ifdef FEAT_EVAL |
Bram Moolenaar | 0dbf720 | 2010-01-27 17:31:43 +0100 | [diff] [blame] | 2571 | if (save_m_expr) |
Bram Moolenaar | 4e42719 | 2006-03-10 21:34:27 +0000 | [diff] [blame] | 2572 | vim_free(s); |
| 2573 | #endif |
| 2574 | } |
Bram Moolenaar | 0dbf720 | 2010-01-27 17:31:43 +0100 | [diff] [blame] | 2575 | #ifdef FEAT_EVAL |
| 2576 | vim_free(save_m_keys); |
| 2577 | vim_free(save_m_str); |
| 2578 | #endif |
Bram Moolenaar | 4e42719 | 2006-03-10 21:34:27 +0000 | [diff] [blame] | 2579 | if (i == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2580 | { |
| 2581 | c = -1; |
| 2582 | break; |
| 2583 | } |
| 2584 | continue; |
| 2585 | } |
| 2586 | } |
| 2587 | |
| 2588 | /* |
| 2589 | * get a character: 3. from the user - handle <Esc> in Insert mode |
| 2590 | */ |
| 2591 | /* |
| 2592 | * special case: if we get an <ESC> in insert mode and there |
| 2593 | * are no more characters at once, we pretend to go out of |
| 2594 | * insert mode. This prevents the one second delay after |
| 2595 | * typing an <ESC>. If we get something after all, we may |
| 2596 | * have to redisplay the mode. That the cursor is in the wrong |
| 2597 | * place does not matter. |
| 2598 | */ |
| 2599 | c = 0; |
| 2600 | #ifdef FEAT_CMDL_INFO |
| 2601 | new_wcol = curwin->w_wcol; |
| 2602 | new_wrow = curwin->w_wrow; |
| 2603 | #endif |
| 2604 | if ( advance |
| 2605 | && typebuf.tb_len == 1 |
| 2606 | && typebuf.tb_buf[typebuf.tb_off] == ESC |
| 2607 | && !no_mapping |
| 2608 | #ifdef FEAT_EX_EXTRA |
| 2609 | && ex_normal_busy == 0 |
| 2610 | #endif |
| 2611 | && typebuf.tb_maplen == 0 |
| 2612 | && (State & INSERT) |
Bram Moolenaar | 946ffd4 | 2010-12-30 12:30:31 +0100 | [diff] [blame] | 2613 | && (p_timeout |
| 2614 | || (keylen == KEYLEN_PART_KEY && p_ttimeout)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2615 | && (c = inchar(typebuf.tb_buf + typebuf.tb_off |
| 2616 | + typebuf.tb_len, 3, 25L, |
| 2617 | typebuf.tb_change_cnt)) == 0) |
| 2618 | { |
| 2619 | colnr_T col = 0, vcol; |
| 2620 | char_u *ptr; |
| 2621 | |
Bram Moolenaar | 28c258f | 2006-01-25 22:02:51 +0000 | [diff] [blame] | 2622 | if (mode_displayed) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2623 | { |
| 2624 | unshowmode(TRUE); |
| 2625 | mode_deleted = TRUE; |
| 2626 | } |
| 2627 | #ifdef FEAT_GUI |
| 2628 | /* may show different cursor shape */ |
| 2629 | if (gui.in_use) |
| 2630 | { |
| 2631 | int save_State; |
| 2632 | |
| 2633 | save_State = State; |
| 2634 | State = NORMAL; |
| 2635 | gui_update_cursor(TRUE, FALSE); |
| 2636 | State = save_State; |
| 2637 | shape_changed = TRUE; |
| 2638 | } |
| 2639 | #endif |
| 2640 | validate_cursor(); |
| 2641 | old_wcol = curwin->w_wcol; |
| 2642 | old_wrow = curwin->w_wrow; |
| 2643 | |
| 2644 | /* move cursor left, if possible */ |
| 2645 | if (curwin->w_cursor.col != 0) |
| 2646 | { |
| 2647 | if (curwin->w_wcol > 0) |
| 2648 | { |
| 2649 | if (did_ai) |
| 2650 | { |
| 2651 | /* |
| 2652 | * We are expecting to truncate the trailing |
| 2653 | * white-space, so find the last non-white |
| 2654 | * character -- webb |
| 2655 | */ |
| 2656 | col = vcol = curwin->w_wcol = 0; |
| 2657 | ptr = ml_get_curline(); |
| 2658 | while (col < curwin->w_cursor.col) |
| 2659 | { |
| 2660 | if (!vim_iswhite(ptr[col])) |
| 2661 | curwin->w_wcol = vcol; |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2662 | vcol += lbr_chartabsize(ptr, ptr + col, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2663 | (colnr_T)vcol); |
| 2664 | #ifdef FEAT_MBYTE |
| 2665 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2666 | col += (*mb_ptr2len)(ptr + col); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2667 | else |
| 2668 | #endif |
| 2669 | ++col; |
| 2670 | } |
| 2671 | curwin->w_wrow = curwin->w_cline_row |
| 2672 | + curwin->w_wcol / W_WIDTH(curwin); |
| 2673 | curwin->w_wcol %= W_WIDTH(curwin); |
| 2674 | curwin->w_wcol += curwin_col_off(); |
| 2675 | #ifdef FEAT_MBYTE |
| 2676 | col = 0; /* no correction needed */ |
| 2677 | #endif |
| 2678 | } |
| 2679 | else |
| 2680 | { |
| 2681 | --curwin->w_wcol; |
| 2682 | #ifdef FEAT_MBYTE |
| 2683 | col = curwin->w_cursor.col - 1; |
| 2684 | #endif |
| 2685 | } |
| 2686 | } |
| 2687 | else if (curwin->w_p_wrap && curwin->w_wrow) |
| 2688 | { |
| 2689 | --curwin->w_wrow; |
| 2690 | curwin->w_wcol = W_WIDTH(curwin) - 1; |
| 2691 | #ifdef FEAT_MBYTE |
| 2692 | col = curwin->w_cursor.col - 1; |
| 2693 | #endif |
| 2694 | } |
| 2695 | #ifdef FEAT_MBYTE |
| 2696 | if (has_mbyte && col > 0 && curwin->w_wcol > 0) |
| 2697 | { |
| 2698 | /* Correct when the cursor is on the right halve |
| 2699 | * of a double-wide character. */ |
| 2700 | ptr = ml_get_curline(); |
| 2701 | col -= (*mb_head_off)(ptr, ptr + col); |
| 2702 | if ((*mb_ptr2cells)(ptr + col) > 1) |
| 2703 | --curwin->w_wcol; |
| 2704 | } |
| 2705 | #endif |
| 2706 | } |
| 2707 | setcursor(); |
| 2708 | out_flush(); |
| 2709 | #ifdef FEAT_CMDL_INFO |
| 2710 | new_wcol = curwin->w_wcol; |
| 2711 | new_wrow = curwin->w_wrow; |
| 2712 | #endif |
| 2713 | curwin->w_wcol = old_wcol; |
| 2714 | curwin->w_wrow = old_wrow; |
| 2715 | } |
| 2716 | if (c < 0) |
| 2717 | continue; /* end of input script reached */ |
Bram Moolenaar | 20c3892 | 2014-07-23 20:41:14 +0200 | [diff] [blame] | 2718 | |
| 2719 | /* Allow mapping for just typed characters. When we get here c |
| 2720 | * is the number of extra bytes and typebuf.tb_len is 1. */ |
| 2721 | for (n = 1; n <= c; ++n) |
| 2722 | typebuf.tb_noremap[typebuf.tb_off + n] = RM_YES; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2723 | typebuf.tb_len += c; |
| 2724 | |
| 2725 | /* buffer full, don't map */ |
| 2726 | if (typebuf.tb_len >= typebuf.tb_maplen + MAXMAPLEN) |
| 2727 | { |
| 2728 | timedout = TRUE; |
| 2729 | continue; |
| 2730 | } |
| 2731 | |
| 2732 | #ifdef FEAT_EX_EXTRA |
| 2733 | if (ex_normal_busy > 0) |
| 2734 | { |
| 2735 | # ifdef FEAT_CMDWIN |
| 2736 | static int tc = 0; |
| 2737 | # endif |
| 2738 | |
| 2739 | /* No typeahead left and inside ":normal". Must return |
| 2740 | * something to avoid getting stuck. When an incomplete |
| 2741 | * mapping is present, behave like it timed out. */ |
| 2742 | if (typebuf.tb_len > 0) |
| 2743 | { |
| 2744 | timedout = TRUE; |
| 2745 | continue; |
| 2746 | } |
| 2747 | /* When 'insertmode' is set, ESC just beeps in Insert |
| 2748 | * mode. Use CTRL-L to make edit() return. |
| 2749 | * For the command line only CTRL-C always breaks it. |
| 2750 | * For the cmdline window: Alternate between ESC and |
| 2751 | * CTRL-C: ESC for most situations and CTRL-C to close the |
| 2752 | * cmdline window. */ |
| 2753 | if (p_im && (State & INSERT)) |
| 2754 | c = Ctrl_L; |
| 2755 | else if ((State & CMDLINE) |
| 2756 | # ifdef FEAT_CMDWIN |
| 2757 | || (cmdwin_type > 0 && tc == ESC) |
| 2758 | # endif |
| 2759 | ) |
| 2760 | c = Ctrl_C; |
| 2761 | else |
| 2762 | c = ESC; |
| 2763 | # ifdef FEAT_CMDWIN |
| 2764 | tc = c; |
| 2765 | # endif |
| 2766 | break; |
| 2767 | } |
| 2768 | #endif |
| 2769 | |
| 2770 | /* |
| 2771 | * get a character: 3. from the user - update display |
| 2772 | */ |
| 2773 | /* In insert mode a screen update is skipped when characters |
| 2774 | * are still available. But when those available characters |
| 2775 | * are part of a mapping, and we are going to do a blocking |
| 2776 | * wait here. Need to update the screen to display the |
Bram Moolenaar | 6eb634e | 2011-03-03 15:04:08 +0100 | [diff] [blame] | 2777 | * changed text so far. Also for when 'lazyredraw' is set and |
| 2778 | * redrawing was postponed because there was something in the |
| 2779 | * input buffer (e.g., termresponse). */ |
Bram Moolenaar | fd30cd4 | 2011-03-22 13:07:26 +0100 | [diff] [blame] | 2780 | if (((State & INSERT) != 0 || p_lz) && (State & CMDLINE) == 0 |
| 2781 | && advance && must_redraw != 0 && !need_wait_return) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2782 | { |
| 2783 | update_screen(0); |
| 2784 | setcursor(); /* put cursor back where it belongs */ |
| 2785 | } |
| 2786 | |
| 2787 | /* |
| 2788 | * If we have a partial match (and are going to wait for more |
| 2789 | * input from the user), show the partially matched characters |
| 2790 | * to the user with showcmd. |
| 2791 | */ |
| 2792 | #ifdef FEAT_CMDL_INFO |
| 2793 | i = 0; |
| 2794 | #endif |
| 2795 | c1 = 0; |
| 2796 | if (typebuf.tb_len > 0 && advance && !exmode_active) |
| 2797 | { |
| 2798 | if (((State & (NORMAL | INSERT)) || State == LANGMAP) |
| 2799 | && State != HITRETURN) |
| 2800 | { |
| 2801 | /* this looks nice when typing a dead character map */ |
| 2802 | if (State & INSERT |
| 2803 | && ptr2cells(typebuf.tb_buf + typebuf.tb_off |
| 2804 | + typebuf.tb_len - 1) == 1) |
| 2805 | { |
| 2806 | edit_putchar(typebuf.tb_buf[typebuf.tb_off |
| 2807 | + typebuf.tb_len - 1], FALSE); |
| 2808 | setcursor(); /* put cursor back where it belongs */ |
| 2809 | c1 = 1; |
| 2810 | } |
| 2811 | #ifdef FEAT_CMDL_INFO |
| 2812 | /* need to use the col and row from above here */ |
| 2813 | old_wcol = curwin->w_wcol; |
| 2814 | old_wrow = curwin->w_wrow; |
| 2815 | curwin->w_wcol = new_wcol; |
| 2816 | curwin->w_wrow = new_wrow; |
| 2817 | push_showcmd(); |
| 2818 | if (typebuf.tb_len > SHOWCMD_COLS) |
| 2819 | i = typebuf.tb_len - SHOWCMD_COLS; |
| 2820 | while (i < typebuf.tb_len) |
| 2821 | (void)add_to_showcmd(typebuf.tb_buf[typebuf.tb_off |
| 2822 | + i++]); |
| 2823 | curwin->w_wcol = old_wcol; |
| 2824 | curwin->w_wrow = old_wrow; |
| 2825 | #endif |
| 2826 | } |
| 2827 | |
| 2828 | /* this looks nice when typing a dead character map */ |
| 2829 | if ((State & CMDLINE) |
| 2830 | #if defined(FEAT_CRYPT) || defined(FEAT_EVAL) |
| 2831 | && cmdline_star == 0 |
| 2832 | #endif |
| 2833 | && ptr2cells(typebuf.tb_buf + typebuf.tb_off |
| 2834 | + typebuf.tb_len - 1) == 1) |
| 2835 | { |
| 2836 | putcmdline(typebuf.tb_buf[typebuf.tb_off |
| 2837 | + typebuf.tb_len - 1], FALSE); |
| 2838 | c1 = 1; |
| 2839 | } |
| 2840 | } |
| 2841 | |
| 2842 | /* |
| 2843 | * get a character: 3. from the user - get it |
| 2844 | */ |
Bram Moolenaar | 28a37ff | 2005-03-15 22:28:00 +0000 | [diff] [blame] | 2845 | wait_tb_len = typebuf.tb_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2846 | c = inchar(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_len, |
| 2847 | typebuf.tb_buflen - typebuf.tb_off - typebuf.tb_len - 1, |
| 2848 | !advance |
| 2849 | ? 0 |
| 2850 | : ((typebuf.tb_len == 0 |
| 2851 | || !(p_timeout || (p_ttimeout |
Bram Moolenaar | 946ffd4 | 2010-12-30 12:30:31 +0100 | [diff] [blame] | 2852 | && keylen == KEYLEN_PART_KEY))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2853 | ? -1L |
Bram Moolenaar | 946ffd4 | 2010-12-30 12:30:31 +0100 | [diff] [blame] | 2854 | : ((keylen == KEYLEN_PART_KEY && p_ttm >= 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2855 | ? p_ttm |
| 2856 | : p_tm)), typebuf.tb_change_cnt); |
| 2857 | |
| 2858 | #ifdef FEAT_CMDL_INFO |
| 2859 | if (i != 0) |
| 2860 | pop_showcmd(); |
| 2861 | #endif |
| 2862 | if (c1 == 1) |
| 2863 | { |
| 2864 | if (State & INSERT) |
| 2865 | edit_unputchar(); |
| 2866 | if (State & CMDLINE) |
| 2867 | unputcmdline(); |
Bram Moolenaar | bc256d9 | 2012-06-06 12:06:15 +0200 | [diff] [blame] | 2868 | else |
| 2869 | setcursor(); /* put cursor back where it belongs */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2870 | } |
| 2871 | |
| 2872 | if (c < 0) |
| 2873 | continue; /* end of input script reached */ |
| 2874 | if (c == NUL) /* no character available */ |
| 2875 | { |
| 2876 | if (!advance) |
| 2877 | break; |
Bram Moolenaar | 28a37ff | 2005-03-15 22:28:00 +0000 | [diff] [blame] | 2878 | if (wait_tb_len > 0) /* timed out */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2879 | { |
| 2880 | timedout = TRUE; |
| 2881 | continue; |
| 2882 | } |
| 2883 | } |
| 2884 | else |
| 2885 | { /* allow mapping for just typed characters */ |
| 2886 | while (typebuf.tb_buf[typebuf.tb_off |
| 2887 | + typebuf.tb_len] != NUL) |
| 2888 | typebuf.tb_noremap[typebuf.tb_off |
| 2889 | + typebuf.tb_len++] = RM_YES; |
| 2890 | #ifdef USE_IM_CONTROL |
| 2891 | /* Get IM status right after getting keys, not after the |
| 2892 | * timeout for a mapping (focus may be lost by then). */ |
| 2893 | vgetc_im_active = im_get_status(); |
| 2894 | #endif |
| 2895 | } |
| 2896 | } /* for (;;) */ |
| 2897 | } /* if (!character from stuffbuf) */ |
| 2898 | |
| 2899 | /* if advance is FALSE don't loop on NULs */ |
| 2900 | } while (c < 0 || (advance && c == NUL)); |
| 2901 | |
| 2902 | /* |
| 2903 | * The "INSERT" message is taken care of here: |
| 2904 | * if we return an ESC to exit insert mode, the message is deleted |
| 2905 | * if we don't return an ESC but deleted the message before, redisplay it |
| 2906 | */ |
Bram Moolenaar | 09df312 | 2006-01-23 22:23:09 +0000 | [diff] [blame] | 2907 | if (advance && p_smd && msg_silent == 0 && (State & INSERT)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2908 | { |
Bram Moolenaar | 28c258f | 2006-01-25 22:02:51 +0000 | [diff] [blame] | 2909 | if (c == ESC && !mode_deleted && !no_mapping && mode_displayed) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2910 | { |
| 2911 | if (typebuf.tb_len && !KeyTyped) |
| 2912 | redraw_cmdline = TRUE; /* delete mode later */ |
| 2913 | else |
| 2914 | unshowmode(FALSE); |
| 2915 | } |
| 2916 | else if (c != ESC && mode_deleted) |
| 2917 | { |
| 2918 | if (typebuf.tb_len && !KeyTyped) |
| 2919 | redraw_cmdline = TRUE; /* show mode later */ |
| 2920 | else |
| 2921 | showmode(); |
| 2922 | } |
| 2923 | } |
| 2924 | #ifdef FEAT_GUI |
| 2925 | /* may unshow different cursor shape */ |
| 2926 | if (gui.in_use && shape_changed) |
| 2927 | gui_update_cursor(TRUE, FALSE); |
| 2928 | #endif |
| 2929 | |
Bram Moolenaar | 5555acc | 2006-04-07 21:33:12 +0000 | [diff] [blame] | 2930 | --vgetc_busy; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2931 | |
| 2932 | return c; |
| 2933 | } |
| 2934 | |
| 2935 | /* |
| 2936 | * inchar() - get one character from |
| 2937 | * 1. a scriptfile |
| 2938 | * 2. the keyboard |
| 2939 | * |
| 2940 | * As much characters as we can get (upto 'maxlen') are put in "buf" and |
| 2941 | * NUL terminated (buffer length must be 'maxlen' + 1). |
| 2942 | * Minimum for "maxlen" is 3!!!! |
| 2943 | * |
| 2944 | * "tb_change_cnt" is the value of typebuf.tb_change_cnt if "buf" points into |
| 2945 | * it. When typebuf.tb_change_cnt changes (e.g., when a message is received |
| 2946 | * from a remote client) "buf" can no longer be used. "tb_change_cnt" is 0 |
| 2947 | * otherwise. |
| 2948 | * |
| 2949 | * If we got an interrupt all input is read until none is available. |
| 2950 | * |
| 2951 | * If wait_time == 0 there is no waiting for the char. |
| 2952 | * If wait_time == n we wait for n msec for a character to arrive. |
| 2953 | * If wait_time == -1 we wait forever for a character to arrive. |
| 2954 | * |
| 2955 | * Return the number of obtained characters. |
| 2956 | * Return -1 when end of input script reached. |
| 2957 | */ |
| 2958 | int |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 2959 | inchar( |
| 2960 | char_u *buf, |
| 2961 | int maxlen, |
| 2962 | long wait_time, /* milli seconds */ |
| 2963 | int tb_change_cnt) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2964 | { |
| 2965 | int len = 0; /* init for GCC */ |
| 2966 | int retesc = FALSE; /* return ESC with gotint */ |
| 2967 | int script_char; |
| 2968 | |
| 2969 | if (wait_time == -1L || wait_time > 100L) /* flush output before waiting */ |
| 2970 | { |
| 2971 | cursor_on(); |
| 2972 | out_flush(); |
| 2973 | #ifdef FEAT_GUI |
| 2974 | if (gui.in_use) |
| 2975 | { |
| 2976 | gui_update_cursor(FALSE, FALSE); |
| 2977 | # ifdef FEAT_MOUSESHAPE |
| 2978 | if (postponed_mouseshape) |
| 2979 | update_mouseshape(-1); |
| 2980 | # endif |
| 2981 | } |
| 2982 | #endif |
| 2983 | } |
| 2984 | |
| 2985 | /* |
| 2986 | * Don't reset these when at the hit-return prompt, otherwise a endless |
| 2987 | * recursive loop may result (write error in swapfile, hit-return, timeout |
| 2988 | * on char wait, flush swapfile, write error....). |
| 2989 | */ |
| 2990 | if (State != HITRETURN) |
| 2991 | { |
| 2992 | did_outofmem_msg = FALSE; /* display out of memory message (again) */ |
| 2993 | did_swapwrite_msg = FALSE; /* display swap file write error again */ |
| 2994 | } |
| 2995 | undo_off = FALSE; /* restart undo now */ |
| 2996 | |
| 2997 | /* |
Bram Moolenaar | ee3f7a5 | 2008-01-01 13:17:56 +0000 | [diff] [blame] | 2998 | * Get a character from a script file if there is one. |
| 2999 | * If interrupted: Stop reading script files, close them all. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3000 | */ |
| 3001 | script_char = -1; |
Bram Moolenaar | ee3f7a5 | 2008-01-01 13:17:56 +0000 | [diff] [blame] | 3002 | while (scriptin[curscript] != NULL && script_char < 0 |
| 3003 | #ifdef FEAT_EVAL |
| 3004 | && !ignore_script |
| 3005 | #endif |
| 3006 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3007 | { |
Bram Moolenaar | f233048 | 2008-06-24 20:19:36 +0000 | [diff] [blame] | 3008 | |
Bram Moolenaar | 93c88e0 | 2015-09-15 14:12:05 +0200 | [diff] [blame] | 3009 | #ifdef MESSAGE_QUEUE |
| 3010 | parse_queued_messages(); |
Bram Moolenaar | f233048 | 2008-06-24 20:19:36 +0000 | [diff] [blame] | 3011 | #endif |
| 3012 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3013 | if (got_int || (script_char = getc(scriptin[curscript])) < 0) |
| 3014 | { |
| 3015 | /* Reached EOF. |
| 3016 | * Careful: closescript() frees typebuf.tb_buf[] and buf[] may |
| 3017 | * point inside typebuf.tb_buf[]. Don't use buf[] after this! */ |
| 3018 | closescript(); |
| 3019 | /* |
| 3020 | * When reading script file is interrupted, return an ESC to get |
| 3021 | * back to normal mode. |
| 3022 | * Otherwise return -1, because typebuf.tb_buf[] has changed. |
| 3023 | */ |
| 3024 | if (got_int) |
| 3025 | retesc = TRUE; |
| 3026 | else |
| 3027 | return -1; |
| 3028 | } |
| 3029 | else |
| 3030 | { |
| 3031 | buf[0] = script_char; |
| 3032 | len = 1; |
| 3033 | } |
| 3034 | } |
| 3035 | |
| 3036 | if (script_char < 0) /* did not get a character from script */ |
| 3037 | { |
| 3038 | /* |
| 3039 | * If we got an interrupt, skip all previously typed characters and |
| 3040 | * return TRUE if quit reading script file. |
| 3041 | * Stop reading typeahead when a single CTRL-C was read, |
| 3042 | * fill_input_buf() returns this when not able to read from stdin. |
| 3043 | * Don't use buf[] here, closescript() may have freed typebuf.tb_buf[] |
| 3044 | * and buf may be pointing inside typebuf.tb_buf[]. |
| 3045 | */ |
| 3046 | if (got_int) |
| 3047 | { |
| 3048 | #define DUM_LEN MAXMAPLEN * 3 + 3 |
| 3049 | char_u dum[DUM_LEN + 1]; |
| 3050 | |
| 3051 | for (;;) |
| 3052 | { |
| 3053 | len = ui_inchar(dum, DUM_LEN, 0L, 0); |
| 3054 | if (len == 0 || (len == 1 && dum[0] == 3)) |
| 3055 | break; |
| 3056 | } |
| 3057 | return retesc; |
| 3058 | } |
| 3059 | |
| 3060 | /* |
| 3061 | * Always flush the output characters when getting input characters |
| 3062 | * from the user. |
| 3063 | */ |
| 3064 | out_flush(); |
| 3065 | |
| 3066 | /* |
| 3067 | * Fill up to a third of the buffer, because each character may be |
| 3068 | * tripled below. |
| 3069 | */ |
| 3070 | len = ui_inchar(buf, maxlen / 3, wait_time, tb_change_cnt); |
| 3071 | } |
| 3072 | |
| 3073 | if (typebuf_changed(tb_change_cnt)) |
| 3074 | return 0; |
| 3075 | |
| 3076 | return fix_input_buffer(buf, len, script_char >= 0); |
| 3077 | } |
| 3078 | |
| 3079 | /* |
| 3080 | * Fix typed characters for use by vgetc() and check_termcode(). |
| 3081 | * buf[] must have room to triple the number of bytes! |
| 3082 | * Returns the new length. |
| 3083 | */ |
| 3084 | int |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 3085 | fix_input_buffer( |
| 3086 | char_u *buf, |
| 3087 | int len, |
| 3088 | int script) /* TRUE when reading from a script */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3089 | { |
| 3090 | int i; |
| 3091 | char_u *p = buf; |
| 3092 | |
| 3093 | /* |
| 3094 | * Two characters are special: NUL and K_SPECIAL. |
| 3095 | * When compiled With the GUI CSI is also special. |
| 3096 | * Replace NUL by K_SPECIAL KS_ZERO KE_FILLER |
| 3097 | * Replace K_SPECIAL by K_SPECIAL KS_SPECIAL KE_FILLER |
| 3098 | * Replace CSI by K_SPECIAL KS_EXTRA KE_CSI |
| 3099 | * Don't replace K_SPECIAL when reading a script file. |
| 3100 | */ |
| 3101 | for (i = len; --i >= 0; ++p) |
| 3102 | { |
| 3103 | #ifdef FEAT_GUI |
| 3104 | /* When the GUI is used any character can come after a CSI, don't |
| 3105 | * escape it. */ |
| 3106 | if (gui.in_use && p[0] == CSI && i >= 2) |
| 3107 | { |
| 3108 | p += 2; |
| 3109 | i -= 2; |
| 3110 | } |
| 3111 | /* When the GUI is not used CSI needs to be escaped. */ |
| 3112 | else if (!gui.in_use && p[0] == CSI) |
| 3113 | { |
| 3114 | mch_memmove(p + 3, p + 1, (size_t)i); |
| 3115 | *p++ = K_SPECIAL; |
| 3116 | *p++ = KS_EXTRA; |
| 3117 | *p = (int)KE_CSI; |
| 3118 | len += 2; |
| 3119 | } |
| 3120 | else |
| 3121 | #endif |
| 3122 | if (p[0] == NUL || (p[0] == K_SPECIAL && !script |
Bram Moolenaar | 28a37ff | 2005-03-15 22:28:00 +0000 | [diff] [blame] | 3123 | #ifdef FEAT_AUTOCMD |
| 3124 | /* timeout may generate K_CURSORHOLD */ |
| 3125 | && (i < 2 || p[1] != KS_EXTRA || p[2] != (int)KE_CURSORHOLD) |
| 3126 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3127 | #if defined(WIN3264) && !defined(FEAT_GUI) |
| 3128 | /* Win32 console passes modifiers */ |
| 3129 | && (i < 2 || p[1] != KS_MODIFIER) |
| 3130 | #endif |
| 3131 | )) |
| 3132 | { |
| 3133 | mch_memmove(p + 3, p + 1, (size_t)i); |
| 3134 | p[2] = K_THIRD(p[0]); |
| 3135 | p[1] = K_SECOND(p[0]); |
| 3136 | p[0] = K_SPECIAL; |
| 3137 | p += 2; |
| 3138 | len += 2; |
| 3139 | } |
| 3140 | } |
| 3141 | *p = NUL; /* add trailing NUL */ |
| 3142 | return len; |
| 3143 | } |
| 3144 | |
| 3145 | #if defined(USE_INPUT_BUF) || defined(PROTO) |
| 3146 | /* |
| 3147 | * Return TRUE when bytes are in the input buffer or in the typeahead buffer. |
| 3148 | * 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] | 3149 | * or feedkeys() may insert characters in the typeahead buffer while we are |
Bram Moolenaar | 4a85b41 | 2006-04-23 22:40:29 +0000 | [diff] [blame] | 3150 | * waiting for input to arrive. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3151 | */ |
| 3152 | int |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 3153 | input_available(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3154 | { |
| 3155 | return (!vim_is_input_buf_empty() |
Bram Moolenaar | 4a85b41 | 2006-04-23 22:40:29 +0000 | [diff] [blame] | 3156 | # if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL) |
| 3157 | || typebuf_was_filled |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3158 | # endif |
| 3159 | ); |
| 3160 | } |
| 3161 | #endif |
| 3162 | |
| 3163 | /* |
| 3164 | * map[!] : show all key mappings |
| 3165 | * map[!] {lhs} : show key mapping for {lhs} |
| 3166 | * map[!] {lhs} {rhs} : set key mapping for {lhs} to {rhs} |
| 3167 | * noremap[!] {lhs} {rhs} : same, but no remapping for {rhs} |
| 3168 | * unmap[!] {lhs} : remove key mapping for {lhs} |
| 3169 | * abbr : show all abbreviations |
| 3170 | * abbr {lhs} : show abbreviations for {lhs} |
| 3171 | * abbr {lhs} {rhs} : set abbreviation for {lhs} to {rhs} |
| 3172 | * noreabbr {lhs} {rhs} : same, but no remapping for {rhs} |
| 3173 | * unabbr {lhs} : remove abbreviation for {lhs} |
| 3174 | * |
| 3175 | * maptype: 0 for :map, 1 for :unmap, 2 for noremap. |
| 3176 | * |
| 3177 | * arg is pointer to any arguments. Note: arg cannot be a read-only string, |
| 3178 | * it will be modified. |
| 3179 | * |
Bram Moolenaar | 371d540 | 2006-03-20 21:47:49 +0000 | [diff] [blame] | 3180 | * for :map mode is NORMAL + VISUAL + SELECTMODE + OP_PENDING |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3181 | * for :map! mode is INSERT + CMDLINE |
| 3182 | * for :cmap mode is CMDLINE |
| 3183 | * for :imap mode is INSERT |
| 3184 | * for :lmap mode is LANGMAP |
| 3185 | * for :nmap mode is NORMAL |
Bram Moolenaar | 371d540 | 2006-03-20 21:47:49 +0000 | [diff] [blame] | 3186 | * for :vmap mode is VISUAL + SELECTMODE |
| 3187 | * for :xmap mode is VISUAL |
| 3188 | * for :smap mode is SELECTMODE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3189 | * for :omap mode is OP_PENDING |
| 3190 | * |
| 3191 | * for :abbr mode is INSERT + CMDLINE |
| 3192 | * for :iabbr mode is INSERT |
| 3193 | * for :cabbr mode is CMDLINE |
| 3194 | * |
| 3195 | * Return 0 for success |
| 3196 | * 1 for invalid arguments |
| 3197 | * 2 for no match |
| 3198 | * 4 for out of mem |
| 3199 | * 5 for entry not unique |
| 3200 | */ |
| 3201 | int |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 3202 | do_map( |
| 3203 | int maptype, |
| 3204 | char_u *arg, |
| 3205 | int mode, |
| 3206 | int abbrev) /* not a mapping but an abbreviation */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3207 | { |
| 3208 | char_u *keys; |
| 3209 | mapblock_T *mp, **mpp; |
| 3210 | char_u *rhs; |
| 3211 | char_u *p; |
| 3212 | int n; |
| 3213 | int len = 0; /* init for GCC */ |
| 3214 | char_u *newstr; |
| 3215 | int hasarg; |
| 3216 | int haskey; |
| 3217 | int did_it = FALSE; |
| 3218 | #ifdef FEAT_LOCALMAP |
| 3219 | int did_local = FALSE; |
| 3220 | #endif |
| 3221 | int round; |
| 3222 | char_u *keys_buf = NULL; |
| 3223 | char_u *arg_buf = NULL; |
| 3224 | int retval = 0; |
| 3225 | int do_backslash; |
| 3226 | int hash; |
| 3227 | int new_hash; |
| 3228 | mapblock_T **abbr_table; |
| 3229 | mapblock_T **map_table; |
| 3230 | int unique = FALSE; |
Bram Moolenaar | 72179e1 | 2013-06-29 13:58:31 +0200 | [diff] [blame] | 3231 | int nowait = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3232 | int silent = FALSE; |
Bram Moolenaar | 9c10238 | 2006-05-03 21:26:49 +0000 | [diff] [blame] | 3233 | int special = FALSE; |
Bram Moolenaar | 4e42719 | 2006-03-10 21:34:27 +0000 | [diff] [blame] | 3234 | #ifdef FEAT_EVAL |
| 3235 | int expr = FALSE; |
| 3236 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3237 | int noremap; |
Bram Moolenaar | bd74325 | 2010-10-20 21:23:33 +0200 | [diff] [blame] | 3238 | char_u *orig_rhs; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3239 | |
| 3240 | keys = arg; |
| 3241 | map_table = maphash; |
| 3242 | abbr_table = &first_abbr; |
| 3243 | |
| 3244 | /* For ":noremap" don't remap, otherwise do remap. */ |
| 3245 | if (maptype == 2) |
| 3246 | noremap = REMAP_NONE; |
| 3247 | else |
| 3248 | noremap = REMAP_YES; |
| 3249 | |
Bram Moolenaar | 72179e1 | 2013-06-29 13:58:31 +0200 | [diff] [blame] | 3250 | /* Accept <buffer>, <nowait>, <silent>, <expr> <script> and <unique> in |
| 3251 | * any order. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3252 | for (;;) |
| 3253 | { |
| 3254 | #ifdef FEAT_LOCALMAP |
| 3255 | /* |
| 3256 | * Check for "<buffer>": mapping local to buffer. |
| 3257 | */ |
| 3258 | if (STRNCMP(keys, "<buffer>", 8) == 0) |
| 3259 | { |
| 3260 | keys = skipwhite(keys + 8); |
| 3261 | map_table = curbuf->b_maphash; |
| 3262 | abbr_table = &curbuf->b_first_abbr; |
| 3263 | continue; |
| 3264 | } |
| 3265 | #endif |
| 3266 | |
| 3267 | /* |
Bram Moolenaar | 72179e1 | 2013-06-29 13:58:31 +0200 | [diff] [blame] | 3268 | * Check for "<nowait>": don't wait for more characters. |
| 3269 | */ |
| 3270 | if (STRNCMP(keys, "<nowait>", 8) == 0) |
| 3271 | { |
| 3272 | keys = skipwhite(keys + 8); |
| 3273 | nowait = TRUE; |
| 3274 | continue; |
| 3275 | } |
| 3276 | |
| 3277 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3278 | * Check for "<silent>": don't echo commands. |
| 3279 | */ |
| 3280 | if (STRNCMP(keys, "<silent>", 8) == 0) |
| 3281 | { |
| 3282 | keys = skipwhite(keys + 8); |
| 3283 | silent = TRUE; |
| 3284 | continue; |
| 3285 | } |
| 3286 | |
Bram Moolenaar | 9c10238 | 2006-05-03 21:26:49 +0000 | [diff] [blame] | 3287 | /* |
| 3288 | * Check for "<special>": accept special keys in <> |
| 3289 | */ |
| 3290 | if (STRNCMP(keys, "<special>", 9) == 0) |
| 3291 | { |
| 3292 | keys = skipwhite(keys + 9); |
| 3293 | special = TRUE; |
| 3294 | continue; |
| 3295 | } |
| 3296 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3297 | #ifdef FEAT_EVAL |
| 3298 | /* |
| 3299 | * Check for "<script>": remap script-local mappings only |
| 3300 | */ |
| 3301 | if (STRNCMP(keys, "<script>", 8) == 0) |
| 3302 | { |
| 3303 | keys = skipwhite(keys + 8); |
| 3304 | noremap = REMAP_SCRIPT; |
| 3305 | continue; |
| 3306 | } |
Bram Moolenaar | 4e42719 | 2006-03-10 21:34:27 +0000 | [diff] [blame] | 3307 | |
| 3308 | /* |
| 3309 | * Check for "<expr>": {rhs} is an expression. |
| 3310 | */ |
| 3311 | if (STRNCMP(keys, "<expr>", 6) == 0) |
| 3312 | { |
| 3313 | keys = skipwhite(keys + 6); |
| 3314 | expr = TRUE; |
| 3315 | continue; |
| 3316 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3317 | #endif |
| 3318 | /* |
| 3319 | * Check for "<unique>": don't overwrite an existing mapping. |
| 3320 | */ |
| 3321 | if (STRNCMP(keys, "<unique>", 8) == 0) |
| 3322 | { |
| 3323 | keys = skipwhite(keys + 8); |
| 3324 | unique = TRUE; |
| 3325 | continue; |
| 3326 | } |
| 3327 | break; |
| 3328 | } |
| 3329 | |
| 3330 | validate_maphash(); |
| 3331 | |
| 3332 | /* |
Bram Moolenaar | f6f4a01 | 2011-08-17 17:18:20 +0200 | [diff] [blame] | 3333 | * Find end of keys and skip CTRL-Vs (and backslashes) in it. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3334 | * Accept backslash like CTRL-V when 'cpoptions' does not contain 'B'. |
Bram Moolenaar | f6f4a01 | 2011-08-17 17:18:20 +0200 | [diff] [blame] | 3335 | * with :unmap white space is included in the keys, no argument possible. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3336 | */ |
| 3337 | p = keys; |
| 3338 | do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL); |
| 3339 | while (*p && (maptype == 1 || !vim_iswhite(*p))) |
| 3340 | { |
| 3341 | if ((p[0] == Ctrl_V || (do_backslash && p[0] == '\\')) && |
| 3342 | p[1] != NUL) |
| 3343 | ++p; /* skip CTRL-V or backslash */ |
| 3344 | ++p; |
| 3345 | } |
| 3346 | if (*p != NUL) |
| 3347 | *p++ = NUL; |
Bram Moolenaar | bd74325 | 2010-10-20 21:23:33 +0200 | [diff] [blame] | 3348 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3349 | p = skipwhite(p); |
| 3350 | rhs = p; |
| 3351 | hasarg = (*rhs != NUL); |
| 3352 | haskey = (*keys != NUL); |
| 3353 | |
| 3354 | /* check for :unmap without argument */ |
| 3355 | if (maptype == 1 && !haskey) |
| 3356 | { |
| 3357 | retval = 1; |
| 3358 | goto theend; |
| 3359 | } |
| 3360 | |
| 3361 | /* |
| 3362 | * If mapping has been given as ^V<C_UP> say, then replace the term codes |
| 3363 | * with the appropriate two bytes. If it is a shifted special key, unshift |
| 3364 | * it too, giving another two bytes. |
| 3365 | * replace_termcodes() may move the result to allocated memory, which |
| 3366 | * needs to be freed later (*keys_buf and *arg_buf). |
| 3367 | * replace_termcodes() also removes CTRL-Vs and sometimes backslashes. |
| 3368 | */ |
| 3369 | if (haskey) |
Bram Moolenaar | 9c10238 | 2006-05-03 21:26:49 +0000 | [diff] [blame] | 3370 | keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, special); |
Bram Moolenaar | b3ae56c | 2010-10-27 17:39:05 +0200 | [diff] [blame] | 3371 | orig_rhs = rhs; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3372 | if (hasarg) |
| 3373 | { |
| 3374 | if (STRICMP(rhs, "<nop>") == 0) /* "<Nop>" means nothing */ |
| 3375 | rhs = (char_u *)""; |
| 3376 | else |
Bram Moolenaar | 9c10238 | 2006-05-03 21:26:49 +0000 | [diff] [blame] | 3377 | rhs = replace_termcodes(rhs, &arg_buf, FALSE, TRUE, special); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3378 | } |
| 3379 | |
| 3380 | #ifdef FEAT_FKMAP |
| 3381 | /* |
Bram Moolenaar | bd74325 | 2010-10-20 21:23:33 +0200 | [diff] [blame] | 3382 | * When in right-to-left mode and alternate keymap option set, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3383 | * reverse the character flow in the rhs in Farsi. |
| 3384 | */ |
| 3385 | if (p_altkeymap && curwin->w_p_rl) |
| 3386 | lrswap(rhs); |
| 3387 | #endif |
| 3388 | |
| 3389 | /* |
| 3390 | * check arguments and translate function keys |
| 3391 | */ |
| 3392 | if (haskey) |
| 3393 | { |
| 3394 | len = (int)STRLEN(keys); |
| 3395 | if (len > MAXMAPLEN) /* maximum length of MAXMAPLEN chars */ |
| 3396 | { |
| 3397 | retval = 1; |
| 3398 | goto theend; |
| 3399 | } |
| 3400 | |
| 3401 | if (abbrev && maptype != 1) |
| 3402 | { |
| 3403 | /* |
| 3404 | * If an abbreviation ends in a keyword character, the |
| 3405 | * rest must be all keyword-char or all non-keyword-char. |
| 3406 | * Otherwise we won't be able to find the start of it in a |
| 3407 | * vi-compatible way. |
| 3408 | */ |
| 3409 | #ifdef FEAT_MBYTE |
| 3410 | if (has_mbyte) |
| 3411 | { |
| 3412 | int first, last; |
| 3413 | int same = -1; |
| 3414 | |
| 3415 | first = vim_iswordp(keys); |
| 3416 | last = first; |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 3417 | p = keys + (*mb_ptr2len)(keys); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3418 | n = 1; |
| 3419 | while (p < keys + len) |
| 3420 | { |
| 3421 | ++n; /* nr of (multi-byte) chars */ |
| 3422 | last = vim_iswordp(p); /* type of last char */ |
| 3423 | if (same == -1 && last != first) |
| 3424 | same = n - 1; /* count of same char type */ |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 3425 | p += (*mb_ptr2len)(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3426 | } |
| 3427 | if (last && n > 2 && same >= 0 && same < n - 1) |
| 3428 | { |
| 3429 | retval = 1; |
| 3430 | goto theend; |
| 3431 | } |
| 3432 | } |
| 3433 | else |
| 3434 | #endif |
| 3435 | if (vim_iswordc(keys[len - 1])) /* ends in keyword char */ |
| 3436 | for (n = 0; n < len - 2; ++n) |
| 3437 | if (vim_iswordc(keys[n]) != vim_iswordc(keys[len - 2])) |
| 3438 | { |
| 3439 | retval = 1; |
| 3440 | goto theend; |
| 3441 | } |
Bram Moolenaar | f6f95d9 | 2009-11-11 15:23:37 +0000 | [diff] [blame] | 3442 | /* An abbreviation cannot contain white space. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3443 | for (n = 0; n < len; ++n) |
| 3444 | if (vim_iswhite(keys[n])) |
| 3445 | { |
| 3446 | retval = 1; |
| 3447 | goto theend; |
| 3448 | } |
| 3449 | } |
| 3450 | } |
| 3451 | |
| 3452 | if (haskey && hasarg && abbrev) /* if we will add an abbreviation */ |
| 3453 | no_abbr = FALSE; /* reset flag that indicates there are |
| 3454 | no abbreviations */ |
| 3455 | |
| 3456 | if (!haskey || (maptype != 1 && !hasarg)) |
| 3457 | msg_start(); |
| 3458 | |
| 3459 | #ifdef FEAT_LOCALMAP |
| 3460 | /* |
| 3461 | * Check if a new local mapping wasn't already defined globally. |
| 3462 | */ |
| 3463 | if (map_table == curbuf->b_maphash && haskey && hasarg && maptype != 1) |
| 3464 | { |
| 3465 | /* need to loop over all global hash lists */ |
| 3466 | for (hash = 0; hash < 256 && !got_int; ++hash) |
| 3467 | { |
| 3468 | if (abbrev) |
| 3469 | { |
| 3470 | if (hash != 0) /* there is only one abbreviation list */ |
| 3471 | break; |
| 3472 | mp = first_abbr; |
| 3473 | } |
| 3474 | else |
| 3475 | mp = maphash[hash]; |
| 3476 | for ( ; mp != NULL && !got_int; mp = mp->m_next) |
| 3477 | { |
| 3478 | /* check entries with the same mode */ |
| 3479 | if ((mp->m_mode & mode) != 0 |
| 3480 | && mp->m_keylen == len |
| 3481 | && unique |
| 3482 | && STRNCMP(mp->m_keys, keys, (size_t)len) == 0) |
| 3483 | { |
| 3484 | if (abbrev) |
| 3485 | EMSG2(_("E224: global abbreviation already exists for %s"), |
| 3486 | mp->m_keys); |
| 3487 | else |
| 3488 | EMSG2(_("E225: global mapping already exists for %s"), |
| 3489 | mp->m_keys); |
| 3490 | retval = 5; |
| 3491 | goto theend; |
| 3492 | } |
| 3493 | } |
| 3494 | } |
| 3495 | } |
| 3496 | |
| 3497 | /* |
| 3498 | * When listing global mappings, also list buffer-local ones here. |
| 3499 | */ |
| 3500 | if (map_table != curbuf->b_maphash && !hasarg && maptype != 1) |
| 3501 | { |
| 3502 | /* need to loop over all global hash lists */ |
| 3503 | for (hash = 0; hash < 256 && !got_int; ++hash) |
| 3504 | { |
| 3505 | if (abbrev) |
| 3506 | { |
| 3507 | if (hash != 0) /* there is only one abbreviation list */ |
| 3508 | break; |
| 3509 | mp = curbuf->b_first_abbr; |
| 3510 | } |
| 3511 | else |
| 3512 | mp = curbuf->b_maphash[hash]; |
| 3513 | for ( ; mp != NULL && !got_int; mp = mp->m_next) |
| 3514 | { |
| 3515 | /* check entries with the same mode */ |
| 3516 | if ((mp->m_mode & mode) != 0) |
| 3517 | { |
| 3518 | if (!haskey) /* show all entries */ |
| 3519 | { |
| 3520 | showmap(mp, TRUE); |
| 3521 | did_local = TRUE; |
| 3522 | } |
| 3523 | else |
| 3524 | { |
| 3525 | n = mp->m_keylen; |
| 3526 | if (STRNCMP(mp->m_keys, keys, |
| 3527 | (size_t)(n < len ? n : len)) == 0) |
| 3528 | { |
| 3529 | showmap(mp, TRUE); |
| 3530 | did_local = TRUE; |
| 3531 | } |
| 3532 | } |
| 3533 | } |
| 3534 | } |
| 3535 | } |
| 3536 | } |
| 3537 | #endif |
| 3538 | |
| 3539 | /* |
| 3540 | * Find an entry in the maphash[] list that matches. |
| 3541 | * For :unmap we may loop two times: once to try to unmap an entry with a |
| 3542 | * matching 'from' part, a second time, if the first fails, to unmap an |
| 3543 | * entry with a matching 'to' part. This was done to allow ":ab foo bar" |
| 3544 | * to be unmapped by typing ":unab foo", where "foo" will be replaced by |
| 3545 | * "bar" because of the abbreviation. |
| 3546 | */ |
| 3547 | for (round = 0; (round == 0 || maptype == 1) && round <= 1 |
| 3548 | && !did_it && !got_int; ++round) |
| 3549 | { |
| 3550 | /* need to loop over all hash lists */ |
| 3551 | for (hash = 0; hash < 256 && !got_int; ++hash) |
| 3552 | { |
| 3553 | if (abbrev) |
| 3554 | { |
Bram Moolenaar | be4d506 | 2006-03-18 21:30:13 +0000 | [diff] [blame] | 3555 | if (hash > 0) /* there is only one abbreviation list */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3556 | break; |
| 3557 | mpp = abbr_table; |
| 3558 | } |
| 3559 | else |
| 3560 | mpp = &(map_table[hash]); |
| 3561 | for (mp = *mpp; mp != NULL && !got_int; mp = *mpp) |
| 3562 | { |
| 3563 | |
| 3564 | if (!(mp->m_mode & mode)) /* skip entries with wrong mode */ |
| 3565 | { |
| 3566 | mpp = &(mp->m_next); |
| 3567 | continue; |
| 3568 | } |
| 3569 | if (!haskey) /* show all entries */ |
| 3570 | { |
| 3571 | showmap(mp, map_table != maphash); |
| 3572 | did_it = TRUE; |
| 3573 | } |
| 3574 | else /* do we have a match? */ |
| 3575 | { |
| 3576 | if (round) /* second round: Try unmap "rhs" string */ |
| 3577 | { |
| 3578 | n = (int)STRLEN(mp->m_str); |
| 3579 | p = mp->m_str; |
| 3580 | } |
| 3581 | else |
| 3582 | { |
| 3583 | n = mp->m_keylen; |
| 3584 | p = mp->m_keys; |
| 3585 | } |
| 3586 | if (STRNCMP(p, keys, (size_t)(n < len ? n : len)) == 0) |
| 3587 | { |
| 3588 | if (maptype == 1) /* delete entry */ |
| 3589 | { |
| 3590 | /* Only accept a full match. For abbreviations we |
| 3591 | * ignore trailing space when matching with the |
| 3592 | * "lhs", since an abbreviation can't have |
| 3593 | * trailing space. */ |
| 3594 | if (n != len && (!abbrev || round || n > len |
| 3595 | || *skipwhite(keys + n) != NUL)) |
| 3596 | { |
| 3597 | mpp = &(mp->m_next); |
| 3598 | continue; |
| 3599 | } |
| 3600 | /* |
| 3601 | * We reset the indicated mode bits. If nothing is |
| 3602 | * left the entry is deleted below. |
| 3603 | */ |
| 3604 | mp->m_mode &= ~mode; |
| 3605 | did_it = TRUE; /* remember we did something */ |
| 3606 | } |
| 3607 | else if (!hasarg) /* show matching entry */ |
| 3608 | { |
| 3609 | showmap(mp, map_table != maphash); |
| 3610 | did_it = TRUE; |
| 3611 | } |
Bram Moolenaar | b6799ac | 2007-05-10 16:44:05 +0000 | [diff] [blame] | 3612 | else if (n != len) /* new entry is ambiguous */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3613 | { |
| 3614 | mpp = &(mp->m_next); |
| 3615 | continue; |
| 3616 | } |
| 3617 | else if (unique) |
| 3618 | { |
| 3619 | if (abbrev) |
| 3620 | EMSG2(_("E226: abbreviation already exists for %s"), |
| 3621 | p); |
| 3622 | else |
| 3623 | EMSG2(_("E227: mapping already exists for %s"), p); |
| 3624 | retval = 5; |
| 3625 | goto theend; |
| 3626 | } |
| 3627 | else /* new rhs for existing entry */ |
| 3628 | { |
| 3629 | mp->m_mode &= ~mode; /* remove mode bits */ |
| 3630 | if (mp->m_mode == 0 && !did_it) /* reuse entry */ |
| 3631 | { |
| 3632 | newstr = vim_strsave(rhs); |
| 3633 | if (newstr == NULL) |
| 3634 | { |
| 3635 | retval = 4; /* no mem */ |
| 3636 | goto theend; |
| 3637 | } |
| 3638 | vim_free(mp->m_str); |
| 3639 | mp->m_str = newstr; |
Bram Moolenaar | bd74325 | 2010-10-20 21:23:33 +0200 | [diff] [blame] | 3640 | vim_free(mp->m_orig_str); |
| 3641 | mp->m_orig_str = vim_strsave(orig_rhs); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3642 | mp->m_noremap = noremap; |
Bram Moolenaar | 72179e1 | 2013-06-29 13:58:31 +0200 | [diff] [blame] | 3643 | mp->m_nowait = nowait; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3644 | mp->m_silent = silent; |
| 3645 | mp->m_mode = mode; |
Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 3646 | #ifdef FEAT_EVAL |
Bram Moolenaar | 4e42719 | 2006-03-10 21:34:27 +0000 | [diff] [blame] | 3647 | mp->m_expr = expr; |
Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 3648 | mp->m_script_ID = current_SID; |
| 3649 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3650 | did_it = TRUE; |
| 3651 | } |
| 3652 | } |
| 3653 | if (mp->m_mode == 0) /* entry can be deleted */ |
| 3654 | { |
| 3655 | map_free(mpp); |
| 3656 | continue; /* continue with *mpp */ |
| 3657 | } |
| 3658 | |
| 3659 | /* |
| 3660 | * May need to put this entry into another hash list. |
| 3661 | */ |
| 3662 | new_hash = MAP_HASH(mp->m_mode, mp->m_keys[0]); |
| 3663 | if (!abbrev && new_hash != hash) |
| 3664 | { |
| 3665 | *mpp = mp->m_next; |
| 3666 | mp->m_next = map_table[new_hash]; |
| 3667 | map_table[new_hash] = mp; |
| 3668 | |
| 3669 | continue; /* continue with *mpp */ |
| 3670 | } |
| 3671 | } |
| 3672 | } |
| 3673 | mpp = &(mp->m_next); |
| 3674 | } |
| 3675 | } |
| 3676 | } |
| 3677 | |
| 3678 | if (maptype == 1) /* delete entry */ |
| 3679 | { |
| 3680 | if (!did_it) |
| 3681 | retval = 2; /* no match */ |
Bram Moolenaar | 9a95bdc | 2014-10-09 13:36:16 +0200 | [diff] [blame] | 3682 | else if (*keys == Ctrl_C) |
Bram Moolenaar | 651863c | 2015-01-14 12:44:41 +0100 | [diff] [blame] | 3683 | { |
Bram Moolenaar | 9a95bdc | 2014-10-09 13:36:16 +0200 | [diff] [blame] | 3684 | /* If CTRL-C has been unmapped, reuse it for Interrupting. */ |
Bram Moolenaar | 4357973 | 2015-01-14 14:08:44 +0100 | [diff] [blame] | 3685 | #ifdef FEAT_LOCALMAP |
Bram Moolenaar | 651863c | 2015-01-14 12:44:41 +0100 | [diff] [blame] | 3686 | if (map_table == curbuf->b_maphash) |
| 3687 | curbuf->b_mapped_ctrl_c &= ~mode; |
| 3688 | else |
Bram Moolenaar | 4357973 | 2015-01-14 14:08:44 +0100 | [diff] [blame] | 3689 | #endif |
Bram Moolenaar | 651863c | 2015-01-14 12:44:41 +0100 | [diff] [blame] | 3690 | mapped_ctrl_c &= ~mode; |
| 3691 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3692 | goto theend; |
| 3693 | } |
| 3694 | |
| 3695 | if (!haskey || !hasarg) /* print entries */ |
| 3696 | { |
| 3697 | if (!did_it |
| 3698 | #ifdef FEAT_LOCALMAP |
| 3699 | && !did_local |
| 3700 | #endif |
| 3701 | ) |
| 3702 | { |
| 3703 | if (abbrev) |
| 3704 | MSG(_("No abbreviation found")); |
| 3705 | else |
| 3706 | MSG(_("No mapping found")); |
| 3707 | } |
| 3708 | goto theend; /* listing finished */ |
| 3709 | } |
| 3710 | |
| 3711 | if (did_it) /* have added the new entry already */ |
| 3712 | goto theend; |
| 3713 | |
| 3714 | /* |
| 3715 | * Get here when adding a new entry to the maphash[] list or abbrlist. |
| 3716 | */ |
| 3717 | mp = (mapblock_T *)alloc((unsigned)sizeof(mapblock_T)); |
| 3718 | if (mp == NULL) |
| 3719 | { |
| 3720 | retval = 4; /* no mem */ |
| 3721 | goto theend; |
| 3722 | } |
| 3723 | |
Bram Moolenaar | 9a95bdc | 2014-10-09 13:36:16 +0200 | [diff] [blame] | 3724 | /* If CTRL-C has been mapped, don't always use it for Interrupting. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3725 | if (*keys == Ctrl_C) |
Bram Moolenaar | 651863c | 2015-01-14 12:44:41 +0100 | [diff] [blame] | 3726 | { |
Bram Moolenaar | 4357973 | 2015-01-14 14:08:44 +0100 | [diff] [blame] | 3727 | #ifdef FEAT_LOCALMAP |
Bram Moolenaar | 651863c | 2015-01-14 12:44:41 +0100 | [diff] [blame] | 3728 | if (map_table == curbuf->b_maphash) |
| 3729 | curbuf->b_mapped_ctrl_c |= mode; |
| 3730 | else |
Bram Moolenaar | 4357973 | 2015-01-14 14:08:44 +0100 | [diff] [blame] | 3731 | #endif |
Bram Moolenaar | 651863c | 2015-01-14 12:44:41 +0100 | [diff] [blame] | 3732 | mapped_ctrl_c |= mode; |
| 3733 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3734 | |
| 3735 | mp->m_keys = vim_strsave(keys); |
| 3736 | mp->m_str = vim_strsave(rhs); |
Bram Moolenaar | bd74325 | 2010-10-20 21:23:33 +0200 | [diff] [blame] | 3737 | mp->m_orig_str = vim_strsave(orig_rhs); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3738 | if (mp->m_keys == NULL || mp->m_str == NULL) |
| 3739 | { |
| 3740 | vim_free(mp->m_keys); |
| 3741 | vim_free(mp->m_str); |
Bram Moolenaar | bd74325 | 2010-10-20 21:23:33 +0200 | [diff] [blame] | 3742 | vim_free(mp->m_orig_str); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3743 | vim_free(mp); |
| 3744 | retval = 4; /* no mem */ |
| 3745 | goto theend; |
| 3746 | } |
| 3747 | mp->m_keylen = (int)STRLEN(mp->m_keys); |
| 3748 | mp->m_noremap = noremap; |
Bram Moolenaar | 72179e1 | 2013-06-29 13:58:31 +0200 | [diff] [blame] | 3749 | mp->m_nowait = nowait; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3750 | mp->m_silent = silent; |
| 3751 | mp->m_mode = mode; |
Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 3752 | #ifdef FEAT_EVAL |
Bram Moolenaar | 4e42719 | 2006-03-10 21:34:27 +0000 | [diff] [blame] | 3753 | mp->m_expr = expr; |
Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 3754 | mp->m_script_ID = current_SID; |
| 3755 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3756 | |
| 3757 | /* add the new entry in front of the abbrlist or maphash[] list */ |
| 3758 | if (abbrev) |
| 3759 | { |
| 3760 | mp->m_next = *abbr_table; |
| 3761 | *abbr_table = mp; |
| 3762 | } |
| 3763 | else |
| 3764 | { |
| 3765 | n = MAP_HASH(mp->m_mode, mp->m_keys[0]); |
| 3766 | mp->m_next = map_table[n]; |
| 3767 | map_table[n] = mp; |
| 3768 | } |
| 3769 | |
| 3770 | theend: |
| 3771 | vim_free(keys_buf); |
| 3772 | vim_free(arg_buf); |
| 3773 | return retval; |
| 3774 | } |
| 3775 | |
| 3776 | /* |
| 3777 | * Delete one entry from the abbrlist or maphash[]. |
| 3778 | * "mpp" is a pointer to the m_next field of the PREVIOUS entry! |
| 3779 | */ |
| 3780 | static void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 3781 | map_free(mapblock_T **mpp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3782 | { |
| 3783 | mapblock_T *mp; |
| 3784 | |
| 3785 | mp = *mpp; |
| 3786 | vim_free(mp->m_keys); |
| 3787 | vim_free(mp->m_str); |
Bram Moolenaar | bd74325 | 2010-10-20 21:23:33 +0200 | [diff] [blame] | 3788 | vim_free(mp->m_orig_str); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3789 | *mpp = mp->m_next; |
| 3790 | vim_free(mp); |
| 3791 | } |
| 3792 | |
| 3793 | /* |
| 3794 | * Initialize maphash[] for first use. |
| 3795 | */ |
| 3796 | static void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 3797 | validate_maphash(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3798 | { |
| 3799 | if (!maphash_valid) |
| 3800 | { |
| 3801 | vim_memset(maphash, 0, sizeof(maphash)); |
| 3802 | maphash_valid = TRUE; |
| 3803 | } |
| 3804 | } |
| 3805 | |
| 3806 | /* |
| 3807 | * Get the mapping mode from the command name. |
| 3808 | */ |
| 3809 | int |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 3810 | get_map_mode(char_u **cmdp, int forceit) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3811 | { |
| 3812 | char_u *p; |
| 3813 | int modec; |
| 3814 | int mode; |
| 3815 | |
| 3816 | p = *cmdp; |
| 3817 | modec = *p++; |
| 3818 | if (modec == 'i') |
| 3819 | mode = INSERT; /* :imap */ |
| 3820 | else if (modec == 'l') |
| 3821 | mode = LANGMAP; /* :lmap */ |
| 3822 | else if (modec == 'c') |
| 3823 | mode = CMDLINE; /* :cmap */ |
| 3824 | else if (modec == 'n' && *p != 'o') /* avoid :noremap */ |
| 3825 | mode = NORMAL; /* :nmap */ |
| 3826 | else if (modec == 'v') |
Bram Moolenaar | 371d540 | 2006-03-20 21:47:49 +0000 | [diff] [blame] | 3827 | mode = VISUAL + SELECTMODE; /* :vmap */ |
| 3828 | else if (modec == 'x') |
| 3829 | mode = VISUAL; /* :xmap */ |
| 3830 | else if (modec == 's') |
| 3831 | mode = SELECTMODE; /* :smap */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3832 | else if (modec == 'o') |
| 3833 | mode = OP_PENDING; /* :omap */ |
| 3834 | else |
| 3835 | { |
| 3836 | --p; |
| 3837 | if (forceit) |
| 3838 | mode = INSERT + CMDLINE; /* :map ! */ |
| 3839 | else |
Bram Moolenaar | 371d540 | 2006-03-20 21:47:49 +0000 | [diff] [blame] | 3840 | mode = VISUAL + SELECTMODE + NORMAL + OP_PENDING;/* :map */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3841 | } |
| 3842 | |
| 3843 | *cmdp = p; |
| 3844 | return mode; |
| 3845 | } |
| 3846 | |
| 3847 | /* |
| 3848 | * Clear all mappings or abbreviations. |
| 3849 | * 'abbr' should be FALSE for mappings, TRUE for abbreviations. |
| 3850 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3851 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 3852 | map_clear( |
| 3853 | char_u *cmdp, |
| 3854 | char_u *arg UNUSED, |
| 3855 | int forceit, |
| 3856 | int abbr) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3857 | { |
| 3858 | int mode; |
| 3859 | #ifdef FEAT_LOCALMAP |
| 3860 | int local; |
| 3861 | |
| 3862 | local = (STRCMP(arg, "<buffer>") == 0); |
| 3863 | if (!local && *arg != NUL) |
| 3864 | { |
| 3865 | EMSG(_(e_invarg)); |
| 3866 | return; |
| 3867 | } |
| 3868 | #endif |
| 3869 | |
| 3870 | mode = get_map_mode(&cmdp, forceit); |
| 3871 | map_clear_int(curbuf, mode, |
| 3872 | #ifdef FEAT_LOCALMAP |
| 3873 | local, |
| 3874 | #else |
| 3875 | FALSE, |
| 3876 | #endif |
| 3877 | abbr); |
| 3878 | } |
| 3879 | |
| 3880 | /* |
| 3881 | * Clear all mappings in "mode". |
| 3882 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3883 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 3884 | map_clear_int( |
| 3885 | buf_T *buf UNUSED, /* buffer for local mappings */ |
| 3886 | int mode, /* mode in which to delete */ |
| 3887 | int local UNUSED, /* TRUE for buffer-local mappings */ |
| 3888 | int abbr) /* TRUE for abbreviations */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3889 | { |
| 3890 | mapblock_T *mp, **mpp; |
| 3891 | int hash; |
| 3892 | int new_hash; |
| 3893 | |
| 3894 | validate_maphash(); |
| 3895 | |
| 3896 | for (hash = 0; hash < 256; ++hash) |
| 3897 | { |
| 3898 | if (abbr) |
| 3899 | { |
Bram Moolenaar | be4d506 | 2006-03-18 21:30:13 +0000 | [diff] [blame] | 3900 | if (hash > 0) /* there is only one abbrlist */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3901 | break; |
| 3902 | #ifdef FEAT_LOCALMAP |
| 3903 | if (local) |
| 3904 | mpp = &buf->b_first_abbr; |
| 3905 | else |
| 3906 | #endif |
| 3907 | mpp = &first_abbr; |
| 3908 | } |
| 3909 | else |
| 3910 | { |
| 3911 | #ifdef FEAT_LOCALMAP |
| 3912 | if (local) |
| 3913 | mpp = &buf->b_maphash[hash]; |
| 3914 | else |
| 3915 | #endif |
| 3916 | mpp = &maphash[hash]; |
| 3917 | } |
| 3918 | while (*mpp != NULL) |
| 3919 | { |
| 3920 | mp = *mpp; |
| 3921 | if (mp->m_mode & mode) |
| 3922 | { |
| 3923 | mp->m_mode &= ~mode; |
| 3924 | if (mp->m_mode == 0) /* entry can be deleted */ |
| 3925 | { |
| 3926 | map_free(mpp); |
| 3927 | continue; |
| 3928 | } |
| 3929 | /* |
| 3930 | * May need to put this entry into another hash list. |
| 3931 | */ |
| 3932 | new_hash = MAP_HASH(mp->m_mode, mp->m_keys[0]); |
| 3933 | if (!abbr && new_hash != hash) |
| 3934 | { |
| 3935 | *mpp = mp->m_next; |
| 3936 | #ifdef FEAT_LOCALMAP |
| 3937 | if (local) |
| 3938 | { |
| 3939 | mp->m_next = buf->b_maphash[new_hash]; |
| 3940 | buf->b_maphash[new_hash] = mp; |
| 3941 | } |
| 3942 | else |
| 3943 | #endif |
| 3944 | { |
| 3945 | mp->m_next = maphash[new_hash]; |
| 3946 | maphash[new_hash] = mp; |
| 3947 | } |
| 3948 | continue; /* continue with *mpp */ |
| 3949 | } |
| 3950 | } |
| 3951 | mpp = &(mp->m_next); |
| 3952 | } |
| 3953 | } |
| 3954 | } |
| 3955 | |
Bram Moolenaar | bd74325 | 2010-10-20 21:23:33 +0200 | [diff] [blame] | 3956 | /* |
| 3957 | * Return characters to represent the map mode in an allocated string. |
| 3958 | * Returns NULL when out of memory. |
| 3959 | */ |
| 3960 | char_u * |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 3961 | map_mode_to_chars(int mode) |
Bram Moolenaar | bd74325 | 2010-10-20 21:23:33 +0200 | [diff] [blame] | 3962 | { |
| 3963 | garray_T mapmode; |
| 3964 | |
| 3965 | ga_init2(&mapmode, 1, 7); |
| 3966 | |
| 3967 | if ((mode & (INSERT + CMDLINE)) == INSERT + CMDLINE) |
| 3968 | ga_append(&mapmode, '!'); /* :map! */ |
| 3969 | else if (mode & INSERT) |
| 3970 | ga_append(&mapmode, 'i'); /* :imap */ |
| 3971 | else if (mode & LANGMAP) |
| 3972 | ga_append(&mapmode, 'l'); /* :lmap */ |
| 3973 | else if (mode & CMDLINE) |
| 3974 | ga_append(&mapmode, 'c'); /* :cmap */ |
| 3975 | else if ((mode & (NORMAL + VISUAL + SELECTMODE + OP_PENDING)) |
| 3976 | == NORMAL + VISUAL + SELECTMODE + OP_PENDING) |
| 3977 | ga_append(&mapmode, ' '); /* :map */ |
| 3978 | else |
| 3979 | { |
| 3980 | if (mode & NORMAL) |
| 3981 | ga_append(&mapmode, 'n'); /* :nmap */ |
| 3982 | if (mode & OP_PENDING) |
| 3983 | ga_append(&mapmode, 'o'); /* :omap */ |
| 3984 | if ((mode & (VISUAL + SELECTMODE)) == VISUAL + SELECTMODE) |
| 3985 | ga_append(&mapmode, 'v'); /* :vmap */ |
| 3986 | else |
| 3987 | { |
| 3988 | if (mode & VISUAL) |
| 3989 | ga_append(&mapmode, 'x'); /* :xmap */ |
| 3990 | if (mode & SELECTMODE) |
| 3991 | ga_append(&mapmode, 's'); /* :smap */ |
| 3992 | } |
| 3993 | } |
| 3994 | |
| 3995 | ga_append(&mapmode, NUL); |
| 3996 | return (char_u *)mapmode.ga_data; |
| 3997 | } |
| 3998 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3999 | static void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 4000 | showmap( |
| 4001 | mapblock_T *mp, |
| 4002 | int local) /* TRUE for buffer-local map */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4003 | { |
Bram Moolenaar | bd74325 | 2010-10-20 21:23:33 +0200 | [diff] [blame] | 4004 | int len = 1; |
| 4005 | char_u *mapchars; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4006 | |
| 4007 | if (msg_didout || msg_silent != 0) |
Bram Moolenaar | fa47a92 | 2009-02-22 22:43:27 +0000 | [diff] [blame] | 4008 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4009 | msg_putchar('\n'); |
Bram Moolenaar | fa47a92 | 2009-02-22 22:43:27 +0000 | [diff] [blame] | 4010 | if (got_int) /* 'q' typed at MORE prompt */ |
| 4011 | return; |
| 4012 | } |
Bram Moolenaar | bd74325 | 2010-10-20 21:23:33 +0200 | [diff] [blame] | 4013 | |
| 4014 | mapchars = map_mode_to_chars(mp->m_mode); |
| 4015 | if (mapchars != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4016 | { |
Bram Moolenaar | bd74325 | 2010-10-20 21:23:33 +0200 | [diff] [blame] | 4017 | msg_puts(mapchars); |
Bram Moolenaar | 6b5ef06 | 2010-10-27 12:18:00 +0200 | [diff] [blame] | 4018 | len = (int)STRLEN(mapchars); |
Bram Moolenaar | bd74325 | 2010-10-20 21:23:33 +0200 | [diff] [blame] | 4019 | vim_free(mapchars); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4020 | } |
Bram Moolenaar | bd74325 | 2010-10-20 21:23:33 +0200 | [diff] [blame] | 4021 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4022 | while (++len <= 3) |
| 4023 | msg_putchar(' '); |
| 4024 | |
Bram Moolenaar | f233048 | 2008-06-24 20:19:36 +0000 | [diff] [blame] | 4025 | /* Display the LHS. Get length of what we write. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4026 | len = msg_outtrans_special(mp->m_keys, TRUE); |
| 4027 | do |
| 4028 | { |
| 4029 | msg_putchar(' '); /* padd with blanks */ |
| 4030 | ++len; |
| 4031 | } while (len < 12); |
| 4032 | |
| 4033 | if (mp->m_noremap == REMAP_NONE) |
| 4034 | msg_puts_attr((char_u *)"*", hl_attr(HLF_8)); |
| 4035 | else if (mp->m_noremap == REMAP_SCRIPT) |
| 4036 | msg_puts_attr((char_u *)"&", hl_attr(HLF_8)); |
| 4037 | else |
| 4038 | msg_putchar(' '); |
| 4039 | |
| 4040 | if (local) |
| 4041 | msg_putchar('@'); |
| 4042 | else |
| 4043 | msg_putchar(' '); |
| 4044 | |
| 4045 | /* Use FALSE below if we only want things like <Up> to show up as such on |
Bram Moolenaar | bd74325 | 2010-10-20 21:23:33 +0200 | [diff] [blame] | 4046 | * the rhs, and not M-x etc, TRUE gets both -- webb */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4047 | if (*mp->m_str == NUL) |
| 4048 | msg_puts_attr((char_u *)"<Nop>", hl_attr(HLF_8)); |
| 4049 | else |
Bram Moolenaar | b8bf541 | 2011-08-17 20:33:22 +0200 | [diff] [blame] | 4050 | { |
| 4051 | /* Remove escaping of CSI, because "m_str" is in a format to be used |
| 4052 | * as typeahead. */ |
| 4053 | char_u *s = vim_strsave(mp->m_str); |
| 4054 | if (s != NULL) |
| 4055 | { |
| 4056 | vim_unescape_csi(s); |
| 4057 | msg_outtrans_special(s, FALSE); |
| 4058 | vim_free(s); |
| 4059 | } |
| 4060 | } |
Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 4061 | #ifdef FEAT_EVAL |
| 4062 | if (p_verbose > 0) |
| 4063 | last_set_msg(mp->m_script_ID); |
| 4064 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4065 | out_flush(); /* show one line at a time */ |
| 4066 | } |
| 4067 | |
| 4068 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 4069 | /* |
| 4070 | * Return TRUE if a map exists that has "str" in the rhs for mode "modechars". |
| 4071 | * Recognize termcap codes in "str". |
| 4072 | * Also checks mappings local to the current buffer. |
| 4073 | */ |
| 4074 | int |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 4075 | map_to_exists(char_u *str, char_u *modechars, int abbr) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4076 | { |
| 4077 | int mode = 0; |
| 4078 | char_u *rhs; |
| 4079 | char_u *buf; |
| 4080 | int retval; |
| 4081 | |
Bram Moolenaar | 9c10238 | 2006-05-03 21:26:49 +0000 | [diff] [blame] | 4082 | rhs = replace_termcodes(str, &buf, FALSE, TRUE, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4083 | |
| 4084 | if (vim_strchr(modechars, 'n') != NULL) |
| 4085 | mode |= NORMAL; |
| 4086 | if (vim_strchr(modechars, 'v') != NULL) |
Bram Moolenaar | 371d540 | 2006-03-20 21:47:49 +0000 | [diff] [blame] | 4087 | mode |= VISUAL + SELECTMODE; |
| 4088 | if (vim_strchr(modechars, 'x') != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4089 | mode |= VISUAL; |
Bram Moolenaar | 371d540 | 2006-03-20 21:47:49 +0000 | [diff] [blame] | 4090 | if (vim_strchr(modechars, 's') != NULL) |
| 4091 | mode |= SELECTMODE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4092 | if (vim_strchr(modechars, 'o') != NULL) |
| 4093 | mode |= OP_PENDING; |
| 4094 | if (vim_strchr(modechars, 'i') != NULL) |
| 4095 | mode |= INSERT; |
| 4096 | if (vim_strchr(modechars, 'l') != NULL) |
| 4097 | mode |= LANGMAP; |
| 4098 | if (vim_strchr(modechars, 'c') != NULL) |
| 4099 | mode |= CMDLINE; |
| 4100 | |
Bram Moolenaar | be4d506 | 2006-03-18 21:30:13 +0000 | [diff] [blame] | 4101 | retval = map_to_exists_mode(rhs, mode, abbr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4102 | vim_free(buf); |
| 4103 | |
| 4104 | return retval; |
| 4105 | } |
| 4106 | #endif |
| 4107 | |
| 4108 | /* |
| 4109 | * Return TRUE if a map exists that has "str" in the rhs for mode "mode". |
| 4110 | * Also checks mappings local to the current buffer. |
| 4111 | */ |
| 4112 | int |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 4113 | map_to_exists_mode(char_u *rhs, int mode, int abbr) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4114 | { |
| 4115 | mapblock_T *mp; |
| 4116 | int hash; |
| 4117 | # ifdef FEAT_LOCALMAP |
| 4118 | int expand_buffer = FALSE; |
| 4119 | |
| 4120 | validate_maphash(); |
| 4121 | |
| 4122 | /* Do it twice: once for global maps and once for local maps. */ |
| 4123 | for (;;) |
| 4124 | { |
| 4125 | # endif |
| 4126 | for (hash = 0; hash < 256; ++hash) |
| 4127 | { |
Bram Moolenaar | be4d506 | 2006-03-18 21:30:13 +0000 | [diff] [blame] | 4128 | if (abbr) |
| 4129 | { |
| 4130 | if (hash > 0) /* there is only one abbr list */ |
| 4131 | break; |
| 4132 | #ifdef FEAT_LOCALMAP |
| 4133 | if (expand_buffer) |
| 4134 | mp = curbuf->b_first_abbr; |
| 4135 | else |
| 4136 | #endif |
| 4137 | mp = first_abbr; |
| 4138 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4139 | # ifdef FEAT_LOCALMAP |
Bram Moolenaar | be4d506 | 2006-03-18 21:30:13 +0000 | [diff] [blame] | 4140 | else if (expand_buffer) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4141 | mp = curbuf->b_maphash[hash]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4142 | # endif |
Bram Moolenaar | be4d506 | 2006-03-18 21:30:13 +0000 | [diff] [blame] | 4143 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4144 | mp = maphash[hash]; |
| 4145 | for (; mp; mp = mp->m_next) |
| 4146 | { |
| 4147 | if ((mp->m_mode & mode) |
| 4148 | && strstr((char *)mp->m_str, (char *)rhs) != NULL) |
| 4149 | return TRUE; |
| 4150 | } |
| 4151 | } |
| 4152 | # ifdef FEAT_LOCALMAP |
| 4153 | if (expand_buffer) |
| 4154 | break; |
| 4155 | expand_buffer = TRUE; |
| 4156 | } |
| 4157 | # endif |
| 4158 | |
| 4159 | return FALSE; |
| 4160 | } |
| 4161 | |
| 4162 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 4163 | /* |
| 4164 | * Used below when expanding mapping/abbreviation names. |
| 4165 | */ |
| 4166 | static int expand_mapmodes = 0; |
| 4167 | static int expand_isabbrev = 0; |
| 4168 | #ifdef FEAT_LOCALMAP |
| 4169 | static int expand_buffer = FALSE; |
| 4170 | #endif |
| 4171 | |
| 4172 | /* |
| 4173 | * Work out what to complete when doing command line completion of mapping |
| 4174 | * or abbreviation names. |
| 4175 | */ |
| 4176 | char_u * |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 4177 | set_context_in_map_cmd( |
| 4178 | expand_T *xp, |
| 4179 | char_u *cmd, |
| 4180 | char_u *arg, |
| 4181 | int forceit, /* TRUE if '!' given */ |
| 4182 | int isabbrev, /* TRUE if abbreviation */ |
| 4183 | int isunmap, /* TRUE if unmap/unabbrev command */ |
| 4184 | cmdidx_T cmdidx) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4185 | { |
| 4186 | if (forceit && cmdidx != CMD_map && cmdidx != CMD_unmap) |
| 4187 | xp->xp_context = EXPAND_NOTHING; |
| 4188 | else |
| 4189 | { |
| 4190 | if (isunmap) |
| 4191 | expand_mapmodes = get_map_mode(&cmd, forceit || isabbrev); |
| 4192 | else |
| 4193 | { |
| 4194 | expand_mapmodes = INSERT + CMDLINE; |
| 4195 | if (!isabbrev) |
Bram Moolenaar | 371d540 | 2006-03-20 21:47:49 +0000 | [diff] [blame] | 4196 | expand_mapmodes += VISUAL + SELECTMODE + NORMAL + OP_PENDING; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4197 | } |
| 4198 | expand_isabbrev = isabbrev; |
| 4199 | xp->xp_context = EXPAND_MAPPINGS; |
| 4200 | #ifdef FEAT_LOCALMAP |
| 4201 | expand_buffer = FALSE; |
| 4202 | #endif |
| 4203 | for (;;) |
| 4204 | { |
| 4205 | #ifdef FEAT_LOCALMAP |
| 4206 | if (STRNCMP(arg, "<buffer>", 8) == 0) |
| 4207 | { |
| 4208 | expand_buffer = TRUE; |
| 4209 | arg = skipwhite(arg + 8); |
| 4210 | continue; |
| 4211 | } |
| 4212 | #endif |
| 4213 | if (STRNCMP(arg, "<unique>", 8) == 0) |
| 4214 | { |
| 4215 | arg = skipwhite(arg + 8); |
| 4216 | continue; |
| 4217 | } |
Bram Moolenaar | 72179e1 | 2013-06-29 13:58:31 +0200 | [diff] [blame] | 4218 | if (STRNCMP(arg, "<nowait>", 8) == 0) |
| 4219 | { |
| 4220 | arg = skipwhite(arg + 8); |
| 4221 | continue; |
| 4222 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4223 | if (STRNCMP(arg, "<silent>", 8) == 0) |
| 4224 | { |
| 4225 | arg = skipwhite(arg + 8); |
| 4226 | continue; |
| 4227 | } |
Bram Moolenaar | 4e42719 | 2006-03-10 21:34:27 +0000 | [diff] [blame] | 4228 | #ifdef FEAT_EVAL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4229 | if (STRNCMP(arg, "<script>", 8) == 0) |
| 4230 | { |
| 4231 | arg = skipwhite(arg + 8); |
| 4232 | continue; |
| 4233 | } |
Bram Moolenaar | 4e42719 | 2006-03-10 21:34:27 +0000 | [diff] [blame] | 4234 | if (STRNCMP(arg, "<expr>", 6) == 0) |
| 4235 | { |
| 4236 | arg = skipwhite(arg + 6); |
| 4237 | continue; |
| 4238 | } |
| 4239 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4240 | break; |
| 4241 | } |
| 4242 | xp->xp_pattern = arg; |
| 4243 | } |
| 4244 | |
| 4245 | return NULL; |
| 4246 | } |
| 4247 | |
| 4248 | /* |
| 4249 | * Find all mapping/abbreviation names that match regexp 'prog'. |
| 4250 | * For command line expansion of ":[un]map" and ":[un]abbrev" in all modes. |
| 4251 | * Return OK if matches found, FAIL otherwise. |
| 4252 | */ |
| 4253 | int |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 4254 | ExpandMappings( |
| 4255 | regmatch_T *regmatch, |
| 4256 | int *num_file, |
| 4257 | char_u ***file) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4258 | { |
| 4259 | mapblock_T *mp; |
| 4260 | int hash; |
| 4261 | int count; |
| 4262 | int round; |
| 4263 | char_u *p; |
| 4264 | int i; |
| 4265 | |
| 4266 | validate_maphash(); |
| 4267 | |
| 4268 | *num_file = 0; /* return values in case of FAIL */ |
| 4269 | *file = NULL; |
| 4270 | |
| 4271 | /* |
| 4272 | * round == 1: Count the matches. |
| 4273 | * round == 2: Build the array to keep the matches. |
| 4274 | */ |
| 4275 | for (round = 1; round <= 2; ++round) |
| 4276 | { |
| 4277 | count = 0; |
| 4278 | |
Bram Moolenaar | 72179e1 | 2013-06-29 13:58:31 +0200 | [diff] [blame] | 4279 | for (i = 0; i < 6; ++i) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4280 | { |
| 4281 | if (i == 0) |
| 4282 | p = (char_u *)"<silent>"; |
| 4283 | else if (i == 1) |
| 4284 | p = (char_u *)"<unique>"; |
| 4285 | #ifdef FEAT_EVAL |
| 4286 | else if (i == 2) |
| 4287 | p = (char_u *)"<script>"; |
Bram Moolenaar | 4e42719 | 2006-03-10 21:34:27 +0000 | [diff] [blame] | 4288 | else if (i == 3) |
| 4289 | p = (char_u *)"<expr>"; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4290 | #endif |
| 4291 | #ifdef FEAT_LOCALMAP |
Bram Moolenaar | 4e42719 | 2006-03-10 21:34:27 +0000 | [diff] [blame] | 4292 | else if (i == 4 && !expand_buffer) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4293 | p = (char_u *)"<buffer>"; |
| 4294 | #endif |
Bram Moolenaar | 72179e1 | 2013-06-29 13:58:31 +0200 | [diff] [blame] | 4295 | else if (i == 5) |
| 4296 | p = (char_u *)"<nowait>"; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4297 | else |
| 4298 | continue; |
| 4299 | |
| 4300 | if (vim_regexec(regmatch, p, (colnr_T)0)) |
| 4301 | { |
| 4302 | if (round == 1) |
| 4303 | ++count; |
| 4304 | else |
| 4305 | (*file)[count++] = vim_strsave(p); |
| 4306 | } |
| 4307 | } |
| 4308 | |
| 4309 | for (hash = 0; hash < 256; ++hash) |
| 4310 | { |
| 4311 | if (expand_isabbrev) |
| 4312 | { |
Bram Moolenaar | be4d506 | 2006-03-18 21:30:13 +0000 | [diff] [blame] | 4313 | if (hash > 0) /* only one abbrev list */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4314 | break; /* for (hash) */ |
| 4315 | mp = first_abbr; |
| 4316 | } |
| 4317 | #ifdef FEAT_LOCALMAP |
| 4318 | else if (expand_buffer) |
| 4319 | mp = curbuf->b_maphash[hash]; |
| 4320 | #endif |
| 4321 | else |
| 4322 | mp = maphash[hash]; |
| 4323 | for (; mp; mp = mp->m_next) |
| 4324 | { |
| 4325 | if (mp->m_mode & expand_mapmodes) |
| 4326 | { |
| 4327 | p = translate_mapping(mp->m_keys, TRUE); |
| 4328 | if (p != NULL && vim_regexec(regmatch, p, (colnr_T)0)) |
| 4329 | { |
| 4330 | if (round == 1) |
| 4331 | ++count; |
| 4332 | else |
| 4333 | { |
| 4334 | (*file)[count++] = p; |
| 4335 | p = NULL; |
| 4336 | } |
| 4337 | } |
| 4338 | vim_free(p); |
| 4339 | } |
| 4340 | } /* for (mp) */ |
| 4341 | } /* for (hash) */ |
| 4342 | |
| 4343 | if (count == 0) /* no match found */ |
| 4344 | break; /* for (round) */ |
| 4345 | |
| 4346 | if (round == 1) |
| 4347 | { |
| 4348 | *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *))); |
| 4349 | if (*file == NULL) |
| 4350 | return FAIL; |
| 4351 | } |
| 4352 | } /* for (round) */ |
| 4353 | |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 4354 | if (count > 1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4355 | { |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 4356 | char_u **ptr1; |
| 4357 | char_u **ptr2; |
| 4358 | char_u **ptr3; |
| 4359 | |
| 4360 | /* Sort the matches */ |
| 4361 | sort_strings(*file, count); |
| 4362 | |
| 4363 | /* Remove multiple entries */ |
| 4364 | ptr1 = *file; |
| 4365 | ptr2 = ptr1 + 1; |
| 4366 | ptr3 = ptr1 + count; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4367 | |
| 4368 | while (ptr2 < ptr3) |
| 4369 | { |
| 4370 | if (STRCMP(*ptr1, *ptr2)) |
| 4371 | *++ptr1 = *ptr2++; |
| 4372 | else |
| 4373 | { |
| 4374 | vim_free(*ptr2++); |
| 4375 | count--; |
| 4376 | } |
| 4377 | } |
| 4378 | } |
| 4379 | |
| 4380 | *num_file = count; |
| 4381 | return (count == 0 ? FAIL : OK); |
| 4382 | } |
| 4383 | #endif /* FEAT_CMDL_COMPL */ |
| 4384 | |
| 4385 | /* |
| 4386 | * Check for an abbreviation. |
| 4387 | * Cursor is at ptr[col]. When inserting, mincol is where insert started. |
| 4388 | * "c" is the character typed before check_abbr was called. It may have |
| 4389 | * ABBR_OFF added to avoid prepending a CTRL-V to it. |
| 4390 | * |
| 4391 | * Historic vi practice: The last character of an abbreviation must be an id |
| 4392 | * character ([a-zA-Z0-9_]). The characters in front of it must be all id |
| 4393 | * characters or all non-id characters. This allows for abbr. "#i" to |
| 4394 | * "#include". |
| 4395 | * |
| 4396 | * Vim addition: Allow for abbreviations that end in a non-keyword character. |
| 4397 | * Then there must be white space before the abbr. |
| 4398 | * |
| 4399 | * return TRUE if there is an abbreviation, FALSE if not |
| 4400 | */ |
| 4401 | int |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 4402 | check_abbr( |
| 4403 | int c, |
| 4404 | char_u *ptr, |
| 4405 | int col, |
| 4406 | int mincol) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4407 | { |
| 4408 | int len; |
| 4409 | int scol; /* starting column of the abbr. */ |
| 4410 | int j; |
Bram Moolenaar | 4e42719 | 2006-03-10 21:34:27 +0000 | [diff] [blame] | 4411 | char_u *s; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4412 | char_u tb[MB_MAXBYTES + 4]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4413 | mapblock_T *mp; |
| 4414 | #ifdef FEAT_LOCALMAP |
| 4415 | mapblock_T *mp2; |
| 4416 | #endif |
| 4417 | #ifdef FEAT_MBYTE |
| 4418 | int clen = 0; /* length in characters */ |
| 4419 | #endif |
| 4420 | int is_id = TRUE; |
| 4421 | int vim_abbr; |
| 4422 | |
| 4423 | if (typebuf.tb_no_abbr_cnt) /* abbrev. are not recursive */ |
| 4424 | return FALSE; |
Bram Moolenaar | e0ebfd7 | 2012-04-05 16:07:06 +0200 | [diff] [blame] | 4425 | |
| 4426 | /* no remapping implies no abbreviation, except for CTRL-] */ |
| 4427 | if ((KeyNoremap & (RM_NONE|RM_SCRIPT)) != 0 && c != Ctrl_RSB) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4428 | return FALSE; |
| 4429 | |
| 4430 | /* |
| 4431 | * Check for word before the cursor: If it ends in a keyword char all |
Bram Moolenaar | f6f95d9 | 2009-11-11 15:23:37 +0000 | [diff] [blame] | 4432 | * chars before it must be keyword chars or non-keyword chars, but not |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4433 | * white space. If it ends in a non-keyword char we accept any characters |
| 4434 | * before it except white space. |
| 4435 | */ |
| 4436 | if (col == 0) /* cannot be an abbr. */ |
| 4437 | return FALSE; |
| 4438 | |
| 4439 | #ifdef FEAT_MBYTE |
| 4440 | if (has_mbyte) |
| 4441 | { |
| 4442 | char_u *p; |
| 4443 | |
| 4444 | p = mb_prevptr(ptr, ptr + col); |
| 4445 | if (!vim_iswordp(p)) |
| 4446 | vim_abbr = TRUE; /* Vim added abbr. */ |
| 4447 | else |
| 4448 | { |
| 4449 | vim_abbr = FALSE; /* vi compatible abbr. */ |
| 4450 | if (p > ptr) |
| 4451 | is_id = vim_iswordp(mb_prevptr(ptr, p)); |
| 4452 | } |
| 4453 | clen = 1; |
| 4454 | while (p > ptr + mincol) |
| 4455 | { |
| 4456 | p = mb_prevptr(ptr, p); |
| 4457 | if (vim_isspace(*p) || (!vim_abbr && is_id != vim_iswordp(p))) |
| 4458 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 4459 | p += (*mb_ptr2len)(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4460 | break; |
| 4461 | } |
| 4462 | ++clen; |
| 4463 | } |
| 4464 | scol = (int)(p - ptr); |
| 4465 | } |
| 4466 | else |
| 4467 | #endif |
| 4468 | { |
| 4469 | if (!vim_iswordc(ptr[col - 1])) |
| 4470 | vim_abbr = TRUE; /* Vim added abbr. */ |
| 4471 | else |
| 4472 | { |
| 4473 | vim_abbr = FALSE; /* vi compatible abbr. */ |
| 4474 | if (col > 1) |
| 4475 | is_id = vim_iswordc(ptr[col - 2]); |
| 4476 | } |
| 4477 | for (scol = col - 1; scol > 0 && !vim_isspace(ptr[scol - 1]) |
| 4478 | && (vim_abbr || is_id == vim_iswordc(ptr[scol - 1])); --scol) |
| 4479 | ; |
| 4480 | } |
| 4481 | |
| 4482 | if (scol < mincol) |
| 4483 | scol = mincol; |
| 4484 | if (scol < col) /* there is a word in front of the cursor */ |
| 4485 | { |
| 4486 | ptr += scol; |
| 4487 | len = col - scol; |
| 4488 | #ifdef FEAT_LOCALMAP |
| 4489 | mp = curbuf->b_first_abbr; |
| 4490 | mp2 = first_abbr; |
| 4491 | if (mp == NULL) |
| 4492 | { |
| 4493 | mp = mp2; |
| 4494 | mp2 = NULL; |
| 4495 | } |
| 4496 | #else |
| 4497 | mp = first_abbr; |
| 4498 | #endif |
| 4499 | for ( ; mp; |
| 4500 | #ifdef FEAT_LOCALMAP |
| 4501 | mp->m_next == NULL ? (mp = mp2, mp2 = NULL) : |
| 4502 | #endif |
| 4503 | (mp = mp->m_next)) |
| 4504 | { |
Bram Moolenaar | 4920a44 | 2014-10-21 19:35:31 +0200 | [diff] [blame] | 4505 | int qlen = mp->m_keylen; |
| 4506 | char_u *q = mp->m_keys; |
| 4507 | int match; |
| 4508 | |
| 4509 | if (vim_strbyte(mp->m_keys, K_SPECIAL) != NULL) |
| 4510 | { |
| 4511 | /* might have CSI escaped mp->m_keys */ |
| 4512 | q = vim_strsave(mp->m_keys); |
| 4513 | if (q != NULL) |
| 4514 | { |
| 4515 | vim_unescape_csi(q); |
| 4516 | qlen = (int)STRLEN(q); |
| 4517 | } |
| 4518 | } |
| 4519 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4520 | /* find entries with right mode and keys */ |
Bram Moolenaar | 4920a44 | 2014-10-21 19:35:31 +0200 | [diff] [blame] | 4521 | match = (mp->m_mode & State) |
Bram Moolenaar | bdef518 | 2014-10-21 16:22:17 +0200 | [diff] [blame] | 4522 | && qlen == len |
Bram Moolenaar | 4920a44 | 2014-10-21 19:35:31 +0200 | [diff] [blame] | 4523 | && !STRNCMP(q, ptr, (size_t)len); |
| 4524 | if (q != mp->m_keys) |
| 4525 | vim_free(q); |
| 4526 | if (match) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4527 | break; |
| 4528 | } |
| 4529 | if (mp != NULL) |
| 4530 | { |
| 4531 | /* |
| 4532 | * Found a match: |
| 4533 | * Insert the rest of the abbreviation in typebuf.tb_buf[]. |
| 4534 | * This goes from end to start. |
| 4535 | * |
| 4536 | * Characters 0x000 - 0x100: normal chars, may need CTRL-V, |
| 4537 | * except K_SPECIAL: Becomes K_SPECIAL KS_SPECIAL KE_FILLER |
| 4538 | * Characters where IS_SPECIAL() == TRUE: key codes, need |
| 4539 | * K_SPECIAL. Other characters (with ABBR_OFF): don't use CTRL-V. |
| 4540 | * |
| 4541 | * Character CTRL-] is treated specially - it completes the |
| 4542 | * abbreviation, but is not inserted into the input stream. |
| 4543 | */ |
| 4544 | j = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4545 | if (c != Ctrl_RSB) |
| 4546 | { |
Bram Moolenaar | da9591e | 2009-09-30 13:17:02 +0000 | [diff] [blame] | 4547 | /* special key code, split up */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4548 | if (IS_SPECIAL(c) || c == K_SPECIAL) |
| 4549 | { |
| 4550 | tb[j++] = K_SPECIAL; |
| 4551 | tb[j++] = K_SECOND(c); |
| 4552 | tb[j++] = K_THIRD(c); |
| 4553 | } |
| 4554 | else |
| 4555 | { |
| 4556 | if (c < ABBR_OFF && (c < ' ' || c > '~')) |
| 4557 | tb[j++] = Ctrl_V; /* special char needs CTRL-V */ |
| 4558 | #ifdef FEAT_MBYTE |
| 4559 | if (has_mbyte) |
| 4560 | { |
| 4561 | /* if ABBR_OFF has been added, remove it here */ |
| 4562 | if (c >= ABBR_OFF) |
| 4563 | c -= ABBR_OFF; |
| 4564 | j += (*mb_char2bytes)(c, tb + j); |
| 4565 | } |
| 4566 | else |
| 4567 | #endif |
| 4568 | tb[j++] = c; |
| 4569 | } |
| 4570 | tb[j] = NUL; |
| 4571 | /* insert the last typed char */ |
| 4572 | (void)ins_typebuf(tb, 1, 0, TRUE, mp->m_silent); |
| 4573 | } |
Bram Moolenaar | 4e42719 | 2006-03-10 21:34:27 +0000 | [diff] [blame] | 4574 | #ifdef FEAT_EVAL |
| 4575 | if (mp->m_expr) |
Bram Moolenaar | da9591e | 2009-09-30 13:17:02 +0000 | [diff] [blame] | 4576 | s = eval_map_expr(mp->m_str, c); |
Bram Moolenaar | 4e42719 | 2006-03-10 21:34:27 +0000 | [diff] [blame] | 4577 | else |
| 4578 | #endif |
| 4579 | s = mp->m_str; |
| 4580 | if (s != NULL) |
| 4581 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4582 | /* insert the to string */ |
Bram Moolenaar | 4e42719 | 2006-03-10 21:34:27 +0000 | [diff] [blame] | 4583 | (void)ins_typebuf(s, mp->m_noremap, 0, TRUE, mp->m_silent); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4584 | /* no abbrev. for these chars */ |
Bram Moolenaar | 4e42719 | 2006-03-10 21:34:27 +0000 | [diff] [blame] | 4585 | typebuf.tb_no_abbr_cnt += (int)STRLEN(s) + j + 1; |
| 4586 | #ifdef FEAT_EVAL |
| 4587 | if (mp->m_expr) |
| 4588 | vim_free(s); |
| 4589 | #endif |
| 4590 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4591 | |
| 4592 | tb[0] = Ctrl_H; |
| 4593 | tb[1] = NUL; |
| 4594 | #ifdef FEAT_MBYTE |
| 4595 | if (has_mbyte) |
| 4596 | len = clen; /* Delete characters instead of bytes */ |
| 4597 | #endif |
| 4598 | while (len-- > 0) /* delete the from string */ |
| 4599 | (void)ins_typebuf(tb, 1, 0, TRUE, mp->m_silent); |
| 4600 | return TRUE; |
| 4601 | } |
| 4602 | } |
| 4603 | return FALSE; |
| 4604 | } |
| 4605 | |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 4606 | #ifdef FEAT_EVAL |
| 4607 | /* |
| 4608 | * Evaluate the RHS of a mapping or abbreviations and take care of escaping |
| 4609 | * special characters. |
| 4610 | */ |
| 4611 | static char_u * |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 4612 | eval_map_expr( |
| 4613 | char_u *str, |
| 4614 | int c) /* NUL or typed character for abbreviation */ |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 4615 | { |
| 4616 | char_u *res; |
Bram Moolenaar | 8424a62 | 2006-04-19 21:23:36 +0000 | [diff] [blame] | 4617 | char_u *p; |
Bram Moolenaar | f6f4a01 | 2011-08-17 17:18:20 +0200 | [diff] [blame] | 4618 | char_u *expr; |
Bram Moolenaar | c1b5286 | 2006-04-28 22:32:28 +0000 | [diff] [blame] | 4619 | char_u *save_cmd; |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 4620 | pos_T save_cursor; |
Bram Moolenaar | 6376064 | 2011-12-23 14:54:04 +0100 | [diff] [blame] | 4621 | int save_msg_col; |
| 4622 | int save_msg_row; |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 4623 | |
Bram Moolenaar | f6f4a01 | 2011-08-17 17:18:20 +0200 | [diff] [blame] | 4624 | /* Remove escaping of CSI, because "str" is in a format to be used as |
| 4625 | * typeahead. */ |
| 4626 | expr = vim_strsave(str); |
| 4627 | if (expr == NULL) |
| 4628 | return NULL; |
| 4629 | vim_unescape_csi(expr); |
| 4630 | |
Bram Moolenaar | c1b5286 | 2006-04-28 22:32:28 +0000 | [diff] [blame] | 4631 | save_cmd = save_cmdline_alloc(); |
| 4632 | if (save_cmd == NULL) |
Bram Moolenaar | f6f4a01 | 2011-08-17 17:18:20 +0200 | [diff] [blame] | 4633 | { |
| 4634 | vim_free(expr); |
Bram Moolenaar | c1b5286 | 2006-04-28 22:32:28 +0000 | [diff] [blame] | 4635 | return NULL; |
Bram Moolenaar | f6f4a01 | 2011-08-17 17:18:20 +0200 | [diff] [blame] | 4636 | } |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 4637 | |
| 4638 | /* Forbid changing text or using ":normal" to avoid most of the bad side |
| 4639 | * effects. Also restore the cursor position. */ |
| 4640 | ++textlock; |
| 4641 | #ifdef FEAT_EX_EXTRA |
| 4642 | ++ex_normal_lock; |
| 4643 | #endif |
Bram Moolenaar | da9591e | 2009-09-30 13:17:02 +0000 | [diff] [blame] | 4644 | set_vim_var_char(c); /* set v:char to the typed character */ |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 4645 | save_cursor = curwin->w_cursor; |
Bram Moolenaar | 6376064 | 2011-12-23 14:54:04 +0100 | [diff] [blame] | 4646 | save_msg_col = msg_col; |
| 4647 | save_msg_row = msg_row; |
Bram Moolenaar | f6f4a01 | 2011-08-17 17:18:20 +0200 | [diff] [blame] | 4648 | p = eval_to_string(expr, NULL, FALSE); |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 4649 | --textlock; |
| 4650 | #ifdef FEAT_EX_EXTRA |
| 4651 | --ex_normal_lock; |
| 4652 | #endif |
| 4653 | curwin->w_cursor = save_cursor; |
Bram Moolenaar | 6376064 | 2011-12-23 14:54:04 +0100 | [diff] [blame] | 4654 | msg_col = save_msg_col; |
| 4655 | msg_row = save_msg_row; |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 4656 | |
Bram Moolenaar | c1b5286 | 2006-04-28 22:32:28 +0000 | [diff] [blame] | 4657 | restore_cmdline_alloc(save_cmd); |
Bram Moolenaar | f6f4a01 | 2011-08-17 17:18:20 +0200 | [diff] [blame] | 4658 | vim_free(expr); |
| 4659 | |
Bram Moolenaar | 8424a62 | 2006-04-19 21:23:36 +0000 | [diff] [blame] | 4660 | if (p == NULL) |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 4661 | return NULL; |
Bram Moolenaar | f6f4a01 | 2011-08-17 17:18:20 +0200 | [diff] [blame] | 4662 | /* Escape CSI in the result to be able to use the string as typeahead. */ |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 4663 | res = vim_strsave_escape_csi(p); |
| 4664 | vim_free(p); |
| 4665 | |
| 4666 | return res; |
| 4667 | } |
| 4668 | #endif |
| 4669 | |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 4670 | /* |
| 4671 | * Copy "p" to allocated memory, escaping K_SPECIAL and CSI so that the result |
| 4672 | * can be put in the typeahead buffer. |
| 4673 | * Returns NULL when out of memory. |
| 4674 | */ |
| 4675 | char_u * |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 4676 | vim_strsave_escape_csi( |
| 4677 | char_u *p) |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 4678 | { |
| 4679 | char_u *res; |
| 4680 | char_u *s, *d; |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 4681 | |
| 4682 | /* Need a buffer to hold up to three times as much. */ |
Bram Moolenaar | 8424a62 | 2006-04-19 21:23:36 +0000 | [diff] [blame] | 4683 | res = alloc((unsigned)(STRLEN(p) * 3) + 1); |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 4684 | if (res != NULL) |
| 4685 | { |
Bram Moolenaar | 8424a62 | 2006-04-19 21:23:36 +0000 | [diff] [blame] | 4686 | d = res; |
| 4687 | for (s = p; *s != NUL; ) |
| 4688 | { |
| 4689 | if (s[0] == K_SPECIAL && s[1] != NUL && s[2] != NUL) |
| 4690 | { |
| 4691 | /* Copy special key unmodified. */ |
| 4692 | *d++ = *s++; |
| 4693 | *d++ = *s++; |
| 4694 | *d++ = *s++; |
| 4695 | } |
| 4696 | else |
| 4697 | { |
Bram Moolenaar | 229f8db | 2013-05-06 05:50:28 +0200 | [diff] [blame] | 4698 | #ifdef FEAT_MBYTE |
| 4699 | int len = mb_char2len(PTR2CHAR(s)); |
| 4700 | int len2 = mb_ptr2len(s); |
| 4701 | #endif |
Bram Moolenaar | 8424a62 | 2006-04-19 21:23:36 +0000 | [diff] [blame] | 4702 | /* Add character, possibly multi-byte to destination, escaping |
| 4703 | * CSI and K_SPECIAL. */ |
| 4704 | d = add_char2buf(PTR2CHAR(s), d); |
Bram Moolenaar | 229f8db | 2013-05-06 05:50:28 +0200 | [diff] [blame] | 4705 | #ifdef FEAT_MBYTE |
| 4706 | while (len < len2) |
| 4707 | { |
| 4708 | /* add following combining char */ |
| 4709 | d = add_char2buf(PTR2CHAR(s + len), d); |
| 4710 | len += mb_char2len(PTR2CHAR(s + len)); |
| 4711 | } |
| 4712 | #endif |
Bram Moolenaar | 8424a62 | 2006-04-19 21:23:36 +0000 | [diff] [blame] | 4713 | mb_ptr_adv(s); |
| 4714 | } |
| 4715 | } |
| 4716 | *d = NUL; |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 4717 | } |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 4718 | return res; |
| 4719 | } |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 4720 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4721 | /* |
Bram Moolenaar | 81b8587 | 2007-03-04 20:22:01 +0000 | [diff] [blame] | 4722 | * Remove escaping from CSI and K_SPECIAL characters. Reverse of |
| 4723 | * vim_strsave_escape_csi(). Works in-place. |
| 4724 | */ |
| 4725 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 4726 | vim_unescape_csi(char_u *p) |
Bram Moolenaar | 81b8587 | 2007-03-04 20:22:01 +0000 | [diff] [blame] | 4727 | { |
| 4728 | char_u *s = p, *d = p; |
| 4729 | |
| 4730 | while (*s != NUL) |
| 4731 | { |
| 4732 | if (s[0] == K_SPECIAL && s[1] == KS_SPECIAL && s[2] == KE_FILLER) |
| 4733 | { |
| 4734 | *d++ = K_SPECIAL; |
| 4735 | s += 3; |
| 4736 | } |
| 4737 | else if ((s[0] == K_SPECIAL || s[0] == CSI) |
| 4738 | && s[1] == KS_EXTRA && s[2] == (int)KE_CSI) |
| 4739 | { |
| 4740 | *d++ = CSI; |
| 4741 | s += 3; |
| 4742 | } |
| 4743 | else |
| 4744 | *d++ = *s++; |
| 4745 | } |
| 4746 | *d = NUL; |
| 4747 | } |
| 4748 | |
| 4749 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4750 | * Write map commands for the current mappings to an .exrc file. |
| 4751 | * Return FAIL on error, OK otherwise. |
| 4752 | */ |
| 4753 | int |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 4754 | makemap( |
| 4755 | FILE *fd, |
| 4756 | buf_T *buf) /* buffer for local mappings or NULL */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4757 | { |
| 4758 | mapblock_T *mp; |
Bram Moolenaar | 01b9a40 | 2008-07-22 16:58:47 +0000 | [diff] [blame] | 4759 | char_u c1, c2, c3; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4760 | char_u *p; |
| 4761 | char *cmd; |
| 4762 | int abbr; |
| 4763 | int hash; |
| 4764 | int did_cpo = FALSE; |
| 4765 | int i; |
| 4766 | |
| 4767 | validate_maphash(); |
| 4768 | |
| 4769 | /* |
| 4770 | * Do the loop twice: Once for mappings, once for abbreviations. |
| 4771 | * Then loop over all map hash lists. |
| 4772 | */ |
| 4773 | for (abbr = 0; abbr < 2; ++abbr) |
| 4774 | for (hash = 0; hash < 256; ++hash) |
| 4775 | { |
| 4776 | if (abbr) |
| 4777 | { |
Bram Moolenaar | be4d506 | 2006-03-18 21:30:13 +0000 | [diff] [blame] | 4778 | if (hash > 0) /* there is only one abbr list */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4779 | break; |
| 4780 | #ifdef FEAT_LOCALMAP |
| 4781 | if (buf != NULL) |
| 4782 | mp = buf->b_first_abbr; |
| 4783 | else |
| 4784 | #endif |
| 4785 | mp = first_abbr; |
| 4786 | } |
| 4787 | else |
| 4788 | { |
| 4789 | #ifdef FEAT_LOCALMAP |
| 4790 | if (buf != NULL) |
| 4791 | mp = buf->b_maphash[hash]; |
| 4792 | else |
| 4793 | #endif |
| 4794 | mp = maphash[hash]; |
| 4795 | } |
| 4796 | |
| 4797 | for ( ; mp; mp = mp->m_next) |
| 4798 | { |
| 4799 | /* skip script-local mappings */ |
| 4800 | if (mp->m_noremap == REMAP_SCRIPT) |
| 4801 | continue; |
| 4802 | |
| 4803 | /* skip mappings that contain a <SNR> (script-local thing), |
| 4804 | * they probably don't work when loaded again */ |
| 4805 | for (p = mp->m_str; *p != NUL; ++p) |
| 4806 | if (p[0] == K_SPECIAL && p[1] == KS_EXTRA |
| 4807 | && p[2] == (int)KE_SNR) |
| 4808 | break; |
| 4809 | if (*p != NUL) |
| 4810 | continue; |
| 4811 | |
Bram Moolenaar | 01b9a40 | 2008-07-22 16:58:47 +0000 | [diff] [blame] | 4812 | /* It's possible to create a mapping and then ":unmap" certain |
| 4813 | * modes. We recreate this here by mapping the individual |
| 4814 | * modes, which requires up to three of them. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4815 | c1 = NUL; |
| 4816 | c2 = NUL; |
Bram Moolenaar | 01b9a40 | 2008-07-22 16:58:47 +0000 | [diff] [blame] | 4817 | c3 = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4818 | if (abbr) |
| 4819 | cmd = "abbr"; |
| 4820 | else |
| 4821 | cmd = "map"; |
| 4822 | switch (mp->m_mode) |
| 4823 | { |
Bram Moolenaar | 371d540 | 2006-03-20 21:47:49 +0000 | [diff] [blame] | 4824 | case NORMAL + VISUAL + SELECTMODE + OP_PENDING: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4825 | break; |
| 4826 | case NORMAL: |
| 4827 | c1 = 'n'; |
| 4828 | break; |
Bram Moolenaar | 371d540 | 2006-03-20 21:47:49 +0000 | [diff] [blame] | 4829 | case VISUAL: |
| 4830 | c1 = 'x'; |
| 4831 | break; |
| 4832 | case SELECTMODE: |
| 4833 | c1 = 's'; |
| 4834 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4835 | case OP_PENDING: |
| 4836 | c1 = 'o'; |
| 4837 | break; |
Bram Moolenaar | 01b9a40 | 2008-07-22 16:58:47 +0000 | [diff] [blame] | 4838 | case NORMAL + VISUAL: |
| 4839 | c1 = 'n'; |
| 4840 | c2 = 'x'; |
| 4841 | break; |
| 4842 | case NORMAL + SELECTMODE: |
| 4843 | c1 = 'n'; |
| 4844 | c2 = 's'; |
| 4845 | break; |
| 4846 | case NORMAL + OP_PENDING: |
| 4847 | c1 = 'n'; |
| 4848 | c2 = 'o'; |
| 4849 | break; |
| 4850 | case VISUAL + SELECTMODE: |
| 4851 | c1 = 'v'; |
| 4852 | break; |
| 4853 | case VISUAL + OP_PENDING: |
| 4854 | c1 = 'x'; |
| 4855 | c2 = 'o'; |
| 4856 | break; |
| 4857 | case SELECTMODE + OP_PENDING: |
| 4858 | c1 = 's'; |
| 4859 | c2 = 'o'; |
| 4860 | break; |
Bram Moolenaar | 371d540 | 2006-03-20 21:47:49 +0000 | [diff] [blame] | 4861 | case NORMAL + VISUAL + SELECTMODE: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4862 | c1 = 'n'; |
| 4863 | c2 = 'v'; |
| 4864 | break; |
Bram Moolenaar | 01b9a40 | 2008-07-22 16:58:47 +0000 | [diff] [blame] | 4865 | case NORMAL + VISUAL + OP_PENDING: |
| 4866 | c1 = 'n'; |
| 4867 | c2 = 'x'; |
| 4868 | c3 = 'o'; |
| 4869 | break; |
| 4870 | case NORMAL + SELECTMODE + OP_PENDING: |
| 4871 | c1 = 'n'; |
| 4872 | c2 = 's'; |
| 4873 | c3 = 'o'; |
| 4874 | break; |
Bram Moolenaar | 371d540 | 2006-03-20 21:47:49 +0000 | [diff] [blame] | 4875 | case VISUAL + SELECTMODE + OP_PENDING: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4876 | c1 = 'v'; |
| 4877 | c2 = 'o'; |
| 4878 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4879 | case CMDLINE + INSERT: |
| 4880 | if (!abbr) |
| 4881 | cmd = "map!"; |
| 4882 | break; |
| 4883 | case CMDLINE: |
| 4884 | c1 = 'c'; |
| 4885 | break; |
| 4886 | case INSERT: |
| 4887 | c1 = 'i'; |
| 4888 | break; |
| 4889 | case LANGMAP: |
| 4890 | c1 = 'l'; |
| 4891 | break; |
| 4892 | default: |
| 4893 | EMSG(_("E228: makemap: Illegal mode")); |
| 4894 | return FAIL; |
| 4895 | } |
Bram Moolenaar | 01b9a40 | 2008-07-22 16:58:47 +0000 | [diff] [blame] | 4896 | do /* do this twice if c2 is set, 3 times with c3 */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4897 | { |
| 4898 | /* When outputting <> form, need to make sure that 'cpo' |
| 4899 | * is set to the Vim default. */ |
| 4900 | if (!did_cpo) |
| 4901 | { |
| 4902 | if (*mp->m_str == NUL) /* will use <Nop> */ |
| 4903 | did_cpo = TRUE; |
| 4904 | else |
| 4905 | for (i = 0; i < 2; ++i) |
| 4906 | for (p = (i ? mp->m_str : mp->m_keys); *p; ++p) |
| 4907 | if (*p == K_SPECIAL || *p == NL) |
| 4908 | did_cpo = TRUE; |
| 4909 | if (did_cpo) |
| 4910 | { |
| 4911 | if (fprintf(fd, "let s:cpo_save=&cpo") < 0 |
| 4912 | || put_eol(fd) < 0 |
| 4913 | || fprintf(fd, "set cpo&vim") < 0 |
| 4914 | || put_eol(fd) < 0) |
| 4915 | return FAIL; |
| 4916 | } |
| 4917 | } |
| 4918 | if (c1 && putc(c1, fd) < 0) |
| 4919 | return FAIL; |
| 4920 | if (mp->m_noremap != REMAP_YES && fprintf(fd, "nore") < 0) |
| 4921 | return FAIL; |
Bram Moolenaar | fe86f2d | 2008-11-28 20:29:07 +0000 | [diff] [blame] | 4922 | if (fputs(cmd, fd) < 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4923 | return FAIL; |
| 4924 | if (buf != NULL && fputs(" <buffer>", fd) < 0) |
| 4925 | return FAIL; |
Bram Moolenaar | 72179e1 | 2013-06-29 13:58:31 +0200 | [diff] [blame] | 4926 | if (mp->m_nowait && fputs(" <nowait>", fd) < 0) |
| 4927 | return FAIL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4928 | if (mp->m_silent && fputs(" <silent>", fd) < 0) |
| 4929 | return FAIL; |
Bram Moolenaar | 4e42719 | 2006-03-10 21:34:27 +0000 | [diff] [blame] | 4930 | #ifdef FEAT_EVAL |
| 4931 | if (mp->m_noremap == REMAP_SCRIPT |
| 4932 | && fputs("<script>", fd) < 0) |
| 4933 | return FAIL; |
| 4934 | if (mp->m_expr && fputs(" <expr>", fd) < 0) |
| 4935 | return FAIL; |
| 4936 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4937 | |
| 4938 | if ( putc(' ', fd) < 0 |
| 4939 | || put_escstr(fd, mp->m_keys, 0) == FAIL |
| 4940 | || putc(' ', fd) < 0 |
| 4941 | || put_escstr(fd, mp->m_str, 1) == FAIL |
| 4942 | || put_eol(fd) < 0) |
| 4943 | return FAIL; |
| 4944 | c1 = c2; |
Bram Moolenaar | 01b9a40 | 2008-07-22 16:58:47 +0000 | [diff] [blame] | 4945 | c2 = c3; |
| 4946 | c3 = NUL; |
| 4947 | } while (c1 != NUL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4948 | } |
| 4949 | } |
| 4950 | |
| 4951 | if (did_cpo) |
| 4952 | if (fprintf(fd, "let &cpo=s:cpo_save") < 0 |
| 4953 | || put_eol(fd) < 0 |
| 4954 | || fprintf(fd, "unlet s:cpo_save") < 0 |
| 4955 | || put_eol(fd) < 0) |
| 4956 | return FAIL; |
| 4957 | return OK; |
| 4958 | } |
| 4959 | |
| 4960 | /* |
| 4961 | * write escape string to file |
| 4962 | * "what": 0 for :map lhs, 1 for :map rhs, 2 for :set |
| 4963 | * |
| 4964 | * return FAIL for failure, OK otherwise |
| 4965 | */ |
| 4966 | int |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 4967 | put_escstr(FILE *fd, char_u *strstart, int what) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4968 | { |
| 4969 | char_u *str = strstart; |
| 4970 | int c; |
| 4971 | int modifiers; |
| 4972 | |
| 4973 | /* :map xx <Nop> */ |
| 4974 | if (*str == NUL && what == 1) |
| 4975 | { |
| 4976 | if (fprintf(fd, "<Nop>") < 0) |
| 4977 | return FAIL; |
| 4978 | return OK; |
| 4979 | } |
| 4980 | |
| 4981 | for ( ; *str != NUL; ++str) |
| 4982 | { |
| 4983 | #ifdef FEAT_MBYTE |
| 4984 | char_u *p; |
| 4985 | |
| 4986 | /* Check for a multi-byte character, which may contain escaped |
| 4987 | * K_SPECIAL and CSI bytes */ |
| 4988 | p = mb_unescape(&str); |
| 4989 | if (p != NULL) |
| 4990 | { |
| 4991 | while (*p != NUL) |
Bram Moolenaar | 51485f0 | 2005-06-04 21:55:20 +0000 | [diff] [blame] | 4992 | if (fputc(*p++, fd) < 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4993 | return FAIL; |
| 4994 | --str; |
| 4995 | continue; |
| 4996 | } |
| 4997 | #endif |
| 4998 | |
| 4999 | c = *str; |
| 5000 | /* |
| 5001 | * Special key codes have to be translated to be able to make sense |
| 5002 | * when they are read back. |
| 5003 | */ |
| 5004 | if (c == K_SPECIAL && what != 2) |
| 5005 | { |
| 5006 | modifiers = 0x0; |
| 5007 | if (str[1] == KS_MODIFIER) |
| 5008 | { |
| 5009 | modifiers = str[2]; |
| 5010 | str += 3; |
| 5011 | c = *str; |
| 5012 | } |
| 5013 | if (c == K_SPECIAL) |
| 5014 | { |
| 5015 | c = TO_SPECIAL(str[1], str[2]); |
| 5016 | str += 2; |
| 5017 | } |
| 5018 | if (IS_SPECIAL(c) || modifiers) /* special key */ |
| 5019 | { |
Bram Moolenaar | fe86f2d | 2008-11-28 20:29:07 +0000 | [diff] [blame] | 5020 | if (fputs((char *)get_special_key_name(c, modifiers), fd) < 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5021 | return FAIL; |
| 5022 | continue; |
| 5023 | } |
| 5024 | } |
| 5025 | |
| 5026 | /* |
| 5027 | * A '\n' in a map command should be written as <NL>. |
| 5028 | * A '\n' in a set command should be written as \^V^J. |
| 5029 | */ |
| 5030 | if (c == NL) |
| 5031 | { |
| 5032 | if (what == 2) |
| 5033 | { |
| 5034 | if (fprintf(fd, IF_EB("\\\026\n", "\\" CTRL_V_STR "\n")) < 0) |
| 5035 | return FAIL; |
| 5036 | } |
| 5037 | else |
| 5038 | { |
| 5039 | if (fprintf(fd, "<NL>") < 0) |
| 5040 | return FAIL; |
| 5041 | } |
| 5042 | continue; |
| 5043 | } |
| 5044 | |
| 5045 | /* |
| 5046 | * Some characters have to be escaped with CTRL-V to |
| 5047 | * prevent them from misinterpreted in DoOneCmd(). |
| 5048 | * A space, Tab and '"' has to be escaped with a backslash to |
| 5049 | * prevent it to be misinterpreted in do_set(). |
| 5050 | * A space has to be escaped with a CTRL-V when it's at the start of a |
| 5051 | * ":map" rhs. |
| 5052 | * A '<' has to be escaped with a CTRL-V to prevent it being |
| 5053 | * interpreted as the start of a special key name. |
| 5054 | * A space in the lhs of a :map needs a CTRL-V. |
| 5055 | */ |
| 5056 | if (what == 2 && (vim_iswhite(c) || c == '"' || c == '\\')) |
| 5057 | { |
| 5058 | if (putc('\\', fd) < 0) |
| 5059 | return FAIL; |
| 5060 | } |
| 5061 | else if (c < ' ' || c > '~' || c == '|' |
| 5062 | || (what == 0 && c == ' ') |
| 5063 | || (what == 1 && str == strstart && c == ' ') |
| 5064 | || (what != 2 && c == '<')) |
| 5065 | { |
| 5066 | if (putc(Ctrl_V, fd) < 0) |
| 5067 | return FAIL; |
| 5068 | } |
| 5069 | if (putc(c, fd) < 0) |
| 5070 | return FAIL; |
| 5071 | } |
| 5072 | return OK; |
| 5073 | } |
| 5074 | |
| 5075 | /* |
| 5076 | * Check all mappings for the presence of special key codes. |
| 5077 | * Used after ":set term=xxx". |
| 5078 | */ |
| 5079 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 5080 | check_map_keycodes(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5081 | { |
| 5082 | mapblock_T *mp; |
| 5083 | char_u *p; |
| 5084 | int i; |
| 5085 | char_u buf[3]; |
| 5086 | char_u *save_name; |
| 5087 | int abbr; |
| 5088 | int hash; |
| 5089 | #ifdef FEAT_LOCALMAP |
| 5090 | buf_T *bp; |
| 5091 | #endif |
| 5092 | |
| 5093 | validate_maphash(); |
| 5094 | save_name = sourcing_name; |
| 5095 | sourcing_name = (char_u *)"mappings"; /* avoids giving error messages */ |
| 5096 | |
| 5097 | #ifdef FEAT_LOCALMAP |
| 5098 | /* This this once for each buffer, and then once for global |
| 5099 | * mappings/abbreviations with bp == NULL */ |
| 5100 | for (bp = firstbuf; ; bp = bp->b_next) |
| 5101 | { |
| 5102 | #endif |
| 5103 | /* |
| 5104 | * Do the loop twice: Once for mappings, once for abbreviations. |
| 5105 | * Then loop over all map hash lists. |
| 5106 | */ |
| 5107 | for (abbr = 0; abbr <= 1; ++abbr) |
| 5108 | for (hash = 0; hash < 256; ++hash) |
| 5109 | { |
| 5110 | if (abbr) |
| 5111 | { |
| 5112 | if (hash) /* there is only one abbr list */ |
| 5113 | break; |
| 5114 | #ifdef FEAT_LOCALMAP |
| 5115 | if (bp != NULL) |
| 5116 | mp = bp->b_first_abbr; |
| 5117 | else |
| 5118 | #endif |
| 5119 | mp = first_abbr; |
| 5120 | } |
| 5121 | else |
| 5122 | { |
| 5123 | #ifdef FEAT_LOCALMAP |
| 5124 | if (bp != NULL) |
| 5125 | mp = bp->b_maphash[hash]; |
| 5126 | else |
| 5127 | #endif |
| 5128 | mp = maphash[hash]; |
| 5129 | } |
| 5130 | for ( ; mp != NULL; mp = mp->m_next) |
| 5131 | { |
| 5132 | for (i = 0; i <= 1; ++i) /* do this twice */ |
| 5133 | { |
| 5134 | if (i == 0) |
| 5135 | p = mp->m_keys; /* once for the "from" part */ |
| 5136 | else |
| 5137 | p = mp->m_str; /* and once for the "to" part */ |
| 5138 | while (*p) |
| 5139 | { |
| 5140 | if (*p == K_SPECIAL) |
| 5141 | { |
| 5142 | ++p; |
| 5143 | if (*p < 128) /* for "normal" tcap entries */ |
| 5144 | { |
| 5145 | buf[0] = p[0]; |
| 5146 | buf[1] = p[1]; |
| 5147 | buf[2] = NUL; |
| 5148 | (void)add_termcap_entry(buf, FALSE); |
| 5149 | } |
| 5150 | ++p; |
| 5151 | } |
| 5152 | ++p; |
| 5153 | } |
| 5154 | } |
| 5155 | } |
| 5156 | } |
| 5157 | #ifdef FEAT_LOCALMAP |
| 5158 | if (bp == NULL) |
| 5159 | break; |
| 5160 | } |
| 5161 | #endif |
| 5162 | sourcing_name = save_name; |
| 5163 | } |
| 5164 | |
Bram Moolenaar | bd74325 | 2010-10-20 21:23:33 +0200 | [diff] [blame] | 5165 | #if defined(FEAT_EVAL) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5166 | /* |
Bram Moolenaar | bd74325 | 2010-10-20 21:23:33 +0200 | [diff] [blame] | 5167 | * Check the string "keys" against the lhs of all mappings. |
| 5168 | * Return pointer to rhs of mapping (mapblock->m_str). |
| 5169 | * NULL when no mapping found. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5170 | */ |
| 5171 | char_u * |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 5172 | check_map( |
| 5173 | char_u *keys, |
| 5174 | int mode, |
| 5175 | int exact, /* require exact match */ |
| 5176 | int ign_mod, /* ignore preceding modifier */ |
| 5177 | int abbr, /* do abbreviations */ |
| 5178 | mapblock_T **mp_ptr, /* return: pointer to mapblock or NULL */ |
| 5179 | int *local_ptr) /* return: buffer-local mapping or NULL */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5180 | { |
| 5181 | int hash; |
| 5182 | int len, minlen; |
| 5183 | mapblock_T *mp; |
Bram Moolenaar | 686f51e | 2005-05-20 21:19:57 +0000 | [diff] [blame] | 5184 | char_u *s; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5185 | #ifdef FEAT_LOCALMAP |
| 5186 | int local; |
| 5187 | #endif |
| 5188 | |
| 5189 | validate_maphash(); |
| 5190 | |
| 5191 | len = (int)STRLEN(keys); |
| 5192 | #ifdef FEAT_LOCALMAP |
| 5193 | for (local = 1; local >= 0; --local) |
| 5194 | #endif |
| 5195 | /* loop over all hash lists */ |
| 5196 | for (hash = 0; hash < 256; ++hash) |
| 5197 | { |
Bram Moolenaar | be4d506 | 2006-03-18 21:30:13 +0000 | [diff] [blame] | 5198 | if (abbr) |
| 5199 | { |
| 5200 | if (hash > 0) /* there is only one list. */ |
| 5201 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5202 | #ifdef FEAT_LOCALMAP |
Bram Moolenaar | be4d506 | 2006-03-18 21:30:13 +0000 | [diff] [blame] | 5203 | if (local) |
| 5204 | mp = curbuf->b_first_abbr; |
| 5205 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5206 | #endif |
Bram Moolenaar | be4d506 | 2006-03-18 21:30:13 +0000 | [diff] [blame] | 5207 | mp = first_abbr; |
| 5208 | } |
| 5209 | #ifdef FEAT_LOCALMAP |
| 5210 | else if (local) |
| 5211 | mp = curbuf->b_maphash[hash]; |
| 5212 | #endif |
| 5213 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5214 | mp = maphash[hash]; |
| 5215 | for ( ; mp != NULL; mp = mp->m_next) |
| 5216 | { |
| 5217 | /* skip entries with wrong mode, wrong length and not matching |
| 5218 | * ones */ |
Bram Moolenaar | 686f51e | 2005-05-20 21:19:57 +0000 | [diff] [blame] | 5219 | if ((mp->m_mode & mode) && (!exact || mp->m_keylen == len)) |
| 5220 | { |
| 5221 | if (len > mp->m_keylen) |
| 5222 | minlen = mp->m_keylen; |
| 5223 | else |
| 5224 | minlen = len; |
| 5225 | s = mp->m_keys; |
| 5226 | if (ign_mod && s[0] == K_SPECIAL && s[1] == KS_MODIFIER |
| 5227 | && s[2] != NUL) |
| 5228 | { |
| 5229 | s += 3; |
| 5230 | if (len > mp->m_keylen - 3) |
| 5231 | minlen = mp->m_keylen - 3; |
| 5232 | } |
| 5233 | if (STRNCMP(s, keys, minlen) == 0) |
Bram Moolenaar | bd74325 | 2010-10-20 21:23:33 +0200 | [diff] [blame] | 5234 | { |
| 5235 | if (mp_ptr != NULL) |
| 5236 | *mp_ptr = mp; |
| 5237 | if (local_ptr != NULL) |
Bram Moolenaar | b8e8670 | 2010-10-22 22:13:52 +0200 | [diff] [blame] | 5238 | #ifdef FEAT_LOCALMAP |
Bram Moolenaar | bd74325 | 2010-10-20 21:23:33 +0200 | [diff] [blame] | 5239 | *local_ptr = local; |
Bram Moolenaar | b8e8670 | 2010-10-22 22:13:52 +0200 | [diff] [blame] | 5240 | #else |
| 5241 | *local_ptr = 0; |
| 5242 | #endif |
Bram Moolenaar | 686f51e | 2005-05-20 21:19:57 +0000 | [diff] [blame] | 5243 | return mp->m_str; |
Bram Moolenaar | bd74325 | 2010-10-20 21:23:33 +0200 | [diff] [blame] | 5244 | } |
Bram Moolenaar | 686f51e | 2005-05-20 21:19:57 +0000 | [diff] [blame] | 5245 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5246 | } |
| 5247 | } |
| 5248 | |
| 5249 | return NULL; |
| 5250 | } |
| 5251 | #endif |
| 5252 | |
Bram Moolenaar | e7fedb6 | 2015-12-31 19:07:19 +0100 | [diff] [blame] | 5253 | #if defined(MSDOS) || defined(MSWIN) || defined(MACOS) |
Bram Moolenaar | 371d540 | 2006-03-20 21:47:49 +0000 | [diff] [blame] | 5254 | |
| 5255 | #define VIS_SEL (VISUAL+SELECTMODE) /* abbreviation */ |
| 5256 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5257 | /* |
| 5258 | * Default mappings for some often used keys. |
| 5259 | */ |
| 5260 | static struct initmap |
| 5261 | { |
| 5262 | char_u *arg; |
| 5263 | int mode; |
| 5264 | } initmappings[] = |
| 5265 | { |
Bram Moolenaar | e7fedb6 | 2015-12-31 19:07:19 +0100 | [diff] [blame] | 5266 | #if defined(MSDOS) || defined(MSWIN) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5267 | /* Use the Windows (CUA) keybindings. */ |
| 5268 | # ifdef FEAT_GUI |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5269 | /* paste, copy and cut */ |
| 5270 | {(char_u *)"<S-Insert> \"*P", NORMAL}, |
Bram Moolenaar | 371d540 | 2006-03-20 21:47:49 +0000 | [diff] [blame] | 5271 | {(char_u *)"<S-Insert> \"-d\"*P", VIS_SEL}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5272 | {(char_u *)"<S-Insert> <C-R><C-O>*", INSERT+CMDLINE}, |
Bram Moolenaar | 371d540 | 2006-03-20 21:47:49 +0000 | [diff] [blame] | 5273 | {(char_u *)"<C-Insert> \"*y", VIS_SEL}, |
| 5274 | {(char_u *)"<S-Del> \"*d", VIS_SEL}, |
| 5275 | {(char_u *)"<C-Del> \"*d", VIS_SEL}, |
| 5276 | {(char_u *)"<C-X> \"*d", VIS_SEL}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5277 | /* Missing: CTRL-C (cancel) and CTRL-V (block selection) */ |
| 5278 | # else |
Bram Moolenaar | 371d540 | 2006-03-20 21:47:49 +0000 | [diff] [blame] | 5279 | {(char_u *)"\316w <C-Home>", NORMAL+VIS_SEL}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5280 | {(char_u *)"\316w <C-Home>", INSERT+CMDLINE}, |
Bram Moolenaar | 371d540 | 2006-03-20 21:47:49 +0000 | [diff] [blame] | 5281 | {(char_u *)"\316u <C-End>", NORMAL+VIS_SEL}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5282 | {(char_u *)"\316u <C-End>", INSERT+CMDLINE}, |
| 5283 | |
| 5284 | /* paste, copy and cut */ |
| 5285 | # ifdef FEAT_CLIPBOARD |
| 5286 | # ifdef DJGPP |
| 5287 | {(char_u *)"\316\122 \"*P", NORMAL}, /* SHIFT-Insert is "*P */ |
Bram Moolenaar | 0909215 | 2010-08-08 16:38:42 +0200 | [diff] [blame] | 5288 | {(char_u *)"\316\122 \"-d\"*P", VIS_SEL}, /* SHIFT-Insert is "-d"*P */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5289 | {(char_u *)"\316\122 \022\017*", INSERT}, /* SHIFT-Insert is ^R^O* */ |
Bram Moolenaar | 371d540 | 2006-03-20 21:47:49 +0000 | [diff] [blame] | 5290 | {(char_u *)"\316\222 \"*y", VIS_SEL}, /* CTRL-Insert is "*y */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5291 | # if 0 /* Shift-Del produces the same code as Del */ |
Bram Moolenaar | 371d540 | 2006-03-20 21:47:49 +0000 | [diff] [blame] | 5292 | {(char_u *)"\316\123 \"*d", VIS_SEL}, /* SHIFT-Del is "*d */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5293 | # endif |
Bram Moolenaar | 371d540 | 2006-03-20 21:47:49 +0000 | [diff] [blame] | 5294 | {(char_u *)"\316\223 \"*d", VIS_SEL}, /* CTRL-Del is "*d */ |
| 5295 | {(char_u *)"\030 \"-d", VIS_SEL}, /* CTRL-X is "-d */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5296 | # else |
| 5297 | {(char_u *)"\316\324 \"*P", NORMAL}, /* SHIFT-Insert is "*P */ |
Bram Moolenaar | 0909215 | 2010-08-08 16:38:42 +0200 | [diff] [blame] | 5298 | {(char_u *)"\316\324 \"-d\"*P", VIS_SEL}, /* SHIFT-Insert is "-d"*P */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5299 | {(char_u *)"\316\324 \022\017*", INSERT}, /* SHIFT-Insert is ^R^O* */ |
Bram Moolenaar | 371d540 | 2006-03-20 21:47:49 +0000 | [diff] [blame] | 5300 | {(char_u *)"\316\325 \"*y", VIS_SEL}, /* CTRL-Insert is "*y */ |
| 5301 | {(char_u *)"\316\327 \"*d", VIS_SEL}, /* SHIFT-Del is "*d */ |
| 5302 | {(char_u *)"\316\330 \"*d", VIS_SEL}, /* CTRL-Del is "*d */ |
| 5303 | {(char_u *)"\030 \"-d", VIS_SEL}, /* CTRL-X is "-d */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5304 | # endif |
| 5305 | # else |
| 5306 | {(char_u *)"\316\324 P", NORMAL}, /* SHIFT-Insert is P */ |
Bram Moolenaar | 371d540 | 2006-03-20 21:47:49 +0000 | [diff] [blame] | 5307 | {(char_u *)"\316\324 \"-dP", VIS_SEL}, /* SHIFT-Insert is "-dP */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5308 | {(char_u *)"\316\324 \022\017\"", INSERT}, /* SHIFT-Insert is ^R^O" */ |
Bram Moolenaar | 371d540 | 2006-03-20 21:47:49 +0000 | [diff] [blame] | 5309 | {(char_u *)"\316\325 y", VIS_SEL}, /* CTRL-Insert is y */ |
| 5310 | {(char_u *)"\316\327 d", VIS_SEL}, /* SHIFT-Del is d */ |
| 5311 | {(char_u *)"\316\330 d", VIS_SEL}, /* CTRL-Del is d */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5312 | # endif |
| 5313 | # endif |
| 5314 | #endif |
| 5315 | |
| 5316 | #if defined(MACOS) |
| 5317 | /* Use the Standard MacOS binding. */ |
| 5318 | /* paste, copy and cut */ |
| 5319 | {(char_u *)"<D-v> \"*P", NORMAL}, |
Bram Moolenaar | 371d540 | 2006-03-20 21:47:49 +0000 | [diff] [blame] | 5320 | {(char_u *)"<D-v> \"-d\"*P", VIS_SEL}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5321 | {(char_u *)"<D-v> <C-R>*", INSERT+CMDLINE}, |
Bram Moolenaar | 371d540 | 2006-03-20 21:47:49 +0000 | [diff] [blame] | 5322 | {(char_u *)"<D-c> \"*y", VIS_SEL}, |
| 5323 | {(char_u *)"<D-x> \"*d", VIS_SEL}, |
| 5324 | {(char_u *)"<Backspace> \"-d", VIS_SEL}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5325 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5326 | }; |
Bram Moolenaar | 371d540 | 2006-03-20 21:47:49 +0000 | [diff] [blame] | 5327 | |
| 5328 | # undef VIS_SEL |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5329 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5330 | |
| 5331 | /* |
| 5332 | * Set up default mappings. |
| 5333 | */ |
| 5334 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 5335 | init_mappings(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5336 | { |
Bram Moolenaar | e7fedb6 | 2015-12-31 19:07:19 +0100 | [diff] [blame] | 5337 | #if defined(MSDOS) || defined(MSWIN) ||defined(MACOS) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5338 | int i; |
| 5339 | |
| 5340 | for (i = 0; i < sizeof(initmappings) / sizeof(struct initmap); ++i) |
| 5341 | add_map(initmappings[i].arg, initmappings[i].mode); |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5342 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5343 | } |
| 5344 | |
Bram Moolenaar | e7fedb6 | 2015-12-31 19:07:19 +0100 | [diff] [blame] | 5345 | #if defined(MSDOS) || defined(MSWIN) \ |
Bram Moolenaar | 52b4b55 | 2005-03-07 23:00:57 +0000 | [diff] [blame] | 5346 | || defined(FEAT_CMDWIN) || defined(MACOS) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5347 | /* |
| 5348 | * Add a mapping "map" for mode "mode". |
| 5349 | * Need to put string in allocated memory, because do_map() will modify it. |
| 5350 | */ |
| 5351 | void |
Bram Moolenaar | 66f948e | 2016-01-30 16:39:25 +0100 | [diff] [blame] | 5352 | add_map(char_u *map, int mode) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5353 | { |
| 5354 | char_u *s; |
| 5355 | char_u *cpo_save = p_cpo; |
| 5356 | |
| 5357 | p_cpo = (char_u *)""; /* Allow <> notation */ |
| 5358 | s = vim_strsave(map); |
| 5359 | if (s != NULL) |
| 5360 | { |
| 5361 | (void)do_map(0, s, mode, FALSE); |
| 5362 | vim_free(s); |
| 5363 | } |
| 5364 | p_cpo = cpo_save; |
| 5365 | } |
Bram Moolenaar | 52b4b55 | 2005-03-07 23:00:57 +0000 | [diff] [blame] | 5366 | #endif |