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