1 ;;; abbrev.el --- abbrev mode commands for Emacs
3 ;; Copyright (C) 1985, 1986, 1987, 1992 Free Software Foundation, Inc.
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)
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.
25 (defconst only-global-abbrevs nil
"\
26 *t means user plans to use global abbrevs only.
27 Makes the commands to define mode-specific abbrevs define global ones instead.")
29 (defun abbrev-mode (arg)
31 With arg, turn abbrev mode on iff arg is positive.
32 In abbrev mode, inserting an abbreviation causes it to expand
33 and be replaced by its expansion."
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.
40 (defvar edit-abbrevs-map nil
41 "Keymap used in edit-abbrevs.")
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
))
48 (defun kill-all-abbrevs ()
49 "Undefine all defined abbrevs."
51 (let ((tables abbrev-table-name-list
))
53 (clear-abbrev-table (symbol-value (car tables
)))
54 (setq tables
(cdr tables
)))))
56 (defun insert-abbrevs ()
57 "Insert after point a description of all defined abbrevs.
58 Mark is set after the inserted text."
62 (let ((tables abbrev-table-name-list
))
64 (insert-abbrev-table-description (car tables
) t
)
65 (setq tables
(cdr tables
))))
68 (defun list-abbrevs ()
69 "Display a list of all defined abbrevs."
71 (display-buffer (prepare-abbrev-list-buffer)))
73 (defun prepare-abbrev-list-buffer ()
75 (set-buffer (get-buffer-create "*Abbrevs*"))
77 (let ((tables abbrev-table-name-list
))
79 (insert-abbrev-table-description (car tables
) t
)
80 (setq tables
(cdr tables
))))
81 (goto-char (point-min))
82 (set-buffer-modified-p nil
)
84 (get-buffer-create "*Abbrevs*"))
86 (defun edit-abbrevs-mode ()
87 "Major mode for editing the list of abbrev definitions.
90 (setq major-mode
'edit-abbrevs-mode
)
91 (setq mode-name
"Edit-Abbrevs")
92 (use-local-map edit-abbrevs-map
))
94 (defun edit-abbrevs ()
95 "Alter abbrev definitions by editing a list of them.
96 Selects a buffer containing a list of abbrev definitions.
97 You can edit them and type \\<edit-abbrevs-map>\\[edit-abbrevs-redefine] to redefine abbrevs
98 according to your editing.
99 Buffer contains a header line for each abbrev table,
100 which is the abbrev table name in parentheses.
101 This is followed by one line per abbrev in that table:
102 NAME USECOUNT EXPANSION HOOK
103 where NAME and EXPANSION are strings with quotes,
104 USECOUNT is an integer, and HOOK is any valid function
105 or may be omitted (it is usually omitted)."
107 (switch-to-buffer (prepare-abbrev-list-buffer)))
109 (defun edit-abbrevs-redefine ()
110 "Redefine abbrevs according to current buffer contents."
113 (set-buffer-modified-p nil
))
115 (defun define-abbrevs (&optional arg
)
116 "Define abbrevs according to current visible buffer contents.
117 See documentation of `edit-abbrevs' for info on the format of the
118 text you must have in the buffer.
119 With argument, eliminate all abbrev definitions except
120 the ones defined from the buffer now."
122 (if arg
(kill-all-abbrevs))
124 (goto-char (point-min))
125 (while (and (not (eobp)) (re-search-forward "^(" nil t
))
126 (let* ((buf (current-buffer))
130 (while (progn (forward-line 1)
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
)))))
139 (defun read-abbrev-file (&optional file quietly
)
140 "Read abbrev definitions from file written with `write-abbrev-file'.
141 Optional argument FILE is the name of the file to read;
142 it defaults to the value of `abbrev-file-name'.
143 Optional 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
)
147 (setq save-abbrevs t abbrevs-changed nil
))
149 (defun quietly-read-abbrev-file (&optional file
)
150 "Read abbrev definitions from file written with write-abbrev-file.
151 Optional argument FILE is the name of the file to read;
152 it defaults to the value of `abbrev-file-name'.
153 Does not print anything."
154 ;(interactive "fRead abbrev file: ")
155 (read-abbrev-file file t
))
157 (defun write-abbrev-file (file)
158 "Write all abbrev definitions to a file of Lisp code.
159 The file written can be loaded in another session to define the same abbrevs.
160 The argument FILE is the file name to write."
163 (read-file-name "Write abbrev file: "
164 (file-name-directory (expand-file-name abbrev-file-name
))
166 (or (and file
(> (length file
) 0))
167 (setq file abbrev-file-name
))
169 (set-buffer (get-buffer-create " write-abbrev-file"))
171 (let ((tables abbrev-table-name-list
))
173 (insert-abbrev-table-description (car tables
) nil
)
174 (setq tables
(cdr tables
))))
175 (write-region 1 (point-max) file
)
178 (defun add-mode-abbrev (arg)
179 "Define mode-specific abbrev for last word(s) before point.
180 Argument is how many words before point form the expansion;
181 or zero means the region is the expansion.
182 A negative argument means to undefine the specified abbrev.
183 Reads the abbreviation in the minibuffer.
185 Don't use this function in a Lisp program; use `define-abbrev' instead."
188 (if only-global-abbrevs
190 (or local-abbrev-table
191 (error "No per-mode abbrev table")))
194 (defun add-global-abbrev (arg)
195 "Define global (all modes) abbrev for last word(s) before point.
196 The prefix argument specifies the number of words before point that form the
197 expansion; or zero means the region is the expansion.
198 A negative argument means to undefine the specified abbrev.
199 This command uses the minibuffer to read the abbreviation.
201 Don't use this function in a Lisp program; use `define-abbrev' instead."
203 (add-abbrev global-abbrev-table
"Global" arg
))
205 (defun add-abbrev (table type arg
)
206 (let ((exp (and (>= arg
0)
210 (save-excursion (forward-word (- arg
)) (point))))))
213 (read-string (format (if exp
"%s abbrev for \"%s\": "
214 "Undefine %s abbrev: ")
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
))))
222 (defun inverse-add-mode-abbrev (arg)
223 "Define last word before point as a mode-specific abbrev.
224 With prefix argument N, defines the Nth word before point.
225 This command uses the minibuffer to read the expansion.
226 Expands the abbreviation after defining it."
229 (if only-global-abbrevs
231 (or local-abbrev-table
232 (error "No per-mode abbrev table")))
235 (defun inverse-add-global-abbrev (arg)
236 "Define last word before point as a global (mode-independent) abbrev.
237 With prefix argument N, defines the Nth word before point.
238 This command uses the minibuffer to read the expansion.
239 Expands the abbreviation after defining it."
241 (inverse-add-abbrev global-abbrev-table
"Global" arg
))
243 (defun inverse-add-abbrev (table type arg
)
244 (let (name nameloc exp
)
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\": "
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
))))
255 (define-abbrev table
(downcase name
) exp
)
260 (defun abbrev-prefix-mark (&optional arg
)
261 "Mark current point as the beginning of an abbrev.
262 Abbrev to be expanded starts here rather than at beginning of word.
263 This way, you can expand an abbrev with a prefix: insert the prefix,
264 use this command, then insert the abbrev."
266 (or arg
(expand-abbrev))
267 (setq abbrev-start-location
(point-marker)
268 abbrev-start-location-buffer
(current-buffer))
271 (defun expand-region-abbrevs (start end
&optional noquery
)
272 "For abbrev occurrence in the region, offer to expand it.
273 The user is asked to type y or n for each occurrence.
274 A prefix argument means don't query; expand all abbrevs.
275 If called from a Lisp program, arguments are START END &optional NOQUERY."
279 (let ((lim (- (point-max) end
))
281 (while (and (not (eobp))
282 (progn (forward-word 1)
283 (<= (setq pnt
(point)) (- (point-max) lim
))))
284 (if (abbrev-expansion
287 (save-excursion (forward-word -
1) (point))
289 (if (or noquery
(y-or-n-p (format "Expand `%s'? " string
)))
290 (expand-abbrev)))))))
292 ;;; abbrev.el ends here