(Coding Conventions): Explain when the package's
[bpt/emacs.git] / lisp / abbrev.el
CommitLineData
c0274f38 1;;; abbrev.el --- abbrev mode commands for Emacs
e2fbf49d 2
0d30b337 3;; Copyright (C) 1985, 1986, 1987, 1992, 2002, 2003, 2004,
aaef169d 4;; 2005, 2006 Free Software Foundation, Inc.
e2fbf49d 5
6228c05b 6;; Maintainer: FSF
f5f727f8 7;; Keywords: abbrev convenience
e9571d2a 8
e2fbf49d
JB
9;; This file is part of GNU Emacs.
10
11;; GNU Emacs is free software; you can redistribute it and/or modify
12;; it under the terms of the GNU General Public License as published by
e5167999 13;; the Free Software Foundation; either version 2, or (at your option)
e2fbf49d
JB
14;; any later version.
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
b578f267 22;; along with GNU Emacs; see the file COPYING. If not, write to the
086add15
LK
23;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24;; Boston, MA 02110-1301, USA.
e2fbf49d 25
e41b2db1
ER
26;;; Commentary:
27
28;; This facility is documented in the Emacs Manual.
29
e5167999 30;;; Code:
e2fbf49d 31
7cfedc97 32(defcustom only-global-abbrevs nil
e7fdaf63 33 "Non-nil means user plans to use global abbrevs only.
1d1e35a0
RS
34This makes the commands that normally define mode-specific abbrevs
35define global abbrevs instead."
36 :type 'boolean
f5f727f8
DN
37 :group 'abbrev-mode
38 :group 'convenience)
e2fbf49d 39
68d9550f 40(defun abbrev-mode (&optional arg)
38002bff 41 "Toggle Abbrev mode in the current buffer.
a864e33e 42With argument ARG, turn abbrev mode on iff ARG is positive.
38002bff 43In Abbrev mode, inserting an abbreviation causes it to expand
e2fbf49d
JB
44and be replaced by its expansion."
45 (interactive "P")
46 (setq abbrev-mode
47 (if (null arg) (not abbrev-mode)
48 (> (prefix-numeric-value arg) 0)))
e47df26a 49 (force-mode-line-update))
1d1e35a0
RS
50
51(defcustom abbrev-mode nil
38002bff 52 "Enable or disable Abbrev mode.
5cf1c7ac
RS
53Non-nil means automatically expand abbrevs as they are inserted.
54
38002bff 55Setting this variable with `setq' changes it for the current buffer.
5cf1c7ac 56Changing it with \\[customize] sets the default value.
38002bff
RS
57Interactively, use the command `abbrev-mode'
58to enable or disable Abbrev mode in the current buffer."
1d1e35a0
RS
59 :type 'boolean
60 :group 'abbrev-mode)
6c07f4d4 61;;;###autoload(put 'abbrev-mode 'safe-local-variable t)
1d1e35a0 62
e2fbf49d 63\f
e7fdaf63
JPW
64(defvar edit-abbrevs-map
65 (let ((map (make-sparse-keymap)))
66 (define-key map "\C-x\C-s" 'edit-abbrevs-redefine)
67 (define-key map "\C-c\C-c" 'edit-abbrevs-redefine)
68 map)
38002bff 69 "Keymap used in `edit-abbrevs'.")
e2fbf49d
JB
70
71(defun kill-all-abbrevs ()
72 "Undefine all defined abbrevs."
73 (interactive)
74 (let ((tables abbrev-table-name-list))
75 (while tables
76 (clear-abbrev-table (symbol-value (car tables)))
77 (setq tables (cdr tables)))))
78
ad9d51b2
RS
79(defun copy-abbrev-table (table)
80 "Make a new abbrev-table with the same abbrevs as TABLE."
81 (let ((new-table (make-abbrev-table)))
82 (mapatoms
83 (lambda (symbol)
84 (define-abbrev new-table
85 (symbol-name symbol)
86 (symbol-value symbol)
87 (symbol-function symbol)))
88 table)
89 new-table))
90
e2fbf49d
JB
91(defun insert-abbrevs ()
92 "Insert after point a description of all defined abbrevs.
93Mark is set after the inserted text."
94 (interactive)
95 (push-mark
96 (save-excursion
71baa28f
EZ
97 (let ((tables abbrev-table-name-list))
98 (while tables
99 (insert-abbrev-table-description (car tables) t)
100 (setq tables (cdr tables))))
101 (point))))
e2fbf49d 102
c88a9944
GM
103(defun list-abbrevs (&optional local)
104 "Display a list of defined abbrevs.
105If LOCAL is non-nil, interactively when invoked with a
106prefix arg, display only local, i.e. mode-specific, abbrevs.
107Otherwise display all abbrevs."
108 (interactive "P")
109 (display-buffer (prepare-abbrev-list-buffer local)))
110
111(defun abbrev-table-name (table)
112 "Value is the name of abbrev table TABLE."
113 (let ((tables abbrev-table-name-list)
114 found)
115 (while (and (not found) tables)
116 (when (eq (symbol-value (car tables)) table)
117 (setq found (car tables)))
118 (setq tables (cdr tables)))
119 found))
7cfedc97 120
c88a9944 121(defun prepare-abbrev-list-buffer (&optional local)
e2fbf49d 122 (save-excursion
25160ec0
GM
123 (let ((table local-abbrev-table))
124 (set-buffer (get-buffer-create "*Abbrevs*"))
125 (erase-buffer)
126 (if local
127 (insert-abbrev-table-description (abbrev-table-name table) t)
128 (dolist (table abbrev-table-name-list)
129 (insert-abbrev-table-description table t)))
130 (goto-char (point-min))
131 (set-buffer-modified-p nil)
132 (edit-abbrevs-mode)
133 (current-buffer))))
e2fbf49d
JB
134
135(defun edit-abbrevs-mode ()
136 "Major mode for editing the list of abbrev definitions.
137\\{edit-abbrevs-map}"
138 (interactive)
1a96d1f3 139 (kill-all-local-variables)
e2fbf49d
JB
140 (setq major-mode 'edit-abbrevs-mode)
141 (setq mode-name "Edit-Abbrevs")
1a96d1f3
LK
142 (use-local-map edit-abbrevs-map)
143 (run-mode-hooks 'edit-abbrevs-mode-hook))
e2fbf49d
JB
144
145(defun edit-abbrevs ()
146 "Alter abbrev definitions by editing a list of them.
147Selects a buffer containing a list of abbrev definitions.
148You can edit them and type \\<edit-abbrevs-map>\\[edit-abbrevs-redefine] to redefine abbrevs
149according to your editing.
150Buffer contains a header line for each abbrev table,
151 which is the abbrev table name in parentheses.
152This is followed by one line per abbrev in that table:
153NAME USECOUNT EXPANSION HOOK
154where NAME and EXPANSION are strings with quotes,
155USECOUNT is an integer, and HOOK is any valid function
156or 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)
163 (define-abbrevs t)
164 (set-buffer-modified-p nil))
165
166(defun define-abbrevs (&optional arg)
167 "Define abbrevs according to current visible buffer contents.
168See documentation of `edit-abbrevs' for info on the format of the
169text you must have in the buffer.
170With argument, eliminate all abbrev definitions except
171the ones defined from the buffer now."
172 (interactive "P")
173 (if arg (kill-all-abbrevs))
174 (save-excursion
71baa28f
EZ
175 (goto-char (point-min))
176 (while (and (not (eobp)) (re-search-forward "^(" nil t))
177 (let* ((buf (current-buffer))
178 (table (read buf))
179 abbrevs name hook exp count sys)
180 (forward-line 1)
181 (while (progn (forward-line 1)
182 (not (eolp)))
183 (setq name (read buf) count (read buf))
184 (if (equal count '(sys))
185 (setq sys t count (read buf)))
186 (setq exp (read buf))
187 (skip-chars-backward " \t\n\f")
188 (setq hook (if (not (eolp)) (read buf)))
189 (skip-chars-backward " \t\n\f")
190 (setq abbrevs (cons (list name exp hook count sys) abbrevs)))
191 (define-abbrev-table table abbrevs)))))
e2fbf49d
JB
192
193(defun read-abbrev-file (&optional file quietly)
194 "Read abbrev definitions from file written with `write-abbrev-file'.
195Optional argument FILE is the name of the file to read;
196it defaults to the value of `abbrev-file-name'.
d0b6d945 197Optional second argument QUIETLY non-nil means don't display a message."
e2fbf49d
JB
198 (interactive "fRead abbrev file: ")
199 (load (if (and file (> (length file) 0)) file abbrev-file-name)
200 nil quietly)
d0b6d945 201 (setq abbrevs-changed nil))
e2fbf49d
JB
202
203(defun quietly-read-abbrev-file (&optional file)
e7fdaf63 204 "Read abbrev definitions from file written with `write-abbrev-file'.
e2fbf49d
JB
205Optional argument FILE is the name of the file to read;
206it defaults to the value of `abbrev-file-name'.
d0b6d945 207Does not display any message."
71baa28f 208 ;(interactive "fRead abbrev file: ")
e2fbf49d
JB
209 (read-abbrev-file file t))
210
68063965
LT
211(defun write-abbrev-file (&optional file)
212 "Write all user-level abbrev definitions to a file of Lisp code.
213This does not include system abbrevs; it includes only the abbrev tables
214listed in listed in `abbrev-table-name-list'.
e2fbf49d 215The file written can be loaded in another session to define the same abbrevs.
68063965
LT
216The argument FILE is the file name to write. If omitted or nil, the file
217specified in `abbrev-file-name' is used."
e2fbf49d
JB
218 (interactive
219 (list
220 (read-file-name "Write abbrev file: "
221 (file-name-directory (expand-file-name abbrev-file-name))
222 abbrev-file-name)))
e2fbf49d
JB
223 (or (and file (> (length file) 0))
224 (setq file abbrev-file-name))
a166f623
DL
225 (let ((coding-system-for-write 'emacs-mule))
226 (with-temp-file file
227 (insert ";;-*-coding: emacs-mule;-*-\n")
71baa28f
EZ
228 (dolist (table
229 ;; We sort the table in order to ease the automatic
230 ;; merging of different versions of the user's abbrevs
231 ;; file. This is useful, for example, for when the
232 ;; user keeps their home directory in a revision
233 ;; control system, and is therefore keeping multiple
234 ;; slightly-differing copies loosely synchronized.
235 (sort (copy-sequence abbrev-table-name-list)
236 (lambda (s1 s2)
237 (string< (symbol-name s1)
238 (symbol-name s2)))))
a166f623 239 (insert-abbrev-table-description table nil)))))
e2fbf49d
JB
240\f
241(defun add-mode-abbrev (arg)
242 "Define mode-specific abbrev for last word(s) before point.
243Argument is how many words before point form the expansion;
244or zero means the region is the expansion.
245A negative argument means to undefine the specified abbrev.
246Reads the abbreviation in the minibuffer.
247
248Don't use this function in a Lisp program; use `define-abbrev' instead."
249 (interactive "p")
250 (add-abbrev
251 (if only-global-abbrevs
71296446 252 global-abbrev-table
e2fbf49d
JB
253 (or local-abbrev-table
254 (error "No per-mode abbrev table")))
255 "Mode" arg))
256
257(defun add-global-abbrev (arg)
258 "Define global (all modes) abbrev for last word(s) before point.
259The prefix argument specifies the number of words before point that form the
260expansion; or zero means the region is the expansion.
261A negative argument means to undefine the specified abbrev.
262This command uses the minibuffer to read the abbreviation.
263
264Don't use this function in a Lisp program; use `define-abbrev' instead."
265 (interactive "p")
266 (add-abbrev global-abbrev-table "Global" arg))
267
268(defun add-abbrev (table type arg)
269 (let ((exp (and (>= arg 0)
34f3cd03 270 (buffer-substring-no-properties
e2fbf49d
JB
271 (point)
272 (if (= arg 0) (mark)
273 (save-excursion (forward-word (- arg)) (point))))))
274 name)
275 (setq name
276 (read-string (format (if exp "%s abbrev for \"%s\": "
277 "Undefine %s abbrev: ")
278 type exp)))
a0f88464 279 (set-text-properties 0 (length name) nil name)
e2fbf49d
JB
280 (if (or (null exp)
281 (not (abbrev-expansion name table))
282 (y-or-n-p (format "%s expands to \"%s\"; redefine? "
283 name (abbrev-expansion name table))))
284 (define-abbrev table (downcase name) exp))))
7cfedc97 285
e66b273f 286(defun inverse-add-mode-abbrev (n)
e2fbf49d
JB
287 "Define last word before point as a mode-specific abbrev.
288With prefix argument N, defines the Nth word before point.
289This command uses the minibuffer to read the expansion.
290Expands the abbreviation after defining it."
291 (interactive "p")
292 (inverse-add-abbrev
293 (if only-global-abbrevs
7cfedc97 294 global-abbrev-table
e2fbf49d
JB
295 (or local-abbrev-table
296 (error "No per-mode abbrev table")))
e66b273f 297 "Mode" n))
e2fbf49d 298
e66b273f 299(defun inverse-add-global-abbrev (n)
e2fbf49d
JB
300 "Define last word before point as a global (mode-independent) abbrev.
301With prefix argument N, defines the Nth word before point.
302This command uses the minibuffer to read the expansion.
303Expands the abbreviation after defining it."
304 (interactive "p")
e66b273f 305 (inverse-add-abbrev global-abbrev-table "Global" n))
e2fbf49d
JB
306
307(defun inverse-add-abbrev (table type arg)
46684068 308 (let (name exp start end)
e2fbf49d 309 (save-excursion
46684068
GM
310 (forward-word (1+ (- arg)))
311 (setq end (point))
312 (backward-word 1)
313 (setq start (point)
314 name (buffer-substring-no-properties start end)))
315
316 (setq exp (read-string (format "%s expansion for \"%s\": " type name)
317 nil nil nil t))
318 (when (or (not (abbrev-expansion name table))
319 (y-or-n-p (format "%s expands to \"%s\"; redefine? "
320 name (abbrev-expansion name table))))
321 (define-abbrev table (downcase name) exp)
322 (save-excursion
323 (goto-char end)
324 (expand-abbrev)))))
e2fbf49d
JB
325
326(defun abbrev-prefix-mark (&optional arg)
327 "Mark current point as the beginning of an abbrev.
328Abbrev to be expanded starts here rather than at beginning of word.
329This way, you can expand an abbrev with a prefix: insert the prefix,
68063965
LT
330use this command, then insert the abbrev. This command inserts a
331temporary hyphen after the prefix \(until the intended abbrev
332expansion occurs).
333If the prefix is itself an abbrev, this command expands it, unless
334ARG is non-nil. Interactively, ARG is the prefix argument."
e2fbf49d
JB
335 (interactive "P")
336 (or arg (expand-abbrev))
337 (setq abbrev-start-location (point-marker)
338 abbrev-start-location-buffer (current-buffer))
339 (insert "-"))
340
341(defun expand-region-abbrevs (start end &optional noquery)
342 "For abbrev occurrence in the region, offer to expand it.
e66b273f
JB
343The user is asked to type `y' or `n' for each occurrence.
344A prefix argument means don't query; expand all abbrevs."
e2fbf49d
JB
345 (interactive "r\nP")
346 (save-excursion
347 (goto-char start)
348 (let ((lim (- (point-max) end))
349 pnt string)
350 (while (and (not (eobp))
351 (progn (forward-word 1)
352 (<= (setq pnt (point)) (- (point-max) lim))))
353 (if (abbrev-expansion
354 (setq string
34f3cd03 355 (buffer-substring-no-properties
e2fbf49d
JB
356 (save-excursion (forward-word -1) (point))
357 pnt)))
358 (if (or noquery (y-or-n-p (format "Expand `%s'? " string)))
359 (expand-abbrev)))))))
c0274f38 360
ab5796a9 361;;; arch-tag: dbd6f3ae-dfe3-40ba-b00f-f9e3ff960df5
c0274f38 362;;; abbrev.el ends here