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 the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
26 ;; This facility is documented in the Emacs Manual.
30 (defconst only-global-abbrevs nil
"\
31 *t means user plans to use global abbrevs only.
32 Makes the commands to define mode-specific abbrevs define global ones instead.")
34 (defun abbrev-mode (arg)
36 With argument ARG, turn abbrev mode on iff ARG is positive.
37 In abbrev mode, inserting an abbreviation causes it to expand
38 and be replaced by its expansion."
41 (if (null arg
) (not abbrev-mode
)
42 (> (prefix-numeric-value arg
) 0)))
43 (force-mode-line-update))
45 (defvar edit-abbrevs-map nil
46 "Keymap used in edit-abbrevs.")
49 (setq edit-abbrevs-map
(make-sparse-keymap))
50 (define-key edit-abbrevs-map
"\C-x\C-s" 'edit-abbrevs-redefine
)
51 (define-key edit-abbrevs-map
"\C-c\C-c" 'edit-abbrevs-redefine
))
53 (defun kill-all-abbrevs ()
54 "Undefine all defined abbrevs."
56 (let ((tables abbrev-table-name-list
))
58 (clear-abbrev-table (symbol-value (car tables
)))
59 (setq tables
(cdr tables
)))))
61 (defun insert-abbrevs ()
62 "Insert after point a description of all defined abbrevs.
63 Mark is set after the inserted text."
67 (let ((tables abbrev-table-name-list
))
69 (insert-abbrev-table-description (car tables
) t
)
70 (setq tables
(cdr tables
))))
73 (defun list-abbrevs ()
74 "Display a list of all defined abbrevs."
76 (display-buffer (prepare-abbrev-list-buffer)))
78 (defun prepare-abbrev-list-buffer ()
80 (set-buffer (get-buffer-create "*Abbrevs*"))
82 (let ((tables abbrev-table-name-list
))
84 (insert-abbrev-table-description (car tables
) t
)
85 (setq tables
(cdr tables
))))
86 (goto-char (point-min))
87 (set-buffer-modified-p nil
)
89 (get-buffer-create "*Abbrevs*"))
91 (defun edit-abbrevs-mode ()
92 "Major mode for editing the list of abbrev definitions.
95 (setq major-mode
'edit-abbrevs-mode
)
96 (setq mode-name
"Edit-Abbrevs")
97 (use-local-map edit-abbrevs-map
))
99 (defun edit-abbrevs ()
100 "Alter abbrev definitions by editing a list of them.
101 Selects a buffer containing a list of abbrev definitions.
102 You can edit them and type \\<edit-abbrevs-map>\\[edit-abbrevs-redefine] to redefine abbrevs
103 according to your editing.
104 Buffer contains a header line for each abbrev table,
105 which is the abbrev table name in parentheses.
106 This is followed by one line per abbrev in that table:
107 NAME USECOUNT EXPANSION HOOK
108 where NAME and EXPANSION are strings with quotes,
109 USECOUNT is an integer, and HOOK is any valid function
110 or may be omitted (it is usually omitted)."
112 (switch-to-buffer (prepare-abbrev-list-buffer)))
114 (defun edit-abbrevs-redefine ()
115 "Redefine abbrevs according to current buffer contents."
118 (set-buffer-modified-p nil
))
120 (defun define-abbrevs (&optional arg
)
121 "Define abbrevs according to current visible buffer contents.
122 See documentation of `edit-abbrevs' for info on the format of the
123 text you must have in the buffer.
124 With argument, eliminate all abbrev definitions except
125 the ones defined from the buffer now."
127 (if arg
(kill-all-abbrevs))
129 (goto-char (point-min))
130 (while (and (not (eobp)) (re-search-forward "^(" nil t
))
131 (let* ((buf (current-buffer))
133 abbrevs name hook exp count
)
135 (while (progn (forward-line 1)
137 (setq name
(read buf
) count
(read buf
) exp
(read buf
))
138 (skip-chars-backward " \t\n\f")
139 (setq hook
(if (not (eolp)) (read buf
)))
140 (skip-chars-backward " \t\n\f")
141 (setq abbrevs
(cons (list name exp hook count
) abbrevs
)))
142 (define-abbrev-table table abbrevs
)))))
144 (defun read-abbrev-file (&optional file quietly
)
145 "Read abbrev definitions from file written with `write-abbrev-file'.
146 Optional argument FILE is the name of the file to read;
147 it defaults to the value of `abbrev-file-name'.
148 Optional second argument QUIETLY non-nil means don't print anything."
149 (interactive "fRead abbrev file: ")
150 (load (if (and file
(> (length file
) 0)) file abbrev-file-name
)
152 (setq save-abbrevs t abbrevs-changed nil
))
154 (defun quietly-read-abbrev-file (&optional file
)
155 "Read abbrev definitions from file written with write-abbrev-file.
156 Optional argument FILE is the name of the file to read;
157 it defaults to the value of `abbrev-file-name'.
158 Does not print anything."
159 ;(interactive "fRead abbrev file: ")
160 (read-abbrev-file file t
))
162 (defun write-abbrev-file (file)
163 "Write all abbrev definitions to a file of Lisp code.
164 The file written can be loaded in another session to define the same abbrevs.
165 The argument FILE is the file name to write."
168 (read-file-name "Write abbrev file: "
169 (file-name-directory (expand-file-name abbrev-file-name
))
171 (or (and file
(> (length file
) 0))
172 (setq file abbrev-file-name
))
174 (set-buffer (get-buffer-create " write-abbrev-file"))
176 (let ((tables abbrev-table-name-list
))
178 (insert-abbrev-table-description (car tables
) nil
)
179 (setq tables
(cdr tables
))))
180 (write-region 1 (point-max) file
)
183 (defun add-mode-abbrev (arg)
184 "Define mode-specific abbrev for last word(s) before point.
185 Argument is how many words before point form the expansion;
186 or zero means the region is the expansion.
187 A negative argument means to undefine the specified abbrev.
188 Reads the abbreviation in the minibuffer.
190 Don't use this function in a Lisp program; use `define-abbrev' instead."
193 (if only-global-abbrevs
195 (or local-abbrev-table
196 (error "No per-mode abbrev table")))
199 (defun add-global-abbrev (arg)
200 "Define global (all modes) abbrev for last word(s) before point.
201 The prefix argument specifies the number of words before point that form the
202 expansion; or zero means the region is the expansion.
203 A negative argument means to undefine the specified abbrev.
204 This command uses the minibuffer to read the abbreviation.
206 Don't use this function in a Lisp program; use `define-abbrev' instead."
208 (add-abbrev global-abbrev-table
"Global" arg
))
210 (defun add-abbrev (table type arg
)
211 (let ((exp (and (>= arg
0)
215 (save-excursion (forward-word (- arg
)) (point))))))
218 (read-string (format (if exp
"%s abbrev for \"%s\": "
219 "Undefine %s abbrev: ")
221 (set-text-properties 0 (length name
) nil name
)
223 (not (abbrev-expansion name table
))
224 (y-or-n-p (format "%s expands to \"%s\"; redefine? "
225 name
(abbrev-expansion name table
))))
226 (define-abbrev table
(downcase name
) exp
))))
228 (defun inverse-add-mode-abbrev (arg)
229 "Define last word before point as a mode-specific abbrev.
230 With prefix argument N, defines the Nth word before point.
231 This command uses the minibuffer to read the expansion.
232 Expands the abbreviation after defining it."
235 (if only-global-abbrevs
237 (or local-abbrev-table
238 (error "No per-mode abbrev table")))
241 (defun inverse-add-global-abbrev (arg)
242 "Define last word before point as a global (mode-independent) abbrev.
243 With prefix argument N, defines the Nth word before point.
244 This command uses the minibuffer to read the expansion.
245 Expands the abbreviation after defining it."
247 (inverse-add-abbrev global-abbrev-table
"Global" arg
))
249 (defun inverse-add-abbrev (table type arg
)
250 (let (name nameloc exp
)
252 (forward-word (- arg
))
253 (setq name
(buffer-substring (point) (progn (forward-word 1)
254 (setq nameloc
(point))))))
255 (set-text-properties 0 (length name
) nil name
)
256 (setq exp
(read-string (format "%s expansion for \"%s\": "
258 (if (or (not (abbrev-expansion name table
))
259 (y-or-n-p (format "%s expands to \"%s\"; redefine? "
260 name
(abbrev-expansion name table
))))
262 (define-abbrev table
(downcase name
) exp
)
267 (defun abbrev-prefix-mark (&optional arg
)
268 "Mark current point as the beginning of an abbrev.
269 Abbrev to be expanded starts here rather than at beginning of word.
270 This way, you can expand an abbrev with a prefix: insert the prefix,
271 use this command, then insert the abbrev."
273 (or arg
(expand-abbrev))
274 (setq abbrev-start-location
(point-marker)
275 abbrev-start-location-buffer
(current-buffer))
278 (defun expand-region-abbrevs (start end
&optional noquery
)
279 "For abbrev occurrence in the region, offer to expand it.
280 The user is asked to type y or n for each occurrence.
281 A prefix argument means don't query; expand all abbrevs.
282 If called from a Lisp program, arguments are START END &optional NOQUERY."
286 (let ((lim (- (point-max) end
))
288 (while (and (not (eobp))
289 (progn (forward-word 1)
290 (<= (setq pnt
(point)) (- (point-max) lim
))))
291 (if (abbrev-expansion
294 (save-excursion (forward-word -
1) (point))
296 (if (or noquery
(y-or-n-p (format "Expand `%s'? " string
)))
297 (expand-abbrev)))))))
299 ;;; abbrev.el ends here