Move lisp/emacs-lisp/authors.el to admin/
[bpt/emacs.git] / lisp / abbrev.el
CommitLineData
ba83908c 1;;; abbrev.el --- abbrev mode commands for Emacs -*- lexical-binding: t -*-
e2fbf49d 2
ba318903 3;; Copyright (C) 1985-1987, 1992, 2001-2014 Free Software Foundation,
ab422c4d 4;; Inc.
e2fbf49d 5
34dc21db 6;; Maintainer: emacs-devel@gnu.org
f5f727f8 7;; Keywords: abbrev convenience
bd78fa1d 8;; Package: emacs
e9571d2a 9
e2fbf49d
JB
10;; This file is part of GNU Emacs.
11
eb3fa2cf 12;; GNU Emacs is free software: you can redistribute it and/or modify
e2fbf49d 13;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
e2fbf49d
JB
16
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
eb3fa2cf 23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
e2fbf49d 24
e41b2db1
ER
25;;; Commentary:
26
27;; This facility is documented in the Emacs Manual.
28
e047f448
SM
29;; Todo:
30
e047f448
SM
31;; - Cleanup name space.
32
e5167999 33;;; Code:
e2fbf49d 34
f58e0fd5 35(eval-when-compile (require 'cl-lib))
e047f448
SM
36
37(defgroup abbrev-mode nil
38 "Word abbreviations mode."
39 :link '(custom-manual "(emacs)Abbrevs")
40 :group 'abbrev)
41
2aa0e5bf
SM
42(defcustom abbrev-file-name
43 (locate-user-emacs-file "abbrev_defs" ".abbrev_defs")
790d0270
SM
44 "Default name of file from which to read abbrevs."
45 :initialize 'custom-initialize-delay
2aa0e5bf
SM
46 :type 'file)
47
7cfedc97 48(defcustom only-global-abbrevs nil
e7fdaf63 49 "Non-nil means user plans to use global abbrevs only.
1d1e35a0
RS
50This makes the commands that normally define mode-specific abbrevs
51define global abbrevs instead."
52 :type 'boolean
f5f727f8
DN
53 :group 'abbrev-mode
54 :group 'convenience)
e2fbf49d 55
497afe07 56(define-minor-mode abbrev-mode
38002bff 57 "Toggle Abbrev mode in the current buffer.
06e21633
CY
58With a prefix argument ARG, enable Abbrev mode if ARG is
59positive, and disable it otherwise. If called from Lisp, enable
60Abbrev mode if ARG is omitted or nil.
61
62In Abbrev mode, inserting an abbreviation causes it to expand and
63be replaced by its expansion."
9d794026
GM
64 ;; It's defined in C, this stops the d-m-m macro defining it again.
65 :variable abbrev-mode)
1d1e35a0 66
f31b1257 67(put 'abbrev-mode 'safe-local-variable 'booleanp)
1d1e35a0 68
e2fbf49d 69\f
86cf7329 70(defvar edit-abbrevs-mode-map
e7fdaf63 71 (let ((map (make-sparse-keymap)))
eb7ffc14
LL
72 (define-key map "\C-x\C-s" 'abbrev-edit-save-buffer)
73 (define-key map "\C-x\C-w" 'abbrev-edit-save-to-file)
e7fdaf63
JPW
74 (define-key map "\C-c\C-c" 'edit-abbrevs-redefine)
75 map)
38002bff 76 "Keymap used in `edit-abbrevs'.")
86cf7329
SM
77(define-obsolete-variable-alias 'edit-abbrevs-map
78 'edit-abbrevs-mode-map "24.4")
e2fbf49d
JB
79
80(defun kill-all-abbrevs ()
81 "Undefine all defined abbrevs."
82 (interactive)
0b281d03
SM
83 (dolist (tablesym abbrev-table-name-list)
84 (clear-abbrev-table (symbol-value tablesym))))
e2fbf49d 85
ad9d51b2 86(defun copy-abbrev-table (table)
ea16568d
GM
87 "Make a new abbrev-table with the same abbrevs as TABLE.
88Does not copy property lists."
ad9d51b2
RS
89 (let ((new-table (make-abbrev-table)))
90 (mapatoms
91 (lambda (symbol)
92 (define-abbrev new-table
93 (symbol-name symbol)
94 (symbol-value symbol)
95 (symbol-function symbol)))
96 table)
97 new-table))
98
e2fbf49d
JB
99(defun insert-abbrevs ()
100 "Insert after point a description of all defined abbrevs.
101Mark is set after the inserted text."
102 (interactive)
103 (push-mark
104 (save-excursion
0b281d03
SM
105 (dolist (tablesym abbrev-table-name-list)
106 (insert-abbrev-table-description tablesym t))
71baa28f 107 (point))))
e2fbf49d 108
c88a9944
GM
109(defun list-abbrevs (&optional local)
110 "Display a list of defined abbrevs.
111If LOCAL is non-nil, interactively when invoked with a
112prefix arg, display only local, i.e. mode-specific, abbrevs.
113Otherwise display all abbrevs."
114 (interactive "P")
115 (display-buffer (prepare-abbrev-list-buffer local)))
116
117(defun abbrev-table-name (table)
118 "Value is the name of abbrev table TABLE."
119 (let ((tables abbrev-table-name-list)
120 found)
121 (while (and (not found) tables)
122 (when (eq (symbol-value (car tables)) table)
123 (setq found (car tables)))
124 (setq tables (cdr tables)))
125 found))
7cfedc97 126
c88a9944 127(defun prepare-abbrev-list-buffer (&optional local)
8d4708af
BG
128 (let ((local-table local-abbrev-table))
129 (with-current-buffer (get-buffer-create "*Abbrevs*")
130 (erase-buffer)
131 (if local
132 (insert-abbrev-table-description
133 (abbrev-table-name local-table) t)
947b6566
LL
134 (let (empty-tables)
135 (dolist (table abbrev-table-name-list)
136 (if (abbrev-table-empty-p (symbol-value table))
137 (push table empty-tables)
138 (insert-abbrev-table-description table t)))
139 (dolist (table (nreverse empty-tables))
12a106a9 140 (insert-abbrev-table-description table t)))
0ba2624f 141 ;; Note: `list-abbrevs' can display only local abbrevs, in
12a106a9
LL
142 ;; which case editing could lose abbrevs of other tables. Thus
143 ;; enter `edit-abbrevs-mode' only if LOCAL is nil.
144 (edit-abbrevs-mode))
8d4708af
BG
145 (goto-char (point-min))
146 (set-buffer-modified-p nil)
8d4708af 147 (current-buffer))))
e2fbf49d 148
e2fbf49d
JB
149(defun edit-abbrevs ()
150 "Alter abbrev definitions by editing a list of them.
12a106a9
LL
151Selects a buffer containing a list of abbrev definitions with
152point located in the abbrev table of current buffer.
e2fbf49d
JB
153You can edit them and type \\<edit-abbrevs-map>\\[edit-abbrevs-redefine] to redefine abbrevs
154according to your editing.
155Buffer contains a header line for each abbrev table,
156 which is the abbrev table name in parentheses.
157This is followed by one line per abbrev in that table:
158NAME USECOUNT EXPANSION HOOK
159where NAME and EXPANSION are strings with quotes,
160USECOUNT is an integer, and HOOK is any valid function
161or may be omitted (it is usually omitted)."
162 (interactive)
12a106a9
LL
163 (let ((table-name (abbrev-table-name local-abbrev-table)))
164 (switch-to-buffer (prepare-abbrev-list-buffer))
165 (when (and table-name
166 (search-forward
167 (concat "(" (symbol-name table-name) ")\n\n") nil t))
168 (goto-char (match-end 0)))))
e2fbf49d
JB
169
170(defun edit-abbrevs-redefine ()
171 "Redefine abbrevs according to current buffer contents."
172 (interactive)
4cabf12b
RS
173 (save-restriction
174 (widen)
175 (define-abbrevs t)
176 (set-buffer-modified-p nil)))
e2fbf49d
JB
177
178(defun define-abbrevs (&optional arg)
179 "Define abbrevs according to current visible buffer contents.
180See documentation of `edit-abbrevs' for info on the format of the
181text you must have in the buffer.
182With argument, eliminate all abbrev definitions except
183the ones defined from the buffer now."
184 (interactive "P")
185 (if arg (kill-all-abbrevs))
186 (save-excursion
71baa28f
EZ
187 (goto-char (point-min))
188 (while (and (not (eobp)) (re-search-forward "^(" nil t))
189 (let* ((buf (current-buffer))
190 (table (read buf))
191 abbrevs name hook exp count sys)
192 (forward-line 1)
193 (while (progn (forward-line 1)
194 (not (eolp)))
195 (setq name (read buf) count (read buf))
196 (if (equal count '(sys))
2d44d9cc
LL
197 (setq sys t count (read buf))
198 (setq sys nil))
71baa28f
EZ
199 (setq exp (read buf))
200 (skip-chars-backward " \t\n\f")
201 (setq hook (if (not (eolp)) (read buf)))
202 (skip-chars-backward " \t\n\f")
203 (setq abbrevs (cons (list name exp hook count sys) abbrevs)))
204 (define-abbrev-table table abbrevs)))))
e2fbf49d
JB
205
206(defun read-abbrev-file (&optional file quietly)
207 "Read abbrev definitions from file written with `write-abbrev-file'.
208Optional argument FILE is the name of the file to read;
209it defaults to the value of `abbrev-file-name'.
d0b6d945 210Optional second argument QUIETLY non-nil means don't display a message."
4cabf12b
RS
211 (interactive
212 (list
213 (read-file-name (format "Read abbrev file (default %s): "
214 abbrev-file-name)
215 nil abbrev-file-name t)))
ec7793c3 216 (load (or file abbrev-file-name) nil quietly)
d0b6d945 217 (setq abbrevs-changed nil))
e2fbf49d
JB
218
219(defun quietly-read-abbrev-file (&optional file)
e7fdaf63 220 "Read abbrev definitions from file written with `write-abbrev-file'.
e2fbf49d
JB
221Optional argument FILE is the name of the file to read;
222it defaults to the value of `abbrev-file-name'.
d0b6d945 223Does not display any message."
71baa28f 224 ;(interactive "fRead abbrev file: ")
e2fbf49d
JB
225 (read-abbrev-file file t))
226
eb7ffc14 227(defun write-abbrev-file (&optional file verbose)
68063965
LT
228 "Write all user-level abbrev definitions to a file of Lisp code.
229This does not include system abbrevs; it includes only the abbrev tables
230listed in listed in `abbrev-table-name-list'.
e2fbf49d 231The file written can be loaded in another session to define the same abbrevs.
68063965 232The argument FILE is the file name to write. If omitted or nil, the file
eb7ffc14
LL
233specified in `abbrev-file-name' is used.
234If VERBOSE is non-nil, display a message indicating where abbrevs
235have been saved."
e2fbf49d
JB
236 (interactive
237 (list
238 (read-file-name "Write abbrev file: "
239 (file-name-directory (expand-file-name abbrev-file-name))
240 abbrev-file-name)))
e2fbf49d
JB
241 (or (and file (> (length file) 0))
242 (setq file abbrev-file-name))
4b978a67
LL
243 (let ((coding-system-for-write 'utf-8))
244 (with-temp-buffer
71baa28f 245 (dolist (table
4b978a67
LL
246 ;; We sort the table in order to ease the automatic
247 ;; merging of different versions of the user's abbrevs
248 ;; file. This is useful, for example, for when the
249 ;; user keeps their home directory in a revision
250 ;; control system, and is therefore keeping multiple
251 ;; slightly-differing copies loosely synchronized.
252 (sort (copy-sequence abbrev-table-name-list)
253 (lambda (s1 s2)
254 (string< (symbol-name s1)
255 (symbol-name s2)))))
256 (insert-abbrev-table-description table nil))
257 (when (unencodable-char-position (point-min) (point-max) 'utf-8)
258 (setq coding-system-for-write
259 (if (> emacs-major-version 24)
260 'utf-8-emacs
261 ;; For compatibility with Emacs 22 (See Bug#8308)
262 'emacs-mule)))
263 (goto-char (point-min))
264 (insert (format ";;-*-coding: %s;-*-\n" coding-system-for-write))
eb7ffc14
LL
265 (write-region nil nil file nil (and (not verbose) 0)))))
266
267(defun abbrev-edit-save-to-file (file)
268 "Save all user-level abbrev definitions in current buffer to FILE."
269 (interactive
270 (list (read-file-name "Save abbrevs to file: "
271 (file-name-directory
272 (expand-file-name abbrev-file-name))
273 abbrev-file-name)))
274 (edit-abbrevs-redefine)
275 (write-abbrev-file file t))
276
277(defun abbrev-edit-save-buffer ()
278 "Save all user-level abbrev definitions in current buffer.
279The saved abbrevs are written to the file specified by
280`abbrev-file-name'."
281 (interactive)
282 (abbrev-edit-save-to-file abbrev-file-name))
283
e2fbf49d
JB
284\f
285(defun add-mode-abbrev (arg)
286 "Define mode-specific abbrev for last word(s) before point.
287Argument is how many words before point form the expansion;
288or zero means the region is the expansion.
289A negative argument means to undefine the specified abbrev.
290Reads the abbreviation in the minibuffer.
291
292Don't use this function in a Lisp program; use `define-abbrev' instead."
293 (interactive "p")
294 (add-abbrev
295 (if only-global-abbrevs
71296446 296 global-abbrev-table
e2fbf49d
JB
297 (or local-abbrev-table
298 (error "No per-mode abbrev table")))
299 "Mode" arg))
300
301(defun add-global-abbrev (arg)
302 "Define global (all modes) abbrev for last word(s) before point.
303The prefix argument specifies the number of words before point that form the
304expansion; or zero means the region is the expansion.
305A negative argument means to undefine the specified abbrev.
306This command uses the minibuffer to read the abbreviation.
307
308Don't use this function in a Lisp program; use `define-abbrev' instead."
309 (interactive "p")
310 (add-abbrev global-abbrev-table "Global" arg))
311
312(defun add-abbrev (table type arg)
313 (let ((exp (and (>= arg 0)
34f3cd03 314 (buffer-substring-no-properties
e2fbf49d
JB
315 (point)
316 (if (= arg 0) (mark)
317 (save-excursion (forward-word (- arg)) (point))))))
318 name)
319 (setq name
320 (read-string (format (if exp "%s abbrev for \"%s\": "
321 "Undefine %s abbrev: ")
322 type exp)))
a0f88464 323 (set-text-properties 0 (length name) nil name)
e2fbf49d
JB
324 (if (or (null exp)
325 (not (abbrev-expansion name table))
326 (y-or-n-p (format "%s expands to \"%s\"; redefine? "
327 name (abbrev-expansion name table))))
328 (define-abbrev table (downcase name) exp))))
7cfedc97 329
e66b273f 330(defun inverse-add-mode-abbrev (n)
e2fbf49d
JB
331 "Define last word before point as a mode-specific abbrev.
332With prefix argument N, defines the Nth word before point.
333This command uses the minibuffer to read the expansion.
334Expands the abbreviation after defining it."
335 (interactive "p")
336 (inverse-add-abbrev
337 (if only-global-abbrevs
7cfedc97 338 global-abbrev-table
e2fbf49d
JB
339 (or local-abbrev-table
340 (error "No per-mode abbrev table")))
e66b273f 341 "Mode" n))
e2fbf49d 342
e66b273f 343(defun inverse-add-global-abbrev (n)
e2fbf49d
JB
344 "Define last word before point as a global (mode-independent) abbrev.
345With prefix argument N, defines the Nth word before point.
346This command uses the minibuffer to read the expansion.
347Expands the abbreviation after defining it."
348 (interactive "p")
e66b273f 349 (inverse-add-abbrev global-abbrev-table "Global" n))
e2fbf49d
JB
350
351(defun inverse-add-abbrev (table type arg)
46684068 352 (let (name exp start end)
e2fbf49d 353 (save-excursion
46684068
GM
354 (forward-word (1+ (- arg)))
355 (setq end (point))
356 (backward-word 1)
357 (setq start (point)
358 name (buffer-substring-no-properties start end)))
359
360 (setq exp (read-string (format "%s expansion for \"%s\": " type name)
361 nil nil nil t))
362 (when (or (not (abbrev-expansion name table))
363 (y-or-n-p (format "%s expands to \"%s\"; redefine? "
364 name (abbrev-expansion name table))))
365 (define-abbrev table (downcase name) exp)
366 (save-excursion
367 (goto-char end)
368 (expand-abbrev)))))
e2fbf49d
JB
369
370(defun abbrev-prefix-mark (&optional arg)
371 "Mark current point as the beginning of an abbrev.
372Abbrev to be expanded starts here rather than at beginning of word.
373This way, you can expand an abbrev with a prefix: insert the prefix,
68063965 374use this command, then insert the abbrev. This command inserts a
29c48340 375temporary hyphen after the prefix (until the intended abbrev
68063965
LT
376expansion occurs).
377If the prefix is itself an abbrev, this command expands it, unless
378ARG is non-nil. Interactively, ARG is the prefix argument."
e2fbf49d
JB
379 (interactive "P")
380 (or arg (expand-abbrev))
381 (setq abbrev-start-location (point-marker)
382 abbrev-start-location-buffer (current-buffer))
383 (insert "-"))
384
385(defun expand-region-abbrevs (start end &optional noquery)
386 "For abbrev occurrence in the region, offer to expand it.
e66b273f
JB
387The user is asked to type `y' or `n' for each occurrence.
388A prefix argument means don't query; expand all abbrevs."
e2fbf49d
JB
389 (interactive "r\nP")
390 (save-excursion
391 (goto-char start)
392 (let ((lim (- (point-max) end))
393 pnt string)
394 (while (and (not (eobp))
395 (progn (forward-word 1)
396 (<= (setq pnt (point)) (- (point-max) lim))))
397 (if (abbrev-expansion
398 (setq string
34f3cd03 399 (buffer-substring-no-properties
e2fbf49d
JB
400 (save-excursion (forward-word -1) (point))
401 pnt)))
402 (if (or noquery (y-or-n-p (format "Expand `%s'? " string)))
403 (expand-abbrev)))))))
c0274f38 404
e047f448
SM
405;;; Abbrev properties.
406
407(defun abbrev-table-get (table prop)
408 "Get the PROP property of abbrev table TABLE."
409 (let ((sym (intern-soft "" table)))
410 (if sym (get sym prop))))
411
412(defun abbrev-table-put (table prop val)
413 "Set the PROP property of abbrev table TABLE to VAL."
414 (let ((sym (intern "" table)))
415 (set sym nil) ; Make sure it won't be confused for an abbrev.
416 (put sym prop val)))
417
79415279
SM
418(defalias 'abbrev-get 'get
419 "Get the property PROP of abbrev ABBREV
420
421\(fn ABBREV PROP)")
422
423(defalias 'abbrev-put 'put
7e99158a 424 "Set the property PROP of abbrev ABBREV to value VAL.
79415279
SM
425See `define-abbrev' for the effect of some special properties.
426
427\(fn ABBREV PROP VAL)")
e047f448 428
e047f448
SM
429;;; Code that used to be implemented in src/abbrev.c
430
431(defvar abbrev-table-name-list '(fundamental-mode-abbrev-table
432 global-abbrev-table)
433 "List of symbols whose values are abbrev tables.")
434
435(defun make-abbrev-table (&optional props)
436 "Create a new, empty abbrev table object.
da59e7b2 437PROPS is a list of properties."
e047f448
SM
438 ;; The value 59 is an arbitrary prime number.
439 (let ((table (make-vector 59 0)))
440 ;; Each abbrev-table has a `modiff' counter which can be used to detect
441 ;; when an abbreviation was added. An example of use would be to
442 ;; construct :regexp dynamically as the union of all abbrev names, so
443 ;; `modiff' can let us detect that an abbrev was added and hence :regexp
444 ;; needs to be refreshed.
445 ;; The presence of `modiff' entry is also used as a tag indicating this
446 ;; vector is really an abbrev-table.
447 (abbrev-table-put table :abbrev-table-modiff 0)
448 (while (consp props)
449 (abbrev-table-put table (pop props) (pop props)))
450 table))
451
452(defun abbrev-table-p (object)
ea16568d 453 "Return non-nil if OBJECT is an abbrev table."
e047f448
SM
454 (and (vectorp object)
455 (numberp (abbrev-table-get object :abbrev-table-modiff))))
456
947b6566
LL
457(defun abbrev-table-empty-p (object &optional ignore-system)
458 "Return nil if there are no abbrev symbols in OBJECT.
459If IGNORE-SYSTEM is non-nil, system definitions are ignored."
460 (unless (abbrev-table-p object)
461 (error "Non abbrev table object"))
462 (not (catch 'some
463 (mapatoms (lambda (abbrev)
464 (unless (or (zerop (length (symbol-name abbrev)))
465 (and ignore-system
466 (abbrev-get abbrev :system)))
467 (throw 'some t)))
468 object))))
469
e047f448
SM
470(defvar global-abbrev-table (make-abbrev-table)
471 "The abbrev table whose abbrevs affect all buffers.
472Each buffer may also have a local abbrev table.
473If it does, the local table overrides the global one
474for any particular abbrev defined in both.")
475
476(defvar abbrev-minor-mode-table-alist nil
477 "Alist of abbrev tables to use for minor modes.
478Each element looks like (VARIABLE . ABBREV-TABLE);
ea16568d
GM
479ABBREV-TABLE is active whenever VARIABLE's value is non-nil.
480ABBREV-TABLE can also be a list of abbrev tables.")
e047f448
SM
481
482(defvar fundamental-mode-abbrev-table
483 (let ((table (make-abbrev-table)))
484 ;; Set local-abbrev-table's default to be fundamental-mode-abbrev-table.
485 (setq-default local-abbrev-table table)
486 table)
487 "The abbrev table of mode-specific abbrevs for Fundamental Mode.")
488
489(defvar abbrevs-changed nil
490 "Set non-nil by defining or altering any word abbrevs.
491This causes `save-some-buffers' to offer to save the abbrevs.")
492
493(defcustom abbrev-all-caps nil
494 "Non-nil means expand multi-word abbrevs all caps if abbrev was so."
495 :type 'boolean
496 :group 'abbrev-mode)
497
498(defvar abbrev-start-location nil
499 "Buffer position for `expand-abbrev' to use as the start of the abbrev.
500When nil, use the word before point as the abbrev.
501Calling `expand-abbrev' sets this to nil.")
502
503(defvar abbrev-start-location-buffer nil
504 "Buffer that `abbrev-start-location' has been set for.
505Trying to expand an abbrev in any other buffer clears `abbrev-start-location'.")
506
507(defvar last-abbrev nil
508 "The abbrev-symbol of the last abbrev expanded. See `abbrev-symbol'.")
509
510(defvar last-abbrev-text nil
511 "The exact text of the last abbrev expanded.
29c48340 512It is nil if the abbrev has already been unexpanded.")
e047f448
SM
513
514(defvar last-abbrev-location 0
515 "The location of the start of the last abbrev expanded.")
516
517;; (defvar local-abbrev-table fundamental-mode-abbrev-table
518;; "Local (mode-specific) abbrev table of current buffer.")
519;; (make-variable-buffer-local 'local-abbrev-table)
520
521(defcustom pre-abbrev-expand-hook nil
522 "Function or functions to be called before abbrev expansion is done.
523This is the first thing that `expand-abbrev' does, and so this may change
524the current abbrev table before abbrev lookup happens."
525 :type 'hook
526 :group 'abbrev-mode)
d36ed1c8 527(make-obsolete-variable 'pre-abbrev-expand-hook 'abbrev-expand-function "23.1")
e047f448
SM
528
529(defun clear-abbrev-table (table)
530 "Undefine all abbrevs in abbrev table TABLE, leaving it empty."
531 (setq abbrevs-changed t)
0b281d03
SM
532 (let* ((sym (intern-soft "" table)))
533 (dotimes (i (length table))
534 (aset table i 0))
535 ;; Preserve the table's properties.
f58e0fd5 536 (cl-assert sym)
938a9a9e
SM
537 (let ((newsym (intern "" table)))
538 (set newsym nil) ; Make sure it won't be confused for an abbrev.
539 (setplist newsym (symbol-plist sym)))
0b281d03 540 (abbrev-table-put table :abbrev-table-modiff
f13f18d9 541 (1+ (abbrev-table-get table :abbrev-table-modiff))))
ba9db1c5 542 ;; For backward compatibility, always return nil.
f13f18d9 543 nil)
e047f448
SM
544
545(defun define-abbrev (table name expansion &optional hook &rest props)
546 "Define an abbrev in TABLE named NAME, to expand to EXPANSION and call HOOK.
547NAME must be a string, and should be lower-case.
548EXPANSION should usually be a string.
549To undefine an abbrev, define it with EXPANSION = nil.
550If HOOK is non-nil, it should be a function of no arguments;
551it is called after EXPANSION is inserted.
434c0be6
CY
552If EXPANSION is not a string (and not nil), the abbrev is a
553 special one, which does not expand in the usual way but only
554 runs HOOK.
e047f448 555
ea16568d
GM
556If HOOK is a non-nil symbol with a non-nil `no-self-insert' property,
557it can control whether the character that triggered abbrev expansion
558is inserted. If such a HOOK returns non-nil, the character is not
559inserted. If such a HOOK returns nil, then so does `abbrev-insert'
560\(and `expand-abbrev'), as if no abbrev expansion had taken place.
561
e047f448 562PROPS is a property list. The following properties are special:
da59e7b2
SM
563- `:count': the value for the abbrev's usage-count, which is incremented each
564 time the abbrev is used (the default is zero).
79415279 565- `:system': if non-nil, says that this is a \"system\" abbreviation
e047f448 566 which should not be saved in the user's abbreviation file.
79415279 567 Unless `:system' is `force', a system abbreviation will not
e047f448
SM
568 overwrite a non-system abbreviation of the same name.
569- `:case-fixed': non-nil means that abbreviations are looked up without
570 case-folding, and the expansion is not capitalized/upcased.
64f00939 571- `:enable-function': a function of no argument which returns non-nil if the
e047f448
SM
572 abbrev should be used for a particular call of `expand-abbrev'.
573
574An obsolete but still supported calling form is:
575
79415279 576\(define-abbrev TABLE NAME EXPANSION &optional HOOK COUNT SYSTEM)."
e047f448
SM
577 (when (and (consp props) (or (null (car props)) (numberp (car props))))
578 ;; Old-style calling convention.
f58e0fd5
SM
579 (setq props `(:count ,(car props)
580 ,@(if (cadr props) (list :system (cadr props))))))
79415279
SM
581 (unless (plist-get props :count)
582 (setq props (plist-put props :count 0)))
583 (let ((system-flag (plist-get props :system))
e047f448
SM
584 (sym (intern name table)))
585 ;; Don't override a prior user-defined abbrev with a system abbrev,
586 ;; unless system-flag is `force'.
587 (unless (and (not (memq system-flag '(nil force)))
588 (boundp sym) (symbol-value sym)
79415279 589 (not (abbrev-get sym :system)))
e047f448 590 (unless (or system-flag
6bdd9204 591 (and (boundp sym)
e047f448
SM
592 ;; load-file-name
593 (equal (symbol-value sym) expansion)
594 (equal (symbol-function sym) hook)))
595 (setq abbrevs-changed t))
596 (set sym expansion)
597 (fset sym hook)
79415279
SM
598 (setplist sym
599 ;; Don't store the `force' value of `system-flag' into
600 ;; the :system property.
601 (if (eq 'force system-flag) (plist-put props :system t) props))
e047f448
SM
602 (abbrev-table-put table :abbrev-table-modiff
603 (1+ (abbrev-table-get table :abbrev-table-modiff))))
604 name))
605
606(defun abbrev--check-chars (abbrev global)
607 "Check if the characters in ABBREV have word syntax in either the
608current (if global is nil) or standard syntax table."
609 (with-syntax-table
1f1e06e2 610 (cond ((null global) (syntax-table))
e047f448 611 ;; ((syntax-table-p global) global)
1f1e06e2 612 (t (standard-syntax-table)))
e047f448
SM
613 (when (string-match "\\W" abbrev)
614 (let ((badchars ())
615 (pos 0))
616 (while (string-match "\\W" abbrev pos)
f58e0fd5 617 (cl-pushnew (aref abbrev (match-beginning 0)) badchars)
e047f448
SM
618 (setq pos (1+ pos)))
619 (error "Some abbrev characters (%s) are not word constituents %s"
620 (apply 'string (nreverse badchars))
621 (if global "in the standard syntax" "in this mode"))))))
622
623(defun define-global-abbrev (abbrev expansion)
624 "Define ABBREV as a global abbreviation for EXPANSION.
625The characters in ABBREV must all be word constituents in the standard
626syntax table."
627 (interactive "sDefine global abbrev: \nsExpansion for %s: ")
628 (abbrev--check-chars abbrev 'global)
629 (define-abbrev global-abbrev-table (downcase abbrev) expansion))
630
631(defun define-mode-abbrev (abbrev expansion)
632 "Define ABBREV as a mode-specific abbreviation for EXPANSION.
633The characters in ABBREV must all be word-constituents in the current mode."
634 (interactive "sDefine mode abbrev: \nsExpansion for %s: ")
635 (unless local-abbrev-table
636 (error "Major mode has no abbrev table"))
637 (abbrev--check-chars abbrev nil)
638 (define-abbrev local-abbrev-table (downcase abbrev) expansion))
639
640(defun abbrev--active-tables (&optional tables)
641 "Return the list of abbrev tables currently active.
642TABLES if non-nil overrides the usual rules. It can hold
643either a single abbrev table or a list of abbrev tables."
644 ;; We could just remove the `tables' arg and let callers use
645 ;; (or table (abbrev--active-tables)) but then they'd have to be careful
646 ;; to treat the distinction between a single table and a list of tables.
647 (cond
648 ((consp tables) tables)
649 ((vectorp tables) (list tables))
650 (t
651 (let ((tables (if (listp local-abbrev-table)
652 (append local-abbrev-table
653 (list global-abbrev-table))
654 (list local-abbrev-table global-abbrev-table))))
655 ;; Add the minor-mode abbrev tables.
656 (dolist (x abbrev-minor-mode-table-alist)
657 (when (and (symbolp (car x)) (boundp (car x)) (symbol-value (car x)))
658 (setq tables
659 (if (listp (cdr x))
660 (append (cdr x) tables) (cons (cdr x) tables)))))
661 tables))))
f57a9512 662
e047f448 663
351edece
SM
664(defun abbrev--symbol (abbrev table)
665 "Return the symbol representing abbrev named ABBREV in TABLE.
666This symbol's name is ABBREV, but it is not the canonical symbol of that name;
667it is interned in the abbrev-table TABLE rather than the normal obarray.
668The value is nil if that abbrev is not defined."
669 (let* ((case-fold (not (abbrev-table-get table :case-fixed)))
670 ;; In case the table doesn't set :case-fixed but some of the
671 ;; abbrevs do, we have to be careful.
672 (sym
673 ;; First try without case-folding.
674 (or (intern-soft abbrev table)
675 (when case-fold
676 ;; We didn't find any abbrev, try case-folding.
677 (let ((sym (intern-soft (downcase abbrev) table)))
678 ;; Only use it if it doesn't require :case-fixed.
679 (and sym (not (abbrev-get sym :case-fixed))
680 sym))))))
681 (if (symbol-value sym)
682 sym)))
683
e047f448
SM
684(defun abbrev-symbol (abbrev &optional table)
685 "Return the symbol representing abbrev named ABBREV.
686This symbol's name is ABBREV, but it is not the canonical symbol of that name;
687it is interned in an abbrev-table rather than the normal obarray.
688The value is nil if that abbrev is not defined.
689Optional second arg TABLE is abbrev table to look it up in.
690The default is to try buffer's mode-specific abbrev table, then global table."
691 (let ((tables (abbrev--active-tables table))
692 sym)
351edece
SM
693 (while (and tables (not sym))
694 (let* ((table (pop tables)))
e047f448 695 (setq tables (append (abbrev-table-get table :parents) tables))
351edece
SM
696 (setq sym (abbrev--symbol abbrev table))))
697 sym))
f57a9512 698
e047f448
SM
699
700(defun abbrev-expansion (abbrev &optional table)
701 "Return the string that ABBREV expands into in the current buffer.
702Optionally specify an abbrev table as second arg;
703then ABBREV is looked up in that table only."
704 (symbol-value (abbrev-symbol abbrev table)))
705
706
707(defun abbrev--before-point ()
708 "Try and find an abbrev before point. Return it if found, nil otherwise."
709 (unless (eq abbrev-start-location-buffer (current-buffer))
710 (setq abbrev-start-location nil))
711
712 (let ((tables (abbrev--active-tables))
713 (pos (point))
714 start end name res)
715
716 (if abbrev-start-location
717 (progn
718 (setq start abbrev-start-location)
719 (setq abbrev-start-location nil)
720 ;; Remove the hyphen inserted by `abbrev-prefix-mark'.
721 (if (and (< start (point-max))
722 (eq (char-after start) ?-))
723 (delete-region start (1+ start)))
724 (skip-syntax-backward " ")
725 (setq end (point))
2b86bfb1
SM
726 (when (> end start)
727 (setq name (buffer-substring start end))
728 (goto-char pos) ; Restore point.
729 (list (abbrev-symbol name tables) name start end)))
f57a9512 730
e047f448
SM
731 (while (and tables (not (car res)))
732 (let* ((table (pop tables))
733 (enable-fun (abbrev-table-get table :enable-function)))
734 (setq tables (append (abbrev-table-get table :parents) tables))
735 (setq res
736 (and (or (not enable-fun) (funcall enable-fun))
fe4346f0
SM
737 (let ((re (abbrev-table-get table :regexp)))
738 (if (null re)
739 ;; We used to default `re' to "\\<\\(\\w+\\)\\W*"
740 ;; but when words-include-escapes is set, that
741 ;; is not right and fixing it is boring.
742 (let ((lim (point)))
743 (backward-word 1)
744 (setq start (point))
745 (forward-word 1)
746 (setq end (min (point) lim)))
747 (when (looking-back re (line-beginning-position))
748 (setq start (match-beginning 1))
749 (setq end (match-end 1)))))
79415279 750 (setq name (buffer-substring start end))
351edece 751 (let ((abbrev (abbrev--symbol name table)))
79415279
SM
752 (when abbrev
753 (setq enable-fun (abbrev-get abbrev :enable-function))
754 (and (or (not enable-fun) (funcall enable-fun))
755 ;; This will also look it up in parent tables.
756 ;; This is not on purpose, but it seems harmless.
757 (list abbrev name start end))))))
e047f448
SM
758 ;; Restore point.
759 (goto-char pos)))
760 res)))
761
a3709a8c
SM
762(defun abbrev-insert (abbrev &optional name wordstart wordend)
763 "Insert abbrev ABBREV at point.
764If non-nil, NAME is the name by which this abbrev was found.
765If non-nil, WORDSTART is the place where to insert the abbrev.
78e4a31a 766If WORDEND is non-nil, the abbrev replaces the previous text between
a3709a8c 767WORDSTART and WORDEND.
ea16568d
GM
768Return ABBREV if the expansion should be considered as having taken place.
769The return value can be influenced by a `no-self-insert' property;
770see `define-abbrev' for details."
a3709a8c
SM
771 (unless name (setq name (symbol-name abbrev)))
772 (unless wordstart (setq wordstart (point)))
773 (unless wordend (setq wordend wordstart))
774 ;; Increment use count.
775 (abbrev-put abbrev :count (1+ (abbrev-get abbrev :count)))
776 (let ((value abbrev))
777 ;; If this abbrev has an expansion, delete the abbrev
778 ;; and insert the expansion.
779 (when (stringp (symbol-value abbrev))
780 (goto-char wordstart)
781 ;; Insert at beginning so that markers at the end (e.g. point)
782 ;; are preserved.
783 (insert (symbol-value abbrev))
784 (delete-char (- wordend wordstart))
785 (let ((case-fold-search nil))
786 ;; If the abbrev's name is different from the buffer text (the
787 ;; only difference should be capitalization), then we may want
788 ;; to adjust the capitalization of the expansion.
789 (when (and (not (equal name (symbol-name abbrev)))
790 (string-match "[[:upper:]]" name))
791 (if (not (string-match "[[:lower:]]" name))
792 ;; Abbrev was all caps. If expansion is multiple words,
793 ;; normally capitalize each word.
794 (if (and (not abbrev-all-caps)
795 (save-excursion
796 (> (progn (backward-word 1) (point))
797 (progn (goto-char wordstart)
798 (forward-word 1) (point)))))
799 (upcase-initials-region wordstart (point))
800 (upcase-region wordstart (point)))
801 ;; Abbrev included some caps. Cap first initial of expansion.
802 (let ((end (point)))
803 ;; Find the initial.
804 (goto-char wordstart)
805 (skip-syntax-forward "^w" (1- end))
806 ;; Change just that.
807 (upcase-initials-region (point) (1+ (point)))
808 (goto-char end))))))
809 ;; Now point is at the end of the expansion and the beginning is
810 ;; in last-abbrev-location.
811 (when (symbol-function abbrev)
812 (let* ((hook (symbol-function abbrev))
813 (expanded
814 ;; If the abbrev has a hook function, run it.
815 (funcall hook)))
816 ;; In addition, if the hook function is a symbol with
817 ;; a non-nil `no-self-insert' property, let the value it
818 ;; returned specify whether we consider that an expansion took
819 ;; place. If it returns nil, no expansion has been done.
820 (if (and (symbolp hook)
821 (null expanded)
822 (get hook 'no-self-insert))
823 (setq value nil))))
824 value))
825
e047f448 826(defvar abbrev-expand-functions nil
e38a5ebe 827 "Wrapper hook around `abbrev--default-expand'.")
d36ed1c8
SM
828(make-obsolete-variable 'abbrev-expand-functions 'abbrev-expand-function "24.4")
829
830(defvar abbrev-expand-function #'abbrev--default-expand
e38a5ebe 831 "Function that `expand-abbrev' uses to perform abbrev expansion.
d36ed1c8 832Takes no argument and should return the abbrev symbol if expansion took place.")
e047f448
SM
833
834(defun expand-abbrev ()
835 "Expand the abbrev before point, if there is an abbrev there.
836Effective when explicitly called even when `abbrev-mode' is nil.
e38a5ebe
GM
837Before doing anything else, runs `pre-abbrev-expand-hook'.
838Calls `abbrev-expand-function' with no argument to do the work,
839and returns whatever it does. (This should be the abbrev symbol
840if expansion occurred, else nil.)"
e047f448
SM
841 (interactive)
842 (run-hooks 'pre-abbrev-expand-hook)
d36ed1c8
SM
843 (funcall abbrev-expand-function))
844
845(defun abbrev--default-expand ()
e38a5ebe
GM
846 "Default function to use for `abbrev-expand-function'.
847This respects the wrapper hook `abbrev-expand-functions'.
848Calls `abbrev-insert' to insert any expansion, and returns what it does."
a185548b 849 (with-wrapper-hook abbrev-expand-functions ()
f58e0fd5 850 (pcase-let ((`(,sym ,name ,wordstart ,wordend) (abbrev--before-point)))
e047f448 851 (when sym
856b2f11
SM
852 (let ((startpos (copy-marker (point) t))
853 (endmark (copy-marker wordend t)))
854 (unless (or ;; executing-kbd-macro
855 noninteractive
290d5b58 856 (window-minibuffer-p))
856b2f11
SM
857 ;; Add an undo boundary, in case we are doing this for
858 ;; a self-inserting command which has avoided making one so far.
859 (undo-boundary))
860 ;; Now sym is the abbrev symbol.
861 (setq last-abbrev-text name)
862 (setq last-abbrev sym)
863 (setq last-abbrev-location wordstart)
864 ;; If this abbrev has an expansion, delete the abbrev
865 ;; and insert the expansion.
866 (prog1
867 (abbrev-insert sym name wordstart wordend)
868 ;; Yuck!! If expand-abbrev is called with point slightly
869 ;; further than the end of the abbrev, move point back to
870 ;; where it started.
871 (if (and (> startpos endmark)
872 (= (point) endmark)) ;Obey skeletons that move point.
873 (goto-char startpos))))))))
e047f448
SM
874
875(defun unexpand-abbrev ()
876 "Undo the expansion of the last abbrev that expanded.
877This differs from ordinary undo in that other editing done since then
878is not undone."
879 (interactive)
880 (save-excursion
881 (unless (or (< last-abbrev-location (point-min))
882 (> last-abbrev-location (point-max)))
883 (goto-char last-abbrev-location)
884 (when (stringp last-abbrev-text)
885 ;; This isn't correct if last-abbrev's hook was used
886 ;; to do the expansion.
887 (let ((val (symbol-value last-abbrev)))
888 (unless (stringp val)
f68cfe84 889 (error "Value of abbrev-symbol must be a string"))
e047f448
SM
890 ;; Don't inherit properties here; just copy from old contents.
891 (insert last-abbrev-text)
f68cfe84
SM
892 ;; Delete after inserting, to better preserve markers.
893 (delete-region (point) (+ (point) (length val)))
e047f448
SM
894 (setq last-abbrev-text nil))))))
895
896(defun abbrev--write (sym)
897 "Write the abbrev in a `read'able form.
898Only writes the non-system abbrevs.
899Presumes that `standard-output' points to `current-buffer'."
79415279 900 (unless (or (null (symbol-value sym)) (abbrev-get sym :system))
e047f448 901 (insert " (")
d548715c 902 (prin1 (symbol-name sym))
e047f448
SM
903 (insert " ")
904 (prin1 (symbol-value sym))
905 (insert " ")
906 (prin1 (symbol-function sym))
907 (insert " ")
79415279 908 (prin1 (abbrev-get sym :count))
e047f448
SM
909 (insert ")\n")))
910
911(defun abbrev--describe (sym)
912 (when (symbol-value sym)
913 (prin1 (symbol-name sym))
79415279 914 (if (null (abbrev-get sym :system))
e047f448
SM
915 (indent-to 15 1)
916 (insert " (sys)")
917 (indent-to 20 1))
79415279 918 (prin1 (abbrev-get sym :count))
e047f448
SM
919 (indent-to 20 1)
920 (prin1 (symbol-value sym))
921 (when (symbol-function sym)
922 (indent-to 45 1)
923 (prin1 (symbol-function sym)))
924 (terpri)))
925
926(defun insert-abbrev-table-description (name &optional readable)
927 "Insert before point a full description of abbrev table named NAME.
928NAME is a symbol whose value is an abbrev table.
929If optional 2nd arg READABLE is non-nil, a human-readable description
930is inserted. Otherwise the description is an expression,
931a call to `define-abbrev-table', which would
932define the abbrev table NAME exactly as it is currently defined.
933
934Abbrevs marked as \"system abbrevs\" are omitted."
935 (let ((table (symbol-value name))
936 (symbols ()))
937 (mapatoms (lambda (sym) (if (symbol-value sym) (push sym symbols))) table)
938 (setq symbols (sort symbols 'string-lessp))
939 (let ((standard-output (current-buffer)))
940 (if readable
941 (progn
942 (insert "(")
943 (prin1 name)
944 (insert ")\n\n")
945 (mapc 'abbrev--describe symbols)
946 (insert "\n\n"))
947 (insert "(define-abbrev-table '")
948 (prin1 name)
42da39c1
CY
949 (if (null symbols)
950 (insert " '())\n\n")
951 (insert "\n '(\n")
952 (mapc 'abbrev--write symbols)
953 (insert " ))\n\n")))
e047f448
SM
954 nil)))
955
e1ca6a5b 956(put 'define-abbrev-table 'doc-string-elt 3)
e047f448
SM
957(defun define-abbrev-table (tablename definitions
958 &optional docstring &rest props)
959 "Define TABLENAME (a symbol) as an abbrev table name.
960Define abbrevs in it according to DEFINITIONS, which is a list of elements
da59e7b2 961of the form (ABBREVNAME EXPANSION ...) that are passed to `define-abbrev'.
e047f448
SM
962PROPS is a property list to apply to the table.
963Properties with special meaning:
964- `:parents' contains a list of abbrev tables from which this table inherits
965 abbreviations.
966- `:case-fixed' non-nil means that abbreviations are looked up without
967 case-folding, and the expansion is not capitalized/upcased.
ea16568d
GM
968- `:regexp' is a regular expression that specifies how to extract the
969 name of the abbrev before point. The submatch 1 is treated
970 as the potential name of an abbrev. If :regexp is nil, the default
971 behavior uses `backward-word' and `forward-word' to extract the name
972 of the abbrev, which can therefore only be a single word.
e047f448 973- `:enable-function' can be set to a function of no argument which returns
d239a055
JB
974 non-nil if and only if the abbrevs in this table should be used for this
975 instance of `expand-abbrev'."
4eebd7fe
SM
976 ;; We used to manually add the docstring, but we also want to record this
977 ;; location as the definition of the variable (in load-history), so we may
978 ;; as well just use `defvar'.
979 (eval `(defvar ,tablename nil ,@(if (stringp docstring) (list docstring))))
e047f448
SM
980 (let ((table (if (boundp tablename) (symbol-value tablename))))
981 (unless table
9e2a4d4d 982 (setq table (make-abbrev-table))
e047f448 983 (set tablename table)
2b75be67
SM
984 (unless (memq tablename abbrev-table-name-list)
985 (push tablename abbrev-table-name-list)))
9e2a4d4d
SM
986 ;; We used to just pass them to `make-abbrev-table', but that fails
987 ;; if the table was pre-existing as is the case if it was created by
988 ;; loading the user's abbrev file.
989 (while (consp props)
990 (abbrev-table-put table (pop props) (pop props)))
e047f448
SM
991 (dolist (elt definitions)
992 (apply 'define-abbrev table elt))))
993
a3709a8c
SM
994(defun abbrev-table-menu (table &optional prompt sortfun)
995 "Return a menu that shows all abbrevs in TABLE.
996Selecting an entry runs `abbrev-insert'.
997PROMPT is the prompt to use for the keymap.
998SORTFUN is passed to `sort' to change the default ordering."
999 (unless sortfun (setq sortfun 'string-lessp))
1000 (let ((entries ()))
1001 (mapatoms (lambda (abbrev)
1002 (when (symbol-value abbrev)
1003 (let ((name (symbol-name abbrev)))
1004 (push `(,(intern name) menu-item ,name
1005 (lambda () (interactive)
1006 (abbrev-insert ',abbrev)))
1007 entries))))
1008 table)
1009 (nconc (make-sparse-keymap prompt)
1010 (sort entries (lambda (x y)
1011 (funcall sortfun (nth 2 x) (nth 2 y)))))))
1012
86cf7329
SM
1013;; Keep it after define-abbrev-table, since define-derived-mode uses
1014;; define-abbrev-table.
41a4a82f 1015(define-derived-mode edit-abbrevs-mode fundamental-mode "Edit-Abbrevs"
86cf7329
SM
1016 "Major mode for editing the list of abbrev definitions.")
1017
c06d4c1f
RS
1018(provide 'abbrev)
1019
c0274f38 1020;;; abbrev.el ends here