include seq.h in each file, now, only included in a few places
[nvi.git] / ex / ex_map.c
blobe8282178c8839f97e8e6c5cf693ca6c14f06a9c0
1 /*-
2 * Copyright (c) 1992, 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: ex_map.c,v 8.4 1993/11/27 15:52:30 bostic Exp $ (Berkeley) $Date: 1993/11/27 15:52:30 $";
10 #endif /* not lint */
12 #include <sys/types.h>
14 #include <ctype.h>
15 #include <stdlib.h>
16 #include <string.h>
18 #include "vi.h"
19 #include "seq.h"
20 #include "excmd.h"
23 * ex_map -- :map[!] [input] [replacement]
24 * Map a key/string or display mapped keys.
26 * Historical note:
27 * Historic vi maps were fairly bizarre, and likely to differ in
28 * very subtle and strange ways from this implementation. Two
29 * things worth noting are that vi would often hang or drop core
30 * if the map was strange enough (ex: map X "xy$@x^V), or, simply
31 * not work. One trick worth remembering is that if you put a
32 * mark at the start of the map, e.g. map X mx"xy ...), or if you
33 * put the map in a .exrc file, things would often work much better.
34 * No clue why.
36 int
37 ex_map(sp, ep, cmdp)
38 SCR *sp;
39 EXF *ep;
40 EXCMDARG *cmdp;
42 char *input, *output;
43 enum seqtype stype;
44 int key;
45 char *name, buf[10];
47 stype = F_ISSET(cmdp, E_FORCE) ? SEQ_INPUT : SEQ_COMMAND;
49 switch (cmdp->argc) {
50 case 0:
51 if (seq_dump(sp, stype, 1) == 0)
52 msgq(sp, M_INFO, "No %s map entries.",
53 stype == SEQ_INPUT ? "input" : "command");
54 return (0);
55 case 2:
56 input = cmdp->argv[0];
57 output = cmdp->argv[1];
58 break;
59 default:
60 abort();
64 * If the mapped string is #[0-9] (and wasn't quoted in any
65 * way, then map to a function key.
67 if (input[0] == '#' && isdigit(input[1]) && !input[2]) {
68 key = atoi(input + 1);
69 (void)snprintf(buf, sizeof(buf), "f%d", key);
70 #ifdef notdef
71 if (FKEY[key]) { /* CCC */
72 input = FKEY[key];
73 name = buf;
74 } else {
75 msgq(sp, M_ERR, "This terminal has no %s key.", buf);
76 return (1);
78 #else
79 name = NULL;
80 #endif
81 } else {
82 name = NULL;
84 /* Some single keys may not be remapped in command mode. */
85 if (stype == SEQ_COMMAND && input[1] == '\0')
86 switch (sp->special[input[0]]) {
87 case K_COLON:
88 case K_CR:
89 case K_ESCAPE:
90 case K_NL:
91 msgq(sp, M_ERR,
92 "The %s character may not be remapped.",
93 charname(sp, input[0]));
94 return (1);
97 return (seq_set(sp, name, input, output, stype, 1));
101 * ex_unmap -- (:unmap[!] key)
102 * Unmap a key.
105 ex_unmap(sp, ep, cmdp)
106 SCR *sp;
107 EXF *ep;
108 EXCMDARG *cmdp;
110 char *input;
112 input = cmdp->argv[0];
113 if (seq_delete(sp,
114 input, F_ISSET(cmdp, E_FORCE) ? SEQ_INPUT : SEQ_COMMAND)) {
115 msgq(sp, M_INFO, "\"%s\" isn't mapped.", input);
116 return (1);
118 return (0);
122 * map_save --
123 * Save the mapped sequences to a file.
126 map_save(sp, fp)
127 SCR *sp;
128 FILE *fp;
130 if (seq_save(sp, fp, "map ", SEQ_COMMAND))
131 return (1);
132 return (seq_save(sp, fp, "map! ", SEQ_INPUT));