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