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