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