1 ;;; abbrev.el --- abbrev mode commands for Emacs
3 ;; Copyright (C) 1985, 1986, 1987, 1992 Free Software Foundation, Inc.
5 ;; This file is part of GNU Emacs.
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 2, or (at your option)
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.
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.
23 (defconst only-global-abbrevs nil
"\
24 *t means user plans to use global abbrevs only.
25 Makes the commands to define mode-specific abbrevs define global ones instead.")
27 (defun abbrev-mode (arg)
29 With argument ARG, turn abbrev mode on iff ARG is positive.
30 In abbrev mode, inserting an abbreviation causes it to expand
31 and be replaced by its expansion."
34 (if (null arg
) (not abbrev-mode
)
35 (> (prefix-numeric-value arg
) 0)))
36 (set-buffer-modified-p (buffer-modified-p))) ;No-op, but updates mode line.
38 (defvar edit-abbrevs-map nil
39 "Keymap used in edit-abbrevs.")
42 (setq edit-abbrevs-map
(make-sparse-keymap))
43 (define-key edit-abbrevs-map
"\C-x\C-s" 'edit-abbrevs-redefine
)
44 (define-key edit-abbrevs-map
"\C-c\C-c" 'edit-abbrevs-redefine
))
46 (defun kill-all-abbrevs ()
47 "Undefine all defined abbrevs."
49 (let ((tables abbrev-table-name-list
))
51 (clear-abbrev-table (symbol-value (car tables
)))
52 (setq tables
(cdr tables
)))))
54 (defun insert-abbrevs ()
55 "Insert after point a description of all defined abbrevs.
56 Mark is set after the inserted text."
60 (let ((tables abbrev-table-name-list
))
62 (insert-abbrev-table-description (car tables
) t
)
63 (setq tables
(cdr tables
))))
66 (defun list-abbrevs ()
67 "Display a list of all defined abbrevs."
69 (display-buffer (prepare-abbrev-list-buffer)))
71 (defun prepare-abbrev-list-buffer ()
73 (set-buffer (get-buffer-create "*Abbrevs*"))
75 (let ((tables abbrev-table-name-list
))
77 (insert-abbrev-table-description (car tables
) t
)
78 (setq tables
(cdr tables
))))
79 (goto-char (point-min))
80 (set-buffer-modified-p nil
)
82 (get-buffer-create "*Abbrevs*"))
84 (defun edit-abbrevs-mode ()
85 "Major mode for editing the list of abbrev definitions.
88 (setq major-mode
'edit-abbrevs-mode
)
89 (setq mode-name
"Edit-Abbrevs")
90 (use-local-map edit-abbrevs-map
))
92 (defun edit-abbrevs ()
93 "Alter abbrev definitions by editing a list of them.
94 Selects a buffer containing a list of abbrev definitions.
95 You can edit them and type \\<edit-abbrevs-map>\\[edit-abbrevs-redefine] to redefine abbrevs
96 according to your editing.
97 Buffer contains a header line for each abbrev table,
98 which is the abbrev table name in parentheses.
99 This is followed by one line per abbrev in that table:
100 NAME USECOUNT EXPANSION HOOK
101 where NAME and EXPANSION are strings with quotes,
102 USECOUNT is an integer, and HOOK is any valid function
103 or may be omitted (it is usually omitted)."
105 (switch-to-buffer (prepare-abbrev-list-buffer)))
107 (defun edit-abbrevs-redefine ()
108 "Redefine abbrevs according to current buffer contents."
111 (set-buffer-modified-p nil
))
113 (defun define-abbrevs (&optional arg
)
114 "Define abbrevs according to current visible buffer contents.
115 See documentation of `edit-abbrevs' for info on the format of the
116 text you must have in the buffer.
117 With argument, eliminate all abbrev definitions except
118 the ones defined from the buffer now."
120 (if arg
(kill-all-abbrevs))
122 (goto-char (point-min))
123 (while (and (not (eobp)) (re-search-forward "^(" nil t
))
124 (let* ((buf (current-buffer))
126 abbrevs name hook exp count
)
128 (while (progn (forward-line 1)
130 (setq name
(read buf
) count
(read buf
) exp
(read buf
))
131 (skip-chars-backward " \t\n\f")
132 (setq hook
(if (not (eolp)) (read buf
)))
133 (skip-chars-backward " \t\n\f")
134 (setq abbrevs
(cons (list name exp hook count
) abbrevs
)))
135 (define-abbrev-table table abbrevs
)))))
137 (defun read-abbrev-file (&optional file quietly
)
138 "Read abbrev definitions from file written with `write-abbrev-file'.
139 Optional argument FILE is the name of the file to read;
140 it defaults to the value of `abbrev-file-name'.
141 Optional second argument QUIETLY non-nil means don't print anything."
142 (interactive "fRead abbrev file: ")
143 (load (if (and file
(> (length file
) 0)) file abbrev-file-name
)
145 (setq save-abbrevs t abbrevs-changed nil
))
147 (defun quietly-read-abbrev-file (&optional file
)
148 "Read abbrev definitions from file written with write-abbrev-file.
149 Optional argument FILE is the name of the file to read;
150 it defaults to the value of `abbrev-file-name'.
151 Does not print anything."
152 ;(interactive "fRead abbrev file: ")
153 (read-abbrev-file file t
))
155 (defun write-abbrev-file (file)
156 "Write all abbrev definitions to a file of Lisp code.
157 The file written can be loaded in another session to define the same abbrevs.
158 The argument FILE is the file name to write."
161 (read-file-name "Write abbrev file: "
162 (file-name-directory (expand-file-name abbrev-file-name
))
164 (or (and file
(> (length file
) 0))
165 (setq file abbrev-file-name
))
167 (set-buffer (get-buffer-create " write-abbrev-file"))
169 (let ((tables abbrev-table-name-list
))
171 (insert-abbrev-table-description (car tables
) nil
)
172 (setq tables
(cdr tables
))))
173 (write-region 1 (point-max) file
)
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.
183 Don't use this function in a Lisp program; use `define-abbrev' instead."
186 (if only-global-abbrevs
188 (or local-abbrev-table
189 (error "No per-mode abbrev table")))
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.
199 Don't use this function in a Lisp program; use `define-abbrev' instead."
201 (add-abbrev global-abbrev-table
"Global" arg
))
203 (defun add-abbrev (table type arg
)
204 (let ((exp (and (>= arg
0)
208 (save-excursion (forward-word (- arg
)) (point))))))
211 (read-string (format (if exp
"%s abbrev for \"%s\": "
212 "Undefine %s abbrev: ")
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
))))
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."
227 (if only-global-abbrevs
229 (or local-abbrev-table
230 (error "No per-mode abbrev table")))
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."
239 (inverse-add-abbrev global-abbrev-table
"Global" arg
))
241 (defun inverse-add-abbrev (table type arg
)
242 (let (name nameloc exp
)
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\": "
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
))))
253 (define-abbrev table
(downcase name
) exp
)
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."
264 (or arg
(expand-abbrev))
265 (setq abbrev-start-location
(point-marker)
266 abbrev-start-location-buffer
(current-buffer))
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."
277 (let ((lim (- (point-max) end
))
279 (while (and (not (eobp))
280 (progn (forward-word 1)
281 (<= (setq pnt
(point)) (- (point-max) lim
))))
282 (if (abbrev-expansion
285 (save-excursion (forward-word -
1) (point))
287 (if (or noquery
(y-or-n-p (format "Expand `%s'? " string
)))
288 (expand-abbrev)))))))
290 ;;; abbrev.el ends here