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