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 (table-name definitions)
71 "Define TABLE-NAME (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 table-name) (symbol-value table-name))))
75 (cond ((vectorp table))
77 (setq table (make-abbrev-table))
78 (set table-name table)
79 (setq abbrev-table-name-list (cons table-name abbrev-table-name-list)))
81 (setq table (wrong-type-argument 'vectorp table))
82 (set table-name table)))
84 (apply (function define-abbrev) table (car definitions))
85 (setq definitions (cdr definitions)))))
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'."
91 (check-type expansion (or null string))
92 (check-type count (or null integer))
93 (check-type table vector)
94 (let* ((sym (intern name table))
95 (oexp (and (boundp sym) (symbol-value sym)))
96 (ohook (and (fboundp sym) (symbol-function sym))))
97 (unless (and (equal ohook hook)
100 (string-equal oexp expansion))
101 (setq abbrevs-changed t)
102 ;; If there is a non-word character in the string, set the flag.
103 (if (string-match "\\W" name)
104 (set (intern " " table) nil)))
107 (setplist sym (or count 0))
111 ;; Fixup stuff from bootstrap def of define-abbrev-table in subr.el
112 (let ((l abbrev-table-name-list))
114 (let ((fixup (car l)))
117 (setq abbrev-table-name-list (delq fixup abbrev-table-name-list))
118 (define-abbrev-table (car fixup) (cdr fixup))))
120 ;; These are no longer initialized by C code
121 (if (not global-abbrev-table)
123 (setq global-abbrev-table (make-abbrev-table))
124 (setq abbrev-table-name-list (cons 'global-abbrev-table
125 abbrev-table-name-list))))
126 (if (not fundamental-mode-abbrev-table)
128 (setq fundamental-mode-abbrev-table (make-abbrev-table))
129 (setq abbrev-table-name-list (cons 'fundamental-mode-abbrev-table
130 abbrev-table-name-list))))
131 (and (eq major-mode 'fundamental-mode)
132 (not local-abbrev-table)
133 (setq local-abbrev-table fundamental-mode-abbrev-table)))
136 (defun define-global-abbrev (name expansion)
137 "Define ABBREV as a global abbreviation for EXPANSION."
138 (interactive "sDefine global abbrev: \nsExpansion for %s: ")
139 (define-abbrev global-abbrev-table
140 (downcase name) expansion nil 0))
142 (defun define-mode-abbrev (name expansion)
143 "Define ABBREV as a mode-specific abbreviation for EXPANSION."
144 (interactive "sDefine mode abbrev: \nsExpansion for %s: ")
145 (define-abbrev (or local-abbrev-table
146 (error "Major mode has no abbrev table"))
147 (downcase name) expansion nil 0))
149 (defun abbrev-symbol (abbrev &optional table)
150 "Return the symbol representing abbrev named ABBREV.
151 This symbol's name is ABBREV, but it is not the canonical symbol of that name;
152 it is interned in an abbrev-table rather than the normal obarray.
153 The value is nil if that abbrev is not defined.
154 Optional second arg TABLE is abbrev table to look it up in.
155 The default is to try buffer's mode-specific abbrev table, then global table."
156 (let ((frob (function (lambda (table)
157 (let ((sym (intern-soft abbrev table)))
158 (if (and (boundp sym)
159 (stringp (symbol-value sym)))
164 (or (and local-abbrev-table
165 (funcall frob local-abbrev-table))
166 (funcall frob global-abbrev-table)))))
168 (defun abbrev-expansion (abbrev &optional table)
169 "Return the string that ABBREV expands into in the current buffer.
170 Optionally specify an abbrev table as second arg;
171 then ABBREV is looked up in that table only."
172 (let ((sym (abbrev-symbol abbrev table)))
177 (defun unexpand-abbrev ()
178 "Undo the expansion of the last abbrev that expanded.
179 This differs from ordinary undo in that other editing done since then
182 (if (or (< last-abbrev-location (point-min))
183 (> last-abbrev-location (point-max))
184 (not (stringp last-abbrev-text)))
186 (let* ((opoint (point))
187 (val (symbol-value last-abbrev))
188 (adjust (length val)))
189 ;; This isn't correct if (symbol-function last-abbrev-text)
190 ;; was used to do the expansion
191 (goto-char last-abbrev-location)
192 (delete-region last-abbrev-location (+ last-abbrev-location adjust))
193 (insert last-abbrev-text)
194 (setq adjust (- adjust (length last-abbrev-text)))
195 (setq last-abbrev-text nil)
196 (if (< last-abbrev-location opoint)
197 (goto-char (- opoint adjust))
198 (goto-char opoint)))))
202 (defun insert-abbrev-table-description (name &optional human-readable)
203 "Insert before point a full description of abbrev table named NAME.
204 NAME is a symbol whose value is an abbrev table.
205 If optional second argument HUMAN-READABLE is non-nil, insert a
206 human-readable description. Otherwise the description is an
207 expression, a call to `define-abbrev-table', which would define the
208 abbrev table NAME exactly as it is currently defined."
209 (let ((table (symbol-value name))
210 (stream (current-buffer)))
211 (message "Abbrev-table %s..." name)
214 (prin1 (list name) stream)
215 ;; Need two terpri's or cretinous edit-abbrevs blows out
218 (mapatoms (function (lambda (sym)
219 (if (symbol-value sym)
220 (let* ((n (prin1-to-string (symbol-name sym)))
224 (write-char ?\ stream)
226 (princ (format " %-5S " (symbol-plist sym))
228 (if (not (symbol-function sym))
229 (prin1 (symbol-value sym) stream)
231 (setq n (prin1-to-string (symbol-value sym))
232 pos (+ pos 6 (length n)))
235 (write-char ?\ stream)
237 (prin1 (symbol-function sym) stream)))
242 (princ "\(define-abbrev-table '" stream)
244 (princ " '\(\n" stream)
245 (mapatoms (function (lambda (sym)
246 (if (symbol-value sym)
249 (prin1 (list (symbol-name sym)
251 (symbol-function sym)
256 (princ " \)\)\n" stream)))
259 ;;; End code not in FSF
261 (defun abbrev-mode (arg)
263 With argument ARG, enable abbrev mode if ARG is positive, else disable.
264 In abbrev mode, inserting an abbreviation causes it to expand
265 and be replaced by its expansion."
268 (if (null arg) (not abbrev-mode)
269 (> (prefix-numeric-value arg) 0)))
274 (defvar edit-abbrevs-map nil
275 "Keymap used in edit-abbrevs.")
278 (setq edit-abbrevs-map (make-sparse-keymap))
280 (set-keymap-name edit-abbrevs-map 'edit-abbrevs-map)
281 (define-key edit-abbrevs-map "\C-x\C-s" 'edit-abbrevs-redefine)
282 (define-key edit-abbrevs-map "\C-c\C-c" 'edit-abbrevs-redefine))
284 (defun kill-all-abbrevs ()
285 "Undefine all defined abbrevs."
287 (let ((tables abbrev-table-name-list))
289 (clear-abbrev-table (symbol-value (car tables)))
290 (setq tables (cdr tables)))))
292 (defun insert-abbrevs ()
293 "Insert after point a description of all defined abbrevs.
294 Mark is set after the inserted text."
298 (let ((tables abbrev-table-name-list))
300 (insert-abbrev-table-description (car tables) t)
301 (setq tables (cdr tables))))
304 (defun list-abbrevs ()
305 "Display a list of all defined abbrevs."
307 (display-buffer (prepare-abbrev-list-buffer)))
309 (defun prepare-abbrev-list-buffer ()
311 (set-buffer (get-buffer-create "*Abbrevs*"))
313 (let ((tables abbrev-table-name-list))
315 (insert-abbrev-table-description (car tables) t)
316 (setq tables (cdr tables))))
317 (goto-char (point-min))
318 (set-buffer-modified-p nil)
320 (get-buffer-create "*Abbrevs*"))
322 (defun edit-abbrevs-mode ()
323 "Major mode for editing the list of abbrev definitions.
324 \\{edit-abbrevs-map}"
326 (setq major-mode 'edit-abbrevs-mode)
327 (setq mode-name "Edit-Abbrevs")
328 (use-local-map edit-abbrevs-map))
330 (defun edit-abbrevs ()
331 "Alter abbrev definitions by editing a list of them.
332 Selects a buffer containing a list of abbrev definitions.
333 You can edit them and type \\<edit-abbrevs-map>\\[edit-abbrevs-redefine] to redefine abbrevs
334 according to your editing.
335 Buffer contains a header line for each abbrev table,
336 which is the abbrev table name in parentheses.
337 This is followed by one line per abbrev in that table:
338 NAME USECOUNT EXPANSION HOOK
339 where NAME and EXPANSION are strings with quotes,
340 USECOUNT is an integer, and HOOK is any valid function
341 or may be omitted (it is usually omitted)."
343 (switch-to-buffer (prepare-abbrev-list-buffer)))
345 (defun edit-abbrevs-redefine ()
346 "Redefine abbrevs according to current buffer contents."
349 (set-buffer-modified-p nil))
351 (defun define-abbrevs (&optional arg)
352 "Define abbrevs according to current visible buffer contents.
353 See documentation of `edit-abbrevs' for info on the format of the
354 text you must have in the buffer.
355 With argument, eliminate all abbrev definitions except
356 the ones defined from the buffer now."
358 (if arg (kill-all-abbrevs))
360 (goto-char (point-min))
361 (while (and (not (eobp)) (re-search-forward "^(" nil t))
362 (let* ((buf (current-buffer))
364 abbrevs name hook exp count)
366 (while (progn (forward-line 1)
368 (setq name (read buf) count (read buf) exp (read buf))
369 (skip-chars-backward " \t\n\f")
370 (setq hook (if (not (eolp)) (read buf)))
371 (skip-chars-backward " \t\n\f")
372 (setq abbrevs (cons (list name exp hook count) abbrevs)))
373 (define-abbrev-table table abbrevs)))))
375 (defun read-abbrev-file (&optional file quietly)
376 "Read abbrev definitions from file written with `write-abbrev-file'.
377 Optional argument FILE is the name of the file to read;
378 it defaults to the value of `abbrev-file-name'.
379 Optional second argument QUIETLY non-nil means don't print anything."
380 (interactive "fRead abbrev file: ")
381 (load (if (and file (> (length file) 0)) file abbrev-file-name)
383 (setq save-abbrevs t abbrevs-changed nil))
385 (defun quietly-read-abbrev-file (&optional file)
386 "Read abbrev definitions from file written with `write-abbrev-file'.
387 Optional argument FILE is the name of the file to read;
388 it defaults to the value of `abbrev-file-name'.
389 Does not print anything."
390 ;(interactive "fRead abbrev file: ")
391 (read-abbrev-file file t))
393 (defun write-abbrev-file (file)
394 "Write all abbrev definitions to a file of Lisp code.
395 The file written can be loaded in another session to define the same abbrevs.
396 The argument FILE is the file name to write."
399 (read-file-name "Write abbrev file: "
400 (file-name-directory (expand-file-name abbrev-file-name))
402 (or (and file (> (length file) 0))
403 (setq file abbrev-file-name))
405 (set-buffer (get-buffer-create " write-abbrev-file"))
407 (let ((tables abbrev-table-name-list))
409 (insert-abbrev-table-description (car tables) nil)
410 (setq tables (cdr tables))))
411 (write-region 1 (point-max) file)
414 (defun abbrev-string-to-be-defined (arg)
415 "Return the string for which an abbrev will be defined.
416 ARG is the argument to `add-global-abbrev' or `add-mode-abbrev'."
417 (if (and (not arg) (region-active-p)) (setq arg 0)
418 (setq arg (prefix-numeric-value arg)))
423 (save-excursion (backward-word arg) (point))))))
425 (defun add-mode-abbrev (arg)
426 "Define mode-specific abbrev for last word(s) before point.
427 Argument is how many words before point form the expansion;
428 or zero means the region is the expansion.
429 A negative argument means to undefine the specified abbrev.
430 Reads the abbreviation in the minibuffer.
432 Don't use this function in a Lisp program; use `define-abbrev' instead."
436 (if only-global-abbrevs
438 (or local-abbrev-table
439 (error "No per-mode abbrev table")))
442 (defun add-global-abbrev (arg)
443 "Define global (all modes) abbrev for last word(s) before point.
444 The prefix argument specifies the number of words before point that form the
445 expansion; or zero means the region is the expansion.
446 A negative argument means to undefine the specified abbrev.
447 This command uses the minibuffer to read the abbreviation.
449 Don't use this function in a Lisp program; use `define-abbrev' instead."
452 (add-abbrev global-abbrev-table "Global" arg))
454 (defun add-abbrev (table type arg)
455 "Add an abbreviation to abbrev table TABLE.
456 TYPE is a string describing in English the kind of abbrev this will be
457 (typically, \"global\" or \"mode-specific\"); this is used in
458 prompting the user. ARG is the number of words in the expansion.
460 Return the symbol that internally represents the new abbrev, or nil if
461 the user declines to confirm redefining an existing abbrev."
463 (let ((exp (abbrev-string-to-be-defined arg))
466 (read-string (format (if exp "%s abbrev for \"%s\": "
467 "Undefine %s abbrev: ")
469 (set-text-properties 0 (length name) nil name)
471 (not (abbrev-expansion name table))
472 (y-or-n-p (format "%s expands to \"%s\"; redefine? "
473 name (abbrev-expansion name table))))
474 (define-abbrev table (downcase name) exp))))
476 (defun inverse-abbrev-string-to-be-defined (arg)
477 "Return the string for which an inverse abbrev will be defined.
478 ARG is the argument to `inverse-add-global-abbrev' or
479 `inverse-add-mode-abbrev'."
482 (buffer-substring (point) (progn (forward-word 1) (point)))))
484 (defun inverse-add-mode-abbrev (arg)
485 "Define last word before point as a mode-specific abbrev.
486 With prefix argument N, defines the Nth word before point.
487 This command uses the minibuffer to read the expansion.
488 Expands the abbreviation after defining it."
491 (if only-global-abbrevs
493 (or local-abbrev-table
494 (error "No per-mode abbrev table")))
497 (defun inverse-add-global-abbrev (arg)
498 "Define last word before point as a global (mode-independent) abbrev.
499 With prefix argument N, defines the Nth word before point.
500 This command uses the minibuffer to read the expansion.
501 Expands the abbreviation after defining it."
503 (inverse-add-abbrev global-abbrev-table "Global" arg))
505 (defun inverse-add-abbrev (table type arg)
506 (let (name nameloc exp)
509 (setq name (buffer-substring (point) (progn (forward-word 1)
510 (setq nameloc (point))))))
511 (set-text-properties 0 (length name) nil name)
512 (setq exp (read-string (format "%s expansion for \"%s\": "
514 (if (or (not (abbrev-expansion name table))
515 (y-or-n-p (format "%s expands to \"%s\"; redefine? "
516 name (abbrev-expansion name table))))
518 (define-abbrev table (downcase name) exp)
523 (defun abbrev-prefix-mark (&optional arg)
524 "Mark current point as the beginning of an abbrev.
525 Abbrev to be expanded starts here rather than at beginning of word.
526 This way, you can expand an abbrev with a prefix: insert the prefix,
527 use this command, then insert the abbrev."
529 (or arg (expand-abbrev))
530 (setq abbrev-start-location (point-marker)
531 abbrev-start-location-buffer (current-buffer))
532 (let ((e (make-extent (point) (point))))
533 (set-extent-begin-glyph e (make-glyph [string :data "-"]))))
535 (defun expand-region-abbrevs (start end &optional noquery)
536 "For abbrev occurrence in the region, offer to expand it.
537 The user is asked to type y or n for each occurrence.
538 A prefix argument means don't query; expand all abbrevs.
539 If called from a Lisp program, arguments are START END &optional NOQUERY."
543 (let ((lim (- (point-max) end))
545 (while (and (not (eobp))
546 (progn (forward-word 1)
547 (<= (setq pnt (point)) (- (point-max) lim))))
548 (if (abbrev-expansion
551 (save-excursion (backward-word) (point))
553 (if (or noquery (y-or-n-p (format "Expand `%s'? " string)))
554 (expand-abbrev)))))))
556 ;;; abbrev.el ends here