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