Mercurial > emacs
annotate lisp/abbrev.el @ 22967:63801d5488f4
(Vthis_command): Renamed from this_command.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Sun, 09 Aug 1998 21:55:27 +0000 |
parents | a77d473867b8 |
children | 8c7fdc02cb78 |
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 |
22250
a77d473867b8
*** empty log message ***
Dan Nicolaescu <done@ece.arizona.edu>
parents:
19307
diff
changeset
|
5 ;; Keywords: abbrev convenience |
2247
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 | |
14169 | 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. | |
411 | 23 |
2307
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2247
diff
changeset
|
24 ;;; Commentary: |
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2247
diff
changeset
|
25 |
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2247
diff
changeset
|
26 ;; 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
|
27 |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
732
diff
changeset
|
28 ;;; Code: |
411 | 29 |
19307 | 30 (defcustom only-global-abbrevs nil |
31 "*t means user plans to use global abbrevs only. | |
32 This makes the commands that normally define mode-specific abbrevs | |
33 define global abbrevs instead." | |
34 :type 'boolean | |
22250
a77d473867b8
*** empty log message ***
Dan Nicolaescu <done@ece.arizona.edu>
parents:
19307
diff
changeset
|
35 :group 'abbrev-mode |
a77d473867b8
*** empty log message ***
Dan Nicolaescu <done@ece.arizona.edu>
parents:
19307
diff
changeset
|
36 :group 'convenience) |
411 | 37 |
38 (defun abbrev-mode (arg) | |
39 "Toggle abbrev mode. | |
1329 | 40 With argument ARG, turn abbrev mode on iff ARG is positive. |
411 | 41 In abbrev mode, inserting an abbreviation causes it to expand |
42 and be replaced by its expansion." | |
43 (interactive "P") | |
44 (setq abbrev-mode | |
45 (if (null arg) (not abbrev-mode) | |
46 (> (prefix-numeric-value arg) 0))) | |
11558
03792bb111ab
(abbrev-mode): Use force-mode-line-update.
Karl Heuer <kwzh@gnu.org>
parents:
9180
diff
changeset
|
47 (force-mode-line-update)) |
19307 | 48 |
49 (defcustom abbrev-mode nil | |
50 "Toggle abbrev mode. | |
51 In abbrev mode, inserting an abbreviation causes it to expand | |
52 and be replaced by its expansion. | |
53 You must modify via \\[customize] for this variable to have an effect." | |
54 :set (lambda (symbol value) | |
55 (abbrev-mode (if value 1 0))) | |
56 :type 'boolean | |
57 :group 'abbrev-mode) | |
58 | |
411 | 59 |
60 (defvar edit-abbrevs-map nil | |
61 "Keymap used in edit-abbrevs.") | |
62 (if edit-abbrevs-map | |
63 nil | |
64 (setq edit-abbrevs-map (make-sparse-keymap)) | |
65 (define-key edit-abbrevs-map "\C-x\C-s" 'edit-abbrevs-redefine) | |
66 (define-key edit-abbrevs-map "\C-c\C-c" 'edit-abbrevs-redefine)) | |
67 | |
68 (defun kill-all-abbrevs () | |
69 "Undefine all defined abbrevs." | |
70 (interactive) | |
71 (let ((tables abbrev-table-name-list)) | |
72 (while tables | |
73 (clear-abbrev-table (symbol-value (car tables))) | |
74 (setq tables (cdr tables))))) | |
75 | |
76 (defun insert-abbrevs () | |
77 "Insert after point a description of all defined abbrevs. | |
78 Mark is set after the inserted text." | |
79 (interactive) | |
80 (push-mark | |
81 (save-excursion | |
82 (let ((tables abbrev-table-name-list)) | |
83 (while tables | |
84 (insert-abbrev-table-description (car tables) t) | |
85 (setq tables (cdr tables)))) | |
86 (point)))) | |
87 | |
88 (defun list-abbrevs () | |
89 "Display a list of all defined abbrevs." | |
90 (interactive) | |
91 (display-buffer (prepare-abbrev-list-buffer))) | |
92 | |
93 (defun prepare-abbrev-list-buffer () | |
94 (save-excursion | |
95 (set-buffer (get-buffer-create "*Abbrevs*")) | |
96 (erase-buffer) | |
97 (let ((tables abbrev-table-name-list)) | |
98 (while tables | |
99 (insert-abbrev-table-description (car tables) t) | |
100 (setq tables (cdr tables)))) | |
101 (goto-char (point-min)) | |
102 (set-buffer-modified-p nil) | |
103 (edit-abbrevs-mode)) | |
104 (get-buffer-create "*Abbrevs*")) | |
105 | |
106 (defun edit-abbrevs-mode () | |
107 "Major mode for editing the list of abbrev definitions. | |
108 \\{edit-abbrevs-map}" | |
109 (interactive) | |
110 (setq major-mode 'edit-abbrevs-mode) | |
111 (setq mode-name "Edit-Abbrevs") | |
112 (use-local-map edit-abbrevs-map)) | |
113 | |
114 (defun edit-abbrevs () | |
115 "Alter abbrev definitions by editing a list of them. | |
116 Selects a buffer containing a list of abbrev definitions. | |
117 You can edit them and type \\<edit-abbrevs-map>\\[edit-abbrevs-redefine] to redefine abbrevs | |
118 according to your editing. | |
119 Buffer contains a header line for each abbrev table, | |
120 which is the abbrev table name in parentheses. | |
121 This is followed by one line per abbrev in that table: | |
122 NAME USECOUNT EXPANSION HOOK | |
123 where NAME and EXPANSION are strings with quotes, | |
124 USECOUNT is an integer, and HOOK is any valid function | |
125 or may be omitted (it is usually omitted)." | |
126 (interactive) | |
127 (switch-to-buffer (prepare-abbrev-list-buffer))) | |
128 | |
129 (defun edit-abbrevs-redefine () | |
130 "Redefine abbrevs according to current buffer contents." | |
131 (interactive) | |
132 (define-abbrevs t) | |
133 (set-buffer-modified-p nil)) | |
134 | |
135 (defun define-abbrevs (&optional arg) | |
136 "Define abbrevs according to current visible buffer contents. | |
137 See documentation of `edit-abbrevs' for info on the format of the | |
138 text you must have in the buffer. | |
139 With argument, eliminate all abbrev definitions except | |
140 the ones defined from the buffer now." | |
141 (interactive "P") | |
142 (if arg (kill-all-abbrevs)) | |
143 (save-excursion | |
144 (goto-char (point-min)) | |
145 (while (and (not (eobp)) (re-search-forward "^(" nil t)) | |
146 (let* ((buf (current-buffer)) | |
147 (table (read buf)) | |
862
46630543d659
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
148 abbrevs name hook exp count) |
411 | 149 (forward-line 1) |
150 (while (progn (forward-line 1) | |
151 (not (eolp))) | |
152 (setq name (read buf) count (read buf) exp (read buf)) | |
153 (skip-chars-backward " \t\n\f") | |
154 (setq hook (if (not (eolp)) (read buf))) | |
155 (skip-chars-backward " \t\n\f") | |
156 (setq abbrevs (cons (list name exp hook count) abbrevs))) | |
157 (define-abbrev-table table abbrevs))))) | |
158 | |
159 (defun read-abbrev-file (&optional file quietly) | |
160 "Read abbrev definitions from file written with `write-abbrev-file'. | |
161 Optional argument FILE is the name of the file to read; | |
162 it defaults to the value of `abbrev-file-name'. | |
163 Optional second argument QUIETLY non-nil means don't print anything." | |
164 (interactive "fRead abbrev file: ") | |
165 (load (if (and file (> (length file) 0)) file abbrev-file-name) | |
166 nil quietly) | |
167 (setq save-abbrevs t abbrevs-changed nil)) | |
168 | |
169 (defun quietly-read-abbrev-file (&optional file) | |
170 "Read abbrev definitions from file written with write-abbrev-file. | |
171 Optional argument FILE is the name of the file to read; | |
172 it defaults to the value of `abbrev-file-name'. | |
173 Does not print anything." | |
174 ;(interactive "fRead abbrev file: ") | |
175 (read-abbrev-file file t)) | |
176 | |
177 (defun write-abbrev-file (file) | |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
732
diff
changeset
|
178 "Write all abbrev definitions to a file of Lisp code. |
411 | 179 The file written can be loaded in another session to define the same abbrevs. |
180 The argument FILE is the file name to write." | |
181 (interactive | |
182 (list | |
183 (read-file-name "Write abbrev file: " | |
184 (file-name-directory (expand-file-name abbrev-file-name)) | |
185 abbrev-file-name))) | |
186 (or (and file (> (length file) 0)) | |
187 (setq file abbrev-file-name)) | |
188 (save-excursion | |
189 (set-buffer (get-buffer-create " write-abbrev-file")) | |
190 (erase-buffer) | |
191 (let ((tables abbrev-table-name-list)) | |
192 (while tables | |
193 (insert-abbrev-table-description (car tables) nil) | |
194 (setq tables (cdr tables)))) | |
195 (write-region 1 (point-max) file) | |
196 (erase-buffer))) | |
197 | |
198 (defun add-mode-abbrev (arg) | |
199 "Define mode-specific abbrev for last word(s) before point. | |
200 Argument is how many words before point form the expansion; | |
201 or zero means the region is the expansion. | |
202 A negative argument means to undefine the specified abbrev. | |
203 Reads the abbreviation in the minibuffer. | |
204 | |
205 Don't use this function in a Lisp program; use `define-abbrev' instead." | |
206 (interactive "p") | |
207 (add-abbrev | |
208 (if only-global-abbrevs | |
209 global-abbrev-table | |
210 (or local-abbrev-table | |
211 (error "No per-mode abbrev table"))) | |
212 "Mode" arg)) | |
213 | |
214 (defun add-global-abbrev (arg) | |
215 "Define global (all modes) abbrev for last word(s) before point. | |
216 The prefix argument specifies the number of words before point that form the | |
217 expansion; or zero means the region is the expansion. | |
218 A negative argument means to undefine the specified abbrev. | |
219 This command uses the minibuffer to read the abbreviation. | |
220 | |
221 Don't use this function in a Lisp program; use `define-abbrev' instead." | |
222 (interactive "p") | |
223 (add-abbrev global-abbrev-table "Global" arg)) | |
224 | |
225 (defun add-abbrev (table type arg) | |
226 (let ((exp (and (>= arg 0) | |
227 (buffer-substring | |
228 (point) | |
229 (if (= arg 0) (mark) | |
230 (save-excursion (forward-word (- arg)) (point)))))) | |
231 name) | |
232 (setq name | |
233 (read-string (format (if exp "%s abbrev for \"%s\": " | |
234 "Undefine %s abbrev: ") | |
235 type exp))) | |
9180
569ed1f55710
(add-abbrev, inverse-add-abbrev):
Richard M. Stallman <rms@gnu.org>
parents:
2307
diff
changeset
|
236 (set-text-properties 0 (length name) nil name) |
411 | 237 (if (or (null exp) |
238 (not (abbrev-expansion name table)) | |
239 (y-or-n-p (format "%s expands to \"%s\"; redefine? " | |
240 name (abbrev-expansion name table)))) | |
241 (define-abbrev table (downcase name) exp)))) | |
242 | |
243 (defun inverse-add-mode-abbrev (arg) | |
244 "Define last word before point as a mode-specific abbrev. | |
245 With prefix argument N, defines the Nth word before point. | |
246 This command uses the minibuffer to read the expansion. | |
247 Expands the abbreviation after defining it." | |
248 (interactive "p") | |
249 (inverse-add-abbrev | |
250 (if only-global-abbrevs | |
251 global-abbrev-table | |
252 (or local-abbrev-table | |
253 (error "No per-mode abbrev table"))) | |
254 "Mode" arg)) | |
255 | |
256 (defun inverse-add-global-abbrev (arg) | |
257 "Define last word before point as a global (mode-independent) abbrev. | |
258 With prefix argument N, defines the Nth word before point. | |
259 This command uses the minibuffer to read the expansion. | |
260 Expands the abbreviation after defining it." | |
261 (interactive "p") | |
262 (inverse-add-abbrev global-abbrev-table "Global" arg)) | |
263 | |
264 (defun inverse-add-abbrev (table type arg) | |
265 (let (name nameloc exp) | |
266 (save-excursion | |
267 (forward-word (- arg)) | |
268 (setq name (buffer-substring (point) (progn (forward-word 1) | |
269 (setq nameloc (point)))))) | |
9180
569ed1f55710
(add-abbrev, inverse-add-abbrev):
Richard M. Stallman <rms@gnu.org>
parents:
2307
diff
changeset
|
270 (set-text-properties 0 (length name) nil name) |
411 | 271 (setq exp (read-string (format "%s expansion for \"%s\": " |
272 type name))) | |
273 (if (or (not (abbrev-expansion name table)) | |
274 (y-or-n-p (format "%s expands to \"%s\"; redefine? " | |
275 name (abbrev-expansion name table)))) | |
276 (progn | |
277 (define-abbrev table (downcase name) exp) | |
278 (save-excursion | |
279 (goto-char nameloc) | |
280 (expand-abbrev)))))) | |
281 | |
282 (defun abbrev-prefix-mark (&optional arg) | |
283 "Mark current point as the beginning of an abbrev. | |
284 Abbrev to be expanded starts here rather than at beginning of word. | |
285 This way, you can expand an abbrev with a prefix: insert the prefix, | |
286 use this command, then insert the abbrev." | |
287 (interactive "P") | |
288 (or arg (expand-abbrev)) | |
289 (setq abbrev-start-location (point-marker) | |
290 abbrev-start-location-buffer (current-buffer)) | |
291 (insert "-")) | |
292 | |
293 (defun expand-region-abbrevs (start end &optional noquery) | |
294 "For abbrev occurrence in the region, offer to expand it. | |
295 The user is asked to type y or n for each occurrence. | |
296 A prefix argument means don't query; expand all abbrevs. | |
297 If called from a Lisp program, arguments are START END &optional NOQUERY." | |
298 (interactive "r\nP") | |
299 (save-excursion | |
300 (goto-char start) | |
301 (let ((lim (- (point-max) end)) | |
302 pnt string) | |
303 (while (and (not (eobp)) | |
304 (progn (forward-word 1) | |
305 (<= (setq pnt (point)) (- (point-max) lim)))) | |
306 (if (abbrev-expansion | |
307 (setq string | |
308 (buffer-substring | |
309 (save-excursion (forward-word -1) (point)) | |
310 pnt))) | |
311 (if (or noquery (y-or-n-p (format "Expand `%s'? " string))) | |
312 (expand-abbrev))))))) | |
662
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
411
diff
changeset
|
313 |
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
411
diff
changeset
|
314 ;;; abbrev.el ends here |