1 /* $OpenBSD: ex_map.c,v 1.5 2009/10/27 23:59:47 deraadt Exp $ */
2
3 /*-
4 * Copyright (c) 1992, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 * Copyright (c) 1992, 1993, 1994, 1995, 1996
7 * Keith Bostic. All rights reserved.
8 *
9 * See the LICENSE file for redistribution information.
10 */
11
12 #include "config.h"
13
14 #include <sys/types.h>
15 #include <sys/queue.h>
16
17 #include <bitstring.h>
18 #include <ctype.h>
19 #include <limits.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include "../common/common.h"
25
26 /*
27 * ex_map -- :map[!] [input] [replacement]
28 * Map a key/string or display mapped keys.
29 *
30 * Historical note:
31 * Historic vi maps were fairly bizarre, and likely to differ in
32 * very subtle and strange ways from this implementation. Two
33 * things worth noting are that vi would often hang or drop core
34 * if the map was strange enough (ex: map X "xy$@x^V), or, simply
35 * not work. One trick worth remembering is that if you put a
36 * mark at the start of the map, e.g. map X mx"xy ...), or if you
37 * put the map in a .exrc file, things would often work much better.
38 * No clue why.
39 *
40 * PUBLIC: int ex_map(SCR *, EXCMD *);
41 */
42 int
ex_map(sp,cmdp)43 ex_map(sp, cmdp)
44 SCR *sp;
45 EXCMD *cmdp;
46 {
47 seq_t stype;
48 CHAR_T *input, *p;
49
50 stype = FL_ISSET(cmdp->iflags, E_C_FORCE) ? SEQ_INPUT : SEQ_COMMAND;
51
52 switch (cmdp->argc) {
53 case 0:
54 if (seq_dump(sp, stype, 1) == 0)
55 msgq(sp, M_INFO, stype == SEQ_INPUT ?
56 "132|No input map entries" :
57 "133|No command map entries");
58 return (0);
59 case 2:
60 input = cmdp->argv[0]->bp;
61 break;
62 default:
63 abort();
64 }
65
66 /*
67 * If the mapped string is #[0-9]* (and wasn't quoted) then store the
68 * function key mapping. If the screen specific routine has been set,
69 * call it as well. Note, the SEQ_FUNCMAP type is persistent across
70 * screen types, maybe the next screen type will get it right.
71 */
72 if (input[0] == '#' && isdigit(input[1])) {
73 for (p = input + 2; isdigit(*p); ++p);
74 if (p[0] != '\0')
75 goto nofunc;
76
77 if (seq_set(sp, NULL, 0, input, cmdp->argv[0]->len,
78 cmdp->argv[1]->bp, cmdp->argv[1]->len, stype,
79 SEQ_FUNCMAP | SEQ_USERDEF))
80 return (1);
81 return (sp->gp->scr_fmap == NULL ? 0 :
82 sp->gp->scr_fmap(sp, stype, input, cmdp->argv[0]->len,
83 cmdp->argv[1]->bp, cmdp->argv[1]->len));
84 }
85
86 /* Some single keys may not be remapped in command mode. */
87 nofunc: if (stype == SEQ_COMMAND && input[1] == '\0')
88 switch (KEY_VAL(sp, input[0])) {
89 case K_COLON:
90 case K_ESCAPE:
91 case K_NL:
92 msgq(sp, M_ERR,
93 "134|The %s character may not be remapped",
94 KEY_NAME(sp, input[0]));
95 return (1);
96 }
97 return (seq_set(sp, NULL, 0, input, cmdp->argv[0]->len,
98 cmdp->argv[1]->bp, cmdp->argv[1]->len, stype, SEQ_USERDEF));
99 }
100
101 /*
102 * ex_unmap -- (:unmap[!] key)
103 * Unmap a key.
104 *
105 * PUBLIC: int ex_unmap(SCR *, EXCMD *);
106 */
107 int
ex_unmap(sp,cmdp)108 ex_unmap(sp, cmdp)
109 SCR *sp;
110 EXCMD *cmdp;
111 {
112 if (seq_delete(sp, cmdp->argv[0]->bp, cmdp->argv[0]->len,
113 FL_ISSET(cmdp->iflags, E_C_FORCE) ? SEQ_INPUT : SEQ_COMMAND)) {
114 msgq_str(sp, M_INFO,
115 cmdp->argv[0]->bp, "135|\"%s\" isn't currently mapped");
116 return (1);
117 }
118 return (0);
119 }
120