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 ;; This facility is documented in the Emacs Manual.
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)
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."
40 (if (null arg
) (not abbrev-mode
)
41 (> (prefix-numeric-value arg
) 0)))
42 (force-mode-line-update))
44 (defvar edit-abbrevs-map nil
45 "Keymap used in edit-abbrevs.")
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."
55 (let ((tables abbrev-table-name-list
))
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."
66 (let ((tables abbrev-table-name-list
))
68 (insert-abbrev-table-description (car tables
) t
)
69 (setq tables
(cdr tables
))))
72 (defun list-abbrevs ()
73 "Display a list of all defined abbrevs."
75 (display-buffer (prepare-abbrev-list-buffer)))
77 (defun prepare-abbrev-list-buffer ()
79 (set-buffer (get-buffer-create "*Abbrevs*"))
81 (let ((tables abbrev-table-name-list
))
83 (insert-abbrev-table-description (car tables
) t
)
84 (setq tables
(cdr tables
))))
85 (goto-char (point-min))
86 (set-buffer-modified-p nil
)
88 (get-buffer-create "*Abbrevs*"))
90 (defun edit-abbrevs-mode ()
91 "Major mode for editing the list of abbrev definitions.
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)."
111 (switch-to-buffer (prepare-abbrev-list-buffer)))
113 (defun edit-abbrevs-redefine ()
114 "Redefine abbrevs according to current buffer contents."
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."
126 (if arg
(kill-all-abbrevs))
128 (goto-char (point-min))
129 (while (and (not (eobp)) (re-search-forward "^(" nil t
))
130 (let* ((buf (current-buffer))
132 abbrevs name hook exp count
)
134 (while (progn (forward-line 1)
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
)
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."
167 (read-file-name "Write abbrev file: "
168 (file-name-directory (expand-file-name abbrev-file-name
))
170 (or (and file
(> (length file
) 0))
171 (setq file abbrev-file-name
))
173 (set-buffer (get-buffer-create " write-abbrev-file"))
175 (let ((tables abbrev-table-name-list
))
177 (insert-abbrev-table-description (car tables
) nil
)
178 (setq tables
(cdr tables
))))
179 (write-region 1 (point-max) file
)
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."
192 (if only-global-abbrevs
194 (or local-abbrev-table
195 (error "No per-mode abbrev table")))
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."
207 (add-abbrev global-abbrev-table
"Global" arg
))
209 (defun add-abbrev (table type arg
)
210 (let ((exp (and (>= arg
0)
214 (save-excursion (forward-word (- arg
)) (point))))))
217 (read-string (format (if exp
"%s abbrev for \"%s\": "
218 "Undefine %s abbrev: ")
220 (set-text-properties 0 (length name
) nil name
)
222 (not (abbrev-expansion name table
))
223 (y-or-n-p (format "%s expands to \"%s\"; redefine? "
224 name
(abbrev-expansion name table
))))
225 (define-abbrev table
(downcase name
) exp
))))
227 (defun inverse-add-mode-abbrev (arg)
228 "Define last word before point as a mode-specific abbrev.
229 With prefix argument N, defines the Nth word before point.
230 This command uses the minibuffer to read the expansion.
231 Expands the abbreviation after defining it."
234 (if only-global-abbrevs
236 (or local-abbrev-table
237 (error "No per-mode abbrev table")))
240 (defun inverse-add-global-abbrev (arg)
241 "Define last word before point as a global (mode-independent) abbrev.
242 With prefix argument N, defines the Nth word before point.
243 This command uses the minibuffer to read the expansion.
244 Expands the abbreviation after defining it."
246 (inverse-add-abbrev global-abbrev-table
"Global" arg
))
248 (defun inverse-add-abbrev (table type arg
)
249 (let (name nameloc exp
)
251 (forward-word (- arg
))
252 (setq name
(buffer-substring (point) (progn (forward-word 1)
253 (setq nameloc
(point))))))
254 (set-text-properties 0 (length name
) nil name
)
255 (setq exp
(read-string (format "%s expansion for \"%s\": "
257 (if (or (not (abbrev-expansion name table
))
258 (y-or-n-p (format "%s expands to \"%s\"; redefine? "
259 name
(abbrev-expansion name table
))))
261 (define-abbrev table
(downcase name
) exp
)
266 (defun abbrev-prefix-mark (&optional arg
)
267 "Mark current point as the beginning of an abbrev.
268 Abbrev to be expanded starts here rather than at beginning of word.
269 This way, you can expand an abbrev with a prefix: insert the prefix,
270 use this command, then insert the abbrev."
272 (or arg
(expand-abbrev))
273 (setq abbrev-start-location
(point-marker)
274 abbrev-start-location-buffer
(current-buffer))
277 (defun expand-region-abbrevs (start end
&optional noquery
)
278 "For abbrev occurrence in the region, offer to expand it.
279 The user is asked to type y or n for each occurrence.
280 A prefix argument means don't query; expand all abbrevs.
281 If called from a Lisp program, arguments are START END &optional NOQUERY."
285 (let ((lim (- (point-max) end
))
287 (while (and (not (eobp))
288 (progn (forward-word 1)
289 (<= (setq pnt
(point)) (- (point-max) lim
))))
290 (if (abbrev-expansion
293 (save-excursion (forward-word -
1) (point))
295 (if (or noquery
(y-or-n-p (format "Expand `%s'? " string
)))
296 (expand-abbrev)))))))
298 ;;; abbrev.el ends here