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