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