(assq-delete-all): Ignore non-cons elememts.
[bpt/emacs.git] / lisp / subr.el
1 ;;; subr.el --- basic lisp subroutines for Emacs
2
3 ;; Copyright (C) 1985, 86, 92, 94, 95, 99, 2000, 2001, 2002, 2003
4 ;; Free Software Foundation, Inc.
5
6 ;; Maintainer: FSF
7 ;; Keywords: internal
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29 (defvar custom-declare-variable-list nil
30 "Record `defcustom' calls made before `custom.el' is loaded to handle them.
31 Each element of this list holds the arguments to one call to `defcustom'.")
32
33 ;; Use this, rather than defcustom, in subr.el and other files loaded
34 ;; before custom.el.
35 (defun custom-declare-variable-early (&rest arguments)
36 (setq custom-declare-variable-list
37 (cons arguments custom-declare-variable-list)))
38
39 \f
40 (defun macro-declaration-function (macro decl)
41 "Process a declaration found in a macro definition.
42 This is set as the value of the variable `macro-declaration-function'.
43 MACRO is the name of the macro being defined.
44 DECL is a list `(declare ...)' containing the declarations.
45 The return value of this function is not used."
46 (dolist (d (cdr decl))
47 (cond ((and (consp d) (eq (car d) 'indent))
48 (put macro 'lisp-indent-function (cadr d)))
49 ((and (consp d) (eq (car d) 'debug))
50 (put macro 'edebug-form-spec (cadr d)))
51 (t
52 (message "Unknown declaration %s" d)))))
53
54 (setq macro-declaration-function 'macro-declaration-function)
55
56 \f
57 ;;;; Lisp language features.
58
59 (defalias 'not 'null)
60
61 (defmacro lambda (&rest cdr)
62 "Return a lambda expression.
63 A call of the form (lambda ARGS DOCSTRING INTERACTIVE BODY) is
64 self-quoting; the result of evaluating the lambda expression is the
65 expression itself. The lambda expression may then be treated as a
66 function, i.e., stored as the function value of a symbol, passed to
67 funcall or mapcar, etc.
68
69 ARGS should take the same form as an argument list for a `defun'.
70 DOCSTRING is an optional documentation string.
71 If present, it should describe how to call the function.
72 But documentation strings are usually not useful in nameless functions.
73 INTERACTIVE should be a call to the function `interactive', which see.
74 It may also be omitted.
75 BODY should be a list of Lisp expressions."
76 ;; Note that this definition should not use backquotes; subr.el should not
77 ;; depend on backquote.el.
78 (list 'function (cons 'lambda cdr)))
79
80 (defmacro push (newelt listname)
81 "Add NEWELT to the list stored in the symbol LISTNAME.
82 This is equivalent to (setq LISTNAME (cons NEWELT LISTNAME)).
83 LISTNAME must be a symbol."
84 (list 'setq listname
85 (list 'cons newelt listname)))
86
87 (defmacro pop (listname)
88 "Return the first element of LISTNAME's value, and remove it from the list.
89 LISTNAME must be a symbol whose value is a list.
90 If the value is nil, `pop' returns nil but does not actually
91 change the list."
92 (list 'car
93 (list 'prog1 listname
94 (list 'setq listname (list 'cdr listname)))))
95
96 (defmacro when (cond &rest body)
97 "If COND yields non-nil, do BODY, else return nil."
98 (list 'if cond (cons 'progn body)))
99
100 (defmacro unless (cond &rest body)
101 "If COND yields nil, do BODY, else return nil."
102 (cons 'if (cons cond (cons nil body))))
103
104 (defmacro dolist (spec &rest body)
105 "(dolist (VAR LIST [RESULT]) BODY...): loop over a list.
106 Evaluate BODY with VAR bound to each car from LIST, in turn.
107 Then evaluate RESULT to get return value, default nil."
108 (let ((temp (make-symbol "--dolist-temp--")))
109 (list 'let (list (list temp (nth 1 spec)) (car spec))
110 (list 'while temp
111 (list 'setq (car spec) (list 'car temp))
112 (cons 'progn
113 (append body
114 (list (list 'setq temp (list 'cdr temp))))))
115 (if (cdr (cdr spec))
116 (cons 'progn
117 (cons (list 'setq (car spec) nil) (cdr (cdr spec))))))))
118
119 (defmacro dotimes (spec &rest body)
120 "(dotimes (VAR COUNT [RESULT]) BODY...): loop a certain number of times.
121 Evaluate BODY with VAR bound to successive integers running from 0,
122 inclusive, to COUNT, exclusive. Then evaluate RESULT to get
123 the return value (nil if RESULT is omitted)."
124 (let ((temp (make-symbol "--dotimes-temp--")))
125 (list 'let (list (list temp (nth 1 spec)) (list (car spec) 0))
126 (list 'while (list '< (car spec) temp)
127 (cons 'progn
128 (append body (list (list 'setq (car spec)
129 (list '1+ (car spec)))))))
130 (if (cdr (cdr spec))
131 (car (cdr (cdr spec)))
132 nil))))
133
134 (defsubst caar (x)
135 "Return the car of the car of X."
136 (car (car x)))
137
138 (defsubst cadr (x)
139 "Return the car of the cdr of X."
140 (car (cdr x)))
141
142 (defsubst cdar (x)
143 "Return the cdr of the car of X."
144 (cdr (car x)))
145
146 (defsubst cddr (x)
147 "Return the cdr of the cdr of X."
148 (cdr (cdr x)))
149
150 (defun last (x &optional n)
151 "Return the last link of the list X. Its car is the last element.
152 If X is nil, return nil.
153 If N is non-nil, return the Nth-to-last link of X.
154 If N is bigger than the length of X, return X."
155 (if n
156 (let ((m 0) (p x))
157 (while (consp p)
158 (setq m (1+ m) p (cdr p)))
159 (if (<= n 0) p
160 (if (< n m) (nthcdr (- m n) x) x)))
161 (while (consp (cdr x))
162 (setq x (cdr x)))
163 x))
164
165 (defun butlast (x &optional n)
166 "Returns a copy of LIST with the last N elements removed."
167 (if (and n (<= n 0)) x
168 (nbutlast (copy-sequence x) n)))
169
170 (defun nbutlast (x &optional n)
171 "Modifies LIST to remove the last N elements."
172 (let ((m (length x)))
173 (or n (setq n 1))
174 (and (< n m)
175 (progn
176 (if (> n 0) (setcdr (nthcdr (- (1- m) n) x) nil))
177 x))))
178
179 (defun number-sequence (from &optional to inc)
180 "Return a sequence of numbers from FROM to TO (both inclusive) as a list.
181 INC is the increment used between numbers in the sequence.
182 So, the Nth element of the list is (+ FROM (* N INC)) where N counts from
183 zero.
184 If INC is nil, it defaults to 1 (one).
185 If TO is nil, it defaults to FROM.
186 If TO is less than FROM, the value is nil.
187 Note that FROM, TO and INC can be integer or float."
188 (if (not to)
189 (list from)
190 (or inc (setq inc 1))
191 (let (seq)
192 (while (<= from to)
193 (setq seq (cons from seq)
194 from (+ from inc)))
195 (nreverse seq))))
196
197 (defun remove (elt seq)
198 "Return a copy of SEQ with all occurrences of ELT removed.
199 SEQ must be a list, vector, or string. The comparison is done with `equal'."
200 (if (nlistp seq)
201 ;; If SEQ isn't a list, there's no need to copy SEQ because
202 ;; `delete' will return a new object.
203 (delete elt seq)
204 (delete elt (copy-sequence seq))))
205
206 (defun remq (elt list)
207 "Return a copy of LIST with all occurrences of ELT removed.
208 The comparison is done with `eq'."
209 (if (memq elt list)
210 (delq elt (copy-sequence list))
211 list))
212
213 (defun copy-tree (tree &optional vecp)
214 "Make a copy of TREE.
215 If TREE is a cons cell, this recursively copies both its car and its cdr.
216 Contrast to `copy-sequence', which copies only along the cdrs. With second
217 argument VECP, this copies vectors as well as conses."
218 (if (consp tree)
219 (let (result)
220 (while (consp tree)
221 (let ((newcar (car tree)))
222 (if (or (consp (car tree)) (and vecp (vectorp (car tree))))
223 (setq newcar (copy-tree (car tree) vecp)))
224 (push newcar result))
225 (setq tree (cdr tree)))
226 (nconc (nreverse result) tree))
227 (if (and vecp (vectorp tree))
228 (let ((i (length (setq tree (copy-sequence tree)))))
229 (while (>= (setq i (1- i)) 0)
230 (aset tree i (copy-tree (aref tree i) vecp)))
231 tree)
232 tree)))
233
234 (defun assoc-default (key alist &optional test default)
235 "Find object KEY in a pseudo-alist ALIST.
236 ALIST is a list of conses or objects. Each element (or the element's car,
237 if it is a cons) is compared with KEY by evaluating (TEST (car elt) KEY).
238 If that is non-nil, the element matches;
239 then `assoc-default' returns the element's cdr, if it is a cons,
240 or DEFAULT if the element is not a cons.
241
242 If no element matches, the value is nil.
243 If TEST is omitted or nil, `equal' is used."
244 (let (found (tail alist) value)
245 (while (and tail (not found))
246 (let ((elt (car tail)))
247 (when (funcall (or test 'equal) (if (consp elt) (car elt) elt) key)
248 (setq found t value (if (consp elt) (cdr elt) default))))
249 (setq tail (cdr tail)))
250 value))
251
252 (defun assoc-ignore-case (key alist)
253 "Like `assoc', but ignores differences in case and text representation.
254 KEY must be a string. Upper-case and lower-case letters are treated as equal.
255 Unibyte strings are converted to multibyte for comparison."
256 (let (element)
257 (while (and alist (not element))
258 (if (eq t (compare-strings key 0 nil (car (car alist)) 0 nil t))
259 (setq element (car alist)))
260 (setq alist (cdr alist)))
261 element))
262
263 (defun assoc-ignore-representation (key alist)
264 "Like `assoc', but ignores differences in text representation.
265 KEY must be a string.
266 Unibyte strings are converted to multibyte for comparison."
267 (let (element)
268 (while (and alist (not element))
269 (if (eq t (compare-strings key 0 nil (car (car alist)) 0 nil))
270 (setq element (car alist)))
271 (setq alist (cdr alist)))
272 element))
273
274 (defun member-ignore-case (elt list)
275 "Like `member', but ignores differences in case and text representation.
276 ELT must be a string. Upper-case and lower-case letters are treated as equal.
277 Unibyte strings are converted to multibyte for comparison.
278 Non-strings in LIST are ignored."
279 (while (and list
280 (not (and (stringp (car list))
281 (eq t (compare-strings elt 0 nil (car list) 0 nil t)))))
282 (setq list (cdr list)))
283 list)
284
285 \f
286 ;;;; Keymap support.
287
288 (defun undefined ()
289 (interactive)
290 (ding))
291
292 ;Prevent the \{...} documentation construct
293 ;from mentioning keys that run this command.
294 (put 'undefined 'suppress-keymap t)
295
296 (defun suppress-keymap (map &optional nodigits)
297 "Make MAP override all normally self-inserting keys to be undefined.
298 Normally, as an exception, digits and minus-sign are set to make prefix args,
299 but optional second arg NODIGITS non-nil treats them like other chars."
300 (define-key map [remap self-insert-command] 'undefined)
301 (or nodigits
302 (let (loop)
303 (define-key map "-" 'negative-argument)
304 ;; Make plain numbers do numeric args.
305 (setq loop ?0)
306 (while (<= loop ?9)
307 (define-key map (char-to-string loop) 'digit-argument)
308 (setq loop (1+ loop))))))
309
310 ;Moved to keymap.c
311 ;(defun copy-keymap (keymap)
312 ; "Return a copy of KEYMAP"
313 ; (while (not (keymapp keymap))
314 ; (setq keymap (signal 'wrong-type-argument (list 'keymapp keymap))))
315 ; (if (vectorp keymap)
316 ; (copy-sequence keymap)
317 ; (copy-alist keymap)))
318
319 (defvar key-substitution-in-progress nil
320 "Used internally by substitute-key-definition.")
321
322 (defun substitute-key-definition (olddef newdef keymap &optional oldmap prefix)
323 "Replace OLDDEF with NEWDEF for any keys in KEYMAP now defined as OLDDEF.
324 In other words, OLDDEF is replaced with NEWDEF where ever it appears.
325 Alternatively, if optional fourth argument OLDMAP is specified, we redefine
326 in KEYMAP as NEWDEF those keys which are defined as OLDDEF in OLDMAP."
327 ;; Don't document PREFIX in the doc string because we don't want to
328 ;; advertise it. It's meant for recursive calls only. Here's its
329 ;; meaning
330
331 ;; If optional argument PREFIX is specified, it should be a key
332 ;; prefix, a string. Redefined bindings will then be bound to the
333 ;; original key, with PREFIX added at the front.
334 (or prefix (setq prefix ""))
335 (let* ((scan (or oldmap keymap))
336 (vec1 (vector nil))
337 (prefix1 (vconcat prefix vec1))
338 (key-substitution-in-progress
339 (cons scan key-substitution-in-progress)))
340 ;; Scan OLDMAP, finding each char or event-symbol that
341 ;; has any definition, and act on it with hack-key.
342 (while (consp scan)
343 (if (consp (car scan))
344 (let ((char (car (car scan)))
345 (defn (cdr (car scan))))
346 ;; The inside of this let duplicates exactly
347 ;; the inside of the following let that handles array elements.
348 (aset vec1 0 char)
349 (aset prefix1 (length prefix) char)
350 (let (inner-def skipped)
351 ;; Skip past menu-prompt.
352 (while (stringp (car-safe defn))
353 (setq skipped (cons (car defn) skipped))
354 (setq defn (cdr defn)))
355 ;; Skip past cached key-equivalence data for menu items.
356 (and (consp defn) (consp (car defn))
357 (setq defn (cdr defn)))
358 (setq inner-def defn)
359 ;; Look past a symbol that names a keymap.
360 (while (and (symbolp inner-def)
361 (fboundp inner-def))
362 (setq inner-def (symbol-function inner-def)))
363 (if (or (eq defn olddef)
364 ;; Compare with equal if definition is a key sequence.
365 ;; That is useful for operating on function-key-map.
366 (and (or (stringp defn) (vectorp defn))
367 (equal defn olddef)))
368 (define-key keymap prefix1 (nconc (nreverse skipped) newdef))
369 (if (and (keymapp defn)
370 ;; Avoid recursively scanning
371 ;; where KEYMAP does not have a submap.
372 (let ((elt (lookup-key keymap prefix1)))
373 (or (null elt)
374 (keymapp elt)))
375 ;; Avoid recursively rescanning keymap being scanned.
376 (not (memq inner-def
377 key-substitution-in-progress)))
378 ;; If this one isn't being scanned already,
379 ;; scan it now.
380 (substitute-key-definition olddef newdef keymap
381 inner-def
382 prefix1)))))
383 (if (vectorp (car scan))
384 (let* ((array (car scan))
385 (len (length array))
386 (i 0))
387 (while (< i len)
388 (let ((char i) (defn (aref array i)))
389 ;; The inside of this let duplicates exactly
390 ;; the inside of the previous let.
391 (aset vec1 0 char)
392 (aset prefix1 (length prefix) char)
393 (let (inner-def skipped)
394 ;; Skip past menu-prompt.
395 (while (stringp (car-safe defn))
396 (setq skipped (cons (car defn) skipped))
397 (setq defn (cdr defn)))
398 (and (consp defn) (consp (car defn))
399 (setq defn (cdr defn)))
400 (setq inner-def defn)
401 (while (and (symbolp inner-def)
402 (fboundp inner-def))
403 (setq inner-def (symbol-function inner-def)))
404 (if (or (eq defn olddef)
405 (and (or (stringp defn) (vectorp defn))
406 (equal defn olddef)))
407 (define-key keymap prefix1
408 (nconc (nreverse skipped) newdef))
409 (if (and (keymapp defn)
410 (let ((elt (lookup-key keymap prefix1)))
411 (or (null elt)
412 (keymapp elt)))
413 (not (memq inner-def
414 key-substitution-in-progress)))
415 (substitute-key-definition olddef newdef keymap
416 inner-def
417 prefix1)))))
418 (setq i (1+ i))))
419 (if (char-table-p (car scan))
420 (map-char-table
421 (function (lambda (char defn)
422 (let ()
423 ;; The inside of this let duplicates exactly
424 ;; the inside of the previous let,
425 ;; except that it uses set-char-table-range
426 ;; instead of define-key.
427 (aset vec1 0 char)
428 (aset prefix1 (length prefix) char)
429 (let (inner-def skipped)
430 ;; Skip past menu-prompt.
431 (while (stringp (car-safe defn))
432 (setq skipped (cons (car defn) skipped))
433 (setq defn (cdr defn)))
434 (and (consp defn) (consp (car defn))
435 (setq defn (cdr defn)))
436 (setq inner-def defn)
437 (while (and (symbolp inner-def)
438 (fboundp inner-def))
439 (setq inner-def (symbol-function inner-def)))
440 (if (or (eq defn olddef)
441 (and (or (stringp defn) (vectorp defn))
442 (equal defn olddef)))
443 (define-key keymap prefix1
444 (nconc (nreverse skipped) newdef))
445 (if (and (keymapp defn)
446 (let ((elt (lookup-key keymap prefix1)))
447 (or (null elt)
448 (keymapp elt)))
449 (not (memq inner-def
450 key-substitution-in-progress)))
451 (substitute-key-definition olddef newdef keymap
452 inner-def
453 prefix1)))))))
454 (car scan)))))
455 (setq scan (cdr scan)))))
456
457 (defun define-key-after (keymap key definition &optional after)
458 "Add binding in KEYMAP for KEY => DEFINITION, right after AFTER's binding.
459 This is like `define-key' except that the binding for KEY is placed
460 just after the binding for the event AFTER, instead of at the beginning
461 of the map. Note that AFTER must be an event type (like KEY), NOT a command
462 \(like DEFINITION).
463
464 If AFTER is t or omitted, the new binding goes at the end of the keymap.
465 AFTER should be a single event type--a symbol or a character, not a sequence.
466
467 Bindings are always added before any inherited map.
468
469 The order of bindings in a keymap matters when it is used as a menu."
470 (unless after (setq after t))
471 (or (keymapp keymap)
472 (signal 'wrong-type-argument (list 'keymapp keymap)))
473 (setq key
474 (if (<= (length key) 1) (aref key 0)
475 (setq keymap (lookup-key keymap
476 (apply 'vector
477 (butlast (mapcar 'identity key)))))
478 (aref key (1- (length key)))))
479 (let ((tail keymap) done inserted)
480 (while (and (not done) tail)
481 ;; Delete any earlier bindings for the same key.
482 (if (eq (car-safe (car (cdr tail))) key)
483 (setcdr tail (cdr (cdr tail))))
484 ;; If we hit an included map, go down that one.
485 (if (keymapp (car tail)) (setq tail (car tail)))
486 ;; When we reach AFTER's binding, insert the new binding after.
487 ;; If we reach an inherited keymap, insert just before that.
488 ;; If we reach the end of this keymap, insert at the end.
489 (if (or (and (eq (car-safe (car tail)) after)
490 (not (eq after t)))
491 (eq (car (cdr tail)) 'keymap)
492 (null (cdr tail)))
493 (progn
494 ;; Stop the scan only if we find a parent keymap.
495 ;; Keep going past the inserted element
496 ;; so we can delete any duplications that come later.
497 (if (eq (car (cdr tail)) 'keymap)
498 (setq done t))
499 ;; Don't insert more than once.
500 (or inserted
501 (setcdr tail (cons (cons key definition) (cdr tail))))
502 (setq inserted t)))
503 (setq tail (cdr tail)))))
504
505
506 (defmacro kbd (keys)
507 "Convert KEYS to the internal Emacs key representation.
508 KEYS should be a string constant in the format used for
509 saving keyboard macros (see `insert-kbd-macro')."
510 (read-kbd-macro keys))
511
512 (put 'keyboard-translate-table 'char-table-extra-slots 0)
513
514 (defun keyboard-translate (from to)
515 "Translate character FROM to TO at a low level.
516 This function creates a `keyboard-translate-table' if necessary
517 and then modifies one entry in it."
518 (or (char-table-p keyboard-translate-table)
519 (setq keyboard-translate-table
520 (make-char-table 'keyboard-translate-table nil)))
521 (aset keyboard-translate-table from to))
522
523 \f
524 ;;;; The global keymap tree.
525
526 ;;; global-map, esc-map, and ctl-x-map have their values set up in
527 ;;; keymap.c; we just give them docstrings here.
528
529 (defvar global-map nil
530 "Default global keymap mapping Emacs keyboard input into commands.
531 The value is a keymap which is usually (but not necessarily) Emacs's
532 global map.")
533
534 (defvar esc-map nil
535 "Default keymap for ESC (meta) commands.
536 The normal global definition of the character ESC indirects to this keymap.")
537
538 (defvar ctl-x-map nil
539 "Default keymap for C-x commands.
540 The normal global definition of the character C-x indirects to this keymap.")
541
542 (defvar ctl-x-4-map (make-sparse-keymap)
543 "Keymap for subcommands of C-x 4.")
544 (defalias 'ctl-x-4-prefix ctl-x-4-map)
545 (define-key ctl-x-map "4" 'ctl-x-4-prefix)
546
547 (defvar ctl-x-5-map (make-sparse-keymap)
548 "Keymap for frame commands.")
549 (defalias 'ctl-x-5-prefix ctl-x-5-map)
550 (define-key ctl-x-map "5" 'ctl-x-5-prefix)
551
552 \f
553 ;;;; Event manipulation functions.
554
555 ;; The call to `read' is to ensure that the value is computed at load time
556 ;; and not compiled into the .elc file. The value is negative on most
557 ;; machines, but not on all!
558 (defconst listify-key-sequence-1 (logior 128 (read "?\\M-\\^@")))
559
560 (defun listify-key-sequence (key)
561 "Convert a key sequence to a list of events."
562 (if (vectorp key)
563 (append key nil)
564 (mapcar (function (lambda (c)
565 (if (> c 127)
566 (logxor c listify-key-sequence-1)
567 c)))
568 (append key nil))))
569
570 (defsubst eventp (obj)
571 "True if the argument is an event object."
572 (or (integerp obj)
573 (and (symbolp obj)
574 (get obj 'event-symbol-elements))
575 (and (consp obj)
576 (symbolp (car obj))
577 (get (car obj) 'event-symbol-elements))))
578
579 (defun event-modifiers (event)
580 "Returns a list of symbols representing the modifier keys in event EVENT.
581 The elements of the list may include `meta', `control',
582 `shift', `hyper', `super', `alt', `click', `double', `triple', `drag',
583 and `down'."
584 (let ((type event))
585 (if (listp type)
586 (setq type (car type)))
587 (if (symbolp type)
588 (cdr (get type 'event-symbol-elements))
589 (let ((list nil))
590 (or (zerop (logand type ?\M-\^@))
591 (setq list (cons 'meta list)))
592 (or (and (zerop (logand type ?\C-\^@))
593 (>= (logand type 127) 32))
594 (setq list (cons 'control list)))
595 (or (and (zerop (logand type ?\S-\^@))
596 (= (logand type 255) (downcase (logand type 255))))
597 (setq list (cons 'shift list)))
598 (or (zerop (logand type ?\H-\^@))
599 (setq list (cons 'hyper list)))
600 (or (zerop (logand type ?\s-\^@))
601 (setq list (cons 'super list)))
602 (or (zerop (logand type ?\A-\^@))
603 (setq list (cons 'alt list)))
604 list))))
605
606 (defun event-basic-type (event)
607 "Returns the basic type of the given event (all modifiers removed).
608 The value is a printing character (not upper case) or a symbol."
609 (if (consp event)
610 (setq event (car event)))
611 (if (symbolp event)
612 (car (get event 'event-symbol-elements))
613 (let ((base (logand event (1- (lsh 1 18)))))
614 (downcase (if (< base 32) (logior base 64) base)))))
615
616 (defsubst mouse-movement-p (object)
617 "Return non-nil if OBJECT is a mouse movement event."
618 (and (consp object)
619 (eq (car object) 'mouse-movement)))
620
621 (defsubst event-start (event)
622 "Return the starting position of EVENT.
623 If EVENT is a mouse press or a mouse click, this returns the location
624 of the event.
625 If EVENT is a drag, this returns the drag's starting position.
626 The return value is of the form
627 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
628 The `posn-' functions access elements of such lists."
629 (if (consp event) (nth 1 event)
630 (list (selected-window) (point) '(0 . 0) 0)))
631
632 (defsubst event-end (event)
633 "Return the ending location of EVENT. EVENT should be a click or drag event.
634 If EVENT is a click event, this function is the same as `event-start'.
635 The return value is of the form
636 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
637 The `posn-' functions access elements of such lists."
638 (if (consp event) (nth (if (consp (nth 2 event)) 2 1) event)
639 (list (selected-window) (point) '(0 . 0) 0)))
640
641 (defsubst event-click-count (event)
642 "Return the multi-click count of EVENT, a click or drag event.
643 The return value is a positive integer."
644 (if (and (consp event) (integerp (nth 2 event))) (nth 2 event) 1))
645
646 (defsubst posn-window (position)
647 "Return the window in POSITION.
648 POSITION should be a list of the form
649 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
650 as returned by the `event-start' and `event-end' functions."
651 (nth 0 position))
652
653 (defsubst posn-point (position)
654 "Return the buffer location in POSITION.
655 POSITION should be a list of the form
656 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
657 as returned by the `event-start' and `event-end' functions."
658 (if (consp (nth 1 position))
659 (car (nth 1 position))
660 (nth 1 position)))
661
662 (defsubst posn-x-y (position)
663 "Return the x and y coordinates in POSITION.
664 POSITION should be a list of the form
665 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
666 as returned by the `event-start' and `event-end' functions."
667 (nth 2 position))
668
669 (defun posn-col-row (position)
670 "Return the column and row in POSITION, measured in characters.
671 POSITION should be a list of the form
672 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
673 as returned by the `event-start' and `event-end' functions.
674 For a scroll-bar event, the result column is 0, and the row
675 corresponds to the vertical position of the click in the scroll bar."
676 (let* ((pair (nth 2 position))
677 (window (posn-window position)))
678 (if (eq (if (consp (nth 1 position))
679 (car (nth 1 position))
680 (nth 1 position))
681 'vertical-scroll-bar)
682 (cons 0 (scroll-bar-scale pair (1- (window-height window))))
683 (if (eq (if (consp (nth 1 position))
684 (car (nth 1 position))
685 (nth 1 position))
686 'horizontal-scroll-bar)
687 (cons (scroll-bar-scale pair (window-width window)) 0)
688 (let* ((frame (if (framep window) window (window-frame window)))
689 (x (/ (car pair) (frame-char-width frame)))
690 (y (/ (cdr pair) (+ (frame-char-height frame)
691 (or (frame-parameter frame 'line-spacing)
692 default-line-spacing
693 0)))))
694 (cons x y))))))
695
696 (defsubst posn-timestamp (position)
697 "Return the timestamp of POSITION.
698 POSITION should be a list of the form
699 (WINDOW BUFFER-POSITION (X . Y) TIMESTAMP)
700 as returned by the `event-start' and `event-end' functions."
701 (nth 3 position))
702
703 \f
704 ;;;; Obsolescent names for functions.
705
706 (defalias 'dot 'point)
707 (defalias 'dot-marker 'point-marker)
708 (defalias 'dot-min 'point-min)
709 (defalias 'dot-max 'point-max)
710 (defalias 'window-dot 'window-point)
711 (defalias 'set-window-dot 'set-window-point)
712 (defalias 'read-input 'read-string)
713 (defalias 'send-string 'process-send-string)
714 (defalias 'send-region 'process-send-region)
715 (defalias 'show-buffer 'set-window-buffer)
716 (defalias 'buffer-flush-undo 'buffer-disable-undo)
717 (defalias 'eval-current-buffer 'eval-buffer)
718 (defalias 'compiled-function-p 'byte-code-function-p)
719 (defalias 'define-function 'defalias)
720
721 (defalias 'sref 'aref)
722 (make-obsolete 'sref 'aref "20.4")
723 (make-obsolete 'char-bytes "now always returns 1." "20.4")
724 (make-obsolete 'chars-in-region "use (abs (- BEG END))." "20.3")
725 (make-obsolete 'dot 'point "before 19.15")
726 (make-obsolete 'dot-max 'point-max "before 19.15")
727 (make-obsolete 'dot-min 'point-min "before 19.15")
728 (make-obsolete 'dot-marker 'point-marker "before 19.15")
729 (make-obsolete 'buffer-flush-undo 'buffer-disable-undo "before 19.15")
730 (make-obsolete 'baud-rate "use the baud-rate variable instead." "before 19.15")
731 (make-obsolete 'compiled-function-p 'byte-code-function-p "before 19.15")
732 (make-obsolete 'define-function 'defalias "20.1")
733
734 (defun insert-string (&rest args)
735 "Mocklisp-compatibility insert function.
736 Like the function `insert' except that any argument that is a number
737 is converted into a string by expressing it in decimal."
738 (dolist (el args)
739 (insert (if (integerp el) (number-to-string el) el))))
740 (make-obsolete 'insert-string 'insert "21.4")
741 (defun makehash (&optional test) (make-hash-table :test (or test 'eql)))
742 (make-obsolete 'makehash 'make-hash-table "21.4")
743
744 ;; Some programs still use this as a function.
745 (defun baud-rate ()
746 "Return the value of the `baud-rate' variable."
747 baud-rate)
748
749 (defalias 'focus-frame 'ignore)
750 (defalias 'unfocus-frame 'ignore)
751
752 \f
753 ;;;; Obsolescence declarations for variables.
754
755 (make-obsolete-variable 'directory-sep-char "do not use it." "21.1")
756 (make-obsolete-variable 'mode-line-inverse-video "use the appropriate faces instead." "21.1")
757 (make-obsolete-variable 'unread-command-char
758 "use `unread-command-events' instead. That variable is a list of events to reread, so it now uses nil to mean `no event', instead of -1."
759 "before 19.15")
760 (make-obsolete-variable 'executing-macro 'executing-kbd-macro "before 19.34")
761 (make-obsolete-variable 'post-command-idle-hook
762 "use timers instead, with `run-with-idle-timer'." "before 19.34")
763 (make-obsolete-variable 'post-command-idle-delay
764 "use timers instead, with `run-with-idle-timer'." "before 19.34")
765
766 \f
767 ;;;; Alternate names for functions - these are not being phased out.
768
769 (defalias 'string= 'string-equal)
770 (defalias 'string< 'string-lessp)
771 (defalias 'move-marker 'set-marker)
772 (defalias 'rplaca 'setcar)
773 (defalias 'rplacd 'setcdr)
774 (defalias 'beep 'ding) ;preserve lingual purity
775 (defalias 'indent-to-column 'indent-to)
776 (defalias 'backward-delete-char 'delete-backward-char)
777 (defalias 'search-forward-regexp (symbol-function 're-search-forward))
778 (defalias 'search-backward-regexp (symbol-function 're-search-backward))
779 (defalias 'int-to-string 'number-to-string)
780 (defalias 'store-match-data 'set-match-data)
781 (defalias 'make-variable-frame-localizable 'make-variable-frame-local)
782 ;; These are the XEmacs names:
783 (defalias 'point-at-eol 'line-end-position)
784 (defalias 'point-at-bol 'line-beginning-position)
785
786 ;;; Should this be an obsolete name? If you decide it should, you get
787 ;;; to go through all the sources and change them.
788 (defalias 'string-to-int 'string-to-number)
789 \f
790 ;;;; Hook manipulation functions.
791
792 (defun make-local-hook (hook)
793 "Make the hook HOOK local to the current buffer.
794 The return value is HOOK.
795
796 You never need to call this function now that `add-hook' does it for you
797 if its LOCAL argument is non-nil.
798
799 When a hook is local, its local and global values
800 work in concert: running the hook actually runs all the hook
801 functions listed in *either* the local value *or* the global value
802 of the hook variable.
803
804 This function works by making t a member of the buffer-local value,
805 which acts as a flag to run the hook functions in the default value as
806 well. This works for all normal hooks, but does not work for most
807 non-normal hooks yet. We will be changing the callers of non-normal
808 hooks so that they can handle localness; this has to be done one by
809 one.
810
811 This function does nothing if HOOK is already local in the current
812 buffer.
813
814 Do not use `make-local-variable' to make a hook variable buffer-local."
815 (if (local-variable-p hook)
816 nil
817 (or (boundp hook) (set hook nil))
818 (make-local-variable hook)
819 (set hook (list t)))
820 hook)
821 (make-obsolete 'make-local-hook "not necessary any more." "21.1")
822
823 (defun add-hook (hook function &optional append local)
824 "Add to the value of HOOK the function FUNCTION.
825 FUNCTION is not added if already present.
826 FUNCTION is added (if necessary) at the beginning of the hook list
827 unless the optional argument APPEND is non-nil, in which case
828 FUNCTION is added at the end.
829
830 The optional fourth argument, LOCAL, if non-nil, says to modify
831 the hook's buffer-local value rather than its default value.
832 This makes the hook buffer-local if needed, and it makes t a member
833 of the buffer-local value. That acts as a flag to run the hook
834 functions in the default value as well as in the local value.
835
836 HOOK should be a symbol, and FUNCTION may be any valid function. If
837 HOOK is void, it is first set to nil. If HOOK's value is a single
838 function, it is changed to a list of functions."
839 (or (boundp hook) (set hook nil))
840 (or (default-boundp hook) (set-default hook nil))
841 (if local (unless (local-variable-if-set-p hook)
842 (set (make-local-variable hook) (list t)))
843 ;; Detect the case where make-local-variable was used on a hook
844 ;; and do what we used to do.
845 (unless (and (consp (symbol-value hook)) (memq t (symbol-value hook)))
846 (setq local t)))
847 (let ((hook-value (if local (symbol-value hook) (default-value hook))))
848 ;; If the hook value is a single function, turn it into a list.
849 (when (or (not (listp hook-value)) (eq (car hook-value) 'lambda))
850 (setq hook-value (list hook-value)))
851 ;; Do the actual addition if necessary
852 (unless (member function hook-value)
853 (setq hook-value
854 (if append
855 (append hook-value (list function))
856 (cons function hook-value))))
857 ;; Set the actual variable
858 (if local (set hook hook-value) (set-default hook hook-value))))
859
860 (defun remove-hook (hook function &optional local)
861 "Remove from the value of HOOK the function FUNCTION.
862 HOOK should be a symbol, and FUNCTION may be any valid function. If
863 FUNCTION isn't the value of HOOK, or, if FUNCTION doesn't appear in the
864 list of hooks to run in HOOK, then nothing is done. See `add-hook'.
865
866 The optional third argument, LOCAL, if non-nil, says to modify
867 the hook's buffer-local value rather than its default value.
868 This makes the hook buffer-local if needed."
869 (or (boundp hook) (set hook nil))
870 (or (default-boundp hook) (set-default hook nil))
871 (if local (unless (local-variable-if-set-p hook)
872 (set (make-local-variable hook) (list t)))
873 ;; Detect the case where make-local-variable was used on a hook
874 ;; and do what we used to do.
875 (unless (and (consp (symbol-value hook)) (memq t (symbol-value hook)))
876 (setq local t)))
877 (let ((hook-value (if local (symbol-value hook) (default-value hook))))
878 ;; Remove the function, for both the list and the non-list cases.
879 (if (or (not (listp hook-value)) (eq (car hook-value) 'lambda))
880 (if (equal hook-value function) (setq hook-value nil))
881 (setq hook-value (delete function (copy-sequence hook-value))))
882 ;; If the function is on the global hook, we need to shadow it locally
883 ;;(when (and local (member function (default-value hook))
884 ;; (not (member (cons 'not function) hook-value)))
885 ;; (push (cons 'not function) hook-value))
886 ;; Set the actual variable
887 (if (not local)
888 (set-default hook hook-value)
889 (if (equal hook-value '(t))
890 (kill-local-variable hook)
891 (set hook hook-value)))))
892
893 (defun add-to-list (list-var element &optional append)
894 "Add to the value of LIST-VAR the element ELEMENT if it isn't there yet.
895 The test for presence of ELEMENT is done with `equal'.
896 If ELEMENT is added, it is added at the beginning of the list,
897 unless the optional argument APPEND is non-nil, in which case
898 ELEMENT is added at the end.
899
900 The return value is the new value of LIST-VAR.
901
902 If you want to use `add-to-list' on a variable that is not defined
903 until a certain package is loaded, you should put the call to `add-to-list'
904 into a hook function that will be run only after loading the package.
905 `eval-after-load' provides one way to do this. In some cases
906 other hooks, such as major mode hooks, can do the job."
907 (if (member element (symbol-value list-var))
908 (symbol-value list-var)
909 (set list-var
910 (if append
911 (append (symbol-value list-var) (list element))
912 (cons element (symbol-value list-var))))))
913
914 \f
915 ;;; Load history
916
917 ;;; (defvar symbol-file-load-history-loaded nil
918 ;;; "Non-nil means we have loaded the file `fns-VERSION.el' in `exec-directory'.
919 ;;; That file records the part of `load-history' for preloaded files,
920 ;;; which is cleared out before dumping to make Emacs smaller.")
921
922 ;;; (defun load-symbol-file-load-history ()
923 ;;; "Load the file `fns-VERSION.el' in `exec-directory' if not already done.
924 ;;; That file records the part of `load-history' for preloaded files,
925 ;;; which is cleared out before dumping to make Emacs smaller."
926 ;;; (unless symbol-file-load-history-loaded
927 ;;; (load (expand-file-name
928 ;;; ;; fns-XX.YY.ZZ.el does not work on DOS filesystem.
929 ;;; (if (eq system-type 'ms-dos)
930 ;;; "fns.el"
931 ;;; (format "fns-%s.el" emacs-version))
932 ;;; exec-directory)
933 ;;; ;; The file name fns-%s.el already has a .el extension.
934 ;;; nil nil t)
935 ;;; (setq symbol-file-load-history-loaded t)))
936
937 (defun symbol-file (function)
938 "Return the input source from which FUNCTION was loaded.
939 The value is normally a string that was passed to `load':
940 either an absolute file name, or a library name
941 \(with no directory name and no `.el' or `.elc' at the end).
942 It can also be nil, if the definition is not associated with any file."
943 (if (and (symbolp function) (fboundp function)
944 (eq 'autoload (car-safe (symbol-function function))))
945 (nth 1 (symbol-function function))
946 (let ((files load-history)
947 file)
948 (while files
949 (if (member function (cdr (car files)))
950 (setq file (car (car files)) files nil))
951 (setq files (cdr files)))
952 file)))
953
954 \f
955 ;;;; Specifying things to do after certain files are loaded.
956
957 (defun eval-after-load (file form)
958 "Arrange that, if FILE is ever loaded, FORM will be run at that time.
959 This makes or adds to an entry on `after-load-alist'.
960 If FILE is already loaded, evaluate FORM right now.
961 It does nothing if FORM is already on the list for FILE.
962 FILE must match exactly. Normally FILE is the name of a library,
963 with no directory or extension specified, since that is how `load'
964 is normally called.
965 FILE can also be a feature (i.e. a symbol), in which case FORM is
966 evaluated whenever that feature is `provide'd."
967 (let ((elt (assoc file after-load-alist)))
968 ;; Make sure there is an element for FILE.
969 (unless elt (setq elt (list file)) (push elt after-load-alist))
970 ;; Add FORM to the element if it isn't there.
971 (unless (member form (cdr elt))
972 (nconc elt (list form))
973 ;; If the file has been loaded already, run FORM right away.
974 (if (if (symbolp file)
975 (featurep file)
976 ;; Make sure `load-history' contains the files dumped with
977 ;; Emacs for the case that FILE is one of them.
978 ;; (load-symbol-file-load-history)
979 (assoc file load-history))
980 (eval form))))
981 form)
982
983 (defun eval-next-after-load (file)
984 "Read the following input sexp, and run it whenever FILE is loaded.
985 This makes or adds to an entry on `after-load-alist'.
986 FILE should be the name of a library, with no directory name."
987 (eval-after-load file (read)))
988 \f
989 ;;; make-network-process wrappers
990
991 (if (featurep 'make-network-process)
992 (progn
993
994 (defun open-network-stream (name buffer host service)
995 "Open a TCP connection for a service to a host.
996 Returns a subprocess-object to represent the connection.
997 Input and output work as for subprocesses; `delete-process' closes it.
998 Args are NAME BUFFER HOST SERVICE.
999 NAME is name for process. It is modified if necessary to make it unique.
1000 BUFFER is the buffer (or buffer-name) to associate with the process.
1001 Process output goes at end of that buffer, unless you specify
1002 an output stream or filter function to handle the output.
1003 BUFFER may be also nil, meaning that this process is not associated
1004 with any buffer
1005 Third arg is name of the host to connect to, or its IP address.
1006 Fourth arg SERVICE is name of the service desired, or an integer
1007 specifying a port number to connect to."
1008 (make-network-process :name name :buffer buffer
1009 :host host :service service))
1010
1011 (defun open-network-stream-nowait (name buffer host service &optional sentinel filter)
1012 "Initiate connection to a TCP connection for a service to a host.
1013 It returns nil if non-blocking connects are not supported; otherwise,
1014 it returns a subprocess-object to represent the connection.
1015
1016 This function is similar to `open-network-stream', except that this
1017 function returns before the connection is established. When the
1018 connection is completed, the sentinel function will be called with
1019 second arg matching `open' (if successful) or `failed' (on error).
1020
1021 Args are NAME BUFFER HOST SERVICE SENTINEL FILTER.
1022 NAME, BUFFER, HOST, and SERVICE are as for `open-network-stream'.
1023 Optional args, SENTINEL and FILTER specifies the sentinel and filter
1024 functions to be used for this network stream."
1025 (if (featurep 'make-network-process '(:nowait t))
1026 (make-network-process :name name :buffer buffer :nowait t
1027 :host host :service service
1028 :filter filter :sentinel sentinel)))
1029
1030 (defun open-network-stream-server (name buffer service &optional sentinel filter)
1031 "Create a network server process for a TCP service.
1032 It returns nil if server processes are not supported; otherwise,
1033 it returns a subprocess-object to represent the server.
1034
1035 When a client connects to the specified service, a new subprocess
1036 is created to handle the new connection, and the sentinel function
1037 is called for the new process.
1038
1039 Args are NAME BUFFER SERVICE SENTINEL FILTER.
1040 NAME is name for the server process. Client processes are named by
1041 appending the ip-address and port number of the client to NAME.
1042 BUFFER is the buffer (or buffer-name) to associate with the server
1043 process. Client processes will not get a buffer if a process filter
1044 is specified or BUFFER is nil; otherwise, a new buffer is created for
1045 the client process. The name is similar to the process name.
1046 Third arg SERVICE is name of the service desired, or an integer
1047 specifying a port number to connect to. It may also be t to selected
1048 an unused port number for the server.
1049 Optional args, SENTINEL and FILTER specifies the sentinel and filter
1050 functions to be used for the client processes; the server process
1051 does not use these function."
1052 (if (featurep 'make-network-process '(:server t))
1053 (make-network-process :name name :buffer buffer
1054 :service service :server t :noquery t
1055 :sentinel sentinel :filter filter)))
1056
1057 )) ;; (featurep 'make-network-process)
1058
1059
1060 ;; compatibility
1061
1062 (defun process-kill-without-query (process &optional flag)
1063 "Say no query needed if PROCESS is running when Emacs is exited.
1064 Optional second argument if non-nil says to require a query.
1065 Value is t if a query was formerly required.
1066 New code should not use this function; use `process-query-on-exit-flag'
1067 or `set-process-query-on-exit-flag' instead."
1068 (let ((old (process-query-on-exit-flag process)))
1069 (set-process-query-on-exit-flag process nil)
1070 old))
1071
1072 ;; process plist management
1073
1074 (defun process-get (process propname)
1075 "Return the value of PROCESS' PROPNAME property.
1076 This is the last value stored with `(process-put PROCESS PROPNAME VALUE)'."
1077 (plist-get (process-plist process) propname))
1078
1079 (defun process-put (process propname value)
1080 "Change PROCESS' PROPNAME property to VALUE.
1081 It can be retrieved with `(process-get PROCESS PROPNAME)'."
1082 (set-process-plist process
1083 (plist-put (process-plist process) propname value)))
1084
1085 \f
1086 ;;;; Input and display facilities.
1087
1088 (defvar read-quoted-char-radix 8
1089 "*Radix for \\[quoted-insert] and other uses of `read-quoted-char'.
1090 Legitimate radix values are 8, 10 and 16.")
1091
1092 (custom-declare-variable-early
1093 'read-quoted-char-radix 8
1094 "*Radix for \\[quoted-insert] and other uses of `read-quoted-char'.
1095 Legitimate radix values are 8, 10 and 16."
1096 :type '(choice (const 8) (const 10) (const 16))
1097 :group 'editing-basics)
1098
1099 (defun read-quoted-char (&optional prompt)
1100 "Like `read-char', but do not allow quitting.
1101 Also, if the first character read is an octal digit,
1102 we read any number of octal digits and return the
1103 specified character code. Any nondigit terminates the sequence.
1104 If the terminator is RET, it is discarded;
1105 any other terminator is used itself as input.
1106
1107 The optional argument PROMPT specifies a string to use to prompt the user.
1108 The variable `read-quoted-char-radix' controls which radix to use
1109 for numeric input."
1110 (let ((message-log-max nil) done (first t) (code 0) char translated)
1111 (while (not done)
1112 (let ((inhibit-quit first)
1113 ;; Don't let C-h get the help message--only help function keys.
1114 (help-char nil)
1115 (help-form
1116 "Type the special character you want to use,
1117 or the octal character code.
1118 RET terminates the character code and is discarded;
1119 any other non-digit terminates the character code and is then used as input."))
1120 (setq char (read-event (and prompt (format "%s-" prompt)) t))
1121 (if inhibit-quit (setq quit-flag nil)))
1122 ;; Translate TAB key into control-I ASCII character, and so on.
1123 ;; Note: `read-char' does it using the `ascii-character' property.
1124 ;; We could try and use read-key-sequence instead, but then C-q ESC
1125 ;; or C-q C-x might not return immediately since ESC or C-x might be
1126 ;; bound to some prefix in function-key-map or key-translation-map.
1127 (setq translated char)
1128 (let ((translation (lookup-key function-key-map (vector char))))
1129 (if (arrayp translation)
1130 (setq translated (aref translation 0))))
1131 (cond ((null translated))
1132 ((not (integerp translated))
1133 (setq unread-command-events (list char)
1134 done t))
1135 ((/= (logand translated ?\M-\^@) 0)
1136 ;; Turn a meta-character into a character with the 0200 bit set.
1137 (setq code (logior (logand translated (lognot ?\M-\^@)) 128)
1138 done t))
1139 ((and (<= ?0 translated) (< translated (+ ?0 (min 10 read-quoted-char-radix))))
1140 (setq code (+ (* code read-quoted-char-radix) (- translated ?0)))
1141 (and prompt (setq prompt (message "%s %c" prompt translated))))
1142 ((and (<= ?a (downcase translated))
1143 (< (downcase translated) (+ ?a -10 (min 26 read-quoted-char-radix))))
1144 (setq code (+ (* code read-quoted-char-radix)
1145 (+ 10 (- (downcase translated) ?a))))
1146 (and prompt (setq prompt (message "%s %c" prompt translated))))
1147 ((and (not first) (eq translated ?\C-m))
1148 (setq done t))
1149 ((not first)
1150 (setq unread-command-events (list char)
1151 done t))
1152 (t (setq code translated
1153 done t)))
1154 (setq first nil))
1155 code))
1156
1157 (defun read-passwd (prompt &optional confirm default)
1158 "Read a password, prompting with PROMPT. Echo `.' for each character typed.
1159 End with RET, LFD, or ESC. DEL or C-h rubs out. C-u kills line.
1160 Optional argument CONFIRM, if non-nil, then read it twice to make sure.
1161 Optional DEFAULT is a default password to use instead of empty input."
1162 (if confirm
1163 (let (success)
1164 (while (not success)
1165 (let ((first (read-passwd prompt nil default))
1166 (second (read-passwd "Confirm password: " nil default)))
1167 (if (equal first second)
1168 (progn
1169 (and (arrayp second) (fillarray second ?\0))
1170 (setq success first))
1171 (and (arrayp first) (fillarray first ?\0))
1172 (and (arrayp second) (fillarray second ?\0))
1173 (message "Password not repeated accurately; please start over")
1174 (sit-for 1))))
1175 success)
1176 (let ((pass nil)
1177 (c 0)
1178 (echo-keystrokes 0)
1179 (cursor-in-echo-area t))
1180 (while (progn (message "%s%s"
1181 prompt
1182 (make-string (length pass) ?.))
1183 (setq c (read-char-exclusive nil t))
1184 (and (/= c ?\r) (/= c ?\n) (/= c ?\e)))
1185 (clear-this-command-keys)
1186 (if (= c ?\C-u)
1187 (progn
1188 (and (arrayp pass) (fillarray pass ?\0))
1189 (setq pass ""))
1190 (if (and (/= c ?\b) (/= c ?\177))
1191 (let* ((new-char (char-to-string c))
1192 (new-pass (concat pass new-char)))
1193 (and (arrayp pass) (fillarray pass ?\0))
1194 (fillarray new-char ?\0)
1195 (setq c ?\0)
1196 (setq pass new-pass))
1197 (if (> (length pass) 0)
1198 (let ((new-pass (substring pass 0 -1)))
1199 (and (arrayp pass) (fillarray pass ?\0))
1200 (setq pass new-pass))))))
1201 (message nil)
1202 (or pass default ""))))
1203 \f
1204 ;;; Atomic change groups.
1205
1206 (defmacro atomic-change-group (&rest body)
1207 "Perform BODY as an atomic change group.
1208 This means that if BODY exits abnormally,
1209 all of its changes to the current buffer are undone.
1210 This works regardless of whether undo is enabled in the buffer.
1211
1212 This mechanism is transparent to ordinary use of undo;
1213 if undo is enabled in the buffer and BODY succeeds, the
1214 user can undo the change normally."
1215 (let ((handle (make-symbol "--change-group-handle--"))
1216 (success (make-symbol "--change-group-success--")))
1217 `(let ((,handle (prepare-change-group))
1218 (,success nil))
1219 (unwind-protect
1220 (progn
1221 ;; This is inside the unwind-protect because
1222 ;; it enables undo if that was disabled; we need
1223 ;; to make sure that it gets disabled again.
1224 (activate-change-group ,handle)
1225 ,@body
1226 (setq ,success t))
1227 ;; Either of these functions will disable undo
1228 ;; if it was disabled before.
1229 (if ,success
1230 (accept-change-group ,handle)
1231 (cancel-change-group ,handle))))))
1232
1233 (defun prepare-change-group (&optional buffer)
1234 "Return a handle for the current buffer's state, for a change group.
1235 If you specify BUFFER, make a handle for BUFFER's state instead.
1236
1237 Pass the handle to `activate-change-group' afterward to initiate
1238 the actual changes of the change group.
1239
1240 To finish the change group, call either `accept-change-group' or
1241 `cancel-change-group' passing the same handle as argument. Call
1242 `accept-change-group' to accept the changes in the group as final;
1243 call `cancel-change-group' to undo them all. You should use
1244 `unwind-protect' to make sure the group is always finished. The call
1245 to `activate-change-group' should be inside the `unwind-protect'.
1246 Once you finish the group, don't use the handle again--don't try to
1247 finish the same group twice. For a simple example of correct use, see
1248 the source code of `atomic-change-group'.
1249
1250 The handle records only the specified buffer. To make a multibuffer
1251 change group, call this function once for each buffer you want to
1252 cover, then use `nconc' to combine the returned values, like this:
1253
1254 (nconc (prepare-change-group buffer-1)
1255 (prepare-change-group buffer-2))
1256
1257 You can then activate that multibuffer change group with a single
1258 call to `activate-change-group' and finish it with a single call
1259 to `accept-change-group' or `cancel-change-group'."
1260
1261 (list (cons (current-buffer) buffer-undo-list)))
1262
1263 (defun activate-change-group (handle)
1264 "Activate a change group made with `prepare-change-group' (which see)."
1265 (dolist (elt handle)
1266 (with-current-buffer (car elt)
1267 (if (eq buffer-undo-list t)
1268 (setq buffer-undo-list nil)))))
1269
1270 (defun accept-change-group (handle)
1271 "Finish a change group made with `prepare-change-group' (which see).
1272 This finishes the change group by accepting its changes as final."
1273 (dolist (elt handle)
1274 (with-current-buffer (car elt)
1275 (if (eq elt t)
1276 (setq buffer-undo-list t)))))
1277
1278 (defun cancel-change-group (handle)
1279 "Finish a change group made with `prepare-change-group' (which see).
1280 This finishes the change group by reverting all of its changes."
1281 (dolist (elt handle)
1282 (with-current-buffer (car elt)
1283 (setq elt (cdr elt))
1284 (let ((old-car
1285 (if (consp elt) (car elt)))
1286 (old-cdr
1287 (if (consp elt) (cdr elt))))
1288 ;; Temporarily truncate the undo log at ELT.
1289 (when (consp elt)
1290 (setcar elt nil) (setcdr elt nil))
1291 (unless (eq last-command 'undo) (undo-start))
1292 ;; Make sure there's no confusion.
1293 (when (and (consp elt) (not (eq elt (last pending-undo-list))))
1294 (error "Undoing to some unrelated state"))
1295 ;; Undo it all.
1296 (while pending-undo-list (undo-more 1))
1297 ;; Reset the modified cons cell ELT to its original content.
1298 (when (consp elt)
1299 (setcar elt old-car)
1300 (setcdr elt old-cdr))
1301 ;; Revert the undo info to what it was when we grabbed the state.
1302 (setq buffer-undo-list elt)))))
1303 \f
1304 ;; For compatibility.
1305 (defalias 'redraw-modeline 'force-mode-line-update)
1306
1307 (defun force-mode-line-update (&optional all)
1308 "Force the mode line of the current buffer to be redisplayed.
1309 With optional non-nil ALL, force redisplay of all mode lines."
1310 (if all (save-excursion (set-buffer (other-buffer))))
1311 (set-buffer-modified-p (buffer-modified-p)))
1312
1313 (defun momentary-string-display (string pos &optional exit-char message)
1314 "Momentarily display STRING in the buffer at POS.
1315 Display remains until next character is typed.
1316 If the char is EXIT-CHAR (optional third arg, default is SPC) it is swallowed;
1317 otherwise it is then available as input (as a command if nothing else).
1318 Display MESSAGE (optional fourth arg) in the echo area.
1319 If MESSAGE is nil, instructions to type EXIT-CHAR are displayed there."
1320 (or exit-char (setq exit-char ?\ ))
1321 (let ((inhibit-read-only t)
1322 ;; Don't modify the undo list at all.
1323 (buffer-undo-list t)
1324 (modified (buffer-modified-p))
1325 (name buffer-file-name)
1326 insert-end)
1327 (unwind-protect
1328 (progn
1329 (save-excursion
1330 (goto-char pos)
1331 ;; defeat file locking... don't try this at home, kids!
1332 (setq buffer-file-name nil)
1333 (insert-before-markers string)
1334 (setq insert-end (point))
1335 ;; If the message end is off screen, recenter now.
1336 (if (< (window-end nil t) insert-end)
1337 (recenter (/ (window-height) 2)))
1338 ;; If that pushed message start off the screen,
1339 ;; scroll to start it at the top of the screen.
1340 (move-to-window-line 0)
1341 (if (> (point) pos)
1342 (progn
1343 (goto-char pos)
1344 (recenter 0))))
1345 (message (or message "Type %s to continue editing.")
1346 (single-key-description exit-char))
1347 (let ((char (read-event)))
1348 (or (eq char exit-char)
1349 (setq unread-command-events (list char)))))
1350 (if insert-end
1351 (save-excursion
1352 (delete-region pos insert-end)))
1353 (setq buffer-file-name name)
1354 (set-buffer-modified-p modified))))
1355
1356 \f
1357 ;;;; Overlay operations
1358
1359 (defun copy-overlay (o)
1360 "Return a copy of overlay O."
1361 (let ((o1 (make-overlay (overlay-start o) (overlay-end o)
1362 ;; FIXME: there's no easy way to find the
1363 ;; insertion-type of the two markers.
1364 (overlay-buffer o)))
1365 (props (overlay-properties o)))
1366 (while props
1367 (overlay-put o1 (pop props) (pop props)))
1368 o1))
1369
1370 (defun remove-overlays (beg end name val)
1371 "Clear BEG and END of overlays whose property NAME has value VAL.
1372 Overlays might be moved and or split."
1373 (if (< end beg)
1374 (setq beg (prog1 end (setq end beg))))
1375 (save-excursion
1376 (dolist (o (overlays-in beg end))
1377 (when (eq (overlay-get o name) val)
1378 ;; Either push this overlay outside beg...end
1379 ;; or split it to exclude beg...end
1380 ;; or delete it entirely (if it is contained in beg...end).
1381 (if (< (overlay-start o) beg)
1382 (if (> (overlay-end o) end)
1383 (progn
1384 (move-overlay (copy-overlay o)
1385 (overlay-start o) beg)
1386 (move-overlay o end (overlay-end o)))
1387 (move-overlay o (overlay-start o) beg))
1388 (if (> (overlay-end o) end)
1389 (move-overlay o end (overlay-end o))
1390 (delete-overlay o)))))))
1391 \f
1392 ;;;; Miscellanea.
1393
1394 ;; A number of major modes set this locally.
1395 ;; Give it a global value to avoid compiler warnings.
1396 (defvar font-lock-defaults nil)
1397
1398 (defvar suspend-hook nil
1399 "Normal hook run by `suspend-emacs', before suspending.")
1400
1401 (defvar suspend-resume-hook nil
1402 "Normal hook run by `suspend-emacs', after Emacs is continued.")
1403
1404 (defvar temp-buffer-show-hook nil
1405 "Normal hook run by `with-output-to-temp-buffer' after displaying the buffer.
1406 When the hook runs, the temporary buffer is current, and the window it
1407 was displayed in is selected. This hook is normally set up with a
1408 function to make the buffer read only, and find function names and
1409 variable names in it, provided the major mode is still Help mode.")
1410
1411 (defvar temp-buffer-setup-hook nil
1412 "Normal hook run by `with-output-to-temp-buffer' at the start.
1413 When the hook runs, the temporary buffer is current.
1414 This hook is normally set up with a function to put the buffer in Help
1415 mode.")
1416
1417 ;; Avoid compiler warnings about this variable,
1418 ;; which has a special meaning on certain system types.
1419 (defvar buffer-file-type nil
1420 "Non-nil if the visited file is a binary file.
1421 This variable is meaningful on MS-DOG and Windows NT.
1422 On those systems, it is automatically local in every buffer.
1423 On other systems, this variable is normally always nil.")
1424
1425 ;; This should probably be written in C (i.e., without using `walk-windows').
1426 (defun get-buffer-window-list (buffer &optional minibuf frame)
1427 "Return windows currently displaying BUFFER, or nil if none.
1428 See `walk-windows' for the meaning of MINIBUF and FRAME."
1429 (let ((buffer (if (bufferp buffer) buffer (get-buffer buffer))) windows)
1430 (walk-windows (function (lambda (window)
1431 (if (eq (window-buffer window) buffer)
1432 (setq windows (cons window windows)))))
1433 minibuf frame)
1434 windows))
1435
1436 (defun ignore (&rest ignore)
1437 "Do nothing and return nil.
1438 This function accepts any number of arguments, but ignores them."
1439 (interactive)
1440 nil)
1441
1442 (defun error (&rest args)
1443 "Signal an error, making error message by passing all args to `format'.
1444 In Emacs, the convention is that error messages start with a capital
1445 letter but *do not* end with a period. Please follow this convention
1446 for the sake of consistency."
1447 (while t
1448 (signal 'error (list (apply 'format args)))))
1449
1450 (defalias 'user-original-login-name 'user-login-name)
1451
1452 (defvar yank-excluded-properties)
1453
1454 (defun remove-yank-excluded-properties (start end)
1455 "Remove `yank-excluded-properties' between START and END positions.
1456 Replaces `category' properties with their defined properties."
1457 (let ((inhibit-read-only t))
1458 ;; Replace any `category' property with the properties it stands for.
1459 (unless (memq yank-excluded-properties '(t nil))
1460 (save-excursion
1461 (goto-char start)
1462 (while (< (point) end)
1463 (let ((cat (get-text-property (point) 'category))
1464 run-end)
1465 (setq run-end
1466 (next-single-property-change (point) 'category nil end))
1467 (when cat
1468 (let (run-end2 original)
1469 (remove-list-of-text-properties (point) run-end '(category))
1470 (while (< (point) run-end)
1471 (setq run-end2 (next-property-change (point) nil run-end))
1472 (setq original (text-properties-at (point)))
1473 (set-text-properties (point) run-end2 (symbol-plist cat))
1474 (add-text-properties (point) run-end2 original)
1475 (goto-char run-end2))))
1476 (goto-char run-end)))))
1477 (if (eq yank-excluded-properties t)
1478 (set-text-properties start end nil)
1479 (remove-list-of-text-properties start end yank-excluded-properties))))
1480
1481 (defvar yank-undo-function)
1482
1483 (defun insert-for-yank (string)
1484 "Insert STRING at point, stripping some text properties.
1485 Strip text properties from the inserted text according to
1486 `yank-excluded-properties'. Otherwise just like (insert STRING).
1487
1488 If STRING has a non-nil `yank-handler' property on the first character,
1489 the normal insert behaviour is modified in various ways. The value of
1490 the yank-handler property must be a list with one to five elements
1491 with the following format: (FUNCTION PARAM NOEXCLUDE UNDO).
1492 When FUNCTION is present and non-nil, it is called instead of `insert'
1493 to insert the string. FUNCTION takes one argument--the object to insert.
1494 If PARAM is present and non-nil, it replaces STRING as the object
1495 passed to FUNCTION (or `insert'); for example, if FUNCTION is
1496 `yank-rectangle', PARAM may be a list of strings to insert as a
1497 rectangle.
1498 If NOEXCLUDE is present and non-nil, the normal removal of the
1499 yank-excluded-properties is not performed; instead FUNCTION is
1500 responsible for removing those properties. This may be necessary
1501 if FUNCTION adjusts point before or after inserting the object.
1502 If UNDO is present and non-nil, it is a function that will be called
1503 by `yank-pop' to undo the insertion of the current object. It is
1504 called with two arguments, the start and end of the current region.
1505 FUNCTION may set `yank-undo-function' to override the UNDO value."
1506 (let* ((handler (and (stringp string)
1507 (get-text-property 0 'yank-handler string)))
1508 (param (or (nth 1 handler) string))
1509 (opoint (point)))
1510 (setq yank-undo-function t)
1511 (if (nth 0 handler) ;; FUNCTION
1512 (funcall (car handler) param)
1513 (insert param))
1514 (unless (nth 2 handler) ;; NOEXCLUDE
1515 (remove-yank-excluded-properties opoint (point)))
1516 (if (eq yank-undo-function t) ;; not set by FUNCTION
1517 (setq yank-undo-function (nth 3 handler))) ;; UNDO
1518 (if (nth 4 handler) ;; COMMAND
1519 (setq this-command (nth 4 handler)))))
1520
1521 (defun insert-buffer-substring-no-properties (buf &optional start end)
1522 "Insert before point a substring of buffer BUFFER, without text properties.
1523 BUFFER may be a buffer or a buffer name.
1524 Arguments START and END are character numbers specifying the substring.
1525 They default to the beginning and the end of BUFFER."
1526 (let ((opoint (point)))
1527 (insert-buffer-substring buf start end)
1528 (let ((inhibit-read-only t))
1529 (set-text-properties opoint (point) nil))))
1530
1531 (defun insert-buffer-substring-as-yank (buf &optional start end)
1532 "Insert before point a part of buffer BUFFER, stripping some text properties.
1533 BUFFER may be a buffer or a buffer name. Arguments START and END are
1534 character numbers specifying the substring. They default to the
1535 beginning and the end of BUFFER. Strip text properties from the
1536 inserted text according to `yank-excluded-properties'."
1537 (let ((opoint (point)))
1538 (insert-buffer-substring buf start end)
1539 (remove-yank-excluded-properties opoint (point))))
1540
1541 \f
1542 ;; Synchronous shell commands.
1543
1544 (defun start-process-shell-command (name buffer &rest args)
1545 "Start a program in a subprocess. Return the process object for it.
1546 Args are NAME BUFFER COMMAND &rest COMMAND-ARGS.
1547 NAME is name for process. It is modified if necessary to make it unique.
1548 BUFFER is the buffer or (buffer-name) to associate with the process.
1549 Process output goes at end of that buffer, unless you specify
1550 an output stream or filter function to handle the output.
1551 BUFFER may be also nil, meaning that this process is not associated
1552 with any buffer
1553 Third arg is command name, the name of a shell command.
1554 Remaining arguments are the arguments for the command.
1555 Wildcards and redirection are handled as usual in the shell."
1556 (cond
1557 ((eq system-type 'vax-vms)
1558 (apply 'start-process name buffer args))
1559 ;; We used to use `exec' to replace the shell with the command,
1560 ;; but that failed to handle (...) and semicolon, etc.
1561 (t
1562 (start-process name buffer shell-file-name shell-command-switch
1563 (mapconcat 'identity args " ")))))
1564
1565 (defun call-process-shell-command (command &optional infile buffer display
1566 &rest args)
1567 "Execute the shell command COMMAND synchronously in separate process.
1568 The remaining arguments are optional.
1569 The program's input comes from file INFILE (nil means `/dev/null').
1570 Insert output in BUFFER before point; t means current buffer;
1571 nil for BUFFER means discard it; 0 means discard and don't wait.
1572 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,
1573 REAL-BUFFER says what to do with standard output, as above,
1574 while STDERR-FILE says what to do with standard error in the child.
1575 STDERR-FILE may be nil (discard standard error output),
1576 t (mix it with ordinary output), or a file name string.
1577
1578 Fourth arg DISPLAY non-nil means redisplay buffer as output is inserted.
1579 Remaining arguments are strings passed as additional arguments for COMMAND.
1580 Wildcards and redirection are handled as usual in the shell.
1581
1582 If BUFFER is 0, `call-process-shell-command' returns immediately with value nil.
1583 Otherwise it waits for COMMAND to terminate and returns a numeric exit
1584 status or a signal description string.
1585 If you quit, the process is killed with SIGINT, or SIGKILL if you quit again."
1586 (cond
1587 ((eq system-type 'vax-vms)
1588 (apply 'call-process command infile buffer display args))
1589 ;; We used to use `exec' to replace the shell with the command,
1590 ;; but that failed to handle (...) and semicolon, etc.
1591 (t
1592 (call-process shell-file-name
1593 infile buffer display
1594 shell-command-switch
1595 (mapconcat 'identity (cons command args) " ")))))
1596 \f
1597 (defmacro with-current-buffer (buffer &rest body)
1598 "Execute the forms in BODY with BUFFER as the current buffer.
1599 The value returned is the value of the last form in BODY.
1600 See also `with-temp-buffer'."
1601 (cons 'save-current-buffer
1602 (cons (list 'set-buffer buffer)
1603 body)))
1604
1605 (defmacro with-temp-file (file &rest body)
1606 "Create a new buffer, evaluate BODY there, and write the buffer to FILE.
1607 The value returned is the value of the last form in BODY.
1608 See also `with-temp-buffer'."
1609 (let ((temp-file (make-symbol "temp-file"))
1610 (temp-buffer (make-symbol "temp-buffer")))
1611 `(let ((,temp-file ,file)
1612 (,temp-buffer
1613 (get-buffer-create (generate-new-buffer-name " *temp file*"))))
1614 (unwind-protect
1615 (prog1
1616 (with-current-buffer ,temp-buffer
1617 ,@body)
1618 (with-current-buffer ,temp-buffer
1619 (widen)
1620 (write-region (point-min) (point-max) ,temp-file nil 0)))
1621 (and (buffer-name ,temp-buffer)
1622 (kill-buffer ,temp-buffer))))))
1623
1624 (defmacro with-temp-message (message &rest body)
1625 "Display MESSAGE temporarily if non-nil while BODY is evaluated.
1626 The original message is restored to the echo area after BODY has finished.
1627 The value returned is the value of the last form in BODY.
1628 MESSAGE is written to the message log buffer if `message-log-max' is non-nil.
1629 If MESSAGE is nil, the echo area and message log buffer are unchanged.
1630 Use a MESSAGE of \"\" to temporarily clear the echo area."
1631 (let ((current-message (make-symbol "current-message"))
1632 (temp-message (make-symbol "with-temp-message")))
1633 `(let ((,temp-message ,message)
1634 (,current-message))
1635 (unwind-protect
1636 (progn
1637 (when ,temp-message
1638 (setq ,current-message (current-message))
1639 (message "%s" ,temp-message))
1640 ,@body)
1641 (and ,temp-message
1642 (if ,current-message
1643 (message "%s" ,current-message)
1644 (message nil)))))))
1645
1646 (defmacro with-temp-buffer (&rest body)
1647 "Create a temporary buffer, and evaluate BODY there like `progn'.
1648 See also `with-temp-file' and `with-output-to-string'."
1649 (let ((temp-buffer (make-symbol "temp-buffer")))
1650 `(let ((,temp-buffer
1651 (get-buffer-create (generate-new-buffer-name " *temp*"))))
1652 (unwind-protect
1653 (with-current-buffer ,temp-buffer
1654 ,@body)
1655 (and (buffer-name ,temp-buffer)
1656 (kill-buffer ,temp-buffer))))))
1657
1658 (defmacro with-output-to-string (&rest body)
1659 "Execute BODY, return the text it sent to `standard-output', as a string."
1660 `(let ((standard-output
1661 (get-buffer-create (generate-new-buffer-name " *string-output*"))))
1662 (let ((standard-output standard-output))
1663 ,@body)
1664 (with-current-buffer standard-output
1665 (prog1
1666 (buffer-string)
1667 (kill-buffer nil)))))
1668
1669 (defmacro with-local-quit (&rest body)
1670 "Execute BODY with `inhibit-quit' temporarily bound to nil."
1671 (declare (debug t) (indent 0))
1672 `(condition-case nil
1673 (let ((inhibit-quit nil))
1674 ,@body)
1675 (quit (setq quit-flag t))))
1676
1677 (defmacro combine-after-change-calls (&rest body)
1678 "Execute BODY, but don't call the after-change functions till the end.
1679 If BODY makes changes in the buffer, they are recorded
1680 and the functions on `after-change-functions' are called several times
1681 when BODY is finished.
1682 The return value is the value of the last form in BODY.
1683
1684 If `before-change-functions' is non-nil, then calls to the after-change
1685 functions can't be deferred, so in that case this macro has no effect.
1686
1687 Do not alter `after-change-functions' or `before-change-functions'
1688 in BODY."
1689 `(unwind-protect
1690 (let ((combine-after-change-calls t))
1691 . ,body)
1692 (combine-after-change-execute)))
1693
1694
1695 (defvar delay-mode-hooks nil
1696 "If non-nil, `run-mode-hooks' should delay running the hooks.")
1697 (defvar delayed-mode-hooks nil
1698 "List of delayed mode hooks waiting to be run.")
1699 (make-variable-buffer-local 'delayed-mode-hooks)
1700
1701 (defun run-mode-hooks (&rest hooks)
1702 "Run mode hooks `delayed-mode-hooks' and HOOKS, or delay HOOKS.
1703 Execution is delayed if `delay-mode-hooks' is non-nil.
1704 Major mode functions should use this."
1705 (if delay-mode-hooks
1706 ;; Delaying case.
1707 (dolist (hook hooks)
1708 (push hook delayed-mode-hooks))
1709 ;; Normal case, just run the hook as before plus any delayed hooks.
1710 (setq hooks (nconc (nreverse delayed-mode-hooks) hooks))
1711 (setq delayed-mode-hooks nil)
1712 (apply 'run-hooks hooks)))
1713
1714 (defmacro delay-mode-hooks (&rest body)
1715 "Execute BODY, but delay any `run-mode-hooks'.
1716 Only affects hooks run in the current buffer."
1717 `(progn
1718 (make-local-variable 'delay-mode-hooks)
1719 (let ((delay-mode-hooks t))
1720 ,@body)))
1721
1722 ;; PUBLIC: find if the current mode derives from another.
1723
1724 (defun derived-mode-p (&rest modes)
1725 "Non-nil if the current major mode is derived from one of MODES.
1726 Uses the `derived-mode-parent' property of the symbol to trace backwards."
1727 (let ((parent major-mode))
1728 (while (and (not (memq parent modes))
1729 (setq parent (get parent 'derived-mode-parent))))
1730 parent))
1731
1732 (defmacro with-syntax-table (table &rest body)
1733 "Evaluate BODY with syntax table of current buffer set to TABLE.
1734 The syntax table of the current buffer is saved, BODY is evaluated, and the
1735 saved table is restored, even in case of an abnormal exit.
1736 Value is what BODY returns."
1737 (let ((old-table (make-symbol "table"))
1738 (old-buffer (make-symbol "buffer")))
1739 `(let ((,old-table (syntax-table))
1740 (,old-buffer (current-buffer)))
1741 (unwind-protect
1742 (progn
1743 (set-syntax-table ,table)
1744 ,@body)
1745 (save-current-buffer
1746 (set-buffer ,old-buffer)
1747 (set-syntax-table ,old-table))))))
1748 \f
1749 ;;; Matching and substitution
1750
1751 (defvar save-match-data-internal)
1752
1753 ;; We use save-match-data-internal as the local variable because
1754 ;; that works ok in practice (people should not use that variable elsewhere).
1755 ;; We used to use an uninterned symbol; the compiler handles that properly
1756 ;; now, but it generates slower code.
1757 (defmacro save-match-data (&rest body)
1758 "Execute the BODY forms, restoring the global value of the match data.
1759 The value returned is the value of the last form in BODY."
1760 ;; It is better not to use backquote here,
1761 ;; because that makes a bootstrapping problem
1762 ;; if you need to recompile all the Lisp files using interpreted code.
1763 (list 'let
1764 '((save-match-data-internal (match-data)))
1765 (list 'unwind-protect
1766 (cons 'progn body)
1767 '(set-match-data save-match-data-internal))))
1768
1769 (defun match-string (num &optional string)
1770 "Return string of text matched by last search.
1771 NUM specifies which parenthesized expression in the last regexp.
1772 Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
1773 Zero means the entire text matched by the whole regexp or whole string.
1774 STRING should be given if the last search was by `string-match' on STRING."
1775 (if (match-beginning num)
1776 (if string
1777 (substring string (match-beginning num) (match-end num))
1778 (buffer-substring (match-beginning num) (match-end num)))))
1779
1780 (defun match-string-no-properties (num &optional string)
1781 "Return string of text matched by last search, without text properties.
1782 NUM specifies which parenthesized expression in the last regexp.
1783 Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
1784 Zero means the entire text matched by the whole regexp or whole string.
1785 STRING should be given if the last search was by `string-match' on STRING."
1786 (if (match-beginning num)
1787 (if string
1788 (let ((result
1789 (substring string (match-beginning num) (match-end num))))
1790 (set-text-properties 0 (length result) nil result)
1791 result)
1792 (buffer-substring-no-properties (match-beginning num)
1793 (match-end num)))))
1794
1795 (defun split-string (string &optional separators)
1796 "Splits STRING into substrings where there are matches for SEPARATORS.
1797 Each match for SEPARATORS is a splitting point.
1798 The substrings between the splitting points are made into a list
1799 which is returned.
1800 If SEPARATORS is absent, it defaults to \"[ \\f\\t\\n\\r\\v]+\".
1801
1802 If there is match for SEPARATORS at the beginning of STRING, we do not
1803 include a null substring for that. Likewise, if there is a match
1804 at the end of STRING, we don't include a null substring for that.
1805
1806 Modifies the match data; use `save-match-data' if necessary."
1807 (let ((rexp (or separators "[ \f\t\n\r\v]+"))
1808 (start 0)
1809 notfirst
1810 (list nil))
1811 (while (and (string-match rexp string
1812 (if (and notfirst
1813 (= start (match-beginning 0))
1814 (< start (length string)))
1815 (1+ start) start))
1816 (< (match-beginning 0) (length string)))
1817 (setq notfirst t)
1818 (or (eq (match-beginning 0) 0)
1819 (and (eq (match-beginning 0) (match-end 0))
1820 (eq (match-beginning 0) start))
1821 (setq list
1822 (cons (substring string start (match-beginning 0))
1823 list)))
1824 (setq start (match-end 0)))
1825 (or (eq start (length string))
1826 (setq list
1827 (cons (substring string start)
1828 list)))
1829 (nreverse list)))
1830
1831 (defun subst-char-in-string (fromchar tochar string &optional inplace)
1832 "Replace FROMCHAR with TOCHAR in STRING each time it occurs.
1833 Unless optional argument INPLACE is non-nil, return a new string."
1834 (let ((i (length string))
1835 (newstr (if inplace string (copy-sequence string))))
1836 (while (> i 0)
1837 (setq i (1- i))
1838 (if (eq (aref newstr i) fromchar)
1839 (aset newstr i tochar)))
1840 newstr))
1841
1842 (defun replace-regexp-in-string (regexp rep string &optional
1843 fixedcase literal subexp start)
1844 "Replace all matches for REGEXP with REP in STRING.
1845
1846 Return a new string containing the replacements.
1847
1848 Optional arguments FIXEDCASE, LITERAL and SUBEXP are like the
1849 arguments with the same names of function `replace-match'. If START
1850 is non-nil, start replacements at that index in STRING.
1851
1852 REP is either a string used as the NEWTEXT arg of `replace-match' or a
1853 function. If it is a function it is applied to each match to generate
1854 the replacement passed to `replace-match'; the match-data at this
1855 point are such that match 0 is the function's argument.
1856
1857 To replace only the first match (if any), make REGEXP match up to \\'
1858 and replace a sub-expression, e.g.
1859 (replace-regexp-in-string \"\\\\(foo\\\\).*\\\\'\" \"bar\" \" foo foo\" nil nil 1)
1860 => \" bar foo\"
1861 "
1862
1863 ;; To avoid excessive consing from multiple matches in long strings,
1864 ;; don't just call `replace-match' continually. Walk down the
1865 ;; string looking for matches of REGEXP and building up a (reversed)
1866 ;; list MATCHES. This comprises segments of STRING which weren't
1867 ;; matched interspersed with replacements for segments that were.
1868 ;; [For a `large' number of replacements it's more efficient to
1869 ;; operate in a temporary buffer; we can't tell from the function's
1870 ;; args whether to choose the buffer-based implementation, though it
1871 ;; might be reasonable to do so for long enough STRING.]
1872 (let ((l (length string))
1873 (start (or start 0))
1874 matches str mb me)
1875 (save-match-data
1876 (while (and (< start l) (string-match regexp string start))
1877 (setq mb (match-beginning 0)
1878 me (match-end 0))
1879 ;; If we matched the empty string, make sure we advance by one char
1880 (when (= me mb) (setq me (min l (1+ mb))))
1881 ;; Generate a replacement for the matched substring.
1882 ;; Operate only on the substring to minimize string consing.
1883 ;; Set up match data for the substring for replacement;
1884 ;; presumably this is likely to be faster than munging the
1885 ;; match data directly in Lisp.
1886 (string-match regexp (setq str (substring string mb me)))
1887 (setq matches
1888 (cons (replace-match (if (stringp rep)
1889 rep
1890 (funcall rep (match-string 0 str)))
1891 fixedcase literal str subexp)
1892 (cons (substring string start mb) ; unmatched prefix
1893 matches)))
1894 (setq start me))
1895 ;; Reconstruct a string from the pieces.
1896 (setq matches (cons (substring string start l) matches)) ; leftover
1897 (apply #'concat (nreverse matches)))))
1898 \f
1899 (defun shell-quote-argument (argument)
1900 "Quote an argument for passing as argument to an inferior shell."
1901 (if (eq system-type 'ms-dos)
1902 ;; Quote using double quotes, but escape any existing quotes in
1903 ;; the argument with backslashes.
1904 (let ((result "")
1905 (start 0)
1906 end)
1907 (if (or (null (string-match "[^\"]" argument))
1908 (< (match-end 0) (length argument)))
1909 (while (string-match "[\"]" argument start)
1910 (setq end (match-beginning 0)
1911 result (concat result (substring argument start end)
1912 "\\" (substring argument end (1+ end)))
1913 start (1+ end))))
1914 (concat "\"" result (substring argument start) "\""))
1915 (if (eq system-type 'windows-nt)
1916 (concat "\"" argument "\"")
1917 (if (equal argument "")
1918 "''"
1919 ;; Quote everything except POSIX filename characters.
1920 ;; This should be safe enough even for really weird shells.
1921 (let ((result "") (start 0) end)
1922 (while (string-match "[^-0-9a-zA-Z_./]" argument start)
1923 (setq end (match-beginning 0)
1924 result (concat result (substring argument start end)
1925 "\\" (substring argument end (1+ end)))
1926 start (1+ end)))
1927 (concat result (substring argument start)))))))
1928
1929 (defun make-syntax-table (&optional oldtable)
1930 "Return a new syntax table.
1931 Create a syntax table which inherits from OLDTABLE (if non-nil) or
1932 from `standard-syntax-table' otherwise."
1933 (let ((table (make-char-table 'syntax-table nil)))
1934 (set-char-table-parent table (or oldtable (standard-syntax-table)))
1935 table))
1936
1937 (defun syntax-after (pos)
1938 "Return the syntax of the char after POS."
1939 (unless (or (< pos (point-min)) (>= pos (point-max)))
1940 (let ((st (if parse-sexp-lookup-properties
1941 (get-char-property pos 'syntax-table))))
1942 (if (consp st) st
1943 (aref (or st (syntax-table)) (char-after pos))))))
1944
1945 (defun add-to-invisibility-spec (arg)
1946 "Add elements to `buffer-invisibility-spec'.
1947 See documentation for `buffer-invisibility-spec' for the kind of elements
1948 that can be added."
1949 (if (eq buffer-invisibility-spec t)
1950 (setq buffer-invisibility-spec (list t)))
1951 (setq buffer-invisibility-spec
1952 (cons arg buffer-invisibility-spec)))
1953
1954 (defun remove-from-invisibility-spec (arg)
1955 "Remove elements from `buffer-invisibility-spec'."
1956 (if (consp buffer-invisibility-spec)
1957 (setq buffer-invisibility-spec (delete arg buffer-invisibility-spec))))
1958 \f
1959 (defun global-set-key (key command)
1960 "Give KEY a global binding as COMMAND.
1961 COMMAND is the command definition to use; usually it is
1962 a symbol naming an interactively-callable function.
1963 KEY is a key sequence; noninteractively, it is a string or vector
1964 of characters or event types, and non-ASCII characters with codes
1965 above 127 (such as ISO Latin-1) can be included if you use a vector.
1966
1967 Note that if KEY has a local binding in the current buffer,
1968 that local binding will continue to shadow any global binding
1969 that you make with this function."
1970 (interactive "KSet key globally: \nCSet key %s to command: ")
1971 (or (vectorp key) (stringp key)
1972 (signal 'wrong-type-argument (list 'arrayp key)))
1973 (define-key (current-global-map) key command))
1974
1975 (defun local-set-key (key command)
1976 "Give KEY a local binding as COMMAND.
1977 COMMAND is the command definition to use; usually it is
1978 a symbol naming an interactively-callable function.
1979 KEY is a key sequence; noninteractively, it is a string or vector
1980 of characters or event types, and non-ASCII characters with codes
1981 above 127 (such as ISO Latin-1) can be included if you use a vector.
1982
1983 The binding goes in the current buffer's local map,
1984 which in most cases is shared with all other buffers in the same major mode."
1985 (interactive "KSet key locally: \nCSet key %s locally to command: ")
1986 (let ((map (current-local-map)))
1987 (or map
1988 (use-local-map (setq map (make-sparse-keymap))))
1989 (or (vectorp key) (stringp key)
1990 (signal 'wrong-type-argument (list 'arrayp key)))
1991 (define-key map key command)))
1992
1993 (defun global-unset-key (key)
1994 "Remove global binding of KEY.
1995 KEY is a string representing a sequence of keystrokes."
1996 (interactive "kUnset key globally: ")
1997 (global-set-key key nil))
1998
1999 (defun local-unset-key (key)
2000 "Remove local binding of KEY.
2001 KEY is a string representing a sequence of keystrokes."
2002 (interactive "kUnset key locally: ")
2003 (if (current-local-map)
2004 (local-set-key key nil))
2005 nil)
2006 \f
2007 ;; We put this here instead of in frame.el so that it's defined even on
2008 ;; systems where frame.el isn't loaded.
2009 (defun frame-configuration-p (object)
2010 "Return non-nil if OBJECT seems to be a frame configuration.
2011 Any list whose car is `frame-configuration' is assumed to be a frame
2012 configuration."
2013 (and (consp object)
2014 (eq (car object) 'frame-configuration)))
2015
2016 (defun functionp (object)
2017 "Non-nil iff OBJECT is a type of object that can be called as a function."
2018 (or (and (symbolp object) (fboundp object)
2019 (condition-case nil
2020 (setq object (indirect-function object))
2021 (error nil))
2022 (eq (car-safe object) 'autoload)
2023 (not (car-safe (cdr-safe (cdr-safe (cdr-safe (cdr-safe object)))))))
2024 (subrp object) (byte-code-function-p object)
2025 (eq (car-safe object) 'lambda)))
2026
2027 (defun interactive-form (function)
2028 "Return the interactive form of FUNCTION.
2029 If function is a command (see `commandp'), value is a list of the form
2030 \(interactive SPEC). If function is not a command, return nil."
2031 (setq function (indirect-function function))
2032 (when (commandp function)
2033 (cond ((byte-code-function-p function)
2034 (when (> (length function) 5)
2035 (let ((spec (aref function 5)))
2036 (if spec
2037 (list 'interactive spec)
2038 (list 'interactive)))))
2039 ((subrp function)
2040 (subr-interactive-form function))
2041 ((eq (car-safe function) 'lambda)
2042 (setq function (cddr function))
2043 (when (stringp (car function))
2044 (setq function (cdr function)))
2045 (let ((form (car function)))
2046 (when (eq (car-safe form) 'interactive)
2047 (copy-sequence form)))))))
2048
2049 (defun assq-delete-all (key alist)
2050 "Delete from ALIST all elements whose car is KEY.
2051 Return the modified alist.
2052 Elements of ALIST that are not conses are ignored."
2053 (let ((tail alist))
2054 (while tail
2055 (if (and (consp (car tail)) (eq (car (car tail)) key))
2056 (setq alist (delq (car tail) alist)))
2057 (setq tail (cdr tail)))
2058 alist))
2059
2060 (defun make-temp-file (prefix &optional dir-flag suffix)
2061 "Create a temporary file.
2062 The returned file name (created by appending some random characters at the end
2063 of PREFIX, and expanding against `temporary-file-directory' if necessary),
2064 is guaranteed to point to a newly created empty file.
2065 You can then use `write-region' to write new data into the file.
2066
2067 If DIR-FLAG is non-nil, create a new empty directory instead of a file.
2068
2069 If SUFFIX is non-nil, add that at the end of the file name."
2070 (let ((umask (default-file-modes))
2071 file)
2072 (unwind-protect
2073 (progn
2074 ;; Create temp files with strict access rights. It's easy to
2075 ;; loosen them later, whereas it's impossible to close the
2076 ;; time-window of loose permissions otherwise.
2077 (set-default-file-modes ?\700)
2078 (while (condition-case ()
2079 (progn
2080 (setq file
2081 (make-temp-name
2082 (expand-file-name prefix temporary-file-directory)))
2083 (if suffix
2084 (setq file (concat file suffix)))
2085 (if dir-flag
2086 (make-directory file)
2087 (write-region "" nil file nil 'silent nil 'excl))
2088 nil)
2089 (file-already-exists t))
2090 ;; the file was somehow created by someone else between
2091 ;; `make-temp-name' and `write-region', let's try again.
2092 nil)
2093 file)
2094 ;; Reset the umask.
2095 (set-default-file-modes umask))))
2096
2097 \f
2098 ;; If a minor mode is not defined with define-minor-mode,
2099 ;; add it here explicitly.
2100 ;; isearch-mode is deliberately excluded, since you should
2101 ;; not call it yourself.
2102 (defvar minor-mode-list '(auto-save-mode auto-fill-mode abbrev-mode
2103 overwrite-mode view-mode)
2104 "List of all minor mode functions.")
2105
2106 (defun add-minor-mode (toggle name &optional keymap after toggle-fun)
2107 "Register a new minor mode.
2108
2109 This is an XEmacs-compatibility function. Use `define-minor-mode' instead.
2110
2111 TOGGLE is a symbol which is the name of a buffer-local variable that
2112 is toggled on or off to say whether the minor mode is active or not.
2113
2114 NAME specifies what will appear in the mode line when the minor mode
2115 is active. NAME should be either a string starting with a space, or a
2116 symbol whose value is such a string.
2117
2118 Optional KEYMAP is the keymap for the minor mode that will be added
2119 to `minor-mode-map-alist'.
2120
2121 Optional AFTER specifies that TOGGLE should be added after AFTER
2122 in `minor-mode-alist'.
2123
2124 Optional TOGGLE-FUN is an interactive function to toggle the mode.
2125 It defaults to (and should by convention be) TOGGLE.
2126
2127 If TOGGLE has a non-nil `:included' property, an entry for the mode is
2128 included in the mode-line minor mode menu.
2129 If TOGGLE has a `:menu-tag', that is used for the menu item's label."
2130 (unless (memq toggle minor-mode-list)
2131 (push toggle minor-mode-list))
2132
2133 (unless toggle-fun (setq toggle-fun toggle))
2134 ;; Add the name to the minor-mode-alist.
2135 (when name
2136 (let ((existing (assq toggle minor-mode-alist)))
2137 (if existing
2138 (setcdr existing (list name))
2139 (let ((tail minor-mode-alist) found)
2140 (while (and tail (not found))
2141 (if (eq after (caar tail))
2142 (setq found tail)
2143 (setq tail (cdr tail))))
2144 (if found
2145 (let ((rest (cdr found)))
2146 (setcdr found nil)
2147 (nconc found (list (list toggle name)) rest))
2148 (setq minor-mode-alist (cons (list toggle name)
2149 minor-mode-alist)))))))
2150 ;; Add the toggle to the minor-modes menu if requested.
2151 (when (get toggle :included)
2152 (define-key mode-line-mode-menu
2153 (vector toggle)
2154 (list 'menu-item
2155 (concat
2156 (or (get toggle :menu-tag)
2157 (if (stringp name) name (symbol-name toggle)))
2158 (let ((mode-name (if (symbolp name) (symbol-value name))))
2159 (if (and (stringp mode-name) (string-match "[^ ]+" mode-name))
2160 (concat " (" (match-string 0 mode-name) ")"))))
2161 toggle-fun
2162 :button (cons :toggle toggle))))
2163
2164 ;; Add the map to the minor-mode-map-alist.
2165 (when keymap
2166 (let ((existing (assq toggle minor-mode-map-alist)))
2167 (if existing
2168 (setcdr existing keymap)
2169 (let ((tail minor-mode-map-alist) found)
2170 (while (and tail (not found))
2171 (if (eq after (caar tail))
2172 (setq found tail)
2173 (setq tail (cdr tail))))
2174 (if found
2175 (let ((rest (cdr found)))
2176 (setcdr found nil)
2177 (nconc found (list (cons toggle keymap)) rest))
2178 (setq minor-mode-map-alist (cons (cons toggle keymap)
2179 minor-mode-map-alist))))))))
2180 \f
2181 ;; Clones ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2182
2183 (defun text-clone-maintain (ol1 after beg end &optional len)
2184 "Propagate the changes made under the overlay OL1 to the other clones.
2185 This is used on the `modification-hooks' property of text clones."
2186 (when (and after (not undo-in-progress) (overlay-start ol1))
2187 (let ((margin (if (overlay-get ol1 'text-clone-spreadp) 1 0)))
2188 (setq beg (max beg (+ (overlay-start ol1) margin)))
2189 (setq end (min end (- (overlay-end ol1) margin)))
2190 (when (<= beg end)
2191 (save-excursion
2192 (when (overlay-get ol1 'text-clone-syntax)
2193 ;; Check content of the clone's text.
2194 (let ((cbeg (+ (overlay-start ol1) margin))
2195 (cend (- (overlay-end ol1) margin)))
2196 (goto-char cbeg)
2197 (save-match-data
2198 (if (not (re-search-forward
2199 (overlay-get ol1 'text-clone-syntax) cend t))
2200 ;; Mark the overlay for deletion.
2201 (overlay-put ol1 'text-clones nil)
2202 (when (< (match-end 0) cend)
2203 ;; Shrink the clone at its end.
2204 (setq end (min end (match-end 0)))
2205 (move-overlay ol1 (overlay-start ol1)
2206 (+ (match-end 0) margin)))
2207 (when (> (match-beginning 0) cbeg)
2208 ;; Shrink the clone at its beginning.
2209 (setq beg (max (match-beginning 0) beg))
2210 (move-overlay ol1 (- (match-beginning 0) margin)
2211 (overlay-end ol1)))))))
2212 ;; Now go ahead and update the clones.
2213 (let ((head (- beg (overlay-start ol1)))
2214 (tail (- (overlay-end ol1) end))
2215 (str (buffer-substring beg end))
2216 (nothing-left t)
2217 (inhibit-modification-hooks t))
2218 (dolist (ol2 (overlay-get ol1 'text-clones))
2219 (let ((oe (overlay-end ol2)))
2220 (unless (or (eq ol1 ol2) (null oe))
2221 (setq nothing-left nil)
2222 (let ((mod-beg (+ (overlay-start ol2) head)))
2223 ;;(overlay-put ol2 'modification-hooks nil)
2224 (goto-char (- (overlay-end ol2) tail))
2225 (unless (> mod-beg (point))
2226 (save-excursion (insert str))
2227 (delete-region mod-beg (point)))
2228 ;;(overlay-put ol2 'modification-hooks '(text-clone-maintain))
2229 ))))
2230 (if nothing-left (delete-overlay ol1))))))))
2231
2232 (defun text-clone-create (start end &optional spreadp syntax)
2233 "Create a text clone of START...END at point.
2234 Text clones are chunks of text that are automatically kept identical:
2235 changes done to one of the clones will be immediately propagated to the other.
2236
2237 The buffer's content at point is assumed to be already identical to
2238 the one between START and END.
2239 If SYNTAX is provided it's a regexp that describes the possible text of
2240 the clones; the clone will be shrunk or killed if necessary to ensure that
2241 its text matches the regexp.
2242 If SPREADP is non-nil it indicates that text inserted before/after the
2243 clone should be incorporated in the clone."
2244 ;; To deal with SPREADP we can either use an overlay with `nil t' along
2245 ;; with insert-(behind|in-front-of)-hooks or use a slightly larger overlay
2246 ;; (with a one-char margin at each end) with `t nil'.
2247 ;; We opted for a larger overlay because it behaves better in the case
2248 ;; where the clone is reduced to the empty string (we want the overlay to
2249 ;; stay when the clone's content is the empty string and we want to use
2250 ;; `evaporate' to make sure those overlays get deleted when needed).
2251 ;;
2252 (let* ((pt-end (+ (point) (- end start)))
2253 (start-margin (if (or (not spreadp) (bobp) (<= start (point-min)))
2254 0 1))
2255 (end-margin (if (or (not spreadp)
2256 (>= pt-end (point-max))
2257 (>= start (point-max)))
2258 0 1))
2259 (ol1 (make-overlay (- start start-margin) (+ end end-margin) nil t))
2260 (ol2 (make-overlay (- (point) start-margin) (+ pt-end end-margin) nil t))
2261 (dups (list ol1 ol2)))
2262 (overlay-put ol1 'modification-hooks '(text-clone-maintain))
2263 (when spreadp (overlay-put ol1 'text-clone-spreadp t))
2264 (when syntax (overlay-put ol1 'text-clone-syntax syntax))
2265 ;;(overlay-put ol1 'face 'underline)
2266 (overlay-put ol1 'evaporate t)
2267 (overlay-put ol1 'text-clones dups)
2268 ;;
2269 (overlay-put ol2 'modification-hooks '(text-clone-maintain))
2270 (when spreadp (overlay-put ol2 'text-clone-spreadp t))
2271 (when syntax (overlay-put ol2 'text-clone-syntax syntax))
2272 ;;(overlay-put ol2 'face 'underline)
2273 (overlay-put ol2 'evaporate t)
2274 (overlay-put ol2 'text-clones dups)))
2275
2276 (defun play-sound (sound)
2277 "SOUND is a list of the form `(sound KEYWORD VALUE...)'.
2278 The following keywords are recognized:
2279
2280 :file FILE - read sound data from FILE. If FILE isn't an
2281 absolute file name, it is searched in `data-directory'.
2282
2283 :data DATA - read sound data from string DATA.
2284
2285 Exactly one of :file or :data must be present.
2286
2287 :volume VOL - set volume to VOL. VOL must an integer in the
2288 range 0..100 or a float in the range 0..1.0. If not specified,
2289 don't change the volume setting of the sound device.
2290
2291 :device DEVICE - play sound on DEVICE. If not specified,
2292 a system-dependent default device name is used."
2293 (unless (fboundp 'play-sound-internal)
2294 (error "This Emacs binary lacks sound support"))
2295 (play-sound-internal sound))
2296
2297 (defun define-mail-user-agent (symbol composefunc sendfunc
2298 &optional abortfunc hookvar)
2299 "Define a symbol to identify a mail-sending package for `mail-user-agent'.
2300
2301 SYMBOL can be any Lisp symbol. Its function definition and/or
2302 value as a variable do not matter for this usage; we use only certain
2303 properties on its property list, to encode the rest of the arguments.
2304
2305 COMPOSEFUNC is program callable function that composes an outgoing
2306 mail message buffer. This function should set up the basics of the
2307 buffer without requiring user interaction. It should populate the
2308 standard mail headers, leaving the `to:' and `subject:' headers blank
2309 by default.
2310
2311 COMPOSEFUNC should accept several optional arguments--the same
2312 arguments that `compose-mail' takes. See that function's documentation.
2313
2314 SENDFUNC is the command a user would run to send the message.
2315
2316 Optional ABORTFUNC is the command a user would run to abort the
2317 message. For mail packages that don't have a separate abort function,
2318 this can be `kill-buffer' (the equivalent of omitting this argument).
2319
2320 Optional HOOKVAR is a hook variable that gets run before the message
2321 is actually sent. Callers that use the `mail-user-agent' may
2322 install a hook function temporarily on this hook variable.
2323 If HOOKVAR is nil, `mail-send-hook' is used.
2324
2325 The properties used on SYMBOL are `composefunc', `sendfunc',
2326 `abortfunc', and `hookvar'."
2327 (put symbol 'composefunc composefunc)
2328 (put symbol 'sendfunc sendfunc)
2329 (put symbol 'abortfunc (or abortfunc 'kill-buffer))
2330 (put symbol 'hookvar (or hookvar 'mail-send-hook)))
2331
2332 ;;; subr.el ends here