Mercurial > emacs
annotate lisp/abbrev.el @ 9394:802381617d14
(rmail-delete-forward): Use rmail-select-summary.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Sat, 08 Oct 1994 04:12:07 +0000 |
parents | 569ed1f55710 |
children | 03792bb111ab |
rev | line source |
---|---|
662
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
411
diff
changeset
|
1 ;;; abbrev.el --- abbrev mode commands for Emacs |
411 | 2 |
846
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
807
diff
changeset
|
3 ;; Copyright (C) 1985, 1986, 1987, 1992 Free Software Foundation, Inc. |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
807
diff
changeset
|
4 |
2247
2c7997f249eb
Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
1329
diff
changeset
|
5 ;; Keywords: abbrev |
2c7997f249eb
Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
1329
diff
changeset
|
6 |
411 | 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 | |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
732
diff
changeset
|
11 ;; the Free Software Foundation; either version 2, or (at your option) |
411 | 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 | |
21 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
22 | |
2307
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2247
diff
changeset
|
23 ;;; Commentary: |
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2247
diff
changeset
|
24 |
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2247
diff
changeset
|
25 ;; This facility is documented in the Emacs Manual. |
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2247
diff
changeset
|
26 |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
732
diff
changeset
|
27 ;;; Code: |
411 | 28 |
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.") | |
32 | |
33 (defun abbrev-mode (arg) | |
34 "Toggle abbrev mode. | |
1329 | 35 With argument ARG, turn abbrev mode on iff ARG is positive. |
411 | 36 In abbrev mode, inserting an abbreviation causes it to expand |
37 and be replaced by its expansion." | |
38 (interactive "P") | |
39 (setq abbrev-mode | |
40 (if (null arg) (not abbrev-mode) | |
41 (> (prefix-numeric-value arg) 0))) | |
42 (set-buffer-modified-p (buffer-modified-p))) ;No-op, but updates mode line. | |
43 | |
44 (defvar edit-abbrevs-map nil | |
45 "Keymap used in edit-abbrevs.") | |
46 (if edit-abbrevs-map | |
47 nil | |
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)) | |
51 | |
52 (defun kill-all-abbrevs () | |
53 "Undefine all defined abbrevs." | |
54 (interactive) | |
55 (let ((tables abbrev-table-name-list)) | |
56 (while tables | |
57 (clear-abbrev-table (symbol-value (car tables))) | |
58 (setq tables (cdr tables))))) | |
59 | |
60 (defun insert-abbrevs () | |
61 "Insert after point a description of all defined abbrevs. | |
62 Mark is set after the inserted text." | |
63 (interactive) | |
64 (push-mark | |
65 (save-excursion | |
66 (let ((tables abbrev-table-name-list)) | |
67 (while tables | |
68 (insert-abbrev-table-description (car tables) t) | |
69 (setq tables (cdr tables)))) | |
70 (point)))) | |
71 | |
72 (defun list-abbrevs () | |
73 "Display a list of all defined abbrevs." | |
74 (interactive) | |
75 (display-buffer (prepare-abbrev-list-buffer))) | |
76 | |
77 (defun prepare-abbrev-list-buffer () | |
78 (save-excursion | |
79 (set-buffer (get-buffer-create "*Abbrevs*")) | |
80 (erase-buffer) | |
81 (let ((tables abbrev-table-name-list)) | |
82 (while tables | |
83 (insert-abbrev-table-description (car tables) t) | |
84 (setq tables (cdr tables)))) | |
85 (goto-char (point-min)) | |
86 (set-buffer-modified-p nil) | |
87 (edit-abbrevs-mode)) | |
88 (get-buffer-create "*Abbrevs*")) | |
89 | |
90 (defun edit-abbrevs-mode () | |
91 "Major mode for editing the list of abbrev definitions. | |
92 \\{edit-abbrevs-map}" | |
93 (interactive) | |
94 (setq major-mode 'edit-abbrevs-mode) | |
95 (setq mode-name "Edit-Abbrevs") | |
96 (use-local-map edit-abbrevs-map)) | |
97 | |
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)." | |
110 (interactive) | |
111 (switch-to-buffer (prepare-abbrev-list-buffer))) | |
112 | |
113 (defun edit-abbrevs-redefine () | |
114 "Redefine abbrevs according to current buffer contents." | |
115 (interactive) | |
116 (define-abbrevs t) | |
117 (set-buffer-modified-p nil)) | |
118 | |
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." | |
125 (interactive "P") | |
126 (if arg (kill-all-abbrevs)) | |
127 (save-excursion | |
128 (goto-char (point-min)) | |
129 (while (and (not (eobp)) (re-search-forward "^(" nil t)) | |
130 (let* ((buf (current-buffer)) | |
131 (table (read buf)) | |
862
46630543d659
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
132 abbrevs name hook exp count) |
411 | 133 (forward-line 1) |
134 (while (progn (forward-line 1) | |
135 (not (eolp))) | |
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))))) | |
142 | |
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) | |
150 nil quietly) | |
151 (setq save-abbrevs t abbrevs-changed nil)) | |
152 | |
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)) | |
160 | |
161 (defun write-abbrev-file (file) | |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
732
diff
changeset
|
162 "Write all abbrev definitions to a file of Lisp code. |
411 | 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." | |
165 (interactive | |
166 (list | |
167 (read-file-name "Write abbrev file: " | |
168 (file-name-directory (expand-file-name abbrev-file-name)) | |
169 abbrev-file-name))) | |
170 (or (and file (> (length file) 0)) | |
171 (setq file abbrev-file-name)) | |
172 (save-excursion | |
173 (set-buffer (get-buffer-create " write-abbrev-file")) | |
174 (erase-buffer) | |
175 (let ((tables abbrev-table-name-list)) | |
176 (while tables | |
177 (insert-abbrev-table-description (car tables) nil) | |
178 (setq tables (cdr tables)))) | |
179 (write-region 1 (point-max) file) | |
180 (erase-buffer))) | |
181 | |
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. | |
188 | |
189 Don't use this function in a Lisp program; use `define-abbrev' instead." | |
190 (interactive "p") | |
191 (add-abbrev | |
192 (if only-global-abbrevs | |
193 global-abbrev-table | |
194 (or local-abbrev-table | |
195 (error "No per-mode abbrev table"))) | |
196 "Mode" arg)) | |
197 | |
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. | |
204 | |
205 Don't use this function in a Lisp program; use `define-abbrev' instead." | |
206 (interactive "p") | |
207 (add-abbrev global-abbrev-table "Global" arg)) | |
208 | |
209 (defun add-abbrev (table type arg) | |
210 (let ((exp (and (>= arg 0) | |
211 (buffer-substring | |
212 (point) | |
213 (if (= arg 0) (mark) | |
214 (save-excursion (forward-word (- arg)) (point)))))) | |
215 name) | |
216 (setq name | |
217 (read-string (format (if exp "%s abbrev for \"%s\": " | |
218 "Undefine %s abbrev: ") | |
219 type exp))) | |
9180
569ed1f55710
(add-abbrev, inverse-add-abbrev):
Richard M. Stallman <rms@gnu.org>
parents:
2307
diff
changeset
|
220 (set-text-properties 0 (length name) nil name) |
411 | 221 (if (or (null exp) |
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)))) | |
226 | |
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." | |
232 (interactive "p") | |
233 (inverse-add-abbrev | |
234 (if only-global-abbrevs | |
235 global-abbrev-table | |
236 (or local-abbrev-table | |
237 (error "No per-mode abbrev table"))) | |
238 "Mode" arg)) | |
239 | |
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." | |
245 (interactive "p") | |
246 (inverse-add-abbrev global-abbrev-table "Global" arg)) | |
247 | |
248 (defun inverse-add-abbrev (table type arg) | |
249 (let (name nameloc exp) | |
250 (save-excursion | |
251 (forward-word (- arg)) | |
252 (setq name (buffer-substring (point) (progn (forward-word 1) | |
253 (setq nameloc (point)))))) | |
9180
569ed1f55710
(add-abbrev, inverse-add-abbrev):
Richard M. Stallman <rms@gnu.org>
parents:
2307
diff
changeset
|
254 (set-text-properties 0 (length name) nil name) |
411 | 255 (setq exp (read-string (format "%s expansion for \"%s\": " |
256 type name))) | |
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)))) | |
260 (progn | |
261 (define-abbrev table (downcase name) exp) | |
262 (save-excursion | |
263 (goto-char nameloc) | |
264 (expand-abbrev)))))) | |
265 | |
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." | |
271 (interactive "P") | |
272 (or arg (expand-abbrev)) | |
273 (setq abbrev-start-location (point-marker) | |
274 abbrev-start-location-buffer (current-buffer)) | |
275 (insert "-")) | |
276 | |
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." | |
282 (interactive "r\nP") | |
283 (save-excursion | |
284 (goto-char start) | |
285 (let ((lim (- (point-max) end)) | |
286 pnt string) | |
287 (while (and (not (eobp)) | |
288 (progn (forward-word 1) | |
289 (<= (setq pnt (point)) (- (point-max) lim)))) | |
290 (if (abbrev-expansion | |
291 (setq string | |
292 (buffer-substring | |
293 (save-excursion (forward-word -1) (point)) | |
294 pnt))) | |
295 (if (or noquery (y-or-n-p (format "Expand `%s'? " string))) | |
296 (expand-abbrev))))))) | |
662
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
411
diff
changeset
|
297 |
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
411
diff
changeset
|
298 ;;; abbrev.el ends here |