2 * tc.bind.c: Key binding functions
5 * Copyright (c) 1980, 1991 The Regents of the University of California.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 static void printkey (const KEYCMD *, CStr *);
37 static KEYCMD parsecmd (Char *);
38 static void bad_spec (const Char *);
39 static CStr *parsestring (const Char *, CStr *);
40 static CStr *parsebind (const Char *, CStr *);
41 static void print_all_keys (void);
42 static void printkeys (KEYCMD *, int, int);
43 static void bindkey_usage (void);
44 static void list_functions (void);
46 extern int MapsAreInited;
53 dobindkey(Char **v, struct command *c)
56 int ntype, no, removeb, key, bindk;
70 key = removeb = bindk = 0;
71 for (no = 1, par = v[no];
72 par != NULL && (*par++ & CHAR) == '-'; no++, par = v[no]) {
73 if ((p = (*par & CHAR)) == '-') {
106 #else /* EMACSDEFAULT */
108 #endif /* VIDEFAULT */
125 if (!IsArrowKey(v[no]))
126 xprintf(CGETS(20, 1, "Invalid key name `%S'\n"), v[no]);
127 in.buf = Strsave(v[no++]);
128 in.len = Strlen(in.buf);
132 if (parsebind(v[no++], &in) == NULL)
136 if (parsestring(v[no++], &in) == NULL)
140 cleanup_push(in.buf, xfree);
143 if (in.buf[0] > 0xFF) {
145 cleanup_until(in.buf);
149 ch = (uChar) in.buf[0];
153 (void) ClearArrowKeys(&in);
154 else if (in.len > 1) {
155 (void) DeleteXkey(&in);
157 else if (map[ch] == F_XKEY) {
158 (void) DeleteXkey(&in);
159 map[ch] = F_UNASSIGNED;
162 map[ch] = F_UNASSIGNED;
164 cleanup_until(in.buf);
172 cleanup_until(in.buf);
177 cleanup_until(in.buf);
183 if (parsestring(v[no], &out) == NULL) {
184 cleanup_until(in.buf);
187 cleanup_push(out.buf, xfree);
189 if (SetArrowKeys(&in, XmapStr(&out), ntype) == -1)
190 xprintf(CGETS(20, 2, "Bad key name: %S\n"), in.buf);
192 cleanup_ignore(out.buf);
195 AddXkey(&in, XmapStr(&out), ntype);
199 if ((cmd = parsecmd(v[no])) == 0) {
200 cleanup_until(in.buf);
204 (void) SetArrowKeys(&in, XmapCmd((int) cmd), ntype);
207 AddXkey(&in, XmapCmd((int) cmd), ntype);
220 cleanup_until(in.buf);
226 printkey(const KEYCMD *map, CStr *in)
231 unsigned char *unparsed;
233 unparsed = unparsestring(in, STRQQ);
234 cleanup_push(unparsed, xfree);
235 for (fp = FuncNames; fp->name; fp++) {
236 if (fp->func == map[(uChar) *(in->buf)]) {
237 xprintf("%s\t->\t%s\n", unparsed, fp->name);
240 cleanup_until(unparsed);
251 for (fp = FuncNames; fp->name; fp++) {
252 if (strcmp(short2str(str), fp->name) == 0) {
253 return (KEYCMD) fp->func;
256 xprintf(CGETS(20, 3, "Bad command name: %S\n"), str);
262 bad_spec(const Char *str)
264 xprintf(CGETS(20, 4, "Bad key spec %S\n"), str);
268 parsebind(const Char *s, CStr *str)
270 struct Strbuf b = Strbuf_INIT;
272 cleanup_push(&b, Strbuf_cleanup);
274 Strbuf_append1(&b, *s);
282 Strbuf_append1(&b, (*s == '?') ? '\177' : ((*s & CHAR) & 0237));
284 Strbuf_append1(&b, (*s == '?') ? CTL_ESC('\177')
285 : _toebcdic[_toascii[*s & CHAR] & 0237]);
295 #endif /* WINNT_NATIVE */
296 if (s[1] != '-' || s[2] == '\0')
300 case 'F': case 'f': /* Turn into ^[str */
301 Strbuf_append1(&b, CTL_ESC('\033'));
302 Strbuf_append(&b, s);
305 case 'C': case 'c': /* Turn into ^c */
307 Strbuf_append1(&b, (*s == '?') ? '\177' : ((*s & CHAR) & 0237));
309 Strbuf_append1(&b, (*s == '?') ? CTL_ESC('\177')
310 : _toebcdic[_toascii[*s & CHAR] & 0237]);
314 case 'X' : case 'x': /* Turn into ^Xc */
316 Strbuf_append1(&b, 'X' & 0237);
318 Strbuf_append1(&b, _toebcdic[_toascii['X'] & 0237]);
320 Strbuf_append1(&b, *s);
323 case 'M' : case 'm': /* Turn into 0x80|c */
325 Strbuf_append1(&b, CTL_ESC('\033'));
326 Strbuf_append1(&b, *s);
329 Strbuf_append1(&b, *s | 0x80);
331 Strbuf_append1(&b, _toebcdic[_toascii[*s] | 0x80]);
336 case 'N' : case 'n': /* NT */
340 bnt = nt_translate_bindkey(s);
342 Strbuf_append1(&b, bnt);
347 #endif /* WINNT_NATIVE */
361 Strbuf_terminate(&b);
362 str->buf = xrealloc(b.s, (b.len + 1) * sizeof (*str->buf));
374 parsestring(const Char *str, CStr *buf)
376 struct Strbuf b = Strbuf_INIT;
381 xprintf("%s", CGETS(20, 5, "Null string specification\n"));
385 cleanup_push(&b, Strbuf_cleanup);
386 for (p = str; *p != 0; p++) {
387 if ((*p & CHAR) == '\\' || (*p & CHAR) == '^') {
388 if ((es = parseescape(&p)) == CHAR_ERR) {
392 Strbuf_append1(&b, es);
395 Strbuf_append1(&b, *p & CHAR);
399 Strbuf_terminate(&b);
400 buf->buf = xrealloc(b.s, (b.len + 1) * sizeof (*buf->buf));
414 xprintf("%s", CGETS(20, 6, "Standard key bindings\n"));
416 for (i = 0; i < 256; i++) {
417 if (CcKeyMap[prev] == CcKeyMap[i])
419 printkeys(CcKeyMap, prev, i - 1);
422 printkeys(CcKeyMap, prev, i - 1);
424 xprintf("%s", CGETS(20, 7, "Alternative key bindings\n"));
426 for (i = 0; i < 256; i++) {
427 if (CcAltMap[prev] == CcAltMap[i])
429 printkeys(CcAltMap, prev, i - 1);
432 printkeys(CcAltMap, prev, i - 1);
433 xprintf("%s", CGETS(20, 8, "Multi-character bindings\n"));
434 PrintXkey(NULL); /* print all Xkey bindings */
435 xprintf("%s", CGETS(20, 9, "Arrow key bindings\n"));
436 PrintArrowKeys(&nilstr);
440 printkeys(KEYCMD *map, int first, int last)
443 Char firstbuf[2], lastbuf[2];
445 unsigned char *unparsed;
449 firstbuf[0] = (Char) first;
451 lastbuf[0] = (Char) last;
456 unparsed = unparsestring(&fb, STRQQ);
457 cleanup_push(unparsed, xfree);
458 if (map[first] == F_UNASSIGNED) {
460 xprintf(CGETS(20, 10, "%-15s-> is undefined\n"), unparsed);
461 cleanup_until(unparsed);
465 for (fp = FuncNames; fp->name; fp++) {
466 if (fp->func == map[first]) {
468 xprintf("%-15s-> %s\n", unparsed, fp->name);
472 p = unparsestring(&lb, STRQQ);
473 cleanup_push(p, xfree);
474 xprintf("%-4s to %-7s-> %s\n", unparsed, p, fp->name);
476 cleanup_until(unparsed);
480 xprintf(CGETS(20, 11, "BUG!!! %s isn't bound to anything.\n"), unparsed);
482 xprintf("CcKeyMap[%d] == %d\n", first, CcKeyMap[first]);
484 xprintf("CcAltMap[%d] == %d\n", first, CcAltMap[first]);
485 cleanup_until(unparsed);
491 xprintf("%s", CGETS(20, 12,
492 "Usage: bindkey [options] [--] [KEY [COMMAND]]\n"));
493 xprintf("%s", CGETS(20, 13,
494 " -a list or bind KEY in alternative key map\n"));
495 xprintf("%s", CGETS(20, 14,
496 " -b interpret KEY as a C-, M-, F- or X- key name\n"));
497 xprintf("%s", CGETS(20, 15,
498 " -s interpret COMMAND as a literal string to be output\n"));
499 xprintf("%s", CGETS(20, 16,
500 " -c interpret COMMAND as a builtin or external command\n"));
501 xprintf("%s", CGETS(20, 17,
502 " -v bind all keys to vi bindings\n"));
503 xprintf("%s", CGETS(20, 18,
504 " -e bind all keys to emacs bindings\n"));
505 xprintf(CGETS(20, 19,
506 " -d bind all keys to default editor's bindings (%s)\n"),
509 #else /* EMACSDEFAULT */
511 #endif /* VIDEFAULT */
513 xprintf("%s", CGETS(20, 20,
514 " -l list editor commands with descriptions\n"));
515 xprintf("%s", CGETS(20, 21,
516 " -r remove KEY's binding\n"));
517 xprintf("%s", CGETS(20, 22,
518 " -k interpret KEY as a symbolic arrow-key name\n"));
519 xprintf("%s", CGETS(20, 23,
520 " -- force a break from option processing\n"));
521 xprintf("%s", CGETS(20, 24,
522 " -u (or any invalid option) this message\n"));
524 xprintf("%s", CGETS(20, 25,
525 "Without KEY or COMMAND, prints all bindings\n"));
526 xprintf("%s", CGETS(20, 26,
527 "Without COMMAND, prints the binding for KEY.\n"));
535 for (fp = FuncNames; fp->name; fp++) {
536 xprintf("%s\n %s\n", fp->name, fp->desc);