annotate lisp/cl/cl-extra.el @ 192:9d35321dd38c

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