1 ;;; abbrev.el --- abbrev mode commands for Emacs
3 ;; Copyright (C) 1985, 1986, 1987, 1992, 1997 Free Software Foundation, Inc.
5 ;; Maintainer: XEmacs Development Team
6 ;; Keywords: abbrev, dumped
8 ;; This file is part of XEmacs.
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)
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.
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
25 ;;; Synched up with: FSF 19.34 (With some additions)
29 ;; This file is dumped with XEmacs.
31 ;; This facility is documented in the Emacs Manual.
36 "Abbreviation handling, typing shortcuts, macros."
40 (defgroup abbrev-mode nil
41 "Word abbreviations mode."
44 ;jwz: this is preloaded so don't ;;;###autoload
45 (defcustom only-global-abbrevs nil "\
46 *Non-nil means user plans to use global abbrevs only.
47 Makes the commands to define mode-specific abbrevs define global ones instead."
51 ;;; XEmacs: the following block of code is not in FSF
52 (defvar abbrev-table-name-list '()
53 "List of symbols whose values are abbrev tables.")
55 (defvar abbrevs-changed nil
56 "Set non-nil by defining or altering any word abbrevs.
57 This causes `save-some-buffers' to offer to save the abbrevs.")
59 (defun make-abbrev-table ()
60 "Return a new, empty abbrev table object."
61 (make-vector 59 0)) ; 59 is prime
63 (defun clear-abbrev-table (table)
64 "Undefine all abbrevs in abbrev table TABLE, leaving it empty."
66 (setq abbrevs-changed t)
70 (defun define-abbrev-table (name defs)
71 "Define TABNAME (a symbol) as an abbrev table name.
72 Define abbrevs in it according to DEFINITIONS, which is a list of elements
73 of the form (ABBREVNAME EXPANSION HOOK USECOUNT)."
74 (let ((table (and (boundp name) (symbol-value name))))
75 (cond ((vectorp table))
77 (setq table (make-abbrev-table))
79 (setq abbrev-table-name-list (cons name abbrev-table-name-list)))
81 (setq table (signal 'wrong-type-argument (list 'vectorp table)))
84 (apply (function define-abbrev) table (car defs))
85 (setq defs (cdr defs)))))
87 (defun define-abbrev (table name &optional expansion hook count)
88 "Define an abbrev in TABLE named NAME, to expand to EXPANSION or call HOOK.
89 NAME and EXPANSION are strings. Hook is a function or `nil'.
90 To undefine an abbrev, define it with an expansion of `nil'."
93 (setq expansion (signal 'wrong-type-argument
94 (list 'stringp expansion))))
97 (setq count (signal 'wrong-type-argument
98 (list 'fixnump count))))
100 (setq table (signal 'wrong-type-argument
101 (list 'vectorp table))))
102 (let* ((sym (intern name table))
103 (oexp (and (boundp sym) (symbol-value sym)))
104 (ohook (and (fboundp sym) (symbol-function sym))))
105 (unless (and (equal ohook hook)
108 (string-equal oexp expansion))
109 (setq abbrevs-changed t)
110 ;; If there is a non-word character in the string, set the flag.
111 (if (string-match "\\W" name)
112 (set (intern " " table) nil)))
115 (setplist sym (or count 0))
119 ;; Fixup stuff from bootstrap def of define-abbrev-table in subr.el
120 (let ((l abbrev-table-name-list))
122 (let ((fixup (car l)))
125 (setq abbrev-table-name-list (delq fixup abbrev-table-name-list))
126 (define-abbrev-table (car fixup) (cdr fixup))))
128 ;; These are no longer initialized by C code
129 (if (not global-abbrev-table)
131 (setq global-abbrev-table (make-abbrev-table))
132 (setq abbrev-table-name-list (cons 'global-abbrev-table
133 abbrev-table-name-list))))
134 (if (not fundamental-mode-abbrev-table)
136 (setq fundamental-mode-abbrev-table (make-abbrev-table))
137 (setq abbrev-table-name-list (cons 'fundamental-mode-abbrev-table
138 abbrev-table-name-list))))
139 (and (eq major-mode 'fundamental-mode)
140 (not local-abbrev-table)
141 (setq local-abbrev-table fundamental-mode-abbrev-table)))
144 (defun define-global-abbrev (name expansion)
145 "Define ABBREV as a global abbreviation for EXPANSION."
146 (interactive "sDefine global abbrev: \nsExpansion for %s: ")
147 (define-abbrev global-abbrev-table
148 (downcase name) expansion nil 0))
150 (defun define-mode-abbrev (name expansion)
151 "Define ABBREV as a mode-specific abbreviation for EXPANSION."
152 (interactive "sDefine mode abbrev: \nsExpansion for %s: ")
153 (define-abbrev (or local-abbrev-table
154 (error "Major mode has no abbrev table"))
155 (downcase name) expansion nil 0))
157 (defun abbrev-symbol (abbrev &optional table)
158 "Return the symbol representing abbrev named ABBREV.
159 This symbol's name is ABBREV, but it is not the canonical symbol of that name;
160 it is interned in an abbrev-table rather than the normal obarray.
161 The value is nil if that abbrev is not defined.
162 Optional second arg TABLE is abbrev table to look it up in.
163 The default is to try buffer's mode-specific abbrev table, then global table."
164 (let ((frob (function (lambda (table)
165 (let ((sym (intern-soft abbrev table)))
166 (if (and (boundp sym)
167 (stringp (symbol-value sym)))
172 (or (and local-abbrev-table
173 (funcall frob local-abbrev-table))
174 (funcall frob global-abbrev-table)))))
176 (defun abbrev-expansion (abbrev &optional table)
177 "Return the string that ABBREV expands into in the current buffer.
178 Optionally specify an abbrev table as second arg;
179 then ABBREV is looked up in that table only."
180 (let ((sym (abbrev-symbol abbrev table)))
185 (defun unexpand-abbrev ()
186 "Undo the expansion of the last abbrev that expanded.
187 This differs from ordinary undo in that other editing done since then
190 (if (or (< last-abbrev-location (point-min))
191 (> last-abbrev-location (point-max))
192 (not (stringp last-abbrev-text)))
194 (let* ((opoint (point))
195 (val (symbol-value last-abbrev))
196 (adjust (length val)))
197 ;; This isn't correct if (symbol-function last-abbrev-text)
198 ;; was used to do the expansion
199 (goto-char last-abbrev-location)
200 (delete-region last-abbrev-location (+ last-abbrev-location adjust))
201 (insert last-abbrev-text)
202 (setq adjust (- adjust (length last-abbrev-text)))
203 (setq last-abbrev-text nil)
204 (if (< last-abbrev-location opoint)
205 (goto-char (- opoint adjust))
206 (goto-char opoint)))))
210 (defun insert-abbrev-table-description (name human-readable)
211 "Insert before point a full description of abbrev table named NAME.
212 NAME is a symbol whose value is an abbrev table.
213 If optional 2nd arg HUMAN is non-nil, insert a human-readable description.
214 Otherwise the description is an expression,
215 a call to `define-abbrev-table', which would
216 define the abbrev table NAME exactly as it is currently defined."
217 (let ((table (symbol-value name))
218 (stream (current-buffer)))
219 (message "Abbrev-table %s..." name)
222 (prin1 (list name) stream)
223 ;; Need two terpri's or cretinous edit-abbrevs blows out
226 (mapatoms (function (lambda (sym)
227 (if (symbol-value sym)
228 (let* ((n (prin1-to-string (symbol-name sym)))
232 (write-char ?\ stream)
234 (princ (format " %-5S " (symbol-plist sym))
236 (if (not (symbol-function sym))
237 (prin1 (symbol-value sym) stream)
239 (setq n (prin1-to-string (symbol-value sym))
240 pos (+ pos 6 (length n)))
243 (write-char ?\ stream)
245 (prin1 (symbol-function sym) stream)))
250 (princ "\(define-abbrev-table '" stream)
252 (princ " '\(\n" stream)
253 (mapatoms (function (lambda (sym)
254 (if (symbol-value sym)
257 (prin1 (list (symbol-name sym)
259 (symbol-function sym)
264 (princ " \)\)\n" stream)))
267 ;;; End code not in FSF
269 (defun abbrev-mode (arg)
271 With argument ARG, turn abbrev mode on iff ARG is positive.
272 In abbrev mode, inserting an abbreviation causes it to expand
273 and be replaced by its expansion."
276 (if (null arg) (not abbrev-mode)
277 (> (prefix-numeric-value arg) 0)))
282 (defvar edit-abbrevs-map nil
283 "Keymap used in edit-abbrevs.")
286 (setq edit-abbrevs-map (make-sparse-keymap))
288 (set-keymap-name edit-abbrevs-map 'edit-abbrevs-map)
289 (define-key edit-abbrevs-map "\C-x\C-s" 'edit-abbrevs-redefine)
290 (define-key edit-abbrevs-map "\C-c\C-c" 'edit-abbrevs-redefine))
292 (defun kill-all-abbrevs ()
293 "Undefine all defined abbrevs."
295 (let ((tables abbrev-table-name-list))
297 (clear-abbrev-table (symbol-value (car tables)))
298 (setq tables (cdr tables)))))
300 (defun insert-abbrevs ()
301 "Insert after point a description of all defined abbrevs.
302 Mark is set after the inserted text."
306 (let ((tables abbrev-table-name-list))
308 (insert-abbrev-table-description (car tables) t)
309 (setq tables (cdr tables))))
312 (defun list-abbrevs ()
313 "Display a list of all defined abbrevs."
315 (display-buffer (prepare-abbrev-list-buffer)))
317 (defun prepare-abbrev-list-buffer ()
319 (set-buffer (get-buffer-create "*Abbrevs*"))
321 (let ((tables abbrev-table-name-list))
323 (insert-abbrev-table-description (car tables) t)
324 (setq tables (cdr tables))))
325 (goto-char (point-min))
326 (set-buffer-modified-p nil)
328 (get-buffer-create "*Abbrevs*"))
330 (defun edit-abbrevs-mode ()
331 "Major mode for editing the list of abbrev definitions.
332 \\{edit-abbrevs-map}"
334 (setq major-mode 'edit-abbrevs-mode)
335 (setq mode-name "Edit-Abbrevs")
336 (use-local-map edit-abbrevs-map))
338 (defun edit-abbrevs ()
339 "Alter abbrev definitions by editing a list of them.
340 Selects a buffer containing a list of abbrev definitions.
341 You can edit them and type \\<edit-abbrevs-map>\\[edit-abbrevs-redefine] to redefine abbrevs
342 according to your editing.
343 Buffer contains a header line for each abbrev table,
344 which is the abbrev table name in parentheses.
345 This is followed by one line per abbrev in that table:
346 NAME USECOUNT EXPANSION HOOK
347 where NAME and EXPANSION are strings with quotes,
348 USECOUNT is an integer, and HOOK is any valid function
349 or may be omitted (it is usually omitted)."
351 (switch-to-buffer (prepare-abbrev-list-buffer)))
353 (defun edit-abbrevs-redefine ()
354 "Redefine abbrevs according to current buffer contents."
357 (set-buffer-modified-p nil))
359 (defun define-abbrevs (&optional arg)
360 "Define abbrevs according to current visible buffer contents.
361 See documentation of `edit-abbrevs' for info on the format of the
362 text you must have in the buffer.
363 With argument, eliminate all abbrev definitions except
364 the ones defined from the buffer now."
366 (if arg (kill-all-abbrevs))
368 (goto-char (point-min))
369 (while (and (not (eobp)) (re-search-forward "^(" nil t))
370 (let* ((buf (current-buffer))
372 abbrevs name hook exp count)
374 (while (progn (forward-line 1)
376 (setq name (read buf) count (read buf) exp (read buf))
377 (skip-chars-backward " \t\n\f")
378 (setq hook (if (not (eolp)) (read buf)))
379 (skip-chars-backward " \t\n\f")
380 (setq abbrevs (cons (list name exp hook count) abbrevs)))
381 (define-abbrev-table table abbrevs)))))
383 (defun read-abbrev-file (&optional file quietly)
384 "Read abbrev definitions from file written with `write-abbrev-file'.
385 Optional argument FILE is the name of the file to read;
386 it defaults to the value of `abbrev-file-name'.
387 Optional second argument QUIETLY non-nil means don't print anything."
388 (interactive "fRead abbrev file: ")
389 (load (if (and file (> (length file) 0)) file abbrev-file-name)
391 (setq save-abbrevs t abbrevs-changed nil))
393 (defun quietly-read-abbrev-file (&optional file)
394 "Read abbrev definitions from file written with write-abbrev-file.
395 Optional argument FILE is the name of the file to read;
396 it defaults to the value of `abbrev-file-name'.
397 Does not print anything."
398 ;(interactive "fRead abbrev file: ")
399 (read-abbrev-file file t))
401 (defun write-abbrev-file (file)
402 "Write all abbrev definitions to a file of Lisp code.
403 The file written can be loaded in another session to define the same abbrevs.
404 The argument FILE is the file name to write."
407 (read-file-name "Write abbrev file: "
408 (file-name-directory (expand-file-name abbrev-file-name))
410 (or (and file (> (length file) 0))
411 (setq file abbrev-file-name))
413 (set-buffer (get-buffer-create " write-abbrev-file"))
415 (let ((tables abbrev-table-name-list))
417 (insert-abbrev-table-description (car tables) nil)
418 (setq tables (cdr tables))))
419 (write-region 1 (point-max) file)
422 (defun add-mode-abbrev (arg)
423 "Define mode-specific abbrev for last word(s) before point.
424 Argument is how many words before point form the expansion;
425 or zero means the region is the expansion.
426 A negative argument means to undefine the specified abbrev.
427 Reads the abbreviation in the minibuffer.
429 Don't use this function in a Lisp program; use `define-abbrev' instead."
433 (if only-global-abbrevs
435 (or local-abbrev-table
436 (error "No per-mode abbrev table")))
439 (defun add-global-abbrev (arg)
440 "Define global (all modes) abbrev for last word(s) before point.
441 The prefix argument specifies the number of words before point that form the
442 expansion; or zero means the region is the expansion.
443 A negative argument means to undefine the specified abbrev.
444 This command uses the minibuffer to read the abbreviation.
446 Don't use this function in a Lisp program; use `define-abbrev' instead."
449 (add-abbrev global-abbrev-table "Global" arg))
451 (defun add-abbrev (table type arg)
453 (if (and (not arg) (region-active-p)) (setq arg 0)
454 (setq arg (prefix-numeric-value arg)))
455 (let ((exp (and (>= arg 0)
459 (save-excursion (forward-word (- arg)) (point))))))
462 (read-string (format (if exp "%s abbrev for \"%s\": "
463 "Undefine %s abbrev: ")
465 (set-text-properties 0 (length name) nil name)
467 (not (abbrev-expansion name table))
468 (y-or-n-p (format "%s expands to \"%s\"; redefine? "
469 name (abbrev-expansion name table))))
470 (define-abbrev table (downcase name) exp))))
472 (defun inverse-add-mode-abbrev (arg)
473 "Define last word before point as a mode-specific abbrev.
474 With prefix argument N, defines the Nth word before point.
475 This command uses the minibuffer to read the expansion.
476 Expands the abbreviation after defining it."
479 (if only-global-abbrevs
481 (or local-abbrev-table
482 (error "No per-mode abbrev table")))
485 (defun inverse-add-global-abbrev (arg)
486 "Define last word before point as a global (mode-independent) abbrev.
487 With prefix argument N, defines the Nth word before point.
488 This command uses the minibuffer to read the expansion.
489 Expands the abbreviation after defining it."
491 (inverse-add-abbrev global-abbrev-table "Global" arg))
493 (defun inverse-add-abbrev (table type arg)
494 (let (name nameloc exp)
496 (forward-word (- arg))
497 (setq name (buffer-substring (point) (progn (forward-word 1)
498 (setq nameloc (point))))))
499 (set-text-properties 0 (length name) nil name)
500 (setq exp (read-string (format "%s expansion for \"%s\": "
502 (if (or (not (abbrev-expansion name table))
503 (y-or-n-p (format "%s expands to \"%s\"; redefine? "
504 name (abbrev-expansion name table))))
506 (define-abbrev table (downcase name) exp)
511 (defun abbrev-prefix-mark (&optional arg)
512 "Mark current point as the beginning of an abbrev.
513 Abbrev to be expanded starts here rather than at beginning of word.
514 This way, you can expand an abbrev with a prefix: insert the prefix,
515 use this command, then insert the abbrev."
517 (or arg (expand-abbrev))
518 (setq abbrev-start-location (point-marker)
519 abbrev-start-location-buffer (current-buffer))
520 (let ((e (make-extent (point) (point))))
521 (set-extent-begin-glyph e (make-glyph [string :data "-"]))))
523 (defun expand-region-abbrevs (start end &optional noquery)
524 "For abbrev occurrence in the region, offer to expand it.
525 The user is asked to type y or n for each occurrence.
526 A prefix argument means don't query; expand all abbrevs.
527 If called from a Lisp program, arguments are START END &optional NOQUERY."
531 (let ((lim (- (point-max) end))
533 (while (and (not (eobp))
534 (progn (forward-word 1)
535 (<= (setq pnt (point)) (- (point-max) lim))))
536 (if (abbrev-expansion
539 (save-excursion (forward-word -1) (point))
541 (if (or noquery (y-or-n-p (format "Expand `%s'? " string)))
542 (expand-abbrev)))))))
544 ;;; abbrev.el ends here