]> code.delx.au - gnu-emacs/blob - lisp/abbrev.el
(timezone-parse-date): Handle 1-digit year.
[gnu-emacs] / lisp / abbrev.el
1 ;;; abbrev.el --- abbrev mode commands for Emacs
2
3 ;; Copyright (C) 1985, 1986, 1987, 1992 Free Software Foundation, Inc.
4
5 ;; Keywords: abbrev
6
7 ;; This file is part of GNU Emacs.
8
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.
13
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.
18
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.
23
24 ;;; Commentary:
25
26 ;; This facility is documented in the Emacs Manual.
27
28 ;;; Code:
29
30 (defvar 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.")
33
34 (defun abbrev-mode (arg)
35 "Toggle abbrev mode.
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."
39 (interactive "P")
40 (setq abbrev-mode
41 (if (null arg) (not abbrev-mode)
42 (> (prefix-numeric-value arg) 0)))
43 (force-mode-line-update))
44 \f
45 (defvar edit-abbrevs-map nil
46 "Keymap used in edit-abbrevs.")
47 (if edit-abbrevs-map
48 nil
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))
52
53 (defun kill-all-abbrevs ()
54 "Undefine all defined abbrevs."
55 (interactive)
56 (let ((tables abbrev-table-name-list))
57 (while tables
58 (clear-abbrev-table (symbol-value (car tables)))
59 (setq tables (cdr tables)))))
60
61 (defun insert-abbrevs ()
62 "Insert after point a description of all defined abbrevs.
63 Mark is set after the inserted text."
64 (interactive)
65 (push-mark
66 (save-excursion
67 (let ((tables abbrev-table-name-list))
68 (while tables
69 (insert-abbrev-table-description (car tables) t)
70 (setq tables (cdr tables))))
71 (point))))
72
73 (defun list-abbrevs ()
74 "Display a list of all defined abbrevs."
75 (interactive)
76 (display-buffer (prepare-abbrev-list-buffer)))
77
78 (defun prepare-abbrev-list-buffer ()
79 (save-excursion
80 (set-buffer (get-buffer-create "*Abbrevs*"))
81 (erase-buffer)
82 (let ((tables abbrev-table-name-list))
83 (while tables
84 (insert-abbrev-table-description (car tables) t)
85 (setq tables (cdr tables))))
86 (goto-char (point-min))
87 (set-buffer-modified-p nil)
88 (edit-abbrevs-mode))
89 (get-buffer-create "*Abbrevs*"))
90
91 (defun edit-abbrevs-mode ()
92 "Major mode for editing the list of abbrev definitions.
93 \\{edit-abbrevs-map}"
94 (interactive)
95 (setq major-mode 'edit-abbrevs-mode)
96 (setq mode-name "Edit-Abbrevs")
97 (use-local-map edit-abbrevs-map))
98
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)."
111 (interactive)
112 (switch-to-buffer (prepare-abbrev-list-buffer)))
113
114 (defun edit-abbrevs-redefine ()
115 "Redefine abbrevs according to current buffer contents."
116 (interactive)
117 (define-abbrevs t)
118 (set-buffer-modified-p nil))
119
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."
126 (interactive "P")
127 (if arg (kill-all-abbrevs))
128 (save-excursion
129 (goto-char (point-min))
130 (while (and (not (eobp)) (re-search-forward "^(" nil t))
131 (let* ((buf (current-buffer))
132 (table (read buf))
133 abbrevs name hook exp count)
134 (forward-line 1)
135 (while (progn (forward-line 1)
136 (not (eolp)))
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)))))
143
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)
151 nil quietly)
152 (setq save-abbrevs t abbrevs-changed nil))
153
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))
161
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."
166 (interactive
167 (list
168 (read-file-name "Write abbrev file: "
169 (file-name-directory (expand-file-name abbrev-file-name))
170 abbrev-file-name)))
171 (or (and file (> (length file) 0))
172 (setq file abbrev-file-name))
173 (save-excursion
174 (set-buffer (get-buffer-create " write-abbrev-file"))
175 (erase-buffer)
176 (let ((tables abbrev-table-name-list))
177 (while tables
178 (insert-abbrev-table-description (car tables) nil)
179 (setq tables (cdr tables))))
180 (write-region 1 (point-max) file)
181 (erase-buffer)))
182 \f
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.
189
190 Don't use this function in a Lisp program; use `define-abbrev' instead."
191 (interactive "p")
192 (add-abbrev
193 (if only-global-abbrevs
194 global-abbrev-table
195 (or local-abbrev-table
196 (error "No per-mode abbrev table")))
197 "Mode" arg))
198
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.
205
206 Don't use this function in a Lisp program; use `define-abbrev' instead."
207 (interactive "p")
208 (add-abbrev global-abbrev-table "Global" arg))
209
210 (defun add-abbrev (table type arg)
211 (let ((exp (and (>= arg 0)
212 (buffer-substring
213 (point)
214 (if (= arg 0) (mark)
215 (save-excursion (forward-word (- arg)) (point))))))
216 name)
217 (setq name
218 (read-string (format (if exp "%s abbrev for \"%s\": "
219 "Undefine %s abbrev: ")
220 type exp)))
221 (set-text-properties 0 (length name) nil name)
222 (if (or (null exp)
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))))
227
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."
233 (interactive "p")
234 (inverse-add-abbrev
235 (if only-global-abbrevs
236 global-abbrev-table
237 (or local-abbrev-table
238 (error "No per-mode abbrev table")))
239 "Mode" arg))
240
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."
246 (interactive "p")
247 (inverse-add-abbrev global-abbrev-table "Global" arg))
248
249 (defun inverse-add-abbrev (table type arg)
250 (let (name nameloc exp)
251 (save-excursion
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\": "
257 type name)))
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))))
261 (progn
262 (define-abbrev table (downcase name) exp)
263 (save-excursion
264 (goto-char nameloc)
265 (expand-abbrev))))))
266
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."
272 (interactive "P")
273 (or arg (expand-abbrev))
274 (setq abbrev-start-location (point-marker)
275 abbrev-start-location-buffer (current-buffer))
276 (insert "-"))
277
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."
283 (interactive "r\nP")
284 (save-excursion
285 (goto-char start)
286 (let ((lim (- (point-max) end))
287 pnt string)
288 (while (and (not (eobp))
289 (progn (forward-word 1)
290 (<= (setq pnt (point)) (- (point-max) lim))))
291 (if (abbrev-expansion
292 (setq string
293 (buffer-substring
294 (save-excursion (forward-word -1) (point))
295 pnt)))
296 (if (or noquery (y-or-n-p (format "Expand `%s'? " string)))
297 (expand-abbrev)))))))
298
299 ;;; abbrev.el ends here