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
|
167
|
80 /* Match the buffer text against names of symbols in obarray. Returns
|
|
81 the matching symbol, or 0 if not found. */
|
|
82
|
|
83 static struct Lisp_Symbol *
|
|
84 abbrev_match (struct buffer *buf, Lisp_Object obarray)
|
|
85 {
|
|
86 Bufpos point = BUF_PT (buf);
|
|
87 Bufpos maxlen = point - BUF_BEGV (buf);
|
|
88 Charcount idx;
|
|
89
|
|
90 struct Lisp_Char_Table *chartab = XCHAR_TABLE (buf->mirror_syntax_table);
|
|
91 struct Lisp_String *abbrev;
|
|
92 struct Lisp_Vector *obvec;
|
173
|
93 struct Lisp_Symbol *sym = NULL;
|
167
|
94 Charcount abbrev_length;
|
|
95 Lisp_Object tail;
|
|
96 int i, found;
|
|
97
|
|
98 CHECK_VECTOR (obarray);
|
|
99 obvec = XVECTOR (obarray);
|
|
100
|
|
101 /* The obarray-traversing code is copied from `map_obarray'. */
|
|
102 found = 0;
|
|
103 for (i = vector_length (obvec) - 1; i >= 0; i--)
|
|
104 {
|
|
105 tail = vector_data (obvec)[i];
|
|
106 if (SYMBOLP (tail))
|
|
107 while (1)
|
|
108 {
|
|
109 sym = XSYMBOL (tail);
|
|
110 if (UNBOUNDP (symbol_value (sym)) || NILP (symbol_value (sym)))
|
|
111 {
|
|
112 /* The symbol value of nil means that abbrev got
|
|
113 undefined. */
|
|
114 goto next;
|
|
115 }
|
|
116 abbrev = symbol_name (sym);
|
|
117 abbrev_length = string_char_length (abbrev);
|
|
118 if (abbrev_length > maxlen)
|
|
119 {
|
|
120 /* This abbrev is too large -- it wouldn't fit. */
|
|
121 goto next;
|
|
122 }
|
|
123 /* If `bar' is an abbrev, and a user presses `fubar<SPC>',
|
|
124 we don't normally want to expand it. OTOH, if the
|
|
125 abbrev begins with non-word syntax, it is OK to
|
|
126 abbreviate it anywhere. */
|
|
127 if (abbrev_length < maxlen && abbrev_length > 0
|
|
128 && (WORD_SYNTAX_P (chartab, string_char (abbrev, 0)))
|
|
129 && (WORD_SYNTAX_P (chartab,
|
|
130 BUF_FETCH_CHAR (buf, point
|
|
131 - (abbrev_length + 1)))))
|
|
132 {
|
|
133 goto next;
|
|
134 }
|
|
135 /* Match abbreviation string against buffer text. */
|
|
136 for (idx = abbrev_length - 1; idx >= 0; idx--)
|
|
137 {
|
|
138 if (DOWNCASE (buf, BUF_FETCH_CHAR (buf, point -
|
|
139 (abbrev_length - idx)))
|
|
140 != DOWNCASE (buf, string_char (abbrev, idx)))
|
|
141 break;
|
|
142 }
|
|
143 if (idx < 0)
|
|
144 {
|
|
145 found = 1;
|
|
146 break;
|
|
147 }
|
|
148 next:
|
|
149 sym = symbol_next (XSYMBOL (tail));
|
|
150 if (!sym)
|
|
151 break;
|
|
152 XSETSYMBOL (tail, sym);
|
|
153 } /* while */
|
|
154 if (found)
|
|
155 break;
|
|
156 } /* for */
|
|
157
|
|
158 return found ? sym : 0;
|
|
159 }
|
|
160
|
|
161 /* Take the word before point, and look it up in OBARRAY, and return
|
|
162 the symbol (or nil). This used to be the default method of
|
|
163 searching, with the obvious limitation that the abbrevs may consist
|
|
164 only of word characters. It is an order of magnitued faster than
|
|
165 the proper `abbrev_match', but then again, vi is an order of
|
|
166 magnitude faster than Emacs. */
|
|
167 static struct Lisp_Symbol *
|
|
168 abbrev_oblookup (struct buffer *buf, Lisp_Object obarray)
|
|
169 {
|
|
170 Bufpos wordstart, wordend;
|
|
171 Bufbyte *word, *p;
|
|
172 Bytecount idx;
|
|
173 Lisp_Object lookup;
|
|
174
|
|
175 CHECK_VECTOR (obarray);
|
|
176
|
|
177 if (!NILP (Vabbrev_start_location))
|
|
178 {
|
|
179 wordstart = get_buffer_pos_char (buf, Vabbrev_start_location,
|
|
180 GB_COERCE_RANGE);
|
|
181 Vabbrev_start_location = Qnil;
|
209
|
182 /*
|
|
183 * Previously, abbrev-prefix-mark inserted a dash to indicate the
|
|
184 * abbrev start point. It now uses an extent with a begin
|
|
185 * glyph so there's no dash to remove.
|
|
186 */
|
|
187 /*
|
167
|
188 if (wordstart != BUF_ZV (buf)
|
|
189 && BUF_FETCH_CHAR (buf, wordstart) == '-')
|
|
190 {
|
|
191 buffer_delete_range (buf, wordstart, wordstart + 1, 0);
|
|
192 }
|
209
|
193 */
|
167
|
194 wordend = BUF_PT (buf);
|
|
195 }
|
|
196 else
|
|
197 {
|
|
198 Bufpos point = BUF_PT (buf);
|
|
199
|
|
200 wordstart = scan_words (buf, point, -1);
|
|
201 if (!wordstart)
|
|
202 return 0;
|
|
203
|
|
204 wordend = scan_words (buf, wordstart, 1);
|
|
205 if (!wordend)
|
|
206 return 0;
|
|
207 if (wordend > BUF_ZV (buf))
|
|
208 wordend = BUF_ZV (buf);
|
|
209 if (wordend > point)
|
|
210 wordend = point;
|
|
211 /* Unlike the original function, we allow expansion only after
|
|
212 the abbrev, not preceded by a number of spaces. This is
|
|
213 because of consistency with abbrev_match. */
|
|
214 if (wordend < point)
|
|
215 return 0;
|
|
216 if (wordend <= wordstart)
|
|
217 return 0;
|
|
218 }
|
|
219
|
|
220 p = word = (Bufbyte *) alloca (MAX_EMCHAR_LEN * (wordend - wordstart));
|
|
221 for (idx = wordstart; idx < wordend; idx++)
|
|
222 {
|
|
223 Emchar c = BUF_FETCH_CHAR (buf, idx);
|
|
224 if (UPPERCASEP (buf, c))
|
|
225 c = DOWNCASE (buf, c);
|
|
226 p += set_charptr_emchar (p, c);
|
|
227 }
|
|
228 lookup = oblookup (obarray, word, p - word);
|
|
229 if (SYMBOLP (lookup) && !NILP (symbol_value (XSYMBOL (lookup))))
|
|
230 return XSYMBOL (lookup);
|
|
231 else
|
|
232 return NULL;
|
|
233 }
|
|
234
|
|
235 /* Return non-zero if OBARRAY contains an interned symbol ` '. */
|
|
236 static int
|
|
237 obarray_has_blank_p (Lisp_Object obarray)
|
|
238 {
|
|
239 Lisp_Object lookup;
|
|
240
|
|
241 lookup = oblookup (obarray, (Bufbyte *)" ", 1);
|
|
242 return SYMBOLP (lookup);
|
|
243 }
|
|
244
|
|
245 /* Analyze case in the buffer substring, and report it. */
|
|
246 static void
|
|
247 abbrev_count_case (struct buffer *buf, Bufpos pos, Charcount length,
|
|
248 int *lccount, int *uccount)
|
|
249 {
|
|
250 Emchar c;
|
|
251
|
|
252 *lccount = *uccount = 0;
|
|
253 while (length--)
|
|
254 {
|
|
255 c = BUF_FETCH_CHAR (buf, pos);
|
|
256 if (UPPERCASEP (buf, c))
|
|
257 ++*uccount;
|
|
258 else if (LOWERCASEP (buf, c))
|
|
259 ++*lccount;
|
|
260 ++pos;
|
|
261 }
|
|
262 }
|
0
|
263
|
20
|
264 DEFUN ("expand-abbrev", Fexpand_abbrev, 0, 0, "", /*
|
167
|
265 Expand the abbrev before point, if any.
|
0
|
266 Effective when explicitly called even when `abbrev-mode' is nil.
|
|
267 Returns t if expansion took place.
|
20
|
268 */
|
|
269 ())
|
0
|
270 {
|
|
271 /* This function can GC */
|
|
272 struct buffer *buf = current_buffer;
|
|
273 int oldmodiff = BUF_MODIFF (buf);
|
167
|
274 Lisp_Object pre_modiff_p;
|
|
275 Bufpos point; /* position of point */
|
|
276 Bufpos abbrev_start; /* position of abbreviation beginning */
|
0
|
277
|
167
|
278 struct Lisp_Symbol *(*fun) (struct buffer *, Lisp_Object);
|
|
279
|
|
280 struct Lisp_Symbol *abbrev_symbol;
|
|
281 struct Lisp_String *abbrev_string;
|
|
282 Lisp_Object expansion, count, hook;
|
175
|
283 Charcount abbrev_length;
|
167
|
284 int lccount, uccount;
|
|
285
|
0
|
286 run_hook (Qpre_abbrev_expand_hook);
|
|
287 /* If the hook changes the buffer, treat that as having "done an
|
|
288 expansion". */
|
167
|
289 pre_modiff_p = (BUF_MODIFF (buf) != oldmodiff ? Qt : Qnil);
|
0
|
290
|
167
|
291 abbrev_symbol = NULL;
|
0
|
292 if (!BUFFERP (Vabbrev_start_location_buffer) ||
|
|
293 XBUFFER (Vabbrev_start_location_buffer) != buf)
|
|
294 Vabbrev_start_location = Qnil;
|
167
|
295 /* We use the more general `abbrev_match' if the obarray blank flag
|
|
296 is not set, and Vabbrev_start_location is nil. Otherwise, use
|
|
297 `abbrev_oblookup'. */
|
|
298 #define MATCHFUN(tbl) ((obarray_has_blank_p (tbl) \
|
|
299 && NILP (Vabbrev_start_location)) \
|
|
300 ? abbrev_match : abbrev_oblookup)
|
|
301 if (!NILP (buf->abbrev_table))
|
0
|
302 {
|
167
|
303 fun = MATCHFUN (buf->abbrev_table);
|
|
304 abbrev_symbol = fun (buf, buf->abbrev_table);
|
0
|
305 }
|
167
|
306 if (!abbrev_symbol && !NILP (Vglobal_abbrev_table))
|
|
307 {
|
|
308 fun = MATCHFUN (Vglobal_abbrev_table);
|
|
309 abbrev_symbol = fun (buf, Vglobal_abbrev_table);
|
|
310 }
|
|
311 if (!abbrev_symbol)
|
|
312 return pre_modiff_p;
|
0
|
313
|
167
|
314 /* NOTE: we hope that `pre-abbrev-expand-hook' didn't do something
|
|
315 nasty, such as changed (or killed) the buffer. */
|
|
316 point = BUF_PT (buf);
|
0
|
317
|
167
|
318 /* OK, we're out of the must-be-fast part. An abbreviation matched.
|
|
319 Now find the parameters, insert the expansion, and make it all
|
|
320 look pretty. */
|
|
321 abbrev_string = symbol_name (abbrev_symbol);
|
|
322 abbrev_length = string_char_length (abbrev_string);
|
|
323 abbrev_start = point - abbrev_length;
|
0
|
324
|
167
|
325 expansion = symbol_value (abbrev_symbol);
|
|
326 CHECK_STRING (expansion);
|
|
327
|
|
328 count = symbol_plist (abbrev_symbol); /* Gag */
|
|
329 if (NILP (count))
|
|
330 count = make_int (0);
|
0
|
331 else
|
167
|
332 CHECK_NATNUM (count);
|
|
333 symbol_plist (abbrev_symbol) = make_int (1 + XINT (count));
|
|
334
|
|
335 /* Count the case in the original text. */
|
|
336 abbrev_count_case (buf, abbrev_start, abbrev_length, &lccount, &uccount);
|
0
|
337
|
167
|
338 /* Remember the last abbrev text, location, etc. */
|
|
339 XSETSYMBOL (Vlast_abbrev, abbrev_symbol);
|
0
|
340 Vlast_abbrev_text =
|
167
|
341 make_string_from_buffer (buf, abbrev_start, abbrev_length);
|
|
342 last_abbrev_point = abbrev_start;
|
0
|
343
|
167
|
344 /* Add an undo boundary, in case we are doing this for a
|
|
345 self-inserting command which has avoided making one so far. */
|
|
346 if (INTERACTIVE)
|
|
347 Fundo_boundary ();
|
0
|
348
|
167
|
349 /* Remove the abbrev */
|
|
350 buffer_delete_range (buf, abbrev_start, point, 0);
|
|
351 /* And insert the expansion. */
|
|
352 buffer_insert_lisp_string (buf, expansion);
|
|
353 point = BUF_PT (buf);
|
0
|
354
|
167
|
355 /* Now fiddle with the case. */
|
0
|
356 if (uccount && !lccount)
|
|
357 {
|
|
358 /* Abbrev was all caps */
|
167
|
359 if (!abbrev_all_caps
|
|
360 && scan_words (buf, point, -1) > scan_words (buf, abbrev_start, 1))
|
|
361 {
|
|
362 Fupcase_initials_region (make_int (abbrev_start), make_int (point),
|
|
363 make_buffer (buf));
|
|
364 }
|
|
365 else
|
|
366 {
|
|
367 /* If expansion is one word, or if user says so, upcase it all. */
|
|
368 Fupcase_region (make_int (abbrev_start), make_int (point),
|
|
369 make_buffer (buf));
|
|
370 }
|
0
|
371 }
|
|
372 else if (uccount)
|
|
373 {
|
|
374 /* Abbrev included some caps. Cap first initial of expansion */
|
167
|
375 Bufpos pos = abbrev_start;
|
0
|
376 /* Find the initial. */
|
167
|
377 while (pos < point
|
|
378 && !WORD_SYNTAX_P (XCHAR_TABLE (buf->mirror_syntax_table),
|
70
|
379 BUF_FETCH_CHAR (buf, pos)))
|
167
|
380 pos++;
|
0
|
381 /* Change just that. */
|
167
|
382 Fupcase_initials_region (make_int (pos), make_int (pos + 1),
|
|
383 make_buffer (buf));
|
0
|
384 }
|
|
385
|
167
|
386 hook = symbol_function (abbrev_symbol);
|
0
|
387 if (!NILP (hook) && !UNBOUNDP (hook))
|
|
388 call0 (hook);
|
|
389
|
|
390 return Qt;
|
|
391 }
|
|
392
|
167
|
393
|
0
|
394 void
|
|
395 syms_of_abbrev (void)
|
|
396 {
|
|
397 defsymbol (&Qpre_abbrev_expand_hook, "pre-abbrev-expand-hook");
|
20
|
398 DEFSUBR (Fexpand_abbrev);
|
0
|
399 }
|
|
400
|
|
401 void
|
|
402 vars_of_abbrev (void)
|
|
403 {
|
|
404 DEFVAR_LISP ("global-abbrev-table", &Vglobal_abbrev_table /*
|
|
405 The abbrev table whose abbrevs affect all buffers.
|
|
406 Each buffer may also have a local abbrev table.
|
|
407 If it does, the local table overrides the global one
|
|
408 for any particular abbrev defined in both.
|
|
409 */ );
|
|
410 Vglobal_abbrev_table = Qnil; /* setup by Lisp code */
|
|
411
|
|
412 DEFVAR_LISP ("last-abbrev", &Vlast_abbrev /*
|
|
413 The abbrev-symbol of the last abbrev expanded.
|
|
414 See the function `abbrev-symbol'.
|
|
415 */ );
|
|
416
|
|
417 DEFVAR_LISP ("last-abbrev-text", &Vlast_abbrev_text /*
|
|
418 The exact text of the last abbrev expanded.
|
|
419 nil if the abbrev has already been unexpanded.
|
|
420 */ );
|
|
421
|
|
422 DEFVAR_INT ("last-abbrev-location", &last_abbrev_point /*
|
|
423 The location of the start of the last abbrev expanded.
|
|
424 */ );
|
|
425
|
|
426 Vlast_abbrev = Qnil;
|
|
427 Vlast_abbrev_text = Qnil;
|
|
428 last_abbrev_point = 0;
|
|
429
|
|
430 DEFVAR_LISP ("abbrev-start-location", &Vabbrev_start_location /*
|
|
431 Buffer position for `expand-abbrev' to use as the start of the abbrev.
|
|
432 nil means use the word before point as the abbrev.
|
|
433 Calling `expand-abbrev' sets this to nil.
|
|
434 */ );
|
|
435 Vabbrev_start_location = Qnil;
|
|
436
|
|
437 DEFVAR_LISP ("abbrev-start-location-buffer", &Vabbrev_start_location_buffer /*
|
|
438 Buffer that `abbrev-start-location' has been set for.
|
|
439 Trying to expand an abbrev in any other buffer clears `abbrev-start-location'.
|
|
440 */ );
|
|
441 Vabbrev_start_location_buffer = Qnil;
|
|
442
|
|
443 DEFVAR_BOOL ("abbrev-all-caps", &abbrev_all_caps /*
|
167
|
444 *Non-nil means expand multi-word abbrevs all caps if abbrev was so.
|
0
|
445 */ );
|
|
446 abbrev_all_caps = 0;
|
|
447
|
|
448 DEFVAR_LISP ("pre-abbrev-expand-hook", &Vpre_abbrev_expand_hook /*
|
|
449 Function or functions to be called before abbrev expansion is done.
|
|
450 This is the first thing that `expand-abbrev' does, and so this may change
|
|
451 the current abbrev table before abbrev lookup happens.
|
|
452 */ );
|
|
453 Vpre_abbrev_expand_hook = Qnil;
|
|
454 }
|