Fix last check-in.
[bpt/emacs.git] / lisp / subr.el
CommitLineData
c88ab9ce 1;;; subr.el --- basic lisp subroutines for Emacs
630cc463 2
b021ef18 3;; Copyright (C) 1985, 86, 92, 94, 95, 99, 2000 Free Software Foundation, Inc.
be9b65ac
DL
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
492878e4 9;; the Free Software Foundation; either version 2, or (at your option)
be9b65ac
DL
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
b578f267
EN
18;; along with GNU Emacs; see the file COPYING. If not, write to the
19;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20;; Boston, MA 02111-1307, USA.
be9b65ac 21
630cc463 22;;; Code:
77a5664f
RS
23(defvar custom-declare-variable-list nil
24 "Record `defcustom' calls made before `custom.el' is loaded to handle them.
25Each element of this list holds the arguments to one call to `defcustom'.")
26
68e3e5f5 27;; Use this, rather than defcustom, in subr.el and other files loaded
77a5664f
RS
28;; before custom.el.
29(defun custom-declare-variable-early (&rest arguments)
30 (setq custom-declare-variable-list
31 (cons arguments custom-declare-variable-list)))
9a5336ae
JB
32\f
33;;;; Lisp language features.
34
35(defmacro lambda (&rest cdr)
36 "Return a lambda expression.
37A call of the form (lambda ARGS DOCSTRING INTERACTIVE BODY) is
38self-quoting; the result of evaluating the lambda expression is the
39expression itself. The lambda expression may then be treated as a
bec0d7f9
RS
40function, i.e., stored as the function value of a symbol, passed to
41funcall or mapcar, etc.
42
9a5336ae 43ARGS should take the same form as an argument list for a `defun'.
8fd68088
RS
44DOCSTRING is an optional documentation string.
45 If present, it should describe how to call the function.
46 But documentation strings are usually not useful in nameless functions.
9a5336ae
JB
47INTERACTIVE should be a call to the function `interactive', which see.
48It may also be omitted.
49BODY should be a list of lisp expressions."
50 ;; Note that this definition should not use backquotes; subr.el should not
51 ;; depend on backquote.el.
52 (list 'function (cons 'lambda cdr)))
53
1be152fc 54(defmacro push (newelt listname)
fa65505b 55 "Add NEWELT to the list stored in the symbol LISTNAME.
1be152fc 56This is equivalent to (setq LISTNAME (cons NEWELT LISTNAME)).
d270117a 57LISTNAME must be a symbol."
22d85d00
DL
58 (list 'setq listname
59 (list 'cons newelt listname)))
d270117a
RS
60
61(defmacro pop (listname)
62 "Return the first element of LISTNAME's value, and remove it from the list.
63LISTNAME must be a symbol whose value is a list.
64If the value is nil, `pop' returns nil but does not actually
65change the list."
66 (list 'prog1 (list 'car listname)
67 (list 'setq listname (list 'cdr listname))))
68
debff3c3 69(defmacro when (cond &rest body)
b021ef18 70 "If COND yields non-nil, do BODY, else return nil."
debff3c3 71 (list 'if cond (cons 'progn body)))
9a5336ae 72
debff3c3 73(defmacro unless (cond &rest body)
b021ef18 74 "If COND yields nil, do BODY, else return nil."
debff3c3 75 (cons 'if (cons cond (cons nil body))))
d370591d 76
a0b0756a
RS
77(defmacro dolist (spec &rest body)
78 "(dolist (VAR LIST [RESULT]) BODY...): loop over a list.
79Evaluate BODY with VAR bound to each car from LIST, in turn.
80Then evaluate RESULT to get return value, default nil."
e4295aa1
RS
81 (let ((temp (make-symbol "--dolist-temp--")))
82 (list 'let (list (list temp (nth 1 spec)) (car spec))
83 (list 'while temp
84 (list 'setq (car spec) (list 'car temp))
85 (cons 'progn
86 (append body
87 (list (list 'setq temp (list 'cdr temp))))))
88 (if (cdr (cdr spec))
89 (cons 'progn
90 (cons (list 'setq (car spec) nil) (cdr (cdr spec))))))))
a0b0756a
RS
91
92(defmacro dotimes (spec &rest body)
93 "(dotimes (VAR COUNT [RESULT]) BODY...): loop a certain number of times.
94Evaluate BODY with VAR bound to successive integers running from 0,
95inclusive, to COUNT, exclusive. Then evaluate RESULT to get
96the return value (nil if RESULT is omitted)."
e4295aa1
RS
97 (let ((temp (make-symbol "--dotimes-temp--")))
98 (list 'let (list (list temp (nth 1 spec)) (list (car spec) 0))
99 (list 'while (list '< (car spec) temp)
100 (cons 'progn
101 (append body (list (list 'setq (car spec)
102 (list '1+ (car spec)))))))
103 (if (cdr (cdr spec))
104 (car (cdr (cdr spec)))
105 nil))))
a0b0756a 106
d370591d
RS
107(defsubst caar (x)
108 "Return the car of the car of X."
109 (car (car x)))
110
111(defsubst cadr (x)
112 "Return the car of the cdr of X."
113 (car (cdr x)))
114
115(defsubst cdar (x)
116 "Return the cdr of the car of X."
117 (cdr (car x)))
118
119(defsubst cddr (x)
120 "Return the cdr of the cdr of X."
121 (cdr (cdr x)))
e8c32c99 122
369fba5f
RS
123(defun last (x &optional n)
124 "Return the last link of the list X. Its car is the last element.
125If X is nil, return nil.
126If N is non-nil, return the Nth-to-last link of X.
127If N is bigger than the length of X, return X."
128 (if n
129 (let ((m 0) (p x))
130 (while (consp p)
131 (setq m (1+ m) p (cdr p)))
132 (if (<= n 0) p
133 (if (< n m) (nthcdr (- m n) x) x)))
134 (while (cdr x)
135 (setq x (cdr x)))
136 x))
526d204e 137
13157efc
GM
138(defun remove (elt seq)
139 "Return a copy of SEQ with all occurences of ELT removed.
140SEQ must be a list, vector, or string. The comparison is done with `equal'."
141 (if (nlistp seq)
142 ;; If SEQ isn't a list, there's no need to copy SEQ because
143 ;; `delete' will return a new object.
144 (delete elt seq)
145 (delete elt (copy-sequence seq))))
146
147(defun remq (elt list)
148 "Return a copy of LIST with all occurences of ELT removed.
149The comparison is done with `eq'."
150 (if (memq elt list)
151 (delq elt (copy-sequence list))
152 list))
153
8a288450
RS
154(defun assoc-default (key alist &optional test default)
155 "Find object KEY in a pseudo-alist ALIST.
156ALIST is a list of conses or objects. Each element (or the element's car,
157if it is a cons) is compared with KEY by evaluating (TEST (car elt) KEY).
158If that is non-nil, the element matches;
159then `assoc-default' returns the element's cdr, if it is a cons,
526d204e 160or DEFAULT if the element is not a cons.
8a288450
RS
161
162If no element matches, the value is nil.
163If TEST is omitted or nil, `equal' is used."
164 (let (found (tail alist) value)
165 (while (and tail (not found))
166 (let ((elt (car tail)))
167 (when (funcall (or test 'equal) (if (consp elt) (car elt) elt) key)
168 (setq found t value (if (consp elt) (cdr elt) default))))
169 (setq tail (cdr tail)))
170 value))
98aae5f6
KH
171
172(defun assoc-ignore-case (key alist)
173 "Like `assoc', but ignores differences in case and text representation.
174KEY must be a string. Upper-case and lower-case letters are treated as equal.
175Unibyte strings are converted to multibyte for comparison."
176 (let (element)
177 (while (and alist (not element))
178 (if (eq t (compare-strings key 0 nil (car (car alist)) 0 nil t))
179 (setq element (car alist)))
180 (setq alist (cdr alist)))
181 element))
182
183(defun assoc-ignore-representation (key alist)
184 "Like `assoc', but ignores differences in text representation.
185KEY must be a string.
186Unibyte strings are converted to multibyte for comparison."
187 (let (element)
188 (while (and alist (not element))
189 (if (eq t (compare-strings key 0 nil (car (car alist)) 0 nil))
190 (setq element (car alist)))
191 (setq alist (cdr alist)))
192 element))
cbbc3205
GM
193
194(defun member-ignore-case (elt list)
195 "Like `member', but ignores differences in case and text representation.
196ELT must be a string. Upper-case and lower-case letters are treated as equal.
197Unibyte strings are converted to multibyte for comparison."
242c13e8
MB
198 (while (and list (not (eq t (compare-strings elt 0 nil (car list) 0 nil t))))
199 (setq list (cdr list)))
200 list)
cbbc3205 201
9a5336ae 202\f
9a5336ae 203;;;; Keymap support.
be9b65ac
DL
204
205(defun undefined ()
206 (interactive)
207 (ding))
208
209;Prevent the \{...} documentation construct
210;from mentioning keys that run this command.
211(put 'undefined 'suppress-keymap t)
212
213(defun suppress-keymap (map &optional nodigits)
214 "Make MAP override all normally self-inserting keys to be undefined.
215Normally, as an exception, digits and minus-sign are set to make prefix args,
216but optional second arg NODIGITS non-nil treats them like other chars."
80e7b471 217 (substitute-key-definition 'self-insert-command 'undefined map global-map)
be9b65ac
DL
218 (or nodigits
219 (let (loop)
220 (define-key map "-" 'negative-argument)
221 ;; Make plain numbers do numeric args.
222 (setq loop ?0)
223 (while (<= loop ?9)
224 (define-key map (char-to-string loop) 'digit-argument)
225 (setq loop (1+ loop))))))
226
be9b65ac
DL
227;Moved to keymap.c
228;(defun copy-keymap (keymap)
229; "Return a copy of KEYMAP"
230; (while (not (keymapp keymap))
231; (setq keymap (signal 'wrong-type-argument (list 'keymapp keymap))))
232; (if (vectorp keymap)
233; (copy-sequence keymap)
234; (copy-alist keymap)))
235
f14dbba7
KH
236(defvar key-substitution-in-progress nil
237 "Used internally by substitute-key-definition.")
238
7f2c2edd 239(defun substitute-key-definition (olddef newdef keymap &optional oldmap prefix)
be9b65ac
DL
240 "Replace OLDDEF with NEWDEF for any keys in KEYMAP now defined as OLDDEF.
241In other words, OLDDEF is replaced with NEWDEF where ever it appears.
4656b314 242Alternatively, if optional fourth argument OLDMAP is specified, we redefine
ff77cf40 243in KEYMAP as NEWDEF those keys which are defined as OLDDEF in OLDMAP."
739f2672
GM
244 ;; Don't document PREFIX in the doc string because we don't want to
245 ;; advertise it. It's meant for recursive calls only. Here's its
246 ;; meaning
247
248 ;; If optional argument PREFIX is specified, it should be a key
249 ;; prefix, a string. Redefined bindings will then be bound to the
250 ;; original key, with PREFIX added at the front.
7f2c2edd
RS
251 (or prefix (setq prefix ""))
252 (let* ((scan (or oldmap keymap))
253 (vec1 (vector nil))
f14dbba7
KH
254 (prefix1 (vconcat prefix vec1))
255 (key-substitution-in-progress
256 (cons scan key-substitution-in-progress)))
7f2c2edd
RS
257 ;; Scan OLDMAP, finding each char or event-symbol that
258 ;; has any definition, and act on it with hack-key.
259 (while (consp scan)
260 (if (consp (car scan))
261 (let ((char (car (car scan)))
262 (defn (cdr (car scan))))
263 ;; The inside of this let duplicates exactly
264 ;; the inside of the following let that handles array elements.
265 (aset vec1 0 char)
266 (aset prefix1 (length prefix) char)
44d798af 267 (let (inner-def skipped)
7f2c2edd
RS
268 ;; Skip past menu-prompt.
269 (while (stringp (car-safe defn))
44d798af 270 (setq skipped (cons (car defn) skipped))
7f2c2edd 271 (setq defn (cdr defn)))
e025dddf
RS
272 ;; Skip past cached key-equivalence data for menu items.
273 (and (consp defn) (consp (car defn))
274 (setq defn (cdr defn)))
7f2c2edd 275 (setq inner-def defn)
e025dddf 276 ;; Look past a symbol that names a keymap.
7f2c2edd
RS
277 (while (and (symbolp inner-def)
278 (fboundp inner-def))
279 (setq inner-def (symbol-function inner-def)))
328a37ec
RS
280 (if (or (eq defn olddef)
281 ;; Compare with equal if definition is a key sequence.
282 ;; That is useful for operating on function-key-map.
283 (and (or (stringp defn) (vectorp defn))
284 (equal defn olddef)))
44d798af 285 (define-key keymap prefix1 (nconc (nreverse skipped) newdef))
f14dbba7 286 (if (and (keymapp defn)
350b7567
RS
287 ;; Avoid recursively scanning
288 ;; where KEYMAP does not have a submap.
afd9831b
RS
289 (let ((elt (lookup-key keymap prefix1)))
290 (or (null elt)
291 (keymapp elt)))
350b7567 292 ;; Avoid recursively rescanning keymap being scanned.
f14dbba7
KH
293 (not (memq inner-def
294 key-substitution-in-progress)))
e025dddf
RS
295 ;; If this one isn't being scanned already,
296 ;; scan it now.
7f2c2edd
RS
297 (substitute-key-definition olddef newdef keymap
298 inner-def
299 prefix1)))))
916cc49f 300 (if (vectorp (car scan))
7f2c2edd
RS
301 (let* ((array (car scan))
302 (len (length array))
303 (i 0))
304 (while (< i len)
305 (let ((char i) (defn (aref array i)))
306 ;; The inside of this let duplicates exactly
307 ;; the inside of the previous let.
308 (aset vec1 0 char)
309 (aset prefix1 (length prefix) char)
44d798af 310 (let (inner-def skipped)
7f2c2edd
RS
311 ;; Skip past menu-prompt.
312 (while (stringp (car-safe defn))
44d798af 313 (setq skipped (cons (car defn) skipped))
7f2c2edd 314 (setq defn (cdr defn)))
e025dddf
RS
315 (and (consp defn) (consp (car defn))
316 (setq defn (cdr defn)))
7f2c2edd
RS
317 (setq inner-def defn)
318 (while (and (symbolp inner-def)
319 (fboundp inner-def))
320 (setq inner-def (symbol-function inner-def)))
328a37ec
RS
321 (if (or (eq defn olddef)
322 (and (or (stringp defn) (vectorp defn))
323 (equal defn olddef)))
44d798af
RS
324 (define-key keymap prefix1
325 (nconc (nreverse skipped) newdef))
f14dbba7 326 (if (and (keymapp defn)
afd9831b
RS
327 (let ((elt (lookup-key keymap prefix1)))
328 (or (null elt)
329 (keymapp elt)))
f14dbba7
KH
330 (not (memq inner-def
331 key-substitution-in-progress)))
7f2c2edd
RS
332 (substitute-key-definition olddef newdef keymap
333 inner-def
334 prefix1)))))
97fd9abf
RS
335 (setq i (1+ i))))
336 (if (char-table-p (car scan))
337 (map-char-table
338 (function (lambda (char defn)
339 (let ()
340 ;; The inside of this let duplicates exactly
341 ;; the inside of the previous let,
342 ;; except that it uses set-char-table-range
343 ;; instead of define-key.
344 (aset vec1 0 char)
345 (aset prefix1 (length prefix) char)
346 (let (inner-def skipped)
347 ;; Skip past menu-prompt.
348 (while (stringp (car-safe defn))
349 (setq skipped (cons (car defn) skipped))
350 (setq defn (cdr defn)))
351 (and (consp defn) (consp (car defn))
352 (setq defn (cdr defn)))
353 (setq inner-def defn)
354 (while (and (symbolp inner-def)
355 (fboundp inner-def))
356 (setq inner-def (symbol-function inner-def)))
357 (if (or (eq defn olddef)
358 (and (or (stringp defn) (vectorp defn))
359 (equal defn olddef)))
9a5114ac
RS
360 (define-key keymap prefix1
361 (nconc (nreverse skipped) newdef))
97fd9abf
RS
362 (if (and (keymapp defn)
363 (let ((elt (lookup-key keymap prefix1)))
364 (or (null elt)
365 (keymapp elt)))
366 (not (memq inner-def
367 key-substitution-in-progress)))
368 (substitute-key-definition olddef newdef keymap
369 inner-def
370 prefix1)))))))
371 (car scan)))))
7f2c2edd 372 (setq scan (cdr scan)))))
9a5336ae 373
4ced66fd 374(defun define-key-after (keymap key definition &optional after)
4434d61b
RS
375 "Add binding in KEYMAP for KEY => DEFINITION, right after AFTER's binding.
376This is like `define-key' except that the binding for KEY is placed
377just after the binding for the event AFTER, instead of at the beginning
c34a9d34
RS
378of the map. Note that AFTER must be an event type (like KEY), NOT a command
379\(like DEFINITION).
380
4ced66fd 381If AFTER is t or omitted, the new binding goes at the end of the keymap.
c34a9d34 382
4ced66fd
DL
383KEY must contain just one event type--that is to say, it must be a
384string or vector of length 1, but AFTER should be a single event
385type--a symbol or a character, not a sequence.
c34a9d34 386
4ced66fd 387Bindings are always added before any inherited map.
c34a9d34 388
4ced66fd
DL
389The order of bindings in a keymap matters when it is used as a menu."
390 (unless after (setq after t))
4434d61b
RS
391 (or (keymapp keymap)
392 (signal 'wrong-type-argument (list 'keymapp keymap)))
ab375e6c 393 (if (> (length key) 1)
626f67f3 394 (error "multi-event key specified in `define-key-after'"))
113d28a8 395 (let ((tail keymap) done inserted
4434d61b
RS
396 (first (aref key 0)))
397 (while (and (not done) tail)
398 ;; Delete any earlier bindings for the same key.
399 (if (eq (car-safe (car (cdr tail))) first)
400 (setcdr tail (cdr (cdr tail))))
401 ;; When we reach AFTER's binding, insert the new binding after.
402 ;; If we reach an inherited keymap, insert just before that.
113d28a8 403 ;; If we reach the end of this keymap, insert at the end.
c34a9d34
RS
404 (if (or (and (eq (car-safe (car tail)) after)
405 (not (eq after t)))
113d28a8
RS
406 (eq (car (cdr tail)) 'keymap)
407 (null (cdr tail)))
4434d61b 408 (progn
113d28a8
RS
409 ;; Stop the scan only if we find a parent keymap.
410 ;; Keep going past the inserted element
411 ;; so we can delete any duplications that come later.
412 (if (eq (car (cdr tail)) 'keymap)
413 (setq done t))
414 ;; Don't insert more than once.
415 (or inserted
416 (setcdr tail (cons (cons (aref key 0) definition) (cdr tail))))
417 (setq inserted t)))
4434d61b
RS
418 (setq tail (cdr tail)))))
419
d128fe85
RS
420(defmacro kbd (keys)
421 "Convert KEYS to the internal Emacs key representation.
422KEYS should be a string constant in the format used for
423saving keyboard macros (see `insert-kbd-macro')."
424 (read-kbd-macro keys))
425
8bed5e3d
RS
426(put 'keyboard-translate-table 'char-table-extra-slots 0)
427
9a5336ae
JB
428(defun keyboard-translate (from to)
429 "Translate character FROM to TO at a low level.
430This function creates a `keyboard-translate-table' if necessary
431and then modifies one entry in it."
8bed5e3d
RS
432 (or (char-table-p keyboard-translate-table)
433 (setq keyboard-translate-table
434 (make-char-table 'keyboard-translate-table nil)))
9a5336ae
JB
435 (aset keyboard-translate-table from to))
436
437\f
438;;;; The global keymap tree.
439
440;;; global-map, esc-map, and ctl-x-map have their values set up in
441;;; keymap.c; we just give them docstrings here.
442
443(defvar global-map nil
444 "Default global keymap mapping Emacs keyboard input into commands.
445The value is a keymap which is usually (but not necessarily) Emacs's
446global map.")
447
448(defvar esc-map nil
449 "Default keymap for ESC (meta) commands.
450The normal global definition of the character ESC indirects to this keymap.")
451
452(defvar ctl-x-map nil
453 "Default keymap for C-x commands.
454The normal global definition of the character C-x indirects to this keymap.")
455
456(defvar ctl-x-4-map (make-sparse-keymap)
457 "Keymap for subcommands of C-x 4")
059184dd 458(defalias 'ctl-x-4-prefix ctl-x-4-map)
9a5336ae
JB
459(define-key ctl-x-map "4" 'ctl-x-4-prefix)
460
461(defvar ctl-x-5-map (make-sparse-keymap)
462 "Keymap for frame commands.")
059184dd 463(defalias 'ctl-x-5-prefix ctl-x-5-map)
9a5336ae
JB
464(define-key ctl-x-map "5" 'ctl-x-5-prefix)
465
0f03054a 466\f
9a5336ae
JB
467;;;; Event manipulation functions.
468
da16e648
KH
469;; The call to `read' is to ensure that the value is computed at load time
470;; and not compiled into the .elc file. The value is negative on most
471;; machines, but not on all!
472(defconst listify-key-sequence-1 (logior 128 (read "?\\M-\\^@")))
114137b8 473
cde6d7e3
RS
474(defun listify-key-sequence (key)
475 "Convert a key sequence to a list of events."
476 (if (vectorp key)
477 (append key nil)
478 (mapcar (function (lambda (c)
479 (if (> c 127)
114137b8 480 (logxor c listify-key-sequence-1)
cde6d7e3
RS
481 c)))
482 (append key nil))))
483
53e5a4e8
RS
484(defsubst eventp (obj)
485 "True if the argument is an event object."
486 (or (integerp obj)
487 (and (symbolp obj)
488 (get obj 'event-symbol-elements))
489 (and (consp obj)
490 (symbolp (car obj))
491 (get (car obj) 'event-symbol-elements))))
492
493(defun event-modifiers (event)
494 "Returns a list of symbols representing the modifier keys in event EVENT.
495The elements of the list may include `meta', `control',
32295976
RS
496`shift', `hyper', `super', `alt', `click', `double', `triple', `drag',
497and `down'."
53e5a4e8
RS
498 (let ((type event))
499 (if (listp type)
500 (setq type (car type)))
501 (if (symbolp type)
502 (cdr (get type 'event-symbol-elements))
503 (let ((list nil))
da16e648 504 (or (zerop (logand type ?\M-\^@))
53e5a4e8 505 (setq list (cons 'meta list)))
da16e648 506 (or (and (zerop (logand type ?\C-\^@))
53e5a4e8
RS
507 (>= (logand type 127) 32))
508 (setq list (cons 'control list)))
da16e648 509 (or (and (zerop (logand type ?\S-\^@))
53e5a4e8
RS
510 (= (logand type 255) (downcase (logand type 255))))
511 (setq list (cons 'shift list)))
da16e648 512 (or (zerop (logand type ?\H-\^@))
53e5a4e8 513 (setq list (cons 'hyper list)))
da16e648 514 (or (zerop (logand type ?\s-\^@))
53e5a4e8 515 (setq list (cons 'super list)))
da16e648 516 (or (zerop (logand type ?\A-\^@))
53e5a4e8
RS
517 (setq list (cons 'alt list)))
518 list))))
519
d63de416
RS
520(defun event-basic-type (event)
521 "Returns the basic type of the given event (all modifiers removed).
522The value is an ASCII printing character (not upper case) or a symbol."
2b0f4ba5
JB
523 (if (consp event)
524 (setq event (car event)))
d63de416
RS
525 (if (symbolp event)
526 (car (get event 'event-symbol-elements))
527 (let ((base (logand event (1- (lsh 1 18)))))
528 (downcase (if (< base 32) (logior base 64) base)))))
529
0f03054a
RS
530(defsubst mouse-movement-p (object)
531 "Return non-nil if OBJECT is a mouse movement event."
532 (and (consp object)
533 (eq (car object) 'mouse-movement)))
534
535(defsubst event-start (event)
536 "Return the starting position of EVENT.
537If EVENT is a mouse press or a mouse click, this returns the location
538of the event.
539If EVENT is a drag, this returns the drag's starting position.
540The return value is of the form
e55c21be 541 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
0f03054a
RS
542The `posn-' functions access elements of such lists."
543 (nth 1 event))
544
545(defsubst event-end (event)
546 "Return the ending location of EVENT. EVENT should be a click or drag event.
547If EVENT is a click event, this function is the same as `event-start'.
548The return value is of the form
e55c21be 549 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
0f03054a 550The `posn-' functions access elements of such lists."
69b95560 551 (nth (if (consp (nth 2 event)) 2 1) event))
0f03054a 552
32295976
RS
553(defsubst event-click-count (event)
554 "Return the multi-click count of EVENT, a click or drag event.
555The return value is a positive integer."
556 (if (integerp (nth 2 event)) (nth 2 event) 1))
557
0f03054a
RS
558(defsubst posn-window (position)
559 "Return the window in POSITION.
560POSITION should be a list of the form
e55c21be 561 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
0f03054a
RS
562as returned by the `event-start' and `event-end' functions."
563 (nth 0 position))
564
565(defsubst posn-point (position)
566 "Return the buffer location in POSITION.
567POSITION should be a list of the form
e55c21be 568 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
0f03054a 569as returned by the `event-start' and `event-end' functions."
15db4e0e
JB
570 (if (consp (nth 1 position))
571 (car (nth 1 position))
572 (nth 1 position)))
0f03054a 573
e55c21be
RS
574(defsubst posn-x-y (position)
575 "Return the x and y coordinates in POSITION.
0f03054a 576POSITION should be a list of the form
e55c21be 577 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
0f03054a
RS
578as returned by the `event-start' and `event-end' functions."
579 (nth 2 position))
580
ed627e08 581(defun posn-col-row (position)
dbbcac56 582 "Return the column and row in POSITION, measured in characters.
e55c21be
RS
583POSITION should be a list of the form
584 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
ed627e08
RS
585as returned by the `event-start' and `event-end' functions.
586For a scroll-bar event, the result column is 0, and the row
587corresponds to the vertical position of the click in the scroll bar."
588 (let ((pair (nth 2 position))
589 (window (posn-window position)))
dbbcac56
KH
590 (if (eq (if (consp (nth 1 position))
591 (car (nth 1 position))
592 (nth 1 position))
ed627e08
RS
593 'vertical-scroll-bar)
594 (cons 0 (scroll-bar-scale pair (1- (window-height window))))
dbbcac56
KH
595 (if (eq (if (consp (nth 1 position))
596 (car (nth 1 position))
597 (nth 1 position))
ed627e08
RS
598 'horizontal-scroll-bar)
599 (cons (scroll-bar-scale pair (window-width window)) 0)
9ba60df9
RS
600 (let* ((frame (if (framep window) window (window-frame window)))
601 (x (/ (car pair) (frame-char-width frame)))
602 (y (/ (cdr pair) (frame-char-height frame))))
ed627e08 603 (cons x y))))))
e55c21be 604
0f03054a
RS
605(defsubst posn-timestamp (position)
606 "Return the timestamp of POSITION.
607POSITION should be a list of the form
e55c21be 608 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
f415c00c 609as returned by the `event-start' and `event-end' functions."
0f03054a 610 (nth 3 position))
9a5336ae 611
0f03054a 612\f
9a5336ae
JB
613;;;; Obsolescent names for functions.
614
059184dd
ER
615(defalias 'dot 'point)
616(defalias 'dot-marker 'point-marker)
617(defalias 'dot-min 'point-min)
618(defalias 'dot-max 'point-max)
619(defalias 'window-dot 'window-point)
620(defalias 'set-window-dot 'set-window-point)
621(defalias 'read-input 'read-string)
622(defalias 'send-string 'process-send-string)
623(defalias 'send-region 'process-send-region)
624(defalias 'show-buffer 'set-window-buffer)
625(defalias 'buffer-flush-undo 'buffer-disable-undo)
626(defalias 'eval-current-buffer 'eval-buffer)
627(defalias 'compiled-function-p 'byte-code-function-p)
ae1cc031 628(defalias 'define-function 'defalias)
be9b65ac 629
0cba3a0f 630(defalias 'sref 'aref)
2598a293
SM
631(make-obsolete 'sref 'aref "20.4")
632(make-obsolete 'char-bytes "Now this function always returns 1" "20.4")
6bb762b3 633
9a5336ae
JB
634;; Some programs still use this as a function.
635(defun baud-rate ()
bcacc42c
RS
636 "Obsolete function returning the value of the `baud-rate' variable.
637Please convert your programs to use the variable `baud-rate' directly."
9a5336ae
JB
638 baud-rate)
639
0a5c0893
MB
640(defalias 'focus-frame 'ignore)
641(defalias 'unfocus-frame 'ignore)
9a5336ae
JB
642\f
643;;;; Alternate names for functions - these are not being phased out.
644
059184dd
ER
645(defalias 'string= 'string-equal)
646(defalias 'string< 'string-lessp)
647(defalias 'move-marker 'set-marker)
059184dd
ER
648(defalias 'not 'null)
649(defalias 'rplaca 'setcar)
650(defalias 'rplacd 'setcdr)
eb8c3be9 651(defalias 'beep 'ding) ;preserve lingual purity
059184dd
ER
652(defalias 'indent-to-column 'indent-to)
653(defalias 'backward-delete-char 'delete-backward-char)
654(defalias 'search-forward-regexp (symbol-function 're-search-forward))
655(defalias 'search-backward-regexp (symbol-function 're-search-backward))
656(defalias 'int-to-string 'number-to-string)
024ae2c6 657(defalias 'store-match-data 'set-match-data)
d6c22d46 658;; These are the XEmacs names:
475fb2fb
KH
659(defalias 'point-at-eol 'line-end-position)
660(defalias 'point-at-bol 'line-beginning-position)
37f6661a
JB
661
662;;; Should this be an obsolete name? If you decide it should, you get
663;;; to go through all the sources and change them.
059184dd 664(defalias 'string-to-int 'string-to-number)
be9b65ac 665\f
9a5336ae 666;;;; Hook manipulation functions.
be9b65ac 667
0e4d378b
RS
668(defun make-local-hook (hook)
669 "Make the hook HOOK local to the current buffer.
71c78f01
RS
670The return value is HOOK.
671
c344cf32
SM
672You never need to call this function now that `add-hook' does it for you
673if its LOCAL argument is non-nil.
674
0e4d378b
RS
675When a hook is local, its local and global values
676work in concert: running the hook actually runs all the hook
677functions listed in *either* the local value *or* the global value
678of the hook variable.
679
7dd1926e
RS
680This function works by making `t' a member of the buffer-local value,
681which acts as a flag to run the hook functions in the default value as
682well. This works for all normal hooks, but does not work for most
683non-normal hooks yet. We will be changing the callers of non-normal
684hooks so that they can handle localness; this has to be done one by
685one.
686
687This function does nothing if HOOK is already local in the current
688buffer.
0e4d378b
RS
689
690Do not use `make-local-variable' to make a hook variable buffer-local."
691 (if (local-variable-p hook)
692 nil
693 (or (boundp hook) (set hook nil))
694 (make-local-variable hook)
71c78f01
RS
695 (set hook (list t)))
696 hook)
0e4d378b
RS
697
698(defun add-hook (hook function &optional append local)
32295976
RS
699 "Add to the value of HOOK the function FUNCTION.
700FUNCTION is not added if already present.
701FUNCTION is added (if necessary) at the beginning of the hook list
702unless the optional argument APPEND is non-nil, in which case
703FUNCTION is added at the end.
704
0e4d378b
RS
705The optional fourth argument, LOCAL, if non-nil, says to modify
706the hook's buffer-local value rather than its default value.
8947a5e2 707This makes the hook buffer-local if needed.
0e4d378b
RS
708To make a hook variable buffer-local, always use
709`make-local-hook', not `make-local-variable'.
710
32295976
RS
711HOOK should be a symbol, and FUNCTION may be any valid function. If
712HOOK is void, it is first set to nil. If HOOK's value is a single
aa09b5ca 713function, it is changed to a list of functions."
be9b65ac 714 (or (boundp hook) (set hook nil))
0e4d378b 715 (or (default-boundp hook) (set-default hook nil))
79372165 716 (if local (unless (local-variable-if-set-p hook) (make-local-hook hook))
8947a5e2
SM
717 ;; Detect the case where make-local-variable was used on a hook
718 ;; and do what we used to do.
719 (unless (and (consp (symbol-value hook)) (memq t (symbol-value hook)))
720 (setq local t)))
721 (let ((hook-value (if local (symbol-value hook) (default-value hook))))
722 ;; If the hook value is a single function, turn it into a list.
723 (when (or (not (listp hook-value)) (eq (car hook-value) 'lambda))
2248c40d 724 (setq hook-value (list hook-value)))
8947a5e2
SM
725 ;; Do the actual addition if necessary
726 (unless (member function hook-value)
727 (setq hook-value
728 (if append
729 (append hook-value (list function))
730 (cons function hook-value))))
731 ;; Set the actual variable
732 (if local (set hook hook-value) (set-default hook hook-value))))
0e4d378b
RS
733
734(defun remove-hook (hook function &optional local)
24980d16
RS
735 "Remove from the value of HOOK the function FUNCTION.
736HOOK should be a symbol, and FUNCTION may be any valid function. If
737FUNCTION isn't the value of HOOK, or, if FUNCTION doesn't appear in the
0e4d378b
RS
738list of hooks to run in HOOK, then nothing is done. See `add-hook'.
739
740The optional third argument, LOCAL, if non-nil, says to modify
741the hook's buffer-local value rather than its default value.
8947a5e2 742This makes the hook buffer-local if needed.
0e4d378b
RS
743To make a hook variable buffer-local, always use
744`make-local-hook', not `make-local-variable'."
8947a5e2
SM
745 (or (boundp hook) (set hook nil))
746 (or (default-boundp hook) (set-default hook nil))
79372165 747 (if local (unless (local-variable-if-set-p hook) (make-local-hook hook))
8947a5e2
SM
748 ;; Detect the case where make-local-variable was used on a hook
749 ;; and do what we used to do.
750 (unless (and (consp (symbol-value hook)) (memq t (symbol-value hook)))
751 (setq local t)))
752 (let ((hook-value (if local (symbol-value hook) (default-value hook))))
e4da9c1c
SM
753 ;; Remove the function, for both the list and the non-list cases.
754 (if (or (not (listp hook-value)) (eq (car hook-value) 'lambda))
755 (if (equal hook-value function) (setq hook-value nil))
756 (setq hook-value (delete function (copy-sequence hook-value))))
8947a5e2
SM
757 ;; If the function is on the global hook, we need to shadow it locally
758 ;;(when (and local (member function (default-value hook))
759 ;; (not (member (cons 'not function) hook-value)))
760 ;; (push (cons 'not function) hook-value))
761 ;; Set the actual variable
762 (if local (set hook hook-value) (set-default hook hook-value))))
6e3af630 763
c8bfa689 764(defun add-to-list (list-var element &optional append)
8851c1f0 765 "Add to the value of LIST-VAR the element ELEMENT if it isn't there yet.
9f0b1f09 766The test for presence of ELEMENT is done with `equal'.
c8bfa689
MB
767If ELEMENT is added, it is added at the beginning of the list,
768unless the optional argument APPEND is non-nil, in which case
769ELEMENT is added at the end.
508bcbca 770
8851c1f0
RS
771If you want to use `add-to-list' on a variable that is not defined
772until a certain package is loaded, you should put the call to `add-to-list'
773into a hook function that will be run only after loading the package.
774`eval-after-load' provides one way to do this. In some cases
775other hooks, such as major mode hooks, can do the job."
15171a06
KH
776 (if (member element (symbol-value list-var))
777 (symbol-value list-var)
c8bfa689
MB
778 (set list-var
779 (if append
780 (append (symbol-value list-var) (list element))
781 (cons element (symbol-value list-var))))))
be9b65ac 782\f
9a5336ae
JB
783;;;; Specifying things to do after certain files are loaded.
784
785(defun eval-after-load (file form)
786 "Arrange that, if FILE is ever loaded, FORM will be run at that time.
787This makes or adds to an entry on `after-load-alist'.
90914938 788If FILE is already loaded, evaluate FORM right now.
12c7071c 789It does nothing if FORM is already on the list for FILE.
9a5336ae 790FILE should be the name of a library, with no directory name."
90914938 791 ;; Make sure there is an element for FILE.
9a5336ae
JB
792 (or (assoc file after-load-alist)
793 (setq after-load-alist (cons (list file) after-load-alist)))
90914938 794 ;; Add FORM to the element if it isn't there.
12c7071c
RS
795 (let ((elt (assoc file after-load-alist)))
796 (or (member form (cdr elt))
90914938
RS
797 (progn
798 (nconc elt (list form))
799 ;; If the file has been loaded already, run FORM right away.
800 (and (assoc file load-history)
801 (eval form)))))
9a5336ae
JB
802 form)
803
804(defun eval-next-after-load (file)
805 "Read the following input sexp, and run it whenever FILE is loaded.
806This makes or adds to an entry on `after-load-alist'.
807FILE should be the name of a library, with no directory name."
808 (eval-after-load file (read)))
809
810\f
811;;;; Input and display facilities.
812
77a5664f 813(defvar read-quoted-char-radix 8
1ba764de 814 "*Radix for \\[quoted-insert] and other uses of `read-quoted-char'.
77a5664f
RS
815Legitimate radix values are 8, 10 and 16.")
816
817(custom-declare-variable-early
818 'read-quoted-char-radix 8
819 "*Radix for \\[quoted-insert] and other uses of `read-quoted-char'.
1ba764de
RS
820Legitimate radix values are 8, 10 and 16."
821 :type '(choice (const 8) (const 10) (const 16))
822 :group 'editing-basics)
823
9a5336ae 824(defun read-quoted-char (&optional prompt)
2444730b
RS
825 "Like `read-char', but do not allow quitting.
826Also, if the first character read is an octal digit,
827we read any number of octal digits and return the
569b03f2 828specified character code. Any nondigit terminates the sequence.
1ba764de 829If the terminator is RET, it is discarded;
2444730b
RS
830any other terminator is used itself as input.
831
569b03f2
RS
832The optional argument PROMPT specifies a string to use to prompt the user.
833The variable `read-quoted-char-radix' controls which radix to use
834for numeric input."
2444730b
RS
835 (let ((message-log-max nil) done (first t) (code 0) char)
836 (while (not done)
837 (let ((inhibit-quit first)
42e636f0
KH
838 ;; Don't let C-h get the help message--only help function keys.
839 (help-char nil)
840 (help-form
841 "Type the special character you want to use,
2444730b 842or the octal character code.
1ba764de 843RET terminates the character code and is discarded;
2444730b 844any other non-digit terminates the character code and is then used as input."))
b7de4d62 845 (setq char (read-event (and prompt (format "%s-" prompt)) t))
9a5336ae 846 (if inhibit-quit (setq quit-flag nil)))
4867f7b2
RS
847 ;; Translate TAB key into control-I ASCII character, and so on.
848 (and char
849 (let ((translated (lookup-key function-key-map (vector char))))
bf896a1b 850 (if (arrayp translated)
4867f7b2 851 (setq char (aref translated 0)))))
9a5336ae 852 (cond ((null char))
1ba764de
RS
853 ((not (integerp char))
854 (setq unread-command-events (list char)
855 done t))
bf896a1b
RS
856 ((/= (logand char ?\M-\^@) 0)
857 ;; Turn a meta-character into a character with the 0200 bit set.
858 (setq code (logior (logand char (lognot ?\M-\^@)) 128)
859 done t))
1ba764de
RS
860 ((and (<= ?0 char) (< char (+ ?0 (min 10 read-quoted-char-radix))))
861 (setq code (+ (* code read-quoted-char-radix) (- char ?0)))
862 (and prompt (setq prompt (message "%s %c" prompt char))))
863 ((and (<= ?a (downcase char))
864 (< (downcase char) (+ ?a -10 (min 26 read-quoted-char-radix))))
92304bc8
RS
865 (setq code (+ (* code read-quoted-char-radix)
866 (+ 10 (- (downcase char) ?a))))
91a6acc3 867 (and prompt (setq prompt (message "%s %c" prompt char))))
1ba764de 868 ((and (not first) (eq char ?\C-m))
2444730b
RS
869 (setq done t))
870 ((not first)
871 (setq unread-command-events (list char)
872 done t))
873 (t (setq code char
874 done t)))
875 (setq first nil))
bf896a1b 876 code))
9a5336ae 877
44071d6b
RS
878(defun read-passwd (prompt &optional confirm default)
879 "Read a password, prompting with PROMPT. Echo `.' for each character typed.
e0e4cb7a 880End with RET, LFD, or ESC. DEL or C-h rubs out. C-u kills line.
44071d6b
RS
881Optional argument CONFIRM, if non-nil, then read it twice to make sure.
882Optional DEFAULT is a default password to use instead of empty input."
883 (if confirm
884 (let (success)
885 (while (not success)
886 (let ((first (read-passwd prompt nil default))
887 (second (read-passwd "Confirm password: " nil default)))
888 (if (equal first second)
889 (setq success first)
890 (message "Password not repeated accurately; please start over")
891 (sit-for 1))))
892 success)
893 (let ((pass nil)
894 (c 0)
895 (echo-keystrokes 0)
896 (cursor-in-echo-area t))
897 (while (progn (message "%s%s"
898 prompt
899 (make-string (length pass) ?.))
42ccb7c8 900 (setq c (read-char-exclusive nil t))
44071d6b
RS
901 (and (/= c ?\r) (/= c ?\n) (/= c ?\e)))
902 (if (= c ?\C-u)
903 (setq pass "")
904 (if (and (/= c ?\b) (/= c ?\177))
905 (setq pass (concat pass (char-to-string c)))
906 (if (> (length pass) 0)
907 (setq pass (substring pass 0 -1))))))
b021ef18 908 (clear-this-command-keys)
44071d6b
RS
909 (message nil)
910 (or pass default ""))))
e0e4cb7a 911\f
9a5336ae
JB
912(defun force-mode-line-update (&optional all)
913 "Force the mode-line of the current buffer to be redisplayed.
7ec2a18c 914With optional non-nil ALL, force redisplay of all mode-lines."
9a5336ae
JB
915 (if all (save-excursion (set-buffer (other-buffer))))
916 (set-buffer-modified-p (buffer-modified-p)))
917
be9b65ac
DL
918(defun momentary-string-display (string pos &optional exit-char message)
919 "Momentarily display STRING in the buffer at POS.
920Display remains until next character is typed.
921If the char is EXIT-CHAR (optional third arg, default is SPC) it is swallowed;
922otherwise it is then available as input (as a command if nothing else).
923Display MESSAGE (optional fourth arg) in the echo area.
924If MESSAGE is nil, instructions to type EXIT-CHAR are displayed there."
925 (or exit-char (setq exit-char ?\ ))
c306e0e0 926 (let ((inhibit-read-only t)
ca2ec1c5
RS
927 ;; Don't modify the undo list at all.
928 (buffer-undo-list t)
be9b65ac
DL
929 (modified (buffer-modified-p))
930 (name buffer-file-name)
931 insert-end)
932 (unwind-protect
933 (progn
934 (save-excursion
935 (goto-char pos)
936 ;; defeat file locking... don't try this at home, kids!
937 (setq buffer-file-name nil)
938 (insert-before-markers string)
3eec84bf
RS
939 (setq insert-end (point))
940 ;; If the message end is off screen, recenter now.
024ae2c6 941 (if (< (window-end nil t) insert-end)
3eec84bf
RS
942 (recenter (/ (window-height) 2)))
943 ;; If that pushed message start off the screen,
944 ;; scroll to start it at the top of the screen.
945 (move-to-window-line 0)
946 (if (> (point) pos)
947 (progn
948 (goto-char pos)
949 (recenter 0))))
be9b65ac
DL
950 (message (or message "Type %s to continue editing.")
951 (single-key-description exit-char))
3547c855 952 (let ((char (read-event)))
be9b65ac 953 (or (eq char exit-char)
dbc4e1c1 954 (setq unread-command-events (list char)))))
be9b65ac
DL
955 (if insert-end
956 (save-excursion
957 (delete-region pos insert-end)))
958 (setq buffer-file-name name)
959 (set-buffer-modified-p modified))))
960
9a5336ae
JB
961\f
962;;;; Miscellanea.
963
448b61c9
RS
964;; A number of major modes set this locally.
965;; Give it a global value to avoid compiler warnings.
966(defvar font-lock-defaults nil)
967
4fb17037
RS
968(defvar suspend-hook nil
969 "Normal hook run by `suspend-emacs', before suspending.")
970
971(defvar suspend-resume-hook nil
972 "Normal hook run by `suspend-emacs', after Emacs is continued.")
973
448b61c9
RS
974;; Avoid compiler warnings about this variable,
975;; which has a special meaning on certain system types.
976(defvar buffer-file-type nil
977 "Non-nil if the visited file is a binary file.
978This variable is meaningful on MS-DOG and Windows NT.
979On those systems, it is automatically local in every buffer.
980On other systems, this variable is normally always nil.")
981
a860d25f 982;; This should probably be written in C (i.e., without using `walk-windows').
63503b24 983(defun get-buffer-window-list (buffer &optional minibuf frame)
a860d25f 984 "Return windows currently displaying BUFFER, or nil if none.
63503b24 985See `walk-windows' for the meaning of MINIBUF and FRAME."
43c5ac8c 986 (let ((buffer (if (bufferp buffer) buffer (get-buffer buffer))) windows)
a860d25f
SM
987 (walk-windows (function (lambda (window)
988 (if (eq (window-buffer window) buffer)
989 (setq windows (cons window windows)))))
63503b24 990 minibuf frame)
a860d25f
SM
991 windows))
992
f9269e19
RS
993(defun ignore (&rest ignore)
994 "Do nothing and return nil.
995This function accepts any number of arguments, but ignores them."
c0f1a4f6 996 (interactive)
9a5336ae
JB
997 nil)
998
999(defun error (&rest args)
aa308ce2
RS
1000 "Signal an error, making error message by passing all args to `format'.
1001In Emacs, the convention is that error messages start with a capital
1002letter but *do not* end with a period. Please follow this convention
1003for the sake of consistency."
9a5336ae
JB
1004 (while t
1005 (signal 'error (list (apply 'format args)))))
1006
cef7ae6e 1007(defalias 'user-original-login-name 'user-login-name)
9a5336ae 1008
be9b65ac
DL
1009(defun start-process-shell-command (name buffer &rest args)
1010 "Start a program in a subprocess. Return the process object for it.
1011Args are NAME BUFFER COMMAND &rest COMMAND-ARGS.
1012NAME is name for process. It is modified if necessary to make it unique.
1013BUFFER is the buffer or (buffer-name) to associate with the process.
1014 Process output goes at end of that buffer, unless you specify
1015 an output stream or filter function to handle the output.
1016 BUFFER may be also nil, meaning that this process is not associated
1017 with any buffer
1018Third arg is command name, the name of a shell command.
1019Remaining arguments are the arguments for the command.
4f1d6310 1020Wildcards and redirection are handled as usual in the shell."
a247bf21
KH
1021 (cond
1022 ((eq system-type 'vax-vms)
1023 (apply 'start-process name buffer args))
b59f6d7a
RS
1024 ;; We used to use `exec' to replace the shell with the command,
1025 ;; but that failed to handle (...) and semicolon, etc.
a247bf21
KH
1026 (t
1027 (start-process name buffer shell-file-name shell-command-switch
b59f6d7a 1028 (mapconcat 'identity args " ")))))
a7ed4c2a 1029\f
a7f284ec
RS
1030(defmacro with-current-buffer (buffer &rest body)
1031 "Execute the forms in BODY with BUFFER as the current buffer.
a2fdb55c
EN
1032The value returned is the value of the last form in BODY.
1033See also `with-temp-buffer'."
ce87039d
SM
1034 (cons 'save-current-buffer
1035 (cons (list 'set-buffer buffer)
1036 body)))
a7f284ec 1037
e5bb8a8c
SM
1038(defmacro with-temp-file (file &rest body)
1039 "Create a new buffer, evaluate BODY there, and write the buffer to FILE.
1040The value returned is the value of the last form in BODY.
a2fdb55c 1041See also `with-temp-buffer'."
a7ed4c2a 1042 (let ((temp-file (make-symbol "temp-file"))
a2fdb55c
EN
1043 (temp-buffer (make-symbol "temp-buffer")))
1044 `(let ((,temp-file ,file)
1045 (,temp-buffer
1046 (get-buffer-create (generate-new-buffer-name " *temp file*"))))
1047 (unwind-protect
1048 (prog1
1049 (with-current-buffer ,temp-buffer
e5bb8a8c 1050 ,@body)
a2fdb55c
EN
1051 (with-current-buffer ,temp-buffer
1052 (widen)
1053 (write-region (point-min) (point-max) ,temp-file nil 0)))
1054 (and (buffer-name ,temp-buffer)
1055 (kill-buffer ,temp-buffer))))))
1056
e5bb8a8c 1057(defmacro with-temp-message (message &rest body)
a600effe 1058 "Display MESSAGE temporarily if non-nil while BODY is evaluated.
e5bb8a8c
SM
1059The original message is restored to the echo area after BODY has finished.
1060The value returned is the value of the last form in BODY.
a600effe
SM
1061MESSAGE is written to the message log buffer if `message-log-max' is non-nil.
1062If MESSAGE is nil, the echo area and message log buffer are unchanged.
1063Use a MESSAGE of \"\" to temporarily clear the echo area."
110201c8
SM
1064 (let ((current-message (make-symbol "current-message"))
1065 (temp-message (make-symbol "with-temp-message")))
1066 `(let ((,temp-message ,message)
1067 (,current-message))
e5bb8a8c
SM
1068 (unwind-protect
1069 (progn
110201c8
SM
1070 (when ,temp-message
1071 (setq ,current-message (current-message))
aadf7ff3 1072 (message "%s" ,temp-message))
e5bb8a8c 1073 ,@body)
75545902
KH
1074 (and ,temp-message ,current-message
1075 (message "%s" ,current-message))))))
e5bb8a8c
SM
1076
1077(defmacro with-temp-buffer (&rest body)
1078 "Create a temporary buffer, and evaluate BODY there like `progn'.
a2fdb55c
EN
1079See also `with-temp-file' and `with-output-to-string'."
1080 (let ((temp-buffer (make-symbol "temp-buffer")))
1081 `(let ((,temp-buffer
1082 (get-buffer-create (generate-new-buffer-name " *temp*"))))
1083 (unwind-protect
1084 (with-current-buffer ,temp-buffer
e5bb8a8c 1085 ,@body)
a2fdb55c
EN
1086 (and (buffer-name ,temp-buffer)
1087 (kill-buffer ,temp-buffer))))))
1088
5db7925d
RS
1089(defmacro with-output-to-string (&rest body)
1090 "Execute BODY, return the text it sent to `standard-output', as a string."
a2fdb55c
EN
1091 `(let ((standard-output
1092 (get-buffer-create (generate-new-buffer-name " *string-output*"))))
5db7925d
RS
1093 (let ((standard-output standard-output))
1094 ,@body)
a2fdb55c
EN
1095 (with-current-buffer standard-output
1096 (prog1
1097 (buffer-string)
1098 (kill-buffer nil)))))
2ec9c94e
RS
1099
1100(defmacro combine-after-change-calls (&rest body)
1101 "Execute BODY, but don't call the after-change functions till the end.
1102If BODY makes changes in the buffer, they are recorded
1103and the functions on `after-change-functions' are called several times
1104when BODY is finished.
31aa282e 1105The return value is the value of the last form in BODY.
2ec9c94e
RS
1106
1107If `before-change-functions' is non-nil, then calls to the after-change
1108functions can't be deferred, so in that case this macro has no effect.
1109
1110Do not alter `after-change-functions' or `before-change-functions'
1111in BODY."
1112 `(unwind-protect
1113 (let ((combine-after-change-calls t))
1114 . ,body)
1115 (combine-after-change-execute)))
1116
c834b52c
SM
1117
1118(defvar combine-run-hooks t
1119 "List of hooks delayed. Or t if we're not delaying hooks.")
1120
1121(defmacro combine-run-hooks (&rest body)
1122 "Execute BODY, but delay any `run-hooks' until the end."
1123 (let ((saved-combine-run-hooks (make-symbol "saved-combine-run-hooks"))
1124 (saved-run-hooks (make-symbol "saved-run-hooks")))
1125 `(let ((,saved-combine-run-hooks combine-run-hooks)
1126 (,saved-run-hooks (symbol-function 'run-hooks)))
1127 (unwind-protect
1128 (progn
1129 ;; If we're not delaying hooks yet, setup the delaying mode
1130 (unless (listp combine-run-hooks)
1131 (setq combine-run-hooks nil)
1132 (fset 'run-hooks
1133 ,(lambda (&rest hooks)
1134 (setq combine-run-hooks
1135 (append combine-run-hooks hooks)))))
1136 ,@body)
1137 ;; If we were not already delaying, then it's now time to set things
1138 ;; back to normal and to execute the delayed hooks.
1139 (unless (listp ,saved-combine-run-hooks)
1140 (setq ,saved-combine-run-hooks combine-run-hooks)
1141 (fset 'run-hooks ,saved-run-hooks)
1142 (setq combine-run-hooks t)
6ad948eb
SM
1143 (if ,saved-combine-run-hooks
1144 (apply 'run-hooks ,saved-combine-run-hooks)))))))
c834b52c
SM
1145
1146
7e8539cc
RS
1147(defmacro with-syntax-table (table &rest body)
1148 "Evaluate BODY with syntax table of current buffer set to a copy of TABLE.
1149The syntax table of the current buffer is saved, BODY is evaluated, and the
1150saved table is restored, even in case of an abnormal exit.
1151Value is what BODY returns."
b3f07093
RS
1152 (let ((old-table (make-symbol "table"))
1153 (old-buffer (make-symbol "buffer")))
7e8539cc
RS
1154 `(let ((,old-table (syntax-table))
1155 (,old-buffer (current-buffer)))
1156 (unwind-protect
1157 (progn
1158 (set-syntax-table (copy-syntax-table ,table))
1159 ,@body)
1160 (save-current-buffer
1161 (set-buffer ,old-buffer)
1162 (set-syntax-table ,old-table))))))
a2fdb55c 1163\f
c7ca41e6
RS
1164(defvar save-match-data-internal)
1165
1166;; We use save-match-data-internal as the local variable because
1167;; that works ok in practice (people should not use that variable elsewhere).
1168;; We used to use an uninterned symbol; the compiler handles that properly
1169;; now, but it generates slower code.
9a5336ae
JB
1170(defmacro save-match-data (&rest body)
1171 "Execute the BODY forms, restoring the global value of the match data."
64ed733a
PE
1172 ;; It is better not to use backquote here,
1173 ;; because that makes a bootstrapping problem
1174 ;; if you need to recompile all the Lisp files using interpreted code.
1175 (list 'let
1176 '((save-match-data-internal (match-data)))
1177 (list 'unwind-protect
1178 (cons 'progn body)
1179 '(set-match-data save-match-data-internal))))
993713ce 1180
cd323f89 1181(defun match-string (num &optional string)
993713ce
SM
1182 "Return string of text matched by last search.
1183NUM specifies which parenthesized expression in the last regexp.
1184 Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
1185Zero means the entire text matched by the whole regexp or whole string.
1186STRING should be given if the last search was by `string-match' on STRING."
cd323f89
SM
1187 (if (match-beginning num)
1188 (if string
1189 (substring string (match-beginning num) (match-end num))
1190 (buffer-substring (match-beginning num) (match-end num)))))
58f950b4 1191
bb760c71
RS
1192(defun match-string-no-properties (num &optional string)
1193 "Return string of text matched by last search, without text properties.
1194NUM specifies which parenthesized expression in the last regexp.
1195 Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
1196Zero means the entire text matched by the whole regexp or whole string.
1197STRING should be given if the last search was by `string-match' on STRING."
1198 (if (match-beginning num)
1199 (if string
1200 (let ((result
1201 (substring string (match-beginning num) (match-end num))))
1202 (set-text-properties 0 (length result) nil result)
1203 result)
1204 (buffer-substring-no-properties (match-beginning num)
1205 (match-end num)))))
1206
edce3654
RS
1207(defun split-string (string &optional separators)
1208 "Splits STRING into substrings where there are matches for SEPARATORS.
1209Each match for SEPARATORS is a splitting point.
1210The substrings between the splitting points are made into a list
1211which is returned.
b222b786
RS
1212If SEPARATORS is absent, it defaults to \"[ \\f\\t\\n\\r\\v]+\".
1213
1214If there is match for SEPARATORS at the beginning of STRING, we do not
1215include a null substring for that. Likewise, if there is a match
b021ef18
DL
1216at the end of STRING, we don't include a null substring for that.
1217
1218Modifies the match data; use `save-match-data' if necessary."
edce3654
RS
1219 (let ((rexp (or separators "[ \f\t\n\r\v]+"))
1220 (start 0)
b222b786 1221 notfirst
edce3654 1222 (list nil))
b222b786
RS
1223 (while (and (string-match rexp string
1224 (if (and notfirst
1225 (= start (match-beginning 0))
1226 (< start (length string)))
1227 (1+ start) start))
1228 (< (match-beginning 0) (length string)))
1229 (setq notfirst t)
7eb47123 1230 (or (eq (match-beginning 0) 0)
b222b786
RS
1231 (and (eq (match-beginning 0) (match-end 0))
1232 (eq (match-beginning 0) start))
edce3654
RS
1233 (setq list
1234 (cons (substring string start (match-beginning 0))
1235 list)))
1236 (setq start (match-end 0)))
1237 (or (eq start (length string))
1238 (setq list
1239 (cons (substring string start)
1240 list)))
1241 (nreverse list)))
1ccaea52
AI
1242
1243(defun subst-char-in-string (fromchar tochar string &optional inplace)
1244 "Replace FROMCHAR with TOCHAR in STRING each time it occurs.
1245Unless optional argument INPLACE is non-nil, return a new string."
e6e71807
SM
1246 (let ((i (length string))
1247 (newstr (if inplace string (copy-sequence string))))
1248 (while (> i 0)
1249 (setq i (1- i))
1250 (if (eq (aref newstr i) fromchar)
1251 (aset newstr i tochar)))
1252 newstr))
b021ef18 1253
1697159c
DL
1254(defun replace-regexp-in-string (regexp rep string &optional
1255 fixedcase literal subexp start)
b021ef18
DL
1256 "Replace all matches for REGEXP with REP in STRING.
1257
1258Return a new string containing the replacements.
1259
1260Optional arguments FIXEDCASE, LITERAL and SUBEXP are like the
1261arguments with the same names of function `replace-match'. If START
1262is non-nil, start replacements at that index in STRING.
1263
1264REP is either a string used as the NEWTEXT arg of `replace-match' or a
1265function. If it is a function it is applied to each match to generate
1266the replacement passed to `replace-match'; the match-data at this
1267point are such that match 0 is the function's argument.
1268
1697159c
DL
1269To replace only the first match (if any), make REGEXP match up to \\'
1270and replace a sub-expression, e.g.
1271 (replace-regexp-in-string \"\\(foo\\).*\\'\" \"bar\" \" foo foo\" nil nil 1)
1272 => \" bar foo\"
1273"
b021ef18
DL
1274
1275 ;; To avoid excessive consing from multiple matches in long strings,
1276 ;; don't just call `replace-match' continually. Walk down the
1277 ;; string looking for matches of REGEXP and building up a (reversed)
1278 ;; list MATCHES. This comprises segments of STRING which weren't
1279 ;; matched interspersed with replacements for segments that were.
1280 ;; [For a `large' number of replacments it's more efficient to
1281 ;; operate in a temporary buffer; we can't tell from the function's
1282 ;; args whether to choose the buffer-based implementation, though it
1283 ;; might be reasonable to do so for long enough STRING.]
1284 (let ((l (length string))
1285 (start (or start 0))
1286 matches str mb me)
1287 (save-match-data
1288 (while (and (< start l) (string-match regexp string start))
1289 (setq mb (match-beginning 0)
1290 me (match-end 0))
a9853251
SM
1291 ;; If we matched the empty string, make sure we advance by one char
1292 (when (= me mb) (setq me (min l (1+ mb))))
1293 ;; Generate a replacement for the matched substring.
1294 ;; Operate only on the substring to minimize string consing.
1295 ;; Set up match data for the substring for replacement;
1296 ;; presumably this is likely to be faster than munging the
1297 ;; match data directly in Lisp.
1298 (string-match regexp (setq str (substring string mb me)))
1299 (setq matches
1300 (cons (replace-match (if (stringp rep)
1301 rep
1302 (funcall rep (match-string 0 str)))
1303 fixedcase literal str subexp)
1304 (cons (substring string start mb) ; unmatched prefix
1305 matches)))
1306 (setq start me))
b021ef18
DL
1307 ;; Reconstruct a string from the pieces.
1308 (setq matches (cons (substring string start l) matches)) ; leftover
1309 (apply #'concat (nreverse matches)))))
a7ed4c2a 1310\f
8af7df60
RS
1311(defun shell-quote-argument (argument)
1312 "Quote an argument for passing as argument to an inferior shell."
c1c74b43 1313 (if (eq system-type 'ms-dos)
8ee75d03
EZ
1314 ;; Quote using double quotes, but escape any existing quotes in
1315 ;; the argument with backslashes.
1316 (let ((result "")
1317 (start 0)
1318 end)
1319 (if (or (null (string-match "[^\"]" argument))
1320 (< (match-end 0) (length argument)))
1321 (while (string-match "[\"]" argument start)
1322 (setq end (match-beginning 0)
1323 result (concat result (substring argument start end)
1324 "\\" (substring argument end (1+ end)))
1325 start (1+ end))))
1326 (concat "\"" result (substring argument start) "\""))
c1c74b43
RS
1327 (if (eq system-type 'windows-nt)
1328 (concat "\"" argument "\"")
e1b65a6b
RS
1329 (if (equal argument "")
1330 "''"
1331 ;; Quote everything except POSIX filename characters.
1332 ;; This should be safe enough even for really weird shells.
1333 (let ((result "") (start 0) end)
1334 (while (string-match "[^-0-9a-zA-Z_./]" argument start)
1335 (setq end (match-beginning 0)
1336 result (concat result (substring argument start end)
1337 "\\" (substring argument end (1+ end)))
1338 start (1+ end)))
1339 (concat result (substring argument start)))))))
8af7df60 1340
297d863b 1341(defun make-syntax-table (&optional oldtable)
984f718a 1342 "Return a new syntax table.
888eb98e
RS
1343If OLDTABLE is non-nil, copy OLDTABLE.
1344Otherwise, create a syntax table which inherits
1345all letters and control characters from the standard syntax table;
1346other characters are copied from the standard syntax table."
297d863b
KH
1347 (if oldtable
1348 (copy-syntax-table oldtable)
1349 (let ((table (copy-syntax-table))
1350 i)
1351 (setq i 0)
1352 (while (<= i 31)
a6889c57 1353 (aset table i nil)
297d863b
KH
1354 (setq i (1+ i)))
1355 (setq i ?A)
1356 (while (<= i ?Z)
a6889c57 1357 (aset table i nil)
297d863b
KH
1358 (setq i (1+ i)))
1359 (setq i ?a)
1360 (while (<= i ?z)
a6889c57 1361 (aset table i nil)
297d863b
KH
1362 (setq i (1+ i)))
1363 (setq i 128)
1364 (while (<= i 255)
a6889c57 1365 (aset table i nil)
297d863b
KH
1366 (setq i (1+ i)))
1367 table)))
31aa282e
KH
1368
1369(defun add-to-invisibility-spec (arg)
1370 "Add elements to `buffer-invisibility-spec'.
1371See documentation for `buffer-invisibility-spec' for the kind of elements
1372that can be added."
1373 (cond
1374 ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t))
1375 (setq buffer-invisibility-spec (list arg)))
1376 (t
11d431ad
KH
1377 (setq buffer-invisibility-spec
1378 (cons arg buffer-invisibility-spec)))))
31aa282e
KH
1379
1380(defun remove-from-invisibility-spec (arg)
1381 "Remove elements from `buffer-invisibility-spec'."
e93b8cbb 1382 (if (consp buffer-invisibility-spec)
071a2a71 1383 (setq buffer-invisibility-spec (delete arg buffer-invisibility-spec))))
baed0109
RS
1384\f
1385(defun global-set-key (key command)
1386 "Give KEY a global binding as COMMAND.
7bba1895
KH
1387COMMAND is the command definition to use; usually it is
1388a symbol naming an interactively-callable function.
1389KEY is a key sequence; noninteractively, it is a string or vector
1390of characters or event types, and non-ASCII characters with codes
1391above 127 (such as ISO Latin-1) can be included if you use a vector.
1392
1393Note that if KEY has a local binding in the current buffer,
1394that local binding will continue to shadow any global binding
1395that you make with this function."
baed0109
RS
1396 (interactive "KSet key globally: \nCSet key %s to command: ")
1397 (or (vectorp key) (stringp key)
1398 (signal 'wrong-type-argument (list 'arrayp key)))
ff663bbe 1399 (define-key (current-global-map) key command))
baed0109
RS
1400
1401(defun local-set-key (key command)
1402 "Give KEY a local binding as COMMAND.
7bba1895
KH
1403COMMAND is the command definition to use; usually it is
1404a symbol naming an interactively-callable function.
1405KEY is a key sequence; noninteractively, it is a string or vector
1406of characters or event types, and non-ASCII characters with codes
1407above 127 (such as ISO Latin-1) can be included if you use a vector.
1408
baed0109
RS
1409The binding goes in the current buffer's local map,
1410which in most cases is shared with all other buffers in the same major mode."
1411 (interactive "KSet key locally: \nCSet key %s locally to command: ")
1412 (let ((map (current-local-map)))
1413 (or map
1414 (use-local-map (setq map (make-sparse-keymap))))
1415 (or (vectorp key) (stringp key)
1416 (signal 'wrong-type-argument (list 'arrayp key)))
ff663bbe 1417 (define-key map key command)))
984f718a 1418
baed0109
RS
1419(defun global-unset-key (key)
1420 "Remove global binding of KEY.
1421KEY is a string representing a sequence of keystrokes."
1422 (interactive "kUnset key globally: ")
1423 (global-set-key key nil))
1424
db2474b8 1425(defun local-unset-key (key)
baed0109
RS
1426 "Remove local binding of KEY.
1427KEY is a string representing a sequence of keystrokes."
1428 (interactive "kUnset key locally: ")
1429 (if (current-local-map)
db2474b8 1430 (local-set-key key nil))
baed0109
RS
1431 nil)
1432\f
4809d0dd
KH
1433;; We put this here instead of in frame.el so that it's defined even on
1434;; systems where frame.el isn't loaded.
1435(defun frame-configuration-p (object)
1436 "Return non-nil if OBJECT seems to be a frame configuration.
1437Any list whose car is `frame-configuration' is assumed to be a frame
1438configuration."
1439 (and (consp object)
1440 (eq (car object) 'frame-configuration)))
1441
a9a44ed1 1442(defun functionp (object)
77a5664f 1443 "Non-nil if OBJECT is a type of object that can be called as a function."
c029995c 1444 (or (subrp object) (byte-code-function-p object)
a9a44ed1
RS
1445 (eq (car-safe object) 'lambda)
1446 (and (symbolp object) (fboundp object))))
1447
9a5336ae
JB
1448;; now in fns.c
1449;(defun nth (n list)
1450; "Returns the Nth element of LIST.
1451;N counts from zero. If LIST is not that long, nil is returned."
1452; (car (nthcdr n list)))
1453;
1454;(defun copy-alist (alist)
1455; "Return a copy of ALIST.
1456;This is a new alist which represents the same mapping
1457;from objects to objects, but does not share the alist structure with ALIST.
1458;The objects mapped (cars and cdrs of elements of the alist)
1459;are shared, however."
1460; (setq alist (copy-sequence alist))
1461; (let ((tail alist))
1462; (while tail
1463; (if (consp (car tail))
1464; (setcar tail (cons (car (car tail)) (cdr (car tail)))))
1465; (setq tail (cdr tail))))
1466; alist)
630cc463 1467
d3a61a11 1468(defun assq-delete-all (key alist)
a62d6695
DL
1469 "Delete from ALIST all elements whose car is KEY.
1470Return the modified alist."
a62d6695
DL
1471 (let ((tail alist))
1472 (while tail
1473 (if (eq (car (car tail)) key)
1474 (setq alist (delq (car tail) alist)))
1475 (setq tail (cdr tail)))
1476 alist))
1477
cdd9f643
RS
1478(defun make-temp-file (prefix &optional dir-flag)
1479 "Create a temporary file.
1480The returned file name (created by appending some random characters at the end
1481of PREFIX, and expanding against `temporary-file-directory' if necessary,
1482is guaranteed to point to a newly created empty file.
1483You can then use `write-region' to write new data into the file.
1484
1485If DIR-FLAG is non-nil, create a new empty directory instead of a file."
1486 (let (file)
1487 (while (condition-case ()
1488 (progn
1489 (setq file
1490 (make-temp-name
1491 (expand-file-name prefix temporary-file-directory)))
1492 (if dir-flag
1493 (make-directory file)
1494 (write-region "" nil file nil 'silent nil 'excl))
1495 nil)
1496 (file-already-exists t))
1497 ;; the file was somehow created by someone else between
1498 ;; `make-temp-name' and `write-region', let's try again.
1499 nil)
1500 file))
1501
d7d47268 1502\f
c94f4677 1503(defun add-minor-mode (toggle name &optional keymap after toggle-fun)
d7d47268 1504 "Register a new minor mode.
c94f4677 1505
0b2cf11f
SM
1506This is an XEmacs-compatibility function. Use `define-minor-mode' instead.
1507
c94f4677
GM
1508TOGGLE is a symbol which is the name of a buffer-local variable that
1509is toggled on or off to say whether the minor mode is active or not.
1510
1511NAME specifies what will appear in the mode line when the minor mode
1512is active. NAME should be either a string starting with a space, or a
1513symbol whose value is such a string.
1514
1515Optional KEYMAP is the keymap for the minor mode that will be added
1516to `minor-mode-map-alist'.
1517
1518Optional AFTER specifies that TOGGLE should be added after AFTER
1519in `minor-mode-alist'.
1520
0b2cf11f
SM
1521Optional TOGGLE-FUN is an interactive function to toggle the mode.
1522It defaults to (and should by convention be) TOGGLE.
1523
1524If TOGGLE has a non-nil `:included' property, an entry for the mode is
1525included in the mode-line minor mode menu.
1526If TOGGLE has a `:menu-tag', that is used for the menu item's label."
1527 (unless toggle-fun (setq toggle-fun toggle))
1528 ;; Add the toggle to the minor-modes menu if requested.
1529 (when (get toggle :included)
1530 (define-key mode-line-mode-menu
1531 (vector toggle)
1532 (list 'menu-item
1533 (or (get toggle :menu-tag)
1534 (if (stringp name) name (symbol-name toggle)))
1535 toggle-fun
1536 :button (cons :toggle toggle))))
1537 ;; Add the name to the minor-mode-alist.
c94f4677 1538 (when name
0b2cf11f
SM
1539 (let ((existing (assq toggle minor-mode-alist)))
1540 (when (and (stringp name) (not (get-text-property 0 'local-map name)))
d6c22d46
DL
1541 (setq name
1542 (apply 'propertize name
1543 'local-map (make-mode-line-mouse2-map toggle-fun)
1544 (unless (get-text-property 0 'help-echo name)
1545 (list 'help-echo
0b2cf11f
SM
1546 (format "mouse-2: turn off %S" toggle))))))
1547 (if existing
1548 (setcdr existing (list name))
1549 (let ((tail minor-mode-alist) found)
1550 (while (and tail (not found))
1551 (if (eq after (caar tail))
1552 (setq found tail)
1553 (setq tail (cdr tail))))
1554 (if found
1555 (let ((rest (cdr found)))
1556 (setcdr found nil)
1557 (nconc found (list (list toggle name)) rest))
1558 (setq minor-mode-alist (cons (list toggle name)
1559 minor-mode-alist)))))))
1560 ;; Add the map to the minor-mode-map-alist.
c94f4677
GM
1561 (when keymap
1562 (let ((existing (assq toggle minor-mode-map-alist)))
0b2cf11f
SM
1563 (if existing
1564 (setcdr existing keymap)
1565 (let ((tail minor-mode-map-alist) found)
1566 (while (and tail (not found))
1567 (if (eq after (caar tail))
1568 (setq found tail)
1569 (setq tail (cdr tail))))
1570 (if found
1571 (let ((rest (cdr found)))
1572 (setcdr found nil)
1573 (nconc found (list (cons toggle keymap)) rest))
1574 (setq minor-mode-map-alist (cons (cons toggle keymap)
1575 minor-mode-map-alist))))))))
d7d47268 1576
ff77cf40
DL
1577;; XEmacs compatibility/convenience.
1578(if (fboundp 'play-sound)
1579 (defun play-sound-file (file &optional volume device)
1580 "Play sound stored in FILE.
1581VOLUME and DEVICE correspond to the keywords of the sound
1582specification for `play-sound'."
1583 (interactive "fPlay sound file: ")
1584 (let ((sound (list :file file)))
1585 (if volume
1586 (plist-put sound :volume volume))
1587 (if device
1588 (plist-put sound :device device))
1589 (push 'sound sound)
1590 (play-sound sound))))
d7d47268 1591
630cc463 1592;;; subr.el ends here