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