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