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