xref: /freebsd/contrib/nvi/ex/ex_abbrev.c (revision 7431dfd4580e850375fe5478d92ec770344db098)
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_abbrev.c,v 10.10 2001/12/16 18:18:54 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 #include "../vi/vi.h"
29 
30 /*
31  * ex_abbr -- :abbreviate [key replacement]
32  *	Create an abbreviation or display abbreviations.
33  *
34  * PUBLIC: int ex_abbr __P((SCR *, EXCMD *));
35  */
36 int
37 ex_abbr(SCR *sp, EXCMD *cmdp)
38 {
39 	CHAR_T *p;
40 	size_t len;
41 
42 	switch (cmdp->argc) {
43 	case 0:
44 		if (seq_dump(sp, SEQ_ABBREV, 0) == 0)
45 			msgq(sp, M_INFO, "105|No abbreviations to display");
46 		return (0);
47 	case 2:
48 		break;
49 	default:
50 		abort();
51 	}
52 
53 	/*
54 	 * Check for illegal characters.
55 	 *
56 	 * !!!
57 	 * Another fun one, historically.  See vi/v_ntext.c:txt_abbrev() for
58 	 * details.  The bottom line is that all abbreviations have to end
59 	 * with a "word" character, because it's the transition from word to
60 	 * non-word characters that triggers the test for an abbreviation.  In
61 	 * addition, because of the way the test is done, there can't be any
62 	 * transitions from word to non-word character (or vice-versa) other
63 	 * than between the next-to-last and last characters of the string,
64 	 * and there can't be any <blank> characters.  Warn the user.
65 	 */
66 	if (!inword(cmdp->argv[0]->bp[cmdp->argv[0]->len - 1])) {
67 		msgq(sp, M_ERR,
68 		    "106|Abbreviations must end with a \"word\" character");
69 			return (1);
70 	}
71 	for (p = cmdp->argv[0]->bp; *p != '\0'; ++p)
72 		if (ISBLANK(p[0])) {
73 			msgq(sp, M_ERR,
74 			    "107|Abbreviations may not contain tabs or spaces");
75 			return (1);
76 		}
77 	if (cmdp->argv[0]->len > 2)
78 		for (p = cmdp->argv[0]->bp,
79 		    len = cmdp->argv[0]->len - 2; len; --len, ++p)
80 			if (inword(p[0]) != inword(p[1])) {
81 				msgq(sp, M_ERR,
82 "108|Abbreviations may not mix word/non-word characters, except at the end");
83 				return (1);
84 			}
85 
86 	if (seq_set(sp, NULL, 0, cmdp->argv[0]->bp, cmdp->argv[0]->len,
87 	    cmdp->argv[1]->bp, cmdp->argv[1]->len, SEQ_ABBREV, SEQ_USERDEF))
88 		return (1);
89 
90 	F_SET(sp->gp, G_ABBREV);
91 	return (0);
92 }
93 
94 /*
95  * ex_unabbr -- :unabbreviate key
96  *      Delete an abbreviation.
97  *
98  * PUBLIC: int ex_unabbr __P((SCR *, EXCMD *));
99  */
100 int
101 ex_unabbr(SCR *sp, EXCMD *cmdp)
102 {
103 	ARGS *ap;
104 
105 	ap = cmdp->argv[0];
106 	if (!F_ISSET(sp->gp, G_ABBREV) ||
107 	    seq_delete(sp, ap->bp, ap->len, SEQ_ABBREV)) {
108 		msgq_wstr(sp, M_ERR, ap->bp,
109 		    "109|\"%s\" is not an abbreviation");
110 		return (1);
111 	}
112 	return (0);
113 }
114