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