0
|
1 /* Primitives for word-abbrev mode.
|
|
2 Copyright (C) 1985, 1986, 1992, 1993 Free Software Foundation, Inc.
|
|
3
|
|
4 This file is part of XEmacs.
|
|
5
|
|
6 XEmacs is free software; you can redistribute it and/or modify it
|
|
7 under the terms of the GNU General Public License as published by the
|
|
8 Free Software Foundation; either version 2, or (at your option) any
|
|
9 later version.
|
|
10
|
|
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 for more details.
|
|
15
|
|
16 You should have received a copy of the GNU General Public License
|
|
17 along with XEmacs; see the file COPYING. If not, write to
|
|
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
19 Boston, MA 02111-1307, USA. */
|
|
20
|
|
21 /* Synched up with: FSF 19.30. Note that there are many more functions in
|
|
22 FSF's abbrev.c. These have been moved into Lisp in XEmacs. */
|
|
23
|
|
24 /* Authorship:
|
|
25
|
|
26 FSF: Original version; a long time ago.
|
|
27 JWZ or Mly: Mostly moved into Lisp; maybe 1992.
|
|
28 Ben Wing: Some changes for Mule for 19.12.
|
167
|
29 Hrvoje Niksic: Largely rewritten in June 1997.
|
0
|
30 */
|
|
31
|
|
32 /* This file has been Mule-ized. */
|
|
33
|
|
34 #include <config.h>
|
|
35 #include "lisp.h"
|
|
36
|
|
37 #include "buffer.h"
|
|
38 #include "commands.h"
|
|
39 #include "insdel.h"
|
|
40 #include "syntax.h"
|
|
41 #include "window.h"
|
|
42
|
|
43 /* An abbrev table is an obarray.
|
|
44 Each defined abbrev is represented by a symbol in that obarray
|
|
45 whose print name is the abbreviation.
|
|
46 The symbol's value is a string which is the expansion.
|
|
47 If its function definition is non-nil, it is called
|
|
48 after the expansion is done.
|
|
49 The plist slot of the abbrev symbol is its usage count. */
|
|
50
|
|
51 /* The table of global abbrevs. These are in effect
|
|
52 in any buffer in which abbrev mode is turned on. */
|
|
53 Lisp_Object Vglobal_abbrev_table;
|
|
54
|
|
55 int abbrev_all_caps;
|
|
56
|
|
57 /* Non-nil => use this location as the start of abbrev to expand
|
|
58 (rather than taking the word before point as the abbrev) */
|
|
59 Lisp_Object Vabbrev_start_location;
|
|
60
|
|
61 /* Buffer that Vabbrev_start_location applies to */
|
|
62 Lisp_Object Vabbrev_start_location_buffer;
|
|
63
|
|
64 /* The symbol representing the abbrev most recently expanded */
|
|
65 Lisp_Object Vlast_abbrev;
|
|
66
|
|
67 /* A string for the actual text of the abbrev most recently expanded.
|
|
68 This has more info than Vlast_abbrev since case is significant. */
|
|
69 Lisp_Object Vlast_abbrev_text;
|
|
70
|
|
71 /* Character address of start of last abbrev expanded */
|
|
72 int last_abbrev_point;
|
|
73
|
167
|
74 Lisp_Object oblookup (Lisp_Object, CONST Bufbyte *, Bytecount);
|
|
75
|
0
|
76 /* Hook to run before expanding any abbrev. */
|
|
77 Lisp_Object Vpre_abbrev_expand_hook, Qpre_abbrev_expand_hook;
|
|
78
|
|
79
|
227
|
80 struct abbrev_match_mapper_closure {
|
|
81 struct buffer *buf;
|
|
82 struct Lisp_Char_Table *chartab;
|
|
83 Charcount point, maxlen;
|
|
84 struct Lisp_Symbol *found;
|
|
85 };
|
|
86
|
|
87 /* For use by abbrev_match(): Match SYMBOL's name against buffer text
|
|
88 before point, case-insensitively. When found, return non-zero, so
|
|
89 that map_obarray terminates mapping. */
|
|
90 static int
|
|
91 abbrev_match_mapper (Lisp_Object symbol, void *arg)
|
|
92 {
|
|
93 struct abbrev_match_mapper_closure *closure =
|
|
94 (struct abbrev_match_mapper_closure *)arg;
|
|
95 Charcount abbrev_length;
|
|
96 struct Lisp_Symbol *sym = XSYMBOL (symbol);
|
|
97 struct Lisp_String *abbrev;
|
|
98
|
|
99 /* symbol_value should be OK here, because abbrevs are not expected
|
|
100 to contain any SYMBOL_MAGIC stuff. */
|
|
101 if (UNBOUNDP (symbol_value (sym)) || NILP (symbol_value (sym)))
|
|
102 {
|
|
103 /* The symbol value of nil means that abbrev got undefined. */
|
|
104 return 0;
|
|
105 }
|
|
106 abbrev = symbol_name (sym);
|
|
107 abbrev_length = string_char_length (abbrev);
|
|
108 if (abbrev_length > closure->maxlen)
|
|
109 {
|
|
110 /* This abbrev is too large -- it wouldn't fit. */
|
|
111 return 0;
|
|
112 }
|
|
113 /* If `bar' is an abbrev, and a user presses `fubar<SPC>', we don't
|
|
114 normally want to expand it. OTOH, if the abbrev begins with
|
|
115 non-word syntax (e.g. `#if'), it is OK to abbreviate it anywhere. */
|
|
116 if (abbrev_length < closure->maxlen && abbrev_length > 0
|
|
117 && (WORD_SYNTAX_P (closure->chartab, string_char (abbrev, 0)))
|
|
118 && (WORD_SYNTAX_P (closure->chartab,
|
|
119 BUF_FETCH_CHAR (closure->buf,
|
|
120 closure->point - (abbrev_length + 1)))))
|
|
121 {
|
|
122 return 0;
|
|
123 }
|
|
124 /* Match abbreviation string against buffer text. */
|
|
125 {
|
|
126 Bufbyte *ptr = string_data (abbrev);
|
|
127 Charcount idx;
|
|
128
|
|
129 for (idx = 0; idx < abbrev_length; idx++)
|
|
130 {
|
|
131 if (DOWNCASE (closure->buf,
|
|
132 BUF_FETCH_CHAR (closure->buf,
|
|
133 closure->point - abbrev_length + idx))
|
|
134 != DOWNCASE (closure->buf, charptr_emchar (ptr)))
|
|
135 {
|
|
136 break;
|
|
137 }
|
|
138 INC_CHARPTR (ptr);
|
|
139 }
|
|
140 if (idx == abbrev_length)
|
|
141 {
|
|
142 /* This is the one. */
|
|
143 closure->found = sym;
|
|
144 return 1;
|
|
145 }
|
|
146 }
|
|
147 return 0;
|
|
148 }
|
|
149
|
167
|
150 /* Match the buffer text against names of symbols in obarray. Returns
|
|
151 the matching symbol, or 0 if not found. */
|
|
152 static struct Lisp_Symbol *
|
|
153 abbrev_match (struct buffer *buf, Lisp_Object obarray)
|
|
154 {
|
227
|
155 struct abbrev_match_mapper_closure closure;
|
167
|
156
|
227
|
157 /* Precalculate some stuff, so mapper function needn't to it in each
|
|
158 iteration. */
|
|
159 closure.buf = buf;
|
|
160 closure.point = BUF_PT (buf);
|
|
161 closure.maxlen = closure.point - BUF_BEGV (buf);
|
|
162 closure.chartab = XCHAR_TABLE (buf->mirror_syntax_table);
|
|
163 closure.found = 0;
|
167
|
164
|
227
|
165 map_obarray (obarray, abbrev_match_mapper, &closure);
|
167
|
166
|
227
|
167 return closure.found;
|
|
168 }
|
|
169
|
|
170 /* Take the word before point (or Vabbrev_start_location, if non-nil),
|
|
171 and look it up in OBARRAY, and return the symbol (or zero). This
|
|
172 used to be the default method of searching, with the obvious
|
|
173 limitation that the abbrevs may consist only of word characters.
|
|
174 It is an order of magnitude faster than the proper abbrev_match(),
|
|
175 but then again, vi is an order of magnitude faster than Emacs.
|
167
|
176
|
227
|
177 This speed difference should be unnoticable, though. I have tested
|
|
178 the degenerated cases of thousands of abbrevs being defined, and
|
|
179 abbrev_match() was still fast enough for normal operation. */
|
167
|
180 static struct Lisp_Symbol *
|
|
181 abbrev_oblookup (struct buffer *buf, Lisp_Object obarray)
|
|
182 {
|
|
183 Bufpos wordstart, wordend;
|
|
184 Bufbyte *word, *p;
|
|
185 Bytecount idx;
|
|
186 Lisp_Object lookup;
|
|
187
|
|
188 CHECK_VECTOR (obarray);
|
|
189
|
|
190 if (!NILP (Vabbrev_start_location))
|
|
191 {
|
|
192 wordstart = get_buffer_pos_char (buf, Vabbrev_start_location,
|
|
193 GB_COERCE_RANGE);
|
|
194 Vabbrev_start_location = Qnil;
|
227
|
195 /* Previously, abbrev-prefix-mark crockishly inserted a dash to
|
|
196 indicate the abbrev start point. It now uses an extent with
|
|
197 a begin glyph so there's no dash to remove. */
|
|
198 #if 0
|
167
|
199 if (wordstart != BUF_ZV (buf)
|
227
|
200 && BUF_FETCH_CHAR (buf, wordstart) == '-')
|
167
|
201 {
|
|
202 buffer_delete_range (buf, wordstart, wordstart + 1, 0);
|
|
203 }
|
227
|
204 #endif
|
167
|
205 wordend = BUF_PT (buf);
|
|
206 }
|
|
207 else
|
|
208 {
|
|
209 Bufpos point = BUF_PT (buf);
|
|
210
|
|
211 wordstart = scan_words (buf, point, -1);
|
|
212 if (!wordstart)
|
|
213 return 0;
|
|
214
|
|
215 wordend = scan_words (buf, wordstart, 1);
|
|
216 if (!wordend)
|
|
217 return 0;
|
|
218 if (wordend > BUF_ZV (buf))
|
|
219 wordend = BUF_ZV (buf);
|
|
220 if (wordend > point)
|
|
221 wordend = point;
|
|
222 /* Unlike the original function, we allow expansion only after
|
|
223 the abbrev, not preceded by a number of spaces. This is
|
|
224 because of consistency with abbrev_match. */
|
|
225 if (wordend < point)
|
|
226 return 0;
|
|
227 if (wordend <= wordstart)
|
|
228 return 0;
|
|
229 }
|
|
230
|
|
231 p = word = (Bufbyte *) alloca (MAX_EMCHAR_LEN * (wordend - wordstart));
|
|
232 for (idx = wordstart; idx < wordend; idx++)
|
|
233 {
|
|
234 Emchar c = BUF_FETCH_CHAR (buf, idx);
|
|
235 if (UPPERCASEP (buf, c))
|
|
236 c = DOWNCASE (buf, c);
|
|
237 p += set_charptr_emchar (p, c);
|
|
238 }
|
|
239 lookup = oblookup (obarray, word, p - word);
|
|
240 if (SYMBOLP (lookup) && !NILP (symbol_value (XSYMBOL (lookup))))
|
|
241 return XSYMBOL (lookup);
|
|
242 else
|
|
243 return NULL;
|
|
244 }
|
227
|
245
|
167
|
246 /* Return non-zero if OBARRAY contains an interned symbol ` '. */
|
|
247 static int
|
|
248 obarray_has_blank_p (Lisp_Object obarray)
|
|
249 {
|
|
250 Lisp_Object lookup;
|
|
251
|
|
252 lookup = oblookup (obarray, (Bufbyte *)" ", 1);
|
|
253 return SYMBOLP (lookup);
|
|
254 }
|
|
255
|
|
256 /* Analyze case in the buffer substring, and report it. */
|
|
257 static void
|
|
258 abbrev_count_case (struct buffer *buf, Bufpos pos, Charcount length,
|
|
259 int *lccount, int *uccount)
|
|
260 {
|
|
261 Emchar c;
|
|
262
|
|
263 *lccount = *uccount = 0;
|
|
264 while (length--)
|
|
265 {
|
|
266 c = BUF_FETCH_CHAR (buf, pos);
|
|
267 if (UPPERCASEP (buf, c))
|
|
268 ++*uccount;
|
|
269 else if (LOWERCASEP (buf, c))
|
|
270 ++*lccount;
|
|
271 ++pos;
|
|
272 }
|
|
273 }
|
227
|
274
|
20
|
275 DEFUN ("expand-abbrev", Fexpand_abbrev, 0, 0, "", /*
|
167
|
276 Expand the abbrev before point, if any.
|
0
|
277 Effective when explicitly called even when `abbrev-mode' is nil.
|
227
|
278 Returns the abbrev symbol, if expansion took place.
|
|
279 If no abbrev matched, but `pre-abbrev-expand-hook' changed the buffer,
|
|
280 returns t.
|
20
|
281 */
|
|
282 ())
|
0
|
283 {
|
|
284 /* This function can GC */
|
|
285 struct buffer *buf = current_buffer;
|
|
286 int oldmodiff = BUF_MODIFF (buf);
|
167
|
287 Lisp_Object pre_modiff_p;
|
|
288 Bufpos point; /* position of point */
|
|
289 Bufpos abbrev_start; /* position of abbreviation beginning */
|
0
|
290
|
167
|
291 struct Lisp_Symbol *(*fun) (struct buffer *, Lisp_Object);
|
|
292
|
|
293 struct Lisp_Symbol *abbrev_symbol;
|
|
294 struct Lisp_String *abbrev_string;
|
|
295 Lisp_Object expansion, count, hook;
|
175
|
296 Charcount abbrev_length;
|
167
|
297 int lccount, uccount;
|
|
298
|
0
|
299 run_hook (Qpre_abbrev_expand_hook);
|
|
300 /* If the hook changes the buffer, treat that as having "done an
|
|
301 expansion". */
|
167
|
302 pre_modiff_p = (BUF_MODIFF (buf) != oldmodiff ? Qt : Qnil);
|
0
|
303
|
167
|
304 abbrev_symbol = NULL;
|
0
|
305 if (!BUFFERP (Vabbrev_start_location_buffer) ||
|
|
306 XBUFFER (Vabbrev_start_location_buffer) != buf)
|
|
307 Vabbrev_start_location = Qnil;
|
227
|
308 /* We use the more general abbrev_match() if the obarray blank flag
|
167
|
309 is not set, and Vabbrev_start_location is nil. Otherwise, use
|
227
|
310 abbrev_oblookup(). */
|
167
|
311 #define MATCHFUN(tbl) ((obarray_has_blank_p (tbl) \
|
|
312 && NILP (Vabbrev_start_location)) \
|
|
313 ? abbrev_match : abbrev_oblookup)
|
|
314 if (!NILP (buf->abbrev_table))
|
0
|
315 {
|
167
|
316 fun = MATCHFUN (buf->abbrev_table);
|
|
317 abbrev_symbol = fun (buf, buf->abbrev_table);
|
0
|
318 }
|
167
|
319 if (!abbrev_symbol && !NILP (Vglobal_abbrev_table))
|
|
320 {
|
|
321 fun = MATCHFUN (Vglobal_abbrev_table);
|
|
322 abbrev_symbol = fun (buf, Vglobal_abbrev_table);
|
|
323 }
|
|
324 if (!abbrev_symbol)
|
|
325 return pre_modiff_p;
|
0
|
326
|
167
|
327 /* NOTE: we hope that `pre-abbrev-expand-hook' didn't do something
|
227
|
328 nasty, such as changed the buffer. Here we protect against the
|
|
329 buffer getting killed. */
|
|
330 if (! BUFFER_LIVE_P (buf))
|
|
331 return Qnil;
|
167
|
332 point = BUF_PT (buf);
|
0
|
333
|
167
|
334 /* OK, we're out of the must-be-fast part. An abbreviation matched.
|
|
335 Now find the parameters, insert the expansion, and make it all
|
227
|
336 look pretty. */
|
167
|
337 abbrev_string = symbol_name (abbrev_symbol);
|
|
338 abbrev_length = string_char_length (abbrev_string);
|
|
339 abbrev_start = point - abbrev_length;
|
0
|
340
|
167
|
341 expansion = symbol_value (abbrev_symbol);
|
|
342 CHECK_STRING (expansion);
|
|
343
|
|
344 count = symbol_plist (abbrev_symbol); /* Gag */
|
|
345 if (NILP (count))
|
227
|
346 count = Qzero;
|
0
|
347 else
|
167
|
348 CHECK_NATNUM (count);
|
|
349 symbol_plist (abbrev_symbol) = make_int (1 + XINT (count));
|
|
350
|
|
351 /* Count the case in the original text. */
|
|
352 abbrev_count_case (buf, abbrev_start, abbrev_length, &lccount, &uccount);
|
0
|
353
|
167
|
354 /* Remember the last abbrev text, location, etc. */
|
|
355 XSETSYMBOL (Vlast_abbrev, abbrev_symbol);
|
0
|
356 Vlast_abbrev_text =
|
167
|
357 make_string_from_buffer (buf, abbrev_start, abbrev_length);
|
|
358 last_abbrev_point = abbrev_start;
|
0
|
359
|
167
|
360 /* Add an undo boundary, in case we are doing this for a
|
|
361 self-inserting command which has avoided making one so far. */
|
|
362 if (INTERACTIVE)
|
|
363 Fundo_boundary ();
|
0
|
364
|
167
|
365 /* Remove the abbrev */
|
|
366 buffer_delete_range (buf, abbrev_start, point, 0);
|
|
367 /* And insert the expansion. */
|
|
368 buffer_insert_lisp_string (buf, expansion);
|
|
369 point = BUF_PT (buf);
|
0
|
370
|
167
|
371 /* Now fiddle with the case. */
|
0
|
372 if (uccount && !lccount)
|
|
373 {
|
|
374 /* Abbrev was all caps */
|
167
|
375 if (!abbrev_all_caps
|
|
376 && scan_words (buf, point, -1) > scan_words (buf, abbrev_start, 1))
|
|
377 {
|
|
378 Fupcase_initials_region (make_int (abbrev_start), make_int (point),
|
|
379 make_buffer (buf));
|
|
380 }
|
|
381 else
|
|
382 {
|
|
383 /* If expansion is one word, or if user says so, upcase it all. */
|
|
384 Fupcase_region (make_int (abbrev_start), make_int (point),
|
|
385 make_buffer (buf));
|
|
386 }
|
0
|
387 }
|
|
388 else if (uccount)
|
|
389 {
|
|
390 /* Abbrev included some caps. Cap first initial of expansion */
|
167
|
391 Bufpos pos = abbrev_start;
|
0
|
392 /* Find the initial. */
|
167
|
393 while (pos < point
|
|
394 && !WORD_SYNTAX_P (XCHAR_TABLE (buf->mirror_syntax_table),
|
70
|
395 BUF_FETCH_CHAR (buf, pos)))
|
167
|
396 pos++;
|
0
|
397 /* Change just that. */
|
167
|
398 Fupcase_initials_region (make_int (pos), make_int (pos + 1),
|
|
399 make_buffer (buf));
|
0
|
400 }
|
|
401
|
167
|
402 hook = symbol_function (abbrev_symbol);
|
0
|
403 if (!NILP (hook) && !UNBOUNDP (hook))
|
|
404 call0 (hook);
|
|
405
|
227
|
406 return Vlast_abbrev;
|
0
|
407 }
|
|
408
|
227
|
409
|
0
|
410 void
|
|
411 syms_of_abbrev (void)
|
|
412 {
|
|
413 defsymbol (&Qpre_abbrev_expand_hook, "pre-abbrev-expand-hook");
|
20
|
414 DEFSUBR (Fexpand_abbrev);
|
0
|
415 }
|
|
416
|
|
417 void
|
|
418 vars_of_abbrev (void)
|
|
419 {
|
|
420 DEFVAR_LISP ("global-abbrev-table", &Vglobal_abbrev_table /*
|
|
421 The abbrev table whose abbrevs affect all buffers.
|
|
422 Each buffer may also have a local abbrev table.
|
|
423 If it does, the local table overrides the global one
|
|
424 for any particular abbrev defined in both.
|
|
425 */ );
|
|
426 Vglobal_abbrev_table = Qnil; /* setup by Lisp code */
|
|
427
|
|
428 DEFVAR_LISP ("last-abbrev", &Vlast_abbrev /*
|
|
429 The abbrev-symbol of the last abbrev expanded.
|
|
430 See the function `abbrev-symbol'.
|
|
431 */ );
|
|
432
|
|
433 DEFVAR_LISP ("last-abbrev-text", &Vlast_abbrev_text /*
|
|
434 The exact text of the last abbrev expanded.
|
|
435 nil if the abbrev has already been unexpanded.
|
|
436 */ );
|
|
437
|
|
438 DEFVAR_INT ("last-abbrev-location", &last_abbrev_point /*
|
|
439 The location of the start of the last abbrev expanded.
|
|
440 */ );
|
|
441
|
|
442 Vlast_abbrev = Qnil;
|
|
443 Vlast_abbrev_text = Qnil;
|
|
444 last_abbrev_point = 0;
|
|
445
|
|
446 DEFVAR_LISP ("abbrev-start-location", &Vabbrev_start_location /*
|
|
447 Buffer position for `expand-abbrev' to use as the start of the abbrev.
|
|
448 nil means use the word before point as the abbrev.
|
|
449 Calling `expand-abbrev' sets this to nil.
|
|
450 */ );
|
|
451 Vabbrev_start_location = Qnil;
|
|
452
|
|
453 DEFVAR_LISP ("abbrev-start-location-buffer", &Vabbrev_start_location_buffer /*
|
|
454 Buffer that `abbrev-start-location' has been set for.
|
|
455 Trying to expand an abbrev in any other buffer clears `abbrev-start-location'.
|
|
456 */ );
|
|
457 Vabbrev_start_location_buffer = Qnil;
|
|
458
|
|
459 DEFVAR_BOOL ("abbrev-all-caps", &abbrev_all_caps /*
|
167
|
460 *Non-nil means expand multi-word abbrevs all caps if abbrev was so.
|
0
|
461 */ );
|
|
462 abbrev_all_caps = 0;
|
|
463
|
|
464 DEFVAR_LISP ("pre-abbrev-expand-hook", &Vpre_abbrev_expand_hook /*
|
|
465 Function or functions to be called before abbrev expansion is done.
|
|
466 This is the first thing that `expand-abbrev' does, and so this may change
|
|
467 the current abbrev table before abbrev lookup happens.
|
|
468 */ );
|
|
469 Vpre_abbrev_expand_hook = Qnil;
|
|
470 }
|