xref: /freebsd/contrib/nvi/ex/ex_abbrev.c (revision f0957ccae4f402b93cf27b125542343d28b53109)
1b8ba871bSPeter Wemm /*-
2b8ba871bSPeter Wemm  * Copyright (c) 1992, 1993, 1994
3b8ba871bSPeter Wemm  *	The Regents of the University of California.  All rights reserved.
4b8ba871bSPeter Wemm  * Copyright (c) 1992, 1993, 1994, 1995, 1996
5b8ba871bSPeter Wemm  *	Keith Bostic.  All rights reserved.
6b8ba871bSPeter Wemm  *
7b8ba871bSPeter Wemm  * See the LICENSE file for redistribution information.
8b8ba871bSPeter Wemm  */
9b8ba871bSPeter Wemm 
10b8ba871bSPeter Wemm #include "config.h"
11b8ba871bSPeter Wemm 
12b8ba871bSPeter Wemm #ifndef lint
13*f0957ccaSPeter Wemm static const char sccsid[] = "$Id: ex_abbrev.c,v 10.10 2001/12/16 18:18:54 skimo Exp $";
14b8ba871bSPeter Wemm #endif /* not lint */
15b8ba871bSPeter Wemm 
16b8ba871bSPeter Wemm #include <sys/types.h>
17b8ba871bSPeter Wemm #include <sys/queue.h>
18b8ba871bSPeter Wemm #include <sys/time.h>
19b8ba871bSPeter Wemm 
20b8ba871bSPeter Wemm #include <bitstring.h>
21b8ba871bSPeter Wemm #include <ctype.h>
22b8ba871bSPeter Wemm #include <limits.h>
23b8ba871bSPeter Wemm #include <stdio.h>
24b8ba871bSPeter Wemm #include <stdlib.h>
25b8ba871bSPeter Wemm #include <string.h>
26b8ba871bSPeter Wemm 
27b8ba871bSPeter Wemm #include "../common/common.h"
28b8ba871bSPeter Wemm #include "../vi/vi.h"
29b8ba871bSPeter Wemm 
30b8ba871bSPeter Wemm /*
31b8ba871bSPeter Wemm  * ex_abbr -- :abbreviate [key replacement]
32b8ba871bSPeter Wemm  *	Create an abbreviation or display abbreviations.
33b8ba871bSPeter Wemm  *
34b8ba871bSPeter Wemm  * PUBLIC: int ex_abbr __P((SCR *, EXCMD *));
35b8ba871bSPeter Wemm  */
36b8ba871bSPeter Wemm int
37*f0957ccaSPeter Wemm ex_abbr(SCR *sp, EXCMD *cmdp)
38b8ba871bSPeter Wemm {
39b8ba871bSPeter Wemm 	CHAR_T *p;
40b8ba871bSPeter Wemm 	size_t len;
41b8ba871bSPeter Wemm 
42b8ba871bSPeter Wemm 	switch (cmdp->argc) {
43b8ba871bSPeter Wemm 	case 0:
44b8ba871bSPeter Wemm 		if (seq_dump(sp, SEQ_ABBREV, 0) == 0)
45b8ba871bSPeter Wemm 			msgq(sp, M_INFO, "105|No abbreviations to display");
46b8ba871bSPeter Wemm 		return (0);
47b8ba871bSPeter Wemm 	case 2:
48b8ba871bSPeter Wemm 		break;
49b8ba871bSPeter Wemm 	default:
50b8ba871bSPeter Wemm 		abort();
51b8ba871bSPeter Wemm 	}
52b8ba871bSPeter Wemm 
53b8ba871bSPeter Wemm 	/*
54b8ba871bSPeter Wemm 	 * Check for illegal characters.
55b8ba871bSPeter Wemm 	 *
56b8ba871bSPeter Wemm 	 * !!!
57b8ba871bSPeter Wemm 	 * Another fun one, historically.  See vi/v_ntext.c:txt_abbrev() for
58b8ba871bSPeter Wemm 	 * details.  The bottom line is that all abbreviations have to end
59b8ba871bSPeter Wemm 	 * with a "word" character, because it's the transition from word to
60b8ba871bSPeter Wemm 	 * non-word characters that triggers the test for an abbreviation.  In
61b8ba871bSPeter Wemm 	 * addition, because of the way the test is done, there can't be any
62b8ba871bSPeter Wemm 	 * transitions from word to non-word character (or vice-versa) other
63b8ba871bSPeter Wemm 	 * than between the next-to-last and last characters of the string,
64b8ba871bSPeter Wemm 	 * and there can't be any <blank> characters.  Warn the user.
65b8ba871bSPeter Wemm 	 */
66b8ba871bSPeter Wemm 	if (!inword(cmdp->argv[0]->bp[cmdp->argv[0]->len - 1])) {
67b8ba871bSPeter Wemm 		msgq(sp, M_ERR,
68b8ba871bSPeter Wemm 		    "106|Abbreviations must end with a \"word\" character");
69b8ba871bSPeter Wemm 			return (1);
70b8ba871bSPeter Wemm 	}
71b8ba871bSPeter Wemm 	for (p = cmdp->argv[0]->bp; *p != '\0'; ++p)
72*f0957ccaSPeter Wemm 		if (ISBLANK(p[0])) {
73b8ba871bSPeter Wemm 			msgq(sp, M_ERR,
74b8ba871bSPeter Wemm 			    "107|Abbreviations may not contain tabs or spaces");
75b8ba871bSPeter Wemm 			return (1);
76b8ba871bSPeter Wemm 		}
77b8ba871bSPeter Wemm 	if (cmdp->argv[0]->len > 2)
78b8ba871bSPeter Wemm 		for (p = cmdp->argv[0]->bp,
79b8ba871bSPeter Wemm 		    len = cmdp->argv[0]->len - 2; len; --len, ++p)
80b8ba871bSPeter Wemm 			if (inword(p[0]) != inword(p[1])) {
81b8ba871bSPeter Wemm 				msgq(sp, M_ERR,
82b8ba871bSPeter Wemm "108|Abbreviations may not mix word/non-word characters, except at the end");
83b8ba871bSPeter Wemm 				return (1);
84b8ba871bSPeter Wemm 			}
85b8ba871bSPeter Wemm 
86b8ba871bSPeter Wemm 	if (seq_set(sp, NULL, 0, cmdp->argv[0]->bp, cmdp->argv[0]->len,
87b8ba871bSPeter Wemm 	    cmdp->argv[1]->bp, cmdp->argv[1]->len, SEQ_ABBREV, SEQ_USERDEF))
88b8ba871bSPeter Wemm 		return (1);
89b8ba871bSPeter Wemm 
90b8ba871bSPeter Wemm 	F_SET(sp->gp, G_ABBREV);
91b8ba871bSPeter Wemm 	return (0);
92b8ba871bSPeter Wemm }
93b8ba871bSPeter Wemm 
94b8ba871bSPeter Wemm /*
95b8ba871bSPeter Wemm  * ex_unabbr -- :unabbreviate key
96b8ba871bSPeter Wemm  *      Delete an abbreviation.
97b8ba871bSPeter Wemm  *
98b8ba871bSPeter Wemm  * PUBLIC: int ex_unabbr __P((SCR *, EXCMD *));
99b8ba871bSPeter Wemm  */
100b8ba871bSPeter Wemm int
101*f0957ccaSPeter Wemm ex_unabbr(SCR *sp, EXCMD *cmdp)
102b8ba871bSPeter Wemm {
103b8ba871bSPeter Wemm 	ARGS *ap;
104b8ba871bSPeter Wemm 
105b8ba871bSPeter Wemm 	ap = cmdp->argv[0];
106b8ba871bSPeter Wemm 	if (!F_ISSET(sp->gp, G_ABBREV) ||
107b8ba871bSPeter Wemm 	    seq_delete(sp, ap->bp, ap->len, SEQ_ABBREV)) {
108*f0957ccaSPeter Wemm 		msgq_wstr(sp, M_ERR, ap->bp,
109b8ba871bSPeter Wemm 		    "109|\"%s\" is not an abbreviation");
110b8ba871bSPeter Wemm 		return (1);
111b8ba871bSPeter Wemm 	}
112b8ba871bSPeter Wemm 	return (0);
113b8ba871bSPeter Wemm }
114