dot commands that use character searches don't reset the search
[nvi.git] / common / key.c
blob1f91ac74803f700e8c0b62cdae07465b54411b08
1 /*-
2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * %sccs.include.redist.c%
6 */
8 #ifndef lint
9 static char sccsid[] = "$Id: key.c,v 8.35 1993/12/16 14:42:53 bostic Exp $ (Berkeley) $Date: 1993/12/16 14:42:53 $";
10 #endif /* not lint */
12 #include <sys/types.h>
13 #include <sys/time.h>
15 #include <ctype.h>
16 #include <curses.h>
17 #include <errno.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <unistd.h>
22 #include "vi.h"
23 #include "seq.h"
25 static int keycmp __P((const void *, const void *));
28 * If we're reading less than 20 characters, up the size of the tty buffer.
29 * This shouldn't ever happen, other than the first time through, but it's
30 * possible if a map is large enough.
32 #define term_read_grow(sp, tty) \
33 (tty)->len - (tty)->cnt >= 20 ? 0 : __term_read_grow(sp, tty)
34 static int __term_read_grow __P((SCR *, IBUF *));
37 * XXX
38 * THIS REQUIRES THAT ALL SCREENS SHARE A TERMINAL TYPE.
40 typedef struct _tklist {
41 char *ts; /* Key's termcap string. */
42 char *output; /* Corresponding vi command. */
43 char *name; /* Name. */
44 } TKLIST;
45 static TKLIST const tklist[] = {
46 {"kA", "O", "insert line"},
47 {"kD", "x", "delete character"},
48 {"kd", "j", "cursor down"},
49 {"kE", "D", "delete to eol"},
50 {"kF", "\004", "scroll down"},
51 {"kH", "$", "go to eol"},
52 {"kh", "^", "go to sol"},
53 {"kI", "i", "insert at cursor"},
54 {"kL", "dd", "delete line"},
55 {"kl", "h", "cursor left"},
56 {"kN", "\006", "page down"},
57 {"kP", "\002", "page up"},
58 {"kR", "\025", "scroll up"},
59 {"kS", "dG", "delete to end of screen"},
60 {"kr", "l", "cursor right"},
61 {"ku", "k", "cursor up"},
62 {NULL},
66 * XXX
67 * THIS REQUIRES THAT ALL SCREENS SHARE A SPECIAL KEY SET.
69 typedef struct _keylist {
70 u_char value; /* Special value. */
71 CHAR_T ch; /* Key. */
72 } KEYLIST;
73 static KEYLIST keylist[] = {
74 {K_CARAT, '^'},
75 {K_CNTRLR, '\022'},
76 {K_CNTRLT, '\024'},
77 {K_CNTRLZ, '\032'},
78 {K_COLON, ':'},
79 {K_CR, '\r'},
80 {K_ESCAPE, '\033'},
81 {K_FORMFEED, '\f'},
82 {K_NL, '\n'},
83 {K_RIGHTBRACE, '}'},
84 {K_RIGHTPAREN, ')'},
85 {K_TAB, '\t'},
86 {K_VEOF, '\004'},
87 {K_VERASE, '\b'},
88 {K_VKILL, '\025'},
89 {K_VLNEXT, '\026'},
90 {K_VWERASE, '\027'},
91 {K_ZERO, '0'},
95 * term_init --
96 * Initialize the special key lookup table, and the special keys
97 * defined by the terminal's termcap entry.
99 int
100 term_init(sp)
101 SCR *sp;
103 extern CHNAME const asciiname[]; /* XXX */
104 GS *gp;
105 KEYLIST *kp;
106 TKLIST const *tkp;
107 cc_t ch;
108 int cnt;
109 char *sbp, *t, buf[2 * 1024], sbuf[128];
112 * XXX
113 * 8-bit, ASCII only, for now. Recompilation should get you
114 * any 8-bit character set, as long as nul isn't a character.
116 gp = sp->gp;
117 gp->cname = asciiname; /* XXX */
119 /* Set keys found in the termios structure. */
120 #define TERMSET(name, val) { \
121 if ((ch = gp->original_termios.c_cc[name]) != _POSIX_VDISABLE) \
122 for (kp = keylist;; ++kp) \
123 if (kp->value == (val)) { \
124 kp->ch = ch; \
125 break; \
129 * VEOF, VERASE, VKILL are required by POSIX 1003.1-1990,
130 * VWERASE is a 4.4BSD extension.
132 #ifdef VEOF
133 TERMSET(VEOF, K_VEOF);
134 #endif
135 #ifdef VERASE
136 TERMSET(VERASE, K_VERASE);
137 #endif
138 #ifdef VKILL
139 TERMSET(VKILL, K_VKILL);
140 #endif
141 #ifdef VWERASE
142 TERMSET(VWERASE, K_VWERASE);
143 #endif
145 /* Sort the special key list. */
146 qsort(keylist,
147 sizeof(keylist) / sizeof(keylist[0]), sizeof(keylist[0]), keycmp);
149 /* Initialize the fast lookup table. */
150 CALLOC_RET(sp,
151 gp->special_key, u_char *, MAX_FAST_KEY + 1, sizeof(u_char));
152 for (gp->max_special = 0, kp = keylist,
153 cnt = sizeof(keylist) / sizeof(keylist[0]); cnt--; ++kp) {
154 if (gp->max_special < kp->value)
155 gp->max_special = kp->value;
156 if (kp->ch <= MAX_FAST_KEY)
157 gp->special_key[kp->ch] = kp->value;
160 /* Set key sequences found in the termcap entry. */
161 switch (tgetent(buf, O_STR(sp, O_TERM))) {
162 case -1:
163 msgq(sp, M_ERR,
164 "tgetent: %s: %s.", O_STR(sp, O_TERM), strerror(errno));
165 return (0);
166 case 0:
167 msgq(sp, M_ERR,
168 "%s: unknown terminal type.", O_STR(sp, O_TERM));
169 return (0);
172 for (tkp = tklist; tkp->name != NULL; ++tkp) {
173 sbp = sbuf;
174 if ((t = tgetstr(tkp->ts, &sbp)) == NULL)
175 continue;
176 if (seq_set(sp, tkp->name, strlen(tkp->name), t, strlen(t),
177 tkp->output, strlen(tkp->output), SEQ_COMMAND, 0))
178 return (1);
180 return (0);
184 * term_push --
185 * Push keys onto the front of a buffer.
187 * There is a single input buffer in ex/vi. Characters are read onto the
188 * end of the buffer by the terminal input routines, and pushed onto the
189 * front of the buffer various other functions in ex/vi. Each key has an
190 * associated flag value, which indicates if it has already been quoted,
191 * if it is the result of a mapping or an abbreviation.
194 term_push(sp, s, len, cmap, flags)
195 SCR *sp;
196 CHAR_T *s; /* Characters. */
197 size_t len; /* Number of chars. */
198 u_int cmap; /* Map count. */
199 u_int flags; /* CH_* flags. */
201 IBUF *tty;
202 size_t nlen;
204 /* If we have room, stuff the keys into the buffer. */
205 tty = sp->gp->tty;
206 if (len <= tty->next ||
207 (tty->ch != NULL && tty->cnt == 0 && len <= tty->len)) {
208 if (tty->cnt != 0)
209 tty->next -= len;
210 tty->cnt += len;
211 memmove(tty->ch + tty->next, s, len * sizeof(CHAR_T));
212 memset(tty->chf + tty->next, flags, len);
213 memset(tty->cmap + tty->next, cmap, len);
214 return (0);
217 /* Get enough space plus a little extra. */
218 nlen = tty->cnt + len;
219 if (nlen > tty->len) {
220 size_t olen;
222 nlen += 64;
223 olen = tty->len;
224 BINC_RET(sp, tty->ch, olen, nlen * sizeof(tty->ch[0]));
225 olen = tty->len;
226 BINC_RET(sp, tty->chf, olen, nlen * sizeof(tty->chf[0]));
227 BINC_RET(sp, tty->cmap, tty->len, nlen * sizeof(tty->cmap[0]));
231 * If there are currently characters in the queue, shift them up,
232 * leaving some extra room.
234 #define TERM_PUSH_SHIFT 30
235 if (tty->cnt) {
236 memmove(tty->ch + TERM_PUSH_SHIFT + len,
237 tty->ch + tty->next, tty->cnt * sizeof(tty->ch[0]));
238 memmove(tty->chf + TERM_PUSH_SHIFT + len,
239 tty->chf + tty->next, tty->cnt * sizeof(tty->chf[0]));
240 memmove(tty->cmap + TERM_PUSH_SHIFT + len,
241 tty->cmap + tty->next, tty->cnt * sizeof(tty->cmap[0]));
244 /* Put the new characters into the queue. */
245 tty->next = TERM_PUSH_SHIFT;
246 tty->cnt += len;
247 memmove(tty->ch + TERM_PUSH_SHIFT, s, len * sizeof(tty->ch[0]));
248 memset(tty->chf + TERM_PUSH_SHIFT, flags, len * sizeof(tty->chf[0]));
249 memset(tty->cmap + TERM_PUSH_SHIFT, cmap, len * sizeof(tty->cmap[0]));
250 return (0);
254 * Remove characters from the queue, simultaneously clearing the
255 * flag and map counts.
257 #define QREM_HEAD(q, len) { \
258 size_t __off = (q)->next; \
259 if (len == 1) { \
260 tty->chf[__off] = 0; \
261 tty->cmap[__off] = 0; \
262 } else { \
263 memset(tty->chf + __off, 0, len); \
264 memset(tty->cmap + __off, 0, len); \
266 if (((q)->cnt -= len) == 0) \
267 (q)->next = 0; \
268 else \
269 (q)->next += len; \
271 #define QREM_TAIL(q, len) { \
272 size_t __off = (q)->next + (q)->cnt - 1; \
273 if (len == 1) { \
274 tty->chf[__off] = 0; \
275 tty->cmap[__off] = 0; \
276 } else { \
277 memset(tty->chf + __off, 0, len); \
278 memset(tty->cmap + __off, 0, len); \
280 if (((q)->cnt -= len) == 0) \
281 (q)->next = 0; \
285 * term_key --
286 * Get the next key.
288 * !!!
289 * The flag TXT_MAPNODIGIT probably needs some explanation. First, the idea
290 * of mapping keys is that one or more keystrokes act like a function key.
291 * What's going on is that vi is reading a number, and the character following
292 * the number may or may not be mapped (TXT_MAPCOMMAND). For example, if the
293 * user is entering the z command, a valid command is "z40+", and we don't want
294 * to map the '+', i.e. if '+' is mapped to "xxx", we don't want to change it
295 * into "z40xxx". However, if the user enters "35x", we want to put all of the
296 * characters through the mapping code.
298 * Historical practice is a bit muddled here. (Surprise!) It always permitted
299 * mapping digits as long as they weren't the first character of the map, e.g.
300 * ":map ^A1 xxx" was okay. It also permitted the mapping of the digits 1-9
301 * (the digit 0 was a special case as it doesn't indicate the start of a count)
302 * as the first character of the map, but then ignored those mappings. While
303 * it's probably stupid to map digits, vi isn't your mother.
305 * The way this works is that the TXT_MAPNODIGIT causes term_key to return the
306 * end-of-digit without "looking" at the next character, i.e. leaving it as the
307 * user entered it. Presumably, the next term_key call will tell us how the
308 * user wants it handled.
310 * There is one more complication. Users might map keys to digits, and, as
311 * it's described above, the commands "map g 1G|d2g" would return the keys
312 * "d2<end-of-digits>1G", when the user probably wanted "d21<end-of-digits>G".
313 * So, if a map starts off with a digit we continue as before, otherwise, we
314 * pretend that we haven't mapped the character and return <end-of-digits>.
316 * Now that that's out of the way, let's talk about Energizer Bunny macros.
317 * It's easy to create macros that expand to a loop, e.g. map x 3x. It's
318 * fairly easy to detect this example, because it's all internal to term_key.
319 * If we're expanding a macro and it gets big enough, at some point we can
320 * assume it's looping and kill it. The examples that are tough are the ones
321 * where the parser is involved, e.g. map x "ayyx"byy. We do an expansion
322 * on 'x', and get "ayyx"byy. We then return the first 4 characters, and then
323 * find the looping macro again. There is no way that we can detect this
324 * without doing a full parse of the command, because the character that might
325 * cause the loop (in this case 'x') may be a literal character, e.g. the map
326 * map x "ayy"xyy"byy is perfectly legal and won't cause a loop.
328 * Historic vi tried to detect looping macros by disallowing obvious cases in
329 * the map command, maps that that ended with the same letter as they started
330 * (which wrongly disallowed "map x 'x"), and detecting macros that expanded
331 * too many times before keys were returned to the command parser. It didn't
332 * get many (most?) of the tricky cases right, however, and it was certainly
333 * possible to create macros that ran forever. And, even if it did figure out
334 * what was going on, the user was usually tossed into ex mode. Finally, any
335 * changes made before vi realized that the macro was recursing were left in
336 * place. This implementation counts how many times each input character has
337 * been mapped. If it reaches some arbitrary value, we flush all mapped keys
338 * and return an error.
340 * XXX
341 * The final issue is recovery. It would be possible to undo all of the work
342 * that was done by the macro if we entered a record into the log so that we
343 * knew when the macro started, and, in fact, this might be worth doing at some
344 * point. Given that this might make the log grow unacceptably (consider that
345 * cursor keys are done with maps), for now we leave any changes made in place.
347 enum input
348 term_key(sp, chp, flags)
349 SCR *sp;
350 CH *chp;
351 u_int flags;
353 enum input rval;
354 struct timeval t;
355 CHAR_T ch;
356 GS *gp;
357 IBUF *tty;
358 SEQ *qp;
359 int cmap, ispartial, nr;
361 gp = sp->gp;
362 tty = gp->tty;
365 * If the queue is empty, read more keys in. Since no timeout is
366 * requested, s_key_read will either return an error or will read
367 * some number of characters.
369 loop: if (tty->cnt == 0) {
370 if (term_read_grow(sp, tty))
371 return (INP_ERR);
372 if (rval = sp->s_key_read(sp, &nr, NULL))
373 return (rval);
375 * If there's something on the mode line that we wanted
376 * the user to see, they just entered a character so we
377 * can presume they saw it.
379 if (F_ISSET(sp, S_UPDATE_MODE))
380 F_CLR(sp, S_UPDATE_MODE);
383 /* If the key is mappable and should be mapped, look it up. */
384 if (!(tty->chf[tty->next] & CH_NOMAP) &&
385 LF_ISSET(TXT_MAPCOMMAND | TXT_MAPINPUT)) {
386 /* Set up timeout value. */
387 t.tv_sec = O_VAL(sp, O_KEYTIME) / 10;
388 t.tv_usec = (O_VAL(sp, O_KEYTIME) % 10) * 100000L;
390 /* Get the next key. */
391 newmap: ch = tty->ch[tty->next];
392 if (ch < MAX_BIT_SEQ && !bit_test(gp->seqb, ch))
393 goto nomap;
395 /* Search the map. */
396 remap: qp = seq_find(sp, NULL, &tty->ch[tty->next], tty->cnt,
397 LF_ISSET(TXT_MAPCOMMAND) ? SEQ_COMMAND : SEQ_INPUT,
398 &ispartial);
401 * If get a partial match, read more characters and retry
402 * the map. If no characters read, return the characters
403 * unmapped.
405 if (ispartial) {
406 if (term_read_grow(sp, tty))
407 return (INP_ERR);
408 if (rval = sp->s_key_read(sp, &nr, &t))
409 return (rval);
410 if (nr)
411 goto remap;
412 goto nomap;
415 /* If no map, return the character. */
416 if (qp == NULL)
417 goto nomap;
420 * If looking for the end of a digit string, and the first
421 * character of the map is it, pretend we haven't seen the
422 * character.
424 if (LF_ISSET(TXT_MAPNODIGIT) && !isdigit(qp->output[0]))
425 goto not_digit_ch;
428 * Only permit a character to be remapped a certain number
429 * of times before we figure that it's not going to finish.
431 if ((cmap = tty->cmap[tty->next]) > MAX_MAP_COUNT) {
432 term_map_flush(sp, "Character remapped too many times");
433 return (INP_ERR);
436 /* Delete the mapped characters from the queue. */
437 QREM_HEAD(tty, qp->ilen);
439 /* If remapping characters, push the character on the queue. */
440 if (O_ISSET(sp, O_REMAP)) {
441 if (term_push(sp, qp->output, qp->olen, ++cmap, 0))
442 return (INP_ERR);
443 goto newmap;
446 /* Else, push the characters on the queue and return one. */
447 if (term_push(sp, qp->output, qp->olen, 0, CH_NOMAP))
448 return (INP_ERR);
451 nomap: ch = tty->ch[tty->next];
452 if (LF_ISSET(TXT_MAPNODIGIT) && !isdigit(ch)) {
453 not_digit_ch: chp->ch = NOT_DIGIT_CH;
454 chp->value = 0;
455 chp->flags = 0;
456 return (INP_OK);
459 /* Fill in the return information. */
460 chp->ch = ch;
461 chp->flags = tty->chf[tty->next];
462 chp->value = term_key_val(sp, ch);
464 /* Delete the character from the queue. */
465 QREM_HEAD(tty, 1);
468 * O_BEAUTIFY eliminates all control characters except
469 * escape, form-feed, newline and tab.
471 if (isprint(ch) ||
472 !LF_ISSET(TXT_BEAUTIFY) || !O_ISSET(sp, O_BEAUTIFY) ||
473 chp->value == K_ESCAPE || chp->value == K_FORMFEED ||
474 chp->value == K_NL || chp->value == K_TAB)
475 return (INP_OK);
477 goto loop;
481 * term_ab_flush --
482 * Flush any abbreviated keys.
484 void
485 term_ab_flush(sp, msg)
486 SCR *sp;
487 char *msg;
489 IBUF *tty;
491 tty = sp->gp->tty;
492 if (!tty->cnt || !(tty->chf[tty->next] & CH_ABBREVIATED))
493 return;
494 do {
495 QREM_HEAD(tty, 1);
496 } while (tty->cnt && tty->chf[tty->next] & CH_ABBREVIATED);
497 msgq(sp, M_ERR, "%s: keys flushed.", msg);
501 * term_map_flush --
502 * Flush any mapped keys.
504 void
505 term_map_flush(sp, msg)
506 SCR *sp;
507 char *msg;
509 IBUF *tty;
511 tty = sp->gp->tty;
512 if (!tty->cnt || !tty->cmap[tty->next])
513 return;
514 do {
515 QREM_HEAD(tty, 1);
516 } while (tty->cnt && tty->cmap[tty->next]);
517 msgq(sp, M_ERR, "%s: keys flushed.", msg);
522 * term_user_key --
523 * Get the next key, but require the user enter one.
525 enum input
526 term_user_key(sp, chp)
527 SCR *sp;
528 CH *chp;
530 enum input rval;
531 struct timeval t;
532 IBUF *tty;
533 int nr;
536 * Read any keys the user has waiting. Make the race condition
537 * as short as possible.
539 t.tv_sec = 0;
540 t.tv_usec = 1;
541 tty = sp->gp->tty;
542 if (term_read_grow(sp, tty))
543 return (INP_ERR);
544 if (rval = sp->s_key_read(sp, &nr, &t))
545 return (rval);
547 /* Read another key. */
548 if (term_read_grow(sp, tty))
549 return (INP_ERR);
550 if (rval = sp->s_key_read(sp, &nr, NULL))
551 return (rval);
553 /* Fill in the return information. */
554 chp->ch = tty->ch[tty->next + (tty->cnt - 1)];
555 chp->flags = 0;
556 chp->value = term_key_val(sp, chp->ch);
558 QREM_TAIL(tty, 1);
559 return (INP_OK);
563 * term_key_ch --
564 * Fill in the key for a value.
567 term_key_ch(sp, val, chp)
568 SCR *sp;
569 int val;
570 CHAR_T *chp;
572 KEYLIST *kp;
574 for (kp = keylist;; ++kp)
575 if (kp->value == val) {
576 *chp = kp->ch;
577 return (0);
579 return (1);
583 * __term_key_val --
584 * Fill in the value for a key. This routine is the backup
585 * for the term_key_val() macro.
588 __term_key_val(sp, ch)
589 SCR *sp;
590 ARG_CHAR_T ch;
592 KEYLIST k, *kp;
594 k.ch = ch;
595 kp = bsearch(&k, keylist,
596 sizeof(keylist) / sizeof(keylist[0]), sizeof(keylist[0]), keycmp);
597 return (kp == NULL ? 0 : kp->value);
601 * __term_read_grow --
602 * Grow the terminal queue. This routine is the backup for
603 * the term_read_grow() macro.
605 static int
606 __term_read_grow(sp, tty)
607 SCR *sp;
608 IBUF *tty;
610 size_t alen, len, nlen;
612 nlen = tty->len + 64;
613 alen = tty->len - (tty->next + tty->cnt);
615 len = tty->len;
616 BINC_RET(sp, tty->ch, len, nlen * sizeof(tty->ch[0]));
617 memset(tty->ch + tty->next + tty->cnt, 0, alen * sizeof(tty->ch[0]));
619 len = tty->len;
620 BINC_RET(sp, tty->chf, len, nlen * sizeof(tty->chf[0]));
621 memset(tty->chf + tty->next + tty->cnt, 0, alen * sizeof(tty->chf[0]));
623 BINC_RET(sp, tty->cmap, tty->len, nlen * sizeof(tty->cmap[0]));
624 memset(tty->cmap +
625 tty->next + tty->cnt, 0, alen * sizeof(tty->cmap[0]));
626 return (0);
629 static int
630 keycmp(ap, bp)
631 const void *ap, *bp;
633 return (((KEYLIST *)ap)->ch - ((KEYLIST *)bp)->ch);