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;
|
|
93 struct Lisp_Symbol *sym;
|
|
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;
|
|
182 if (wordstart != BUF_ZV (buf)
|
|
183 && BUF_FETCH_CHAR (buf, wordstart) == '-')
|
|
184 {
|
|
185 buffer_delete_range (buf, wordstart, wordstart + 1, 0);
|
|
186 }
|
|
187 wordend = BUF_PT (buf);
|
|
188 }
|
|
189 else
|
|
190 {
|
|
191 Bufpos point = BUF_PT (buf);
|
|
192
|
|
193 wordstart = scan_words (buf, point, -1);
|
|
194 if (!wordstart)
|
|
195 return 0;
|
|
196
|
|
197 wordend = scan_words (buf, wordstart, 1);
|
|
198 if (!wordend)
|
|
199 return 0;
|
|
200 if (wordend > BUF_ZV (buf))
|
|
201 wordend = BUF_ZV (buf);
|
|
202 if (wordend > point)
|
|
203 wordend = point;
|
|
204 /* Unlike the original function, we allow expansion only after
|
|
205 the abbrev, not preceded by a number of spaces. This is
|
|
206 because of consistency with abbrev_match. */
|
|
207 if (wordend < point)
|
|
208 return 0;
|
|
209 if (wordend <= wordstart)
|
|
210 return 0;
|
|
211 }
|
|
212
|
|
213 p = word = (Bufbyte *) alloca (MAX_EMCHAR_LEN * (wordend - wordstart));
|
|
214 for (idx = wordstart; idx < wordend; idx++)
|
|
215 {
|
|
216 Emchar c = BUF_FETCH_CHAR (buf, idx);
|
|
217 if (UPPERCASEP (buf, c))
|
|
218 c = DOWNCASE (buf, c);
|
|
219 p += set_charptr_emchar (p, c);
|
|
220 }
|
|
221 lookup = oblookup (obarray, word, p - word);
|
|
222 if (SYMBOLP (lookup) && !NILP (symbol_value (XSYMBOL (lookup))))
|
|
223 return XSYMBOL (lookup);
|
|
224 else
|
|
225 return NULL;
|
|
226 }
|
|
227
|
|
228 /* Return non-zero if OBARRAY contains an interned symbol ` '. */
|
|
229 static int
|
|
230 obarray_has_blank_p (Lisp_Object obarray)
|
|
231 {
|
|
232 Lisp_Object lookup;
|
|
233
|
|
234 lookup = oblookup (obarray, (Bufbyte *)" ", 1);
|
|
235 return SYMBOLP (lookup);
|
|
236 }
|
|
237
|
|
238 /* Analyze case in the buffer substring, and report it. */
|
|
239 static void
|
|
240 abbrev_count_case (struct buffer *buf, Bufpos pos, Charcount length,
|
|
241 int *lccount, int *uccount)
|
|
242 {
|
|
243 Emchar c;
|
|
244
|
|
245 *lccount = *uccount = 0;
|
|
246 while (length--)
|
|
247 {
|
|
248 c = BUF_FETCH_CHAR (buf, pos);
|
|
249 if (UPPERCASEP (buf, c))
|
|
250 ++*uccount;
|
|
251 else if (LOWERCASEP (buf, c))
|
|
252 ++*lccount;
|
|
253 ++pos;
|
|
254 }
|
|
255 }
|
0
|
256
|
20
|
257 DEFUN ("expand-abbrev", Fexpand_abbrev, 0, 0, "", /*
|
167
|
258 Expand the abbrev before point, if any.
|
0
|
259 Effective when explicitly called even when `abbrev-mode' is nil.
|
|
260 Returns t if expansion took place.
|
20
|
261 */
|
|
262 ())
|
0
|
263 {
|
|
264 /* This function can GC */
|
|
265 struct buffer *buf = current_buffer;
|
|
266 int oldmodiff = BUF_MODIFF (buf);
|
167
|
267 Lisp_Object pre_modiff_p;
|
|
268 Bufpos point; /* position of point */
|
|
269 Bufpos abbrev_start; /* position of abbreviation beginning */
|
0
|
270
|
167
|
271 struct Lisp_Symbol *(*fun) (struct buffer *, Lisp_Object);
|
|
272
|
|
273 struct Lisp_Symbol *abbrev_symbol;
|
|
274 struct Lisp_String *abbrev_string;
|
|
275 Lisp_Object expansion, count, hook;
|
|
276 Charcount abbrev_length, idx;
|
|
277 int lccount, uccount;
|
|
278
|
0
|
279 run_hook (Qpre_abbrev_expand_hook);
|
|
280 /* If the hook changes the buffer, treat that as having "done an
|
|
281 expansion". */
|
167
|
282 pre_modiff_p = (BUF_MODIFF (buf) != oldmodiff ? Qt : Qnil);
|
0
|
283
|
167
|
284 abbrev_symbol = NULL;
|
0
|
285 if (!BUFFERP (Vabbrev_start_location_buffer) ||
|
|
286 XBUFFER (Vabbrev_start_location_buffer) != buf)
|
|
287 Vabbrev_start_location = Qnil;
|
167
|
288 /* We use the more general `abbrev_match' if the obarray blank flag
|
|
289 is not set, and Vabbrev_start_location is nil. Otherwise, use
|
|
290 `abbrev_oblookup'. */
|
|
291 #define MATCHFUN(tbl) ((obarray_has_blank_p (tbl) \
|
|
292 && NILP (Vabbrev_start_location)) \
|
|
293 ? abbrev_match : abbrev_oblookup)
|
|
294 if (!NILP (buf->abbrev_table))
|
0
|
295 {
|
167
|
296 fun = MATCHFUN (buf->abbrev_table);
|
|
297 abbrev_symbol = fun (buf, buf->abbrev_table);
|
0
|
298 }
|
167
|
299 if (!abbrev_symbol && !NILP (Vglobal_abbrev_table))
|
|
300 {
|
|
301 fun = MATCHFUN (Vglobal_abbrev_table);
|
|
302 abbrev_symbol = fun (buf, Vglobal_abbrev_table);
|
|
303 }
|
|
304 if (!abbrev_symbol)
|
|
305 return pre_modiff_p;
|
0
|
306
|
167
|
307 /* NOTE: we hope that `pre-abbrev-expand-hook' didn't do something
|
|
308 nasty, such as changed (or killed) the buffer. */
|
|
309 point = BUF_PT (buf);
|
0
|
310
|
167
|
311 /* OK, we're out of the must-be-fast part. An abbreviation matched.
|
|
312 Now find the parameters, insert the expansion, and make it all
|
|
313 look pretty. */
|
|
314 abbrev_string = symbol_name (abbrev_symbol);
|
|
315 abbrev_length = string_char_length (abbrev_string);
|
|
316 abbrev_start = point - abbrev_length;
|
0
|
317
|
167
|
318 expansion = symbol_value (abbrev_symbol);
|
|
319 CHECK_STRING (expansion);
|
|
320
|
|
321 count = symbol_plist (abbrev_symbol); /* Gag */
|
|
322 if (NILP (count))
|
|
323 count = make_int (0);
|
0
|
324 else
|
167
|
325 CHECK_NATNUM (count);
|
|
326 symbol_plist (abbrev_symbol) = make_int (1 + XINT (count));
|
|
327
|
|
328 /* Count the case in the original text. */
|
|
329 abbrev_count_case (buf, abbrev_start, abbrev_length, &lccount, &uccount);
|
0
|
330
|
167
|
331 /* Remember the last abbrev text, location, etc. */
|
|
332 XSETSYMBOL (Vlast_abbrev, abbrev_symbol);
|
0
|
333 Vlast_abbrev_text =
|
167
|
334 make_string_from_buffer (buf, abbrev_start, abbrev_length);
|
|
335 last_abbrev_point = abbrev_start;
|
0
|
336
|
167
|
337 /* Add an undo boundary, in case we are doing this for a
|
|
338 self-inserting command which has avoided making one so far. */
|
|
339 if (INTERACTIVE)
|
|
340 Fundo_boundary ();
|
0
|
341
|
167
|
342 /* Remove the abbrev */
|
|
343 buffer_delete_range (buf, abbrev_start, point, 0);
|
|
344 /* And insert the expansion. */
|
|
345 buffer_insert_lisp_string (buf, expansion);
|
|
346 point = BUF_PT (buf);
|
0
|
347
|
167
|
348 /* Now fiddle with the case. */
|
0
|
349 if (uccount && !lccount)
|
|
350 {
|
|
351 /* Abbrev was all caps */
|
167
|
352 if (!abbrev_all_caps
|
|
353 && scan_words (buf, point, -1) > scan_words (buf, abbrev_start, 1))
|
|
354 {
|
|
355 Fupcase_initials_region (make_int (abbrev_start), make_int (point),
|
|
356 make_buffer (buf));
|
|
357 }
|
|
358 else
|
|
359 {
|
|
360 /* If expansion is one word, or if user says so, upcase it all. */
|
|
361 Fupcase_region (make_int (abbrev_start), make_int (point),
|
|
362 make_buffer (buf));
|
|
363 }
|
0
|
364 }
|
|
365 else if (uccount)
|
|
366 {
|
|
367 /* Abbrev included some caps. Cap first initial of expansion */
|
167
|
368 Bufpos pos = abbrev_start;
|
0
|
369 /* Find the initial. */
|
167
|
370 while (pos < point
|
|
371 && !WORD_SYNTAX_P (XCHAR_TABLE (buf->mirror_syntax_table),
|
70
|
372 BUF_FETCH_CHAR (buf, pos)))
|
167
|
373 pos++;
|
0
|
374 /* Change just that. */
|
167
|
375 Fupcase_initials_region (make_int (pos), make_int (pos + 1),
|
|
376 make_buffer (buf));
|
0
|
377 }
|
|
378
|
167
|
379 hook = symbol_function (abbrev_symbol);
|
0
|
380 if (!NILP (hook) && !UNBOUNDP (hook))
|
|
381 call0 (hook);
|
|
382
|
|
383 return Qt;
|
|
384 }
|
|
385
|
167
|
386
|
0
|
387 void
|
|
388 syms_of_abbrev (void)
|
|
389 {
|
|
390 defsymbol (&Qpre_abbrev_expand_hook, "pre-abbrev-expand-hook");
|
20
|
391 DEFSUBR (Fexpand_abbrev);
|
0
|
392 }
|
|
393
|
|
394 void
|
|
395 vars_of_abbrev (void)
|
|
396 {
|
|
397 DEFVAR_LISP ("global-abbrev-table", &Vglobal_abbrev_table /*
|
|
398 The abbrev table whose abbrevs affect all buffers.
|
|
399 Each buffer may also have a local abbrev table.
|
|
400 If it does, the local table overrides the global one
|
|
401 for any particular abbrev defined in both.
|
|
402 */ );
|
|
403 Vglobal_abbrev_table = Qnil; /* setup by Lisp code */
|
|
404
|
|
405 DEFVAR_LISP ("last-abbrev", &Vlast_abbrev /*
|
|
406 The abbrev-symbol of the last abbrev expanded.
|
|
407 See the function `abbrev-symbol'.
|
|
408 */ );
|
|
409
|
|
410 DEFVAR_LISP ("last-abbrev-text", &Vlast_abbrev_text /*
|
|
411 The exact text of the last abbrev expanded.
|
|
412 nil if the abbrev has already been unexpanded.
|
|
413 */ );
|
|
414
|
|
415 DEFVAR_INT ("last-abbrev-location", &last_abbrev_point /*
|
|
416 The location of the start of the last abbrev expanded.
|
|
417 */ );
|
|
418
|
|
419 Vlast_abbrev = Qnil;
|
|
420 Vlast_abbrev_text = Qnil;
|
|
421 last_abbrev_point = 0;
|
|
422
|
|
423 DEFVAR_LISP ("abbrev-start-location", &Vabbrev_start_location /*
|
|
424 Buffer position for `expand-abbrev' to use as the start of the abbrev.
|
|
425 nil means use the word before point as the abbrev.
|
|
426 Calling `expand-abbrev' sets this to nil.
|
|
427 */ );
|
|
428 Vabbrev_start_location = Qnil;
|
|
429
|
|
430 DEFVAR_LISP ("abbrev-start-location-buffer", &Vabbrev_start_location_buffer /*
|
|
431 Buffer that `abbrev-start-location' has been set for.
|
|
432 Trying to expand an abbrev in any other buffer clears `abbrev-start-location'.
|
|
433 */ );
|
|
434 Vabbrev_start_location_buffer = Qnil;
|
|
435
|
|
436 DEFVAR_BOOL ("abbrev-all-caps", &abbrev_all_caps /*
|
167
|
437 *Non-nil means expand multi-word abbrevs all caps if abbrev was so.
|
0
|
438 */ );
|
|
439 abbrev_all_caps = 0;
|
|
440
|
|
441 DEFVAR_LISP ("pre-abbrev-expand-hook", &Vpre_abbrev_expand_hook /*
|
|
442 Function or functions to be called before abbrev expansion is done.
|
|
443 This is the first thing that `expand-abbrev' does, and so this may change
|
|
444 the current abbrev table before abbrev lookup happens.
|
|
445 */ );
|
|
446 Vpre_abbrev_expand_hook = Qnil;
|
|
447 }
|