Debian Codegraph @

abbrev.c

raw
1 /**************************************************************************
2 * This program is Copyright (C) 1986-2002 by Jonathan Payne. JOVE is *
3 * provided by Jonathan and Jovehacks without charge and without *
4 * warranty. You may copy, modify, and/or distribute JOVE, provided that *
5 * this notice is included in all the source files and documentation. *
6 **************************************************************************/
7
8 #include "jove.h"
9
10 #ifdef ABBREV /* the body is the rest of this file */
11
12 #include "fp.h"
13 #include "jctype.h"
14 #include "abbrev.h"
15 #include "ask.h"
16 #include "commands.h" /* for ExecCmd() */
17 #include "delete.h"
18 #include "insert.h"
19 #include "disp.h"
20 #include "fmt.h"
21 #include "move.h"
22 #include "wind.h"
23
24 #define HASHSIZE 20
25
26 struct abbrev {
27 unsigned int a_hash;
28 char *a_abbrev,
29 *a_phrase;
30 struct abbrev *a_next;
31 const data_obj *a_cmdhook;
32 };
33
34 private void
35 define(struct abbrev **, char *, char *);
36
37 #define GLOBAL NMAJORS
38 private struct abbrev *A_tables[NMAJORS + 1][HASHSIZE]; /* Must be zeroed! */
39
40 jbool AutoCaseAbbrev = YES; /* VAR: automatically do case on abbreviations */
41
42 private unsigned int
43 hash(const char *a)
44 {
45 unsigned int hashval = 0;
46 char c;
47
48 while ((c = *a++) != '\0')
49 hashval = (hashval << 2) + c;
50
51 return hashval;
52 }
53
54 private void
55 def_abbrev(struct abbrev *table[HASHSIZE])
56 {
57 char abbrev[100],
58 phrase[100];
59
60 jamstr(abbrev, ask((char *)NULL, "abbrev: "));
61 jamstr(phrase, ask((char *)NULL, "abbrev: %s phrase: ", abbrev));
62 define(table, abbrev, phrase);
63 }
64
65 private struct abbrev *
66 lookup_abbrev(struct abbrev *table[HASHSIZE], const char *abbrev)
67 {
68 struct abbrev *ap;
69 unsigned int h;
70
71 h = hash(abbrev);
72 for (ap = table[h % HASHSIZE]; ap; ap = ap->a_next)
73 if (ap->a_hash == h && strcmp(ap->a_abbrev, abbrev) == 0)
74 break;
75 return ap;
76 }
77
78 private void
79 define(struct abbrev **table, char *abbrev, char *phrase)
80 {
81 struct abbrev *ap;
82
83 ap = lookup_abbrev(table, abbrev);
84 if (ap == NULL) {
85 unsigned int h = hash(abbrev);
86
87 ap = (struct abbrev *) emalloc(sizeof *ap);
88 ap->a_hash = h;
89 ap->a_abbrev = copystr(abbrev);
90 h %= HASHSIZE;
91 ap->a_next = table[h];
92 ap->a_cmdhook = NULL;
93 table[h] = ap;
94 } else
95 free(ap->a_phrase);
96 ap->a_phrase = copystr(phrase);
97 }
98
99 void
100 AbbrevExpand(void)
101 {
102 char wordbuf[100],
103 *wp = wordbuf,
104 *cp,
105 c;
106 int col;
107 int UC_count = 0;
108 struct abbrev *ap;
109
110 col = curchar;
111 while (col != 0 && jisident(linebuf[col - 1]))
112 col -= 1;
113 if (curchar-col >= (int)sizeof(wordbuf))
114 return; /* too long for us -- ignore it */
115
116 while (col < curchar) {
117 c = linebuf[col];
118 if (AutoCaseAbbrev && jisupper(c)) {
119 UC_count += 1;
120 c = CharDowncase(c);
121 }
122 *wp++ = c;
123 col += 1;
124 }
125 *wp = '\0';
126
127 if ((ap = lookup_abbrev(A_tables[curbuf->b_major], wordbuf)) != NULL
128 || (ap = lookup_abbrev(A_tables[GLOBAL], wordbuf)) != NULL)
129 {
130 del_char(BACKWARD, (int)(wp - wordbuf), NO);
131
132 for (cp = ap->a_phrase; (c = *cp) != '\0'; ) {
133 if (UC_count > 0 && jislower(c)
134 && (cp == ap->a_phrase
135 || (UC_count > 1 && (jiswhite(cp[-1]) || cp[-1] == '-'))))
136 c = CharUpcase(c);
137 insert_c(c, 1);
138 cp += 1;
139 }
140 if (ap->a_cmdhook != NULL)
141 ExecCmd(ap->a_cmdhook);
142 }
143 }
144
145 private const char *const mode_names[NMAJORS + 1] = {
146 "Fundamental Mode",
147 "Text Mode",
148 "C Mode",
149 #ifdef LISP
150 "Lisp Mode",
151 #endif
152 "Global"
153 };
154
155 private void
156 save_abbrevs(char *file)
157 {
158 File *fp;
159 struct abbrev *ap,
160 **tp;
161 char buf[LBSIZE];
162 int i,
163 count = 0;
164
165 fp = open_file(file, buf, F_WRITE, YES);
166 for (i = 0; i <= GLOBAL; i++) {
167 fwritef(fp, "------%s abbrevs------\n", mode_names[i]);
168 for (tp = A_tables[i]; tp < &A_tables[i][HASHSIZE]; tp++)
169 for (ap = *tp; ap; ap = ap->a_next) {
170 fwritef(fp, "%s:%s\n",
171 ap->a_abbrev,
172 ap->a_phrase);
173 count += 1;
174 }
175 }
176 f_close(fp);
177 add_mess(" %d written.", count);
178 }
179
180 private void
181 rest_abbrevs(char *file)
182 {
183 int mode = -1, /* Should be ++'d immediately */
184 lnum = 0;
185 char *phrase_p;
186 File *fp;
187 char buf[LBSIZE];
188
189 fp = open_file(file, buf, F_READ, YES);
190 while (mode<=GLOBAL && !f_gets(fp, genbuf, (size_t) LBSIZE)
191 && genbuf[0] != '\0')
192 {
193 static const char sep[] = "------";
194
195 lnum += 1;
196 if (strncmp(genbuf, sep, sizeof(sep)-1) == 0) {
197 mode += 1;
198 } else if (mode == -1 || (phrase_p = strchr(genbuf, ':')) == NULL) {
199 complain("Abbrev. format error, line %d.", file, lnum);
200 /* NOTREACHED */
201 } else {
202 *phrase_p++ = '\0'; /* Null terminate the abbrev. */
203 define(A_tables[mode], genbuf, phrase_p);
204 }
205 }
206 f_close(fp);
207 message(NullStr);
208 }
209
210 void
211 DefGAbbrev(void)
212 {
213 def_abbrev(A_tables[GLOBAL]);
214 }
215
216 void
217 DefMAbbrev(void)
218 {
219 def_abbrev(A_tables[curbuf->b_major]);
220 }
221
222 void
223 SaveAbbrevs(void)
224 {
225 char filebuf[FILESIZE];
226
227 save_abbrevs(ask_file((char *)NULL, (char *)NULL, filebuf));
228 }
229
230 void
231 RestAbbrevs(void)
232 {
233 char filebuf[FILESIZE];
234
235 rest_abbrevs(ask_file((char *)NULL, (char *)NULL, filebuf));
236 }
237
238 void
239 EditAbbrevs(void)
240 {
241 static const char
242 EditName[] = "Abbreviation Edit";
243 char tname[128];
244 Buffer *obuf = curbuf,
245 *ebuf;
246
247 if ((ebuf = buf_exists(EditName)) != NULL) {
248 if (ebuf->b_type != B_SCRATCH)
249 confirm("Over-write buffer %b? ", ebuf);
250 }
251 SetBuf(ebuf = do_select(curwind, EditName));
252 ebuf->b_type = B_SCRATCH;
253 buf_clear(ebuf);
254 /* Empty buffer. Save the definitions to a tmp file
255 * and read them into this buffer so we can edit them.
256 */
257 PathCat(tname, sizeof(tname), TmpDir,
258 #ifdef MAC
259 ".jabbXXX" /* must match string in mac.c:Ffilter() */
260 #else
261 "jaXXXXXX"
262 #endif
263 );
264
265 /* do a safe form of mktemp */
266 close(MakeTemp(tname, "abbrevs"));
267
268 save_abbrevs(tname);
269 setfname(ebuf, tname);
270 read_file(tname, NO);
271 message("[Edit definitions and then type ^X ^C]");
272 Recur(); /* We edit them ... now */
273 if (IsModified(ebuf)) {
274 file_write(tname, NO);
275 rest_abbrevs(tname);
276 }
277 (void) unlink(tname);
278 if (valid_bp(obuf))
279 SetBuf(do_select(curwind, obuf->b_name));
280 }
281
282 void
283 BindMtoW(void)
284 {
285 struct abbrev *ap;
286 const char *word = ask((char *)NULL, "Word: ");
287
288 if ((ap = lookup_abbrev(A_tables[curbuf->b_major], word)) == NULL
289 && (ap = lookup_abbrev(A_tables[GLOBAL], word)) == NULL) {
290 complain("%s: unknown abbrev.", word);
291 /* NOTREACHED */
292 }
293
294 ap->a_cmdhook = findmac("Macro: ");
295 }
296
297 #endif /* ABBREV */