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