xref: /openbsd/usr.bin/vi/ex/ex_map.c (revision 721c3ea3)
1 /*	$OpenBSD: ex_map.c,v 1.9 2016/05/27 09:18:12 martijn 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(SCR * sp,EXCMD * cmdp)43 ex_map(SCR *sp, EXCMD *cmdp)
44 {
45 	seq_t stype;
46 	CHAR_T *input, *p;
47 
48 	stype = FL_ISSET(cmdp->iflags, E_C_FORCE) ? SEQ_INPUT : SEQ_COMMAND;
49 
50 	switch (cmdp->argc) {
51 	case 0:
52 		if (seq_dump(sp, stype, 1) == 0)
53 			msgq(sp, M_INFO, stype == SEQ_INPUT ?
54 			    "No input map entries" :
55 			    "No command map entries");
56 		return (0);
57 	case 2:
58 		input = cmdp->argv[0]->bp;
59 		break;
60 	default:
61 		abort();
62 	}
63 
64 	/*
65 	 * If the mapped string is #[0-9]* (and wasn't quoted) then store the
66 	 * function key mapping.  If the screen specific routine has been set,
67 	 * call it as well.  Note, the SEQ_FUNCMAP type is persistent across
68 	 * screen types, maybe the next screen type will get it right.
69 	 */
70 	if (input[0] == '#' && isdigit(input[1])) {
71 		for (p = input + 2; isdigit(*p); ++p);
72 		if (p[0] != '\0')
73 			goto nofunc;
74 
75 		if (seq_set(sp, NULL, 0, input, cmdp->argv[0]->len,
76 		    cmdp->argv[1]->bp, cmdp->argv[1]->len, stype,
77 		    SEQ_FUNCMAP | SEQ_USERDEF))
78 			return (1);
79 		return (sp->gp->scr_fmap == NULL ? 0 :
80 		    sp->gp->scr_fmap(sp, stype, input, cmdp->argv[0]->len,
81 		    cmdp->argv[1]->bp, cmdp->argv[1]->len));
82 	}
83 
84 	/* Some single keys may not be remapped in command mode. */
85 nofunc:	if (stype == SEQ_COMMAND && input[1] == '\0')
86 		switch (KEY_VAL(sp, input[0])) {
87 		case K_COLON:
88 		case K_ESCAPE:
89 		case K_NL:
90 			msgq(sp, M_ERR,
91 			    "The %s character may not be remapped",
92 			    KEY_NAME(sp, input[0]));
93 			return (1);
94 		}
95 	return (seq_set(sp, NULL, 0, input, cmdp->argv[0]->len,
96 	    cmdp->argv[1]->bp, cmdp->argv[1]->len, stype, SEQ_USERDEF));
97 }
98 
99 /*
100  * ex_unmap -- (:unmap[!] key)
101  *	Unmap a key.
102  *
103  * PUBLIC: int ex_unmap(SCR *, EXCMD *);
104  */
105 int
ex_unmap(SCR * sp,EXCMD * cmdp)106 ex_unmap(SCR *sp, EXCMD *cmdp)
107 {
108 	if (seq_delete(sp, cmdp->argv[0]->bp, cmdp->argv[0]->len,
109 	    FL_ISSET(cmdp->iflags, E_C_FORCE) ? SEQ_INPUT : SEQ_COMMAND)) {
110 		msgq_str(sp, M_INFO,
111 		    cmdp->argv[0]->bp, "\"%s\" isn't currently mapped");
112 		return (1);
113 	}
114 	return (0);
115 }
116