cl-extra.el 27 KB

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