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