the script used to extract a release
[nvi.git] / common / key.c
blob20be2245cc50f7922af245d9ae9dbae707c0f150
1 /*-
2 * Copyright (c) 1991, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1991, 1993, 1994, 1995, 1996
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
8 */
10 #include "config.h"
12 #ifndef lint
13 static const char sccsid[] = "$Id: key.c,v 10.46 2000/07/22 17:31:19 skimo Exp $ (Berkeley) $Date: 2000/07/22 17:31:19 $";
14 #endif /* not lint */
16 #include <sys/types.h>
17 #include <sys/queue.h>
18 #include <sys/time.h>
20 #include <bitstring.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #include <limits.h>
24 #include <locale.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
30 #include "common.h"
31 #include "../vi/vi.h"
33 static int v_event_append __P((SCR *, EVENT *));
34 static int v_event_grow __P((SCR *, int));
35 static int v_key_cmp __P((const void *, const void *));
36 static void v_keyval __P((SCR *, int, scr_keyval_t));
37 static void v_sync __P((SCR *, int));
40 * !!!
41 * Historic vi always used:
43 * ^D: autoindent deletion
44 * ^H: last character deletion
45 * ^W: last word deletion
46 * ^Q: quote the next character (if not used in flow control).
47 * ^V: quote the next character
49 * regardless of the user's choices for these characters. The user's erase
50 * and kill characters worked in addition to these characters. Nvi wires
51 * down the above characters, but in addition permits the VEOF, VERASE, VKILL
52 * and VWERASE characters described by the user's termios structure.
54 * Ex was not consistent with this scheme, as it historically ran in tty
55 * cooked mode. This meant that the scroll command and autoindent erase
56 * characters were mapped to the user's EOF character, and the character
57 * and word deletion characters were the user's tty character and word
58 * deletion characters. This implementation makes it all consistent, as
59 * described above for vi.
61 * !!!
62 * This means that all screens share a special key set.
64 KEYLIST keylist[] = {
65 {K_BACKSLASH, '\\'}, /* \ */
66 {K_CARAT, '^'}, /* ^ */
67 {K_CNTRLD, '\004'}, /* ^D */
68 {K_CNTRLR, '\022'}, /* ^R */
69 {K_CNTRLT, '\024'}, /* ^T */
70 {K_CNTRLZ, '\032'}, /* ^Z */
71 {K_COLON, ':'}, /* : */
72 {K_CR, '\r'}, /* \r */
73 {K_ESCAPE, '\033'}, /* ^[ */
74 {K_FORMFEED, '\f'}, /* \f */
75 {K_HEXCHAR, '\030'}, /* ^X */
76 {K_NL, '\n'}, /* \n */
77 {K_RIGHTBRACE, '}'}, /* } */
78 {K_RIGHTPAREN, ')'}, /* ) */
79 {K_TAB, '\t'}, /* \t */
80 {K_VERASE, '\b'}, /* \b */
81 {K_VKILL, '\025'}, /* ^U */
82 {K_VLNEXT, '\021'}, /* ^Q */
83 {K_VLNEXT, '\026'}, /* ^V */
84 {K_VWERASE, '\027'}, /* ^W */
85 {K_ZERO, '0'}, /* 0 */
87 #define ADDITIONAL_CHARACTERS 4
88 {K_NOTUSED, 0}, /* VEOF, VERASE, VKILL, VWERASE */
89 {K_NOTUSED, 0},
90 {K_NOTUSED, 0},
91 {K_NOTUSED, 0},
93 static int nkeylist =
94 (sizeof(keylist) / sizeof(keylist[0])) - ADDITIONAL_CHARACTERS;
97 * v_key_init --
98 * Initialize the special key lookup table.
100 * PUBLIC: int v_key_init __P((SCR *));
103 v_key_init(sp)
104 SCR *sp;
106 CHAR_T ch;
107 GS *gp;
108 KEYLIST *kp;
109 int cnt;
111 gp = sp->gp;
114 * XXX
115 * 8-bit only, for now. Recompilation should get you any 8-bit
116 * character set, as long as nul isn't a character.
118 (void)setlocale(LC_ALL, "");
119 #if __linux__
121 * In libc 4.5.26, setlocale(LC_ALL, ""), doesn't setup the table
122 * for ctype(3c) correctly. This bug is fixed in libc 4.6.x.
124 * This code works around this problem for libc 4.5.x users.
125 * Note that this code is harmless if you're using libc 4.6.x.
127 (void)setlocale(LC_CTYPE, "");
128 #endif
129 v_key_ilookup(sp);
131 v_keyval(sp, K_CNTRLD, KEY_VEOF);
132 v_keyval(sp, K_VERASE, KEY_VERASE);
133 v_keyval(sp, K_VKILL, KEY_VKILL);
134 v_keyval(sp, K_VWERASE, KEY_VWERASE);
136 /* Sort the special key list. */
137 qsort(keylist, nkeylist, sizeof(keylist[0]), v_key_cmp);
139 /* Initialize the fast lookup table. */
140 for (gp->max_special = 0, kp = keylist, cnt = nkeylist; cnt--; ++kp) {
141 if (gp->max_special < kp->value)
142 gp->max_special = kp->value;
143 if (kp->ch <= MAX_FAST_KEY)
144 gp->special_key[kp->ch] = kp->value;
147 /* Find a non-printable character to use as a message separator. */
148 for (ch = 1; ch <= MAX_CHAR_T; ++ch)
149 if (!ISPRINT(ch)) {
150 gp->noprint = ch;
151 break;
153 if (ch != gp->noprint) {
154 msgq(sp, M_ERR, "079|No non-printable character found");
155 return (1);
157 return (0);
161 * v_keyval --
162 * Set key values.
164 * We've left some open slots in the keylist table, and if these values exist,
165 * we put them into place. Note, they may reset (or duplicate) values already
166 * in the table, so we check for that first.
168 static void
169 v_keyval(sp, val, name)
170 SCR *sp;
171 int val;
172 scr_keyval_t name;
174 KEYLIST *kp;
175 CHAR_T ch;
176 int dne;
178 /* Get the key's value from the screen. */
179 if (sp->gp->scr_keyval(sp, name, &ch, &dne))
180 return;
181 if (dne)
182 return;
184 /* Check for duplication. */
185 for (kp = keylist; kp->value != K_NOTUSED; ++kp)
186 if (kp->ch == ch) {
187 kp->value = val;
188 return;
191 /* Add a new entry. */
192 if (kp->value == K_NOTUSED) {
193 keylist[nkeylist].ch = ch;
194 keylist[nkeylist].value = val;
195 ++nkeylist;
200 * v_key_ilookup --
201 * Build the fast-lookup key display array.
203 * PUBLIC: void v_key_ilookup __P((SCR *));
205 void
206 v_key_ilookup(sp)
207 SCR *sp;
209 UCHAR_T ch;
210 char *p, *t;
211 GS *gp;
212 size_t len;
214 for (gp = sp->gp, ch = 0;; ++ch) {
215 for (p = gp->cname[ch].name, t = v_key_name(sp, ch),
216 len = gp->cname[ch].len = sp->clen; len--;)
217 *p++ = *t++;
218 if (ch == MAX_FAST_KEY)
219 break;
224 * v_key_len --
225 * Return the length of the string that will display the key.
226 * This routine is the backup for the KEY_LEN() macro.
228 * PUBLIC: size_t v_key_len __P((SCR *, ARG_CHAR_T));
230 size_t
231 v_key_len(sp, ch)
232 SCR *sp;
233 ARG_CHAR_T ch;
235 (void)v_key_name(sp, ch);
236 return (sp->clen);
240 * v_key_name --
241 * Return the string that will display the key. This routine
242 * is the backup for the KEY_NAME() macro.
244 * PUBLIC: u_char *v_key_name __P((SCR *, ARG_CHAR_T));
246 u_char *
247 v_key_name(sp, ach)
248 SCR *sp;
249 ARG_CHAR_T ach;
251 static const char hexdigit[] = "0123456789abcdef";
252 static const char octdigit[] = "01234567";
253 CHAR_T ch, mask;
254 size_t len;
255 int cnt, shift;
256 char *chp;
258 ch = ach;
260 /* See if the character was explicitly declared printable or not. */
261 if ((chp = O_STR(sp, O_PRINT)) != NULL)
262 for (; *chp != '\0'; ++chp)
263 if (*chp == ch)
264 goto pr;
265 if ((chp = O_STR(sp, O_NOPRINT)) != NULL)
266 for (; *chp != '\0'; ++chp)
267 if (*chp == ch)
268 goto nopr;
271 * Historical (ARPA standard) mappings. Printable characters are left
272 * alone. Control characters less than 0x20 are represented as '^'
273 * followed by the character offset from the '@' character in the ASCII
274 * character set. Del (0x7f) is represented as '^' followed by '?'.
276 * XXX
277 * The following code depends on the current locale being identical to
278 * the ASCII map from 0x40 to 0x5f (since 0x1f + 0x40 == 0x5f). I'm
279 * told that this is a reasonable assumption...
281 * XXX
282 * This code will only work with CHAR_T's that are multiples of 8-bit
283 * bytes.
285 * XXX
286 * NB: There's an assumption here that all printable characters take
287 * up a single column on the screen. This is not always correct.
289 if (ISPRINT(ch)) {
290 pr: sp->cname[0] = ch;
291 len = 1;
292 goto done;
294 nopr: if (ISCNTRL(ch) && (ch < 0x20 || ch == 0x7f)) {
295 sp->cname[0] = '^';
296 sp->cname[1] = ch == 0x7f ? '?' : '@' + ch;
297 len = 2;
298 } else if (O_ISSET(sp, O_OCTAL)) {
299 #define BITS (sizeof(CHAR_T) * 8)
300 #define SHIFT (BITS - BITS % 3)
301 #define TOPMASK (BITS % 3 == 2 ? 3 : 1) << (BITS - BITS % 3)
302 sp->cname[0] = '\\';
303 sp->cname[1] = octdigit[(ch & TOPMASK) >> SHIFT];
304 shift = SHIFT - 3;
305 for (len = 2, mask = 7 << (SHIFT - 3),
306 cnt = BITS / 3; cnt-- > 0; mask >>= 3, shift -= 3)
307 sp->cname[len++] = octdigit[(ch & mask) >> shift];
308 } else {
309 sp->cname[0] = '\\';
310 sp->cname[1] = 'x';
311 for (len = 2, chp = (u_int8_t *)&ch,
312 /* sizeof(CHAR_T) conflict with MAX_CHARACTER_COLUMNS
313 * and code depends on big endian
314 * and might not be needed in the long run
316 cnt = /*sizeof(CHAR_T)*/1; cnt-- > 0; ++chp) {
317 sp->cname[len++] = hexdigit[(*chp & 0xf0) >> 4];
318 sp->cname[len++] = hexdigit[*chp & 0x0f];
321 done: sp->cname[sp->clen = len] = '\0';
322 return (sp->cname);
326 * v_key_val --
327 * Fill in the value for a key. This routine is the backup
328 * for the KEY_VAL() macro.
330 * PUBLIC: int v_key_val __P((SCR *, ARG_CHAR_T));
333 v_key_val(sp, ch)
334 SCR *sp;
335 ARG_CHAR_T ch;
337 KEYLIST k, *kp;
339 k.ch = ch;
340 kp = bsearch(&k, keylist, nkeylist, sizeof(keylist[0]), v_key_cmp);
341 return (kp == NULL ? K_NOTUSED : kp->value);
345 * v_event_push --
346 * Push events/keys onto the front of the buffer.
348 * There is a single input buffer in ex/vi. Characters are put onto the
349 * end of the buffer by the terminal input routines, and pushed onto the
350 * front of the buffer by various other functions in ex/vi. Each key has
351 * an associated flag value, which indicates if it has already been quoted,
352 * and if it is the result of a mapping or an abbreviation.
354 * PUBLIC: int v_event_push __P((SCR *, EVENT *, CHAR_T *, size_t, u_int));
357 v_event_push(sp, p_evp, p_s, nitems, flags)
358 SCR *sp;
359 EVENT *p_evp; /* Push event. */
360 CHAR_T *p_s; /* Push characters. */
361 size_t nitems; /* Number of items to push. */
362 u_int flags; /* CH_* flags. */
364 EVENT *evp;
365 GS *gp;
366 WIN *wp;
367 size_t total;
369 /* If we have room, stuff the items into the buffer. */
370 gp = sp->gp;
371 wp = sp->wp;
372 if (nitems <= wp->i_next ||
373 (wp->i_event != NULL && wp->i_cnt == 0 && nitems <= wp->i_nelem)) {
374 if (wp->i_cnt != 0)
375 wp->i_next -= nitems;
376 goto copy;
380 * If there are currently items in the queue, shift them up,
381 * leaving some extra room. Get enough space plus a little
382 * extra.
384 #define TERM_PUSH_SHIFT 30
385 total = wp->i_cnt + wp->i_next + nitems + TERM_PUSH_SHIFT;
386 if (total >= wp->i_nelem && v_event_grow(sp, MAX(total, 64)))
387 return (1);
388 if (wp->i_cnt)
389 MEMMOVE(wp->i_event + TERM_PUSH_SHIFT + nitems,
390 wp->i_event + wp->i_next, wp->i_cnt);
391 wp->i_next = TERM_PUSH_SHIFT;
393 /* Put the new items into the queue. */
394 copy: wp->i_cnt += nitems;
395 for (evp = wp->i_event + wp->i_next; nitems--; ++evp) {
396 if (p_evp != NULL)
397 *evp = *p_evp++;
398 else {
399 evp->e_event = E_CHARACTER;
400 evp->e_c = *p_s++;
401 evp->e_value = KEY_VAL(sp, evp->e_c);
402 FL_INIT(evp->e_flags, flags);
405 return (0);
409 * v_event_append --
410 * Append events onto the tail of the buffer.
412 static int
413 v_event_append(sp, argp)
414 SCR *sp;
415 EVENT *argp;
417 CHAR_T *s; /* Characters. */
418 EVENT *evp;
419 WIN *wp;
420 size_t nevents; /* Number of events. */
422 /* Grow the buffer as necessary. */
423 nevents = argp->e_event == E_STRING ? argp->e_len : 1;
424 wp = sp->wp;
425 if (wp->i_event == NULL ||
426 nevents > wp->i_nelem - (wp->i_next + wp->i_cnt))
427 v_event_grow(sp, MAX(nevents, 64));
428 evp = wp->i_event + wp->i_next + wp->i_cnt;
429 wp->i_cnt += nevents;
431 /* Transform strings of characters into single events. */
432 if (argp->e_event == E_STRING)
433 for (s = argp->e_csp; nevents--; ++evp) {
434 evp->e_event = E_CHARACTER;
435 evp->e_c = *s++;
436 evp->e_value = KEY_VAL(sp, evp->e_c);
437 evp->e_flags = 0;
439 else
440 *evp = *argp;
441 return (0);
444 /* Remove events from the queue. */
445 #define QREM(len) { \
446 if ((wp->i_cnt -= len) == 0) \
447 wp->i_next = 0; \
448 else \
449 wp->i_next += len; \
453 * v_event_get --
454 * Return the next event.
456 * !!!
457 * The flag EC_NODIGIT probably needs some explanation. First, the idea of
458 * mapping keys is that one or more keystrokes act like a function key.
459 * What's going on is that vi is reading a number, and the character following
460 * the number may or may not be mapped (EC_MAPCOMMAND). For example, if the
461 * user is entering the z command, a valid command is "z40+", and we don't want
462 * to map the '+', i.e. if '+' is mapped to "xxx", we don't want to change it
463 * into "z40xxx". However, if the user enters "35x", we want to put all of the
464 * characters through the mapping code.
466 * Historical practice is a bit muddled here. (Surprise!) It always permitted
467 * mapping digits as long as they weren't the first character of the map, e.g.
468 * ":map ^A1 xxx" was okay. It also permitted the mapping of the digits 1-9
469 * (the digit 0 was a special case as it doesn't indicate the start of a count)
470 * as the first character of the map, but then ignored those mappings. While
471 * it's probably stupid to map digits, vi isn't your mother.
473 * The way this works is that the EC_MAPNODIGIT causes term_key to return the
474 * end-of-digit without "looking" at the next character, i.e. leaving it as the
475 * user entered it. Presumably, the next term_key call will tell us how the
476 * user wants it handled.
478 * There is one more complication. Users might map keys to digits, and, as
479 * it's described above, the commands:
481 * :map g 1G
482 * d2g
484 * would return the keys "d2<end-of-digits>1G", when the user probably wanted
485 * "d21<end-of-digits>G". So, if a map starts off with a digit we continue as
486 * before, otherwise, we pretend we haven't mapped the character, and return
487 * <end-of-digits>.
489 * Now that that's out of the way, let's talk about Energizer Bunny macros.
490 * It's easy to create macros that expand to a loop, e.g. map x 3x. It's
491 * fairly easy to detect this example, because it's all internal to term_key.
492 * If we're expanding a macro and it gets big enough, at some point we can
493 * assume it's looping and kill it. The examples that are tough are the ones
494 * where the parser is involved, e.g. map x "ayyx"byy. We do an expansion
495 * on 'x', and get "ayyx"byy. We then return the first 4 characters, and then
496 * find the looping macro again. There is no way that we can detect this
497 * without doing a full parse of the command, because the character that might
498 * cause the loop (in this case 'x') may be a literal character, e.g. the map
499 * map x "ayy"xyy"byy is perfectly legal and won't cause a loop.
501 * Historic vi tried to detect looping macros by disallowing obvious cases in
502 * the map command, maps that that ended with the same letter as they started
503 * (which wrongly disallowed "map x 'x"), and detecting macros that expanded
504 * too many times before keys were returned to the command parser. It didn't
505 * get many (most?) of the tricky cases right, however, and it was certainly
506 * possible to create macros that ran forever. And, even if it did figure out
507 * what was going on, the user was usually tossed into ex mode. Finally, any
508 * changes made before vi realized that the macro was recursing were left in
509 * place. We recover gracefully, but the only recourse the user has in an
510 * infinite macro loop is to interrupt.
512 * !!!
513 * It is historic practice that mapping characters to themselves as the first
514 * part of the mapped string was legal, and did not cause infinite loops, i.e.
515 * ":map! { {^M^T" and ":map n nz." were known to work. The initial, matching
516 * characters were returned instead of being remapped.
518 * !!!
519 * It is also historic practice that the macro "map ] ]]^" caused a single ]
520 * keypress to behave as the command ]] (the ^ got the map past the vi check
521 * for "tail recursion"). Conversely, the mapping "map n nn^" went recursive.
522 * What happened was that, in the historic vi, maps were expanded as the keys
523 * were retrieved, but not all at once and not centrally. So, the keypress ]
524 * pushed ]]^ on the stack, and then the first ] from the stack was passed to
525 * the ]] command code. The ]] command then retrieved a key without entering
526 * the mapping code. This could bite us anytime a user has a map that depends
527 * on secondary keys NOT being mapped. I can't see any possible way to make
528 * this work in here without the complete abandonment of Rationality Itself.
530 * XXX
531 * The final issue is recovery. It would be possible to undo all of the work
532 * that was done by the macro if we entered a record into the log so that we
533 * knew when the macro started, and, in fact, this might be worth doing at some
534 * point. Given that this might make the log grow unacceptably (consider that
535 * cursor keys are done with maps), for now we leave any changes made in place.
537 * PUBLIC: int v_event_get __P((SCR *, EVENT *, int, u_int32_t));
540 v_event_get(sp, argp, timeout, flags)
541 SCR *sp;
542 EVENT *argp;
543 int timeout;
544 u_int32_t flags;
546 EVENT *evp, ev;
547 GS *gp;
548 SEQ *qp;
549 int init_nomap, ispartial, istimeout, remap_cnt;
550 WIN *wp;
552 gp = sp->gp;
553 wp = sp->wp;
555 /* If simply checking for interrupts, argp may be NULL. */
556 if (argp == NULL)
557 argp = &ev;
559 retry: istimeout = remap_cnt = 0;
562 * If the queue isn't empty and we're timing out for characters,
563 * return immediately.
565 if (wp->i_cnt != 0 && LF_ISSET(EC_TIMEOUT))
566 return (0);
569 * If the queue is empty, we're checking for interrupts, or we're
570 * timing out for characters, get more events.
572 if (wp->i_cnt == 0 || LF_ISSET(EC_INTERRUPT | EC_TIMEOUT)) {
574 * If we're reading new characters, check any scripting
575 * windows for input.
577 if (F_ISSET(gp, G_SCRWIN) && sscr_input(sp))
578 return (1);
579 loop: if (gp->scr_event(sp, argp,
580 LF_ISSET(EC_INTERRUPT | EC_QUOTED | EC_RAW), timeout))
581 return (1);
582 switch (argp->e_event) {
583 case E_ERR:
584 case E_SIGHUP:
585 case E_SIGTERM:
587 * Fatal conditions cause the file to be synced to
588 * disk immediately.
590 v_sync(sp, RCV_ENDSESSION | RCV_PRESERVE |
591 (argp->e_event == E_SIGTERM ? 0: RCV_EMAIL));
592 return (1);
593 case E_TIMEOUT:
594 istimeout = 1;
595 break;
596 case E_INTERRUPT:
597 /* Set the global interrupt flag. */
598 F_SET(sp->gp, G_INTERRUPTED);
601 * If the caller was interested in interrupts, return
602 * immediately.
604 if (LF_ISSET(EC_INTERRUPT))
605 return (0);
606 goto append;
607 default:
608 append: if (v_event_append(sp, argp))
609 return (1);
610 break;
615 * If the caller was only interested in interrupts or timeouts, return
616 * immediately. (We may have gotten characters, and that's okay, they
617 * were queued up for later use.)
619 if (LF_ISSET(EC_INTERRUPT | EC_TIMEOUT))
620 return (0);
622 newmap: evp = &wp->i_event[wp->i_next];
625 * If the next event in the queue isn't a character event, return
626 * it, we're done.
628 if (evp->e_event != E_CHARACTER) {
629 *argp = *evp;
630 QREM(1);
631 return (0);
635 * If the key isn't mappable because:
637 * + ... the timeout has expired
638 * + ... it's not a mappable key
639 * + ... neither the command or input map flags are set
640 * + ... there are no maps that can apply to it
642 * return it forthwith.
644 if (istimeout || FL_ISSET(evp->e_flags, CH_NOMAP) ||
645 !LF_ISSET(EC_MAPCOMMAND | EC_MAPINPUT) ||
646 evp->e_c < MAX_BIT_SEQ && !bit_test(gp->seqb, evp->e_c))
647 goto nomap;
649 /* Search the map. */
650 qp = seq_find(sp, NULL, evp, NULL, wp->i_cnt,
651 LF_ISSET(EC_MAPCOMMAND) ? SEQ_COMMAND : SEQ_INPUT, &ispartial);
654 * If get a partial match, get more characters and retry the map.
655 * If time out without further characters, return the characters
656 * unmapped.
658 * !!!
659 * <escape> characters are a problem. Cursor keys start with <escape>
660 * characters, so there's almost always a map in place that begins with
661 * an <escape> character. If we timeout <escape> keys in the same way
662 * that we timeout other keys, the user will get a noticeable pause as
663 * they enter <escape> to terminate input mode. If key timeout is set
664 * for a slow link, users will get an even longer pause. Nvi used to
665 * simply timeout <escape> characters at 1/10th of a second, but this
666 * loses over PPP links where the latency is greater than 100Ms.
668 if (ispartial) {
669 if (O_ISSET(sp, O_TIMEOUT))
670 timeout = (evp->e_value == K_ESCAPE ?
671 O_VAL(sp, O_ESCAPETIME) :
672 O_VAL(sp, O_KEYTIME)) * 100;
673 else
674 timeout = 0;
675 goto loop;
678 /* If no map, return the character. */
679 if (qp == NULL) {
680 nomap: if (!ISDIGIT(evp->e_c) && LF_ISSET(EC_MAPNODIGIT))
681 goto not_digit;
682 *argp = *evp;
683 QREM(1);
684 return (0);
688 * If looking for the end of a digit string, and the first character
689 * of the map is it, pretend we haven't seen the character.
691 if (LF_ISSET(EC_MAPNODIGIT) &&
692 qp->output != NULL && !ISDIGIT(qp->output[0])) {
693 not_digit: argp->e_c = CH_NOT_DIGIT;
694 argp->e_value = K_NOTUSED;
695 argp->e_event = E_CHARACTER;
696 FL_INIT(argp->e_flags, 0);
697 return (0);
700 /* Find out if the initial segments are identical. */
701 init_nomap = !e_memcmp(qp->output, &wp->i_event[wp->i_next], qp->ilen);
703 /* Delete the mapped characters from the queue. */
704 QREM(qp->ilen);
706 /* If keys mapped to nothing, go get more. */
707 if (qp->output == NULL)
708 goto retry;
710 /* If remapping characters... */
711 if (O_ISSET(sp, O_REMAP)) {
713 * Periodically check for interrupts. Always check the first
714 * time through, because it's possible to set up a map that
715 * will return a character every time, but will expand to more,
716 * e.g. "map! a aaaa" will always return a 'a', but we'll never
717 * get anywhere useful.
719 if ((++remap_cnt == 1 || remap_cnt % 10 == 0) &&
720 (gp->scr_event(sp, &ev,
721 EC_INTERRUPT, 0) || ev.e_event == E_INTERRUPT)) {
722 F_SET(sp->gp, G_INTERRUPTED);
723 argp->e_event = E_INTERRUPT;
724 return (0);
728 * If an initial part of the characters mapped, they are not
729 * further remapped -- return the first one. Push the rest
730 * of the characters, or all of the characters if no initial
731 * part mapped, back on the queue.
733 if (init_nomap) {
734 if (v_event_push(sp, NULL, qp->output + qp->ilen,
735 qp->olen - qp->ilen, CH_MAPPED))
736 return (1);
737 if (v_event_push(sp, NULL,
738 qp->output, qp->ilen, CH_NOMAP | CH_MAPPED))
739 return (1);
740 evp = &wp->i_event[wp->i_next];
741 goto nomap;
743 if (v_event_push(sp, NULL, qp->output, qp->olen, CH_MAPPED))
744 return (1);
745 goto newmap;
748 /* Else, push the characters on the queue and return one. */
749 if (v_event_push(sp, NULL, qp->output, qp->olen, CH_MAPPED | CH_NOMAP))
750 return (1);
752 goto nomap;
756 * v_sync --
757 * Walk the screen lists, sync'ing files to their backup copies.
759 static void
760 v_sync(sp, flags)
761 SCR *sp;
762 int flags;
764 GS *gp;
765 WIN *wp;
767 gp = sp->gp;
768 for (wp = gp->dq.cqh_first; wp != (void *)&gp->dq;
769 wp = wp->q.cqe_next)
770 for (sp = wp->scrq.cqh_first; sp != (void *)&wp->scrq;
771 sp = sp->q.cqe_next)
772 rcv_sync(sp, flags);
773 for (sp = gp->hq.cqh_first; sp != (void *)&gp->hq; sp = sp->q.cqe_next)
774 rcv_sync(sp, flags);
778 * v_event_err --
779 * Unexpected event.
781 * PUBLIC: void v_event_err __P((SCR *, EVENT *));
783 void
784 v_event_err(sp, evp)
785 SCR *sp;
786 EVENT *evp;
788 switch (evp->e_event) {
789 case E_CHARACTER:
790 msgq(sp, M_ERR, "276|Unexpected character event");
791 break;
792 case E_EOF:
793 msgq(sp, M_ERR, "277|Unexpected end-of-file event");
794 break;
795 case E_INTERRUPT:
796 msgq(sp, M_ERR, "279|Unexpected interrupt event");
797 break;
798 case E_IPCOMMAND:
799 msgq(sp, M_ERR, "318|Unexpected command or input");
800 break;
801 case E_REPAINT:
802 msgq(sp, M_ERR, "281|Unexpected repaint event");
803 break;
804 case E_STRING:
805 msgq(sp, M_ERR, "285|Unexpected string event");
806 break;
807 case E_TIMEOUT:
808 msgq(sp, M_ERR, "286|Unexpected timeout event");
809 break;
810 case E_WRESIZE:
811 msgq(sp, M_ERR, "316|Unexpected resize event");
812 break;
815 * Theoretically, none of these can occur, as they're handled at the
816 * top editor level.
818 case E_ERR:
819 case E_SIGHUP:
820 case E_SIGTERM:
821 default:
822 abort();
827 * v_event_flush --
828 * Flush any flagged keys, returning if any keys were flushed.
830 * PUBLIC: int v_event_flush __P((SCR *, u_int));
833 v_event_flush(sp, flags)
834 SCR *sp;
835 u_int flags;
837 WIN *wp;
838 int rval;
840 for (rval = 0, wp = sp->wp; wp->i_cnt != 0 &&
841 FL_ISSET(wp->i_event[wp->i_next].e_flags, flags); rval = 1)
842 QREM(1);
843 return (rval);
847 * v_event_grow --
848 * Grow the terminal queue.
850 static int
851 v_event_grow(sp, add)
852 SCR *sp;
853 int add;
855 WIN *wp;
856 size_t new_nelem, olen;
858 wp = sp->wp;
859 new_nelem = wp->i_nelem + add;
860 olen = wp->i_nelem * sizeof(wp->i_event[0]);
861 BINC_RET(sp, (char *)wp->i_event, olen, new_nelem * sizeof(wp->i_event[0]));
862 wp->i_nelem = olen / sizeof(wp->i_event[0]);
863 return (0);
867 * v_key_cmp --
868 * Compare two keys for sorting.
870 static int
871 v_key_cmp(ap, bp)
872 const void *ap, *bp;
874 return (((KEYLIST *)ap)->ch - ((KEYLIST *)bp)->ch);