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