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