(sh-while-getopts, sh-if, sh-case): Revert part of previous change.
[emacs.git] / lisp / subr.el
blob2ca79e54b76f5704730a9d0178204ad82fa6693a
1 ;;; subr.el --- basic lisp subroutines for Emacs
3 ;; Copyright (C) 1985, 86, 92, 94, 95, 99, 2000, 2001, 2002, 2003
4 ;; Free Software Foundation, Inc.
6 ;; Maintainer: FSF
7 ;; Keywords: internal
9 ;; This file is part of GNU Emacs.
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.
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.
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.
26 ;;; Commentary:
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'.")
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)))
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))))
56 (message "Unknown declaration %s" d))))))
58 (setq macro-declaration-function 'macro-declaration-function)
61 ;;;; Lisp language features.
63 (defalias 'not 'null)
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.
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)))
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)))
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)))))
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)))
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))))
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.
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)))))))
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).
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)))))
145 (defsubst caar (x)
146 "Return the car of the car of X."
147 (car (car x)))
149 (defsubst cadr (x)
150 "Return the car of the cdr of X."
151 (car (cdr x)))
153 (defsubst cdar (x)
154 "Return the cdr of the car of X."
155 (cdr (car x)))
157 (defsubst cddr (x)
158 "Return the cdr of the cdr of X."
159 (cdr (cdr x)))
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)))
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)))
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))))
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))))
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))))
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))
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)))
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.
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))
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))
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))
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)
298 ;;;; Keymap support.
300 (defun undefined ()
301 (interactive)
302 (ding))
304 ;Prevent the \{...} documentation construct
305 ;from mentioning keys that run this command.
306 (put 'undefined 'suppress-keymap t)
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))))))
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)))
331 (defvar key-substitution-in-progress nil
332 "Used internally by substitute-key-definition.")
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
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)))))
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).
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.
479 Bindings are always added before any inherited map.
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)))))
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))
524 (put 'keyboard-translate-table 'char-table-extra-slots 0)
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))
536 ;;;; The global keymap tree.
538 ;;; global-map, esc-map, and ctl-x-map have their values set up in
539 ;;; keymap.c; we just give them docstrings here.
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.")
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.")
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.")
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)
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)
565 ;;;; Event manipulation functions.
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-\\^@")))
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)))
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))))
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))))
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)))))
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)))
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)))
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)))
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))
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))
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)))
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))
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))))))
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))
716 ;;;; Obsolescent names for functions.
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)
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")
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")
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)
761 (defalias 'focus-frame 'ignore)
762 (defalias 'unfocus-frame 'ignore)
765 ;;;; Obsolescence declarations for variables.
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")
779 ;;;; Alternate names for functions - these are not being phased out.
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)
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)
802 ;;;; Hook manipulation functions.
804 (defun make-local-hook (hook)
805 "Make the hook HOOK local to the current buffer.
806 The return value is HOOK.
808 You never need to call this function now that `add-hook' does it for you
809 if its LOCAL argument is non-nil.
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.
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.
823 This function does nothing if HOOK is already local in the current
824 buffer.
826 Do not use `make-local-variable' to make a hook variable buffer-local."
827 (if (local-variable-p hook)
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")
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.
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.
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))))
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'.
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)))))
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.
912 The return value is the new value of LIST-VAR.
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))))))
927 ;;; Load history
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.")
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)))
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)))
967 ;;;; Specifying things to do after certain files are loaded.
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)
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)))
1001 ;;; make-network-process wrappers
1003 (if (featurep 'make-network-process)
1004 (progn
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))
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.
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).
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)))
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.
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.
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)))
1069 )) ;; (featurep 'make-network-process)
1072 ;; compatibility
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))
1084 ;; process plist management
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))
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)))
1098 ;;;; Input and display facilities.
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.")
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)
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.
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))
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 ""))))
1216 ;;; Atomic change groups.
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.
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))))))
1245 (defun prepare-change-group (&optional buffer)
1246 "Return a handle for the current buffer's state, for a change group.
1247 If you specify BUFFER, make a handle for BUFFER's state instead.
1249 Pass the handle to `activate-change-group' afterward to initiate
1250 the actual changes of the change group.
1252 To finish the change group, call either `accept-change-group' or
1253 `cancel-change-group' passing the same handle as argument. Call
1254 `accept-change-group' to accept the changes in the group as final;
1255 call `cancel-change-group' to undo them all. You should use
1256 `unwind-protect' to make sure the group is always finished. The call
1257 to `activate-change-group' should be inside the `unwind-protect'.
1258 Once you finish the group, don't use the handle again--don't try to
1259 finish the same group twice. For a simple example of correct use, see
1260 the source code of `atomic-change-group'.
1262 The handle records only the specified buffer. To make a multibuffer
1263 change group, call this function once for each buffer you want to
1264 cover, then use `nconc' to combine the returned values, like this:
1266 (nconc (prepare-change-group buffer-1)
1267 (prepare-change-group buffer-2))
1269 You can then activate that multibuffer change group with a single
1270 call to `activate-change-group' and finish it with a single call
1271 to `accept-change-group' or `cancel-change-group'."
1273 (if buffer
1274 (list (cons buffer (with-current-buffer buffer buffer-undo-list)))
1275 (list (cons (current-buffer) buffer-undo-list))))
1277 (defun activate-change-group (handle)
1278 "Activate a change group made with `prepare-change-group' (which see)."
1279 (dolist (elt handle)
1280 (with-current-buffer (car elt)
1281 (if (eq buffer-undo-list t)
1282 (setq buffer-undo-list nil)))))
1284 (defun accept-change-group (handle)
1285 "Finish a change group made with `prepare-change-group' (which see).
1286 This finishes the change group by accepting its changes as final."
1287 (dolist (elt handle)
1288 (with-current-buffer (car elt)
1289 (if (eq elt t)
1290 (setq buffer-undo-list t)))))
1292 (defun cancel-change-group (handle)
1293 "Finish a change group made with `prepare-change-group' (which see).
1294 This finishes the change group by reverting all of its changes."
1295 (dolist (elt handle)
1296 (with-current-buffer (car elt)
1297 (setq elt (cdr elt))
1298 (let ((old-car
1299 (if (consp elt) (car elt)))
1300 (old-cdr
1301 (if (consp elt) (cdr elt))))
1302 ;; Temporarily truncate the undo log at ELT.
1303 (when (consp elt)
1304 (setcar elt nil) (setcdr elt nil))
1305 (unless (eq last-command 'undo) (undo-start))
1306 ;; Make sure there's no confusion.
1307 (when (and (consp elt) (not (eq elt (last pending-undo-list))))
1308 (error "Undoing to some unrelated state"))
1309 ;; Undo it all.
1310 (while pending-undo-list (undo-more 1))
1311 ;; Reset the modified cons cell ELT to its original content.
1312 (when (consp elt)
1313 (setcar elt old-car)
1314 (setcdr elt old-cdr))
1315 ;; Revert the undo info to what it was when we grabbed the state.
1316 (setq buffer-undo-list elt)))))
1318 ;; For compatibility.
1319 (defalias 'redraw-modeline 'force-mode-line-update)
1321 (defun force-mode-line-update (&optional all)
1322 "Force the mode line of the current buffer to be redisplayed.
1323 With optional non-nil ALL, force redisplay of all mode lines."
1324 (if all (save-excursion (set-buffer (other-buffer))))
1325 (set-buffer-modified-p (buffer-modified-p)))
1327 (defun momentary-string-display (string pos &optional exit-char message)
1328 "Momentarily display STRING in the buffer at POS.
1329 Display remains until next character is typed.
1330 If the char is EXIT-CHAR (optional third arg, default is SPC) it is swallowed;
1331 otherwise it is then available as input (as a command if nothing else).
1332 Display MESSAGE (optional fourth arg) in the echo area.
1333 If MESSAGE is nil, instructions to type EXIT-CHAR are displayed there."
1334 (or exit-char (setq exit-char ?\ ))
1335 (let ((inhibit-read-only t)
1336 ;; Don't modify the undo list at all.
1337 (buffer-undo-list t)
1338 (modified (buffer-modified-p))
1339 (name buffer-file-name)
1340 insert-end)
1341 (unwind-protect
1342 (progn
1343 (save-excursion
1344 (goto-char pos)
1345 ;; defeat file locking... don't try this at home, kids!
1346 (setq buffer-file-name nil)
1347 (insert-before-markers string)
1348 (setq insert-end (point))
1349 ;; If the message end is off screen, recenter now.
1350 (if (< (window-end nil t) insert-end)
1351 (recenter (/ (window-height) 2)))
1352 ;; If that pushed message start off the screen,
1353 ;; scroll to start it at the top of the screen.
1354 (move-to-window-line 0)
1355 (if (> (point) pos)
1356 (progn
1357 (goto-char pos)
1358 (recenter 0))))
1359 (message (or message "Type %s to continue editing.")
1360 (single-key-description exit-char))
1361 (let ((char (read-event)))
1362 (or (eq char exit-char)
1363 (setq unread-command-events (list char)))))
1364 (if insert-end
1365 (save-excursion
1366 (delete-region pos insert-end)))
1367 (setq buffer-file-name name)
1368 (set-buffer-modified-p modified))))
1371 ;;;; Overlay operations
1373 (defun copy-overlay (o)
1374 "Return a copy of overlay O."
1375 (let ((o1 (make-overlay (overlay-start o) (overlay-end o)
1376 ;; FIXME: there's no easy way to find the
1377 ;; insertion-type of the two markers.
1378 (overlay-buffer o)))
1379 (props (overlay-properties o)))
1380 (while props
1381 (overlay-put o1 (pop props) (pop props)))
1382 o1))
1384 (defun remove-overlays (beg end name val)
1385 "Clear BEG and END of overlays whose property NAME has value VAL.
1386 Overlays might be moved and or split."
1387 (if (< end beg)
1388 (setq beg (prog1 end (setq end beg))))
1389 (save-excursion
1390 (dolist (o (overlays-in beg end))
1391 (when (eq (overlay-get o name) val)
1392 ;; Either push this overlay outside beg...end
1393 ;; or split it to exclude beg...end
1394 ;; or delete it entirely (if it is contained in beg...end).
1395 (if (< (overlay-start o) beg)
1396 (if (> (overlay-end o) end)
1397 (progn
1398 (move-overlay (copy-overlay o)
1399 (overlay-start o) beg)
1400 (move-overlay o end (overlay-end o)))
1401 (move-overlay o (overlay-start o) beg))
1402 (if (> (overlay-end o) end)
1403 (move-overlay o end (overlay-end o))
1404 (delete-overlay o)))))))
1406 ;;;; Miscellanea.
1408 ;; A number of major modes set this locally.
1409 ;; Give it a global value to avoid compiler warnings.
1410 (defvar font-lock-defaults nil)
1412 (defvar suspend-hook nil
1413 "Normal hook run by `suspend-emacs', before suspending.")
1415 (defvar suspend-resume-hook nil
1416 "Normal hook run by `suspend-emacs', after Emacs is continued.")
1418 (defvar temp-buffer-show-hook nil
1419 "Normal hook run by `with-output-to-temp-buffer' after displaying the buffer.
1420 When the hook runs, the temporary buffer is current, and the window it
1421 was displayed in is selected. This hook is normally set up with a
1422 function to make the buffer read only, and find function names and
1423 variable names in it, provided the major mode is still Help mode.")
1425 (defvar temp-buffer-setup-hook nil
1426 "Normal hook run by `with-output-to-temp-buffer' at the start.
1427 When the hook runs, the temporary buffer is current.
1428 This hook is normally set up with a function to put the buffer in Help
1429 mode.")
1431 ;; Avoid compiler warnings about this variable,
1432 ;; which has a special meaning on certain system types.
1433 (defvar buffer-file-type nil
1434 "Non-nil if the visited file is a binary file.
1435 This variable is meaningful on MS-DOG and Windows NT.
1436 On those systems, it is automatically local in every buffer.
1437 On other systems, this variable is normally always nil.")
1439 ;; This should probably be written in C (i.e., without using `walk-windows').
1440 (defun get-buffer-window-list (buffer &optional minibuf frame)
1441 "Return windows currently displaying BUFFER, or nil if none.
1442 See `walk-windows' for the meaning of MINIBUF and FRAME."
1443 (let ((buffer (if (bufferp buffer) buffer (get-buffer buffer))) windows)
1444 (walk-windows (function (lambda (window)
1445 (if (eq (window-buffer window) buffer)
1446 (setq windows (cons window windows)))))
1447 minibuf frame)
1448 windows))
1450 (defun ignore (&rest ignore)
1451 "Do nothing and return nil.
1452 This function accepts any number of arguments, but ignores them."
1453 (interactive)
1454 nil)
1456 (defun error (&rest args)
1457 "Signal an error, making error message by passing all args to `format'.
1458 In Emacs, the convention is that error messages start with a capital
1459 letter but *do not* end with a period. Please follow this convention
1460 for the sake of consistency."
1461 (while t
1462 (signal 'error (list (apply 'format args)))))
1464 (defalias 'user-original-login-name 'user-login-name)
1466 (defvar yank-excluded-properties)
1468 (defun remove-yank-excluded-properties (start end)
1469 "Remove `yank-excluded-properties' between START and END positions.
1470 Replaces `category' properties with their defined properties."
1471 (let ((inhibit-read-only t))
1472 ;; Replace any `category' property with the properties it stands for.
1473 (unless (memq yank-excluded-properties '(t nil))
1474 (save-excursion
1475 (goto-char start)
1476 (while (< (point) end)
1477 (let ((cat (get-text-property (point) 'category))
1478 run-end)
1479 (setq run-end
1480 (next-single-property-change (point) 'category nil end))
1481 (when cat
1482 (let (run-end2 original)
1483 (remove-list-of-text-properties (point) run-end '(category))
1484 (while (< (point) run-end)
1485 (setq run-end2 (next-property-change (point) nil run-end))
1486 (setq original (text-properties-at (point)))
1487 (set-text-properties (point) run-end2 (symbol-plist cat))
1488 (add-text-properties (point) run-end2 original)
1489 (goto-char run-end2))))
1490 (goto-char run-end)))))
1491 (if (eq yank-excluded-properties t)
1492 (set-text-properties start end nil)
1493 (remove-list-of-text-properties start end yank-excluded-properties))))
1495 (defvar yank-undo-function)
1497 (defun insert-for-yank (string)
1498 "Insert STRING at point, stripping some text properties.
1499 Strip text properties from the inserted text according to
1500 `yank-excluded-properties'. Otherwise just like (insert STRING).
1502 If STRING has a non-nil `yank-handler' property on the first character,
1503 the normal insert behaviour is modified in various ways. The value of
1504 the yank-handler property must be a list with one to five elements
1505 with the following format: (FUNCTION PARAM NOEXCLUDE UNDO).
1506 When FUNCTION is present and non-nil, it is called instead of `insert'
1507 to insert the string. FUNCTION takes one argument--the object to insert.
1508 If PARAM is present and non-nil, it replaces STRING as the object
1509 passed to FUNCTION (or `insert'); for example, if FUNCTION is
1510 `yank-rectangle', PARAM may be a list of strings to insert as a
1511 rectangle.
1512 If NOEXCLUDE is present and non-nil, the normal removal of the
1513 yank-excluded-properties is not performed; instead FUNCTION is
1514 responsible for removing those properties. This may be necessary
1515 if FUNCTION adjusts point before or after inserting the object.
1516 If UNDO is present and non-nil, it is a function that will be called
1517 by `yank-pop' to undo the insertion of the current object. It is
1518 called with two arguments, the start and end of the current region.
1519 FUNCTION may set `yank-undo-function' to override the UNDO value."
1520 (let* ((handler (and (stringp string)
1521 (get-text-property 0 'yank-handler string)))
1522 (param (or (nth 1 handler) string))
1523 (opoint (point)))
1524 (setq yank-undo-function t)
1525 (if (nth 0 handler) ;; FUNCTION
1526 (funcall (car handler) param)
1527 (insert param))
1528 (unless (nth 2 handler) ;; NOEXCLUDE
1529 (remove-yank-excluded-properties opoint (point)))
1530 (if (eq yank-undo-function t) ;; not set by FUNCTION
1531 (setq yank-undo-function (nth 3 handler))) ;; UNDO
1532 (if (nth 4 handler) ;; COMMAND
1533 (setq this-command (nth 4 handler)))))
1535 (defun insert-buffer-substring-no-properties (buf &optional start end)
1536 "Insert before point a substring of buffer BUFFER, without text properties.
1537 BUFFER may be a buffer or a buffer name.
1538 Arguments START and END are character numbers specifying the substring.
1539 They default to the beginning and the end of BUFFER."
1540 (let ((opoint (point)))
1541 (insert-buffer-substring buf start end)
1542 (let ((inhibit-read-only t))
1543 (set-text-properties opoint (point) nil))))
1545 (defun insert-buffer-substring-as-yank (buf &optional start end)
1546 "Insert before point a part of buffer BUFFER, stripping some text properties.
1547 BUFFER may be a buffer or a buffer name. Arguments START and END are
1548 character numbers specifying the substring. They default to the
1549 beginning and the end of BUFFER. Strip text properties from the
1550 inserted text according to `yank-excluded-properties'."
1551 (let ((opoint (point)))
1552 (insert-buffer-substring buf start end)
1553 (remove-yank-excluded-properties opoint (point))))
1556 ;; Synchronous shell commands.
1558 (defun start-process-shell-command (name buffer &rest args)
1559 "Start a program in a subprocess. Return the process object for it.
1560 Args are NAME BUFFER COMMAND &rest COMMAND-ARGS.
1561 NAME is name for process. It is modified if necessary to make it unique.
1562 BUFFER is the buffer or (buffer-name) to associate with the process.
1563 Process output goes at end of that buffer, unless you specify
1564 an output stream or filter function to handle the output.
1565 BUFFER may be also nil, meaning that this process is not associated
1566 with any buffer
1567 Third arg is command name, the name of a shell command.
1568 Remaining arguments are the arguments for the command.
1569 Wildcards and redirection are handled as usual in the shell."
1570 (cond
1571 ((eq system-type 'vax-vms)
1572 (apply 'start-process name buffer args))
1573 ;; We used to use `exec' to replace the shell with the command,
1574 ;; but that failed to handle (...) and semicolon, etc.
1576 (start-process name buffer shell-file-name shell-command-switch
1577 (mapconcat 'identity args " ")))))
1579 (defun call-process-shell-command (command &optional infile buffer display
1580 &rest args)
1581 "Execute the shell command COMMAND synchronously in separate process.
1582 The remaining arguments are optional.
1583 The program's input comes from file INFILE (nil means `/dev/null').
1584 Insert output in BUFFER before point; t means current buffer;
1585 nil for BUFFER means discard it; 0 means discard and don't wait.
1586 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,
1587 REAL-BUFFER says what to do with standard output, as above,
1588 while STDERR-FILE says what to do with standard error in the child.
1589 STDERR-FILE may be nil (discard standard error output),
1590 t (mix it with ordinary output), or a file name string.
1592 Fourth arg DISPLAY non-nil means redisplay buffer as output is inserted.
1593 Remaining arguments are strings passed as additional arguments for COMMAND.
1594 Wildcards and redirection are handled as usual in the shell.
1596 If BUFFER is 0, `call-process-shell-command' returns immediately with value nil.
1597 Otherwise it waits for COMMAND to terminate and returns a numeric exit
1598 status or a signal description string.
1599 If you quit, the process is killed with SIGINT, or SIGKILL if you quit again."
1600 (cond
1601 ((eq system-type 'vax-vms)
1602 (apply 'call-process command infile buffer display args))
1603 ;; We used to use `exec' to replace the shell with the command,
1604 ;; but that failed to handle (...) and semicolon, etc.
1606 (call-process shell-file-name
1607 infile buffer display
1608 shell-command-switch
1609 (mapconcat 'identity (cons command args) " ")))))
1611 (defmacro with-current-buffer (buffer &rest body)
1612 "Execute the forms in BODY with BUFFER as the current buffer.
1613 The value returned is the value of the last form in BODY.
1614 See also `with-temp-buffer'."
1615 (declare (indent 1) (debug t))
1616 `(save-current-buffer
1617 (set-buffer ,buffer)
1618 ,@body))
1620 (defmacro with-selected-window (window &rest body)
1621 "Execute the forms in BODY with WINDOW as the selected window.
1622 The value returned is the value of the last form in BODY.
1623 See also `with-temp-buffer'."
1624 (declare (indent 1) (debug t))
1625 `(save-selected-window
1626 (select-window ,window 'norecord)
1627 ,@body))
1629 (defmacro with-temp-file (file &rest body)
1630 "Create a new buffer, evaluate BODY there, and write the buffer to FILE.
1631 The value returned is the value of the last form in BODY.
1632 See also `with-temp-buffer'."
1633 (let ((temp-file (make-symbol "temp-file"))
1634 (temp-buffer (make-symbol "temp-buffer")))
1635 `(let ((,temp-file ,file)
1636 (,temp-buffer
1637 (get-buffer-create (generate-new-buffer-name " *temp file*"))))
1638 (unwind-protect
1639 (prog1
1640 (with-current-buffer ,temp-buffer
1641 ,@body)
1642 (with-current-buffer ,temp-buffer
1643 (widen)
1644 (write-region (point-min) (point-max) ,temp-file nil 0)))
1645 (and (buffer-name ,temp-buffer)
1646 (kill-buffer ,temp-buffer))))))
1648 (defmacro with-temp-message (message &rest body)
1649 "Display MESSAGE temporarily if non-nil while BODY is evaluated.
1650 The original message is restored to the echo area after BODY has finished.
1651 The value returned is the value of the last form in BODY.
1652 MESSAGE is written to the message log buffer if `message-log-max' is non-nil.
1653 If MESSAGE is nil, the echo area and message log buffer are unchanged.
1654 Use a MESSAGE of \"\" to temporarily clear the echo area."
1655 (let ((current-message (make-symbol "current-message"))
1656 (temp-message (make-symbol "with-temp-message")))
1657 `(let ((,temp-message ,message)
1658 (,current-message))
1659 (unwind-protect
1660 (progn
1661 (when ,temp-message
1662 (setq ,current-message (current-message))
1663 (message "%s" ,temp-message))
1664 ,@body)
1665 (and ,temp-message
1666 (if ,current-message
1667 (message "%s" ,current-message)
1668 (message nil)))))))
1670 (defmacro with-temp-buffer (&rest body)
1671 "Create a temporary buffer, and evaluate BODY there like `progn'.
1672 See also `with-temp-file' and `with-output-to-string'."
1673 (declare (indent 0) (debug t))
1674 (let ((temp-buffer (make-symbol "temp-buffer")))
1675 `(let ((,temp-buffer
1676 (get-buffer-create (generate-new-buffer-name " *temp*"))))
1677 (unwind-protect
1678 (with-current-buffer ,temp-buffer
1679 ,@body)
1680 (and (buffer-name ,temp-buffer)
1681 (kill-buffer ,temp-buffer))))))
1683 (defmacro with-output-to-string (&rest body)
1684 "Execute BODY, return the text it sent to `standard-output', as a string."
1685 (declare (indent 0) (debug t))
1686 `(let ((standard-output
1687 (get-buffer-create (generate-new-buffer-name " *string-output*"))))
1688 (let ((standard-output standard-output))
1689 ,@body)
1690 (with-current-buffer standard-output
1691 (prog1
1692 (buffer-string)
1693 (kill-buffer nil)))))
1695 (defmacro with-local-quit (&rest body)
1696 "Execute BODY with `inhibit-quit' temporarily bound to nil."
1697 (declare (debug t) (indent 0))
1698 `(condition-case nil
1699 (let ((inhibit-quit nil))
1700 ,@body)
1701 (quit (setq quit-flag t))))
1703 (defmacro combine-after-change-calls (&rest body)
1704 "Execute BODY, but don't call the after-change functions till the end.
1705 If BODY makes changes in the buffer, they are recorded
1706 and the functions on `after-change-functions' are called several times
1707 when BODY is finished.
1708 The return value is the value of the last form in BODY.
1710 If `before-change-functions' is non-nil, then calls to the after-change
1711 functions can't be deferred, so in that case this macro has no effect.
1713 Do not alter `after-change-functions' or `before-change-functions'
1714 in BODY."
1715 (declare (indent 0) (debug t))
1716 `(unwind-protect
1717 (let ((combine-after-change-calls t))
1718 . ,body)
1719 (combine-after-change-execute)))
1722 (defvar delay-mode-hooks nil
1723 "If non-nil, `run-mode-hooks' should delay running the hooks.")
1724 (defvar delayed-mode-hooks nil
1725 "List of delayed mode hooks waiting to be run.")
1726 (make-variable-buffer-local 'delayed-mode-hooks)
1728 (defun run-mode-hooks (&rest hooks)
1729 "Run mode hooks `delayed-mode-hooks' and HOOKS, or delay HOOKS.
1730 Execution is delayed if `delay-mode-hooks' is non-nil.
1731 Major mode functions should use this."
1732 (if delay-mode-hooks
1733 ;; Delaying case.
1734 (dolist (hook hooks)
1735 (push hook delayed-mode-hooks))
1736 ;; Normal case, just run the hook as before plus any delayed hooks.
1737 (setq hooks (nconc (nreverse delayed-mode-hooks) hooks))
1738 (setq delayed-mode-hooks nil)
1739 (apply 'run-hooks hooks)))
1741 (defmacro delay-mode-hooks (&rest body)
1742 "Execute BODY, but delay any `run-mode-hooks'.
1743 Only affects hooks run in the current buffer."
1744 `(progn
1745 (make-local-variable 'delay-mode-hooks)
1746 (let ((delay-mode-hooks t))
1747 ,@body)))
1749 ;; PUBLIC: find if the current mode derives from another.
1751 (defun derived-mode-p (&rest modes)
1752 "Non-nil if the current major mode is derived from one of MODES.
1753 Uses the `derived-mode-parent' property of the symbol to trace backwards."
1754 (let ((parent major-mode))
1755 (while (and (not (memq parent modes))
1756 (setq parent (get parent 'derived-mode-parent))))
1757 parent))
1759 (defmacro with-syntax-table (table &rest body)
1760 "Evaluate BODY with syntax table of current buffer set to TABLE.
1761 The syntax table of the current buffer is saved, BODY is evaluated, and the
1762 saved table is restored, even in case of an abnormal exit.
1763 Value is what BODY returns."
1764 (let ((old-table (make-symbol "table"))
1765 (old-buffer (make-symbol "buffer")))
1766 `(let ((,old-table (syntax-table))
1767 (,old-buffer (current-buffer)))
1768 (unwind-protect
1769 (progn
1770 (set-syntax-table ,table)
1771 ,@body)
1772 (save-current-buffer
1773 (set-buffer ,old-buffer)
1774 (set-syntax-table ,old-table))))))
1776 ;;; Matching and substitution
1778 (defvar save-match-data-internal)
1780 ;; We use save-match-data-internal as the local variable because
1781 ;; that works ok in practice (people should not use that variable elsewhere).
1782 ;; We used to use an uninterned symbol; the compiler handles that properly
1783 ;; now, but it generates slower code.
1784 (defmacro save-match-data (&rest body)
1785 "Execute the BODY forms, restoring the global value of the match data.
1786 The value returned is the value of the last form in BODY."
1787 ;; It is better not to use backquote here,
1788 ;; because that makes a bootstrapping problem
1789 ;; if you need to recompile all the Lisp files using interpreted code.
1790 (declare (indent 0) (debug t))
1791 (list 'let
1792 '((save-match-data-internal (match-data)))
1793 (list 'unwind-protect
1794 (cons 'progn body)
1795 '(set-match-data save-match-data-internal))))
1797 (defun match-string (num &optional string)
1798 "Return string of text matched by last search.
1799 NUM specifies which parenthesized expression in the last regexp.
1800 Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
1801 Zero means the entire text matched by the whole regexp or whole string.
1802 STRING should be given if the last search was by `string-match' on STRING."
1803 (if (match-beginning num)
1804 (if string
1805 (substring string (match-beginning num) (match-end num))
1806 (buffer-substring (match-beginning num) (match-end num)))))
1808 (defun match-string-no-properties (num &optional string)
1809 "Return string of text matched by last search, without text properties.
1810 NUM specifies which parenthesized expression in the last regexp.
1811 Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
1812 Zero means the entire text matched by the whole regexp or whole string.
1813 STRING should be given if the last search was by `string-match' on STRING."
1814 (if (match-beginning num)
1815 (if string
1816 (let ((result
1817 (substring string (match-beginning num) (match-end num))))
1818 (set-text-properties 0 (length result) nil result)
1819 result)
1820 (buffer-substring-no-properties (match-beginning num)
1821 (match-end num)))))
1823 (defun looking-back (regexp)
1824 "Return t if text before point matches regular expression REGEXP.
1825 This function modifies the match data that `match-beginning',
1826 `match-end' and `match-data' access; save and restore the match
1827 data if you want to preserve them."
1828 (save-excursion
1829 (let ((beg (point)))
1830 (if (re-search-backward regexp nil t)
1831 (if (= (match-end 0) beg)
1833 nil)
1834 nil))))
1836 (defconst split-string-default-separators "[ \f\t\n\r\v]+"
1837 "The default value of separators for `split-string'.
1839 A regexp matching strings of whitespace. May be locale-dependent
1840 \(as yet unimplemented). Should not match non-breaking spaces.
1842 Warning: binding this to a different value and using it as default is
1843 likely to have undesired semantics.")
1845 ;; The specification says that if both SEPARATORS and OMIT-NULLS are
1846 ;; defaulted, OMIT-NULLS should be treated as t. Simplifying the logical
1847 ;; expression leads to the equivalent implementation that if SEPARATORS
1848 ;; is defaulted, OMIT-NULLS is treated as t.
1849 (defun split-string (string &optional separators omit-nulls)
1850 "Splits STRING into substrings bounded by matches for SEPARATORS.
1852 The beginning and end of STRING, and each match for SEPARATORS, are
1853 splitting points. The substrings matching SEPARATORS are removed, and
1854 the substrings between the splitting points are collected as a list,
1855 which is returned.
1857 If SEPARATORS is non-nil, it should be a regular expression matching text
1858 which separates, but is not part of, the substrings. If nil it defaults to
1859 `split-string-default-separators', normally \"[ \\f\\t\\n\\r\\v]+\", and
1860 OMIT-NULLS is forced to t.
1862 If OMIT-NULLs is t, zero-length substrings are omitted from the list \(so
1863 that for the default value of SEPARATORS leading and trailing whitespace
1864 are effectively trimmed). If nil, all zero-length substrings are retained,
1865 which correctly parses CSV format, for example.
1867 Note that the effect of `(split-string STRING)' is the same as
1868 `(split-string STRING split-string-default-separators t)'). In the rare
1869 case that you wish to retain zero-length substrings when splitting on
1870 whitespace, use `(split-string STRING split-string-default-separators)'.
1872 Modifies the match data; use `save-match-data' if necessary."
1873 (let ((keep-nulls (not (if separators omit-nulls t)))
1874 (rexp (or separators split-string-default-separators))
1875 (start 0)
1876 notfirst
1877 (list nil))
1878 (while (and (string-match rexp string
1879 (if (and notfirst
1880 (= start (match-beginning 0))
1881 (< start (length string)))
1882 (1+ start) start))
1883 (< start (length string)))
1884 (setq notfirst t)
1885 (if (or keep-nulls (< start (match-beginning 0)))
1886 (setq list
1887 (cons (substring string start (match-beginning 0))
1888 list)))
1889 (setq start (match-end 0)))
1890 (if (or keep-nulls (< start (length string)))
1891 (setq list
1892 (cons (substring string start)
1893 list)))
1894 (nreverse list)))
1896 (defun subst-char-in-string (fromchar tochar string &optional inplace)
1897 "Replace FROMCHAR with TOCHAR in STRING each time it occurs.
1898 Unless optional argument INPLACE is non-nil, return a new string."
1899 (let ((i (length string))
1900 (newstr (if inplace string (copy-sequence string))))
1901 (while (> i 0)
1902 (setq i (1- i))
1903 (if (eq (aref newstr i) fromchar)
1904 (aset newstr i tochar)))
1905 newstr))
1907 (defun replace-regexp-in-string (regexp rep string &optional
1908 fixedcase literal subexp start)
1909 "Replace all matches for REGEXP with REP in STRING.
1911 Return a new string containing the replacements.
1913 Optional arguments FIXEDCASE, LITERAL and SUBEXP are like the
1914 arguments with the same names of function `replace-match'. If START
1915 is non-nil, start replacements at that index in STRING.
1917 REP is either a string used as the NEWTEXT arg of `replace-match' or a
1918 function. If it is a function it is applied to each match to generate
1919 the replacement passed to `replace-match'; the match-data at this
1920 point are such that match 0 is the function's argument.
1922 To replace only the first match (if any), make REGEXP match up to \\'
1923 and replace a sub-expression, e.g.
1924 (replace-regexp-in-string \"\\\\(foo\\\\).*\\\\'\" \"bar\" \" foo foo\" nil nil 1)
1925 => \" bar foo\"
1928 ;; To avoid excessive consing from multiple matches in long strings,
1929 ;; don't just call `replace-match' continually. Walk down the
1930 ;; string looking for matches of REGEXP and building up a (reversed)
1931 ;; list MATCHES. This comprises segments of STRING which weren't
1932 ;; matched interspersed with replacements for segments that were.
1933 ;; [For a `large' number of replacements it's more efficient to
1934 ;; operate in a temporary buffer; we can't tell from the function's
1935 ;; args whether to choose the buffer-based implementation, though it
1936 ;; might be reasonable to do so for long enough STRING.]
1937 (let ((l (length string))
1938 (start (or start 0))
1939 matches str mb me)
1940 (save-match-data
1941 (while (and (< start l) (string-match regexp string start))
1942 (setq mb (match-beginning 0)
1943 me (match-end 0))
1944 ;; If we matched the empty string, make sure we advance by one char
1945 (when (= me mb) (setq me (min l (1+ mb))))
1946 ;; Generate a replacement for the matched substring.
1947 ;; Operate only on the substring to minimize string consing.
1948 ;; Set up match data for the substring for replacement;
1949 ;; presumably this is likely to be faster than munging the
1950 ;; match data directly in Lisp.
1951 (string-match regexp (setq str (substring string mb me)))
1952 (setq matches
1953 (cons (replace-match (if (stringp rep)
1955 (funcall rep (match-string 0 str)))
1956 fixedcase literal str subexp)
1957 (cons (substring string start mb) ; unmatched prefix
1958 matches)))
1959 (setq start me))
1960 ;; Reconstruct a string from the pieces.
1961 (setq matches (cons (substring string start l) matches)) ; leftover
1962 (apply #'concat (nreverse matches)))))
1964 (defun shell-quote-argument (argument)
1965 "Quote an argument for passing as argument to an inferior shell."
1966 (if (eq system-type 'ms-dos)
1967 ;; Quote using double quotes, but escape any existing quotes in
1968 ;; the argument with backslashes.
1969 (let ((result "")
1970 (start 0)
1971 end)
1972 (if (or (null (string-match "[^\"]" argument))
1973 (< (match-end 0) (length argument)))
1974 (while (string-match "[\"]" argument start)
1975 (setq end (match-beginning 0)
1976 result (concat result (substring argument start end)
1977 "\\" (substring argument end (1+ end)))
1978 start (1+ end))))
1979 (concat "\"" result (substring argument start) "\""))
1980 (if (eq system-type 'windows-nt)
1981 (concat "\"" argument "\"")
1982 (if (equal argument "")
1983 "''"
1984 ;; Quote everything except POSIX filename characters.
1985 ;; This should be safe enough even for really weird shells.
1986 (let ((result "") (start 0) end)
1987 (while (string-match "[^-0-9a-zA-Z_./]" argument start)
1988 (setq end (match-beginning 0)
1989 result (concat result (substring argument start end)
1990 "\\" (substring argument end (1+ end)))
1991 start (1+ end)))
1992 (concat result (substring argument start)))))))
1994 (defun make-syntax-table (&optional oldtable)
1995 "Return a new syntax table.
1996 Create a syntax table which inherits from OLDTABLE (if non-nil) or
1997 from `standard-syntax-table' otherwise."
1998 (let ((table (make-char-table 'syntax-table nil)))
1999 (set-char-table-parent table (or oldtable (standard-syntax-table)))
2000 table))
2002 (defun syntax-after (pos)
2003 "Return the syntax of the char after POS."
2004 (unless (or (< pos (point-min)) (>= pos (point-max)))
2005 (let ((st (if parse-sexp-lookup-properties
2006 (get-char-property pos 'syntax-table))))
2007 (if (consp st) st
2008 (aref (or st (syntax-table)) (char-after pos))))))
2010 (defun add-to-invisibility-spec (arg)
2011 "Add elements to `buffer-invisibility-spec'.
2012 See documentation for `buffer-invisibility-spec' for the kind of elements
2013 that can be added."
2014 (if (eq buffer-invisibility-spec t)
2015 (setq buffer-invisibility-spec (list t)))
2016 (setq buffer-invisibility-spec
2017 (cons arg buffer-invisibility-spec)))
2019 (defun remove-from-invisibility-spec (arg)
2020 "Remove elements from `buffer-invisibility-spec'."
2021 (if (consp buffer-invisibility-spec)
2022 (setq buffer-invisibility-spec (delete arg buffer-invisibility-spec))))
2024 (defun global-set-key (key command)
2025 "Give KEY a global binding as COMMAND.
2026 COMMAND is the command definition to use; usually it is
2027 a symbol naming an interactively-callable function.
2028 KEY is a key sequence; noninteractively, it is a string or vector
2029 of characters or event types, and non-ASCII characters with codes
2030 above 127 (such as ISO Latin-1) can be included if you use a vector.
2032 Note that if KEY has a local binding in the current buffer,
2033 that local binding will continue to shadow any global binding
2034 that you make with this function."
2035 (interactive "KSet key globally: \nCSet key %s to command: ")
2036 (or (vectorp key) (stringp key)
2037 (signal 'wrong-type-argument (list 'arrayp key)))
2038 (define-key (current-global-map) key command))
2040 (defun local-set-key (key command)
2041 "Give KEY a local binding as COMMAND.
2042 COMMAND is the command definition to use; usually it is
2043 a symbol naming an interactively-callable function.
2044 KEY is a key sequence; noninteractively, it is a string or vector
2045 of characters or event types, and non-ASCII characters with codes
2046 above 127 (such as ISO Latin-1) can be included if you use a vector.
2048 The binding goes in the current buffer's local map,
2049 which in most cases is shared with all other buffers in the same major mode."
2050 (interactive "KSet key locally: \nCSet key %s locally to command: ")
2051 (let ((map (current-local-map)))
2052 (or map
2053 (use-local-map (setq map (make-sparse-keymap))))
2054 (or (vectorp key) (stringp key)
2055 (signal 'wrong-type-argument (list 'arrayp key)))
2056 (define-key map key command)))
2058 (defun global-unset-key (key)
2059 "Remove global binding of KEY.
2060 KEY is a string representing a sequence of keystrokes."
2061 (interactive "kUnset key globally: ")
2062 (global-set-key key nil))
2064 (defun local-unset-key (key)
2065 "Remove local binding of KEY.
2066 KEY is a string representing a sequence of keystrokes."
2067 (interactive "kUnset key locally: ")
2068 (if (current-local-map)
2069 (local-set-key key nil))
2070 nil)
2072 ;; We put this here instead of in frame.el so that it's defined even on
2073 ;; systems where frame.el isn't loaded.
2074 (defun frame-configuration-p (object)
2075 "Return non-nil if OBJECT seems to be a frame configuration.
2076 Any list whose car is `frame-configuration' is assumed to be a frame
2077 configuration."
2078 (and (consp object)
2079 (eq (car object) 'frame-configuration)))
2081 (defun functionp (object)
2082 "Non-nil iff OBJECT is a type of object that can be called as a function."
2083 (or (and (symbolp object) (fboundp object)
2084 (condition-case nil
2085 (setq object (indirect-function object))
2086 (error nil))
2087 (eq (car-safe object) 'autoload)
2088 (not (car-safe (cdr-safe (cdr-safe (cdr-safe (cdr-safe object)))))))
2089 (subrp object) (byte-code-function-p object)
2090 (eq (car-safe object) 'lambda)))
2092 (defun interactive-form (function)
2093 "Return the interactive form of FUNCTION.
2094 If function is a command (see `commandp'), value is a list of the form
2095 \(interactive SPEC). If function is not a command, return nil."
2096 (setq function (indirect-function function))
2097 (when (commandp function)
2098 (cond ((byte-code-function-p function)
2099 (when (> (length function) 5)
2100 (let ((spec (aref function 5)))
2101 (if spec
2102 (list 'interactive spec)
2103 (list 'interactive)))))
2104 ((subrp function)
2105 (subr-interactive-form function))
2106 ((eq (car-safe function) 'lambda)
2107 (setq function (cddr function))
2108 (when (stringp (car function))
2109 (setq function (cdr function)))
2110 (let ((form (car function)))
2111 (when (eq (car-safe form) 'interactive)
2112 (copy-sequence form)))))))
2114 (defun assq-delete-all (key alist)
2115 "Delete from ALIST all elements whose car is KEY.
2116 Return the modified alist.
2117 Elements of ALIST that are not conses are ignored."
2118 (let ((tail alist))
2119 (while tail
2120 (if (and (consp (car tail)) (eq (car (car tail)) key))
2121 (setq alist (delq (car tail) alist)))
2122 (setq tail (cdr tail)))
2123 alist))
2125 (defun make-temp-file (prefix &optional dir-flag suffix)
2126 "Create a temporary file.
2127 The returned file name (created by appending some random characters at the end
2128 of PREFIX, and expanding against `temporary-file-directory' if necessary),
2129 is guaranteed to point to a newly created empty file.
2130 You can then use `write-region' to write new data into the file.
2132 If DIR-FLAG is non-nil, create a new empty directory instead of a file.
2134 If SUFFIX is non-nil, add that at the end of the file name."
2135 (let ((umask (default-file-modes))
2136 file)
2137 (unwind-protect
2138 (progn
2139 ;; Create temp files with strict access rights. It's easy to
2140 ;; loosen them later, whereas it's impossible to close the
2141 ;; time-window of loose permissions otherwise.
2142 (set-default-file-modes ?\700)
2143 (while (condition-case ()
2144 (progn
2145 (setq file
2146 (make-temp-name
2147 (expand-file-name prefix temporary-file-directory)))
2148 (if suffix
2149 (setq file (concat file suffix)))
2150 (if dir-flag
2151 (make-directory file)
2152 (write-region "" nil file nil 'silent nil 'excl))
2153 nil)
2154 (file-already-exists t))
2155 ;; the file was somehow created by someone else between
2156 ;; `make-temp-name' and `write-region', let's try again.
2157 nil)
2158 file)
2159 ;; Reset the umask.
2160 (set-default-file-modes umask))))
2163 ;; If a minor mode is not defined with define-minor-mode,
2164 ;; add it here explicitly.
2165 ;; isearch-mode is deliberately excluded, since you should
2166 ;; not call it yourself.
2167 (defvar minor-mode-list '(auto-save-mode auto-fill-mode abbrev-mode
2168 overwrite-mode view-mode)
2169 "List of all minor mode functions.")
2171 (defun add-minor-mode (toggle name &optional keymap after toggle-fun)
2172 "Register a new minor mode.
2174 This is an XEmacs-compatibility function. Use `define-minor-mode' instead.
2176 TOGGLE is a symbol which is the name of a buffer-local variable that
2177 is toggled on or off to say whether the minor mode is active or not.
2179 NAME specifies what will appear in the mode line when the minor mode
2180 is active. NAME should be either a string starting with a space, or a
2181 symbol whose value is such a string.
2183 Optional KEYMAP is the keymap for the minor mode that will be added
2184 to `minor-mode-map-alist'.
2186 Optional AFTER specifies that TOGGLE should be added after AFTER
2187 in `minor-mode-alist'.
2189 Optional TOGGLE-FUN is an interactive function to toggle the mode.
2190 It defaults to (and should by convention be) TOGGLE.
2192 If TOGGLE has a non-nil `:included' property, an entry for the mode is
2193 included in the mode-line minor mode menu.
2194 If TOGGLE has a `:menu-tag', that is used for the menu item's label."
2195 (unless (memq toggle minor-mode-list)
2196 (push toggle minor-mode-list))
2198 (unless toggle-fun (setq toggle-fun toggle))
2199 ;; Add the name to the minor-mode-alist.
2200 (when name
2201 (let ((existing (assq toggle minor-mode-alist)))
2202 (if existing
2203 (setcdr existing (list name))
2204 (let ((tail minor-mode-alist) found)
2205 (while (and tail (not found))
2206 (if (eq after (caar tail))
2207 (setq found tail)
2208 (setq tail (cdr tail))))
2209 (if found
2210 (let ((rest (cdr found)))
2211 (setcdr found nil)
2212 (nconc found (list (list toggle name)) rest))
2213 (setq minor-mode-alist (cons (list toggle name)
2214 minor-mode-alist)))))))
2215 ;; Add the toggle to the minor-modes menu if requested.
2216 (when (get toggle :included)
2217 (define-key mode-line-mode-menu
2218 (vector toggle)
2219 (list 'menu-item
2220 (concat
2221 (or (get toggle :menu-tag)
2222 (if (stringp name) name (symbol-name toggle)))
2223 (let ((mode-name (if (symbolp name) (symbol-value name))))
2224 (if (and (stringp mode-name) (string-match "[^ ]+" mode-name))
2225 (concat " (" (match-string 0 mode-name) ")"))))
2226 toggle-fun
2227 :button (cons :toggle toggle))))
2229 ;; Add the map to the minor-mode-map-alist.
2230 (when keymap
2231 (let ((existing (assq toggle minor-mode-map-alist)))
2232 (if existing
2233 (setcdr existing keymap)
2234 (let ((tail minor-mode-map-alist) found)
2235 (while (and tail (not found))
2236 (if (eq after (caar tail))
2237 (setq found tail)
2238 (setq tail (cdr tail))))
2239 (if found
2240 (let ((rest (cdr found)))
2241 (setcdr found nil)
2242 (nconc found (list (cons toggle keymap)) rest))
2243 (setq minor-mode-map-alist (cons (cons toggle keymap)
2244 minor-mode-map-alist))))))))
2246 ;; Clones ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2248 (defun text-clone-maintain (ol1 after beg end &optional len)
2249 "Propagate the changes made under the overlay OL1 to the other clones.
2250 This is used on the `modification-hooks' property of text clones."
2251 (when (and after (not undo-in-progress) (overlay-start ol1))
2252 (let ((margin (if (overlay-get ol1 'text-clone-spreadp) 1 0)))
2253 (setq beg (max beg (+ (overlay-start ol1) margin)))
2254 (setq end (min end (- (overlay-end ol1) margin)))
2255 (when (<= beg end)
2256 (save-excursion
2257 (when (overlay-get ol1 'text-clone-syntax)
2258 ;; Check content of the clone's text.
2259 (let ((cbeg (+ (overlay-start ol1) margin))
2260 (cend (- (overlay-end ol1) margin)))
2261 (goto-char cbeg)
2262 (save-match-data
2263 (if (not (re-search-forward
2264 (overlay-get ol1 'text-clone-syntax) cend t))
2265 ;; Mark the overlay for deletion.
2266 (overlay-put ol1 'text-clones nil)
2267 (when (< (match-end 0) cend)
2268 ;; Shrink the clone at its end.
2269 (setq end (min end (match-end 0)))
2270 (move-overlay ol1 (overlay-start ol1)
2271 (+ (match-end 0) margin)))
2272 (when (> (match-beginning 0) cbeg)
2273 ;; Shrink the clone at its beginning.
2274 (setq beg (max (match-beginning 0) beg))
2275 (move-overlay ol1 (- (match-beginning 0) margin)
2276 (overlay-end ol1)))))))
2277 ;; Now go ahead and update the clones.
2278 (let ((head (- beg (overlay-start ol1)))
2279 (tail (- (overlay-end ol1) end))
2280 (str (buffer-substring beg end))
2281 (nothing-left t)
2282 (inhibit-modification-hooks t))
2283 (dolist (ol2 (overlay-get ol1 'text-clones))
2284 (let ((oe (overlay-end ol2)))
2285 (unless (or (eq ol1 ol2) (null oe))
2286 (setq nothing-left nil)
2287 (let ((mod-beg (+ (overlay-start ol2) head)))
2288 ;;(overlay-put ol2 'modification-hooks nil)
2289 (goto-char (- (overlay-end ol2) tail))
2290 (unless (> mod-beg (point))
2291 (save-excursion (insert str))
2292 (delete-region mod-beg (point)))
2293 ;;(overlay-put ol2 'modification-hooks '(text-clone-maintain))
2294 ))))
2295 (if nothing-left (delete-overlay ol1))))))))
2297 (defun text-clone-create (start end &optional spreadp syntax)
2298 "Create a text clone of START...END at point.
2299 Text clones are chunks of text that are automatically kept identical:
2300 changes done to one of the clones will be immediately propagated to the other.
2302 The buffer's content at point is assumed to be already identical to
2303 the one between START and END.
2304 If SYNTAX is provided it's a regexp that describes the possible text of
2305 the clones; the clone will be shrunk or killed if necessary to ensure that
2306 its text matches the regexp.
2307 If SPREADP is non-nil it indicates that text inserted before/after the
2308 clone should be incorporated in the clone."
2309 ;; To deal with SPREADP we can either use an overlay with `nil t' along
2310 ;; with insert-(behind|in-front-of)-hooks or use a slightly larger overlay
2311 ;; (with a one-char margin at each end) with `t nil'.
2312 ;; We opted for a larger overlay because it behaves better in the case
2313 ;; where the clone is reduced to the empty string (we want the overlay to
2314 ;; stay when the clone's content is the empty string and we want to use
2315 ;; `evaporate' to make sure those overlays get deleted when needed).
2317 (let* ((pt-end (+ (point) (- end start)))
2318 (start-margin (if (or (not spreadp) (bobp) (<= start (point-min)))
2319 0 1))
2320 (end-margin (if (or (not spreadp)
2321 (>= pt-end (point-max))
2322 (>= start (point-max)))
2323 0 1))
2324 (ol1 (make-overlay (- start start-margin) (+ end end-margin) nil t))
2325 (ol2 (make-overlay (- (point) start-margin) (+ pt-end end-margin) nil t))
2326 (dups (list ol1 ol2)))
2327 (overlay-put ol1 'modification-hooks '(text-clone-maintain))
2328 (when spreadp (overlay-put ol1 'text-clone-spreadp t))
2329 (when syntax (overlay-put ol1 'text-clone-syntax syntax))
2330 ;;(overlay-put ol1 'face 'underline)
2331 (overlay-put ol1 'evaporate t)
2332 (overlay-put ol1 'text-clones dups)
2334 (overlay-put ol2 'modification-hooks '(text-clone-maintain))
2335 (when spreadp (overlay-put ol2 'text-clone-spreadp t))
2336 (when syntax (overlay-put ol2 'text-clone-syntax syntax))
2337 ;;(overlay-put ol2 'face 'underline)
2338 (overlay-put ol2 'evaporate t)
2339 (overlay-put ol2 'text-clones dups)))
2341 (defun play-sound (sound)
2342 "SOUND is a list of the form `(sound KEYWORD VALUE...)'.
2343 The following keywords are recognized:
2345 :file FILE - read sound data from FILE. If FILE isn't an
2346 absolute file name, it is searched in `data-directory'.
2348 :data DATA - read sound data from string DATA.
2350 Exactly one of :file or :data must be present.
2352 :volume VOL - set volume to VOL. VOL must an integer in the
2353 range 0..100 or a float in the range 0..1.0. If not specified,
2354 don't change the volume setting of the sound device.
2356 :device DEVICE - play sound on DEVICE. If not specified,
2357 a system-dependent default device name is used."
2358 (unless (fboundp 'play-sound-internal)
2359 (error "This Emacs binary lacks sound support"))
2360 (play-sound-internal sound))
2362 (defun define-mail-user-agent (symbol composefunc sendfunc
2363 &optional abortfunc hookvar)
2364 "Define a symbol to identify a mail-sending package for `mail-user-agent'.
2366 SYMBOL can be any Lisp symbol. Its function definition and/or
2367 value as a variable do not matter for this usage; we use only certain
2368 properties on its property list, to encode the rest of the arguments.
2370 COMPOSEFUNC is program callable function that composes an outgoing
2371 mail message buffer. This function should set up the basics of the
2372 buffer without requiring user interaction. It should populate the
2373 standard mail headers, leaving the `to:' and `subject:' headers blank
2374 by default.
2376 COMPOSEFUNC should accept several optional arguments--the same
2377 arguments that `compose-mail' takes. See that function's documentation.
2379 SENDFUNC is the command a user would run to send the message.
2381 Optional ABORTFUNC is the command a user would run to abort the
2382 message. For mail packages that don't have a separate abort function,
2383 this can be `kill-buffer' (the equivalent of omitting this argument).
2385 Optional HOOKVAR is a hook variable that gets run before the message
2386 is actually sent. Callers that use the `mail-user-agent' may
2387 install a hook function temporarily on this hook variable.
2388 If HOOKVAR is nil, `mail-send-hook' is used.
2390 The properties used on SYMBOL are `composefunc', `sendfunc',
2391 `abortfunc', and `hookvar'."
2392 (put symbol 'composefunc composefunc)
2393 (put symbol 'sendfunc sendfunc)
2394 (put symbol 'abortfunc (or abortfunc 'kill-buffer))
2395 (put symbol 'hookvar (or hookvar 'mail-send-hook)))
2397 ;;; subr.el ends here