Mercurial > emacs
annotate lisp/emacs-lisp/cl-extra.el @ 64684:eb2cbda455c6
Require CL also when compiling.
author | Stefan Monnier <monnier@iro.umontreal.ca> |
---|---|
date | Fri, 29 Jul 2005 20:51:34 +0000 |
parents | 18a818a2ee7c |
children | 5b1a238fcbb4 edf295560b5a |
rev | line source |
---|---|
16057
a74507d555ba
Turn on byte-compile-dynamic.
Richard M. Stallman <rms@gnu.org>
parents:
15029
diff
changeset
|
1 ;;; cl-extra.el --- Common Lisp features, part 2 -*-byte-compile-dynamic: t;-*- |
4355 | 2 |
64684
eb2cbda455c6
Require CL also when compiling.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
64085
diff
changeset
|
3 ;; Copyright (C) 1993, 2000, 2003, 2005 Free Software Foundation, Inc. |
4355 | 4 |
5 ;; Author: Dave Gillespie <daveg@synaptics.com> | |
6 ;; Keywords: extensions | |
7 | |
8 ;; This file is part of GNU Emacs. | |
9 | |
10 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
11 ;; it under the terms of the GNU General Public License as published by | |
12244 | 12 ;; the Free Software Foundation; either version 2, or (at your option) |
4355 | 13 ;; any later version. |
14 | |
15 ;; GNU Emacs is distributed in the hope that it will be useful, | |
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 ;; GNU General Public License for more details. | |
19 | |
20 ;; You should have received a copy of the GNU General Public License | |
14169 | 21 ;; along with GNU Emacs; see the file COPYING. If not, write to the |
64085 | 22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
23 ;; Boston, MA 02110-1301, USA. | |
4355 | 24 |
7942 | 25 ;;; Commentary: |
4355 | 26 |
27 ;; These are extensions to Emacs Lisp that provide a degree of | |
28 ;; Common Lisp compatibility, beyond what is already built-in | |
29 ;; in Emacs Lisp. | |
30 ;; | |
31 ;; This package was written by Dave Gillespie; it is a complete | |
32 ;; rewrite of Cesar Quiroz's original cl.el package of December 1986. | |
33 ;; | |
34 ;; Bug reports, comments, and suggestions are welcome! | |
35 | |
36 ;; This file contains portions of the Common Lisp extensions | |
37 ;; package which are autoloaded since they are relatively obscure. | |
38 | |
7942 | 39 ;;; Code: |
4355 | 40 |
64684
eb2cbda455c6
Require CL also when compiling.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
64085
diff
changeset
|
41 (require 'cl) |
4355 | 42 |
43 ;;; Type coercion. | |
44 | |
45 (defun coerce (x type) | |
46 "Coerce OBJECT to type TYPE. | |
62627
30ac735c84d8
(coerce, map, maplist, cl-mapc, mapl, mapcan, mapcon, some, every, notany,
Juanma Barranquero <lekktu@gmail.com>
parents:
62409
diff
changeset
|
47 TYPE is a Common Lisp type specifier. |
30ac735c84d8
(coerce, map, maplist, cl-mapc, mapl, mapcan, mapcon, some, every, notany,
Juanma Barranquero <lekktu@gmail.com>
parents:
62409
diff
changeset
|
48 \n(fn OBJECT TYPE)" |
4355 | 49 (cond ((eq type 'list) (if (listp x) x (append x nil))) |
50 ((eq type 'vector) (if (vectorp x) x (vconcat x))) | |
51 ((eq type 'string) (if (stringp x) x (concat x))) | |
52 ((eq type 'array) (if (arrayp x) x (vconcat x))) | |
53 ((and (eq type 'character) (stringp x) (= (length x) 1)) (aref x 0)) | |
54 ((and (eq type 'character) (symbolp x)) (coerce (symbol-name x) type)) | |
55 ((eq type 'float) (float x)) | |
56 ((typep x type) x) | |
57 (t (error "Can't coerce %s to type %s" x type)))) | |
58 | |
59 | |
60 ;;; Predicates. | |
61 | |
62 (defun equalp (x y) | |
62409 | 63 "Return t if two Lisp objects have similar structures and contents. |
4355 | 64 This is like `equal', except that it accepts numerically equal |
65 numbers of different types (float vs. integer), and also compares | |
66 strings case-insensitively." | |
67 (cond ((eq x y) t) | |
68 ((stringp x) | |
69 (and (stringp y) (= (length x) (length y)) | |
14794
c3a2cabb73ef
(equalp): Use string-equal on strings.
Erik Naggum <erik@naggum.no>
parents:
14762
diff
changeset
|
70 (or (string-equal x y) |
c3a2cabb73ef
(equalp): Use string-equal on strings.
Erik Naggum <erik@naggum.no>
parents:
14762
diff
changeset
|
71 (string-equal (downcase x) (downcase y))))) ; lazy but simple! |
4355 | 72 ((numberp x) |
73 (and (numberp y) (= x y))) | |
74 ((consp x) | |
14762
624d5547a6d6
(equalp): Correctly compare last elt of two lists.
Richard M. Stallman <rms@gnu.org>
parents:
14169
diff
changeset
|
75 (while (and (consp x) (consp y) (equalp (car x) (car y))) |
624d5547a6d6
(equalp): Correctly compare last elt of two lists.
Richard M. Stallman <rms@gnu.org>
parents:
14169
diff
changeset
|
76 (setq x (cdr x) y (cdr y))) |
4355 | 77 (and (not (consp x)) (equalp x y))) |
78 ((vectorp x) | |
79 (and (vectorp y) (= (length x) (length y)) | |
80 (let ((i (length x))) | |
81 (while (and (>= (setq i (1- i)) 0) | |
82 (equalp (aref x i) (aref y i)))) | |
83 (< i 0)))) | |
84 (t (equal x y)))) | |
85 | |
86 | |
87 ;;; Control structures. | |
88 | |
89 (defun cl-mapcar-many (cl-func cl-seqs) | |
90 (if (cdr (cdr cl-seqs)) | |
91 (let* ((cl-res nil) | |
92 (cl-n (apply 'min (mapcar 'length cl-seqs))) | |
93 (cl-i 0) | |
94 (cl-args (copy-sequence cl-seqs)) | |
95 cl-p1 cl-p2) | |
96 (setq cl-seqs (copy-sequence cl-seqs)) | |
97 (while (< cl-i cl-n) | |
98 (setq cl-p1 cl-seqs cl-p2 cl-args) | |
99 (while cl-p1 | |
100 (setcar cl-p2 | |
101 (if (consp (car cl-p1)) | |
102 (prog1 (car (car cl-p1)) | |
103 (setcar cl-p1 (cdr (car cl-p1)))) | |
104 (aref (car cl-p1) cl-i))) | |
105 (setq cl-p1 (cdr cl-p1) cl-p2 (cdr cl-p2))) | |
47662
a6d932b28650
(cl-push, cl-pop): Remove. Use pop and push throughout the file instead.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
45699
diff
changeset
|
106 (push (apply cl-func cl-args) cl-res) |
4355 | 107 (setq cl-i (1+ cl-i))) |
108 (nreverse cl-res)) | |
109 (let ((cl-res nil) | |
110 (cl-x (car cl-seqs)) | |
111 (cl-y (nth 1 cl-seqs))) | |
112 (let ((cl-n (min (length cl-x) (length cl-y))) | |
113 (cl-i -1)) | |
114 (while (< (setq cl-i (1+ cl-i)) cl-n) | |
47662
a6d932b28650
(cl-push, cl-pop): Remove. Use pop and push throughout the file instead.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
45699
diff
changeset
|
115 (push (funcall cl-func |
a6d932b28650
(cl-push, cl-pop): Remove. Use pop and push throughout the file instead.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
45699
diff
changeset
|
116 (if (consp cl-x) (pop cl-x) (aref cl-x cl-i)) |
a6d932b28650
(cl-push, cl-pop): Remove. Use pop and push throughout the file instead.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
45699
diff
changeset
|
117 (if (consp cl-y) (pop cl-y) (aref cl-y cl-i))) |
4355 | 118 cl-res))) |
119 (nreverse cl-res)))) | |
120 | |
121 (defun map (cl-type cl-func cl-seq &rest cl-rest) | |
62627
30ac735c84d8
(coerce, map, maplist, cl-mapc, mapl, mapcan, mapcon, some, every, notany,
Juanma Barranquero <lekktu@gmail.com>
parents:
62409
diff
changeset
|
122 "Map a FUNCTION across one or more SEQUENCEs, returning a sequence. |
30ac735c84d8
(coerce, map, maplist, cl-mapc, mapl, mapcan, mapcon, some, every, notany,
Juanma Barranquero <lekktu@gmail.com>
parents:
62409
diff
changeset
|
123 TYPE is the sequence type to return. |
30ac735c84d8
(coerce, map, maplist, cl-mapc, mapl, mapcan, mapcon, some, every, notany,
Juanma Barranquero <lekktu@gmail.com>
parents:
62409
diff
changeset
|
124 \n(fn TYPE FUNCTION SEQUENCE...)" |
4355 | 125 (let ((cl-res (apply 'mapcar* cl-func cl-seq cl-rest))) |
126 (and cl-type (coerce cl-res cl-type)))) | |
127 | |
128 (defun maplist (cl-func cl-list &rest cl-rest) | |
62627
30ac735c84d8
(coerce, map, maplist, cl-mapc, mapl, mapcan, mapcon, some, every, notany,
Juanma Barranquero <lekktu@gmail.com>
parents:
62409
diff
changeset
|
129 "Map FUNCTION to each sublist of LIST or LISTs. |
4355 | 130 Like `mapcar', except applies to lists and their cdr's rather than to |
62627
30ac735c84d8
(coerce, map, maplist, cl-mapc, mapl, mapcan, mapcon, some, every, notany,
Juanma Barranquero <lekktu@gmail.com>
parents:
62409
diff
changeset
|
131 the elements themselves. |
30ac735c84d8
(coerce, map, maplist, cl-mapc, mapl, mapcan, mapcon, some, every, notany,
Juanma Barranquero <lekktu@gmail.com>
parents:
62409
diff
changeset
|
132 \n(fn FUNCTION LIST...)" |
4355 | 133 (if cl-rest |
134 (let ((cl-res nil) | |
135 (cl-args (cons cl-list (copy-sequence cl-rest))) | |
136 cl-p) | |
137 (while (not (memq nil cl-args)) | |
47662
a6d932b28650
(cl-push, cl-pop): Remove. Use pop and push throughout the file instead.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
45699
diff
changeset
|
138 (push (apply cl-func cl-args) cl-res) |
4355 | 139 (setq cl-p cl-args) |
47662
a6d932b28650
(cl-push, cl-pop): Remove. Use pop and push throughout the file instead.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
45699
diff
changeset
|
140 (while cl-p (setcar cl-p (cdr (pop cl-p)) ))) |
4355 | 141 (nreverse cl-res)) |
142 (let ((cl-res nil)) | |
143 (while cl-list | |
47662
a6d932b28650
(cl-push, cl-pop): Remove. Use pop and push throughout the file instead.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
45699
diff
changeset
|
144 (push (funcall cl-func cl-list) cl-res) |
4355 | 145 (setq cl-list (cdr cl-list))) |
146 (nreverse cl-res)))) | |
147 | |
28667 | 148 (defun cl-mapc (cl-func cl-seq &rest cl-rest) |
62627
30ac735c84d8
(coerce, map, maplist, cl-mapc, mapl, mapcan, mapcon, some, every, notany,
Juanma Barranquero <lekktu@gmail.com>
parents:
62409
diff
changeset
|
149 "Like `mapcar', but does not accumulate values returned by the function. |
30ac735c84d8
(coerce, map, maplist, cl-mapc, mapl, mapcan, mapcon, some, every, notany,
Juanma Barranquero <lekktu@gmail.com>
parents:
62409
diff
changeset
|
150 \n(fn FUNCTION SEQUENCE...)" |
4355 | 151 (if cl-rest |
28565 | 152 (progn (apply 'map nil cl-func cl-seq cl-rest) |
153 cl-seq) | |
30074
e06697d4135f
(cl-old-mapc): Removed; don't defalias mapc.
Gerd Moellmann <gerd@gnu.org>
parents:
28667
diff
changeset
|
154 (mapc cl-func cl-seq))) |
4355 | 155 |
156 (defun mapl (cl-func cl-list &rest cl-rest) | |
62627
30ac735c84d8
(coerce, map, maplist, cl-mapc, mapl, mapcan, mapcon, some, every, notany,
Juanma Barranquero <lekktu@gmail.com>
parents:
62409
diff
changeset
|
157 "Like `maplist', but does not accumulate values returned by the function. |
30ac735c84d8
(coerce, map, maplist, cl-mapc, mapl, mapcan, mapcon, some, every, notany,
Juanma Barranquero <lekktu@gmail.com>
parents:
62409
diff
changeset
|
158 \n(fn FUNCTION LIST...)" |
4355 | 159 (if cl-rest |
160 (apply 'maplist cl-func cl-list cl-rest) | |
161 (let ((cl-p cl-list)) | |
162 (while cl-p (funcall cl-func cl-p) (setq cl-p (cdr cl-p))))) | |
163 cl-list) | |
164 | |
165 (defun mapcan (cl-func cl-seq &rest cl-rest) | |
62627
30ac735c84d8
(coerce, map, maplist, cl-mapc, mapl, mapcan, mapcon, some, every, notany,
Juanma Barranquero <lekktu@gmail.com>
parents:
62409
diff
changeset
|
166 "Like `mapcar', but nconc's together the values returned by the function. |
30ac735c84d8
(coerce, map, maplist, cl-mapc, mapl, mapcan, mapcon, some, every, notany,
Juanma Barranquero <lekktu@gmail.com>
parents:
62409
diff
changeset
|
167 \n(fn FUNCTION SEQUENCE...)" |
4355 | 168 (apply 'nconc (apply 'mapcar* cl-func cl-seq cl-rest))) |
169 | |
170 (defun mapcon (cl-func cl-list &rest cl-rest) | |
62627
30ac735c84d8
(coerce, map, maplist, cl-mapc, mapl, mapcan, mapcon, some, every, notany,
Juanma Barranquero <lekktu@gmail.com>
parents:
62409
diff
changeset
|
171 "Like `maplist', but nconc's together the values returned by the function. |
30ac735c84d8
(coerce, map, maplist, cl-mapc, mapl, mapcan, mapcon, some, every, notany,
Juanma Barranquero <lekktu@gmail.com>
parents:
62409
diff
changeset
|
172 \n(fn FUNCTION LIST...)" |
4355 | 173 (apply 'nconc (apply 'maplist cl-func cl-list cl-rest))) |
174 | |
175 (defun some (cl-pred cl-seq &rest cl-rest) | |
176 "Return true if PREDICATE is true of any element of SEQ or SEQs. | |
62627
30ac735c84d8
(coerce, map, maplist, cl-mapc, mapl, mapcan, mapcon, some, every, notany,
Juanma Barranquero <lekktu@gmail.com>
parents:
62409
diff
changeset
|
177 If so, return the true (non-nil) value returned by PREDICATE. |
30ac735c84d8
(coerce, map, maplist, cl-mapc, mapl, mapcan, mapcon, some, every, notany,
Juanma Barranquero <lekktu@gmail.com>
parents:
62409
diff
changeset
|
178 \n(fn PREDICATE SEQ...)" |
4355 | 179 (if (or cl-rest (nlistp cl-seq)) |
180 (catch 'cl-some | |
181 (apply 'map nil | |
182 (function (lambda (&rest cl-x) | |
183 (let ((cl-res (apply cl-pred cl-x))) | |
184 (if cl-res (throw 'cl-some cl-res))))) | |
185 cl-seq cl-rest) nil) | |
186 (let ((cl-x nil)) | |
47662
a6d932b28650
(cl-push, cl-pop): Remove. Use pop and push throughout the file instead.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
45699
diff
changeset
|
187 (while (and cl-seq (not (setq cl-x (funcall cl-pred (pop cl-seq)))))) |
4355 | 188 cl-x))) |
189 | |
190 (defun every (cl-pred cl-seq &rest cl-rest) | |
62627
30ac735c84d8
(coerce, map, maplist, cl-mapc, mapl, mapcan, mapcon, some, every, notany,
Juanma Barranquero <lekktu@gmail.com>
parents:
62409
diff
changeset
|
191 "Return true if PREDICATE is true of every element of SEQ or SEQs. |
30ac735c84d8
(coerce, map, maplist, cl-mapc, mapl, mapcan, mapcon, some, every, notany,
Juanma Barranquero <lekktu@gmail.com>
parents:
62409
diff
changeset
|
192 \n(fn PREDICATE SEQ...)" |
4355 | 193 (if (or cl-rest (nlistp cl-seq)) |
194 (catch 'cl-every | |
195 (apply 'map nil | |
196 (function (lambda (&rest cl-x) | |
197 (or (apply cl-pred cl-x) (throw 'cl-every nil)))) | |
198 cl-seq cl-rest) t) | |
199 (while (and cl-seq (funcall cl-pred (car cl-seq))) | |
200 (setq cl-seq (cdr cl-seq))) | |
201 (null cl-seq))) | |
202 | |
203 (defun notany (cl-pred cl-seq &rest cl-rest) | |
62627
30ac735c84d8
(coerce, map, maplist, cl-mapc, mapl, mapcan, mapcon, some, every, notany,
Juanma Barranquero <lekktu@gmail.com>
parents:
62409
diff
changeset
|
204 "Return true if PREDICATE is false of every element of SEQ or SEQs. |
30ac735c84d8
(coerce, map, maplist, cl-mapc, mapl, mapcan, mapcon, some, every, notany,
Juanma Barranquero <lekktu@gmail.com>
parents:
62409
diff
changeset
|
205 \n(fn PREDICATE SEQ...)" |
4355 | 206 (not (apply 'some cl-pred cl-seq cl-rest))) |
207 | |
208 (defun notevery (cl-pred cl-seq &rest cl-rest) | |
62627
30ac735c84d8
(coerce, map, maplist, cl-mapc, mapl, mapcan, mapcon, some, every, notany,
Juanma Barranquero <lekktu@gmail.com>
parents:
62409
diff
changeset
|
209 "Return true if PREDICATE is false of some element of SEQ or SEQs. |
30ac735c84d8
(coerce, map, maplist, cl-mapc, mapl, mapcan, mapcon, some, every, notany,
Juanma Barranquero <lekktu@gmail.com>
parents:
62409
diff
changeset
|
210 \n(fn PREDICATE SEQ...)" |
4355 | 211 (not (apply 'every cl-pred cl-seq cl-rest))) |
212 | |
213 ;;; Support for `loop'. | |
50802
fe838fc4d9a9
(cl-map-keymap): Redefine as alias.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
47662
diff
changeset
|
214 (defalias 'cl-map-keymap 'map-keymap) |
4355 | 215 |
216 (defun cl-map-keymap-recursively (cl-func-rec cl-map &optional cl-base) | |
217 (or cl-base | |
27123 | 218 (setq cl-base (copy-sequence [0]))) |
50802
fe838fc4d9a9
(cl-map-keymap): Redefine as alias.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
47662
diff
changeset
|
219 (map-keymap |
4355 | 220 (function |
221 (lambda (cl-key cl-bind) | |
222 (aset cl-base (1- (length cl-base)) cl-key) | |
223 (if (keymapp cl-bind) | |
224 (cl-map-keymap-recursively | |
225 cl-func-rec cl-bind | |
27123 | 226 (vconcat cl-base (list 0))) |
4355 | 227 (funcall cl-func-rec cl-base cl-bind)))) |
228 cl-map)) | |
229 | |
230 (defun cl-map-intervals (cl-func &optional cl-what cl-prop cl-start cl-end) | |
231 (or cl-what (setq cl-what (current-buffer))) | |
232 (if (bufferp cl-what) | |
233 (let (cl-mark cl-mark2 (cl-next t) cl-next2) | |
28565 | 234 (with-current-buffer cl-what |
4355 | 235 (setq cl-mark (copy-marker (or cl-start (point-min)))) |
236 (setq cl-mark2 (and cl-end (copy-marker cl-end)))) | |
237 (while (and cl-next (or (not cl-mark2) (< cl-mark cl-mark2))) | |
28565 | 238 (setq cl-next (if cl-prop (next-single-property-change |
239 cl-mark cl-prop cl-what) | |
240 (next-property-change cl-mark cl-what)) | |
241 cl-next2 (or cl-next (with-current-buffer cl-what | |
242 (point-max)))) | |
4355 | 243 (funcall cl-func (prog1 (marker-position cl-mark) |
244 (set-marker cl-mark cl-next2)) | |
245 (if cl-mark2 (min cl-next2 cl-mark2) cl-next2))) | |
246 (set-marker cl-mark nil) (if cl-mark2 (set-marker cl-mark2 nil))) | |
247 (or cl-start (setq cl-start 0)) | |
248 (or cl-end (setq cl-end (length cl-what))) | |
249 (while (< cl-start cl-end) | |
28565 | 250 (let ((cl-next (or (if cl-prop (next-single-property-change |
251 cl-start cl-prop cl-what) | |
252 (next-property-change cl-start cl-what)) | |
4355 | 253 cl-end))) |
254 (funcall cl-func cl-start (min cl-next cl-end)) | |
255 (setq cl-start cl-next))))) | |
256 | |
257 (defun cl-map-overlays (cl-func &optional cl-buffer cl-start cl-end cl-arg) | |
258 (or cl-buffer (setq cl-buffer (current-buffer))) | |
259 (if (fboundp 'overlay-lists) | |
260 | |
261 ;; This is the preferred algorithm, though overlay-lists is undocumented. | |
262 (let (cl-ovl) | |
28565 | 263 (with-current-buffer cl-buffer |
4355 | 264 (setq cl-ovl (overlay-lists)) |
265 (if cl-start (setq cl-start (copy-marker cl-start))) | |
266 (if cl-end (setq cl-end (copy-marker cl-end)))) | |
267 (setq cl-ovl (nconc (car cl-ovl) (cdr cl-ovl))) | |
268 (while (and cl-ovl | |
269 (or (not (overlay-start (car cl-ovl))) | |
270 (and cl-end (>= (overlay-start (car cl-ovl)) cl-end)) | |
271 (and cl-start (<= (overlay-end (car cl-ovl)) cl-start)) | |
272 (not (funcall cl-func (car cl-ovl) cl-arg)))) | |
273 (setq cl-ovl (cdr cl-ovl))) | |
274 (if cl-start (set-marker cl-start nil)) | |
275 (if cl-end (set-marker cl-end nil))) | |
276 | |
277 ;; This alternate algorithm fails to find zero-length overlays. | |
28565 | 278 (let ((cl-mark (with-current-buffer cl-buffer |
279 (copy-marker (or cl-start (point-min))))) | |
280 (cl-mark2 (and cl-end (with-current-buffer cl-buffer | |
281 (copy-marker cl-end)))) | |
4355 | 282 cl-pos cl-ovl) |
283 (while (save-excursion | |
284 (and (setq cl-pos (marker-position cl-mark)) | |
285 (< cl-pos (or cl-mark2 (point-max))) | |
286 (progn | |
287 (set-buffer cl-buffer) | |
288 (setq cl-ovl (overlays-at cl-pos)) | |
289 (set-marker cl-mark (next-overlay-change cl-pos))))) | |
290 (while (and cl-ovl | |
291 (or (/= (overlay-start (car cl-ovl)) cl-pos) | |
292 (not (and (funcall cl-func (car cl-ovl) cl-arg) | |
293 (set-marker cl-mark nil))))) | |
294 (setq cl-ovl (cdr cl-ovl)))) | |
295 (set-marker cl-mark nil) (if cl-mark2 (set-marker cl-mark2 nil))))) | |
296 | |
297 ;;; Support for `setf'. | |
298 (defun cl-set-frame-visible-p (frame val) | |
299 (cond ((null val) (make-frame-invisible frame)) | |
300 ((eq val 'icon) (iconify-frame frame)) | |
301 (t (make-frame-visible frame))) | |
302 val) | |
303 | |
304 ;;; Support for `progv'. | |
305 (defvar cl-progv-save) | |
306 (defun cl-progv-before (syms values) | |
307 (while syms | |
47662
a6d932b28650
(cl-push, cl-pop): Remove. Use pop and push throughout the file instead.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
45699
diff
changeset
|
308 (push (if (boundp (car syms)) |
4355 | 309 (cons (car syms) (symbol-value (car syms))) |
310 (car syms)) cl-progv-save) | |
311 (if values | |
47662
a6d932b28650
(cl-push, cl-pop): Remove. Use pop and push throughout the file instead.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
45699
diff
changeset
|
312 (set (pop syms) (pop values)) |
a6d932b28650
(cl-push, cl-pop): Remove. Use pop and push throughout the file instead.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
45699
diff
changeset
|
313 (makunbound (pop syms))))) |
4355 | 314 |
315 (defun cl-progv-after () | |
316 (while cl-progv-save | |
317 (if (consp (car cl-progv-save)) | |
318 (set (car (car cl-progv-save)) (cdr (car cl-progv-save))) | |
319 (makunbound (car cl-progv-save))) | |
47662
a6d932b28650
(cl-push, cl-pop): Remove. Use pop and push throughout the file instead.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
45699
diff
changeset
|
320 (pop cl-progv-save))) |
4355 | 321 |
322 | |
323 ;;; Numbers. | |
324 | |
325 (defun gcd (&rest args) | |
326 "Return the greatest common divisor of the arguments." | |
47662
a6d932b28650
(cl-push, cl-pop): Remove. Use pop and push throughout the file instead.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
45699
diff
changeset
|
327 (let ((a (abs (or (pop args) 0)))) |
4355 | 328 (while args |
47662
a6d932b28650
(cl-push, cl-pop): Remove. Use pop and push throughout the file instead.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
45699
diff
changeset
|
329 (let ((b (abs (pop args)))) |
4355 | 330 (while (> b 0) (setq b (% a (setq a b)))))) |
331 a)) | |
332 | |
333 (defun lcm (&rest args) | |
334 "Return the least common multiple of the arguments." | |
335 (if (memq 0 args) | |
336 0 | |
47662
a6d932b28650
(cl-push, cl-pop): Remove. Use pop and push throughout the file instead.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
45699
diff
changeset
|
337 (let ((a (abs (or (pop args) 1)))) |
4355 | 338 (while args |
47662
a6d932b28650
(cl-push, cl-pop): Remove. Use pop and push throughout the file instead.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
45699
diff
changeset
|
339 (let ((b (abs (pop args)))) |
4355 | 340 (setq a (* (/ a (gcd a b)) b)))) |
341 a))) | |
342 | |
62627
30ac735c84d8
(coerce, map, maplist, cl-mapc, mapl, mapcan, mapcon, some, every, notany,
Juanma Barranquero <lekktu@gmail.com>
parents:
62409
diff
changeset
|
343 (defun isqrt (x) |
4355 | 344 "Return the integer square root of the argument." |
62627
30ac735c84d8
(coerce, map, maplist, cl-mapc, mapl, mapcan, mapcon, some, every, notany,
Juanma Barranquero <lekktu@gmail.com>
parents:
62409
diff
changeset
|
345 (if (and (integerp x) (> x 0)) |
30ac735c84d8
(coerce, map, maplist, cl-mapc, mapl, mapcan, mapcon, some, every, notany,
Juanma Barranquero <lekktu@gmail.com>
parents:
62409
diff
changeset
|
346 (let ((g (cond ((<= x 100) 10) ((<= x 10000) 100) |
30ac735c84d8
(coerce, map, maplist, cl-mapc, mapl, mapcan, mapcon, some, every, notany,
Juanma Barranquero <lekktu@gmail.com>
parents:
62409
diff
changeset
|
347 ((<= x 1000000) 1000) (t x))) |
4355 | 348 g2) |
62627
30ac735c84d8
(coerce, map, maplist, cl-mapc, mapl, mapcan, mapcon, some, every, notany,
Juanma Barranquero <lekktu@gmail.com>
parents:
62409
diff
changeset
|
349 (while (< (setq g2 (/ (+ g (/ x g)) 2)) g) |
4355 | 350 (setq g g2)) |
351 g) | |
62627
30ac735c84d8
(coerce, map, maplist, cl-mapc, mapl, mapcan, mapcon, some, every, notany,
Juanma Barranquero <lekktu@gmail.com>
parents:
62409
diff
changeset
|
352 (if (eq x 0) 0 (signal 'arith-error nil)))) |
4355 | 353 |
354 (defun floor* (x &optional y) | |
355 "Return a list of the floor of X and the fractional part of X. | |
356 With two arguments, return floor and remainder of their quotient." | |
4517
45e3868140f1
(floor*): Use `floor' instead of doing most the work ourselves.
Paul Eggert <eggert@twinsun.com>
parents:
4355
diff
changeset
|
357 (let ((q (floor x y))) |
45e3868140f1
(floor*): Use `floor' instead of doing most the work ourselves.
Paul Eggert <eggert@twinsun.com>
parents:
4355
diff
changeset
|
358 (list q (- x (if y (* y q) q))))) |
4355 | 359 |
360 (defun ceiling* (x &optional y) | |
361 "Return a list of the ceiling of X and the fractional part of X. | |
362 With two arguments, return ceiling and remainder of their quotient." | |
363 (let ((res (floor* x y))) | |
364 (if (= (car (cdr res)) 0) res | |
365 (list (1+ (car res)) (- (car (cdr res)) (or y 1)))))) | |
366 | |
367 (defun truncate* (x &optional y) | |
368 "Return a list of the integer part of X and the fractional part of X. | |
369 With two arguments, return truncation and remainder of their quotient." | |
370 (if (eq (>= x 0) (or (null y) (>= y 0))) | |
371 (floor* x y) (ceiling* x y))) | |
372 | |
373 (defun round* (x &optional y) | |
374 "Return a list of X rounded to the nearest integer and the remainder. | |
375 With two arguments, return rounding and remainder of their quotient." | |
376 (if y | |
377 (if (and (integerp x) (integerp y)) | |
378 (let* ((hy (/ y 2)) | |
379 (res (floor* (+ x hy) y))) | |
380 (if (and (= (car (cdr res)) 0) | |
381 (= (+ hy hy) y) | |
382 (/= (% (car res) 2) 0)) | |
383 (list (1- (car res)) hy) | |
384 (list (car res) (- (car (cdr res)) hy)))) | |
385 (let ((q (round (/ x y)))) | |
386 (list q (- x (* q y))))) | |
387 (if (integerp x) (list x 0) | |
388 (let ((q (round x))) | |
389 (list q (- x q)))))) | |
390 | |
391 (defun mod* (x y) | |
392 "The remainder of X divided by Y, with the same sign as Y." | |
393 (nth 1 (floor* x y))) | |
394 | |
395 (defun rem* (x y) | |
396 "The remainder of X divided by Y, with the same sign as X." | |
397 (nth 1 (truncate* x y))) | |
398 | |
62627
30ac735c84d8
(coerce, map, maplist, cl-mapc, mapl, mapcan, mapcon, some, every, notany,
Juanma Barranquero <lekktu@gmail.com>
parents:
62409
diff
changeset
|
399 (defun signum (x) |
30ac735c84d8
(coerce, map, maplist, cl-mapc, mapl, mapcan, mapcon, some, every, notany,
Juanma Barranquero <lekktu@gmail.com>
parents:
62409
diff
changeset
|
400 "Return 1 if X is positive, -1 if negative, 0 if zero." |
30ac735c84d8
(coerce, map, maplist, cl-mapc, mapl, mapcan, mapcon, some, every, notany,
Juanma Barranquero <lekktu@gmail.com>
parents:
62409
diff
changeset
|
401 (cond ((> x 0) 1) ((< x 0) -1) (t 0))) |
4355 | 402 |
403 | |
404 ;; Random numbers. | |
405 | |
406 (defvar *random-state*) | |
407 (defun random* (lim &optional state) | |
408 "Return a random nonnegative number less than LIM, an integer or float. | |
409 Optional second arg STATE is a random-state object." | |
410 (or state (setq state *random-state*)) | |
411 ;; Inspired by "ran3" from Numerical Recipes. Additive congruential method. | |
412 (let ((vec (aref state 3))) | |
413 (if (integerp vec) | |
414 (let ((i 0) (j (- 1357335 (% (abs vec) 1357333))) (k 1) ii) | |
415 (aset state 3 (setq vec (make-vector 55 nil))) | |
416 (aset vec 0 j) | |
417 (while (> (setq i (% (+ i 21) 55)) 0) | |
418 (aset vec i (setq j (prog1 k (setq k (- j k)))))) | |
419 (while (< (setq i (1+ i)) 200) (random* 2 state)))) | |
420 (let* ((i (aset state 1 (% (1+ (aref state 1)) 55))) | |
421 (j (aset state 2 (% (1+ (aref state 2)) 55))) | |
422 (n (logand 8388607 (aset vec i (- (aref vec i) (aref vec j)))))) | |
423 (if (integerp lim) | |
424 (if (<= lim 512) (% n lim) | |
425 (if (> lim 8388607) (setq n (+ (lsh n 9) (random* 512 state)))) | |
426 (let ((mask 1023)) | |
427 (while (< mask (1- lim)) (setq mask (1+ (+ mask mask)))) | |
428 (if (< (setq n (logand n mask)) lim) n (random* lim state)))) | |
429 (* (/ n '8388608e0) lim))))) | |
430 | |
431 (defun make-random-state (&optional state) | |
432 "Return a copy of random-state STATE, or of `*random-state*' if omitted. | |
433 If STATE is t, return a new state object seeded from the time of day." | |
434 (cond ((null state) (make-random-state *random-state*)) | |
435 ((vectorp state) (cl-copy-tree state t)) | |
436 ((integerp state) (vector 'cl-random-state-tag -1 30 state)) | |
437 (t (make-random-state (cl-random-time))))) | |
438 | |
439 (defun random-state-p (object) | |
440 "Return t if OBJECT is a random-state object." | |
441 (and (vectorp object) (= (length object) 4) | |
442 (eq (aref object 0) 'cl-random-state-tag))) | |
443 | |
444 | |
445 ;; Implementation limits. | |
446 | |
447 (defun cl-finite-do (func a b) | |
448 (condition-case err | |
449 (let ((res (funcall func a b))) ; check for IEEE infinity | |
450 (and (numberp res) (/= res (/ res 2)) res)) | |
451 (arith-error nil))) | |
452 | |
453 (defvar most-positive-float) | |
454 (defvar most-negative-float) | |
455 (defvar least-positive-float) | |
456 (defvar least-negative-float) | |
457 (defvar least-positive-normalized-float) | |
458 (defvar least-negative-normalized-float) | |
459 (defvar float-epsilon) | |
460 (defvar float-negative-epsilon) | |
461 | |
462 (defun cl-float-limits () | |
463 (or most-positive-float (not (numberp '2e1)) | |
464 (let ((x '2e0) y z) | |
465 ;; Find maximum exponent (first two loops are optimizations) | |
466 (while (cl-finite-do '* x x) (setq x (* x x))) | |
467 (while (cl-finite-do '* x (/ x 2)) (setq x (* x (/ x 2)))) | |
468 (while (cl-finite-do '+ x x) (setq x (+ x x))) | |
469 (setq z x y (/ x 2)) | |
470 ;; Now fill in 1's in the mantissa. | |
471 (while (and (cl-finite-do '+ x y) (/= (+ x y) x)) | |
472 (setq x (+ x y) y (/ y 2))) | |
473 (setq most-positive-float x | |
474 most-negative-float (- x)) | |
475 ;; Divide down until mantissa starts rounding. | |
476 (setq x (/ x z) y (/ 16 z) x (* x y)) | |
477 (while (condition-case err (and (= x (* (/ x 2) 2)) (> (/ y 2) 0)) | |
478 (arith-error nil)) | |
479 (setq x (/ x 2) y (/ y 2))) | |
480 (setq least-positive-normalized-float y | |
481 least-negative-normalized-float (- y)) | |
482 ;; Divide down until value underflows to zero. | |
483 (setq x (/ 1 z) y x) | |
484 (while (condition-case err (> (/ x 2) 0) (arith-error nil)) | |
485 (setq x (/ x 2))) | |
486 (setq least-positive-float x | |
487 least-negative-float (- x)) | |
488 (setq x '1e0) | |
489 (while (/= (+ '1e0 x) '1e0) (setq x (/ x 2))) | |
490 (setq float-epsilon (* x 2)) | |
491 (setq x '1e0) | |
492 (while (/= (- '1e0 x) '1e0) (setq x (/ x 2))) | |
493 (setq float-negative-epsilon (* x 2)))) | |
494 nil) | |
495 | |
496 | |
497 ;;; Sequence functions. | |
498 | |
499 (defun subseq (seq start &optional end) | |
500 "Return the subsequence of SEQ from START to END. | |
501 If END is omitted, it defaults to the length of the sequence. | |
502 If START or END is negative, it counts from the end." | |
503 (if (stringp seq) (substring seq start end) | |
504 (let (len) | |
505 (and end (< end 0) (setq end (+ end (setq len (length seq))))) | |
506 (if (< start 0) (setq start (+ start (or len (setq len (length seq)))))) | |
507 (cond ((listp seq) | |
508 (if (> start 0) (setq seq (nthcdr start seq))) | |
509 (if end | |
510 (let ((res nil)) | |
511 (while (>= (setq end (1- end)) start) | |
47662
a6d932b28650
(cl-push, cl-pop): Remove. Use pop and push throughout the file instead.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
45699
diff
changeset
|
512 (push (pop seq) res)) |
4355 | 513 (nreverse res)) |
514 (copy-sequence seq))) | |
515 (t | |
516 (or end (setq end (or len (length seq)))) | |
517 (let ((res (make-vector (max (- end start) 0) nil)) | |
518 (i 0)) | |
519 (while (< start end) | |
520 (aset res i (aref seq start)) | |
521 (setq i (1+ i) start (1+ start))) | |
522 res)))))) | |
523 | |
524 (defun concatenate (type &rest seqs) | |
62627
30ac735c84d8
(coerce, map, maplist, cl-mapc, mapl, mapcan, mapcon, some, every, notany,
Juanma Barranquero <lekktu@gmail.com>
parents:
62409
diff
changeset
|
525 "Concatenate, into a sequence of type TYPE, the argument SEQUENCEs. |
30ac735c84d8
(coerce, map, maplist, cl-mapc, mapl, mapcan, mapcon, some, every, notany,
Juanma Barranquero <lekktu@gmail.com>
parents:
62409
diff
changeset
|
526 \n(fn TYPE SEQUENCE...)" |
4355 | 527 (cond ((eq type 'vector) (apply 'vconcat seqs)) |
528 ((eq type 'string) (apply 'concat seqs)) | |
529 ((eq type 'list) (apply 'append (append seqs '(nil)))) | |
530 (t (error "Not a sequence type name: %s" type)))) | |
531 | |
532 | |
533 ;;; List functions. | |
534 | |
535 (defun revappend (x y) | |
536 "Equivalent to (append (reverse X) Y)." | |
537 (nconc (reverse x) y)) | |
538 | |
539 (defun nreconc (x y) | |
540 "Equivalent to (nconc (nreverse X) Y)." | |
541 (nconc (nreverse x) y)) | |
542 | |
543 (defun list-length (x) | |
62627
30ac735c84d8
(coerce, map, maplist, cl-mapc, mapl, mapcan, mapcon, some, every, notany,
Juanma Barranquero <lekktu@gmail.com>
parents:
62409
diff
changeset
|
544 "Return the length of list X. Return nil if list is circular." |
4355 | 545 (let ((n 0) (fast x) (slow x)) |
546 (while (and (cdr fast) (not (and (eq fast slow) (> n 0)))) | |
547 (setq n (+ n 2) fast (cdr (cdr fast)) slow (cdr slow))) | |
548 (if fast (if (cdr fast) nil (1+ n)) n))) | |
549 | |
550 (defun tailp (sublist list) | |
551 "Return true if SUBLIST is a tail of LIST." | |
552 (while (and (consp list) (not (eq sublist list))) | |
553 (setq list (cdr list))) | |
554 (if (numberp sublist) (equal sublist list) (eq sublist list))) | |
555 | |
45699
5204b7681bdc
(cl-copy-tree): Moved to `copy-tree' in subr.el. Add a defalias with
Colin Walters <walters@gnu.org>
parents:
32481
diff
changeset
|
556 (defalias 'cl-copy-tree 'copy-tree) |
4355 | 557 |
558 | |
559 ;;; Property lists. | |
560 | |
561 (defun get* (sym tag &optional def) ; See compiler macro in cl-macs.el | |
62627
30ac735c84d8
(coerce, map, maplist, cl-mapc, mapl, mapcan, mapcon, some, every, notany,
Juanma Barranquero <lekktu@gmail.com>
parents:
62409
diff
changeset
|
562 "Return the value of SYMBOL's PROPNAME property, or DEFAULT if none. |
30ac735c84d8
(coerce, map, maplist, cl-mapc, mapl, mapcan, mapcon, some, every, notany,
Juanma Barranquero <lekktu@gmail.com>
parents:
62409
diff
changeset
|
563 \n(fn SYMBOL PROPNAME &optional DEFAULT)" |
4355 | 564 (or (get sym tag) |
565 (and def | |
566 (let ((plist (symbol-plist sym))) | |
567 (while (and plist (not (eq (car plist) tag))) | |
568 (setq plist (cdr (cdr plist)))) | |
569 (if plist (car (cdr plist)) def))))) | |
570 | |
571 (defun getf (plist tag &optional def) | |
572 "Search PROPLIST for property PROPNAME; return its value or DEFAULT. | |
62627
30ac735c84d8
(coerce, map, maplist, cl-mapc, mapl, mapcan, mapcon, some, every, notany,
Juanma Barranquero <lekktu@gmail.com>
parents:
62409
diff
changeset
|
573 PROPLIST is a list of the sort returned by `symbol-plist'. |
30ac735c84d8
(coerce, map, maplist, cl-mapc, mapl, mapcan, mapcon, some, every, notany,
Juanma Barranquero <lekktu@gmail.com>
parents:
62409
diff
changeset
|
574 \n(fn PROPLIST PROPNAME &optional DEFAULT)" |
4355 | 575 (setplist '--cl-getf-symbol-- plist) |
576 (or (get '--cl-getf-symbol-- tag) | |
24826 | 577 ;; Originally we called get* here, |
578 ;; but that fails, because get* has a compiler macro | |
579 ;; definition that uses getf! | |
580 (when def | |
581 (while (and plist (not (eq (car plist) tag))) | |
582 (setq plist (cdr (cdr plist)))) | |
583 (if plist (car (cdr plist)) def)))) | |
4355 | 584 |
585 (defun cl-set-getf (plist tag val) | |
586 (let ((p plist)) | |
587 (while (and p (not (eq (car p) tag))) (setq p (cdr (cdr p)))) | |
588 (if p (progn (setcar (cdr p) val) plist) (list* tag val plist)))) | |
589 | |
590 (defun cl-do-remf (plist tag) | |
591 (let ((p (cdr plist))) | |
592 (while (and (cdr p) (not (eq (car (cdr p)) tag))) (setq p (cdr (cdr p)))) | |
593 (and (cdr p) (progn (setcdr p (cdr (cdr (cdr p)))) t)))) | |
594 | |
595 (defun cl-remprop (sym tag) | |
62627
30ac735c84d8
(coerce, map, maplist, cl-mapc, mapl, mapcan, mapcon, some, every, notany,
Juanma Barranquero <lekktu@gmail.com>
parents:
62409
diff
changeset
|
596 "Remove from SYMBOL's plist the property PROPNAME and its value. |
30ac735c84d8
(coerce, map, maplist, cl-mapc, mapl, mapcan, mapcon, some, every, notany,
Juanma Barranquero <lekktu@gmail.com>
parents:
62409
diff
changeset
|
597 \n(fn SYMBOL PROPNAME)" |
4355 | 598 (let ((plist (symbol-plist sym))) |
599 (if (and plist (eq tag (car plist))) | |
600 (progn (setplist sym (cdr (cdr plist))) t) | |
601 (cl-do-remf plist tag)))) | |
28565 | 602 (defalias 'remprop 'cl-remprop) |
4355 | 603 |
604 | |
605 | |
606 ;;; Hash tables. | |
47662
a6d932b28650
(cl-push, cl-pop): Remove. Use pop and push throughout the file instead.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
45699
diff
changeset
|
607 ;; This is just kept for compatibility with code byte-compiled by Emacs-20. |
4355 | 608 |
47662
a6d932b28650
(cl-push, cl-pop): Remove. Use pop and push throughout the file instead.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
45699
diff
changeset
|
609 ;; No idea if this might still be needed. |
4355 | 610 (defun cl-not-hash-table (x &optional y &rest z) |
27123 | 611 (signal 'wrong-type-argument (list 'cl-hash-table-p (or y x)))) |
4355 | 612 |
32481
4372fcfffc7f
(cl-builtin-gethash, cl-builtin-remhash)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
30086
diff
changeset
|
613 (defvar cl-builtin-gethash (symbol-function 'gethash)) |
4372fcfffc7f
(cl-builtin-gethash, cl-builtin-remhash)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
30086
diff
changeset
|
614 (defvar cl-builtin-remhash (symbol-function 'remhash)) |
4372fcfffc7f
(cl-builtin-gethash, cl-builtin-remhash)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
30086
diff
changeset
|
615 (defvar cl-builtin-clrhash (symbol-function 'clrhash)) |
4372fcfffc7f
(cl-builtin-gethash, cl-builtin-remhash)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
30086
diff
changeset
|
616 (defvar cl-builtin-maphash (symbol-function 'maphash)) |
4372fcfffc7f
(cl-builtin-gethash, cl-builtin-remhash)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
30086
diff
changeset
|
617 |
47662
a6d932b28650
(cl-push, cl-pop): Remove. Use pop and push throughout the file instead.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
45699
diff
changeset
|
618 (defalias 'cl-gethash 'gethash) |
a6d932b28650
(cl-push, cl-pop): Remove. Use pop and push throughout the file instead.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
45699
diff
changeset
|
619 (defalias 'cl-puthash 'puthash) |
a6d932b28650
(cl-push, cl-pop): Remove. Use pop and push throughout the file instead.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
45699
diff
changeset
|
620 (defalias 'cl-remhash 'remhash) |
a6d932b28650
(cl-push, cl-pop): Remove. Use pop and push throughout the file instead.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
45699
diff
changeset
|
621 (defalias 'cl-clrhash 'clrhash) |
a6d932b28650
(cl-push, cl-pop): Remove. Use pop and push throughout the file instead.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
45699
diff
changeset
|
622 (defalias 'cl-maphash 'maphash) |
a6d932b28650
(cl-push, cl-pop): Remove. Use pop and push throughout the file instead.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
45699
diff
changeset
|
623 ;; These three actually didn't exist in Emacs-20. |
a6d932b28650
(cl-push, cl-pop): Remove. Use pop and push throughout the file instead.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
45699
diff
changeset
|
624 (defalias 'cl-make-hash-table 'make-hash-table) |
a6d932b28650
(cl-push, cl-pop): Remove. Use pop and push throughout the file instead.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
45699
diff
changeset
|
625 (defalias 'cl-hash-table-p 'hash-table-p) |
a6d932b28650
(cl-push, cl-pop): Remove. Use pop and push throughout the file instead.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
45699
diff
changeset
|
626 (defalias 'cl-hash-table-count 'hash-table-count) |
4355 | 627 |
628 ;;; Some debugging aids. | |
629 | |
630 (defun cl-prettyprint (form) | |
631 "Insert a pretty-printed rendition of a Lisp FORM in current buffer." | |
632 (let ((pt (point)) last) | |
633 (insert "\n" (prin1-to-string form) "\n") | |
634 (setq last (point)) | |
635 (goto-char (1+ pt)) | |
636 (while (search-forward "(quote " last t) | |
637 (delete-backward-char 7) | |
638 (insert "'") | |
639 (forward-sexp) | |
640 (delete-char 1)) | |
641 (goto-char (1+ pt)) | |
642 (cl-do-prettyprint))) | |
643 | |
644 (defun cl-do-prettyprint () | |
645 (skip-chars-forward " ") | |
646 (if (looking-at "(") | |
647 (let ((skip (or (looking-at "((") (looking-at "(prog") | |
648 (looking-at "(unwind-protect ") | |
649 (looking-at "(function (") | |
650 (looking-at "(cl-block-wrapper "))) | |
651 (two (or (looking-at "(defun ") (looking-at "(defmacro "))) | |
652 (let (or (looking-at "(let\\*? ") (looking-at "(while "))) | |
653 (set (looking-at "(p?set[qf] "))) | |
654 (if (or skip let | |
655 (progn | |
656 (forward-sexp) | |
657 (and (>= (current-column) 78) (progn (backward-sexp) t)))) | |
658 (let ((nl t)) | |
659 (forward-char 1) | |
660 (cl-do-prettyprint) | |
661 (or skip (looking-at ")") (cl-do-prettyprint)) | |
662 (or (not two) (looking-at ")") (cl-do-prettyprint)) | |
663 (while (not (looking-at ")")) | |
664 (if set (setq nl (not nl))) | |
665 (if nl (insert "\n")) | |
666 (lisp-indent-line) | |
667 (cl-do-prettyprint)) | |
668 (forward-char 1)))) | |
669 (forward-sexp))) | |
670 | |
671 (defvar cl-macroexpand-cmacs nil) | |
672 (defvar cl-closure-vars nil) | |
673 | |
674 (defun cl-macroexpand-all (form &optional env) | |
675 "Expand all macro calls through a Lisp FORM. | |
676 This also does some trivial optimizations to make the form prettier." | |
677 (while (or (not (eq form (setq form (macroexpand form env)))) | |
678 (and cl-macroexpand-cmacs | |
679 (not (eq form (setq form (compiler-macroexpand form))))))) | |
680 (cond ((not (consp form)) form) | |
681 ((memq (car form) '(let let*)) | |
682 (if (null (nth 1 form)) | |
683 (cl-macroexpand-all (cons 'progn (cddr form)) env) | |
684 (let ((letf nil) (res nil) (lets (cadr form))) | |
685 (while lets | |
47662
a6d932b28650
(cl-push, cl-pop): Remove. Use pop and push throughout the file instead.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
45699
diff
changeset
|
686 (push (if (consp (car lets)) |
4355 | 687 (let ((exp (cl-macroexpand-all (caar lets) env))) |
688 (or (symbolp exp) (setq letf t)) | |
689 (cons exp (cl-macroexpand-body (cdar lets) env))) | |
690 (let ((exp (cl-macroexpand-all (car lets) env))) | |
691 (if (symbolp exp) exp | |
692 (setq letf t) (list exp nil)))) res) | |
693 (setq lets (cdr lets))) | |
694 (list* (if letf (if (eq (car form) 'let) 'letf 'letf*) (car form)) | |
695 (nreverse res) (cl-macroexpand-body (cddr form) env))))) | |
696 ((eq (car form) 'cond) | |
697 (cons (car form) | |
698 (mapcar (function (lambda (x) (cl-macroexpand-body x env))) | |
699 (cdr form)))) | |
700 ((eq (car form) 'condition-case) | |
701 (list* (car form) (nth 1 form) (cl-macroexpand-all (nth 2 form) env) | |
702 (mapcar (function | |
703 (lambda (x) | |
704 (cons (car x) (cl-macroexpand-body (cdr x) env)))) | |
705 (cdddr form)))) | |
706 ((memq (car form) '(quote function)) | |
707 (if (eq (car-safe (nth 1 form)) 'lambda) | |
708 (let ((body (cl-macroexpand-body (cddadr form) env))) | |
709 (if (and cl-closure-vars (eq (car form) 'function) | |
710 (cl-expr-contains-any body cl-closure-vars)) | |
711 (let* ((new (mapcar 'gensym cl-closure-vars)) | |
712 (sub (pairlis cl-closure-vars new)) (decls nil)) | |
713 (while (or (stringp (car body)) | |
714 (eq (car-safe (car body)) 'interactive)) | |
47662
a6d932b28650
(cl-push, cl-pop): Remove. Use pop and push throughout the file instead.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
45699
diff
changeset
|
715 (push (list 'quote (pop body)) decls)) |
4355 | 716 (put (car (last cl-closure-vars)) 'used t) |
717 (append | |
718 (list 'list '(quote lambda) '(quote (&rest --cl-rest--))) | |
719 (sublis sub (nreverse decls)) | |
720 (list | |
721 (list* 'list '(quote apply) | |
50802
fe838fc4d9a9
(cl-map-keymap): Redefine as alias.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
47662
diff
changeset
|
722 (list 'function |
fe838fc4d9a9
(cl-map-keymap): Redefine as alias.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
47662
diff
changeset
|
723 (list* 'lambda |
fe838fc4d9a9
(cl-map-keymap): Redefine as alias.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
47662
diff
changeset
|
724 (append new (cadadr form)) |
fe838fc4d9a9
(cl-map-keymap): Redefine as alias.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
47662
diff
changeset
|
725 (sublis sub body))) |
4355 | 726 (nconc (mapcar (function |
727 (lambda (x) | |
728 (list 'list '(quote quote) x))) | |
729 cl-closure-vars) | |
730 '((quote --cl-rest--))))))) | |
731 (list (car form) (list* 'lambda (cadadr form) body)))) | |
15029
ba44a899c055
(isqrt): Support expanded range of Lisp integers.
Richard M. Stallman <rms@gnu.org>
parents:
14794
diff
changeset
|
732 (let ((found (assq (cadr form) env))) |
51584
0fdf268507e5
(cl-macroexpand-all):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
50802
diff
changeset
|
733 (if (and found (ignore-errors |
0fdf268507e5
(cl-macroexpand-all):
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
50802
diff
changeset
|
734 (eq (cadr (caddr found)) 'cl-labels-args))) |
15029
ba44a899c055
(isqrt): Support expanded range of Lisp integers.
Richard M. Stallman <rms@gnu.org>
parents:
14794
diff
changeset
|
735 (cl-macroexpand-all (cadr (caddr (cadddr found))) env) |
ba44a899c055
(isqrt): Support expanded range of Lisp integers.
Richard M. Stallman <rms@gnu.org>
parents:
14794
diff
changeset
|
736 form)))) |
4355 | 737 ((memq (car form) '(defun defmacro)) |
738 (list* (car form) (nth 1 form) (cl-macroexpand-body (cddr form) env))) | |
739 ((and (eq (car form) 'progn) (not (cddr form))) | |
740 (cl-macroexpand-all (nth 1 form) env)) | |
741 ((eq (car form) 'setq) | |
742 (let* ((args (cl-macroexpand-body (cdr form) env)) (p args)) | |
743 (while (and p (symbolp (car p))) (setq p (cddr p))) | |
744 (if p (cl-macroexpand-all (cons 'setf args)) (cons 'setq args)))) | |
745 (t (cons (car form) (cl-macroexpand-body (cdr form) env))))) | |
746 | |
747 (defun cl-macroexpand-body (body &optional env) | |
748 (mapcar (function (lambda (x) (cl-macroexpand-all x env))) body)) | |
749 | |
750 (defun cl-prettyexpand (form &optional full) | |
751 (message "Expanding...") | |
752 (let ((cl-macroexpand-cmacs full) (cl-compiling-file full) | |
753 (byte-compile-macro-environment nil)) | |
754 (setq form (cl-macroexpand-all form | |
755 (and (not full) '((block) (eval-when))))) | |
756 (message "Formatting...") | |
757 (prog1 (cl-prettyprint form) | |
758 (message "")))) | |
759 | |
760 | |
761 | |
762 (run-hooks 'cl-extra-load-hook) | |
763 | |
64684
eb2cbda455c6
Require CL also when compiling.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
64085
diff
changeset
|
764 ;; arch-tag: bcd03437-0871-43fb-a8f1-ad0e0b5427ed |
4355 | 765 ;;; cl-extra.el ends here |