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