Setup auto-fill-chars.
[bpt/emacs.git] / lisp / abbrev.el
CommitLineData
c0274f38 1;;; abbrev.el --- abbrev mode commands for Emacs
e2fbf49d 2
e5167999 3;; Copyright (C) 1985, 1986, 1987, 1992 Free Software Foundation, Inc.
e2fbf49d 4
f5f727f8 5;; Keywords: abbrev convenience
e9571d2a 6
e2fbf49d
JB
7;; This file is part of GNU Emacs.
8
9;; GNU Emacs is free software; you can redistribute it and/or modify
10;; it under the terms of the GNU General Public License as published by
e5167999 11;; the Free Software Foundation; either version 2, or (at your option)
e2fbf49d
JB
12;; any later version.
13
14;; GNU Emacs is distributed in the hope that it will be useful,
15;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17;; GNU General Public License for more details.
18
19;; You should have received a copy of the GNU General Public License
b578f267
EN
20;; along with GNU Emacs; see the file COPYING. If not, write to the
21;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22;; Boston, MA 02111-1307, USA.
e2fbf49d 23
e41b2db1
ER
24;;; Commentary:
25
26;; This facility is documented in the Emacs Manual.
27
e5167999 28;;; Code:
e2fbf49d 29
1d1e35a0
RS
30(defcustom only-global-abbrevs nil
31 "*t means user plans to use global abbrevs only.
32This makes the commands that normally define mode-specific abbrevs
33define global abbrevs instead."
34 :type 'boolean
f5f727f8
DN
35 :group 'abbrev-mode
36 :group 'convenience)
e2fbf49d
JB
37
38(defun abbrev-mode (arg)
39 "Toggle abbrev mode.
a864e33e 40With argument ARG, turn abbrev mode on iff ARG is positive.
e2fbf49d
JB
41In abbrev mode, inserting an abbreviation causes it to expand
42and be replaced by its expansion."
43 (interactive "P")
44 (setq abbrev-mode
45 (if (null arg) (not abbrev-mode)
46 (> (prefix-numeric-value arg) 0)))
e47df26a 47 (force-mode-line-update))
1d1e35a0
RS
48
49(defcustom abbrev-mode nil
50 "Toggle abbrev mode.
51In abbrev mode, inserting an abbreviation causes it to expand
52and be replaced by its expansion.
53You must modify via \\[customize] for this variable to have an effect."
54 :set (lambda (symbol value)
55 (abbrev-mode (if value 1 0)))
56 :type 'boolean
57 :group 'abbrev-mode)
58
e2fbf49d
JB
59\f
60(defvar edit-abbrevs-map nil
61 "Keymap used in edit-abbrevs.")
62(if edit-abbrevs-map
63 nil
64 (setq edit-abbrevs-map (make-sparse-keymap))
65 (define-key edit-abbrevs-map "\C-x\C-s" 'edit-abbrevs-redefine)
66 (define-key edit-abbrevs-map "\C-c\C-c" 'edit-abbrevs-redefine))
67
68(defun kill-all-abbrevs ()
69 "Undefine all defined abbrevs."
70 (interactive)
71 (let ((tables abbrev-table-name-list))
72 (while tables
73 (clear-abbrev-table (symbol-value (car tables)))
74 (setq tables (cdr tables)))))
75
76(defun insert-abbrevs ()
77 "Insert after point a description of all defined abbrevs.
78Mark is set after the inserted text."
79 (interactive)
80 (push-mark
81 (save-excursion
82 (let ((tables abbrev-table-name-list))
83 (while tables
84 (insert-abbrev-table-description (car tables) t)
85 (setq tables (cdr tables))))
86 (point))))
87
88(defun list-abbrevs ()
89 "Display a list of all defined abbrevs."
90 (interactive)
91 (display-buffer (prepare-abbrev-list-buffer)))
92
93(defun prepare-abbrev-list-buffer ()
94 (save-excursion
95 (set-buffer (get-buffer-create "*Abbrevs*"))
96 (erase-buffer)
97 (let ((tables abbrev-table-name-list))
98 (while tables
99 (insert-abbrev-table-description (car tables) t)
100 (setq tables (cdr tables))))
101 (goto-char (point-min))
102 (set-buffer-modified-p nil)
103 (edit-abbrevs-mode))
104 (get-buffer-create "*Abbrevs*"))
105
106(defun edit-abbrevs-mode ()
107 "Major mode for editing the list of abbrev definitions.
108\\{edit-abbrevs-map}"
109 (interactive)
110 (setq major-mode 'edit-abbrevs-mode)
111 (setq mode-name "Edit-Abbrevs")
112 (use-local-map edit-abbrevs-map))
113
114(defun edit-abbrevs ()
115 "Alter abbrev definitions by editing a list of them.
116Selects a buffer containing a list of abbrev definitions.
117You can edit them and type \\<edit-abbrevs-map>\\[edit-abbrevs-redefine] to redefine abbrevs
118according to your editing.
119Buffer contains a header line for each abbrev table,
120 which is the abbrev table name in parentheses.
121This is followed by one line per abbrev in that table:
122NAME USECOUNT EXPANSION HOOK
123where NAME and EXPANSION are strings with quotes,
124USECOUNT is an integer, and HOOK is any valid function
125or may be omitted (it is usually omitted)."
126 (interactive)
127 (switch-to-buffer (prepare-abbrev-list-buffer)))
128
129(defun edit-abbrevs-redefine ()
130 "Redefine abbrevs according to current buffer contents."
131 (interactive)
132 (define-abbrevs t)
133 (set-buffer-modified-p nil))
134
135(defun define-abbrevs (&optional arg)
136 "Define abbrevs according to current visible buffer contents.
137See documentation of `edit-abbrevs' for info on the format of the
138text you must have in the buffer.
139With argument, eliminate all abbrev definitions except
140the ones defined from the buffer now."
141 (interactive "P")
142 (if arg (kill-all-abbrevs))
143 (save-excursion
144 (goto-char (point-min))
145 (while (and (not (eobp)) (re-search-forward "^(" nil t))
146 (let* ((buf (current-buffer))
147 (table (read buf))
ba343182 148 abbrevs name hook exp count)
e2fbf49d
JB
149 (forward-line 1)
150 (while (progn (forward-line 1)
151 (not (eolp)))
152 (setq name (read buf) count (read buf) exp (read buf))
153 (skip-chars-backward " \t\n\f")
154 (setq hook (if (not (eolp)) (read buf)))
155 (skip-chars-backward " \t\n\f")
156 (setq abbrevs (cons (list name exp hook count) abbrevs)))
157 (define-abbrev-table table abbrevs)))))
158
159(defun read-abbrev-file (&optional file quietly)
160 "Read abbrev definitions from file written with `write-abbrev-file'.
161Optional argument FILE is the name of the file to read;
162it defaults to the value of `abbrev-file-name'.
163Optional second argument QUIETLY non-nil means don't print anything."
164 (interactive "fRead abbrev file: ")
165 (load (if (and file (> (length file) 0)) file abbrev-file-name)
166 nil quietly)
167 (setq save-abbrevs t abbrevs-changed nil))
168
169(defun quietly-read-abbrev-file (&optional file)
170 "Read abbrev definitions from file written with write-abbrev-file.
171Optional argument FILE is the name of the file to read;
172it defaults to the value of `abbrev-file-name'.
173Does not print anything."
174 ;(interactive "fRead abbrev file: ")
175 (read-abbrev-file file t))
176
177(defun write-abbrev-file (file)
e5167999 178 "Write all abbrev definitions to a file of Lisp code.
e2fbf49d
JB
179The file written can be loaded in another session to define the same abbrevs.
180The argument FILE is the file name to write."
181 (interactive
182 (list
183 (read-file-name "Write abbrev file: "
184 (file-name-directory (expand-file-name abbrev-file-name))
185 abbrev-file-name)))
e2fbf49d
JB
186 (or (and file (> (length file) 0))
187 (setq file abbrev-file-name))
188 (save-excursion
189 (set-buffer (get-buffer-create " write-abbrev-file"))
190 (erase-buffer)
191 (let ((tables abbrev-table-name-list))
192 (while tables
193 (insert-abbrev-table-description (car tables) nil)
194 (setq tables (cdr tables))))
195 (write-region 1 (point-max) file)
196 (erase-buffer)))
197\f
198(defun add-mode-abbrev (arg)
199 "Define mode-specific abbrev for last word(s) before point.
200Argument is how many words before point form the expansion;
201or zero means the region is the expansion.
202A negative argument means to undefine the specified abbrev.
203Reads the abbreviation in the minibuffer.
204
205Don't use this function in a Lisp program; use `define-abbrev' instead."
206 (interactive "p")
207 (add-abbrev
208 (if only-global-abbrevs
209 global-abbrev-table
210 (or local-abbrev-table
211 (error "No per-mode abbrev table")))
212 "Mode" arg))
213
214(defun add-global-abbrev (arg)
215 "Define global (all modes) abbrev for last word(s) before point.
216The prefix argument specifies the number of words before point that form the
217expansion; or zero means the region is the expansion.
218A negative argument means to undefine the specified abbrev.
219This command uses the minibuffer to read the abbreviation.
220
221Don't use this function in a Lisp program; use `define-abbrev' instead."
222 (interactive "p")
223 (add-abbrev global-abbrev-table "Global" arg))
224
225(defun add-abbrev (table type arg)
226 (let ((exp (and (>= arg 0)
227 (buffer-substring
228 (point)
229 (if (= arg 0) (mark)
230 (save-excursion (forward-word (- arg)) (point))))))
231 name)
232 (setq name
233 (read-string (format (if exp "%s abbrev for \"%s\": "
234 "Undefine %s abbrev: ")
235 type exp)))
a0f88464 236 (set-text-properties 0 (length name) nil name)
e2fbf49d
JB
237 (if (or (null exp)
238 (not (abbrev-expansion name table))
239 (y-or-n-p (format "%s expands to \"%s\"; redefine? "
240 name (abbrev-expansion name table))))
241 (define-abbrev table (downcase name) exp))))
242
243(defun inverse-add-mode-abbrev (arg)
244 "Define last word before point as a mode-specific abbrev.
245With prefix argument N, defines the Nth word before point.
246This command uses the minibuffer to read the expansion.
247Expands the abbreviation after defining it."
248 (interactive "p")
249 (inverse-add-abbrev
250 (if only-global-abbrevs
251 global-abbrev-table
252 (or local-abbrev-table
253 (error "No per-mode abbrev table")))
254 "Mode" arg))
255
256(defun inverse-add-global-abbrev (arg)
257 "Define last word before point as a global (mode-independent) abbrev.
258With prefix argument N, defines the Nth word before point.
259This command uses the minibuffer to read the expansion.
260Expands the abbreviation after defining it."
261 (interactive "p")
262 (inverse-add-abbrev global-abbrev-table "Global" arg))
263
264(defun inverse-add-abbrev (table type arg)
265 (let (name nameloc exp)
266 (save-excursion
267 (forward-word (- arg))
268 (setq name (buffer-substring (point) (progn (forward-word 1)
269 (setq nameloc (point))))))
a0f88464 270 (set-text-properties 0 (length name) nil name)
e2fbf49d
JB
271 (setq exp (read-string (format "%s expansion for \"%s\": "
272 type name)))
273 (if (or (not (abbrev-expansion name table))
274 (y-or-n-p (format "%s expands to \"%s\"; redefine? "
275 name (abbrev-expansion name table))))
276 (progn
277 (define-abbrev table (downcase name) exp)
278 (save-excursion
279 (goto-char nameloc)
280 (expand-abbrev))))))
281
282(defun abbrev-prefix-mark (&optional arg)
283 "Mark current point as the beginning of an abbrev.
284Abbrev to be expanded starts here rather than at beginning of word.
285This way, you can expand an abbrev with a prefix: insert the prefix,
286use this command, then insert the abbrev."
287 (interactive "P")
288 (or arg (expand-abbrev))
289 (setq abbrev-start-location (point-marker)
290 abbrev-start-location-buffer (current-buffer))
291 (insert "-"))
292
293(defun expand-region-abbrevs (start end &optional noquery)
294 "For abbrev occurrence in the region, offer to expand it.
295The user is asked to type y or n for each occurrence.
296A prefix argument means don't query; expand all abbrevs.
297If called from a Lisp program, arguments are START END &optional NOQUERY."
298 (interactive "r\nP")
299 (save-excursion
300 (goto-char start)
301 (let ((lim (- (point-max) end))
302 pnt string)
303 (while (and (not (eobp))
304 (progn (forward-word 1)
305 (<= (setq pnt (point)) (- (point-max) lim))))
306 (if (abbrev-expansion
307 (setq string
308 (buffer-substring
309 (save-excursion (forward-word -1) (point))
310 pnt)))
311 (if (or noquery (y-or-n-p (format "Expand `%s'? " string)))
312 (expand-abbrev)))))))
c0274f38
ER
313
314;;; abbrev.el ends here