Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / nvi / common / key.c
blob2fa0f62a6c3b1a966d153ce2ea97d998f76a82e1
1 /* $NetBSD: key.c,v 1.4 2009/01/02 00:32:11 tnozaki Exp $ */
3 /*-
4 * Copyright (c) 1991, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 * Copyright (c) 1991, 1993, 1994, 1995, 1996
7 * Keith Bostic. All rights reserved.
9 * See the LICENSE file for redistribution information.
12 #include "config.h"
14 #ifndef lint
15 static const char sccsid[] = "Id: key.c,v 10.48 2001/06/25 15:19:10 skimo Exp (Berkeley) Date: 2001/06/25 15:19:10";
16 #endif /* not lint */
18 #include <sys/types.h>
19 #include <sys/queue.h>
20 #include <sys/time.h>
22 #include <bitstring.h>
23 #include <ctype.h>
24 #include <errno.h>
25 #include <limits.h>
26 #include <locale.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
32 #include "common.h"
33 #include "../vi/vi.h"
35 static int v_event_append __P((SCR *, EVENT *));
36 static int v_event_grow __P((SCR *, int));
37 static int v_key_cmp __P((const void *, const void *));
38 static void v_keyval __P((SCR *, int, scr_keyval_t));
39 static void v_sync __P((SCR *, int));
42 * !!!
43 * Historic vi always used:
45 * ^D: autoindent deletion
46 * ^H: last character deletion
47 * ^W: last word deletion
48 * ^Q: quote the next character (if not used in flow control).
49 * ^V: quote the next character
51 * regardless of the user's choices for these characters. The user's erase
52 * and kill characters worked in addition to these characters. Nvi wires
53 * down the above characters, but in addition permits the VEOF, VERASE, VKILL
54 * and VWERASE characters described by the user's termios structure.
56 * Ex was not consistent with this scheme, as it historically ran in tty
57 * cooked mode. This meant that the scroll command and autoindent erase
58 * characters were mapped to the user's EOF character, and the character
59 * and word deletion characters were the user's tty character and word
60 * deletion characters. This implementation makes it all consistent, as
61 * described above for vi.
63 * !!!
64 * This means that all screens share a special key set.
66 KEYLIST keylist[] = {
67 {K_BACKSLASH, '\\'}, /* \ */
68 {K_CARAT, '^'}, /* ^ */
69 {K_CNTRLD, '\004'}, /* ^D */
70 {K_CNTRLR, '\022'}, /* ^R */
71 {K_CNTRLT, '\024'}, /* ^T */
72 {K_CNTRLZ, '\032'}, /* ^Z */
73 {K_COLON, ':'}, /* : */
74 {K_CR, '\r'}, /* \r */
75 {K_ESCAPE, '\033'}, /* ^[ */
76 {K_FORMFEED, '\f'}, /* \f */
77 {K_HEXCHAR, '\030'}, /* ^X */
78 {K_NL, '\n'}, /* \n */
79 {K_RIGHTBRACE, '}'}, /* } */
80 {K_RIGHTPAREN, ')'}, /* ) */
81 {K_TAB, '\t'}, /* \t */
82 {K_VERASE, '\b'}, /* \b */
83 {K_VKILL, '\025'}, /* ^U */
84 {K_VLNEXT, '\021'}, /* ^Q */
85 {K_VLNEXT, '\026'}, /* ^V */
86 {K_VWERASE, '\027'}, /* ^W */
87 {K_ZERO, '0'}, /* 0 */
89 #define ADDITIONAL_CHARACTERS 4
90 {K_NOTUSED, 0}, /* VEOF, VERASE, VKILL, VWERASE */
91 {K_NOTUSED, 0},
92 {K_NOTUSED, 0},
93 {K_NOTUSED, 0},
95 static int nkeylist =
96 (sizeof(keylist) / sizeof(keylist[0])) - ADDITIONAL_CHARACTERS;
99 * v_key_init --
100 * Initialize the special key lookup table.
102 * PUBLIC: int v_key_init __P((SCR *));
105 v_key_init(SCR *sp)
107 int ch;
108 GS *gp;
109 KEYLIST *kp;
110 int cnt;
112 gp = sp->gp;
115 * XXX
116 * 8-bit only, for now. Recompilation should get you any 8-bit
117 * character set, as long as nul isn't a character.
119 (void)setlocale(LC_ALL, "");
120 #if __linux__
122 * In libc 4.5.26, setlocale(LC_ALL, ""), doesn't setup the table
123 * for ctype(3c) correctly. This bug is fixed in libc 4.6.x.
125 * This code works around this problem for libc 4.5.x users.
126 * Note that this code is harmless if you're using libc 4.6.x.
128 (void)setlocale(LC_CTYPE, "");
129 #endif
130 v_key_ilookup(sp);
132 v_keyval(sp, K_CNTRLD, KEY_VEOF);
133 v_keyval(sp, K_VERASE, KEY_VERASE);
134 v_keyval(sp, K_VKILL, KEY_VKILL);
135 v_keyval(sp, K_VWERASE, KEY_VWERASE);
137 /* Sort the special key list. */
138 qsort(keylist, nkeylist, sizeof(keylist[0]), v_key_cmp);
140 /* Initialize the fast lookup table. */
141 for (gp->max_special = 0, kp = keylist, cnt = nkeylist; cnt--; ++kp) {
142 if (gp->max_special < kp->ch)
143 gp->max_special = kp->ch;
144 if (kp->ch <= MAX_FAST_KEY)
145 gp->special_key[kp->ch] = kp->value;
148 /* Find a non-printable character to use as a message separator. */
149 for (ch = 1; (unsigned)ch <= MAX_CHAR_T; ++ch)
150 if (!ISPRINT(ch)) {
151 gp->noprint = ch;
152 break;
154 if (ch != gp->noprint) {
155 msgq(sp, M_ERR, "079|No non-printable character found");
156 return (1);
158 return (0);
162 * v_keyval --
163 * Set key values.
165 * We've left some open slots in the keylist table, and if these values exist,
166 * we put them into place. Note, they may reset (or duplicate) values already
167 * in the table, so we check for that first.
169 static void
170 v_keyval(SCR *sp, int val, scr_keyval_t name)
172 KEYLIST *kp;
173 CHAR_T ch;
174 int dne;
176 /* Get the key's value from the screen. */
177 if (sp->gp->scr_keyval(sp, name, &ch, &dne))
178 return;
179 if (dne)
180 return;
182 /* Check for duplication. */
183 for (kp = keylist; kp->value != K_NOTUSED; ++kp)
184 if (kp->ch == ch) {
185 kp->value = val;
186 return;
189 /* Add a new entry. */
190 if (kp->value == K_NOTUSED) {
191 keylist[nkeylist].ch = ch;
192 keylist[nkeylist].value = val;
193 ++nkeylist;
198 * v_key_ilookup --
199 * Build the fast-lookup key display array.
201 * PUBLIC: void v_key_ilookup __P((SCR *));
203 void
204 v_key_ilookup(SCR *sp)
206 UCHAR_T ch;
207 unsigned char *p, *t;
208 GS *gp;
209 size_t len;
211 for (gp = sp->gp, ch = 0;; ++ch) {
212 for (p = gp->cname[ch].name, t = v_key_name(sp, ch),
213 len = gp->cname[ch].len = sp->clen; len--;)
214 *p++ = *t++;
215 if (ch == MAX_FAST_KEY)
216 break;
221 * v_key_len --
222 * Return the length of the string that will display the key.
223 * This routine is the backup for the KEY_LEN() macro.
225 * PUBLIC: size_t v_key_len __P((SCR *, ARG_CHAR_T));
227 size_t
228 v_key_len(SCR *sp, ARG_CHAR_T ch)
230 (void)v_key_name(sp, ch);
231 return (sp->clen);
235 * v_key_name --
236 * Return the string that will display the key. This routine
237 * is the backup for the KEY_NAME() macro.
239 * PUBLIC: u_char *v_key_name __P((SCR *, ARG_CHAR_T));
241 u_char *
242 v_key_name(SCR *sp, ARG_CHAR_T ach)
244 static const char hexdigit[] = "0123456789abcdef";
245 static const char octdigit[] = "01234567";
246 CHAR_T ch, mask;
247 size_t len;
248 int cnt, shift;
249 const char *chp;
251 ch = ach;
253 /* See if the character was explicitly declared printable or not. */
254 if ((chp = O_STR(sp, O_PRINT)) != NULL)
255 for (; *chp != '\0'; ++chp)
256 if (*chp == ch)
257 goto pr;
258 if ((chp = O_STR(sp, O_NOPRINT)) != NULL)
259 for (; *chp != '\0'; ++chp)
260 if (*chp == ch)
261 goto nopr;
264 * Historical (ARPA standard) mappings. Printable characters are left
265 * alone. Control characters less than 0x20 are represented as '^'
266 * followed by the character offset from the '@' character in the ASCII
267 * character set. Del (0x7f) is represented as '^' followed by '?'.
269 * XXX
270 * The following code depends on the current locale being identical to
271 * the ASCII map from 0x40 to 0x5f (since 0x1f + 0x40 == 0x5f). I'm
272 * told that this is a reasonable assumption...
274 * XXX
275 * This code will only work with CHAR_T's that are multiples of 8-bit
276 * bytes.
278 * XXX
279 * NB: There's an assumption here that all printable characters take
280 * up a single column on the screen. This is not always correct.
282 if (ISPRINT(ch)) {
283 pr: sp->cname[0] = ch;
284 len = 1;
285 goto done;
287 nopr: if (ISCNTRL(ch) && (ch < 0x20 || ch == 0x7f)) {
288 sp->cname[0] = '^';
289 sp->cname[1] = ch == 0x7f ? '?' : '@' + ch;
290 len = 2;
291 } else if (O_ISSET(sp, O_OCTAL)) {
292 #define BITS (sizeof(CHAR_T) * 8)
293 #define SHIFT (BITS - BITS % 3)
294 #define TOPMASK (BITS % 3 == 2 ? 3 : 1) << (BITS - BITS % 3)
295 sp->cname[0] = '\\';
296 sp->cname[1] = octdigit[(ch & TOPMASK) >> SHIFT];
297 shift = SHIFT - 3;
298 for (len = 2, mask = 7 << (SHIFT - 3),
299 cnt = BITS / 3; cnt-- > 0; mask >>= 3, shift -= 3)
300 sp->cname[len++] = octdigit[(ch & mask) >> shift];
301 } else {
302 sp->cname[0] = '\\';
303 sp->cname[1] = 'x';
304 for (len = 2, chp = (char *)&ch,
305 /* sizeof(CHAR_T) conflict with MAX_CHARACTER_COLUMNS
306 * and code depends on big endian
307 * and might not be needed in the long run
309 cnt = /*sizeof(CHAR_T)*/1; cnt-- > 0; ++chp) {
310 sp->cname[len++] = hexdigit[(*chp & 0xf0) >> 4];
311 sp->cname[len++] = hexdigit[*chp & 0x0f];
314 done: sp->cname[sp->clen = len] = '\0';
315 return (sp->cname);
319 * v_key_val --
320 * Fill in the value for a key. This routine is the backup
321 * for the KEY_VAL() macro.
323 * PUBLIC: int v_key_val __P((SCR *, ARG_CHAR_T));
326 v_key_val(SCR *sp, ARG_CHAR_T ch)
328 KEYLIST k, *kp;
330 k.ch = ch;
331 kp = bsearch(&k, keylist, nkeylist, sizeof(keylist[0]), v_key_cmp);
332 return (kp == NULL ? K_NOTUSED : kp->value);
336 * v_event_push --
337 * Push events/keys onto the front of the buffer.
339 * There is a single input buffer in ex/vi. Characters are put onto the
340 * end of the buffer by the terminal input routines, and pushed onto the
341 * front of the buffer by various other functions in ex/vi. Each key has
342 * an associated flag value, which indicates if it has already been quoted,
343 * and if it is the result of a mapping or an abbreviation.
345 * PUBLIC: int v_event_push __P((SCR *, EVENT *, const CHAR_T *, size_t, u_int));
348 v_event_push(SCR *sp, EVENT *p_evp, const CHAR_T *p_s, size_t nitems, u_int flags)
350 /* Push event. */
351 /* Push characters. */
352 /* Number of items to push. */
353 /* CH_* flags. */
355 EVENT *evp;
356 GS *gp;
357 WIN *wp;
358 size_t total;
360 /* If we have room, stuff the items into the buffer. */
361 gp = sp->gp;
362 wp = sp->wp;
363 if (nitems <= wp->i_next ||
364 (wp->i_event != NULL && wp->i_cnt == 0 && nitems <= wp->i_nelem)) {
365 if (wp->i_cnt != 0)
366 wp->i_next -= nitems;
367 goto copy;
371 * If there are currently items in the queue, shift them up,
372 * leaving some extra room. Get enough space plus a little
373 * extra.
375 #define TERM_PUSH_SHIFT 30
376 total = wp->i_cnt + wp->i_next + nitems + TERM_PUSH_SHIFT;
377 if (total >= wp->i_nelem && v_event_grow(sp, MAX(total, 64)))
378 return (1);
379 if (wp->i_cnt)
380 MEMMOVE(wp->i_event + TERM_PUSH_SHIFT + nitems,
381 wp->i_event + wp->i_next, wp->i_cnt);
382 wp->i_next = TERM_PUSH_SHIFT;
384 /* Put the new items into the queue. */
385 copy: wp->i_cnt += nitems;
386 for (evp = wp->i_event + wp->i_next; nitems--; ++evp) {
387 if (p_evp != NULL)
388 *evp = *p_evp++;
389 else {
390 evp->e_event = E_CHARACTER;
391 evp->e_c = *p_s++;
392 evp->e_value = KEY_VAL(sp, evp->e_c);
393 FL_INIT(evp->e_flags, flags);
396 return (0);
400 * v_event_append --
401 * Append events onto the tail of the buffer.
403 static int
404 v_event_append(SCR *sp, EVENT *argp)
406 CHAR_T *s; /* Characters. */
407 EVENT *evp;
408 WIN *wp;
409 size_t nevents; /* Number of events. */
411 /* Grow the buffer as necessary. */
412 nevents = argp->e_event == E_STRING ? argp->e_len : 1;
413 wp = sp->wp;
414 if (wp->i_event == NULL ||
415 nevents > wp->i_nelem - (wp->i_next + wp->i_cnt))
416 v_event_grow(sp, MAX(nevents, 64));
417 evp = wp->i_event + wp->i_next + wp->i_cnt;
418 wp->i_cnt += nevents;
420 /* Transform strings of characters into single events. */
421 if (argp->e_event == E_STRING)
422 for (s = argp->e_csp; nevents--; ++evp) {
423 evp->e_event = E_CHARACTER;
424 evp->e_c = *s++;
425 evp->e_value = KEY_VAL(sp, evp->e_c);
426 evp->e_flags = 0;
428 else
429 *evp = *argp;
430 return (0);
433 /* Remove events from the queue. */
434 #define QREM(len) { \
435 if ((wp->i_cnt -= len) == 0) \
436 wp->i_next = 0; \
437 else \
438 wp->i_next += len; \
442 * v_event_get --
443 * Return the next event.
445 * !!!
446 * The flag EC_NODIGIT probably needs some explanation. First, the idea of
447 * mapping keys is that one or more keystrokes act like a function key.
448 * What's going on is that vi is reading a number, and the character following
449 * the number may or may not be mapped (EC_MAPCOMMAND). For example, if the
450 * user is entering the z command, a valid command is "z40+", and we don't want
451 * to map the '+', i.e. if '+' is mapped to "xxx", we don't want to change it
452 * into "z40xxx". However, if the user enters "35x", we want to put all of the
453 * characters through the mapping code.
455 * Historical practice is a bit muddled here. (Surprise!) It always permitted
456 * mapping digits as long as they weren't the first character of the map, e.g.
457 * ":map ^A1 xxx" was okay. It also permitted the mapping of the digits 1-9
458 * (the digit 0 was a special case as it doesn't indicate the start of a count)
459 * as the first character of the map, but then ignored those mappings. While
460 * it's probably stupid to map digits, vi isn't your mother.
462 * The way this works is that the EC_MAPNODIGIT causes term_key to return the
463 * end-of-digit without "looking" at the next character, i.e. leaving it as the
464 * user entered it. Presumably, the next term_key call will tell us how the
465 * user wants it handled.
467 * There is one more complication. Users might map keys to digits, and, as
468 * it's described above, the commands:
470 * :map g 1G
471 * d2g
473 * would return the keys "d2<end-of-digits>1G", when the user probably wanted
474 * "d21<end-of-digits>G". So, if a map starts off with a digit we continue as
475 * before, otherwise, we pretend we haven't mapped the character, and return
476 * <end-of-digits>.
478 * Now that that's out of the way, let's talk about Energizer Bunny macros.
479 * It's easy to create macros that expand to a loop, e.g. map x 3x. It's
480 * fairly easy to detect this example, because it's all internal to term_key.
481 * If we're expanding a macro and it gets big enough, at some point we can
482 * assume it's looping and kill it. The examples that are tough are the ones
483 * where the parser is involved, e.g. map x "ayyx"byy. We do an expansion
484 * on 'x', and get "ayyx"byy. We then return the first 4 characters, and then
485 * find the looping macro again. There is no way that we can detect this
486 * without doing a full parse of the command, because the character that might
487 * cause the loop (in this case 'x') may be a literal character, e.g. the map
488 * map x "ayy"xyy"byy is perfectly legal and won't cause a loop.
490 * Historic vi tried to detect looping macros by disallowing obvious cases in
491 * the map command, maps that that ended with the same letter as they started
492 * (which wrongly disallowed "map x 'x"), and detecting macros that expanded
493 * too many times before keys were returned to the command parser. It didn't
494 * get many (most?) of the tricky cases right, however, and it was certainly
495 * possible to create macros that ran forever. And, even if it did figure out
496 * what was going on, the user was usually tossed into ex mode. Finally, any
497 * changes made before vi realized that the macro was recursing were left in
498 * place. We recover gracefully, but the only recourse the user has in an
499 * infinite macro loop is to interrupt.
501 * !!!
502 * It is historic practice that mapping characters to themselves as the first
503 * part of the mapped string was legal, and did not cause infinite loops, i.e.
504 * ":map! { {^M^T" and ":map n nz." were known to work. The initial, matching
505 * characters were returned instead of being remapped.
507 * !!!
508 * It is also historic practice that the macro "map ] ]]^" caused a single ]
509 * keypress to behave as the command ]] (the ^ got the map past the vi check
510 * for "tail recursion"). Conversely, the mapping "map n nn^" went recursive.
511 * What happened was that, in the historic vi, maps were expanded as the keys
512 * were retrieved, but not all at once and not centrally. So, the keypress ]
513 * pushed ]]^ on the stack, and then the first ] from the stack was passed to
514 * the ]] command code. The ]] command then retrieved a key without entering
515 * the mapping code. This could bite us anytime a user has a map that depends
516 * on secondary keys NOT being mapped. I can't see any possible way to make
517 * this work in here without the complete abandonment of Rationality Itself.
519 * XXX
520 * The final issue is recovery. It would be possible to undo all of the work
521 * that was done by the macro if we entered a record into the log so that we
522 * knew when the macro started, and, in fact, this might be worth doing at some
523 * point. Given that this might make the log grow unacceptably (consider that
524 * cursor keys are done with maps), for now we leave any changes made in place.
526 * PUBLIC: int v_event_get __P((SCR *, EVENT *, int, u_int32_t));
529 v_event_get(SCR *sp, EVENT *argp, int timeout, u_int32_t flags)
531 EVENT *evp, ev;
532 GS *gp;
533 SEQ *qp;
534 int init_nomap, ispartial, istimeout, remap_cnt;
535 WIN *wp;
537 gp = sp->gp;
538 wp = sp->wp;
540 /* If simply checking for interrupts, argp may be NULL. */
541 if (argp == NULL)
542 argp = &ev;
544 retry: istimeout = remap_cnt = 0;
547 * If the queue isn't empty and we're timing out for characters,
548 * return immediately.
550 if (wp->i_cnt != 0 && LF_ISSET(EC_TIMEOUT))
551 return (0);
554 * If the queue is empty, we're checking for interrupts, or we're
555 * timing out for characters, get more events.
557 if (wp->i_cnt == 0 || LF_ISSET(EC_INTERRUPT | EC_TIMEOUT)) {
559 * If we're reading new characters, check any scripting
560 * windows for input.
562 if (F_ISSET(gp, G_SCRWIN) && sscr_input(sp))
563 return (1);
564 loop: if (gp->scr_event(sp, argp,
565 LF_ISSET(EC_INTERRUPT | EC_QUOTED | EC_RAW), timeout))
566 return (1);
567 switch (argp->e_event) {
568 case E_ERR:
569 case E_SIGHUP:
570 case E_SIGTERM:
572 * Fatal conditions cause the file to be synced to
573 * disk immediately.
575 v_sync(sp, RCV_ENDSESSION | RCV_PRESERVE |
576 (argp->e_event == E_SIGTERM ? 0: RCV_EMAIL));
577 return (1);
578 case E_TIMEOUT:
579 istimeout = 1;
580 break;
581 case E_INTERRUPT:
582 /* Set the global interrupt flag. */
583 F_SET(sp->gp, G_INTERRUPTED);
586 * If the caller was interested in interrupts, return
587 * immediately.
589 if (LF_ISSET(EC_INTERRUPT))
590 return (0);
591 goto append;
592 default:
593 append: if (v_event_append(sp, argp))
594 return (1);
595 break;
600 * If the caller was only interested in interrupts or timeouts, return
601 * immediately. (We may have gotten characters, and that's okay, they
602 * were queued up for later use.)
604 if (LF_ISSET(EC_INTERRUPT | EC_TIMEOUT))
605 return (0);
607 newmap: evp = &wp->i_event[wp->i_next];
610 * If the next event in the queue isn't a character event, return
611 * it, we're done.
613 if (evp->e_event != E_CHARACTER) {
614 *argp = *evp;
615 QREM(1);
616 return (0);
620 * If the key isn't mappable because:
622 * + ... the timeout has expired
623 * + ... it's not a mappable key
624 * + ... neither the command or input map flags are set
625 * + ... there are no maps that can apply to it
627 * return it forthwith.
629 if (istimeout || FL_ISSET(evp->e_flags, CH_NOMAP) ||
630 !LF_ISSET(EC_MAPCOMMAND | EC_MAPINPUT) ||
631 ((UCHAR_T)evp->e_c < MAX_BIT_SEQ &&
632 !bit_test(gp->seqb, (UCHAR_T)evp->e_c)))
633 goto nomap;
635 /* Search the map. */
636 qp = seq_find(sp, NULL, evp, NULL, wp->i_cnt,
637 LF_ISSET(EC_MAPCOMMAND) ? SEQ_COMMAND : SEQ_INPUT, &ispartial);
640 * If get a partial match, get more characters and retry the map.
641 * If time out without further characters, return the characters
642 * unmapped.
644 * !!!
645 * <escape> characters are a problem. Cursor keys start with <escape>
646 * characters, so there's almost always a map in place that begins with
647 * an <escape> character. If we timeout <escape> keys in the same way
648 * that we timeout other keys, the user will get a noticeable pause as
649 * they enter <escape> to terminate input mode. If key timeout is set
650 * for a slow link, users will get an even longer pause. Nvi used to
651 * simply timeout <escape> characters at 1/10th of a second, but this
652 * loses over PPP links where the latency is greater than 100Ms.
654 if (ispartial) {
655 if (O_ISSET(sp, O_TIMEOUT))
656 timeout = (evp->e_value == K_ESCAPE ?
657 O_VAL(sp, O_ESCAPETIME) :
658 O_VAL(sp, O_KEYTIME)) * 100;
659 else
660 timeout = 0;
661 goto loop;
664 /* If no map, return the character. */
665 if (qp == NULL) {
666 nomap: if (!ISDIGIT(evp->e_c) && LF_ISSET(EC_MAPNODIGIT))
667 goto not_digit;
668 *argp = *evp;
669 QREM(1);
670 return (0);
674 * If looking for the end of a digit string, and the first character
675 * of the map is it, pretend we haven't seen the character.
677 if (LF_ISSET(EC_MAPNODIGIT) &&
678 qp->output != NULL && !ISDIGIT(qp->output[0])) {
679 not_digit: argp->e_c = CH_NOT_DIGIT;
680 argp->e_value = K_NOTUSED;
681 argp->e_event = E_CHARACTER;
682 FL_INIT(argp->e_flags, 0);
683 return (0);
686 /* Find out if the initial segments are identical. */
687 init_nomap = !e_memcmp(qp->output, &wp->i_event[wp->i_next], qp->ilen);
689 /* Delete the mapped characters from the queue. */
690 QREM(qp->ilen);
692 /* If keys mapped to nothing, go get more. */
693 if (qp->output == NULL)
694 goto retry;
696 /* If remapping characters... */
697 if (O_ISSET(sp, O_REMAP)) {
699 * Periodically check for interrupts. Always check the first
700 * time through, because it's possible to set up a map that
701 * will return a character every time, but will expand to more,
702 * e.g. "map! a aaaa" will always return a 'a', but we'll never
703 * get anywhere useful.
705 if ((++remap_cnt == 1 || remap_cnt % 10 == 0) &&
706 (gp->scr_event(sp, &ev,
707 EC_INTERRUPT, 0) || ev.e_event == E_INTERRUPT)) {
708 F_SET(sp->gp, G_INTERRUPTED);
709 argp->e_event = E_INTERRUPT;
710 return (0);
714 * If an initial part of the characters mapped, they are not
715 * further remapped -- return the first one. Push the rest
716 * of the characters, or all of the characters if no initial
717 * part mapped, back on the queue.
719 if (init_nomap) {
720 if (v_event_push(sp, NULL, qp->output + qp->ilen,
721 qp->olen - qp->ilen, CH_MAPPED))
722 return (1);
723 if (v_event_push(sp, NULL,
724 qp->output, qp->ilen, CH_NOMAP | CH_MAPPED))
725 return (1);
726 evp = &wp->i_event[wp->i_next];
727 goto nomap;
729 if (v_event_push(sp, NULL, qp->output, qp->olen, CH_MAPPED))
730 return (1);
731 goto newmap;
734 /* Else, push the characters on the queue and return one. */
735 if (v_event_push(sp, NULL, qp->output, qp->olen, CH_MAPPED | CH_NOMAP))
736 return (1);
738 goto nomap;
742 * v_sync --
743 * Walk the screen lists, sync'ing files to their backup copies.
745 static void
746 v_sync(SCR *sp, int flags)
748 GS *gp;
749 WIN *wp;
751 gp = sp->gp;
752 for (wp = gp->dq.cqh_first; wp != (void *)&gp->dq;
753 wp = wp->q.cqe_next)
754 for (sp = wp->scrq.cqh_first; sp != (void *)&wp->scrq;
755 sp = sp->q.cqe_next)
756 rcv_sync(sp, flags);
757 for (sp = gp->hq.cqh_first; sp != (void *)&gp->hq; sp = sp->q.cqe_next)
758 rcv_sync(sp, flags);
762 * v_event_err --
763 * Unexpected event.
765 * PUBLIC: void v_event_err __P((SCR *, EVENT *));
767 void
768 v_event_err(SCR *sp, EVENT *evp)
770 switch (evp->e_event) {
771 case E_CHARACTER:
772 msgq(sp, M_ERR, "276|Unexpected character event");
773 break;
774 case E_EOF:
775 msgq(sp, M_ERR, "277|Unexpected end-of-file event");
776 break;
777 case E_INTERRUPT:
778 msgq(sp, M_ERR, "279|Unexpected interrupt event");
779 break;
780 case E_IPCOMMAND:
781 msgq(sp, M_ERR, "318|Unexpected command or input");
782 break;
783 case E_REPAINT:
784 msgq(sp, M_ERR, "281|Unexpected repaint event");
785 break;
786 case E_STRING:
787 msgq(sp, M_ERR, "285|Unexpected string event");
788 break;
789 case E_TIMEOUT:
790 msgq(sp, M_ERR, "286|Unexpected timeout event");
791 break;
792 case E_WRESIZE:
793 msgq(sp, M_ERR, "316|Unexpected resize event");
794 break;
797 * Theoretically, none of these can occur, as they're handled at the
798 * top editor level.
800 case E_ERR:
801 case E_SIGHUP:
802 case E_SIGTERM:
803 default:
804 abort();
809 * v_event_flush --
810 * Flush any flagged keys, returning if any keys were flushed.
812 * PUBLIC: int v_event_flush __P((SCR *, u_int));
815 v_event_flush(SCR *sp, u_int flags)
817 WIN *wp;
818 int rval;
820 for (rval = 0, wp = sp->wp; wp->i_cnt != 0 &&
821 FL_ISSET(wp->i_event[wp->i_next].e_flags, flags); rval = 1)
822 QREM(1);
823 return (rval);
827 * v_event_grow --
828 * Grow the terminal queue.
830 static int
831 v_event_grow(SCR *sp, int add)
833 WIN *wp;
834 size_t new_nelem, olen;
836 wp = sp->wp;
837 new_nelem = wp->i_nelem + add;
838 olen = wp->i_nelem * sizeof(wp->i_event[0]);
839 BINC_RET(sp, EVENT, wp->i_event, olen, new_nelem * sizeof(EVENT));
840 wp->i_nelem = olen / sizeof(wp->i_event[0]);
841 return (0);
845 * v_key_cmp --
846 * Compare two keys for sorting.
848 static int
849 v_key_cmp(const void *ap, const void *bp)
851 return (((const KEYLIST *)ap)->ch - ((const KEYLIST *)bp)->ch);