comparison lisp/abbrev.el @ 209:41ff10fd062f r20-4b3

Import from CVS: tag r20-4b3
author cvs
date Mon, 13 Aug 2007 10:04:58 +0200
parents
children 0e522484dd2a
comparison
equal deleted inserted replaced
208:f427b8ec4379 209:41ff10fd062f
1 ;;; abbrev.el --- abbrev mode commands for Emacs
2
3 ;; Copyright (C) 1985, 1986, 1987, 1992, 1997 Free Software Foundation, Inc.
4
5 ;; Maintainer: XEmacs Development Team
6 ;; Keywords: abbrev, dumped
7
8 ;; This file is part of XEmacs.
9
10 ;; XEmacs is free software; you can redistribute it and/or modify it
11 ;; under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; XEmacs is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ;; General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with XEmacs; see the file COPYING. If not, write to the Free
22 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23 ;; 02111-1307, USA.
24
25 ;;; Synched up with: FSF 19.34 (With some additions)
26
27 ;;; Commentary:
28
29 ;; This file is dumped with XEmacs.
30
31 ;; This facility is documented in the Emacs Manual.
32
33 ;;; Code:
34
35 ;jwz: this is preloaded so don't ;;;###autoload
36 (defcustom only-global-abbrevs nil "\
37 *Non-nil means user plans to use global abbrevs only.
38 Makes the commands to define mode-specific abbrevs define global ones instead."
39 :type 'boolean
40 :group 'abbrev)
41
42 ;;; XEmacs: the following block of code is not in FSF
43 (defvar abbrev-table-name-list '()
44 "List of symbols whose values are abbrev tables.")
45
46 (defvar abbrevs-changed nil
47 "Set non-nil by defining or altering any word abbrevs.
48 This causes `save-some-buffers' to offer to save the abbrevs.")
49
50 (defun make-abbrev-table ()
51 "Create a new, empty abbrev table object."
52 (make-vector 59 0)) ; 59 is prime
53
54 (defun clear-abbrev-table (table)
55 "Undefine all abbrevs in abbrev table TABLE, leaving it empty."
56 (fillarray table 0)
57 (setq abbrevs-changed t)
58 nil)
59
60
61 (defun define-abbrev-table (name defs)
62 "Define TABNAME (a symbol) as an abbrev table name.
63 Define abbrevs in it according to DEFINITIONS, which is a list of elements
64 of the form (ABBREVNAME EXPANSION HOOK USECOUNT)."
65 (let ((table (and (boundp name) (symbol-value name))))
66 (cond ((vectorp table))
67 ((not table)
68 (setq table (make-abbrev-table))
69 (set name table)
70 (setq abbrev-table-name-list (cons name abbrev-table-name-list)))
71 (t
72 (setq table (signal 'wrong-type-argument (list 'vectorp table)))
73 (set name table)))
74 (while defs
75 (apply (function define-abbrev) table (car defs))
76 (setq defs (cdr defs)))))
77
78 (defun define-abbrev (table name &optional expansion hook count)
79 "Define an abbrev in TABLE named NAME, to expand to EXPANSION or call HOOK.
80 NAME and EXPANSION are strings. Hook is a function or `nil'.
81 To undefine an abbrev, define it with an expansion of `nil'."
82 (or (not expansion)
83 (stringp expansion)
84 (setq expansion (signal 'wrong-type-argument
85 (list 'stringp expansion))))
86 (or (not count)
87 (integerp count)
88 (setq count (signal 'wrong-type-argument
89 (list 'fixnump count))))
90 (or (vectorp table)
91 (setq table (signal 'wrong-type-argument
92 (list 'vectorp table))))
93 (let* ((sym (intern name table))
94 (oexp (and (boundp sym) (symbol-value sym)))
95 (ohook (and (fboundp sym) (symbol-function sym))))
96 (unless (and (equal ohook hook)
97 (stringp oexp)
98 (stringp expansion)
99 (string-equal oexp expansion))
100 (setq abbrevs-changed t)
101 ;; If there is a non-word character in the string, set the flag.
102 (if (string-match "\\W" name)
103 (set (intern " " table) nil)))
104 (set sym expansion)
105 (fset sym hook)
106 (setplist sym (or count 0))
107 name))
108
109
110 ;; Fixup stuff from bootstrap def of define-abbrev-table in subr.el
111 (let ((l abbrev-table-name-list))
112 (while l
113 (let ((fixup (car l)))
114 (if (consp fixup)
115 (progn
116 (setq abbrev-table-name-list (delq fixup abbrev-table-name-list))
117 (define-abbrev-table (car fixup) (cdr fixup))))
118 (setq l (cdr l))))
119 ;; These are no longer initialised by C code
120 (if (not global-abbrev-table)
121 (progn
122 (setq global-abbrev-table (make-abbrev-table))
123 (setq abbrev-table-name-list (cons 'global-abbrev-table
124 abbrev-table-name-list))))
125 (if (not fundamental-mode-abbrev-table)
126 (progn
127 (setq fundamental-mode-abbrev-table (make-abbrev-table))
128 (setq abbrev-table-name-list (cons 'fundamental-mode-abbrev-table
129 abbrev-table-name-list))))
130 (and (eq major-mode 'fundamental-mode)
131 (not local-abbrev-table)
132 (setq local-abbrev-table fundamental-mode-abbrev-table)))
133
134
135 (defun define-global-abbrev (name expansion)
136 "Define ABBREV as a global abbreviation for EXPANSION."
137 (interactive "sDefine global abbrev: \nsExpansion for %s: ")
138 (define-abbrev global-abbrev-table
139 (downcase name) expansion nil 0))
140
141 (defun define-mode-abbrev (name expansion)
142 "Define ABBREV as a mode-specific abbreviation for EXPANSION."
143 (interactive "sDefine mode abbrev: \nsExpansion for %s: ")
144 (define-abbrev (or local-abbrev-table
145 (error "Major mode has no abbrev table"))
146 (downcase name) expansion nil 0))
147
148 (defun abbrev-symbol (abbrev &optional table)
149 "Return the symbol representing abbrev named ABBREV.
150 This symbol's name is ABBREV, but it is not the canonical symbol of that name;
151 it is interned in an abbrev-table rather than the normal obarray.
152 The value is nil if that abbrev is not defined.
153 Optional second arg TABLE is abbrev table to look it up in.
154 The default is to try buffer's mode-specific abbrev table, then global table."
155 (let ((frob (function (lambda (table)
156 (let ((sym (intern-soft abbrev table)))
157 (if (and (boundp sym)
158 (stringp (symbol-value sym)))
159 sym
160 nil))))))
161 (if table
162 (funcall frob table)
163 (or (and local-abbrev-table
164 (funcall frob local-abbrev-table))
165 (funcall frob global-abbrev-table)))))
166
167 (defun abbrev-expansion (abbrev &optional table)
168 "Return the string that ABBREV expands into in the current buffer.
169 Optionally specify an abbrev table as second arg;
170 then ABBREV is looked up in that table only."
171 (let ((sym (abbrev-symbol abbrev table)))
172 (if sym
173 (symbol-value sym)
174 nil)))
175
176 (defun unexpand-abbrev ()
177 "Undo the expansion of the last abbrev that expanded.
178 This differs from ordinary undo in that other editing done since then
179 is not undone."
180 (interactive)
181 (if (or (< last-abbrev-location (point-min))
182 (> last-abbrev-location (point-max))
183 (not (stringp last-abbrev-text)))
184 nil
185 (let* ((opoint (point))
186 (val (symbol-value last-abbrev))
187 (adjust (length val)))
188 ;; This isn't correct if (symbol-function last-abbrev-text)
189 ;; was used to do the expansion
190 (goto-char last-abbrev-location)
191 (delete-region last-abbrev-location (+ last-abbrev-location adjust))
192 (insert last-abbrev-text)
193 (setq adjust (- adjust (length last-abbrev-text)))
194 (setq last-abbrev-text nil)
195 (if (< last-abbrev-location opoint)
196 (goto-char (- opoint adjust))
197 (goto-char opoint)))))
198
199
200
201 (defun insert-abbrev-table-description (name human-readable)
202 "Insert before point a full description of abbrev table named NAME.
203 NAME is a symbol whose value is an abbrev table.
204 If optional 2nd arg HUMAN is non-nil, insert a human-readable description.
205 Otherwise the description is an expression,
206 a call to `define-abbrev-table', which would
207 define the abbrev table NAME exactly as it is currently defined."
208 (let ((table (symbol-value name))
209 (stream (current-buffer)))
210 (message "Abbrev-table %s..." name)
211 (if human-readable
212 (progn
213 (prin1 (list name) stream)
214 ;; Need two terpri's or cretinous edit-abbrevs blows out
215 (terpri stream)
216 (terpri stream)
217 (mapatoms (function (lambda (sym)
218 (if (symbol-value sym)
219 (let* ((n (prin1-to-string (symbol-name sym)))
220 (pos (length n)))
221 (princ n stream)
222 (while (< pos 14)
223 (write-char ?\ stream)
224 (setq pos (1+ pos)))
225 (princ (format " %-5S " (symbol-plist sym))
226 stream)
227 (if (not (symbol-function sym))
228 (prin1 (symbol-value sym) stream)
229 (progn
230 (setq n (prin1-to-string (symbol-value sym))
231 pos (+ pos 6 (length n)))
232 (princ n stream)
233 (while (< pos 45)
234 (write-char ?\ stream)
235 (setq pos (1+ pos)))
236 (prin1 (symbol-function sym) stream)))
237 (terpri stream)))))
238 table)
239 (terpri stream))
240 (progn
241 (princ "\(define-abbrev-table '" stream)
242 (prin1 name stream)
243 (princ " '\(\n" stream)
244 (mapatoms (function (lambda (sym)
245 (if (symbol-value sym)
246 (progn
247 (princ " " stream)
248 (prin1 (list (symbol-name sym)
249 (symbol-value sym)
250 (symbol-function sym)
251 (symbol-plist sym))
252 stream)
253 (terpri stream)))))
254 table)
255 (princ " \)\)\n" stream)))
256 (terpri stream))
257 (message ""))
258 ;;; End code not in FSF
259
260 (defun abbrev-mode (arg)
261 "Toggle abbrev mode.
262 With argument ARG, turn abbrev mode on iff ARG is positive.
263 In abbrev mode, inserting an abbreviation causes it to expand
264 and be replaced by its expansion."
265 (interactive "P")
266 (setq abbrev-mode
267 (if (null arg) (not abbrev-mode)
268 (> (prefix-numeric-value arg) 0)))
269 ;; XEmacs change
270 (redraw-modeline))
271
272
273 (defvar edit-abbrevs-map nil
274 "Keymap used in edit-abbrevs.")
275 (if edit-abbrevs-map
276 nil
277 (setq edit-abbrevs-map (make-sparse-keymap))
278 ;; XEmacs change
279 (set-keymap-name edit-abbrevs-map 'edit-abbrevs-map)
280 (define-key edit-abbrevs-map "\C-x\C-s" 'edit-abbrevs-redefine)
281 (define-key edit-abbrevs-map "\C-c\C-c" 'edit-abbrevs-redefine))
282
283 (defun kill-all-abbrevs ()
284 "Undefine all defined abbrevs."
285 (interactive)
286 (let ((tables abbrev-table-name-list))
287 (while tables
288 (clear-abbrev-table (symbol-value (car tables)))
289 (setq tables (cdr tables)))))
290
291 (defun insert-abbrevs ()
292 "Insert after point a description of all defined abbrevs.
293 Mark is set after the inserted text."
294 (interactive)
295 (push-mark
296 (save-excursion
297 (let ((tables abbrev-table-name-list))
298 (while tables
299 (insert-abbrev-table-description (car tables) t)
300 (setq tables (cdr tables))))
301 (point))))
302
303 (defun list-abbrevs ()
304 "Display a list of all defined abbrevs."
305 (interactive)
306 (display-buffer (prepare-abbrev-list-buffer)))
307
308 (defun prepare-abbrev-list-buffer ()
309 (save-excursion
310 (set-buffer (get-buffer-create "*Abbrevs*"))
311 (erase-buffer)
312 (let ((tables abbrev-table-name-list))
313 (while tables
314 (insert-abbrev-table-description (car tables) t)
315 (setq tables (cdr tables))))
316 (goto-char (point-min))
317 (set-buffer-modified-p nil)
318 (edit-abbrevs-mode))
319 (get-buffer-create "*Abbrevs*"))
320
321 (defun edit-abbrevs-mode ()
322 "Major mode for editing the list of abbrev definitions.
323 \\{edit-abbrevs-map}"
324 (interactive)
325 (setq major-mode 'edit-abbrevs-mode)
326 (setq mode-name "Edit-Abbrevs")
327 (use-local-map edit-abbrevs-map))
328
329 (defun edit-abbrevs ()
330 "Alter abbrev definitions by editing a list of them.
331 Selects a buffer containing a list of abbrev definitions.
332 You can edit them and type \\<edit-abbrevs-map>\\[edit-abbrevs-redefine] to redefine abbrevs
333 according to your editing.
334 Buffer contains a header line for each abbrev table,
335 which is the abbrev table name in parentheses.
336 This is followed by one line per abbrev in that table:
337 NAME USECOUNT EXPANSION HOOK
338 where NAME and EXPANSION are strings with quotes,
339 USECOUNT is an integer, and HOOK is any valid function
340 or may be omitted (it is usually omitted)."
341 (interactive)
342 (switch-to-buffer (prepare-abbrev-list-buffer)))
343
344 (defun edit-abbrevs-redefine ()
345 "Redefine abbrevs according to current buffer contents."
346 (interactive)
347 (define-abbrevs t)
348 (set-buffer-modified-p nil))
349
350 (defun define-abbrevs (&optional arg)
351 "Define abbrevs according to current visible buffer contents.
352 See documentation of `edit-abbrevs' for info on the format of the
353 text you must have in the buffer.
354 With argument, eliminate all abbrev definitions except
355 the ones defined from the buffer now."
356 (interactive "P")
357 (if arg (kill-all-abbrevs))
358 (save-excursion
359 (goto-char (point-min))
360 (while (and (not (eobp)) (re-search-forward "^(" nil t))
361 (let* ((buf (current-buffer))
362 (table (read buf))
363 abbrevs name hook exp count)
364 (forward-line 1)
365 (while (progn (forward-line 1)
366 (not (eolp)))
367 (setq name (read buf) count (read buf) exp (read buf))
368 (skip-chars-backward " \t\n\f")
369 (setq hook (if (not (eolp)) (read buf)))
370 (skip-chars-backward " \t\n\f")
371 (setq abbrevs (cons (list name exp hook count) abbrevs)))
372 (define-abbrev-table table abbrevs)))))
373
374 (defun read-abbrev-file (&optional file quietly)
375 "Read abbrev definitions from file written with `write-abbrev-file'.
376 Optional argument FILE is the name of the file to read;
377 it defaults to the value of `abbrev-file-name'.
378 Optional second argument QUIETLY non-nil means don't print anything."
379 (interactive "fRead abbrev file: ")
380 (load (if (and file (> (length file) 0)) file abbrev-file-name)
381 nil quietly)
382 (setq save-abbrevs t abbrevs-changed nil))
383
384 (defun quietly-read-abbrev-file (&optional file)
385 "Read abbrev definitions from file written with write-abbrev-file.
386 Optional argument FILE is the name of the file to read;
387 it defaults to the value of `abbrev-file-name'.
388 Does not print anything."
389 ;(interactive "fRead abbrev file: ")
390 (read-abbrev-file file t))
391
392 (defun write-abbrev-file (file)
393 "Write all abbrev definitions to a file of Lisp code.
394 The file written can be loaded in another session to define the same abbrevs.
395 The argument FILE is the file name to write."
396 (interactive
397 (list
398 (read-file-name "Write abbrev file: "
399 (file-name-directory (expand-file-name abbrev-file-name))
400 abbrev-file-name)))
401 (or (and file (> (length file) 0))
402 (setq file abbrev-file-name))
403 (save-excursion
404 (set-buffer (get-buffer-create " write-abbrev-file"))
405 (erase-buffer)
406 (let ((tables abbrev-table-name-list))
407 (while tables
408 (insert-abbrev-table-description (car tables) nil)
409 (setq tables (cdr tables))))
410 (write-region 1 (point-max) file)
411 (erase-buffer)))
412
413 (defun add-mode-abbrev (arg)
414 "Define mode-specific abbrev for last word(s) before point.
415 Argument is how many words before point form the expansion;
416 or zero means the region is the expansion.
417 A negative argument means to undefine the specified abbrev.
418 Reads the abbreviation in the minibuffer.
419
420 Don't use this function in a Lisp program; use `define-abbrev' instead."
421 ;; XEmacs change:
422 (interactive "P")
423 (add-abbrev
424 (if only-global-abbrevs
425 global-abbrev-table
426 (or local-abbrev-table
427 (error "No per-mode abbrev table")))
428 "Mode" arg))
429
430 (defun add-global-abbrev (arg)
431 "Define global (all modes) abbrev for last word(s) before point.
432 The prefix argument specifies the number of words before point that form the
433 expansion; or zero means the region is the expansion.
434 A negative argument means to undefine the specified abbrev.
435 This command uses the minibuffer to read the abbreviation.
436
437 Don't use this function in a Lisp program; use `define-abbrev' instead."
438 ;; XEmacs change:
439 (interactive "P")
440 (add-abbrev global-abbrev-table "Global" arg))
441
442 (defun add-abbrev (table type arg)
443 ;; XEmacs change:
444 (if (and (not arg) (region-active-p)) (setq arg 0)
445 (setq arg (prefix-numeric-value arg)))
446 (let ((exp (and (>= arg 0)
447 (buffer-substring
448 (point)
449 (if (= arg 0) (mark)
450 (save-excursion (forward-word (- arg)) (point))))))
451 name)
452 (setq name
453 (read-string (format (if exp "%s abbrev for \"%s\": "
454 "Undefine %s abbrev: ")
455 type exp)))
456 (set-text-properties 0 (length name) nil name)
457 (if (or (null exp)
458 (not (abbrev-expansion name table))
459 (y-or-n-p (format "%s expands to \"%s\"; redefine? "
460 name (abbrev-expansion name table))))
461 (define-abbrev table (downcase name) exp))))
462
463 (defun inverse-add-mode-abbrev (arg)
464 "Define last word before point as a mode-specific abbrev.
465 With prefix argument N, defines the Nth word before point.
466 This command uses the minibuffer to read the expansion.
467 Expands the abbreviation after defining it."
468 (interactive "p")
469 (inverse-add-abbrev
470 (if only-global-abbrevs
471 global-abbrev-table
472 (or local-abbrev-table
473 (error "No per-mode abbrev table")))
474 "Mode" arg))
475
476 (defun inverse-add-global-abbrev (arg)
477 "Define last word before point as a global (mode-independent) abbrev.
478 With prefix argument N, defines the Nth word before point.
479 This command uses the minibuffer to read the expansion.
480 Expands the abbreviation after defining it."
481 (interactive "p")
482 (inverse-add-abbrev global-abbrev-table "Global" arg))
483
484 (defun inverse-add-abbrev (table type arg)
485 (let (name nameloc exp)
486 (save-excursion
487 (forward-word (- arg))
488 (setq name (buffer-substring (point) (progn (forward-word 1)
489 (setq nameloc (point))))))
490 (set-text-properties 0 (length name) nil name)
491 (setq exp (read-string (format "%s expansion for \"%s\": "
492 type name)))
493 (if (or (not (abbrev-expansion name table))
494 (y-or-n-p (format "%s expands to \"%s\"; redefine? "
495 name (abbrev-expansion name table))))
496 (progn
497 (define-abbrev table (downcase name) exp)
498 (save-excursion
499 (goto-char nameloc)
500 (expand-abbrev))))))
501
502 (defun abbrev-prefix-mark (&optional arg)
503 "Mark current point as the beginning of an abbrev.
504 Abbrev to be expanded starts here rather than at beginning of word.
505 This way, you can expand an abbrev with a prefix: insert the prefix,
506 use this command, then insert the abbrev."
507 (interactive "P")
508 (or arg (expand-abbrev))
509 (setq abbrev-start-location (point-marker)
510 abbrev-start-location-buffer (current-buffer))
511 (let ((e (make-extent (point) (point))))
512 (set-extent-begin-glyph e (make-glyph [string :data "-"]))))
513
514 (defun expand-region-abbrevs (start end &optional noquery)
515 "For abbrev occurrence in the region, offer to expand it.
516 The user is asked to type y or n for each occurrence.
517 A prefix argument means don't query; expand all abbrevs.
518 If called from a Lisp program, arguments are START END &optional NOQUERY."
519 (interactive "r\nP")
520 (save-excursion
521 (goto-char start)
522 (let ((lim (- (point-max) end))
523 pnt string)
524 (while (and (not (eobp))
525 (progn (forward-word 1)
526 (<= (setq pnt (point)) (- (point-max) lim))))
527 (if (abbrev-expansion
528 (setq string
529 (buffer-substring
530 (save-excursion (forward-word -1) (point))
531 pnt)))
532 (if (or noquery (y-or-n-p (format "Expand `%s'? " string)))
533 (expand-abbrev)))))))
534
535 ;;; abbrev.el ends here