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