annotate lisp/cl-extra.el @ 243:f220cc83d72e r20-5b20

Import from CVS: tag r20-5b20
author cvs
date Mon, 13 Aug 2007 10:17:07 +0200
parents 0e522484dd2a
children 11cf20601dec
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1 ;;; cl-extra.el --- Common Lisp extensions for GNU Emacs Lisp (part two)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
2
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
3 ;; Copyright (C) 1993 Free Software Foundation, Inc.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
4
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
5 ;; Author: Dave Gillespie <daveg@synaptics.com>
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
6 ;; Maintainer: XEmacs Development Team
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
7 ;; Version: 2.02
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
8 ;; Keywords: extensions, dumped
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
9
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
10 ;; This file is part of XEmacs.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
11
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
12 ;; XEmacs is free software; you can redistribute it and/or modify it
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
13 ;; under the terms of the GNU General Public License as published by
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
14 ;; the Free Software Foundation; either version 2, or (at your option)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
15 ;; any later version.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
16
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
17 ;; XEmacs is distributed in the hope that it will be useful, but
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
18 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
20 ;; General Public License for more details.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
21
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
22 ;; You should have received a copy of the GNU General Public License
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
23 ;; along with XEmacs; see the file COPYING. If not, write to the Free
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
24 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
25 ;; 02111-1307, USA.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
26
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
27 ;;; Synched up with: FSF 19.34.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
28
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
29 ;;; Commentary:
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
30
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
31 ;; This file is dumped with XEmacs.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
32
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
33 ;; These are extensions to Emacs Lisp that provide a degree of
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
34 ;; Common Lisp compatibility, beyond what is already built-in
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
35 ;; in Emacs Lisp.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
36 ;;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
37 ;; This package was written by Dave Gillespie; it is a complete
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
38 ;; rewrite of Cesar Quiroz's original cl.el package of December 1986.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
39 ;;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
40 ;; This package works with Emacs 18, Emacs 19, and XEmacs/Lucid Emacs 19.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
41 ;;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
42 ;; Bug reports, comments, and suggestions are welcome!
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
43
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
44 ;; This file contains portions of the Common Lisp extensions
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
45 ;; package which are autoloaded since they are relatively obscure.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
46
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
47 ;; See cl.el for Change Log.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
48
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
49
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
50 ;;; Code:
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
51
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
52 (or (memq 'cl-19 features)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
53 (error "Tried to load `cl-extra' before `cl'!"))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
54
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
55
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
56 ;;; We define these here so that this file can compile without having
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
57 ;;; loaded the cl.el file already.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
58
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
59 (defmacro cl-push (x place) (list 'setq place (list 'cons x place)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
60 (defmacro cl-pop (place)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
61 (list 'car (list 'prog1 place (list 'setq place (list 'cdr place)))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
62
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
63 (defvar cl-emacs-type)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
64
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
65
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
66 ;;; Type coercion.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
67
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
68 (defun coerce (x type)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
69 "Coerce OBJECT to type TYPE.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
70 TYPE is a Common Lisp type specifier."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
71 (cond ((eq type 'list) (if (listp x) x (append x nil)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
72 ((eq type 'vector) (if (vectorp x) x (vconcat x)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
73 ((eq type 'string) (if (stringp x) x (concat x)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
74 ((eq type 'array) (if (arrayp x) x (vconcat x)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
75 ((and (eq type 'character) (stringp x) (= (length x) 1)) (aref x 0))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
76 ((and (eq type 'character) (symbolp x)) (coerce (symbol-name x) type))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
77 ((eq type 'float) (float x))
227
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
78 ((eq type 'bit-vector) (if (bit-vector-p x) x
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
79 (apply 'bit-vector (append x nil))))
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
80 ((eq type 'weak-list)
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
81 (if (weak-list-p x) x
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
82 (let ((wl (make-weak-list)))
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
83 (set-weak-list-list wl (if (listp x) x (append x nil)))
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
84 wl)))
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
85 ((typep x type) x)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
86 (t (error "Can't coerce %s to type %s" x type))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
87
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
88
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
89 ;;; Predicates.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
90
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
91 (defun equalp (x y)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
92 "T if two Lisp objects have similar structures and contents.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
93 This is like `equal', except that it accepts numerically equal
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
94 numbers of different types (float vs. integer), and also compares
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
95 strings case-insensitively."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
96 (cond ((eq x y) t)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
97 ((stringp x)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
98 (and (stringp y) (= (length x) (length y))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
99 (or (string-equal x y)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
100 (string-equal (downcase x) (downcase y))))) ; lazy but simple!
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
101 ((numberp x)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
102 (and (numberp y) (= x y)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
103 ((consp x)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
104 ;; XEmacs change
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
105 (while (and (consp x) (consp y) (equalp (cl-pop x) (cl-pop y))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
106 (and (not (consp x)) (equalp x y)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
107 ((vectorp x)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
108 (and (vectorp y) (= (length x) (length y))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
109 (let ((i (length x)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
110 (while (and (>= (setq i (1- i)) 0)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
111 (equalp (aref x i) (aref y i))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
112 (< i 0))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
113 (t (equal x y))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
114
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
115
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
116 ;;; Control structures.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
117
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
118 (defun cl-mapcar-many (cl-func cl-seqs)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
119 (if (cdr (cdr cl-seqs))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
120 (let* ((cl-res nil)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
121 (cl-n (apply 'min (mapcar 'length cl-seqs)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
122 (cl-i 0)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
123 (cl-args (copy-sequence cl-seqs))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
124 cl-p1 cl-p2)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
125 (setq cl-seqs (copy-sequence cl-seqs))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
126 (while (< cl-i cl-n)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
127 (setq cl-p1 cl-seqs cl-p2 cl-args)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
128 (while cl-p1
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
129 (setcar cl-p2
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
130 (if (consp (car cl-p1))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
131 (prog1 (car (car cl-p1))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
132 (setcar cl-p1 (cdr (car cl-p1))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
133 (aref (car cl-p1) cl-i)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
134 (setq cl-p1 (cdr cl-p1) cl-p2 (cdr cl-p2)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
135 (cl-push (apply cl-func cl-args) cl-res)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
136 (setq cl-i (1+ cl-i)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
137 (nreverse cl-res))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
138 (let ((cl-res nil)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
139 (cl-x (car cl-seqs))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
140 (cl-y (nth 1 cl-seqs)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
141 (let ((cl-n (min (length cl-x) (length cl-y)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
142 (cl-i -1))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
143 (while (< (setq cl-i (1+ cl-i)) cl-n)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
144 (cl-push (funcall cl-func
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
145 (if (consp cl-x) (cl-pop cl-x) (aref cl-x cl-i))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
146 (if (consp cl-y) (cl-pop cl-y) (aref cl-y cl-i)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
147 cl-res)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
148 (nreverse cl-res))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
149
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
150 (defun map (cl-type cl-func cl-seq &rest cl-rest)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
151 "Map a function across one or more sequences, returning a sequence.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
152 TYPE is the sequence type to return, FUNC is the function, and SEQS
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
153 are the argument sequences."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
154 (let ((cl-res (apply 'mapcar* cl-func cl-seq cl-rest)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
155 (and cl-type (coerce cl-res cl-type))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
156
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
157 (defun maplist (cl-func cl-list &rest cl-rest)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
158 "Map FUNC to each sublist of LIST or LISTS.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
159 Like `mapcar', except applies to lists and their cdr's rather than to
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
160 the elements themselves."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
161 (if cl-rest
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
162 (let ((cl-res nil)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
163 (cl-args (cons cl-list (copy-sequence cl-rest)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
164 cl-p)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
165 (while (not (memq nil cl-args))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
166 (cl-push (apply cl-func cl-args) cl-res)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
167 (setq cl-p cl-args)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
168 (while cl-p (setcar cl-p (cdr (cl-pop cl-p)) )))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
169 (nreverse cl-res))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
170 (let ((cl-res nil))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
171 (while cl-list
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
172 (cl-push (funcall cl-func cl-list) cl-res)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
173 (setq cl-list (cdr cl-list)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
174 (nreverse cl-res))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
175
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
176
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
177 ;; mapc is now in C, renamed from `mapc-internal'.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
178
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
179 ;(defun mapc (cl-func cl-seq &rest cl-rest)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
180 ; "Like `mapcar', but does not accumulate values returned by the function."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
181 ; (if cl-rest
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
182 ; (apply 'map nil cl-func cl-seq cl-rest)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
183 ; ;; XEmacs change: we call mapc-internal, which really doesn't
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
184 ; ;; accumulate any results.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
185 ; (mapc-internal cl-func cl-seq))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
186 ; cl-seq)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
187
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
188 (defun mapl (cl-func cl-list &rest cl-rest)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
189 "Like `maplist', but does not accumulate values returned by the function."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
190 (if cl-rest
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
191 (apply 'maplist cl-func cl-list cl-rest)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
192 (let ((cl-p cl-list))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
193 (while cl-p (funcall cl-func cl-p) (setq cl-p (cdr cl-p)))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
194 cl-list)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
195
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
196 (defun mapcan (cl-func cl-seq &rest cl-rest)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
197 "Like `mapcar', but nconc's together the values returned by the function."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
198 (apply 'nconc (apply 'mapcar* cl-func cl-seq cl-rest)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
199
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
200 (defun mapcon (cl-func cl-list &rest cl-rest)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
201 "Like `maplist', but nconc's together the values returned by the function."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
202 (apply 'nconc (apply 'maplist cl-func cl-list cl-rest)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
203
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
204 (defun some (cl-pred cl-seq &rest cl-rest)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
205 "Return true if PREDICATE is true of any element of SEQ or SEQs.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
206 If so, return the true (non-nil) value returned by PREDICATE."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
207 (if (or cl-rest (nlistp cl-seq))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
208 (catch 'cl-some
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
209 (apply 'map nil
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
210 (function (lambda (&rest cl-x)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
211 (let ((cl-res (apply cl-pred cl-x)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
212 (if cl-res (throw 'cl-some cl-res)))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
213 cl-seq cl-rest) nil)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
214 (let ((cl-x nil))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
215 (while (and cl-seq (not (setq cl-x (funcall cl-pred (cl-pop cl-seq))))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
216 cl-x)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
217
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
218 (defun every (cl-pred cl-seq &rest cl-rest)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
219 "Return true if PREDICATE is true of every element of SEQ or SEQs."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
220 (if (or cl-rest (nlistp cl-seq))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
221 (catch 'cl-every
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
222 (apply 'map nil
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
223 (function (lambda (&rest cl-x)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
224 (or (apply cl-pred cl-x) (throw 'cl-every nil))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
225 cl-seq cl-rest) t)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
226 (while (and cl-seq (funcall cl-pred (car cl-seq)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
227 (setq cl-seq (cdr cl-seq)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
228 (null cl-seq)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
229
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
230 (defun notany (cl-pred cl-seq &rest cl-rest)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
231 "Return true if PREDICATE is false of every element of SEQ or SEQs."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
232 (not (apply 'some cl-pred cl-seq cl-rest)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
233
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
234 (defun notevery (cl-pred cl-seq &rest cl-rest)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
235 "Return true if PREDICATE is false of some element of SEQ or SEQs."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
236 (not (apply 'every cl-pred cl-seq cl-rest)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
237
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
238 ;;; Support for `loop'.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
239 (defun cl-map-keymap (cl-func cl-map)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
240 (while (symbolp cl-map) (setq cl-map (symbol-function cl-map)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
241 (if (eq cl-emacs-type 'lucid) (funcall 'map-keymap cl-func cl-map)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
242 (if (listp cl-map)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
243 (let ((cl-p cl-map))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
244 (while (consp (setq cl-p (cdr cl-p)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
245 (cond ((consp (car cl-p))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
246 (funcall cl-func (car (car cl-p)) (cdr (car cl-p))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
247 ((vectorp (car cl-p))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
248 (cl-map-keymap cl-func (car cl-p)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
249 ((eq (car cl-p) 'keymap)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
250 (setq cl-p nil)))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
251 (let ((cl-i -1))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
252 (while (< (setq cl-i (1+ cl-i)) (length cl-map))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
253 (if (aref cl-map cl-i)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
254 (funcall cl-func cl-i (aref cl-map cl-i))))))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
255
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
256 (defun cl-map-keymap-recursively (cl-func-rec cl-map &optional cl-base)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
257 (or cl-base
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
258 (setq cl-base (copy-sequence (if (eq cl-emacs-type 18) "0" [0]))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
259 (cl-map-keymap
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
260 (function
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
261 (lambda (cl-key cl-bind)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
262 (aset cl-base (1- (length cl-base)) cl-key)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
263 (if (keymapp cl-bind)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
264 (cl-map-keymap-recursively
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
265 cl-func-rec cl-bind
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
266 (funcall (if (eq cl-emacs-type 18) 'concat 'vconcat)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
267 cl-base (list 0)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
268 (funcall cl-func-rec cl-base cl-bind))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
269 cl-map))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
270
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
271 (defun cl-map-intervals (cl-func &optional cl-what cl-prop cl-start cl-end)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
272 (or cl-what (setq cl-what (current-buffer)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
273 (if (bufferp cl-what)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
274 (let (cl-mark cl-mark2 (cl-next t) cl-next2)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
275 (save-excursion
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
276 (set-buffer cl-what)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
277 (setq cl-mark (copy-marker (or cl-start (point-min))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
278 (setq cl-mark2 (and cl-end (copy-marker cl-end))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
279 (while (and cl-next (or (not cl-mark2) (< cl-mark cl-mark2)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
280 (setq cl-next (and (fboundp 'next-property-change)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
281 (if cl-prop (next-single-property-change
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
282 cl-mark cl-prop cl-what)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
283 (next-property-change cl-mark cl-what)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
284 cl-next2 (or cl-next (save-excursion
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
285 (set-buffer cl-what) (point-max))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
286 (funcall cl-func (prog1 (marker-position cl-mark)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
287 (set-marker cl-mark cl-next2))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
288 (if cl-mark2 (min cl-next2 cl-mark2) cl-next2)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
289 (set-marker cl-mark nil) (if cl-mark2 (set-marker cl-mark2 nil)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
290 (or cl-start (setq cl-start 0))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
291 (or cl-end (setq cl-end (length cl-what)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
292 (while (< cl-start cl-end)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
293 (let ((cl-next (or (and (fboundp 'next-property-change)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
294 (if cl-prop (next-single-property-change
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
295 cl-start cl-prop cl-what)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
296 (next-property-change cl-start cl-what)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
297 cl-end)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
298 (funcall cl-func cl-start (min cl-next cl-end))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
299 (setq cl-start cl-next)))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
300
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
301 (defun cl-map-overlays (cl-func &optional cl-buffer cl-start cl-end cl-arg)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
302 (or cl-buffer (setq cl-buffer (current-buffer)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
303 (if (fboundp 'overlay-lists)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
304
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
305 ;; This is the preferred algorithm, though overlay-lists is undocumented.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
306 (let (cl-ovl)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
307 (save-excursion
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
308 (set-buffer cl-buffer)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
309 (setq cl-ovl (overlay-lists))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
310 (if cl-start (setq cl-start (copy-marker cl-start)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
311 (if cl-end (setq cl-end (copy-marker cl-end))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
312 (setq cl-ovl (nconc (car cl-ovl) (cdr cl-ovl)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
313 (while (and cl-ovl
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
314 (or (not (overlay-start (car cl-ovl)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
315 (and cl-end (>= (overlay-start (car cl-ovl)) cl-end))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
316 (and cl-start (<= (overlay-end (car cl-ovl)) cl-start))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
317 (not (funcall cl-func (car cl-ovl) cl-arg))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
318 (setq cl-ovl (cdr cl-ovl)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
319 (if cl-start (set-marker cl-start nil))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
320 (if cl-end (set-marker cl-end nil)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
321
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
322 ;; This alternate algorithm fails to find zero-length overlays.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
323 (let ((cl-mark (save-excursion (set-buffer cl-buffer)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
324 (copy-marker (or cl-start (point-min)))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
325 (cl-mark2 (and cl-end (save-excursion (set-buffer cl-buffer)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
326 (copy-marker cl-end))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
327 cl-pos cl-ovl)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
328 (while (save-excursion
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
329 (and (setq cl-pos (marker-position cl-mark))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
330 (< cl-pos (or cl-mark2 (point-max)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
331 (progn
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
332 (set-buffer cl-buffer)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
333 (setq cl-ovl (overlays-at cl-pos))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
334 (set-marker cl-mark (next-overlay-change cl-pos)))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
335 (while (and cl-ovl
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
336 (or (/= (overlay-start (car cl-ovl)) cl-pos)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
337 (not (and (funcall cl-func (car cl-ovl) cl-arg)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
338 (set-marker cl-mark nil)))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
339 (setq cl-ovl (cdr cl-ovl))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
340 (set-marker cl-mark nil) (if cl-mark2 (set-marker cl-mark2 nil)))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
341
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
342 ;;; Support for `setf'.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
343 (defun cl-set-frame-visible-p (frame val)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
344 (cond ((null val) (make-frame-invisible frame))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
345 ((eq val 'icon) (iconify-frame frame))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
346 (t (make-frame-visible frame)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
347 val)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
348
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
349 ;;; Support for `progv'.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
350 (defvar cl-progv-save)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
351 (defun cl-progv-before (syms values)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
352 (while syms
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
353 (cl-push (if (boundp (car syms))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
354 (cons (car syms) (symbol-value (car syms)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
355 (car syms)) cl-progv-save)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
356 (if values
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
357 (set (cl-pop syms) (cl-pop values))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
358 (makunbound (cl-pop syms)))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
359
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
360 (defun cl-progv-after ()
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
361 (while cl-progv-save
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
362 (if (consp (car cl-progv-save))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
363 (set (car (car cl-progv-save)) (cdr (car cl-progv-save)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
364 (makunbound (car cl-progv-save)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
365 (cl-pop cl-progv-save)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
366
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
367
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
368 ;;; Numbers.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
369
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
370 (defun gcd (&rest args)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
371 "Return the greatest common divisor of the arguments."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
372 (let ((a (abs (or (cl-pop args) 0))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
373 (while args
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
374 (let ((b (abs (cl-pop args))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
375 (while (> b 0) (setq b (% a (setq a b))))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
376 a))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
377
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
378 (defun lcm (&rest args)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
379 "Return the least common multiple of the arguments."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
380 (if (memq 0 args)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
381 0
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
382 (let ((a (abs (or (cl-pop args) 1))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
383 (while args
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
384 (let ((b (abs (cl-pop args))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
385 (setq a (* (/ a (gcd a b)) b))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
386 a)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
387
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
388 (defun isqrt (a)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
389 "Return the integer square root of the argument."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
390 (if (and (integerp a) (> a 0))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
391 ;; XEmacs change
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
392 (let ((g (cond ((>= a 1000000) 10000) ((>= a 10000) 1000)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
393 ((>= a 100) 100) (t 10)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
394 g2)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
395 (while (< (setq g2 (/ (+ g (/ a g)) 2)) g)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
396 (setq g g2))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
397 g)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
398 (if (eq a 0) 0 (signal 'arith-error nil))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
399
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
400 (defun cl-expt (x y)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
401 "Return X raised to the power of Y. Works only for integer arguments."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
402 (if (<= y 0) (if (= y 0) 1 (if (memq x '(-1 1)) (cl-expt x (- y)) 0))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
403 (* (if (= (% y 2) 0) 1 x) (cl-expt (* x x) (/ y 2)))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
404 (or (and (fboundp 'expt) (subrp (symbol-function 'expt)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
405 (defalias 'expt 'cl-expt))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
406
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
407 (defun floor* (x &optional y)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
408 "Return a list of the floor of X and the fractional part of X.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
409 With two arguments, return floor and remainder of their quotient."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
410 (let ((q (floor x y)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
411 (list q (- x (if y (* y q) q)))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
412
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
413 (defun ceiling* (x &optional y)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
414 "Return a list of the ceiling of X and the fractional part of X.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
415 With two arguments, return ceiling and remainder of their quotient."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
416 (let ((res (floor* x y)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
417 (if (= (car (cdr res)) 0) res
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
418 (list (1+ (car res)) (- (car (cdr res)) (or y 1))))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
419
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
420 (defun truncate* (x &optional y)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
421 "Return a list of the integer part of X and the fractional part of X.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
422 With two arguments, return truncation and remainder of their quotient."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
423 (if (eq (>= x 0) (or (null y) (>= y 0)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
424 (floor* x y) (ceiling* x y)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
425
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
426 (defun round* (x &optional y)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
427 "Return a list of X rounded to the nearest integer and the remainder.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
428 With two arguments, return rounding and remainder of their quotient."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
429 (if y
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
430 (if (and (integerp x) (integerp y))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
431 (let* ((hy (/ y 2))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
432 (res (floor* (+ x hy) y)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
433 (if (and (= (car (cdr res)) 0)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
434 (= (+ hy hy) y)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
435 (/= (% (car res) 2) 0))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
436 (list (1- (car res)) hy)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
437 (list (car res) (- (car (cdr res)) hy))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
438 (let ((q (round (/ x y))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
439 (list q (- x (* q y)))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
440 (if (integerp x) (list x 0)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
441 (let ((q (round x)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
442 (list q (- x q))))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
443
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
444 (defun mod* (x y)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
445 "The remainder of X divided by Y, with the same sign as Y."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
446 (nth 1 (floor* x y)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
447
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
448 (defun rem* (x y)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
449 "The remainder of X divided by Y, with the same sign as X."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
450 (nth 1 (truncate* x y)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
451
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
452 (defun signum (a)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
453 "Return 1 if A is positive, -1 if negative, 0 if zero."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
454 (cond ((> a 0) 1) ((< a 0) -1) (t 0)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
455
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
456
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
457 ;; Random numbers.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
458
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
459 (defvar *random-state*)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
460 (defun random* (lim &optional state)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
461 "Return a random nonnegative number less than LIM, an integer or float.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
462 Optional second arg STATE is a random-state object."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
463 (or state (setq state *random-state*))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
464 ;; Inspired by "ran3" from Numerical Recipes. Additive congruential method.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
465 (let ((vec (aref state 3)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
466 (if (integerp vec)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
467 (let ((i 0) (j (- 1357335 (% (abs vec) 1357333))) (k 1) ii)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
468 (aset state 3 (setq vec (make-vector 55 nil)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
469 (aset vec 0 j)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
470 (while (> (setq i (% (+ i 21) 55)) 0)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
471 (aset vec i (setq j (prog1 k (setq k (- j k))))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
472 (while (< (setq i (1+ i)) 200) (random* 2 state))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
473 (let* ((i (aset state 1 (% (1+ (aref state 1)) 55)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
474 (j (aset state 2 (% (1+ (aref state 2)) 55)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
475 (n (logand 8388607 (aset vec i (- (aref vec i) (aref vec j))))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
476 (if (integerp lim)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
477 (if (<= lim 512) (% n lim)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
478 (if (> lim 8388607) (setq n (+ (lsh n 9) (random* 512 state))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
479 (let ((mask 1023))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
480 (while (< mask (1- lim)) (setq mask (1+ (+ mask mask))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
481 (if (< (setq n (logand n mask)) lim) n (random* lim state))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
482 (* (/ n '8388608e0) lim)))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
483
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
484 (defun make-random-state (&optional state)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
485 "Return a copy of random-state STATE, or of `*random-state*' if omitted.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
486 If STATE is t, return a new state object seeded from the time of day."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
487 (cond ((null state) (make-random-state *random-state*))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
488 ((vectorp state) (cl-copy-tree state t))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
489 ((integerp state) (vector 'cl-random-state-tag -1 30 state))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
490 (t (make-random-state (cl-random-time)))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
491
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
492 (defun random-state-p (object)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
493 "Return t if OBJECT is a random-state object."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
494 (and (vectorp object) (= (length object) 4)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
495 (eq (aref object 0) 'cl-random-state-tag)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
496
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
497
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
498 ;; Implementation limits.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
499
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
500 (defun cl-finite-do (func a b)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
501 (condition-case err
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
502 (let ((res (funcall func a b))) ; check for IEEE infinity
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
503 (and (numberp res) (/= res (/ res 2)) res))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
504 (arith-error nil)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
505
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
506 (defvar most-positive-float)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
507 (defvar most-negative-float)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
508 (defvar least-positive-float)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
509 (defvar least-negative-float)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
510 (defvar least-positive-normalized-float)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
511 (defvar least-negative-normalized-float)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
512 (defvar float-epsilon)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
513 (defvar float-negative-epsilon)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
514
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
515 (defun cl-float-limits ()
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
516 (or most-positive-float (not (numberp '2e1))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
517 (let ((x '2e0) y z)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
518 ;; Find maximum exponent (first two loops are optimizations)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
519 (while (cl-finite-do '* x x) (setq x (* x x)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
520 (while (cl-finite-do '* x (/ x 2)) (setq x (* x (/ x 2))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
521 (while (cl-finite-do '+ x x) (setq x (+ x x)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
522 (setq z x y (/ x 2))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
523 ;; Now fill in 1's in the mantissa.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
524 (while (and (cl-finite-do '+ x y) (/= (+ x y) x))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
525 (setq x (+ x y) y (/ y 2)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
526 (setq most-positive-float x
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
527 most-negative-float (- x))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
528 ;; Divide down until mantissa starts rounding.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
529 (setq x (/ x z) y (/ 16 z) x (* x y))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
530 (while (condition-case err (and (= x (* (/ x 2) 2)) (> (/ y 2) 0))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
531 (arith-error nil))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
532 (setq x (/ x 2) y (/ y 2)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
533 (setq least-positive-normalized-float y
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
534 least-negative-normalized-float (- y))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
535 ;; Divide down until value underflows to zero.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
536 (setq x (/ 1 z) y x)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
537 (while (condition-case err (> (/ x 2) 0) (arith-error nil))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
538 (setq x (/ x 2)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
539 (setq least-positive-float x
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
540 least-negative-float (- x))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
541 (setq x '1e0)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
542 (while (/= (+ '1e0 x) '1e0) (setq x (/ x 2)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
543 (setq float-epsilon (* x 2))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
544 (setq x '1e0)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
545 (while (/= (- '1e0 x) '1e0) (setq x (/ x 2)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
546 (setq float-negative-epsilon (* x 2))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
547 nil)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
548
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
549
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
550 ;;; Sequence functions.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
551
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
552 ;XEmacs -- our built-in is more powerful.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
553 ;(defun subseq (seq start &optional end)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
554 ; "Return the subsequence of SEQ from START to END.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
555 ;If END is omitted, it defaults to the length of the sequence.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
556 ;If START or END is negative, it counts from the end."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
557 ; (if (stringp seq) (substring seq start end)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
558 ; (let (len)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
559 ; (and end (< end 0) (setq end (+ end (setq len (length seq)))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
560 ; (if (< start 0) (setq start (+ start (or len (setq len (length seq))))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
561 ; (cond ((listp seq)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
562 ; (if (> start 0) (setq seq (nthcdr start seq)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
563 ; (if end
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
564 ; (let ((res nil))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
565 ; (while (>= (setq end (1- end)) start)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
566 ; (cl-push (cl-pop seq) res))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
567 ; (nreverse res))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
568 ; (copy-sequence seq)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
569 ; (t
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
570 ; (or end (setq end (or len (length seq))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
571 ; (let ((res (make-vector (max (- end start) 0) nil))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
572 ; (i 0))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
573 ; (while (< start end)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
574 ; (aset res i (aref seq start))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
575 ; (setq i (1+ i) start (1+ start)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
576 ; res))))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
577
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
578 (defun concatenate (type &rest seqs)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
579 "Concatenate, into a sequence of type TYPE, the argument SEQUENCES."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
580 (cond ((eq type 'vector) (apply 'vconcat seqs))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
581 ((eq type 'string) (apply 'concat seqs))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
582 ((eq type 'list) (apply 'append (append seqs '(nil))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
583 (t (error "Not a sequence type name: %s" type))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
584
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
585
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
586 ;;; List functions.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
587
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
588 (defun revappend (x y)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
589 "Equivalent to (append (reverse X) Y)."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
590 (nconc (reverse x) y))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
591
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
592 (defun nreconc (x y)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
593 "Equivalent to (nconc (nreverse X) Y)."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
594 (nconc (nreverse x) y))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
595
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
596 (defun list-length (x)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
597 "Return the length of a list. Return nil if list is circular."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
598 (let ((n 0) (fast x) (slow x))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
599 (while (and (cdr fast) (not (and (eq fast slow) (> n 0))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
600 (setq n (+ n 2) fast (cdr (cdr fast)) slow (cdr slow)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
601 (if fast (if (cdr fast) nil (1+ n)) n)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
602
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
603 (defun tailp (sublist list)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
604 "Return true if SUBLIST is a tail of LIST."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
605 (while (and (consp list) (not (eq sublist list)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
606 (setq list (cdr list)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
607 (if (numberp sublist) (equal sublist list) (eq sublist list)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
608
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
609 (defun cl-copy-tree (tree &optional vecp)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
610 "Make a copy of TREE.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
611 If TREE is a cons cell, this recursively copies both its car and its cdr.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
612 Contrast to copy-sequence, which copies only along the cdrs. With second
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
613 argument VECP, this copies vectors as well as conses."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
614 (if (consp tree)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
615 (let ((p (setq tree (copy-list tree))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
616 (while (consp p)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
617 (if (or (consp (car p)) (and vecp (vectorp (car p))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
618 (setcar p (cl-copy-tree (car p) vecp)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
619 (or (listp (cdr p)) (setcdr p (cl-copy-tree (cdr p) vecp)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
620 (cl-pop p)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
621 (if (and vecp (vectorp tree))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
622 (let ((i (length (setq tree (copy-sequence tree)))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
623 (while (>= (setq i (1- i)) 0)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
624 (aset tree i (cl-copy-tree (aref tree i) vecp))))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
625 tree)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
626 (or (and (fboundp 'copy-tree) (subrp (symbol-function 'copy-tree)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
627 (defalias 'copy-tree 'cl-copy-tree))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
628
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
629
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
630 ;;; Property lists.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
631
227
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
632 ;; XEmacs: our `get' groks DEFAULT.
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
633 (defalias 'get* 'get)
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
634
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
635 (defun getf (plist tag &optional def)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
636 "Search PROPLIST for property PROPNAME; return its value or DEFAULT.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
637 PROPLIST is a list of the sort returned by `symbol-plist'."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
638 (setplist '--cl-getf-symbol-- plist)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
639 (or (get '--cl-getf-symbol-- tag)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
640 (and def (get* '--cl-getf-symbol-- tag def))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
641
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
642 (defun cl-set-getf (plist tag val)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
643 (let ((p plist))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
644 (while (and p (not (eq (car p) tag))) (setq p (cdr (cdr p))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
645 (if p (progn (setcar (cdr p) val) plist) (list* tag val plist))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
646
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
647 (defun cl-do-remf (plist tag)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
648 (let ((p (cdr plist)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
649 (while (and (cdr p) (not (eq (car (cdr p)) tag))) (setq p (cdr (cdr p))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
650 (and (cdr p) (progn (setcdr p (cdr (cdr (cdr p)))) t))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
651
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
652 (defun cl-remprop (sym tag)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
653 "Remove from SYMBOL's plist the property PROP and its value."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
654 (let ((plist (symbol-plist sym)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
655 (if (and plist (eq tag (car plist)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
656 (progn (setplist sym (cdr (cdr plist))) t)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
657 (cl-do-remf plist tag))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
658 (or (and (fboundp 'remprop) (subrp (symbol-function 'remprop)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
659 (defalias 'remprop 'cl-remprop))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
660
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
661
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
662
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
663 ;;; Hash tables.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
664
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
665 (defun make-hash-table (&rest cl-keys)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
666 "Make an empty Common Lisp-style hash-table.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
667 If :test is `eq', `eql', or `equal', this can use XEmacs built-in hash-tables.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
668 In Emacs 19, or with a different test, this internally uses a-lists.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
669 Keywords supported: :test :size
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
670 The Common Lisp keywords :rehash-size and :rehash-threshold are ignored."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
671 (let ((cl-test (or (car (cdr (memq ':test cl-keys))) 'eql))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
672 (cl-size (or (car (cdr (memq ':size cl-keys))) 20)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
673 ;; XEmacs change
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
674 (if (and (memq cl-test '(eq eql equal)) (fboundp 'make-hashtable))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
675 (funcall 'make-hashtable cl-size cl-test)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
676 (list 'cl-hash-table-tag cl-test
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
677 (if (> cl-size 1) (make-vector cl-size 0)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
678 (let ((sym (make-symbol "--hashsym--"))) (set sym nil) sym))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
679 0))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
680
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
681 (defvar cl-lucid-hash-tag
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
682 (if (and (fboundp 'make-hashtable) (vectorp (make-hashtable 1)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
683 (aref (make-hashtable 1) 0) (make-symbol "--cl-hash-tag--")))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
684
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
685 (defun hash-table-p (x)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
686 "Return t if OBJECT is a hash table."
227
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
687 (or (and (fboundp 'hashtablep) (funcall 'hashtablep x))
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
688 (eq (car-safe x) 'cl-hash-table-tag)
0e522484dd2a Import from CVS: tag r20-5b12
cvs
parents: 209
diff changeset
689 (and (vectorp x) (= (length x) 4) (eq (aref x 0) cl-lucid-hash-tag))))
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
690
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
691 (defun cl-not-hash-table (x &optional y &rest z)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
692 (signal 'wrong-type-argument (list 'hash-table-p (or y x))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
693
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
694 (defun cl-hash-lookup (key table)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
695 (or (eq (car-safe table) 'cl-hash-table-tag) (cl-not-hash-table table))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
696 (let* ((array (nth 2 table)) (test (car (cdr table))) (str key) sym)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
697 (if (symbolp array) (setq str nil sym (symbol-value array))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
698 (while (or (consp str) (and (vectorp str) (> (length str) 0)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
699 (setq str (elt str 0)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
700 (cond ((stringp str) (if (eq test 'equalp) (setq str (downcase str))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
701 ((symbolp str) (setq str (symbol-name str)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
702 ((and (numberp str) (> str -8000000) (< str 8000000))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
703 (or (integerp str) (setq str (truncate str)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
704 (setq str (aref ["0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "10"
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
705 "11" "12" "13" "14" "15"] (logand str 15))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
706 (t (setq str "*")))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
707 (setq sym (symbol-value (intern-soft str array))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
708 (list (and sym (cond ((or (eq test 'eq)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
709 (and (eq test 'eql) (not (numberp key))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
710 (assq key sym))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
711 ((memq test '(eql equal)) (assoc key sym))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
712 (t (assoc* key sym ':test test))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
713 sym str)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
714
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
715 (defvar cl-builtin-gethash
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
716 (if (and (fboundp 'gethash) (subrp (symbol-function 'gethash)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
717 (symbol-function 'gethash) 'cl-not-hash-table))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
718 (defvar cl-builtin-remhash
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
719 (if (and (fboundp 'remhash) (subrp (symbol-function 'remhash)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
720 (symbol-function 'remhash) 'cl-not-hash-table))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
721 (defvar cl-builtin-clrhash
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
722 (if (and (fboundp 'clrhash) (subrp (symbol-function 'clrhash)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
723 (symbol-function 'clrhash) 'cl-not-hash-table))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
724 (defvar cl-builtin-maphash
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
725 (if (and (fboundp 'maphash) (subrp (symbol-function 'maphash)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
726 (symbol-function 'maphash) 'cl-not-hash-table))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
727
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
728 (defun cl-gethash (key table &optional def)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
729 "Look up KEY in HASH-TABLE; return corresponding value, or DEFAULT."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
730 (if (consp table)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
731 (let ((found (cl-hash-lookup key table)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
732 (if (car found) (cdr (car found)) def))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
733 (funcall cl-builtin-gethash key table def)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
734 (defalias 'gethash 'cl-gethash)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
735
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
736 (defun cl-puthash (key val table)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
737 (if (consp table)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
738 (let ((found (cl-hash-lookup key table)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
739 (if (car found) (setcdr (car found) val)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
740 (if (nth 2 found)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
741 (progn
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
742 (if (> (nth 3 table) (* (length (nth 2 table)) 3))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
743 (let ((new-table (make-vector (nth 3 table) 0)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
744 (mapatoms (function
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
745 (lambda (sym)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
746 (set (intern (symbol-name sym) new-table)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
747 (symbol-value sym))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
748 (nth 2 table))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
749 (setcar (cdr (cdr table)) new-table)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
750 (set (intern (nth 2 found) (nth 2 table))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
751 (cons (cons key val) (nth 1 found))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
752 (set (nth 2 table) (cons (cons key val) (nth 1 found))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
753 (setcar (cdr (cdr (cdr table))) (1+ (nth 3 table)))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
754 (funcall 'puthash key val table)) val)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
755
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
756 (defun cl-remhash (key table)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
757 "Remove KEY from HASH-TABLE."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
758 (if (consp table)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
759 (let ((found (cl-hash-lookup key table)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
760 (and (car found)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
761 (let ((del (delq (car found) (nth 1 found))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
762 (setcar (cdr (cdr (cdr table))) (1- (nth 3 table)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
763 (if (nth 2 found) (set (intern (nth 2 found) (nth 2 table)) del)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
764 (set (nth 2 table) del)) t)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
765 (prog1 (not (eq (funcall cl-builtin-gethash key table '--cl--) '--cl--))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
766 (funcall cl-builtin-remhash key table))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
767 (defalias 'remhash 'cl-remhash)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
768
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
769 (defun cl-clrhash (table)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
770 "Clear HASH-TABLE."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
771 (if (consp table)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
772 (progn
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
773 (or (hash-table-p table) (cl-not-hash-table table))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
774 (if (symbolp (nth 2 table)) (set (nth 2 table) nil)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
775 (setcar (cdr (cdr table)) (make-vector (length (nth 2 table)) 0)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
776 (setcar (cdr (cdr (cdr table))) 0))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
777 (funcall cl-builtin-clrhash table))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
778 nil)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
779 (defalias 'clrhash 'cl-clrhash)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
780
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
781 (defun cl-maphash (cl-func cl-table)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
782 "Call FUNCTION on keys and values from HASH-TABLE."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
783 (or (hash-table-p cl-table) (cl-not-hash-table cl-table))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
784 (if (consp cl-table)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
785 (mapatoms (function (lambda (cl-x)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
786 (setq cl-x (symbol-value cl-x))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
787 (while cl-x
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
788 (funcall cl-func (car (car cl-x))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
789 (cdr (car cl-x)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
790 (setq cl-x (cdr cl-x)))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
791 (if (symbolp (nth 2 cl-table))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
792 (vector (nth 2 cl-table)) (nth 2 cl-table)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
793 (funcall cl-builtin-maphash cl-func cl-table)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
794 (defalias 'maphash 'cl-maphash)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
795
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
796 (defun hash-table-count (table)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
797 "Return the number of entries in HASH-TABLE."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
798 (or (hash-table-p table) (cl-not-hash-table table))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
799 (if (consp table) (nth 3 table) (funcall 'hashtable-fullness table)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
800
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
801
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
802 ;;; Some debugging aids.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
803
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
804 (defun cl-prettyprint (form)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
805 "Insert a pretty-printed rendition of a Lisp FORM in current buffer."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
806 (let ((pt (point)) last)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
807 (insert "\n" (prin1-to-string form) "\n")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
808 (setq last (point))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
809 (goto-char (1+ pt))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
810 (while (search-forward "(quote " last t)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
811 (delete-backward-char 7)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
812 (insert "'")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
813 (forward-sexp)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
814 (delete-char 1))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
815 (goto-char (1+ pt))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
816 (cl-do-prettyprint)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
817
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
818 (defun cl-do-prettyprint ()
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
819 (skip-chars-forward " ")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
820 (if (looking-at "(")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
821 (let ((skip (or (looking-at "((") (looking-at "(prog")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
822 (looking-at "(unwind-protect ")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
823 (looking-at "(function (")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
824 (looking-at "(cl-block-wrapper ")))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
825 (two (or (looking-at "(defun ") (looking-at "(defmacro ")))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
826 (let (or (looking-at "(let\\*? ") (looking-at "(while ")))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
827 (set (looking-at "(p?set[qf] ")))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
828 (if (or skip let
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
829 (progn
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
830 (forward-sexp)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
831 (and (>= (current-column) 78) (progn (backward-sexp) t))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
832 (let ((nl t))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
833 (forward-char 1)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
834 (cl-do-prettyprint)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
835 (or skip (looking-at ")") (cl-do-prettyprint))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
836 (or (not two) (looking-at ")") (cl-do-prettyprint))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
837 (while (not (looking-at ")"))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
838 (if set (setq nl (not nl)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
839 (if nl (insert "\n"))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
840 (lisp-indent-line)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
841 (cl-do-prettyprint))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
842 (forward-char 1))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
843 (forward-sexp)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
844
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
845 (defvar cl-macroexpand-cmacs nil)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
846 (defvar cl-closure-vars nil)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
847
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
848 (defun cl-macroexpand-all (form &optional env)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
849 "Expand all macro calls through a Lisp FORM.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
850 This also does some trivial optimizations to make the form prettier."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
851 (while (or (not (eq form (setq form (macroexpand form env))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
852 (and cl-macroexpand-cmacs
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
853 (not (eq form (setq form (compiler-macroexpand form)))))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
854 (cond ((not (consp form)) form)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
855 ((memq (car form) '(let let*))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
856 (if (null (nth 1 form))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
857 (cl-macroexpand-all (cons 'progn (cddr form)) env)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
858 (let ((letf nil) (res nil) (lets (cadr form)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
859 (while lets
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
860 (cl-push (if (consp (car lets))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
861 (let ((exp (cl-macroexpand-all (caar lets) env)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
862 (or (symbolp exp) (setq letf t))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
863 (cons exp (cl-macroexpand-body (cdar lets) env)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
864 (let ((exp (cl-macroexpand-all (car lets) env)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
865 (if (symbolp exp) exp
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
866 (setq letf t) (list exp nil)))) res)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
867 (setq lets (cdr lets)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
868 (list* (if letf (if (eq (car form) 'let) 'letf 'letf*) (car form))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
869 (nreverse res) (cl-macroexpand-body (cddr form) env)))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
870 ((eq (car form) 'cond)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
871 (cons (car form)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
872 (mapcar (function (lambda (x) (cl-macroexpand-body x env)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
873 (cdr form))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
874 ((eq (car form) 'condition-case)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
875 (list* (car form) (nth 1 form) (cl-macroexpand-all (nth 2 form) env)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
876 (mapcar (function
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
877 (lambda (x)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
878 (cons (car x) (cl-macroexpand-body (cdr x) env))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
879 (cdddr form))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
880 ((memq (car form) '(quote function))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
881 (if (eq (car-safe (nth 1 form)) 'lambda)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
882 (let ((body (cl-macroexpand-body (cddadr form) env)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
883 (if (and cl-closure-vars (eq (car form) 'function)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
884 (cl-expr-contains-any body cl-closure-vars))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
885 (let* ((new (mapcar 'gensym cl-closure-vars))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
886 (sub (pairlis cl-closure-vars new)) (decls nil))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
887 (while (or (stringp (car body))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
888 (eq (car-safe (car body)) 'interactive))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
889 (cl-push (list 'quote (cl-pop body)) decls))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
890 (put (car (last cl-closure-vars)) 'used t)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
891 (append
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
892 (list 'list '(quote lambda) '(quote (&rest --cl-rest--)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
893 (sublis sub (nreverse decls))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
894 (list
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
895 (list* 'list '(quote apply)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
896 (list 'list '(quote quote)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
897 (list 'function
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
898 (list* 'lambda
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
899 (append new (cadadr form))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
900 (sublis sub body))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
901 (nconc (mapcar (function
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
902 (lambda (x)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
903 (list 'list '(quote quote) x)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
904 cl-closure-vars)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
905 '((quote --cl-rest--)))))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
906 (list (car form) (list* 'lambda (cadadr form) body))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
907 (let ((found (assq (cadr form) env)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
908 (if (eq (cadr (caddr found)) 'cl-labels-args)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
909 (cl-macroexpand-all (cadr (caddr (cadddr found))) env)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
910 form))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
911 ((memq (car form) '(defun defmacro))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
912 (list* (car form) (nth 1 form) (cl-macroexpand-body (cddr form) env)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
913 ((and (eq (car form) 'progn) (not (cddr form)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
914 (cl-macroexpand-all (nth 1 form) env))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
915 ((eq (car form) 'setq)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
916 (let* ((args (cl-macroexpand-body (cdr form) env)) (p args))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
917 (while (and p (symbolp (car p))) (setq p (cddr p)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
918 (if p (cl-macroexpand-all (cons 'setf args)) (cons 'setq args))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
919 (t (cons (car form) (cl-macroexpand-body (cdr form) env)))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
920
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
921 (defun cl-macroexpand-body (body &optional env)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
922 (mapcar (function (lambda (x) (cl-macroexpand-all x env))) body))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
923
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
924 (defun cl-prettyexpand (form &optional full)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
925 (message "Expanding...")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
926 (let ((cl-macroexpand-cmacs full) (cl-compiling-file full)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
927 (byte-compile-macro-environment nil))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
928 (setq form (cl-macroexpand-all form
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
929 (and (not full) '((block) (eval-when)))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
930 (message "Formatting...")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
931 (prog1 (cl-prettyprint form)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
932 (message ""))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
933
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
934
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
935
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
936 (run-hooks 'cl-extra-load-hook)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
937
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
938 (provide 'cl-extra)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
939
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
940 ;;; cl-extra.el ends here