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