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