xref: /386bsd/usr/local/lib/emacs/19.25/lisp/abbrev.el (revision a2142627)
1;;; abbrev.el --- abbrev mode commands for Emacs
2
3;; Copyright (C) 1985, 1986, 1987, 1992 Free Software Foundation, Inc.
4
5;; Keywords: abbrev
6
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
11;; the Free Software Foundation; either version 2, or (at your option)
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
23;;; Commentary:
24
25;; This facility is documented in the Emacs Manual.
26
27;;; Code:
28
29(defconst only-global-abbrevs nil "\
30*t means user plans to use global abbrevs only.
31Makes the commands to define mode-specific abbrevs define global ones instead.")
32
33(defun abbrev-mode (arg)
34  "Toggle abbrev mode.
35With argument ARG, turn abbrev mode on iff ARG is positive.
36In abbrev mode, inserting an abbreviation causes it to expand
37and 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.
62Mark 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.
100Selects a buffer containing a list of abbrev definitions.
101You can edit them and type \\<edit-abbrevs-map>\\[edit-abbrevs-redefine] to redefine abbrevs
102according to your editing.
103Buffer contains a header line for each abbrev table,
104 which is the abbrev table name in parentheses.
105This is followed by one line per abbrev in that table:
106NAME   USECOUNT   EXPANSION   HOOK
107where NAME and EXPANSION are strings with quotes,
108USECOUNT is an integer, and HOOK is any valid function
109or 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.
121See documentation of `edit-abbrevs' for info on the format of the
122text you must have in the buffer.
123With argument, eliminate all abbrev definitions except
124the 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))
132	    abbrevs name hook exp count)
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'.
145Optional argument FILE is the name of the file to read;
146it defaults to the value of `abbrev-file-name'.
147Optional 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.
155Optional argument FILE is the name of the file to read;
156it defaults to the value of `abbrev-file-name'.
157Does not print anything."
158  ;(interactive "fRead abbrev file: ")
159  (read-abbrev-file file t))
160
161(defun write-abbrev-file (file)
162  "Write all abbrev definitions to a file of Lisp code.
163The file written can be loaded in another session to define the same abbrevs.
164The 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.
184Argument is how many words before point form the expansion;
185or zero means the region is the expansion.
186A negative argument means to undefine the specified abbrev.
187Reads the abbreviation in the minibuffer.
188
189Don'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.
200The prefix argument specifies the number of words before point that form the
201expansion; or zero means the region is the expansion.
202A negative argument means to undefine the specified abbrev.
203This command uses the minibuffer to read the abbreviation.
204
205Don'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)))
220    (if (or (null exp)
221	    (not (abbrev-expansion name table))
222	    (y-or-n-p (format "%s expands to \"%s\"; redefine? "
223			      name (abbrev-expansion name table))))
224	(define-abbrev table (downcase name) exp))))
225
226(defun inverse-add-mode-abbrev (arg)
227  "Define last word before point as a mode-specific abbrev.
228With prefix argument N, defines the Nth word before point.
229This command uses the minibuffer to read the expansion.
230Expands the abbreviation after defining it."
231  (interactive "p")
232  (inverse-add-abbrev
233   (if only-global-abbrevs
234       global-abbrev-table
235     (or local-abbrev-table
236	 (error "No per-mode abbrev table")))
237   "Mode" arg))
238
239(defun inverse-add-global-abbrev (arg)
240  "Define last word before point as a global (mode-independent) abbrev.
241With prefix argument N, defines the Nth word before point.
242This command uses the minibuffer to read the expansion.
243Expands the abbreviation after defining it."
244  (interactive "p")
245  (inverse-add-abbrev global-abbrev-table "Global" arg))
246
247(defun inverse-add-abbrev (table type arg)
248  (let (name nameloc exp)
249    (save-excursion
250     (forward-word (- arg))
251     (setq name (buffer-substring (point) (progn (forward-word 1)
252					       (setq nameloc (point))))))
253    (setq exp (read-string (format "%s expansion for \"%s\": "
254				   type name)))
255    (if (or (not (abbrev-expansion name table))
256	    (y-or-n-p (format "%s expands to \"%s\"; redefine? "
257			      name (abbrev-expansion name table))))
258	(progn
259	 (define-abbrev table (downcase name) exp)
260	 (save-excursion
261	  (goto-char nameloc)
262	  (expand-abbrev))))))
263
264(defun abbrev-prefix-mark (&optional arg)
265  "Mark current point as the beginning of an abbrev.
266Abbrev to be expanded starts here rather than at beginning of word.
267This way, you can expand an abbrev with a prefix: insert the prefix,
268use this command, then insert the abbrev."
269  (interactive "P")
270  (or arg (expand-abbrev))
271  (setq abbrev-start-location (point-marker)
272	abbrev-start-location-buffer (current-buffer))
273  (insert "-"))
274
275(defun expand-region-abbrevs (start end &optional noquery)
276  "For abbrev occurrence in the region, offer to expand it.
277The user is asked to type y or n for each occurrence.
278A prefix argument means don't query; expand all abbrevs.
279If called from a Lisp program, arguments are START END &optional NOQUERY."
280  (interactive "r\nP")
281  (save-excursion
282    (goto-char start)
283    (let ((lim (- (point-max) end))
284	  pnt string)
285      (while (and (not (eobp))
286		  (progn (forward-word 1)
287			 (<= (setq pnt (point)) (- (point-max) lim))))
288	(if (abbrev-expansion
289	     (setq string
290		   (buffer-substring
291		    (save-excursion (forward-word -1) (point))
292		    pnt)))
293	    (if (or noquery (y-or-n-p (format "Expand `%s'? " string)))
294		(expand-abbrev)))))))
295
296;;; abbrev.el ends here
297