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