411
|
1 ;; Abbrev mode commands for Emacs
|
|
2
|
|
3 ;; Copyright (C) 1985-1991 Free Software Foundation, Inc.
|
|
4
|
|
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
|
|
9 ;; the Free Software Foundation; either version 1, or (at your option)
|
|
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
|
|
21
|
|
22 ;;;###autoload
|
|
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.
|
|
29 With arg, turn abbrev mode on iff arg is positive.
|
|
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))
|
|
126 abbrevs)
|
|
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)
|
|
156 "Write all abbrev definitions to file of Lisp code.
|
|
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 (interactive "FWrite abbrev file: ")
|
|
165 (or (and file (> (length file) 0))
|
|
166 (setq file abbrev-file-name))
|
|
167 (save-excursion
|
|
168 (set-buffer (get-buffer-create " write-abbrev-file"))
|
|
169 (erase-buffer)
|
|
170 (let ((tables abbrev-table-name-list))
|
|
171 (while tables
|
|
172 (insert-abbrev-table-description (car tables) nil)
|
|
173 (setq tables (cdr tables))))
|
|
174 (write-region 1 (point-max) file)
|
|
175 (erase-buffer)))
|
|
176
|
|
177 (defun add-mode-abbrev (arg)
|
|
178 "Define mode-specific abbrev for last word(s) before point.
|
|
179 Argument is how many words before point form the expansion;
|
|
180 or zero means the region is the expansion.
|
|
181 A negative argument means to undefine the specified abbrev.
|
|
182 Reads the abbreviation in the minibuffer.
|
|
183
|
|
184 Don't use this function in a Lisp program; use `define-abbrev' instead."
|
|
185 (interactive "p")
|
|
186 (add-abbrev
|
|
187 (if only-global-abbrevs
|
|
188 global-abbrev-table
|
|
189 (or local-abbrev-table
|
|
190 (error "No per-mode abbrev table")))
|
|
191 "Mode" arg))
|
|
192
|
|
193 (defun add-global-abbrev (arg)
|
|
194 "Define global (all modes) abbrev for last word(s) before point.
|
|
195 The prefix argument specifies the number of words before point that form the
|
|
196 expansion; or zero means the region is the expansion.
|
|
197 A negative argument means to undefine the specified abbrev.
|
|
198 This command uses the minibuffer to read the abbreviation.
|
|
199
|
|
200 Don't use this function in a Lisp program; use `define-abbrev' instead."
|
|
201 (interactive "p")
|
|
202 (add-abbrev global-abbrev-table "Global" arg))
|
|
203
|
|
204 (defun add-abbrev (table type arg)
|
|
205 (let ((exp (and (>= arg 0)
|
|
206 (buffer-substring
|
|
207 (point)
|
|
208 (if (= arg 0) (mark)
|
|
209 (save-excursion (forward-word (- arg)) (point))))))
|
|
210 name)
|
|
211 (setq name
|
|
212 (read-string (format (if exp "%s abbrev for \"%s\": "
|
|
213 "Undefine %s abbrev: ")
|
|
214 type exp)))
|
|
215 (if (or (null exp)
|
|
216 (not (abbrev-expansion name table))
|
|
217 (y-or-n-p (format "%s expands to \"%s\"; redefine? "
|
|
218 name (abbrev-expansion name table))))
|
|
219 (define-abbrev table (downcase name) exp))))
|
|
220
|
|
221 (defun inverse-add-mode-abbrev (arg)
|
|
222 "Define last word before point as a mode-specific abbrev.
|
|
223 With prefix argument N, defines the Nth word before point.
|
|
224 This command uses the minibuffer to read the expansion.
|
|
225 Expands the abbreviation after defining it."
|
|
226 (interactive "p")
|
|
227 (inverse-add-abbrev
|
|
228 (if only-global-abbrevs
|
|
229 global-abbrev-table
|
|
230 (or local-abbrev-table
|
|
231 (error "No per-mode abbrev table")))
|
|
232 "Mode" arg))
|
|
233
|
|
234 (defun inverse-add-global-abbrev (arg)
|
|
235 "Define last word before point as a global (mode-independent) abbrev.
|
|
236 With prefix argument N, defines the Nth word before point.
|
|
237 This command uses the minibuffer to read the expansion.
|
|
238 Expands the abbreviation after defining it."
|
|
239 (interactive "p")
|
|
240 (inverse-add-abbrev global-abbrev-table "Global" arg))
|
|
241
|
|
242 (defun inverse-add-abbrev (table type arg)
|
|
243 (let (name nameloc exp)
|
|
244 (save-excursion
|
|
245 (forward-word (- arg))
|
|
246 (setq name (buffer-substring (point) (progn (forward-word 1)
|
|
247 (setq nameloc (point))))))
|
|
248 (setq exp (read-string (format "%s expansion for \"%s\": "
|
|
249 type name)))
|
|
250 (if (or (not (abbrev-expansion name table))
|
|
251 (y-or-n-p (format "%s expands to \"%s\"; redefine? "
|
|
252 name (abbrev-expansion name table))))
|
|
253 (progn
|
|
254 (define-abbrev table (downcase name) exp)
|
|
255 (save-excursion
|
|
256 (goto-char nameloc)
|
|
257 (expand-abbrev))))))
|
|
258
|
|
259 (defun abbrev-prefix-mark (&optional arg)
|
|
260 "Mark current point as the beginning of an abbrev.
|
|
261 Abbrev to be expanded starts here rather than at beginning of word.
|
|
262 This way, you can expand an abbrev with a prefix: insert the prefix,
|
|
263 use this command, then insert the abbrev."
|
|
264 (interactive "P")
|
|
265 (or arg (expand-abbrev))
|
|
266 (setq abbrev-start-location (point-marker)
|
|
267 abbrev-start-location-buffer (current-buffer))
|
|
268 (insert "-"))
|
|
269
|
|
270 (defun expand-region-abbrevs (start end &optional noquery)
|
|
271 "For abbrev occurrence in the region, offer to expand it.
|
|
272 The user is asked to type y or n for each occurrence.
|
|
273 A prefix argument means don't query; expand all abbrevs.
|
|
274 If called from a Lisp program, arguments are START END &optional NOQUERY."
|
|
275 (interactive "r\nP")
|
|
276 (save-excursion
|
|
277 (goto-char start)
|
|
278 (let ((lim (- (point-max) end))
|
|
279 pnt string)
|
|
280 (while (and (not (eobp))
|
|
281 (progn (forward-word 1)
|
|
282 (<= (setq pnt (point)) (- (point-max) lim))))
|
|
283 (if (abbrev-expansion
|
|
284 (setq string
|
|
285 (buffer-substring
|
|
286 (save-excursion (forward-word -1) (point))
|
|
287 pnt)))
|
|
288 (if (or noquery (y-or-n-p (format "Expand `%s'? " string)))
|
|
289 (expand-abbrev)))))))
|