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