annotate lisp/cl-extra.el @ 395:de2c2a7459d2

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