annotate lisp/cl-extra.el @ 5285:99de5fd48e87

Tighten up Common Lisp compatibility, #'butlast, #'nbutlast, #'tailp, #'ldiff lisp/ChangeLog addition: 2010-10-14 Aidan Kehoe <kehoea@parhasard.net> * byte-optimize.el (side-effect-free-fns): * cl-macs.el (remf, getf): * cl-extra.el (tailp, cl-set-getf, cl-do-remf): * cl.el (ldiff, endp): Tighten up Common Lisp compatibility for #'ldiff, #'endp, #'tailp; add circularity checking for the first two. #'cl-set-getf and #'cl-do-remf were Lisp implementations of #'plist-put and #'plist-remprop; change the names to aliases, changes the macros that use them to using #'plist-put and #'plist-remprop directly. src/ChangeLog addition: 2010-10-14 Aidan Kehoe <kehoea@parhasard.net> * fns.c (Fnbutlast, Fbutlast): Tighten up Common Lisp compatibility for these two functions; they need to operate on dotted lists without erroring. tests/ChangeLog addition: 2010-10-14 Aidan Kehoe <kehoea@parhasard.net> * automated/lisp-tests.el (x): Test #'nbutlast, #'butlast with dotted lists. Check that #'ldiff and #'tailp don't hang on circular lists; check that #'tailp returns t with circular lists when that is appropriate. Test them both with dotted lists.
author Aidan Kehoe <kehoea@parhasard.net>
date Thu, 14 Oct 2010 18:50:38 +0100
parents d27c1ee1943b
children 09fed7053634 b9167d522a9a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
613
023b83f4e54b [xemacs-hg @ 2001-06-10 10:42:16 by ben]
ben
parents: 502
diff changeset
1 ;;; cl-extra.el --- Common Lisp extensions for XEmacs Lisp (part two)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
3 ;; Copyright (C) 1993,2000,2003 Free Software Foundation, Inc.
801
2b676dc88c66 [xemacs-hg @ 2002-04-01 03:58:02 by ben]
ben
parents: 776
diff changeset
4 ;; Copyright (C) 2002 Ben Wing.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
5
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
6 ;; Author: Dave Gillespie <daveg@synaptics.com>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
7 ;; Maintainer: XEmacs Development Team
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
8 ;; Version: 2.02
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
9 ;; Keywords: extensions, dumped
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
10
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
11 ;; This file is part of XEmacs.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
12
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
13 ;; XEmacs is free software; you can redistribute it and/or modify it
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
14 ;; under the terms of the GNU General Public License as published by
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
15 ;; the Free Software Foundation; either version 2, or (at your option)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
16 ;; any later version.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
17
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
18 ;; XEmacs is distributed in the hope that it will be useful, but
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
19 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
21 ;; General Public License for more details.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
22
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
23 ;; You should have received a copy of the GNU General Public License
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
24 ;; along with XEmacs; see the file COPYING. If not, write to the Free
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
25 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
26 ;; 02111-1307, USA.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
27
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
28 ;;; Synched up with: FSF 21.3.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
29
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
30 ;;; Commentary:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
31
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
32 ;; This file is dumped with XEmacs.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
33
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
34 ;; These are extensions to Emacs Lisp that provide a degree of
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
35 ;; Common Lisp compatibility, beyond what is already built-in
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
36 ;; in Emacs Lisp.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
37 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
38 ;; This package was written by Dave Gillespie; it is a complete
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
39 ;; rewrite of Cesar Quiroz's original cl.el package of December 1986.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
40 ;;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
41 ;; Bug reports, comments, and suggestions are welcome!
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
42
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
43 ;; This file contains portions of the Common Lisp extensions
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
44 ;; package which are autoloaded since they are relatively obscure.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
45
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
46 ;; See cl.el for Change Log.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
47
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
48
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
49 ;;; Code:
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
50 ;; XEmacs addition
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
51 (eval-when-compile
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
52 (require 'obsolete))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
53
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
54 ;;; Type coercion.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
55
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
56 (defun coerce (x type)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
57 "Coerce OBJECT to type TYPE.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
58 TYPE is a Common Lisp type specifier."
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
59 (cond ((eq type 'list) (if (listp x) x (append x nil)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
60 ((eq type 'vector) (if (vectorp x) x (vconcat x)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
61 ((eq type 'string) (if (stringp x) x (concat x)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
62 ((eq type 'array) (if (arrayp x) x (vconcat x)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
63 ((and (eq type 'character) (stringp x) (= (length x) 1)) (aref x 0))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
64 ((and (eq type 'character) (symbolp x)) (coerce (symbol-name x) type))
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
65 ;; XEmacs addition character <-> integer coercions
446
1ccc32a20af4 Import from CVS: tag r21-2-38
cvs
parents: 442
diff changeset
66 ((and (eq type 'character) (char-int-p x)) (int-char x))
5257
30bf66dd3ca0 Add fixnum as an accepted destination type, #'coerce
Aidan Kehoe <kehoea@parhasard.net>
parents: 5242
diff changeset
67 ((and (memq type '(integer fixnum)) (characterp x)) (char-int x))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
68 ((eq type 'float) (float x))
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
69 ;; XEmacs addition: enhanced numeric type coercions
2367
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 2153
diff changeset
70 ((and-fboundp 'coerce-number
5257
30bf66dd3ca0 Add fixnum as an accepted destination type, #'coerce
Aidan Kehoe <kehoea@parhasard.net>
parents: 5242
diff changeset
71 (memq type '(integer ratio bigfloat fixnum))
2367
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 2153
diff changeset
72 (coerce-number x type)))
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
73 ;; XEmacs addition: bit-vector coercion
4995
8431b52e43b1 Move the various map* functions to C; add #'map-into.
Aidan Kehoe <kehoea@parhasard.net>
parents: 4800
diff changeset
74 ((or (eq type 'bit-vector)
8431b52e43b1 Move the various map* functions to C; add #'map-into.
Aidan Kehoe <kehoea@parhasard.net>
parents: 4800
diff changeset
75 (eq type 'simple-bit-vector))
8431b52e43b1 Move the various map* functions to C; add #'map-into.
Aidan Kehoe <kehoea@parhasard.net>
parents: 4800
diff changeset
76 (if (bit-vector-p x) x (apply 'bit-vector (append x nil))))
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
77 ;; XEmacs addition: weak-list coercion
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
78 ((eq type 'weak-list)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
79 (if (weak-list-p x) x
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
80 (let ((wl (make-weak-list)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
81 (set-weak-list-list wl (if (listp x) x (append x nil)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
82 wl)))
4995
8431b52e43b1 Move the various map* functions to C; add #'map-into.
Aidan Kehoe <kehoea@parhasard.net>
parents: 4800
diff changeset
83 ((and
8431b52e43b1 Move the various map* functions to C; add #'map-into.
Aidan Kehoe <kehoea@parhasard.net>
parents: 4800
diff changeset
84 (consp type)
8431b52e43b1 Move the various map* functions to C; add #'map-into.
Aidan Kehoe <kehoea@parhasard.net>
parents: 4800
diff changeset
85 (or (eq (car type) 'vector)
8431b52e43b1 Move the various map* functions to C; add #'map-into.
Aidan Kehoe <kehoea@parhasard.net>
parents: 4800
diff changeset
86 (eq (car type) 'simple-array)
8431b52e43b1 Move the various map* functions to C; add #'map-into.
Aidan Kehoe <kehoea@parhasard.net>
parents: 4800
diff changeset
87 (eq (car type) 'simple-vector))
8431b52e43b1 Move the various map* functions to C; add #'map-into.
Aidan Kehoe <kehoea@parhasard.net>
parents: 4800
diff changeset
88 (cond
8431b52e43b1 Move the various map* functions to C; add #'map-into.
Aidan Kehoe <kehoea@parhasard.net>
parents: 4800
diff changeset
89 ((equal (cdr-safe type) '(*))
8431b52e43b1 Move the various map* functions to C; add #'map-into.
Aidan Kehoe <kehoea@parhasard.net>
parents: 4800
diff changeset
90 (coerce x 'vector))
8431b52e43b1 Move the various map* functions to C; add #'map-into.
Aidan Kehoe <kehoea@parhasard.net>
parents: 4800
diff changeset
91 ((equal (cdr-safe type) '(bit))
8431b52e43b1 Move the various map* functions to C; add #'map-into.
Aidan Kehoe <kehoea@parhasard.net>
parents: 4800
diff changeset
92 (coerce x 'bit-vector))
8431b52e43b1 Move the various map* functions to C; add #'map-into.
Aidan Kehoe <kehoea@parhasard.net>
parents: 4800
diff changeset
93 ((equal (cdr-safe type) '(character))
8431b52e43b1 Move the various map* functions to C; add #'map-into.
Aidan Kehoe <kehoea@parhasard.net>
parents: 4800
diff changeset
94 (coerce x 'string)))))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
95 ((typep x type) x)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
96 (t (error "Can't coerce %s to type %s" x type))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
97
5219
2d0937dc83cf Tidying of CL files; make docstrings read better, remove commented-out code
Aidan Kehoe <kehoea@parhasard.net>
parents: 5162
diff changeset
98 ;; XEmacs; #'equalp is in C.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
99
4997
8800b5350a13 Move #'some, #'every to C, implementing them with mapcarX.
Aidan Kehoe <kehoea@parhasard.net>
parents: 4996
diff changeset
100 ;; XEmacs; #'map, #'mapc, #'mapl, #'maplist, #'mapcon, #'some and #'every
8800b5350a13 Move #'some, #'every to C, implementing them with mapcarX.
Aidan Kehoe <kehoea@parhasard.net>
parents: 4996
diff changeset
101 ;; are now in C, together with #'map-into, which was never in this file.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
102
5226
7789ae555c45 Add Common Lisp's #'complement to cl-extra.el.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5219
diff changeset
103 ;; The compiler macro for this in cl-macs.el means if #'complement is handed
7789ae555c45 Add Common Lisp's #'complement to cl-extra.el.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5219
diff changeset
104 ;; a constant expression, byte-compiled code will see a byte-compiled
7789ae555c45 Add Common Lisp's #'complement to cl-extra.el.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5219
diff changeset
105 ;; function.
7789ae555c45 Add Common Lisp's #'complement to cl-extra.el.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5219
diff changeset
106 (defun complement (function &optional documentation)
7789ae555c45 Add Common Lisp's #'complement to cl-extra.el.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5219
diff changeset
107 "Return a function which gives the logical inverse of what FUNCTION would."
7789ae555c45 Add Common Lisp's #'complement to cl-extra.el.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5219
diff changeset
108 `(lambda (&rest arguments) ,@(if documentation (list documentation))
7789ae555c45 Add Common Lisp's #'complement to cl-extra.el.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5219
diff changeset
109 (not (apply ',function arguments))))
7789ae555c45 Add Common Lisp's #'complement to cl-extra.el.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5219
diff changeset
110
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
111 (defun notany (cl-pred cl-seq &rest cl-rest)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
112 "Return true if PREDICATE is false of every element of SEQ or SEQs."
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
113 (not (apply 'some cl-pred cl-seq cl-rest)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
114
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
115 (defun notevery (cl-pred cl-seq &rest cl-rest)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
116 "Return true if PREDICATE is false of some element of SEQ or SEQs."
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
117 (not (apply 'every cl-pred cl-seq cl-rest)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
118
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
119 ;;; Support for `loop'.
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
120 (defalias 'cl-map-keymap 'map-keymap)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
121
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
122 (defun cl-map-keymap-recursively (cl-func-rec cl-map &optional cl-base)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
123 (or cl-base
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
124 (setq cl-base (copy-sequence [0])))
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
125 (map-keymap
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
126 (function
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
127 (lambda (cl-key cl-bind)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
128 (aset cl-base (1- (length cl-base)) cl-key)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
129 (if (keymapp cl-bind)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
130 (cl-map-keymap-recursively
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
131 cl-func-rec cl-bind
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
132 (vconcat cl-base (list 0)))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
133 (funcall cl-func-rec cl-base cl-bind))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
134 cl-map))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
135
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
136 (defun cl-map-intervals (cl-func &optional cl-what cl-prop cl-start cl-end)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
137 (or cl-what (setq cl-what (current-buffer)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
138 (if (bufferp cl-what)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
139 (let (cl-mark cl-mark2 (cl-next t) cl-next2)
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
140 (with-current-buffer cl-what
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
141 (setq cl-mark (copy-marker (or cl-start (point-min))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
142 (setq cl-mark2 (and cl-end (copy-marker cl-end))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
143 (while (and cl-next (or (not cl-mark2) (< cl-mark cl-mark2)))
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
144 (setq cl-next (if cl-prop (next-single-property-change
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
145 cl-mark cl-prop cl-what)
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
146 (next-property-change cl-mark cl-what))
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
147 cl-next2 (or cl-next (with-current-buffer cl-what
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
148 (point-max))))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
149 (funcall cl-func (prog1 (marker-position cl-mark)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
150 (set-marker cl-mark cl-next2))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
151 (if cl-mark2 (min cl-next2 cl-mark2) cl-next2)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
152 (set-marker cl-mark nil) (if cl-mark2 (set-marker cl-mark2 nil)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
153 (or cl-start (setq cl-start 0))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
154 (or cl-end (setq cl-end (length cl-what)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
155 (while (< cl-start cl-end)
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
156 (let ((cl-next (or (if cl-prop (next-single-property-change
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
157 cl-start cl-prop cl-what)
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
158 (next-property-change cl-start cl-what))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
159 cl-end)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
160 (funcall cl-func cl-start (min cl-next cl-end))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
161 (setq cl-start cl-next)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
162
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
163 (defun cl-map-overlays (cl-func &optional cl-buffer cl-start cl-end cl-arg)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
164 (or cl-buffer (setq cl-buffer (current-buffer)))
502
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
165 (with-fboundp '(overlay-start overlay-end overlays-at next-overlay-change)
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
166 (if-fboundp 'overlay-lists
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
167
502
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
168 ;; This is the preferred algorithm, though overlay-lists is
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
169 ;; undocumented.
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
170 (let (cl-ovl)
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
171 (with-current-buffer cl-buffer
502
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
172 (setq cl-ovl (overlay-lists))
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
173 (if cl-start (setq cl-start (copy-marker cl-start)))
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
174 (if cl-end (setq cl-end (copy-marker cl-end))))
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
175 (setq cl-ovl (nconc (car cl-ovl) (cdr cl-ovl)))
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
176 (while (and cl-ovl
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
177 (or (not (overlay-start (car cl-ovl)))
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
178 (and cl-end (>= (overlay-start (car cl-ovl)) cl-end))
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
179 (and cl-start (<= (overlay-end (car cl-ovl))
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
180 cl-start))
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
181 (not (funcall cl-func (car cl-ovl) cl-arg))))
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
182 (setq cl-ovl (cdr cl-ovl)))
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
183 (if cl-start (set-marker cl-start nil))
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
184 (if cl-end (set-marker cl-end nil)))
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
185
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
186 ;; This alternate algorithm fails to find zero-length overlays.
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
187 (let ((cl-mark (with-current-buffer cl-buffer
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
188 (copy-marker (or cl-start (point-min)))))
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
189 (cl-mark2 (and cl-end (with-current-buffer cl-buffer
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
190 (copy-marker cl-end))))
502
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
191 cl-pos cl-ovl)
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
192 (while (save-excursion
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
193 (and (setq cl-pos (marker-position cl-mark))
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
194 (< cl-pos (or cl-mark2 (point-max)))
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
195 (progn
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
196 (set-buffer cl-buffer)
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
197 (setq cl-ovl (overlays-at cl-pos))
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
198 (set-marker cl-mark (next-overlay-change cl-pos)))))
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
199 (while (and cl-ovl
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
200 (or (/= (overlay-start (car cl-ovl)) cl-pos)
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
201 (not (and (funcall cl-func (car cl-ovl) cl-arg)
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
202 (set-marker cl-mark nil)))))
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
203 (setq cl-ovl (cdr cl-ovl))))
7039e6323819 [xemacs-hg @ 2001-05-04 22:41:46 by ben]
ben
parents: 446
diff changeset
204 (set-marker cl-mark nil) (if cl-mark2 (set-marker cl-mark2 nil))))))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
205
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
206 ;;; Support for `setf'.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
207 (defun cl-set-frame-visible-p (frame val)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
208 (cond ((null val) (make-frame-invisible frame))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
209 ((eq val 'icon) (iconify-frame frame))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
210 (t (make-frame-visible frame)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
211 val)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
212
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
213 ;;; Support for `progv'.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
214 (defvar cl-progv-save)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
215 (defun cl-progv-before (syms values)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
216 (while syms
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
217 (push (if (boundp (car syms))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
218 (cons (car syms) (symbol-value (car syms)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
219 (car syms)) cl-progv-save)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
220 (if values
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
221 (set (pop syms) (pop values))
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
222 (makunbound (pop syms)))))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
223
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
224 (defun cl-progv-after ()
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
225 (while cl-progv-save
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
226 (if (consp (car cl-progv-save))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
227 (set (car (car cl-progv-save)) (cdr (car cl-progv-save)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
228 (makunbound (car cl-progv-save)))
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
229 (pop cl-progv-save)))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
230
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
231 ;;; Numbers.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
232
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
233 (defun gcd (&rest args)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
234 "Return the greatest common divisor of the arguments."
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
235 (let ((a (abs (or (pop args) 0))))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
236 (while args
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
237 (let ((b (abs (pop args))))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
238 (while (> b 0) (setq b (% a (setq a b))))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
239 a))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
240
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
241 (defun lcm (&rest args)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
242 "Return the least common multiple of the arguments."
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
243 (if (memq 0 args)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
244 0
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
245 (let ((a (abs (or (pop args) 1))))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
246 (while args
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
247 (let ((b (abs (pop args))))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
248 (setq a (* (/ a (gcd a b)) b))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
249 a)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
250
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
251 (defun isqrt (a)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
252 "Return the integer square root of the argument."
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
253 (if (and (integerp a) (> a 0))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
254 ;; XEmacs change
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
255 (let ((g (cond ((>= a 1000000) 10000) ((>= a 10000) 1000)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
256 ((>= a 100) 100) (t 10)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
257 g2)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
258 (while (< (setq g2 (/ (+ g (/ a g)) 2)) g)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
259 (setq g g2))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
260 g)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
261 (if (eq a 0) 0 (signal 'arith-error nil))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
262
4678
b5e1d4f6b66f Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents: 2367
diff changeset
263 ;; We can't use macrolet in this file; whence the literal macro
b5e1d4f6b66f Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents: 2367
diff changeset
264 ;; definition-and-call:
b5e1d4f6b66f Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents: 2367
diff changeset
265 ((macro . (lambda (&rest symbols)
b5e1d4f6b66f Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents: 2367
diff changeset
266 "Make some old CL package truncate and round functions available.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
267
4678
b5e1d4f6b66f Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents: 2367
diff changeset
268 These functions are now implemented in C; their Lisp implementations in this
b5e1d4f6b66f Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents: 2367
diff changeset
269 XEmacs are trivial, so we provide them and mark them obsolete."
b5e1d4f6b66f Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents: 2367
diff changeset
270 (let (symbol result)
b5e1d4f6b66f Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents: 2367
diff changeset
271 (while symbols
b5e1d4f6b66f Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents: 2367
diff changeset
272 (setq symbol (car symbols)
b5e1d4f6b66f Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents: 2367
diff changeset
273 symbols (cdr symbols))
b5e1d4f6b66f Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents: 2367
diff changeset
274 (push `(make-obsolete ',(intern (format "%s*" symbol))
b5e1d4f6b66f Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents: 2367
diff changeset
275 ',symbol "21.5.29")
4800
b828e06dbe38 New (Common Lisp) function get-propertie
Didier Verna <didier@xemacs.org>
parents: 4792
diff changeset
276 result)
4678
b5e1d4f6b66f Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents: 2367
diff changeset
277 (push
b5e1d4f6b66f Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents: 2367
diff changeset
278 `(defun ,(intern (format "%s*" symbol)) (number &optional divisor)
b5e1d4f6b66f Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents: 2367
diff changeset
279 ,(format "See `%s'. This returns a list, not multiple values."
b5e1d4f6b66f Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents: 2367
diff changeset
280 symbol)
b5e1d4f6b66f Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents: 2367
diff changeset
281 (multiple-value-list (,symbol number divisor)))
b5e1d4f6b66f Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents: 2367
diff changeset
282 result))
b5e1d4f6b66f Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents: 2367
diff changeset
283 (cons 'progn result))))
b5e1d4f6b66f Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents: 2367
diff changeset
284 ceiling floor round truncate)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
285
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
286 (defun mod* (x y)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
287 "The remainder of X divided by Y, with the same sign as Y."
4678
b5e1d4f6b66f Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents: 2367
diff changeset
288 (nth-value 1 (floor x y)))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
289
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
290 (defun rem* (x y)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
291 "The remainder of X divided by Y, with the same sign as X."
4678
b5e1d4f6b66f Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents: 2367
diff changeset
292 (nth-value 1 (truncate x y)))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
293
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
294 (defun signum (a)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
295 "Return 1 if A is positive, -1 if negative, 0 if zero."
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
296 (cond ((> a 0) 1) ((< a 0) -1) (t 0)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
297
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
298 ;; Random numbers.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
299
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
300 (defvar *random-state*)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
301 (defun random* (lim &optional state)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
302 "Return a random nonnegative number less than LIM, an integer or float.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
303 Optional second arg STATE is a random-state object."
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
304 (or state (setq state *random-state*))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
305 ;; Inspired by "ran3" from Numerical Recipes. Additive congruential method.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
306 (let ((vec (aref state 3)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
307 (if (integerp vec)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
308 (let ((i 0) (j (- 1357335 (% (abs vec) 1357333))) (k 1))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
309 (aset state 3 (setq vec (make-vector 55 nil)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
310 (aset vec 0 j)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
311 (while (> (setq i (% (+ i 21) 55)) 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
312 (aset vec i (setq j (prog1 k (setq k (- j k))))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
313 (while (< (setq i (1+ i)) 200) (random* 2 state))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
314 (let* ((i (aset state 1 (% (1+ (aref state 1)) 55)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
315 (j (aset state 2 (% (1+ (aref state 2)) 55)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
316 (n (logand 8388607 (aset vec i (- (aref vec i) (aref vec j))))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
317 (if (integerp lim)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
318 (if (<= lim 512) (% n lim)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
319 (if (> lim 8388607) (setq n (+ (lsh n 9) (random* 512 state))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
320 (let ((mask 1023))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
321 (while (< mask (1- lim)) (setq mask (1+ (+ mask mask))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
322 (if (< (setq n (logand n mask)) lim) n (random* lim state))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
323 (* (/ n '8388608e0) lim)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
324
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
325 (defun make-random-state (&optional state)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
326 "Return a copy of random-state STATE, or of `*random-state*' if omitted.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
327 If STATE is t, return a new state object seeded from the time of day."
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
328 (cond ((null state) (make-random-state *random-state*))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
329 ((vectorp state) (cl-copy-tree state t))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
330 ((integerp state) (vector 'cl-random-state-tag -1 30 state))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
331 (t (make-random-state (cl-random-time)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
332
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
333 (defun random-state-p (object)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
334 "Return t if OBJECT is a random-state object."
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
335 (and (vectorp object) (= (length object) 4)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
336 (eq (aref object 0) 'cl-random-state-tag)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
337
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
338
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
339 ;; Implementation limits.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
340
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
341 (defun cl-finite-do (func a b)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
342 (condition-case nil
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
343 (let ((res (funcall func a b))) ; check for IEEE infinity
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
344 (and (numberp res) (/= res (/ res 2)) res))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
345 (arith-error nil)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
346
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
347 (defun cl-float-limits ()
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
348 (or most-positive-float (not (numberp '2e1))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
349 (let ((x '2e0) y z)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
350 ;; Find maximum exponent (first two loops are optimizations)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
351 (while (cl-finite-do '* x x) (setq x (* x x)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
352 (while (cl-finite-do '* x (/ x 2)) (setq x (* x (/ x 2))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
353 (while (cl-finite-do '+ x x) (setq x (+ x x)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
354 (setq z x y (/ x 2))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
355 ;; Now fill in 1's in the mantissa.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
356 (while (and (cl-finite-do '+ x y) (/= (+ x y) x))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
357 (setq x (+ x y) y (/ y 2)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
358 (setq most-positive-float x
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
359 most-negative-float (- x))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
360 ;; Divide down until mantissa starts rounding.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
361 (setq x (/ x z) y (/ 16 z) x (* x y))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
362 (while (condition-case nil (and (= x (* (/ x 2) 2)) (> (/ y 2) 0))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
363 (arith-error nil))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
364 (setq x (/ x 2) y (/ y 2)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
365 (setq least-positive-normalized-float y
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
366 least-negative-normalized-float (- y))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
367 ;; Divide down until value underflows to zero.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
368 (setq x (/ 1 z) y x)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
369 (while (condition-case nil (> (/ x 2) 0) (arith-error nil))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
370 (setq x (/ x 2)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
371 (setq least-positive-float x
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
372 least-negative-float (- x))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
373 (setq x '1e0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
374 (while (/= (+ '1e0 x) '1e0) (setq x (/ x 2)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
375 (setq float-epsilon (* x 2))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
376 (setq x '1e0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
377 (while (/= (- '1e0 x) '1e0) (setq x (/ x 2)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
378 (setq float-negative-epsilon (* x 2))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
379 nil)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
380
5219
2d0937dc83cf Tidying of CL files; make docstrings read better, remove commented-out code
Aidan Kehoe <kehoea@parhasard.net>
parents: 5162
diff changeset
381 ;; XEmacs; call cl-float-limits at dump time.
2d0937dc83cf Tidying of CL files; make docstrings read better, remove commented-out code
Aidan Kehoe <kehoea@parhasard.net>
parents: 5162
diff changeset
382 (cl-float-limits)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
383
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
384 ;;; Sequence functions.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
385
5219
2d0937dc83cf Tidying of CL files; make docstrings read better, remove commented-out code
Aidan Kehoe <kehoea@parhasard.net>
parents: 5162
diff changeset
386 ;; XEmacs; #'subseq is in C.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
387
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
388 (defun concatenate (type &rest seqs)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
389 "Concatenate, into a sequence of type TYPE, the argument SEQUENCES."
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
390 ;; XEmacs change: use case instead of cond for clarity
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
391 (case type
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
392 (vector (apply 'vconcat seqs))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
393 (string (apply 'concat seqs))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
394 (list (apply 'append (append seqs '(nil))))
5242
f3eca926258e Bit vectors are also sequences; enforce this in some CL functions.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5226
diff changeset
395 (bit-vector (apply 'bvconcat seqs))
5219
2d0937dc83cf Tidying of CL files; make docstrings read better, remove commented-out code
Aidan Kehoe <kehoea@parhasard.net>
parents: 5162
diff changeset
396 (t (error 'invalid-argument "Not a sequence type name" type))))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
397
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
398 ;;; List functions.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
399
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
400 (defun revappend (x y)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
401 "Equivalent to (append (reverse X) Y)."
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
402 (nconc (reverse x) y))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
403
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
404 (defun nreconc (x y)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
405 "Equivalent to (nconc (nreverse X) Y)."
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
406 (nconc (nreverse x) y))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
407
5285
99de5fd48e87 Tighten up Common Lisp compatibility, #'butlast, #'nbutlast, #'tailp, #'ldiff
Aidan Kehoe <kehoea@parhasard.net>
parents: 5284
diff changeset
408 ;; XEmacs; check LIST for type and circularity.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
409 (defun tailp (sublist list)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
410 "Return true if SUBLIST is a tail of LIST."
5285
99de5fd48e87 Tighten up Common Lisp compatibility, #'butlast, #'nbutlast, #'tailp, #'ldiff
Aidan Kehoe <kehoea@parhasard.net>
parents: 5284
diff changeset
411 (check-argument-type #'listp list)
99de5fd48e87 Tighten up Common Lisp compatibility, #'butlast, #'nbutlast, #'tailp, #'ldiff
Aidan Kehoe <kehoea@parhasard.net>
parents: 5284
diff changeset
412 (let ((before list) (evenp t))
99de5fd48e87 Tighten up Common Lisp compatibility, #'butlast, #'nbutlast, #'tailp, #'ldiff
Aidan Kehoe <kehoea@parhasard.net>
parents: 5284
diff changeset
413 (while (and (consp list) (not (eq sublist list)))
99de5fd48e87 Tighten up Common Lisp compatibility, #'butlast, #'nbutlast, #'tailp, #'ldiff
Aidan Kehoe <kehoea@parhasard.net>
parents: 5284
diff changeset
414 (setq list (cdr list)
99de5fd48e87 Tighten up Common Lisp compatibility, #'butlast, #'nbutlast, #'tailp, #'ldiff
Aidan Kehoe <kehoea@parhasard.net>
parents: 5284
diff changeset
415 evenp (not evenp))
99de5fd48e87 Tighten up Common Lisp compatibility, #'butlast, #'nbutlast, #'tailp, #'ldiff
Aidan Kehoe <kehoea@parhasard.net>
parents: 5284
diff changeset
416 (if evenp (setq before (cdr before)))
99de5fd48e87 Tighten up Common Lisp compatibility, #'butlast, #'nbutlast, #'tailp, #'ldiff
Aidan Kehoe <kehoea@parhasard.net>
parents: 5284
diff changeset
417 (if (eq before list) (error 'circular-list list)))
99de5fd48e87 Tighten up Common Lisp compatibility, #'butlast, #'nbutlast, #'tailp, #'ldiff
Aidan Kehoe <kehoea@parhasard.net>
parents: 5284
diff changeset
418 (eql sublist list)))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
419
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
420 (defalias 'cl-copy-tree 'copy-tree)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
421
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
422 ;;; Property lists.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
423
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
424 ;; XEmacs: our `get' groks DEFAULT.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
425 (defalias 'get* 'get)
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
426 (defalias 'getf 'plist-get)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
427
5285
99de5fd48e87 Tighten up Common Lisp compatibility, #'butlast, #'nbutlast, #'tailp, #'ldiff
Aidan Kehoe <kehoea@parhasard.net>
parents: 5284
diff changeset
428 ;; XEmacs; these are built-in.
99de5fd48e87 Tighten up Common Lisp compatibility, #'butlast, #'nbutlast, #'tailp, #'ldiff
Aidan Kehoe <kehoea@parhasard.net>
parents: 5284
diff changeset
429 (defalias 'cl-set-getf 'plist-put)
99de5fd48e87 Tighten up Common Lisp compatibility, #'butlast, #'nbutlast, #'tailp, #'ldiff
Aidan Kehoe <kehoea@parhasard.net>
parents: 5284
diff changeset
430 (defalias 'cl-do-remf 'plist-remprop)
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
431 (defalias 'cl-remprop 'remprop)
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
432
4800
b828e06dbe38 New (Common Lisp) function get-propertie
Didier Verna <didier@xemacs.org>
parents: 4792
diff changeset
433 (defun get-properties (plist indicator-list)
b828e06dbe38 New (Common Lisp) function get-propertie
Didier Verna <didier@xemacs.org>
parents: 4792
diff changeset
434 "Find a property from INDICATOR-LIST in PLIST.
b828e06dbe38 New (Common Lisp) function get-propertie
Didier Verna <didier@xemacs.org>
parents: 4792
diff changeset
435 Return 3 values:
b828e06dbe38 New (Common Lisp) function get-propertie
Didier Verna <didier@xemacs.org>
parents: 4792
diff changeset
436 - the first property found,
b828e06dbe38 New (Common Lisp) function get-propertie
Didier Verna <didier@xemacs.org>
parents: 4792
diff changeset
437 - its value,
b828e06dbe38 New (Common Lisp) function get-propertie
Didier Verna <didier@xemacs.org>
parents: 4792
diff changeset
438 - the tail of PLIST beginning with the found entry."
b828e06dbe38 New (Common Lisp) function get-propertie
Didier Verna <didier@xemacs.org>
parents: 4792
diff changeset
439 (do ((plst plist (cddr plst)))
b828e06dbe38 New (Common Lisp) function get-propertie
Didier Verna <didier@xemacs.org>
parents: 4792
diff changeset
440 ((null plst) (values nil nil nil))
b828e06dbe38 New (Common Lisp) function get-propertie
Didier Verna <didier@xemacs.org>
parents: 4792
diff changeset
441 (cond ((atom (cdr plst))
b828e06dbe38 New (Common Lisp) function get-propertie
Didier Verna <didier@xemacs.org>
parents: 4792
diff changeset
442 (error "Malformed property list: %S." plist))
b828e06dbe38 New (Common Lisp) function get-propertie
Didier Verna <didier@xemacs.org>
parents: 4792
diff changeset
443 ((memq (car plst) indicator-list)
b828e06dbe38 New (Common Lisp) function get-propertie
Didier Verna <didier@xemacs.org>
parents: 4792
diff changeset
444 (return (values (car plst) (cadr plst) plst))))))
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
445
5075
868a9ffcc37b Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5056
diff changeset
446 ;; See also the compiler macro in cl-macs.el.
5056
6aba0daedb7c Add #'constantly, as specified by ANSI Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents: 4997
diff changeset
447 (defun constantly (value &rest more-values)
6aba0daedb7c Add #'constantly, as specified by ANSI Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents: 4997
diff changeset
448 "Construct a function always returning VALUE, and possibly MORE-VALUES.
6aba0daedb7c Add #'constantly, as specified by ANSI Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents: 4997
diff changeset
449
6aba0daedb7c Add #'constantly, as specified by ANSI Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents: 4997
diff changeset
450 The constructed function accepts any number of arguments, and ignores them.
6aba0daedb7c Add #'constantly, as specified by ANSI Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents: 4997
diff changeset
451
6aba0daedb7c Add #'constantly, as specified by ANSI Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents: 4997
diff changeset
452 Members of MORE-VALUES, if provided, will be passed as multiple values; see
6aba0daedb7c Add #'constantly, as specified by ANSI Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents: 4997
diff changeset
453 `multiple-value-bind' and `multiple-value-setq'."
5075
868a9ffcc37b Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5056
diff changeset
454 (symbol-macrolet
868a9ffcc37b Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5056
diff changeset
455 ((arglist '(&rest ignore)))
868a9ffcc37b Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5056
diff changeset
456 (if (or more-values (eval-when-compile (not (cl-compiling-file))))
868a9ffcc37b Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5056
diff changeset
457 `(lambda ,arglist (values-list ',(cons value more-values)))
868a9ffcc37b Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5056
diff changeset
458 (make-byte-code
868a9ffcc37b Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5056
diff changeset
459 arglist
868a9ffcc37b Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5056
diff changeset
460 (eval-when-compile
868a9ffcc37b Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5056
diff changeset
461 (let ((compiled (byte-compile-sexp #'(lambda (&rest ignore)
868a9ffcc37b Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5056
diff changeset
462 (declare (ignore ignore))
868a9ffcc37b Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5056
diff changeset
463 'placeholder))))
868a9ffcc37b Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5056
diff changeset
464 (assert (and
868a9ffcc37b Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5056
diff changeset
465 (equal [placeholder]
868a9ffcc37b Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5056
diff changeset
466 (compiled-function-constants compiled))
868a9ffcc37b Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5056
diff changeset
467 (= 1 (compiled-function-stack-depth compiled)))
868a9ffcc37b Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5056
diff changeset
468 t
868a9ffcc37b Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5056
diff changeset
469 "Our assumptions about compiled code appear not to hold.")
868a9ffcc37b Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5056
diff changeset
470 (compiled-function-instructions compiled)))
868a9ffcc37b Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5056
diff changeset
471 (vector value) 1))))
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
472
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
473 ;;; Hash tables.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
474
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
475 ;; The `regular' Common Lisp hash-table stuff has been moved into C.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
476 ;; Only backward compatibility stuff remains here.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
477 (defun make-hashtable (size &optional test)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
478 (make-hash-table :test test :size size))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
479 (defun make-weak-hashtable (size &optional test)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
480 (make-hash-table :test test :size size :weakness t))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
481 (defun make-key-weak-hashtable (size &optional test)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
482 (make-hash-table :test test :size size :weakness 'key))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
483 (defun make-value-weak-hashtable (size &optional test)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
484 (make-hash-table :test test :size size :weakness 'value))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
485
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
486 (define-obsolete-function-alias 'hashtablep 'hash-table-p)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
487 (define-obsolete-function-alias 'hashtable-fullness 'hash-table-count)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
488 (define-obsolete-function-alias 'hashtable-test-function 'hash-table-test)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
489 (define-obsolete-function-alias 'hashtable-type 'hash-table-type)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
490 (define-obsolete-function-alias 'hashtable-size 'hash-table-size)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
491 (define-obsolete-function-alias 'copy-hashtable 'copy-hash-table)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
492
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
493 (make-obsolete 'make-hashtable 'make-hash-table)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
494 (make-obsolete 'make-weak-hashtable 'make-hash-table)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
495 (make-obsolete 'make-key-weak-hashtable 'make-hash-table)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
496 (make-obsolete 'make-value-weak-hashtable 'make-hash-table)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
497 (make-obsolete 'hash-table-type 'hash-table-weakness)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
498
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
499 (when (fboundp 'x-keysym-hash-table)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
500 (make-obsolete 'x-keysym-hashtable 'x-keysym-hash-table))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
501
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
502 ;; Compatibility stuff for old kludgy cl.el hash table implementation
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
503 (defvar cl-builtin-gethash (symbol-function 'gethash))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
504 (defvar cl-builtin-remhash (symbol-function 'remhash))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
505 (defvar cl-builtin-clrhash (symbol-function 'clrhash))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
506 (defvar cl-builtin-maphash (symbol-function 'maphash))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
507
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
508 (defalias 'cl-gethash 'gethash)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
509 (defalias 'cl-puthash 'puthash)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
510 (defalias 'cl-remhash 'remhash)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
511 (defalias 'cl-clrhash 'clrhash)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
512 (defalias 'cl-maphash 'maphash)
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
513 ;; These three actually didn't exist in Emacs-20.
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
514 (defalias 'cl-make-hash-table 'make-hash-table)
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
515 (defalias 'cl-hash-table-p 'hash-table-p)
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
516 (defalias 'cl-hash-table-count 'hash-table-count)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
517
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
518 ;;; Some debugging aids.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
519
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
520 (defun cl-prettyprint (form)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
521 "Insert a pretty-printed rendition of a Lisp FORM in current buffer."
5162
41262f87eb39 Handle (function ...) specially, cl-prettyprint.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5075
diff changeset
522 (let ((pt (point)) last just)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
523 (insert "\n" (prin1-to-string form) "\n")
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
524 (setq last (point))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
525 (goto-char (1+ pt))
5162
41262f87eb39 Handle (function ...) specially, cl-prettyprint.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5075
diff changeset
526 (while (re-search-forward "(\\(?:\\(?:function\\|quote\\) \\)" last t)
41262f87eb39 Handle (function ...) specially, cl-prettyprint.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5075
diff changeset
527 (delete-region (match-beginning 0) (match-end 0))
41262f87eb39 Handle (function ...) specially, cl-prettyprint.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5075
diff changeset
528 (if (= (length "(function ") (- (match-end 0) (match-beginning 0)))
41262f87eb39 Handle (function ...) specially, cl-prettyprint.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5075
diff changeset
529 (insert "#'")
41262f87eb39 Handle (function ...) specially, cl-prettyprint.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5075
diff changeset
530 (insert "'"))
41262f87eb39 Handle (function ...) specially, cl-prettyprint.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5075
diff changeset
531 (setq just (point))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
532 (forward-sexp)
5162
41262f87eb39 Handle (function ...) specially, cl-prettyprint.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5075
diff changeset
533 (delete-char 1)
41262f87eb39 Handle (function ...) specially, cl-prettyprint.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5075
diff changeset
534 (goto-char just))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
535 (goto-char (1+ pt))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
536 (cl-do-prettyprint)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
537
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
538 (defun cl-do-prettyprint ()
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
539 (skip-chars-forward " ")
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
540 (if (looking-at "(")
4800
b828e06dbe38 New (Common Lisp) function get-propertie
Didier Verna <didier@xemacs.org>
parents: 4792
diff changeset
541 (let ((skip (or (looking-at "((")
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
542 ;; XEmacs: be selective about trailing stuff after prog
1729
175ee2cb4d3a [xemacs-hg @ 2003-09-30 22:09:31 by youngs]
youngs
parents: 801
diff changeset
543 (looking-at "(prog[nv12\\(ress-feedback\\|n-with-message\\)]")
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
544 (looking-at "(unwind-protect ")
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
545 (looking-at "(function (")
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
546 (looking-at "(cl-block-wrapper ")))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
547 (two (or (looking-at "(defun ") (looking-at "(defmacro ")))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
548 (let (or (looking-at "(let\\*? ") (looking-at "(while ")))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
549 (set (looking-at "(p?set[qf] ")))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
550 (if (or skip let
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
551 (progn
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
552 (forward-sexp)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
553 (and (>= (current-column) 78) (progn (backward-sexp) t))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
554 (let ((nl t))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
555 (forward-char 1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
556 (cl-do-prettyprint)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
557 (or skip (looking-at ")") (cl-do-prettyprint))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
558 (or (not two) (looking-at ")") (cl-do-prettyprint))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
559 (while (not (looking-at ")"))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
560 (if set (setq nl (not nl)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
561 (if nl (insert "\n"))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
562 (lisp-indent-line)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
563 (cl-do-prettyprint))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
564 (forward-char 1))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
565 (forward-sexp)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
566
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
567 (defvar cl-macroexpand-cmacs nil)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
568 (defvar cl-closure-vars nil)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
569
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
570 (defun cl-macroexpand-all (form &optional env)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
571 "Expand all macro calls through a Lisp FORM.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
572 This also does some trivial optimizations to make the form prettier."
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
573 (while (or (not (eq form (setq form (macroexpand form env))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
574 (and cl-macroexpand-cmacs
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
575 (not (eq form (setq form (compiler-macroexpand form)))))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
576 (cond ((not (consp form)) form)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
577 ((memq (car form) '(let let*))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
578 (if (null (nth 1 form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
579 (cl-macroexpand-all (cons 'progn (cddr form)) env)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
580 (let ((letf nil) (res nil) (lets (cadr form)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
581 (while lets
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
582 (push (if (consp (car lets))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
583 (let ((exp (cl-macroexpand-all (caar lets) env)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
584 (or (symbolp exp) (setq letf t))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
585 (cons exp (cl-macroexpand-body (cdar lets) env)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
586 (let ((exp (cl-macroexpand-all (car lets) env)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
587 (if (symbolp exp) exp
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
588 (setq letf t) (list exp nil)))) res)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
589 (setq lets (cdr lets)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
590 (list* (if letf (if (eq (car form) 'let) 'letf 'letf*) (car form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
591 (nreverse res) (cl-macroexpand-body (cddr form) env)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
592 ((eq (car form) 'cond)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
593 (cons (car form)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
594 (mapcar (function (lambda (x) (cl-macroexpand-body x env)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
595 (cdr form))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
596 ((eq (car form) 'condition-case)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
597 (list* (car form) (nth 1 form) (cl-macroexpand-all (nth 2 form) env)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
598 (mapcar (function
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
599 (lambda (x)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
600 (cons (car x) (cl-macroexpand-body (cdr x) env))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
601 (cdddr form))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
602 ((memq (car form) '(quote function))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
603 (if (eq (car-safe (nth 1 form)) 'lambda)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
604 (let ((body (cl-macroexpand-body (cddadr form) env)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
605 (if (and cl-closure-vars (eq (car form) 'function)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
606 (cl-expr-contains-any body cl-closure-vars))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
607 (let* ((new (mapcar 'gensym cl-closure-vars))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
608 (sub (pairlis cl-closure-vars new)) (decls nil))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
609 (while (or (stringp (car body))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
610 (eq (car-safe (car body)) 'interactive))
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
611 (push (list 'quote (pop body)) decls))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
612 (put (car (last cl-closure-vars)) 'used t)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
613 (append
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
614 (list 'list '(quote lambda) '(quote (&rest --cl-rest--)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
615 (sublis sub (nreverse decls))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
616 (list
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
617 (list* 'list '(quote apply)
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
618 ;; XEmacs: put a quote before the function
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
619 (list 'list '(quote quote)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
620 (list 'function
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
621 (list* 'lambda
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
622 (append new (cadadr form))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
623 (sublis sub body))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
624 (nconc (mapcar (function
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
625 (lambda (x)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
626 (list 'list '(quote quote) x)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
627 cl-closure-vars)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
628 '((quote --cl-rest--)))))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
629 (list (car form) (list* 'lambda (cadadr form) body))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
630 (let ((found (assq (cadr form) env)))
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
631 ;; XEmacs: cadr/caddr operate on nil without errors
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
632 (if (eq (cadr (caddr found)) 'cl-labels-args)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
633 (cl-macroexpand-all (cadr (caddr (cadddr found))) env)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
634 form))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
635 ((memq (car form) '(defun defmacro))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
636 (list* (car form) (nth 1 form) (cl-macroexpand-body (cddr form) env)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
637 ((and (eq (car form) 'progn) (not (cddr form)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
638 (cl-macroexpand-all (nth 1 form) env))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
639 ((eq (car form) 'setq)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
640 (let* ((args (cl-macroexpand-body (cdr form) env)) (p args))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
641 (while (and p (symbolp (car p))) (setq p (cddr p)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
642 (if p (cl-macroexpand-all (cons 'setf args)) (cons 'setq args))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
643 (t (cons (car form) (cl-macroexpand-body (cdr form) env)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
644
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
645 (defun cl-macroexpand-body (body &optional env)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
646 (mapcar (function (lambda (x) (cl-macroexpand-all x env))) body))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
647
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
648 (defun cl-prettyexpand (form &optional full)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
649 (message "Expanding...")
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
650 (let ((cl-macroexpand-cmacs full) (cl-compiling-file full)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
651 (byte-compile-macro-environment nil))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
652 (setq form (cl-macroexpand-all form
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
653 (and (not full) '((block) (eval-when)))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
654 (message "Formatting...")
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
655 (prog1 (cl-prettyprint form)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
656 (message ""))))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
657
5284
d27c1ee1943b Make the order of preloaded-file-list more sane.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5273
diff changeset
658 ;; XEmacs addition; force cl-macs to be available from here on when
d27c1ee1943b Make the order of preloaded-file-list more sane.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5273
diff changeset
659 ;; compiling files to be dumped. This is more reasonable than forcing other
d27c1ee1943b Make the order of preloaded-file-list more sane.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5273
diff changeset
660 ;; files to do the same, multiple times.
d27c1ee1943b Make the order of preloaded-file-list more sane.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5273
diff changeset
661 (eval-when-compile (or (cl-compiling-file) (load "cl-macs")))
d27c1ee1943b Make the order of preloaded-file-list more sane.
Aidan Kehoe <kehoea@parhasard.net>
parents: 5273
diff changeset
662
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
663 (run-hooks 'cl-extra-load-hook)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
664
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
665 ;; XEmacs addition
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
666 (provide 'cl-extra)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
667
2153
393039450288 [xemacs-hg @ 2004-06-26 21:25:23 by james]
james
parents: 1983
diff changeset
668 ;;; arch-tag: bcd03437-0871-43fb-a8f1-ad0e0b5427ed
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
669 ;;; cl-extra.el ends here