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