xref: /minix3/external/bsd/nvi/dist/ex/ex_map.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: ex_map.c,v 1.3 2014/01/26 21:43:45 christos Exp $ */
284d9c625SLionel Sambuc /*-
384d9c625SLionel Sambuc  * Copyright (c) 1992, 1993, 1994
484d9c625SLionel Sambuc  *	The Regents of the University of California.  All rights reserved.
584d9c625SLionel Sambuc  * Copyright (c) 1992, 1993, 1994, 1995, 1996
684d9c625SLionel Sambuc  *	Keith Bostic.  All rights reserved.
784d9c625SLionel Sambuc  *
884d9c625SLionel Sambuc  * See the LICENSE file for redistribution information.
984d9c625SLionel Sambuc  */
1084d9c625SLionel Sambuc 
1184d9c625SLionel Sambuc #include "config.h"
1284d9c625SLionel Sambuc 
13*0a6a1f1dSLionel Sambuc #include <sys/cdefs.h>
14*0a6a1f1dSLionel Sambuc #if 0
1584d9c625SLionel Sambuc #ifndef lint
1684d9c625SLionel Sambuc 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 ";
1784d9c625SLionel Sambuc #endif /* not lint */
18*0a6a1f1dSLionel Sambuc #else
19*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: ex_map.c,v 1.3 2014/01/26 21:43:45 christos Exp $");
20*0a6a1f1dSLionel Sambuc #endif
2184d9c625SLionel Sambuc 
2284d9c625SLionel Sambuc #include <sys/types.h>
2384d9c625SLionel Sambuc #include <sys/queue.h>
2484d9c625SLionel Sambuc 
2584d9c625SLionel Sambuc #include <bitstring.h>
2684d9c625SLionel Sambuc #include <ctype.h>
2784d9c625SLionel Sambuc #include <limits.h>
2884d9c625SLionel Sambuc #include <stdio.h>
2984d9c625SLionel Sambuc #include <stdlib.h>
3084d9c625SLionel Sambuc #include <string.h>
3184d9c625SLionel Sambuc 
3284d9c625SLionel Sambuc #include "../common/common.h"
3384d9c625SLionel Sambuc 
3484d9c625SLionel Sambuc /*
3584d9c625SLionel Sambuc  * ex_map -- :map[!] [input] [replacement]
3684d9c625SLionel Sambuc  *	Map a key/string or display mapped keys.
3784d9c625SLionel Sambuc  *
3884d9c625SLionel Sambuc  * Historical note:
3984d9c625SLionel Sambuc  *	Historic vi maps were fairly bizarre, and likely to differ in
4084d9c625SLionel Sambuc  *	very subtle and strange ways from this implementation.  Two
4184d9c625SLionel Sambuc  *	things worth noting are that vi would often hang or drop core
4284d9c625SLionel Sambuc  *	if the map was strange enough (ex: map X "xy$@x^V), or, simply
4384d9c625SLionel Sambuc  *	not work.  One trick worth remembering is that if you put a
4484d9c625SLionel Sambuc  *	mark at the start of the map, e.g. map X mx"xy ...), or if you
4584d9c625SLionel Sambuc  *	put the map in a .exrc file, things would often work much better.
4684d9c625SLionel Sambuc  *	No clue why.
4784d9c625SLionel Sambuc  *
4884d9c625SLionel Sambuc  * PUBLIC: int ex_map __P((SCR *, EXCMD *));
4984d9c625SLionel Sambuc  */
5084d9c625SLionel Sambuc int
ex_map(SCR * sp,EXCMD * cmdp)5184d9c625SLionel Sambuc ex_map(SCR *sp, EXCMD *cmdp)
5284d9c625SLionel Sambuc {
5384d9c625SLionel Sambuc 	seq_t stype;
5484d9c625SLionel Sambuc 	CHAR_T *input, *p;
5584d9c625SLionel Sambuc 
5684d9c625SLionel Sambuc 	stype = FL_ISSET(cmdp->iflags, E_C_FORCE) ? SEQ_INPUT : SEQ_COMMAND;
5784d9c625SLionel Sambuc 
5884d9c625SLionel Sambuc 	switch (cmdp->argc) {
5984d9c625SLionel Sambuc 	case 0:
6084d9c625SLionel Sambuc 		if (seq_dump(sp, stype, 1) == 0)
6184d9c625SLionel Sambuc 			msgq(sp, M_INFO, stype == SEQ_INPUT ?
6284d9c625SLionel Sambuc 			    "132|No input map entries" :
6384d9c625SLionel Sambuc 			    "133|No command map entries");
6484d9c625SLionel Sambuc 		return (0);
6584d9c625SLionel Sambuc 	case 2:
6684d9c625SLionel Sambuc 		input = cmdp->argv[0]->bp;
6784d9c625SLionel Sambuc 		break;
6884d9c625SLionel Sambuc 	default:
6984d9c625SLionel Sambuc 		abort();
7084d9c625SLionel Sambuc 	}
7184d9c625SLionel Sambuc 
7284d9c625SLionel Sambuc 	/*
7384d9c625SLionel Sambuc 	 * If the mapped string is #[0-9]* (and wasn't quoted) then store the
7484d9c625SLionel Sambuc 	 * function key mapping.  If the screen specific routine has been set,
7584d9c625SLionel Sambuc 	 * call it as well.  Note, the SEQ_FUNCMAP type is persistent across
7684d9c625SLionel Sambuc 	 * screen types, maybe the next screen type will get it right.
7784d9c625SLionel Sambuc 	 */
7884d9c625SLionel Sambuc 	if (input[0] == '#' && ISDIGIT((UCHAR_T)input[1])) {
7984d9c625SLionel Sambuc 		for (p = input + 2; ISDIGIT((UCHAR_T)*p); ++p);
8084d9c625SLionel Sambuc 		if (p[0] != '\0')
8184d9c625SLionel Sambuc 			goto nofunc;
8284d9c625SLionel Sambuc 
8384d9c625SLionel Sambuc 		if (seq_set(sp, NULL, 0, input, cmdp->argv[0]->len,
8484d9c625SLionel Sambuc 		    cmdp->argv[1]->bp, cmdp->argv[1]->len, stype,
8584d9c625SLionel Sambuc 		    SEQ_FUNCMAP | SEQ_USERDEF))
8684d9c625SLionel Sambuc 			return (1);
8784d9c625SLionel Sambuc 		return (sp->gp->scr_fmap == NULL ? 0 :
8884d9c625SLionel Sambuc 		    sp->gp->scr_fmap(sp, stype, input, cmdp->argv[0]->len,
8984d9c625SLionel Sambuc 		    cmdp->argv[1]->bp, cmdp->argv[1]->len));
9084d9c625SLionel Sambuc 	}
9184d9c625SLionel Sambuc 
9284d9c625SLionel Sambuc 	/* Some single keys may not be remapped in command mode. */
9384d9c625SLionel Sambuc nofunc:	if (stype == SEQ_COMMAND && input[1] == '\0')
9484d9c625SLionel Sambuc 		switch (KEY_VAL(sp, input[0])) {
9584d9c625SLionel Sambuc 		case K_COLON:
9684d9c625SLionel Sambuc 		case K_ESCAPE:
9784d9c625SLionel Sambuc 		case K_NL:
9884d9c625SLionel Sambuc 			msgq(sp, M_ERR,
9984d9c625SLionel Sambuc 			    "134|The %s character may not be remapped",
10084d9c625SLionel Sambuc 			    KEY_NAME(sp, input[0]));
10184d9c625SLionel Sambuc 			return (1);
10284d9c625SLionel Sambuc 		}
10384d9c625SLionel Sambuc 	return (seq_set(sp, NULL, 0, input, cmdp->argv[0]->len,
10484d9c625SLionel Sambuc 	    cmdp->argv[1]->bp, cmdp->argv[1]->len, stype, SEQ_USERDEF));
10584d9c625SLionel Sambuc }
10684d9c625SLionel Sambuc 
10784d9c625SLionel Sambuc /*
10884d9c625SLionel Sambuc  * ex_unmap -- (:unmap[!] key)
10984d9c625SLionel Sambuc  *	Unmap a key.
11084d9c625SLionel Sambuc  *
11184d9c625SLionel Sambuc  * PUBLIC: int ex_unmap __P((SCR *, EXCMD *));
11284d9c625SLionel Sambuc  */
11384d9c625SLionel Sambuc int
ex_unmap(SCR * sp,EXCMD * cmdp)11484d9c625SLionel Sambuc ex_unmap(SCR *sp, EXCMD *cmdp)
11584d9c625SLionel Sambuc {
11684d9c625SLionel Sambuc 	if (seq_delete(sp, cmdp->argv[0]->bp, cmdp->argv[0]->len,
11784d9c625SLionel Sambuc 	    FL_ISSET(cmdp->iflags, E_C_FORCE) ? SEQ_INPUT : SEQ_COMMAND)) {
11884d9c625SLionel Sambuc 		msgq_wstr(sp, M_INFO,
11984d9c625SLionel Sambuc 		    cmdp->argv[0]->bp, "135|\"%s\" isn't currently mapped");
12084d9c625SLionel Sambuc 		return (1);
12184d9c625SLionel Sambuc 	}
12284d9c625SLionel Sambuc 	return (0);
12384d9c625SLionel Sambuc }
124