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