Mercurial > emacs
changeset 4355:691a28818487
entered into RCS
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Fri, 30 Jul 1993 20:15:09 +0000 |
parents | 5b54255ee4b3 |
children | 3f0d5006a2c4 |
files | lisp/emacs-lisp/cl-compat.el lisp/emacs-lisp/cl-extra.el lisp/emacs-lisp/cl-macs.el lisp/emacs-lisp/cl-seq.el lisp/emacs-lisp/cl.el |
diffstat | 5 files changed, 5408 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lisp/emacs-lisp/cl-compat.el Fri Jul 30 20:15:09 1993 +0000 @@ -0,0 +1,191 @@ +;; cl-compat.el --- Common Lisp extensions for GNU Emacs Lisp (compatibility) + +;; Copyright (C) 1993 Free Software Foundation, Inc. + +;; Author: Dave Gillespie <daveg@synaptics.com> +;; Version: 2.02 +;; Keywords: extensions + +;; This file is part of GNU Emacs. + +;; GNU Emacs is free software; you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation; either version 1, or (at your option) +;; any later version. + +;; GNU Emacs is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with GNU Emacs; see the file COPYING. If not, write to +;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + +;; Commentary: + +;; These are extensions to Emacs Lisp that provide a degree of +;; Common Lisp compatibility, beyond what is already built-in +;; in Emacs Lisp. +;; +;; This package was written by Dave Gillespie; it is a complete +;; rewrite of Cesar Quiroz's original cl.el package of December 1986. +;; +;; This package works with Emacs 18, Emacs 19, and Lucid Emacs 19. +;; +;; Bug reports, comments, and suggestions are welcome! + +;; This file contains emulations of internal routines of the older +;; CL package which users may have called directly from their code. +;; Use (require 'cl-compat) to get these routines. + +;; See cl.el for Change Log. + + +;; Code: + +;; Require at load-time, but not when compiling cl-compat. +(or (featurep 'cl) (require 'cl)) + + +;;; Keyword routines not supported by new package. + +(defmacro defkeyword (x &optional doc) + (list* 'defconst x (list 'quote x) (and doc (list doc)))) + +(defun keywordp (sym) + (and (symbolp sym) (eq (aref (symbol-name sym) 0) ?\:) (set sym sym))) + +(defun keyword-of (sym) + (or (keywordp sym) (keywordp (intern (format ":%s" sym))))) + + +;;; Multiple values. Note that the new package uses a different +;;; convention for multiple values. The following definitions +;;; emulate the old convention; all function names have been changed +;;; by capitalizing the first letter: Values, Multiple-value-*, +;;; to avoid conflict with the new-style definitions in cl-macs. + +(put 'Multiple-value-bind 'lisp-indent-function 2) +(put 'Multiple-value-setq 'lisp-indent-function 2) +(put 'Multiple-value-call 'lisp-indent-function 1) +(put 'Multiple-value-prog1 'lisp-indent-function 1) + +(defvar *mvalues-values* nil) + +(defun Values (&rest val-forms) + (setq *mvalues-values* val-forms) + (car val-forms)) + +(defun Values-list (val-forms) + (apply 'values val-forms)) + +(defmacro Multiple-value-list (form) + (list 'let* (list '(*mvalues-values* nil) (list '*mvalues-temp* form)) + '(or (and (eq *mvalues-temp* (car *mvalues-values*)) *mvalues-values*) + (list *mvalues-temp*)))) + +(defmacro Multiple-value-call (function &rest args) + (list 'apply function + (cons 'append + (mapcar (function (lambda (x) (list 'Multiple-value-list x))) + args)))) + +(defmacro Multiple-value-bind (vars form &rest body) + (list* 'multiple-value-bind vars (list 'Multiple-value-list form) body)) + +(defmacro Multiple-value-setq (vars form) + (list 'multiple-value-setq vars (list 'Multiple-value-list form))) + +(defmacro Multiple-value-prog1 (form &rest body) + (list 'prog1 form (list* 'let '((*mvalues-values* nil)) body))) + + +;;; Routines for parsing keyword arguments. + +(defun build-klist (arglist keys &optional allow-others) + (let ((res (Multiple-value-call 'mapcar* 'cons (unzip-lists arglist)))) + (or allow-others + (let ((bad (set-difference (mapcar 'car res) keys))) + (if bad (error "Bad keywords: %s not in %s" bad keys)))) + res)) + +(defun extract-from-klist (klist key &optional def) + (let ((res (assq key klist))) (if res (cdr res) def))) + +(defun keyword-argument-supplied-p (klist key) + (assq key klist)) + +(defun elt-satisfies-test-p (item elt klist) + (let ((test-not (cdr (assq ':test-not klist))) + (test (cdr (assq ':test klist))) + (key (cdr (assq ':key klist)))) + (if key (setq elt (funcall key elt))) + (if test-not (not (funcall test-not item elt)) + (funcall (or test 'eql) item elt)))) + + +;;; Rounding functions with old-style multiple value returns. + +(defun cl-floor (a &optional b) (Values-list (floor* a b))) +(defun cl-ceiling (a &optional b) (Values-list (ceiling* a b))) +(defun cl-round (a &optional b) (Values-list (round* a b))) +(defun cl-truncate (a &optional b) (Values-list (truncate* a b))) + +(defun safe-idiv (a b) + (let* ((q (/ (abs a) (abs b))) + (s (* (signum a) (signum b)))) + (Values q (- a (* s q b)) s))) + + +;; Internal routines. + +(defun pair-with-newsyms (oldforms) + (let ((newsyms (mapcar (function (lambda (x) (gensym))) oldforms))) + (Values (mapcar* 'list newsyms oldforms) newsyms))) + +(defun zip-lists (evens odds) + (mapcan 'list evens odds)) + +(defun unzip-lists (list) + (let ((e nil) (o nil)) + (while list + (setq e (cons (car list) e) o (cons (cadr list) o) list (cddr list))) + (Values (nreverse e) (nreverse o)))) + +(defun reassemble-argslists (list) + (let ((n (apply 'min (mapcar 'length list))) (res nil)) + (while (>= (setq n (1- n)) 0) + (setq res (cons (mapcar (function (lambda (x) (elt x n))) list) res))) + res)) + +(defun duplicate-symbols-p (list) + (let ((res nil)) + (while list + (if (memq (car list) (cdr list)) (setq res (cons (car list) res))) + (setq list (cdr list))) + res)) + + +;;; Setf internals. + +(defun setnth (n list x) + (setcar (nthcdr n list) x)) + +(defun setnthcdr (n list x) + (setcdr (nthcdr (1- n) list) x)) + +(defun setelt (seq n x) + (if (consp seq) (setcar (nthcdr n seq) x) (aset seq n x))) + + +;;; Functions omitted: case-clausify, check-do-stepforms, check-do-endforms, +;;; extract-do-inits, extract-do[*]-steps, select-stepping-forms, +;;; elt-satisfies-if[-not]-p, with-keyword-args, mv-bind-clausify, +;;; all names with embedded `$'. + + +(provide 'cl-compat) + +;;; cl-compat.el ends here +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lisp/emacs-lisp/cl-extra.el Fri Jul 30 20:15:09 1993 +0000 @@ -0,0 +1,930 @@ +;; cl-extra.el --- Common Lisp extensions for GNU Emacs Lisp (part two) + +;; Copyright (C) 1993 Free Software Foundation, Inc. + +;; Author: Dave Gillespie <daveg@synaptics.com> +;; Version: 2.02 +;; Keywords: extensions + +;; This file is part of GNU Emacs. + +;; GNU Emacs is free software; you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation; either version 1, or (at your option) +;; any later version. + +;; GNU Emacs is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with GNU Emacs; see the file COPYING. If not, write to +;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + +;; Commentary: + +;; These are extensions to Emacs Lisp that provide a degree of +;; Common Lisp compatibility, beyond what is already built-in +;; in Emacs Lisp. +;; +;; This package was written by Dave Gillespie; it is a complete +;; rewrite of Cesar Quiroz's original cl.el package of December 1986. +;; +;; This package works with Emacs 18, Emacs 19, and Lucid Emacs 19. +;; +;; Bug reports, comments, and suggestions are welcome! + +;; This file contains portions of the Common Lisp extensions +;; package which are autoloaded since they are relatively obscure. + +;; See cl.el for Change Log. + + +;; Code: + +(or (memq 'cl-19 features) + (error "Tried to load `cl-extra' before `cl'!")) + + +;;; We define these here so that this file can compile without having +;;; loaded the cl.el file already. + +(defmacro cl-push (x place) (list 'setq place (list 'cons x place))) +(defmacro cl-pop (place) + (list 'car (list 'prog1 place (list 'setq place (list 'cdr place))))) + +(defvar cl-emacs-type) + + +;;; Type coercion. + +(defun coerce (x type) + "Coerce OBJECT to type TYPE. +TYPE is a Common Lisp type specifier." + (cond ((eq type 'list) (if (listp x) x (append x nil))) + ((eq type 'vector) (if (vectorp x) x (vconcat x))) + ((eq type 'string) (if (stringp x) x (concat x))) + ((eq type 'array) (if (arrayp x) x (vconcat x))) + ((and (eq type 'character) (stringp x) (= (length x) 1)) (aref x 0)) + ((and (eq type 'character) (symbolp x)) (coerce (symbol-name x) type)) + ((eq type 'float) (float x)) + ((typep x type) x) + (t (error "Can't coerce %s to type %s" x type)))) + + +;;; Predicates. + +(defun equalp (x y) + "T if two Lisp objects have similar structures and contents. +This is like `equal', except that it accepts numerically equal +numbers of different types (float vs. integer), and also compares +strings case-insensitively." + (cond ((eq x y) t) + ((stringp x) + (and (stringp y) (= (length x) (length y)) + (or (equal x y) + (equal (downcase x) (downcase y))))) ; lazy but simple! + ((numberp x) + (and (numberp y) (= x y))) + ((consp x) + (while (and (consp x) (consp y) (equalp (cl-pop x) (cl-pop y)))) + (and (not (consp x)) (equalp x y))) + ((vectorp x) + (and (vectorp y) (= (length x) (length y)) + (let ((i (length x))) + (while (and (>= (setq i (1- i)) 0) + (equalp (aref x i) (aref y i)))) + (< i 0)))) + (t (equal x y)))) + + +;;; Control structures. + +(defun cl-mapcar-many (cl-func cl-seqs) + (if (cdr (cdr cl-seqs)) + (let* ((cl-res nil) + (cl-n (apply 'min (mapcar 'length cl-seqs))) + (cl-i 0) + (cl-args (copy-sequence cl-seqs)) + cl-p1 cl-p2) + (setq cl-seqs (copy-sequence cl-seqs)) + (while (< cl-i cl-n) + (setq cl-p1 cl-seqs cl-p2 cl-args) + (while cl-p1 + (setcar cl-p2 + (if (consp (car cl-p1)) + (prog1 (car (car cl-p1)) + (setcar cl-p1 (cdr (car cl-p1)))) + (aref (car cl-p1) cl-i))) + (setq cl-p1 (cdr cl-p1) cl-p2 (cdr cl-p2))) + (cl-push (apply cl-func cl-args) cl-res) + (setq cl-i (1+ cl-i))) + (nreverse cl-res)) + (let ((cl-res nil) + (cl-x (car cl-seqs)) + (cl-y (nth 1 cl-seqs))) + (let ((cl-n (min (length cl-x) (length cl-y))) + (cl-i -1)) + (while (< (setq cl-i (1+ cl-i)) cl-n) + (cl-push (funcall cl-func + (if (consp cl-x) (cl-pop cl-x) (aref cl-x cl-i)) + (if (consp cl-y) (cl-pop cl-y) (aref cl-y cl-i))) + cl-res))) + (nreverse cl-res)))) + +(defun map (cl-type cl-func cl-seq &rest cl-rest) + "Map a function across one or more sequences, returning a sequence. +TYPE is the sequence type to return, FUNC is the function, and SEQS +are the argument sequences." + (let ((cl-res (apply 'mapcar* cl-func cl-seq cl-rest))) + (and cl-type (coerce cl-res cl-type)))) + +(defun maplist (cl-func cl-list &rest cl-rest) + "Map FUNC to each sublist of LIST or LISTS. +Like `mapcar', except applies to lists and their cdr's rather than to +the elements themselves." + (if cl-rest + (let ((cl-res nil) + (cl-args (cons cl-list (copy-sequence cl-rest))) + cl-p) + (while (not (memq nil cl-args)) + (cl-push (apply cl-func cl-args) cl-res) + (setq cl-p cl-args) + (while cl-p (setcar cl-p (cdr (cl-pop cl-p)) ))) + (nreverse cl-res)) + (let ((cl-res nil)) + (while cl-list + (cl-push (funcall cl-func cl-list) cl-res) + (setq cl-list (cdr cl-list))) + (nreverse cl-res)))) + +(defun mapc (cl-func cl-seq &rest cl-rest) + "Like `mapcar', but does not accumulate values returned by the function." + (if cl-rest + (apply 'map nil cl-func cl-seq cl-rest) + (mapcar cl-func cl-seq)) + cl-seq) + +(defun mapl (cl-func cl-list &rest cl-rest) + "Like `maplist', but does not accumulate values returned by the function." + (if cl-rest + (apply 'maplist cl-func cl-list cl-rest) + (let ((cl-p cl-list)) + (while cl-p (funcall cl-func cl-p) (setq cl-p (cdr cl-p))))) + cl-list) + +(defun mapcan (cl-func cl-seq &rest cl-rest) + "Like `mapcar', but nconc's together the values returned by the function." + (apply 'nconc (apply 'mapcar* cl-func cl-seq cl-rest))) + +(defun mapcon (cl-func cl-list &rest cl-rest) + "Like `maplist', but nconc's together the values returned by the function." + (apply 'nconc (apply 'maplist cl-func cl-list cl-rest))) + +(defun some (cl-pred cl-seq &rest cl-rest) + "Return true if PREDICATE is true of any element of SEQ or SEQs. +If so, return the true (non-nil) value returned by PREDICATE." + (if (or cl-rest (nlistp cl-seq)) + (catch 'cl-some + (apply 'map nil + (function (lambda (&rest cl-x) + (let ((cl-res (apply cl-pred cl-x))) + (if cl-res (throw 'cl-some cl-res))))) + cl-seq cl-rest) nil) + (let ((cl-x nil)) + (while (and cl-seq (not (setq cl-x (funcall cl-pred (cl-pop cl-seq)))))) + cl-x))) + +(defun every (cl-pred cl-seq &rest cl-rest) + "Return true if PREDICATE is true of every element of SEQ or SEQs." + (if (or cl-rest (nlistp cl-seq)) + (catch 'cl-every + (apply 'map nil + (function (lambda (&rest cl-x) + (or (apply cl-pred cl-x) (throw 'cl-every nil)))) + cl-seq cl-rest) t) + (while (and cl-seq (funcall cl-pred (car cl-seq))) + (setq cl-seq (cdr cl-seq))) + (null cl-seq))) + +(defun notany (cl-pred cl-seq &rest cl-rest) + "Return true if PREDICATE is false of every element of SEQ or SEQs." + (not (apply 'some cl-pred cl-seq cl-rest))) + +(defun notevery (cl-pred cl-seq &rest cl-rest) + "Return true if PREDICATE is false of some element of SEQ or SEQs." + (not (apply 'every cl-pred cl-seq cl-rest))) + +;;; Support for `loop'. +(defun cl-map-keymap (cl-func cl-map) + (while (symbolp cl-map) (setq cl-map (symbol-function cl-map))) + (if (eq cl-emacs-type 'lucid) (funcall 'map-keymap cl-func cl-map) + (if (listp cl-map) + (let ((cl-p cl-map)) + (while (consp (setq cl-p (cdr cl-p))) + (cond ((consp (car cl-p)) + (funcall cl-func (car (car cl-p)) (cdr (car cl-p)))) + ((vectorp (car cl-p)) + (cl-map-keymap cl-func (car cl-p))) + ((eq (car cl-p) 'keymap) + (setq cl-p nil))))) + (let ((cl-i -1)) + (while (< (setq cl-i (1+ cl-i)) (length cl-map)) + (if (aref cl-map cl-i) + (funcall cl-func cl-i (aref cl-map cl-i)))))))) + +(defun cl-map-keymap-recursively (cl-func-rec cl-map &optional cl-base) + (or cl-base + (setq cl-base (copy-sequence (if (eq cl-emacs-type 18) "0" [0])))) + (cl-map-keymap + (function + (lambda (cl-key cl-bind) + (aset cl-base (1- (length cl-base)) cl-key) + (if (keymapp cl-bind) + (cl-map-keymap-recursively + cl-func-rec cl-bind + (funcall (if (eq cl-emacs-type 18) 'concat 'vconcat) + cl-base (list 0))) + (funcall cl-func-rec cl-base cl-bind)))) + cl-map)) + +(defun cl-map-intervals (cl-func &optional cl-what cl-prop cl-start cl-end) + (or cl-what (setq cl-what (current-buffer))) + (if (bufferp cl-what) + (let (cl-mark cl-mark2 (cl-next t) cl-next2) + (save-excursion + (set-buffer cl-what) + (setq cl-mark (copy-marker (or cl-start (point-min)))) + (setq cl-mark2 (and cl-end (copy-marker cl-end)))) + (while (and cl-next (or (not cl-mark2) (< cl-mark cl-mark2))) + (setq cl-next (and (fboundp 'next-property-change) + (if cl-prop (next-single-property-change + cl-mark cl-prop cl-what) + (next-property-change cl-mark cl-what))) + cl-next2 (or cl-next (save-excursion + (set-buffer cl-what) (point-max)))) + (funcall cl-func (prog1 (marker-position cl-mark) + (set-marker cl-mark cl-next2)) + (if cl-mark2 (min cl-next2 cl-mark2) cl-next2))) + (set-marker cl-mark nil) (if cl-mark2 (set-marker cl-mark2 nil))) + (or cl-start (setq cl-start 0)) + (or cl-end (setq cl-end (length cl-what))) + (while (< cl-start cl-end) + (let ((cl-next (or (and (fboundp 'next-property-change) + (if cl-prop (next-single-property-change + cl-start cl-prop cl-what) + (next-property-change cl-start cl-what))) + cl-end))) + (funcall cl-func cl-start (min cl-next cl-end)) + (setq cl-start cl-next))))) + +(defun cl-map-overlays (cl-func &optional cl-buffer cl-start cl-end cl-arg) + (or cl-buffer (setq cl-buffer (current-buffer))) + (if (fboundp 'overlay-lists) + + ;; This is the preferred algorithm, though overlay-lists is undocumented. + (let (cl-ovl) + (save-excursion + (set-buffer cl-buffer) + (setq cl-ovl (overlay-lists)) + (if cl-start (setq cl-start (copy-marker cl-start))) + (if cl-end (setq cl-end (copy-marker cl-end)))) + (setq cl-ovl (nconc (car cl-ovl) (cdr cl-ovl))) + (while (and cl-ovl + (or (not (overlay-start (car cl-ovl))) + (and cl-end (>= (overlay-start (car cl-ovl)) cl-end)) + (and cl-start (<= (overlay-end (car cl-ovl)) cl-start)) + (not (funcall cl-func (car cl-ovl) cl-arg)))) + (setq cl-ovl (cdr cl-ovl))) + (if cl-start (set-marker cl-start nil)) + (if cl-end (set-marker cl-end nil))) + + ;; This alternate algorithm fails to find zero-length overlays. + (let ((cl-mark (save-excursion (set-buffer cl-buffer) + (copy-marker (or cl-start (point-min))))) + (cl-mark2 (and cl-end (save-excursion (set-buffer cl-buffer) + (copy-marker cl-end)))) + cl-pos cl-ovl) + (while (save-excursion + (and (setq cl-pos (marker-position cl-mark)) + (< cl-pos (or cl-mark2 (point-max))) + (progn + (set-buffer cl-buffer) + (setq cl-ovl (overlays-at cl-pos)) + (set-marker cl-mark (next-overlay-change cl-pos))))) + (while (and cl-ovl + (or (/= (overlay-start (car cl-ovl)) cl-pos) + (not (and (funcall cl-func (car cl-ovl) cl-arg) + (set-marker cl-mark nil))))) + (setq cl-ovl (cdr cl-ovl)))) + (set-marker cl-mark nil) (if cl-mark2 (set-marker cl-mark2 nil))))) + +;;; Support for `setf'. +(defun cl-set-frame-visible-p (frame val) + (cond ((null val) (make-frame-invisible frame)) + ((eq val 'icon) (iconify-frame frame)) + (t (make-frame-visible frame))) + val) + +;;; Support for `progv'. +(defvar cl-progv-save) +(defun cl-progv-before (syms values) + (while syms + (cl-push (if (boundp (car syms)) + (cons (car syms) (symbol-value (car syms))) + (car syms)) cl-progv-save) + (if values + (set (cl-pop syms) (cl-pop values)) + (makunbound (cl-pop syms))))) + +(defun cl-progv-after () + (while cl-progv-save + (if (consp (car cl-progv-save)) + (set (car (car cl-progv-save)) (cdr (car cl-progv-save))) + (makunbound (car cl-progv-save))) + (cl-pop cl-progv-save))) + + +;;; Numbers. + +(defun gcd (&rest args) + "Return the greatest common divisor of the arguments." + (let ((a (abs (or (cl-pop args) 0)))) + (while args + (let ((b (abs (cl-pop args)))) + (while (> b 0) (setq b (% a (setq a b)))))) + a)) + +(defun lcm (&rest args) + "Return the least common multiple of the arguments." + (if (memq 0 args) + 0 + (let ((a (abs (or (cl-pop args) 1)))) + (while args + (let ((b (abs (cl-pop args)))) + (setq a (* (/ a (gcd a b)) b)))) + a))) + +(defun isqrt (a) + "Return the integer square root of the argument." + (if (and (integerp a) (> a 0)) + (let ((g (cond ((>= a 1000000) 10000) ((>= a 10000) 1000) + ((>= a 100) 100) (t 10))) + g2) + (while (< (setq g2 (/ (+ g (/ a g)) 2)) g) + (setq g g2)) + g) + (if (eq a 0) 0 (signal 'arith-error nil)))) + +(defun cl-expt (x y) + "Return X raised to the power of Y. Works only for integer arguments." + (if (<= y 0) (if (= y 0) 1 (if (memq x '(-1 1)) x 0)) + (* (if (= (% y 2) 0) 1 x) (cl-expt (* x x) (/ y 2))))) +(or (and (fboundp 'expt) (subrp (symbol-function 'expt))) + (defalias 'expt 'cl-expt)) + +(defun floor* (x &optional y) + "Return a list of the floor of X and the fractional part of X. +With two arguments, return floor and remainder of their quotient." + (if y + (if (and (integerp x) (integerp y)) + (if (and (>= x 0) (>= y 0)) + (list (/ x y) (% x y)) + (let ((q (cond ((>= x 0) (- (/ (- x y 1) (- y)))) + ((>= y 0) (- (/ (- y x 1) y))) + (t (/ (- x) (- y)))))) + (list q (- x (* q y))))) + (let ((q (floor (/ x y)))) + (list q (- x (* q y))))) + (if (integerp x) (list x 0) + (let ((q (floor x))) + (list q (- x q)))))) + +(defun ceiling* (x &optional y) + "Return a list of the ceiling of X and the fractional part of X. +With two arguments, return ceiling and remainder of their quotient." + (let ((res (floor* x y))) + (if (= (car (cdr res)) 0) res + (list (1+ (car res)) (- (car (cdr res)) (or y 1)))))) + +(defun truncate* (x &optional y) + "Return a list of the integer part of X and the fractional part of X. +With two arguments, return truncation and remainder of their quotient." + (if (eq (>= x 0) (or (null y) (>= y 0))) + (floor* x y) (ceiling* x y))) + +(defun round* (x &optional y) + "Return a list of X rounded to the nearest integer and the remainder. +With two arguments, return rounding and remainder of their quotient." + (if y + (if (and (integerp x) (integerp y)) + (let* ((hy (/ y 2)) + (res (floor* (+ x hy) y))) + (if (and (= (car (cdr res)) 0) + (= (+ hy hy) y) + (/= (% (car res) 2) 0)) + (list (1- (car res)) hy) + (list (car res) (- (car (cdr res)) hy)))) + (let ((q (round (/ x y)))) + (list q (- x (* q y))))) + (if (integerp x) (list x 0) + (let ((q (round x))) + (list q (- x q)))))) + +(defun mod* (x y) + "The remainder of X divided by Y, with the same sign as Y." + (nth 1 (floor* x y))) + +(defun rem* (x y) + "The remainder of X divided by Y, with the same sign as X." + (nth 1 (truncate* x y))) + +(defun signum (a) + "Return 1 if A is positive, -1 if negative, 0 if zero." + (cond ((> a 0) 1) ((< a 0) -1) (t 0))) + + +;; Random numbers. + +(defvar *random-state*) +(defun random* (lim &optional state) + "Return a random nonnegative number less than LIM, an integer or float. +Optional second arg STATE is a random-state object." + (or state (setq state *random-state*)) + ;; Inspired by "ran3" from Numerical Recipes. Additive congruential method. + (let ((vec (aref state 3))) + (if (integerp vec) + (let ((i 0) (j (- 1357335 (% (abs vec) 1357333))) (k 1) ii) + (aset state 3 (setq vec (make-vector 55 nil))) + (aset vec 0 j) + (while (> (setq i (% (+ i 21) 55)) 0) + (aset vec i (setq j (prog1 k (setq k (- j k)))))) + (while (< (setq i (1+ i)) 200) (random* 2 state)))) + (let* ((i (aset state 1 (% (1+ (aref state 1)) 55))) + (j (aset state 2 (% (1+ (aref state 2)) 55))) + (n (logand 8388607 (aset vec i (- (aref vec i) (aref vec j)))))) + (if (integerp lim) + (if (<= lim 512) (% n lim) + (if (> lim 8388607) (setq n (+ (lsh n 9) (random* 512 state)))) + (let ((mask 1023)) + (while (< mask (1- lim)) (setq mask (1+ (+ mask mask)))) + (if (< (setq n (logand n mask)) lim) n (random* lim state)))) + (* (/ n '8388608e0) lim))))) + +(defun make-random-state (&optional state) + "Return a copy of random-state STATE, or of `*random-state*' if omitted. +If STATE is t, return a new state object seeded from the time of day." + (cond ((null state) (make-random-state *random-state*)) + ((vectorp state) (cl-copy-tree state t)) + ((integerp state) (vector 'cl-random-state-tag -1 30 state)) + (t (make-random-state (cl-random-time))))) + +(defun random-state-p (object) + "Return t if OBJECT is a random-state object." + (and (vectorp object) (= (length object) 4) + (eq (aref object 0) 'cl-random-state-tag))) + + +;; Implementation limits. + +(defun cl-finite-do (func a b) + (condition-case err + (let ((res (funcall func a b))) ; check for IEEE infinity + (and (numberp res) (/= res (/ res 2)) res)) + (arith-error nil))) + +(defvar most-positive-float) +(defvar most-negative-float) +(defvar least-positive-float) +(defvar least-negative-float) +(defvar least-positive-normalized-float) +(defvar least-negative-normalized-float) +(defvar float-epsilon) +(defvar float-negative-epsilon) + +(defun cl-float-limits () + (or most-positive-float (not (numberp '2e1)) + (let ((x '2e0) y z) + ;; Find maximum exponent (first two loops are optimizations) + (while (cl-finite-do '* x x) (setq x (* x x))) + (while (cl-finite-do '* x (/ x 2)) (setq x (* x (/ x 2)))) + (while (cl-finite-do '+ x x) (setq x (+ x x))) + (setq z x y (/ x 2)) + ;; Now fill in 1's in the mantissa. + (while (and (cl-finite-do '+ x y) (/= (+ x y) x)) + (setq x (+ x y) y (/ y 2))) + (setq most-positive-float x + most-negative-float (- x)) + ;; Divide down until mantissa starts rounding. + (setq x (/ x z) y (/ 16 z) x (* x y)) + (while (condition-case err (and (= x (* (/ x 2) 2)) (> (/ y 2) 0)) + (arith-error nil)) + (setq x (/ x 2) y (/ y 2))) + (setq least-positive-normalized-float y + least-negative-normalized-float (- y)) + ;; Divide down until value underflows to zero. + (setq x (/ 1 z) y x) + (while (condition-case err (> (/ x 2) 0) (arith-error nil)) + (setq x (/ x 2))) + (setq least-positive-float x + least-negative-float (- x)) + (setq x '1e0) + (while (/= (+ '1e0 x) '1e0) (setq x (/ x 2))) + (setq float-epsilon (* x 2)) + (setq x '1e0) + (while (/= (- '1e0 x) '1e0) (setq x (/ x 2))) + (setq float-negative-epsilon (* x 2)))) + nil) + + +;;; Sequence functions. + +(defun subseq (seq start &optional end) + "Return the subsequence of SEQ from START to END. +If END is omitted, it defaults to the length of the sequence. +If START or END is negative, it counts from the end." + (if (stringp seq) (substring seq start end) + (let (len) + (and end (< end 0) (setq end (+ end (setq len (length seq))))) + (if (< start 0) (setq start (+ start (or len (setq len (length seq)))))) + (cond ((listp seq) + (if (> start 0) (setq seq (nthcdr start seq))) + (if end + (let ((res nil)) + (while (>= (setq end (1- end)) start) + (cl-push (cl-pop seq) res)) + (nreverse res)) + (copy-sequence seq))) + (t + (or end (setq end (or len (length seq)))) + (let ((res (make-vector (max (- end start) 0) nil)) + (i 0)) + (while (< start end) + (aset res i (aref seq start)) + (setq i (1+ i) start (1+ start))) + res)))))) + +(defun concatenate (type &rest seqs) + "Concatenate, into a sequence of type TYPE, the argument SEQUENCES." + (cond ((eq type 'vector) (apply 'vconcat seqs)) + ((eq type 'string) (apply 'concat seqs)) + ((eq type 'list) (apply 'append (append seqs '(nil)))) + (t (error "Not a sequence type name: %s" type)))) + + +;;; List functions. + +(defun revappend (x y) + "Equivalent to (append (reverse X) Y)." + (nconc (reverse x) y)) + +(defun nreconc (x y) + "Equivalent to (nconc (nreverse X) Y)." + (nconc (nreverse x) y)) + +(defun list-length (x) + "Return the length of a list. Return nil if list is circular." + (let ((n 0) (fast x) (slow x)) + (while (and (cdr fast) (not (and (eq fast slow) (> n 0)))) + (setq n (+ n 2) fast (cdr (cdr fast)) slow (cdr slow))) + (if fast (if (cdr fast) nil (1+ n)) n))) + +(defun tailp (sublist list) + "Return true if SUBLIST is a tail of LIST." + (while (and (consp list) (not (eq sublist list))) + (setq list (cdr list))) + (if (numberp sublist) (equal sublist list) (eq sublist list))) + +(defun cl-copy-tree (tree &optional vecp) + "Make a copy of TREE. +If TREE is a cons cell, this recursively copies both its car and its cdr. +Constrast to copy-sequence, which copies only along the cdrs. With second +argument VECP, this copies vectors as well as conses." + (if (consp tree) + (let ((p (setq tree (copy-list tree)))) + (while (consp p) + (if (or (consp (car p)) (and vecp (vectorp (car p)))) + (setcar p (cl-copy-tree (car p) vecp))) + (or (listp (cdr p)) (setcdr p (cl-copy-tree (cdr p) vecp))) + (cl-pop p))) + (if (and vecp (vectorp tree)) + (let ((i (length (setq tree (copy-sequence tree))))) + (while (>= (setq i (1- i)) 0) + (aset tree i (cl-copy-tree (aref tree i) vecp)))))) + tree) +(or (and (fboundp 'copy-tree) (subrp (symbol-function 'copy-tree))) + (defalias 'copy-tree 'cl-copy-tree)) + + +;;; Property lists. + +(defun get* (sym tag &optional def) ; See compiler macro in cl-macs.el + "Return the value of SYMBOL's PROPNAME property, or DEFAULT if none." + (or (get sym tag) + (and def + (let ((plist (symbol-plist sym))) + (while (and plist (not (eq (car plist) tag))) + (setq plist (cdr (cdr plist)))) + (if plist (car (cdr plist)) def))))) + +(defun getf (plist tag &optional def) + "Search PROPLIST for property PROPNAME; return its value or DEFAULT. +PROPLIST is a list of the sort returned by `symbol-plist'." + (setplist '--cl-getf-symbol-- plist) + (or (get '--cl-getf-symbol-- tag) + (and def (get* '--cl-getf-symbol-- tag def)))) + +(defun cl-set-getf (plist tag val) + (let ((p plist)) + (while (and p (not (eq (car p) tag))) (setq p (cdr (cdr p)))) + (if p (progn (setcar (cdr p) val) plist) (list* tag val plist)))) + +(defun cl-do-remf (plist tag) + (let ((p (cdr plist))) + (while (and (cdr p) (not (eq (car (cdr p)) tag))) (setq p (cdr (cdr p)))) + (and (cdr p) (progn (setcdr p (cdr (cdr (cdr p)))) t)))) + +(defun cl-remprop (sym tag) + "Remove from SYMBOL's plist the property PROP and its value." + (let ((plist (symbol-plist sym))) + (if (and plist (eq tag (car plist))) + (progn (setplist sym (cdr (cdr plist))) t) + (cl-do-remf plist tag)))) +(or (and (fboundp 'remprop) (subrp (symbol-function 'remprop))) + (defalias 'remprop 'cl-remprop)) + + + +;;; Hash tables. + +(defun make-hash-table (&rest cl-keys) + "Make an empty Common Lisp-style hash-table. +If :test is `eq', this can use Lucid Emacs built-in hash-tables. +In non-Lucid Emacs, or with non-`eq' test, this internally uses a-lists. +Keywords supported: :test :size +The Common Lisp keywords :rehash-size and :rehash-threshold are ignored." + (let ((cl-test (or (car (cdr (memq ':test cl-keys))) 'eql)) + (cl-size (or (car (cdr (memq ':size cl-keys))) 20))) + (if (and (eq cl-test 'eq) (fboundp 'make-hashtable)) + (funcall 'make-hashtable cl-size) + (list 'cl-hash-table-tag cl-test + (if (> cl-size 1) (make-vector cl-size 0) + (let ((sym (make-symbol "--hashsym--"))) (set sym nil) sym)) + 0)))) + +(defvar cl-lucid-hash-tag + (if (and (fboundp 'make-hashtable) (vectorp (make-hashtable 1))) + (aref (make-hashtable 1) 0) (make-symbol "--cl-hash-tag--"))) + +(defun hash-table-p (x) + "Return t if OBJECT is a hash table." + (or (eq (car-safe x) 'cl-hash-table-tag) + (and (vectorp x) (= (length x) 4) (eq (aref x 0) cl-lucid-hash-tag)) + (and (fboundp 'hashtablep) (funcall 'hashtablep x)))) + +(defun cl-not-hash-table (x &optional y &rest z) + (signal 'wrong-type-argument (list 'hash-table-p (or y x)))) + +(defun cl-hash-lookup (key table) + (or (eq (car-safe table) 'cl-hash-table-tag) (cl-not-hash-table table)) + (let* ((array (nth 2 table)) (test (car (cdr table))) (str key) sym) + (if (symbolp array) (setq str nil sym (symbol-value array)) + (while (or (consp str) (and (vectorp str) (> (length str) 0))) + (setq str (elt str 0))) + (cond ((stringp str) (if (eq test 'equalp) (setq str (downcase str)))) + ((symbolp str) (setq str (symbol-name str))) + ((and (numberp str) (> str -8000000) (< str 8000000)) + (or (integerp str) (setq str (truncate str))) + (setq str (aref ["0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" + "11" "12" "13" "14" "15"] (logand str 15)))) + (t (setq str "*"))) + (setq sym (symbol-value (intern-soft str array)))) + (list (and sym (cond ((or (eq test 'eq) + (and (eq test 'eql) (not (numberp key)))) + (assq key sym)) + ((memq test '(eql equal)) (assoc key sym)) + (t (assoc* key sym ':test test)))) + sym str))) + +(defvar cl-builtin-gethash + (if (and (fboundp 'gethash) (subrp (symbol-function 'gethash))) + (symbol-function 'gethash) 'cl-not-hash-table)) +(defvar cl-builtin-remhash + (if (and (fboundp 'remhash) (subrp (symbol-function 'remhash))) + (symbol-function 'remhash) 'cl-not-hash-table)) +(defvar cl-builtin-clrhash + (if (and (fboundp 'clrhash) (subrp (symbol-function 'clrhash))) + (symbol-function 'clrhash) 'cl-not-hash-table)) +(defvar cl-builtin-maphash + (if (and (fboundp 'maphash) (subrp (symbol-function 'maphash))) + (symbol-function 'maphash) 'cl-not-hash-table)) + +(defun cl-gethash (key table &optional def) + "Look up KEY in HASH-TABLE; return corresponding value, or DEFAULT." + (if (consp table) + (let ((found (cl-hash-lookup key table))) + (if (car found) (cdr (car found)) def)) + (funcall cl-builtin-gethash key table def))) +(defalias 'gethash 'cl-gethash) + +(defun cl-puthash (key val table) + (if (consp table) + (let ((found (cl-hash-lookup key table))) + (if (car found) (setcdr (car found) val) + (if (nth 2 found) + (progn + (if (> (nth 3 table) (* (length (nth 2 table)) 3)) + (let ((new-table (make-vector (nth 3 table) 0))) + (mapatoms (function + (lambda (sym) + (set (intern (symbol-name sym) new-table) + (symbol-value sym)))) + (nth 2 table)) + (setcar (cdr (cdr table)) new-table))) + (set (intern (nth 2 found) (nth 2 table)) + (cons (cons key val) (nth 1 found)))) + (set (nth 2 table) (cons (cons key val) (nth 1 found)))) + (setcar (cdr (cdr (cdr table))) (1+ (nth 3 table))))) + (funcall 'puthash key val table)) val) + +(defun cl-remhash (key table) + "Remove KEY from HASH-TABLE." + (if (consp table) + (let ((found (cl-hash-lookup key table))) + (and (car found) + (let ((del (delq (car found) (nth 1 found)))) + (setcar (cdr (cdr (cdr table))) (1- (nth 3 table))) + (if (nth 2 found) (set (intern (nth 2 found) (nth 2 table)) del) + (set (nth 2 table) del)) t))) + (prog1 (not (eq (funcall cl-builtin-gethash key table '--cl--) '--cl--)) + (funcall cl-builtin-remhash key table)))) +(defalias 'remhash 'cl-remhash) + +(defun cl-clrhash (table) + "Clear HASH-TABLE." + (if (consp table) + (progn + (or (hash-table-p table) (cl-not-hash-table table)) + (if (symbolp (nth 2 table)) (set (nth 2 table) nil) + (setcar (cdr (cdr table)) (make-vector (length (nth 2 table)) 0))) + (setcar (cdr (cdr (cdr table))) 0)) + (funcall cl-builtin-clrhash table)) + nil) +(defalias 'clrhash 'cl-clrhash) + +(defun cl-maphash (cl-func cl-table) + "Call FUNCTION on keys and values from HASH-TABLE." + (or (hash-table-p cl-table) (cl-not-hash-table cl-table)) + (if (consp cl-table) + (mapatoms (function (lambda (cl-x) + (setq cl-x (symbol-value cl-x)) + (while cl-x + (funcall cl-func (car (car cl-x)) + (cdr (car cl-x))) + (setq cl-x (cdr cl-x))))) + (if (symbolp (nth 2 cl-table)) + (vector (nth 2 cl-table)) (nth 2 cl-table))) + (funcall cl-builtin-maphash cl-func cl-table))) +(defalias 'maphash 'cl-maphash) + +(defun hash-table-count (table) + "Return the number of entries in HASH-TABLE." + (or (hash-table-p table) (cl-not-hash-table table)) + (if (consp table) (nth 3 table) (funcall 'hashtable-fullness table))) + + +;;; Some debugging aids. + +(defun cl-prettyprint (form) + "Insert a pretty-printed rendition of a Lisp FORM in current buffer." + (let ((pt (point)) last) + (insert "\n" (prin1-to-string form) "\n") + (setq last (point)) + (goto-char (1+ pt)) + (while (search-forward "(quote " last t) + (delete-backward-char 7) + (insert "'") + (forward-sexp) + (delete-char 1)) + (goto-char (1+ pt)) + (cl-do-prettyprint))) + +(defun cl-do-prettyprint () + (skip-chars-forward " ") + (if (looking-at "(") + (let ((skip (or (looking-at "((") (looking-at "(prog") + (looking-at "(unwind-protect ") + (looking-at "(function (") + (looking-at "(cl-block-wrapper "))) + (two (or (looking-at "(defun ") (looking-at "(defmacro "))) + (let (or (looking-at "(let\\*? ") (looking-at "(while "))) + (set (looking-at "(p?set[qf] "))) + (if (or skip let + (progn + (forward-sexp) + (and (>= (current-column) 78) (progn (backward-sexp) t)))) + (let ((nl t)) + (forward-char 1) + (cl-do-prettyprint) + (or skip (looking-at ")") (cl-do-prettyprint)) + (or (not two) (looking-at ")") (cl-do-prettyprint)) + (while (not (looking-at ")")) + (if set (setq nl (not nl))) + (if nl (insert "\n")) + (lisp-indent-line) + (cl-do-prettyprint)) + (forward-char 1)))) + (forward-sexp))) + +(defvar cl-macroexpand-cmacs nil) +(defvar cl-closure-vars nil) + +(defun cl-macroexpand-all (form &optional env) + "Expand all macro calls through a Lisp FORM. +This also does some trivial optimizations to make the form prettier." + (while (or (not (eq form (setq form (macroexpand form env)))) + (and cl-macroexpand-cmacs + (not (eq form (setq form (compiler-macroexpand form))))))) + (cond ((not (consp form)) form) + ((memq (car form) '(let let*)) + (if (null (nth 1 form)) + (cl-macroexpand-all (cons 'progn (cddr form)) env) + (let ((letf nil) (res nil) (lets (cadr form))) + (while lets + (cl-push (if (consp (car lets)) + (let ((exp (cl-macroexpand-all (caar lets) env))) + (or (symbolp exp) (setq letf t)) + (cons exp (cl-macroexpand-body (cdar lets) env))) + (let ((exp (cl-macroexpand-all (car lets) env))) + (if (symbolp exp) exp + (setq letf t) (list exp nil)))) res) + (setq lets (cdr lets))) + (list* (if letf (if (eq (car form) 'let) 'letf 'letf*) (car form)) + (nreverse res) (cl-macroexpand-body (cddr form) env))))) + ((eq (car form) 'cond) + (cons (car form) + (mapcar (function (lambda (x) (cl-macroexpand-body x env))) + (cdr form)))) + ((eq (car form) 'condition-case) + (list* (car form) (nth 1 form) (cl-macroexpand-all (nth 2 form) env) + (mapcar (function + (lambda (x) + (cons (car x) (cl-macroexpand-body (cdr x) env)))) + (cdddr form)))) + ((memq (car form) '(quote function)) + (if (eq (car-safe (nth 1 form)) 'lambda) + (let ((body (cl-macroexpand-body (cddadr form) env))) + (if (and cl-closure-vars (eq (car form) 'function) + (cl-expr-contains-any body cl-closure-vars)) + (let* ((new (mapcar 'gensym cl-closure-vars)) + (sub (pairlis cl-closure-vars new)) (decls nil)) + (while (or (stringp (car body)) + (eq (car-safe (car body)) 'interactive)) + (cl-push (list 'quote (cl-pop body)) decls)) + (put (car (last cl-closure-vars)) 'used t) + (append + (list 'list '(quote lambda) '(quote (&rest --cl-rest--))) + (sublis sub (nreverse decls)) + (list + (list* 'list '(quote apply) + (list 'list '(quote quote) + (list 'function + (list* 'lambda + (append new (cadadr form)) + (sublis sub body)))) + (nconc (mapcar (function + (lambda (x) + (list 'list '(quote quote) x))) + cl-closure-vars) + '((quote --cl-rest--))))))) + (list (car form) (list* 'lambda (cadadr form) body)))) + form)) + ((memq (car form) '(defun defmacro)) + (list* (car form) (nth 1 form) (cl-macroexpand-body (cddr form) env))) + ((and (eq (car form) 'progn) (not (cddr form))) + (cl-macroexpand-all (nth 1 form) env)) + ((eq (car form) 'setq) + (let* ((args (cl-macroexpand-body (cdr form) env)) (p args)) + (while (and p (symbolp (car p))) (setq p (cddr p))) + (if p (cl-macroexpand-all (cons 'setf args)) (cons 'setq args)))) + (t (cons (car form) (cl-macroexpand-body (cdr form) env))))) + +(defun cl-macroexpand-body (body &optional env) + (mapcar (function (lambda (x) (cl-macroexpand-all x env))) body)) + +(defun cl-prettyexpand (form &optional full) + (message "Expanding...") + (let ((cl-macroexpand-cmacs full) (cl-compiling-file full) + (byte-compile-macro-environment nil)) + (setq form (cl-macroexpand-all form + (and (not full) '((block) (eval-when))))) + (message "Formatting...") + (prog1 (cl-prettyprint form) + (message "")))) + + + +(run-hooks 'cl-extra-load-hook) + +;;; cl-extra.el ends here
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lisp/emacs-lisp/cl-macs.el Fri Jul 30 20:15:09 1993 +0000 @@ -0,0 +1,2610 @@ +;; cl-macs.el --- Common Lisp extensions for GNU Emacs Lisp (part four) + +;; Copyright (C) 1993 Free Software Foundation, Inc. + +;; Author: Dave Gillespie <daveg@synaptics.com> +;; Version: 2.02 +;; Keywords: extensions + +;; This file is part of GNU Emacs. + +;; GNU Emacs is free software; you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation; either version 1, or (at your option) +;; any later version. + +;; GNU Emacs is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with GNU Emacs; see the file COPYING. If not, write to +;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + +;; Commentary: + +;; These are extensions to Emacs Lisp that provide a degree of +;; Common Lisp compatibility, beyond what is already built-in +;; in Emacs Lisp. +;; +;; This package was written by Dave Gillespie; it is a complete +;; rewrite of Cesar Quiroz's original cl.el package of December 1986. +;; +;; This package works with Emacs 18, Emacs 19, and Lucid Emacs 19. +;; +;; Bug reports, comments, and suggestions are welcome! + +;; This file contains the portions of the Common Lisp extensions +;; package which should be autoloaded, but need only be present +;; if the compiler or interpreter is used---this file is not +;; necessary for executing compiled code. + +;; See cl.el for Change Log. + + +;; Code: + +(or (memq 'cl-19 features) + (error "Tried to load `cl-macs' before `cl'!")) + + +;;; We define these here so that this file can compile without having +;;; loaded the cl.el file already. + +(defmacro cl-push (x place) (list 'setq place (list 'cons x place))) +(defmacro cl-pop (place) + (list 'car (list 'prog1 place (list 'setq place (list 'cdr place))))) +(defmacro cl-pop2 (place) + (list 'prog1 (list 'car (list 'cdr place)) + (list 'setq place (list 'cdr (list 'cdr place))))) +(put 'cl-push 'edebug-form-spec 'edebug-sexps) +(put 'cl-pop 'edebug-form-spec 'edebug-sexps) +(put 'cl-pop2 'edebug-form-spec 'edebug-sexps) + +(defvar cl-emacs-type) +(defvar cl-optimize-safety) +(defvar cl-optimize-speed) + + +;;; This kludge allows macros which use cl-transform-function-property +;;; to be called at compile-time. + +(require + (progn + (or (fboundp 'defalias) (fset 'defalias 'fset)) + (or (fboundp 'cl-transform-function-property) + (defalias 'cl-transform-function-property + (function (lambda (n p f) + (list 'put (list 'quote n) (list 'quote p) + (list 'function (cons 'lambda f))))))) + (car (or features (setq features (list 'cl-kludge)))))) + + +;;; Initialization. + +(defvar cl-old-bc-file-form nil) + +;; Patch broken Emacs 18 compiler (re top-level macros). +;; Emacs 19 compiler doesn't need this patch. +;; Also, undo broken definition of `eql' that uses same bytecode as `eq'. +(defun cl-compile-time-init () + (setq cl-old-bc-file-form (symbol-function 'byte-compile-file-form)) + (or (fboundp 'byte-compile-flush-pending) ; Emacs 19 compiler? + (defalias 'byte-compile-file-form + (function + (lambda (form) + (setq form (macroexpand form byte-compile-macro-environment)) + (if (eq (car-safe form) 'progn) + (cons 'progn (mapcar 'byte-compile-file-form (cdr form))) + (funcall cl-old-bc-file-form form)))))) + (put 'eql 'byte-compile 'cl-byte-compile-compiler-macro) + (run-hooks 'cl-hack-bytecomp-hook)) + + +;;; Symbols. + +(defvar *gensym-counter*) +(defun gensym (&optional arg) + "Generate a new uninterned symbol. +The name is made by appending a number to PREFIX, default \"G\"." + (let ((prefix (if (stringp arg) arg "G")) + (num (if (integerp arg) arg + (prog1 *gensym-counter* + (setq *gensym-counter* (1+ *gensym-counter*)))))) + (make-symbol (format "%s%d" prefix num)))) + +(defun gentemp (&optional arg) + "Generate a new interned symbol with a unique name. +The name is made by appending a number to PREFIX, default \"G\"." + (let ((prefix (if (stringp arg) arg "G")) + name) + (while (intern-soft (setq name (format "%s%d" prefix *gensym-counter*))) + (setq *gensym-counter* (1+ *gensym-counter*))) + (intern name))) + + +;;; Program structure. + +(defmacro defun* (name args &rest body) + "(defun* NAME ARGLIST [DOCSTRING] BODY...): define NAME as a function. +Like normal `defun', except ARGLIST allows full Common Lisp conventions, +and BODY is implicitly surrounded by (block NAME ...)." + (let* ((res (cl-transform-lambda (cons args body) name)) + (form (list* 'defun name (cdr res)))) + (if (car res) (list 'progn (car res) form) form))) + +(defmacro defmacro* (name args &rest body) + "(defmacro* NAME ARGLIST [DOCSTRING] BODY...): define NAME as a macro. +Like normal `defmacro', except ARGLIST allows full Common Lisp conventions, +and BODY is implicitly surrounded by (block NAME ...)." + (let* ((res (cl-transform-lambda (cons args body) name)) + (form (list* 'defmacro name (cdr res)))) + (if (car res) (list 'progn (car res) form) form))) + +(defmacro function* (func) + "(function* SYMBOL-OR-LAMBDA): introduce a function. +Like normal `function', except that if argument is a lambda form, its +ARGLIST allows full Common Lisp conventions." + (if (eq (car-safe func) 'lambda) + (let* ((res (cl-transform-lambda (cdr func) 'cl-none)) + (form (list 'function (cons 'lambda (cdr res))))) + (if (car res) (list 'progn (car res) form) form)) + (list 'function func))) + +(defun cl-transform-function-property (func prop form) + (let ((res (cl-transform-lambda form func))) + (append '(progn) (cdr (cdr (car res))) + (list (list 'put (list 'quote func) (list 'quote prop) + (list 'function (cons 'lambda (cdr res)))))))) + +(defconst lambda-list-keywords + '(&optional &rest &key &allow-other-keys &aux &whole &body &environment)) + +(defvar cl-macro-environment nil) +(defvar bind-block) (defvar bind-defs) (defvar bind-enquote) +(defvar bind-inits) (defvar bind-lets) (defvar bind-forms) + +(defun cl-transform-lambda (form bind-block) + (let* ((args (car form)) (body (cdr form)) + (bind-defs nil) (bind-enquote nil) + (bind-inits nil) (bind-lets nil) (bind-forms nil) + (header nil) (simple-args nil)) + (while (or (stringp (car body)) (eq (car-safe (car body)) 'interactive)) + (cl-push (cl-pop body) header)) + (setq args (if (listp args) (copy-list args) (list '&rest args))) + (let ((p (last args))) (if (cdr p) (setcdr p (list '&rest (cdr p))))) + (if (setq bind-defs (cadr (memq '&cl-defs args))) + (setq args (delq '&cl-defs (delq bind-defs args)) + bind-defs (cadr bind-defs))) + (if (setq bind-enquote (memq '&cl-quote args)) + (setq args (delq '&cl-quote args))) + (if (memq '&whole args) (error "&whole not currently implemented")) + (let* ((p (memq '&environment args)) (v (cadr p))) + (if p (setq args (nconc (delq (car p) (delq v args)) + (list '&aux (list v 'cl-macro-environment)))))) + (while (and args (symbolp (car args)) + (not (memq (car args) '(nil &rest &body &key &aux))) + (not (and (eq (car args) '&optional) + (or bind-defs (consp (cadr args)))))) + (cl-push (cl-pop args) simple-args)) + (or (eq bind-block 'cl-none) + (setq body (list (list* 'block bind-block body)))) + (if (null args) + (list* nil (nreverse simple-args) (nconc (nreverse header) body)) + (if (memq '&optional simple-args) (cl-push '&optional args)) + (cl-do-arglist args nil (- (length simple-args) + (if (memq '&optional simple-args) 1 0))) + (setq bind-lets (nreverse bind-lets)) + (list* (and bind-inits (list* 'eval-when '(compile load eval) + (nreverse bind-inits))) + (nconc (nreverse simple-args) + (list '&rest (car (cl-pop bind-lets)))) + (nconc (nreverse header) + (list (nconc (list 'let* bind-lets) + (nreverse bind-forms) body))))))) + +(defun cl-do-arglist (args expr &optional num) ; uses bind-* + (if (nlistp args) + (if (or (memq args lambda-list-keywords) (not (symbolp args))) + (error "Invalid argument name: %s" args) + (cl-push (list args expr) bind-lets)) + (setq args (copy-list args)) + (let ((p (last args))) (if (cdr p) (setcdr p (list '&rest (cdr p))))) + (let ((p (memq '&body args))) (if p (setcar p '&rest))) + (if (memq '&environment args) (error "&environment used incorrectly")) + (let ((save-args args) + (restarg (memq '&rest args)) + (safety (if (cl-compiling-file) cl-optimize-safety 3)) + (keys nil) + (laterarg nil) (exactarg nil) minarg) + (or num (setq num 0)) + (if (listp (cadr restarg)) + (setq restarg (gensym "--rest--")) + (setq restarg (cadr restarg))) + (cl-push (list restarg expr) bind-lets) + (if (eq (car args) '&whole) + (cl-push (list (cl-pop2 args) restarg) bind-lets)) + (let ((p args)) + (setq minarg restarg) + (while (and p (not (memq (car p) lambda-list-keywords))) + (or (eq p args) (setq minarg (list 'cdr minarg))) + (setq p (cdr p))) + (if (memq (car p) '(nil &aux)) + (setq minarg (list '= (list 'length restarg) + (length (ldiff args p))) + exactarg (not (eq args p))))) + (while (and args (not (memq (car args) lambda-list-keywords))) + (let ((poparg (list (if (or (cdr args) (not exactarg)) 'pop 'car) + restarg))) + (cl-do-arglist + (cl-pop args) + (if (or laterarg (= safety 0)) poparg + (list 'if minarg poparg + (list 'signal '(quote wrong-number-of-arguments) + (list 'list (and (not (eq bind-block 'cl-none)) + (list 'quote bind-block)) + (list 'length restarg))))))) + (setq num (1+ num) laterarg t)) + (while (and (eq (car args) '&optional) (cl-pop args)) + (while (and args (not (memq (car args) lambda-list-keywords))) + (let ((arg (cl-pop args))) + (or (consp arg) (setq arg (list arg))) + (if (cddr arg) (cl-do-arglist (nth 2 arg) (list 'and restarg t))) + (let ((def (if (cdr arg) (nth 1 arg) + (or (car bind-defs) + (nth 1 (assq (car arg) bind-defs))))) + (poparg (list 'pop restarg))) + (and def bind-enquote (setq def (list 'quote def))) + (cl-do-arglist (car arg) + (if def (list 'if restarg poparg def) poparg)) + (setq num (1+ num)))))) + (if (eq (car args) '&rest) + (let ((arg (cl-pop2 args))) + (if (consp arg) (cl-do-arglist arg restarg))) + (or (eq (car args) '&key) (= safety 0) exactarg + (cl-push (list 'if restarg + (list 'signal '(quote wrong-number-of-arguments) + (list 'list + (and (not (eq bind-block 'cl-none)) + (list 'quote bind-block)) + (list '+ num (list 'length restarg))))) + bind-forms))) + (while (and (eq (car args) '&key) (cl-pop args)) + (while (and args (not (memq (car args) lambda-list-keywords))) + (let ((arg (cl-pop args))) + (or (consp arg) (setq arg (list arg))) + (let* ((karg (if (consp (car arg)) (caar arg) + (intern (format ":%s" (car arg))))) + (varg (if (consp (car arg)) (cadar arg) (car arg))) + (def (if (cdr arg) (cadr arg) + (or (car bind-defs) (cadr (assq varg bind-defs))))) + (look (list 'memq (list 'quote karg) restarg))) + (and def bind-enquote (setq def (list 'quote def))) + (if (cddr arg) + (let* ((temp (or (nth 2 arg) (gensym))) + (val (list 'car (list 'cdr temp)))) + (cl-do-arglist temp look) + (cl-do-arglist varg + (list 'if temp + (list 'prog1 val (list 'setq temp t)) + def))) + (cl-do-arglist + varg + (list 'car + (list 'cdr + (if (null def) + look + (list 'or look + (if (eq (cl-const-expr-p def) t) + (list + 'quote + (list nil (cl-const-expr-val def))) + (list 'list nil def)))))))) + (cl-push karg keys) + (if (= (aref (symbol-name karg) 0) ?:) + (progn (set karg karg) + (cl-push (list 'setq karg (list 'quote karg)) + bind-inits))))))) + (setq keys (nreverse keys)) + (or (and (eq (car args) '&allow-other-keys) (cl-pop args)) + (null keys) (= safety 0) + (let* ((var (gensym "--keys--")) + (allow '(:allow-other-keys)) + (check (list + 'while var + (list + 'cond + (list (list 'memq (list 'car var) + (list 'quote (append keys allow))) + (list 'setq var (list 'cdr (list 'cdr var)))) + (list (list 'car + (list 'cdr + (list 'memq (cons 'quote allow) + restarg))) + (list 'setq var nil)) + (list t + (list + 'error + (format "Keyword argument %%s not one of %s" + keys) + (list 'car var))))))) + (cl-push (list 'let (list (list var restarg)) check) bind-forms))) + (while (and (eq (car args) '&aux) (cl-pop args)) + (while (and args (not (memq (car args) lambda-list-keywords))) + (if (consp (car args)) + (if (and bind-enquote (cadar args)) + (cl-do-arglist (caar args) + (list 'quote (cadr (cl-pop args)))) + (cl-do-arglist (caar args) (cadr (cl-pop args)))) + (cl-do-arglist (cl-pop args) nil)))) + (if args (error "Malformed argument list %s" save-args))))) + +(defun cl-arglist-args (args) + (if (nlistp args) (list args) + (let ((res nil) (kind nil) arg) + (while (consp args) + (setq arg (cl-pop args)) + (if (memq arg lambda-list-keywords) (setq kind arg) + (if (eq arg '&cl-defs) (cl-pop args) + (and (consp arg) kind (setq arg (car arg))) + (and (consp arg) (cdr arg) (eq kind '&key) (setq arg (cadr arg))) + (setq res (nconc res (cl-arglist-args arg)))))) + (nconc res (and args (list args)))))) + +(defmacro destructuring-bind (args expr &rest body) + (let* ((bind-lets nil) (bind-forms nil) (bind-inits nil) + (bind-defs nil) (bind-block 'cl-none)) + (cl-do-arglist (or args '(&aux)) expr) + (append '(progn) bind-inits + (list (nconc (list 'let* (nreverse bind-lets)) + (nreverse bind-forms) body))))) + + +;;; The `eval-when' form. + +(defvar cl-not-toplevel nil) + +(defmacro eval-when (when &rest body) + "(eval-when (WHEN...) BODY...): control when BODY is evaluated. +If `compile' is in WHEN, BODY is evaluated when compiled at top-level. +If `load' is in WHEN, BODY is evaluated when loaded after top-level compile. +If `eval' is in WHEN, BODY is evaluated when interpreted or at non-top-level." + (if (and (fboundp 'cl-compiling-file) (cl-compiling-file) + (not cl-not-toplevel) (not (boundp 'for-effect))) ; horrible kludge + (let ((comp (or (memq 'compile when) (memq ':compile-toplevel when))) + (cl-not-toplevel t)) + (if (or (memq 'load when) (memq ':load-toplevel when)) + (if comp (cons 'progn (mapcar 'cl-compile-time-too body)) + (list* 'if nil nil body)) + (progn (if comp (eval (cons 'progn body))) nil))) + (and (or (memq 'eval when) (memq ':execute when)) + (cons 'progn body)))) + +(defun cl-compile-time-too (form) + (or (and (symbolp (car-safe form)) (get (car-safe form) 'byte-hunk-handler)) + (setq form (macroexpand + form (cons '(eval-when) byte-compile-macro-environment)))) + (cond ((eq (car-safe form) 'progn) + (cons 'progn (mapcar 'cl-compile-time-too (cdr form)))) + ((eq (car-safe form) 'eval-when) + (let ((when (nth 1 form))) + (if (or (memq 'eval when) (memq ':execute when)) + (list* 'eval-when (cons 'compile when) (cddr form)) + form))) + (t (eval form) form))) + +(or (and (fboundp 'eval-when-compile) + (not (eq (car-safe (symbol-function 'eval-when-compile)) 'autoload))) + (eval '(defmacro eval-when-compile (&rest body) + "Like `progn', but evaluates the body at compile time. +The result of the body appears to the compiler as a quoted constant." + (list 'quote (eval (cons 'progn body)))))) + +(defmacro load-time-value (form &optional read-only) + "Like `progn', but evaluates the body at load time. +The result of the body appears to the compiler as a quoted constant." + (if (cl-compiling-file) + (let* ((temp (gentemp "--cl-load-time--")) + (set (list 'set (list 'quote temp) form))) + (if (and (fboundp 'byte-compile-file-form-defmumble) + (boundp 'this-kind) (boundp 'that-one)) + (fset 'byte-compile-file-form + (list 'lambda '(form) + (list 'fset '(quote byte-compile-file-form) + (list 'quote + (symbol-function 'byte-compile-file-form))) + (list 'byte-compile-file-form (list 'quote set)) + '(byte-compile-file-form form))) + (print set (symbol-value 'outbuffer))) + (list 'symbol-value (list 'quote temp))) + (list 'quote (eval form)))) + + +;;; Conditional control structures. + +(defmacro case (expr &rest clauses) + "(case EXPR CLAUSES...): evals EXPR, chooses from CLAUSES on that value. +Each clause looks like (KEYLIST BODY...). EXPR is evaluated and compared +against each key in each KEYLIST; the corresponding BODY is evaluated. +If no clause succeeds, case returns nil. A single atom may be used in +place of a KEYLIST of one atom. A KEYLIST of `t' or `otherwise' is +allowed only in the final clause, and matches if no other keys match. +Key values are compared by `eql'." + (let* ((temp (if (cl-simple-expr-p expr 3) expr (gensym))) + (head-list nil) + (body (cons + 'cond + (mapcar + (function + (lambda (c) + (cons (cond ((memq (car c) '(t otherwise)) t) + ((eq (car c) 'ecase-error-flag) + (list 'error "ecase failed: %s, %s" + temp (list 'quote (reverse head-list)))) + ((listp (car c)) + (setq head-list (append (car c) head-list)) + (list 'member* temp (list 'quote (car c)))) + (t + (if (memq (car c) head-list) + (error "Duplicate key in case: %s" + (car c))) + (cl-push (car c) head-list) + (list 'eql temp (list 'quote (car c))))) + (or (cdr c) '(nil))))) + clauses)))) + (if (eq temp expr) body + (list 'let (list (list temp expr)) body)))) + +(defmacro ecase (expr &rest clauses) + "(ecase EXPR CLAUSES...): like `case', but error if no case fits. +`otherwise'-clauses are not allowed." + (list* 'case expr (append clauses '((ecase-error-flag))))) + +(defmacro typecase (expr &rest clauses) + "(typecase EXPR CLAUSES...): evals EXPR, chooses from CLAUSES on that value. +Each clause looks like (TYPE BODY...). EXPR is evaluated and, if it +satisfies TYPE, the corresponding BODY is evaluated. If no clause succeeds, +typecase returns nil. A TYPE of `t' or `otherwise' is allowed only in the +final clause, and matches if no other keys match." + (let* ((temp (if (cl-simple-expr-p expr 3) expr (gensym))) + (type-list nil) + (body (cons + 'cond + (mapcar + (function + (lambda (c) + (cons (cond ((eq (car c) 'otherwise) t) + ((eq (car c) 'ecase-error-flag) + (list 'error "etypecase failed: %s, %s" + temp (list 'quote (reverse type-list)))) + (t + (cl-push (car c) type-list) + (cl-make-type-test temp (car c)))) + (or (cdr c) '(nil))))) + clauses)))) + (if (eq temp expr) body + (list 'let (list (list temp expr)) body)))) + +(defmacro etypecase (expr &rest clauses) + "(etypecase EXPR CLAUSES...): like `typecase', but error if no case fits. +`otherwise'-clauses are not allowed." + (list* 'typecase expr (append clauses '((ecase-error-flag))))) + + +;;; Blocks and exits. + +(defmacro block (name &rest body) + "(block NAME BODY...): define a lexically-scoped block named NAME. +NAME may be any symbol. Code inside the BODY forms can call `return-from' +to jump prematurely out of the block. This differs from `catch' and `throw' +in two respects: First, the NAME is an unevaluated symbol rather than a +quoted symbol or other form; and second, NAME is lexically rather than +dynamically scoped: Only references to it within BODY will work. These +references may appear inside macro expansions, but not inside functions +called from BODY." + (if (cl-safe-expr-p (cons 'progn body)) (cons 'progn body) + (list 'cl-block-wrapper + (list* 'catch (list 'quote (intern (format "--cl-block-%s--" name))) + body)))) + +(defvar cl-active-block-names nil) + +(put 'cl-block-wrapper 'byte-compile 'cl-byte-compile-block) +(defun cl-byte-compile-block (cl-form) + (if (fboundp 'byte-compile-form-do-effect) ; Check for optimizing compiler + (progn + (let* ((cl-entry (cons (nth 1 (nth 1 (nth 1 cl-form))) nil)) + (cl-active-block-names (cons cl-entry cl-active-block-names)) + (cl-body (byte-compile-top-level + (cons 'progn (cddr (nth 1 cl-form)))))) + (if (cdr cl-entry) + (byte-compile-form (list 'catch (nth 1 (nth 1 cl-form)) cl-body)) + (byte-compile-form cl-body)))) + (byte-compile-form (nth 1 cl-form)))) + +(put 'cl-block-throw 'byte-compile 'cl-byte-compile-throw) +(defun cl-byte-compile-throw (cl-form) + (let ((cl-found (assq (nth 1 (nth 1 cl-form)) cl-active-block-names))) + (if cl-found (setcdr cl-found t))) + (byte-compile-normal-call (cons 'throw (cdr cl-form)))) + +(defmacro return (&optional res) + "(return [RESULT]): return from the block named nil. +This is equivalent to `(return-from nil RESULT)'." + (list 'return-from nil res)) + +(defmacro return-from (name &optional res) + "(return-from NAME [RESULT]): return from the block named NAME. +This jump out to the innermost enclosing `(block NAME ...)' form, +returning RESULT from that form (or nil if RESULT is omitted). +This is compatible with Common Lisp, but note that `defun' and +`defmacro' do not create implicit blocks as they do in Common Lisp." + (let ((name2 (intern (format "--cl-block-%s--" name)))) + (list 'cl-block-throw (list 'quote name2) res))) + + +;;; The "loop" macro. + +(defvar args) (defvar loop-accum-var) (defvar loop-accum-vars) +(defvar loop-bindings) (defvar loop-body) (defvar loop-destr-temps) +(defvar loop-finally) (defvar loop-finish-flag) (defvar loop-first-flag) +(defvar loop-initially) (defvar loop-map-form) (defvar loop-name) +(defvar loop-result) (defvar loop-result-explicit) +(defvar loop-result-var) (defvar loop-steps) (defvar loop-symbol-macs) + +(defmacro loop (&rest args) + "(loop CLAUSE...): The Common Lisp `loop' macro. +Valid clauses are: + for VAR from/upfrom/downfrom NUM to/upto/downto/above/below NUM by NUM, + for VAR in LIST by FUNC, for VAR on LIST by FUNC, for VAR = INIT then EXPR, + for VAR across ARRAY, repeat NUM, with VAR = INIT, while COND, until COND, + always COND, never COND, thereis COND, collect EXPR into VAR, + append EXPR into VAR, nconc EXPR into VAR, sum EXPR into VAR, + count EXPR into VAR, maximize EXPR into VAR, minimize EXPR into VAR, + if COND CLAUSE [and CLAUSE]... else CLAUSE [and CLAUSE...], + unless COND CLAUSE [and CLAUSE]... else CLAUSE [and CLAUSE...], + do EXPRS..., initially EXPRS..., finally EXPRS..., return EXPR, + finally return EXPR, named NAME." + (if (not (memq t (mapcar 'symbolp (delq nil (delq t (copy-list args)))))) + (list 'block nil (list* 'while t args)) + (let ((loop-name nil) (loop-bindings nil) + (loop-body nil) (loop-steps nil) + (loop-result nil) (loop-result-explicit nil) + (loop-result-var nil) (loop-finish-flag nil) + (loop-accum-var nil) (loop-accum-vars nil) + (loop-initially nil) (loop-finally nil) + (loop-map-form nil) (loop-first-flag nil) + (loop-destr-temps nil) (loop-symbol-macs nil)) + (setq args (append args '(cl-end-loop))) + (while (not (eq (car args) 'cl-end-loop)) (cl-parse-loop-clause)) + (if loop-finish-flag + (cl-push (list (list loop-finish-flag t)) loop-bindings)) + (if loop-first-flag + (progn (cl-push (list (list loop-first-flag t)) loop-bindings) + (cl-push (list 'setq loop-first-flag nil) loop-steps))) + (let* ((epilogue (nconc (nreverse loop-finally) + (list (or loop-result-explicit loop-result)))) + (ands (cl-loop-build-ands (nreverse loop-body))) + (while-body (nconc (cadr ands) (nreverse loop-steps))) + (body (append + (nreverse loop-initially) + (list (if loop-map-form + (list 'block '--cl-finish-- + (subst + (if (eq (car ands) t) while-body + (cons (list 'or (car ands) + '(return-from --cl-finish-- + nil)) + while-body)) + '--cl-map loop-map-form)) + (list* 'while (car ands) while-body))) + (if loop-finish-flag + (if (equal epilogue '(nil)) (list loop-result-var) + (list (list 'if loop-finish-flag + (cons 'progn epilogue) loop-result-var))) + epilogue)))) + (if loop-result-var (cl-push (list loop-result-var) loop-bindings)) + (while loop-bindings + (if (cdar loop-bindings) + (setq body (list (cl-loop-let (cl-pop loop-bindings) body t))) + (let ((lets nil)) + (while (and loop-bindings + (not (cdar loop-bindings))) + (cl-push (car (cl-pop loop-bindings)) lets)) + (setq body (list (cl-loop-let lets body nil)))))) + (if loop-symbol-macs + (setq body (list (list* 'symbol-macrolet loop-symbol-macs body)))) + (list* 'block loop-name body))))) + +(defun cl-parse-loop-clause () ; uses args, loop-* + (let ((word (cl-pop args)) + (hash-types '(hash-key hash-keys hash-value hash-values)) + (key-types '(key-code key-codes key-seq key-seqs + key-binding key-bindings))) + (cond + + ((null args) + (error "Malformed `loop' macro")) + + ((eq word 'named) + (setq loop-name (cl-pop args))) + + ((eq word 'initially) + (if (memq (car args) '(do doing)) (cl-pop args)) + (or (consp (car args)) (error "Syntax error on `initially' clause")) + (while (consp (car args)) + (cl-push (cl-pop args) loop-initially))) + + ((eq word 'finally) + (if (eq (car args) 'return) + (setq loop-result-explicit (or (cl-pop2 args) '(quote nil))) + (if (memq (car args) '(do doing)) (cl-pop args)) + (or (consp (car args)) (error "Syntax error on `finally' clause")) + (if (and (eq (caar args) 'return) (null loop-name)) + (setq loop-result-explicit (or (nth 1 (cl-pop args)) '(quote nil))) + (while (consp (car args)) + (cl-push (cl-pop args) loop-finally))))) + + ((memq word '(for as)) + (let ((loop-for-bindings nil) (loop-for-sets nil) (loop-for-steps nil) + (ands nil)) + (while + (let ((var (or (cl-pop args) (gensym)))) + (setq word (cl-pop args)) + (if (eq word 'being) (setq word (cl-pop args))) + (if (memq word '(the each)) (setq word (cl-pop args))) + (if (memq word '(buffer buffers)) + (setq word 'in args (cons '(buffer-list) args))) + (cond + + ((memq word '(from downfrom upfrom to downto upto + above below by)) + (cl-push word args) + (if (memq (car args) '(downto above)) + (error "Must specify `from' value for downward loop")) + (let* ((down (or (eq (car args) 'downfrom) + (memq (caddr args) '(downto above)))) + (excl (or (memq (car args) '(above below)) + (memq (caddr args) '(above below)))) + (start (and (memq (car args) '(from upfrom downfrom)) + (cl-pop2 args))) + (end (and (memq (car args) + '(to upto downto above below)) + (cl-pop2 args))) + (step (and (eq (car args) 'by) (cl-pop2 args))) + (end-var (and (not (cl-const-expr-p end)) (gensym))) + (step-var (and (not (cl-const-expr-p step)) + (gensym)))) + (and step (numberp step) (<= step 0) + (error "Loop `by' value is not positive: %s" step)) + (cl-push (list var (or start 0)) loop-for-bindings) + (if end-var (cl-push (list end-var end) loop-for-bindings)) + (if step-var (cl-push (list step-var step) + loop-for-bindings)) + (if end + (cl-push (list + (if down (if excl '> '>=) (if excl '< '<=)) + var (or end-var end)) loop-body)) + (cl-push (list var (list (if down '- '+) var + (or step-var step 1))) + loop-for-steps))) + + ((memq word '(in in-ref on)) + (let* ((on (eq word 'on)) + (temp (if (and on (symbolp var)) var (gensym)))) + (cl-push (list temp (cl-pop args)) loop-for-bindings) + (cl-push (list 'consp temp) loop-body) + (if (eq word 'in-ref) + (cl-push (list var (list 'car temp)) loop-symbol-macs) + (or (eq temp var) + (progn + (cl-push (list var nil) loop-for-bindings) + (cl-push (list var (if on temp (list 'car temp))) + loop-for-sets)))) + (cl-push (list temp + (if (eq (car args) 'by) + (let ((step (cl-pop2 args))) + (if (and (memq (car-safe step) + '(quote function + function*)) + (symbolp (nth 1 step))) + (list (nth 1 step) temp) + (list 'funcall step temp))) + (list 'cdr temp))) + loop-for-steps))) + + ((eq word '=) + (let* ((start (cl-pop args)) + (then (if (eq (car args) 'then) (cl-pop2 args) start))) + (cl-push (list var nil) loop-for-bindings) + (if (or ands (eq (car args) 'and)) + (progn + (cl-push (list var + (list 'if + (or loop-first-flag + (setq loop-first-flag + (gensym))) + start var)) + loop-for-sets) + (cl-push (list var then) loop-for-steps)) + (cl-push (list var + (if (eq start then) start + (list 'if + (or loop-first-flag + (setq loop-first-flag (gensym))) + start then))) + loop-for-sets)))) + + ((memq word '(across across-ref)) + (let ((temp-vec (gensym)) (temp-idx (gensym))) + (cl-push (list temp-vec (cl-pop args)) loop-for-bindings) + (cl-push (list temp-idx -1) loop-for-bindings) + (cl-push (list '< (list 'setq temp-idx (list '1+ temp-idx)) + (list 'length temp-vec)) loop-body) + (if (eq word 'across-ref) + (cl-push (list var (list 'aref temp-vec temp-idx)) + loop-symbol-macs) + (cl-push (list var nil) loop-for-bindings) + (cl-push (list var (list 'aref temp-vec temp-idx)) + loop-for-sets)))) + + ((memq word '(element elements)) + (let ((ref (or (memq (car args) '(in-ref of-ref)) + (and (not (memq (car args) '(in of))) + (error "Expected `of'")))) + (seq (cl-pop2 args)) + (temp-seq (gensym)) + (temp-idx (if (eq (car args) 'using) + (if (and (= (length (cadr args)) 2) + (eq (caadr args) 'index)) + (cadr (cl-pop2 args)) + (error "Bad `using' clause")) + (gensym)))) + (cl-push (list temp-seq seq) loop-for-bindings) + (cl-push (list temp-idx 0) loop-for-bindings) + (if ref + (let ((temp-len (gensym))) + (cl-push (list temp-len (list 'length temp-seq)) + loop-for-bindings) + (cl-push (list var (list 'elt temp-seq temp-idx)) + loop-symbol-macs) + (cl-push (list '< temp-idx temp-len) loop-body)) + (cl-push (list var nil) loop-for-bindings) + (cl-push (list 'and temp-seq + (list 'or (list 'consp temp-seq) + (list '< temp-idx + (list 'length temp-seq)))) + loop-body) + (cl-push (list var (list 'if (list 'consp temp-seq) + (list 'pop temp-seq) + (list 'aref temp-seq temp-idx))) + loop-for-sets)) + (cl-push (list temp-idx (list '1+ temp-idx)) + loop-for-steps))) + + ((memq word hash-types) + (or (memq (car args) '(in of)) (error "Expected `of'")) + (let* ((table (cl-pop2 args)) + (other (if (eq (car args) 'using) + (if (and (= (length (cadr args)) 2) + (memq (caadr args) hash-types) + (not (eq (caadr args) word))) + (cadr (cl-pop2 args)) + (error "Bad `using' clause")) + (gensym)))) + (if (memq word '(hash-value hash-values)) + (setq var (prog1 other (setq other var)))) + (setq loop-map-form + (list 'maphash (list 'function + (list* 'lambda (list var other) + '--cl-map)) table)))) + + ((memq word '(symbol present-symbol external-symbol + symbols present-symbols external-symbols)) + (let ((ob (and (memq (car args) '(in of)) (cl-pop2 args)))) + (setq loop-map-form + (list 'mapatoms (list 'function + (list* 'lambda (list var) + '--cl-map)) ob)))) + + ((memq word '(overlay overlays extent extents)) + (let ((buf nil) (from nil) (to nil)) + (while (memq (car args) '(in of from to)) + (cond ((eq (car args) 'from) (setq from (cl-pop2 args))) + ((eq (car args) 'to) (setq to (cl-pop2 args))) + (t (setq buf (cl-pop2 args))))) + (setq loop-map-form + (list 'cl-map-extents + (list 'function (list 'lambda (list var (gensym)) + '(progn . --cl-map) nil)) + buf from to)))) + + ((memq word '(interval intervals)) + (let ((buf nil) (prop nil) (from nil) (to nil) + (var1 (gensym)) (var2 (gensym))) + (while (memq (car args) '(in of property from to)) + (cond ((eq (car args) 'from) (setq from (cl-pop2 args))) + ((eq (car args) 'to) (setq to (cl-pop2 args))) + ((eq (car args) 'property) + (setq prop (cl-pop2 args))) + (t (setq buf (cl-pop2 args))))) + (if (and (consp var) (symbolp (car var)) (symbolp (cdr var))) + (setq var1 (car var) var2 (cdr var)) + (cl-push (list var (list 'cons var1 var2)) loop-for-sets)) + (setq loop-map-form + (list 'cl-map-intervals + (list 'function (list 'lambda (list var1 var2) + '(progn . --cl-map))) + buf prop from to)))) + + ((memq word key-types) + (or (memq (car args) '(in of)) (error "Expected `of'")) + (let ((map (cl-pop2 args)) + (other (if (eq (car args) 'using) + (if (and (= (length (cadr args)) 2) + (memq (caadr args) key-types) + (not (eq (caadr args) word))) + (cadr (cl-pop2 args)) + (error "Bad `using' clause")) + (gensym)))) + (if (memq word '(key-binding key-bindings)) + (setq var (prog1 other (setq other var)))) + (setq loop-map-form + (list (if (memq word '(key-seq key-seqs)) + 'cl-map-keymap-recursively 'cl-map-keymap) + (list 'function (list* 'lambda (list var other) + '--cl-map)) map)))) + + ((memq word '(frame frames screen screens)) + (let ((temp (gensym))) + (cl-push (list var (if (eq cl-emacs-type 'lucid) + '(selected-screen) '(selected-frame))) + loop-for-bindings) + (cl-push (list temp nil) loop-for-bindings) + (cl-push (list 'prog1 (list 'not (list 'eq var temp)) + (list 'or temp (list 'setq temp var))) + loop-body) + (cl-push (list var (list (if (eq cl-emacs-type 'lucid) + 'next-screen 'next-frame) var)) + loop-for-steps))) + + ((memq word '(window windows)) + (let ((scr (and (memq (car args) '(in of)) (cl-pop2 args))) + (temp (gensym))) + (cl-push (list var (if scr + (list (if (eq cl-emacs-type 'lucid) + 'screen-selected-window + 'frame-selected-window) scr) + '(selected-window))) + loop-for-bindings) + (cl-push (list temp nil) loop-for-bindings) + (cl-push (list 'prog1 (list 'not (list 'eq var temp)) + (list 'or temp (list 'setq temp var))) + loop-body) + (cl-push (list var (list 'next-window var)) loop-for-steps))) + + (t + (let ((handler (and (symbolp word) + (get word 'cl-loop-for-handler)))) + (if handler + (funcall handler var) + (error "Expected a `for' preposition, found %s" word))))) + (eq (car args) 'and)) + (setq ands t) + (cl-pop args)) + (if (and ands loop-for-bindings) + (cl-push (nreverse loop-for-bindings) loop-bindings) + (setq loop-bindings (nconc (mapcar 'list loop-for-bindings) + loop-bindings))) + (if loop-for-sets + (cl-push (list 'progn + (cl-loop-let (nreverse loop-for-sets) 'setq ands) + t) loop-body)) + (if loop-for-steps + (cl-push (cons (if ands 'psetq 'setq) + (apply 'append (nreverse loop-for-steps))) + loop-steps)))) + + ((eq word 'repeat) + (let ((temp (gensym))) + (cl-push (list (list temp (cl-pop args))) loop-bindings) + (cl-push (list '>= (list 'setq temp (list '1- temp)) 0) loop-body))) + + ((eq word 'collect) + (let ((what (cl-pop args)) + (var (cl-loop-handle-accum nil 'nreverse))) + (if (eq var loop-accum-var) + (cl-push (list 'progn (list 'push what var) t) loop-body) + (cl-push (list 'progn + (list 'setq var (list 'nconc var (list 'list what))) + t) loop-body)))) + + ((memq word '(nconc nconcing append appending)) + (let ((what (cl-pop args)) + (var (cl-loop-handle-accum nil 'nreverse))) + (cl-push (list 'progn + (list 'setq var + (if (eq var loop-accum-var) + (list 'nconc + (list (if (memq word '(nconc nconcing)) + 'nreverse 'reverse) + what) + var) + (list (if (memq word '(nconc nconcing)) + 'nconc 'append) + var what))) t) loop-body))) + + ((memq word '(concat concating)) + (let ((what (cl-pop args)) + (var (cl-loop-handle-accum ""))) + (cl-push (list 'progn (list 'callf 'concat var what) t) loop-body))) + + ((memq word '(vconcat vconcating)) + (let ((what (cl-pop args)) + (var (cl-loop-handle-accum []))) + (cl-push (list 'progn (list 'callf 'vconcat var what) t) loop-body))) + + ((memq word '(sum summing)) + (let ((what (cl-pop args)) + (var (cl-loop-handle-accum 0))) + (cl-push (list 'progn (list 'incf var what) t) loop-body))) + + ((memq word '(count counting)) + (let ((what (cl-pop args)) + (var (cl-loop-handle-accum 0))) + (cl-push (list 'progn (list 'if what (list 'incf var)) t) loop-body))) + + ((memq word '(minimize minimizing maximize maximizing)) + (let* ((what (cl-pop args)) + (temp (if (cl-simple-expr-p what) what (gensym))) + (var (cl-loop-handle-accum nil)) + (func (intern (substring (symbol-name word) 0 3))) + (set (list 'setq var (list 'if var (list func var temp) temp)))) + (cl-push (list 'progn (if (eq temp what) set + (list 'let (list (list temp what)) set)) + t) loop-body))) + + ((eq word 'with) + (let ((bindings nil)) + (while (progn (cl-push (list (cl-pop args) + (and (eq (car args) '=) (cl-pop2 args))) + bindings) + (eq (car args) 'and)) + (cl-pop args)) + (cl-push (nreverse bindings) loop-bindings))) + + ((eq word 'while) + (cl-push (cl-pop args) loop-body)) + + ((eq word 'until) + (cl-push (list 'not (cl-pop args)) loop-body)) + + ((eq word 'always) + (or loop-finish-flag (setq loop-finish-flag (gensym))) + (cl-push (list 'setq loop-finish-flag (cl-pop args)) loop-body) + (setq loop-result t)) + + ((eq word 'never) + (or loop-finish-flag (setq loop-finish-flag (gensym))) + (cl-push (list 'setq loop-finish-flag (list 'not (cl-pop args))) + loop-body) + (setq loop-result t)) + + ((eq word 'thereis) + (or loop-finish-flag (setq loop-finish-flag (gensym))) + (or loop-result-var (setq loop-result-var (gensym))) + (cl-push (list 'setq loop-finish-flag + (list 'not (list 'setq loop-result-var (cl-pop args)))) + loop-body)) + + ((memq word '(if when unless)) + (let* ((cond (cl-pop args)) + (then (let ((loop-body nil)) + (cl-parse-loop-clause) + (cl-loop-build-ands (nreverse loop-body)))) + (else (let ((loop-body nil)) + (if (eq (car args) 'else) + (progn (cl-pop args) (cl-parse-loop-clause))) + (cl-loop-build-ands (nreverse loop-body)))) + (simple (and (eq (car then) t) (eq (car else) t)))) + (if (eq (car args) 'end) (cl-pop args)) + (if (eq word 'unless) (setq then (prog1 else (setq else then)))) + (let ((form (cons (if simple (cons 'progn (nth 1 then)) (nth 2 then)) + (if simple (nth 1 else) (list (nth 2 else)))))) + (if (cl-expr-contains form 'it) + (let ((temp (gensym))) + (cl-push (list temp) loop-bindings) + (setq form (list* 'if (list 'setq temp cond) + (subst temp 'it form)))) + (setq form (list* 'if cond form))) + (cl-push (if simple (list 'progn form t) form) loop-body)))) + + ((memq word '(do doing)) + (let ((body nil)) + (or (consp (car args)) (error "Syntax error on `do' clause")) + (while (consp (car args)) (cl-push (cl-pop args) body)) + (cl-push (cons 'progn (nreverse (cons t body))) loop-body))) + + ((eq word 'return) + (or loop-finish-flag (setq loop-finish-flag (gensym))) + (or loop-result-var (setq loop-result-var (gensym))) + (cl-push (list 'setq loop-result-var (cl-pop args) + loop-finish-flag nil) loop-body)) + + (t + (let ((handler (and (symbolp word) (get word 'cl-loop-handler)))) + (or handler (error "Expected a loop keyword, found %s" word)) + (funcall handler)))) + (if (eq (car args) 'and) + (progn (cl-pop args) (cl-parse-loop-clause))))) + +(defun cl-loop-let (specs body par) ; uses loop-* + (let ((p specs) (temps nil) (new nil)) + (while (and p (or (symbolp (car-safe (car p))) (null (cadar p)))) + (setq p (cdr p))) + (and par p + (progn + (setq par nil p specs) + (while p + (or (cl-const-expr-p (cadar p)) + (let ((temp (gensym))) + (cl-push (list temp (cadar p)) temps) + (setcar (cdar p) temp))) + (setq p (cdr p))))) + (while specs + (if (and (consp (car specs)) (listp (caar specs))) + (let* ((spec (caar specs)) (nspecs nil) + (expr (cadr (cl-pop specs))) + (temp (cdr (or (assq spec loop-destr-temps) + (car (cl-push (cons spec (or (last spec 0) + (gensym))) + loop-destr-temps)))))) + (cl-push (list temp expr) new) + (while (consp spec) + (cl-push (list (cl-pop spec) + (and expr (list (if spec 'pop 'car) temp))) + nspecs)) + (setq specs (nconc (nreverse nspecs) specs))) + (cl-push (cl-pop specs) new))) + (if (eq body 'setq) + (let ((set (cons (if par 'psetq 'setq) (apply 'nconc (nreverse new))))) + (if temps (list 'let* (nreverse temps) set) set)) + (list* (if par 'let 'let*) + (nconc (nreverse temps) (nreverse new)) body)))) + +(defun cl-loop-handle-accum (def &optional func) ; uses args, loop-* + (if (eq (car args) 'into) + (let ((var (cl-pop2 args))) + (or (memq var loop-accum-vars) + (progn (cl-push (list (list var def)) loop-bindings) + (cl-push var loop-accum-vars))) + var) + (or loop-accum-var + (progn + (cl-push (list (list (setq loop-accum-var (gensym)) def)) + loop-bindings) + (setq loop-result (if func (list func loop-accum-var) + loop-accum-var)) + loop-accum-var)))) + +(defun cl-loop-build-ands (clauses) + (let ((ands nil) + (body nil)) + (while clauses + (if (and (eq (car-safe (car clauses)) 'progn) + (eq (car (last (car clauses))) t)) + (if (cdr clauses) + (setq clauses (cons (nconc (butlast (car clauses)) + (if (eq (car-safe (cadr clauses)) + 'progn) + (cdadr clauses) + (list (cadr clauses)))) + (cddr clauses))) + (setq body (cdr (butlast (cl-pop clauses))))) + (cl-push (cl-pop clauses) ands))) + (setq ands (or (nreverse ands) (list t))) + (list (if (cdr ands) (cons 'and ands) (car ands)) + body + (let ((full (if body + (append ands (list (cons 'progn (append body '(t))))) + ands))) + (if (cdr full) (cons 'and full) (car full)))))) + + +;;; Other iteration control structures. + +(defmacro do (steps endtest &rest body) + "The Common Lisp `do' loop. +Format is: (do ((VAR INIT [STEP])...) (END-TEST [RESULT...]) BODY...)" + (cl-expand-do-loop steps endtest body nil)) + +(defmacro do* (steps endtest &rest body) + "The Common Lisp `do*' loop. +Format is: (do* ((VAR INIT [STEP])...) (END-TEST [RESULT...]) BODY...)" + (cl-expand-do-loop steps endtest body t)) + +(defun cl-expand-do-loop (steps endtest body star) + (list 'block nil + (list* (if star 'let* 'let) + (mapcar (function (lambda (c) + (if (consp c) (list (car c) (nth 1 c)) c))) + steps) + (list* 'while (list 'not (car endtest)) + (append body + (let ((sets (mapcar + (function + (lambda (c) + (and (consp c) (cdr (cdr c)) + (list (car c) (nth 2 c))))) + steps))) + (setq sets (delq nil sets)) + (and sets + (list (cons (if (or star (not (cdr sets))) + 'setq 'psetq) + (apply 'append sets))))))) + (or (cdr endtest) '(nil))))) + +(defmacro dolist (spec &rest body) + "(dolist (VAR LIST [RESULT]) BODY...): loop over a list. +Evaluate BODY with VAR bound to each `car' from LIST, in turn. +Then evaluate RESULT to get return value, default nil." + (let ((temp (gensym "--dolist-temp--"))) + (list 'block nil + (list* 'let (list (list temp (nth 1 spec)) (car spec)) + (list* 'while temp (list 'setq (car spec) (list 'car temp)) + (append body (list (list 'setq temp + (list 'cdr temp))))) + (if (cdr (cdr spec)) + (cons (list 'setq (car spec) nil) (cdr (cdr spec))) + '(nil)))))) + +(defmacro dotimes (spec &rest body) + "(dotimes (VAR COUNT [RESULT]) BODY...): loop a certain number of times. +Evaluate BODY with VAR bound to successive integers from 0, inclusive, +to COUNT, exclusive. Then evaluate RESULT to get return value, default +nil." + (let ((temp (gensym "--dotimes-temp--"))) + (list 'block nil + (list* 'let (list (list temp (nth 1 spec)) (list (car spec) 0)) + (list* 'while (list '< (car spec) temp) + (append body (list (list 'incf (car spec))))) + (or (cdr (cdr spec)) '(nil)))))) + +(defmacro do-symbols (spec &rest body) + "(dosymbols (VAR [OBARRAY [RESULT]]) BODY...): loop over all symbols. +Evaluate BODY with VAR bound to each interned symbol, or to each symbol +from OBARRAY." + ;; Apparently this doesn't have an implicit block. + (list 'block nil + (list 'let (list (car spec)) + (list* 'mapatoms + (list 'function (list* 'lambda (list (car spec)) body)) + (and (cadr spec) (list (cadr spec)))) + (caddr spec)))) + +(defmacro do-all-symbols (spec &rest body) + (list* 'do-symbols (list (car spec) nil (cadr spec)) body)) + + +;;; Assignments. + +(defmacro psetq (&rest args) + "(psetq SYM VAL SYM VAL ...): set SYMs to the values VALs in parallel. +This is like `setq', except that all VAL forms are evaluated (in order) +before assigning any symbols SYM to the corresponding values." + (cons 'psetf args)) + + +;;; Binding control structures. + +(defmacro progv (symbols values &rest body) + "(progv SYMBOLS VALUES BODY...): bind SYMBOLS to VALUES dynamically in BODY. +The forms SYMBOLS and VALUES are evaluated, and must evaluate to lists. +Each SYMBOL in the first list is bound to the corresponding VALUE in the +second list (or made unbound if VALUES is shorter than SYMBOLS); then the +BODY forms are executed and their result is returned. This is much like +a `let' form, except that the list of symbols can be computed at run-time." + (list 'let '((cl-progv-save nil)) + (list 'unwind-protect + (list* 'progn (list 'cl-progv-before symbols values) body) + '(cl-progv-after)))) + +;;; This should really have some way to shadow 'byte-compile properties, etc. +(defmacro flet (bindings &rest body) + "(flet ((FUNC ARGLIST BODY...) ...) FORM...): make temporary function defns. +This is an analogue of `let' that operates on the function cell of FUNC +rather than its value cell. The FORMs are evaluated with the specified +function definitions in place, then the definitions are undone (the FUNCs +go back to their previous definitions, or lack thereof)." + (list* 'letf* + (mapcar + (function + (lambda (x) + (let ((func (list 'function* + (list 'lambda (cadr x) + (list* 'block (car x) (cddr x)))))) + (if (and (cl-compiling-file) + (boundp 'byte-compile-function-environment)) + (cl-push (cons (car x) (eval func)) + byte-compile-function-environment)) + (list (list 'symbol-function (list 'quote (car x))) func)))) + bindings) + body)) + +(defmacro labels (&rest args) (cons 'flet args)) + +;; The following ought to have a better definition for use with newer +;; byte compilers. +(defmacro macrolet (bindings &rest body) + "(macrolet ((NAME ARGLIST BODY...) ...) FORM...): make temporary macro defns. +This is like `flet', but for macros instead of functions." + (if (cdr bindings) + (list 'macrolet + (list (car bindings)) (list* 'macrolet (cdr bindings) body)) + (if (null bindings) (cons 'progn body) + (let* ((name (caar bindings)) + (res (cl-transform-lambda (cdar bindings) name))) + (eval (car res)) + (cl-macroexpand-all (cons 'progn body) + (cons (list* name 'lambda (cdr res)) + cl-macro-environment)))))) + +(defmacro symbol-macrolet (bindings &rest body) + "(symbol-macrolet ((NAME EXPANSION) ...) FORM...): make symbol macro defns. +Within the body FORMs, references to the variable NAME will be replaced +by EXPANSION, and (setq NAME ...) will act like (setf EXPANSION ...)." + (if (cdr bindings) + (list 'symbol-macrolet + (list (car bindings)) (list* 'symbol-macrolet (cdr bindings) body)) + (if (null bindings) (cons 'progn body) + (cl-macroexpand-all (cons 'progn body) + (cons (list (symbol-name (caar bindings)) + (cadar bindings)) + cl-macro-environment))))) + +(defvar cl-closure-vars nil) +(defmacro lexical-let (bindings &rest body) + "(lexical-let BINDINGS BODY...): like `let', but lexically scoped. +The main visible difference is that lambdas inside BODY will create +lexical closures as in Common Lisp." + (let* ((cl-closure-vars cl-closure-vars) + (vars (mapcar (function + (lambda (x) + (or (consp x) (setq x (list x))) + (cl-push (gensym (format "--%s--" (car x))) + cl-closure-vars) + (list (car x) (cadr x) (car cl-closure-vars)))) + bindings)) + (ebody + (cl-macroexpand-all + (cons 'progn body) + (nconc (mapcar (function (lambda (x) + (list (symbol-name (car x)) + (list 'symbol-value (caddr x)) + t))) vars) + (list '(defun . cl-defun-expander)) + cl-macro-environment)))) + (if (not (get (car (last cl-closure-vars)) 'used)) + (list 'let (mapcar (function (lambda (x) + (list (caddr x) (cadr x)))) vars) + (sublis (mapcar (function (lambda (x) + (cons (caddr x) + (list 'quote (caddr x))))) + vars) + ebody)) + (list 'let (mapcar (function (lambda (x) + (list (caddr x) + (list 'make-symbol + (format "--%s--" (car x)))))) + vars) + (apply 'append '(setf) + (mapcar (function + (lambda (x) + (list (list 'symbol-value (caddr x)) (cadr x)))) + vars)) + ebody)))) + +(defmacro lexical-let* (bindings &rest body) + "(lexical-let* BINDINGS BODY...): like `let*', but lexically scoped. +The main visible difference is that lambdas inside BODY will create +lexical closures as in Common Lisp." + (if (null bindings) (cons 'progn body) + (setq bindings (reverse bindings)) + (while bindings + (setq body (list (list* 'lexical-let (list (cl-pop bindings)) body)))) + (car body))) + +(defun cl-defun-expander (func &rest rest) + (list 'progn + (list 'defalias (list 'quote func) + (list 'function (cons 'lambda rest))) + (list 'quote func))) + + +;;; Multiple values. + +(defmacro multiple-value-bind (vars form &rest body) + "(multiple-value-bind (SYM SYM...) FORM BODY): collect multiple return values. +FORM must return a list; the BODY is then executed with the first N elements +of this list bound (`let'-style) to each of the symbols SYM in turn. This +is analogous to the Common Lisp `multiple-value-bind' macro, using lists to +simulate true multiple return values. For compatibility, (values A B C) is +a synonym for (list A B C)." + (let ((temp (gensym)) (n -1)) + (list* 'let* (cons (list temp form) + (mapcar (function + (lambda (v) + (list v (list 'nth (setq n (1+ n)) temp)))) + vars)) + body))) + +(defmacro multiple-value-setq (vars form) + "(multiple-value-setq (SYM SYM...) FORM): collect multiple return values. +FORM must return a list; the first N elements of this list are stored in +each of the symbols SYM in turn. This is analogous to the Common Lisp +`multiple-value-setq' macro, using lists to simulate true multiple return +values. For compatibility, (values A B C) is a synonym for (list A B C)." + (cond ((null vars) (list 'progn form nil)) + ((null (cdr vars)) (list 'setq (car vars) (list 'car form))) + (t + (let* ((temp (gensym)) (n 0)) + (list 'let (list (list temp form)) + (list 'prog1 (list 'setq (cl-pop vars) (list 'car temp)) + (cons 'setq (apply 'nconc + (mapcar (function + (lambda (v) + (list v (list + 'nth + (setq n (1+ n)) + temp)))) + vars))))))))) + + +;;; Declarations. + +(defmacro locally (&rest body) (cons 'progn body)) +(defmacro the (type form) form) + +(defvar cl-proclaim-history t) ; for future compilers +(defvar cl-declare-stack t) ; for future compilers + +(defun cl-do-proclaim (spec hist) + (and hist (listp cl-proclaim-history) (cl-push spec cl-proclaim-history)) + (cond ((eq (car-safe spec) 'special) + (if (boundp 'byte-compile-bound-variables) + (setq byte-compile-bound-variables + (append (cdr spec) byte-compile-bound-variables)))) + + ((eq (car-safe spec) 'inline) + (while (setq spec (cdr spec)) + (or (memq (get (car spec) 'byte-optimizer) + '(nil byte-compile-inline-expand)) + (error "%s already has a byte-optimizer, can't make it inline" + (car spec))) + (put (car spec) 'byte-optimizer 'byte-compile-inline-expand))) + + ((eq (car-safe spec) 'notinline) + (while (setq spec (cdr spec)) + (if (eq (get (car spec) 'byte-optimizer) + 'byte-compile-inline-expand) + (put (car spec) 'byte-optimizer nil)))) + + ((eq (car-safe spec) 'optimize) + (let ((speed (assq (nth 1 (assq 'speed (cdr spec))) + '((0 nil) (1 t) (2 t) (3 t)))) + (safety (assq (nth 1 (assq 'safety (cdr spec))) + '((0 t) (1 t) (2 t) (3 nil))))) + (if speed (setq cl-optimize-speed (car speed) + byte-optimize (nth 1 speed))) + (if safety (setq cl-optimize-safety (car safety) + byte-compile-delete-errors (nth 1 safety))))) + + ((and (eq (car-safe spec) 'warn) (boundp 'byte-compile-warnings)) + (if (eq byte-compile-warnings t) + (setq byte-compile-warnings byte-compile-warning-types)) + (while (setq spec (cdr spec)) + (if (consp (car spec)) + (if (eq (cadar spec) 0) + (setq byte-compile-warnings + (delq (caar spec) byte-compile-warnings)) + (setq byte-compile-warnings + (adjoin (caar spec) byte-compile-warnings))))))) + nil) + +;;; Process any proclamations made before cl-macs was loaded. +(defvar cl-proclaims-deferred) +(let ((p (reverse cl-proclaims-deferred))) + (while p (cl-do-proclaim (cl-pop p) t)) + (setq cl-proclaims-deferred nil)) + +(defmacro declare (&rest specs) + (if (cl-compiling-file) + (while specs + (if (listp cl-declare-stack) (cl-push (car specs) cl-declare-stack)) + (cl-do-proclaim (cl-pop specs) nil))) + nil) + + + +;;; Generalized variables. + +(defmacro define-setf-method (func args &rest body) + "(define-setf-method NAME ARGLIST BODY...): define a `setf' method. +This method shows how to handle `setf's to places of the form (NAME ARGS...). +The argument forms ARGS are bound according to ARGLIST, as if NAME were +going to be expanded as a macro, then the BODY forms are executed and must +return a list of five elements: a temporary-variables list, a value-forms +list, a store-variables list (of length one), a store-form, and an access- +form. See `defsetf' for a simpler way to define most setf-methods." + (append '(eval-when (compile load eval)) + (if (stringp (car body)) + (list (list 'put (list 'quote func) '(quote setf-documentation) + (cl-pop body)))) + (list (cl-transform-function-property + func 'setf-method (cons args body))))) + +(defmacro defsetf (func arg1 &rest args) + "(defsetf NAME FUNC): define a `setf' method. +This macro is an easy-to-use substitute for `define-setf-method' that works +well for simple place forms. In the simple `defsetf' form, `setf's of +the form (setf (NAME ARGS...) VAL) are transformed to function or macro +calls of the form (FUNC ARGS... VAL). Example: (defsetf aref aset). +Alternate form: (defsetf NAME ARGLIST (STORE) BODY...). +Here, the above `setf' call is expanded by binding the argument forms ARGS +according to ARGLIST, binding the value form VAL to STORE, then executing +BODY, which must return a Lisp form that does the necessary `setf' operation. +Actually, ARGLIST and STORE may be bound to temporary variables which are +introduced automatically to preserve proper execution order of the arguments. +Example: (defsetf nth (n x) (v) (list 'setcar (list 'nthcdr n x) v))." + (if (listp arg1) + (let* ((largs nil) (largsr nil) + (temps nil) (tempsr nil) + (restarg nil) (rest-temps nil) + (store-var (car (prog1 (car args) (setq args (cdr args))))) + (store-temp (intern (format "--%s--temp--" store-var))) + (lets1 nil) (lets2 nil) + (docstr nil) (p arg1)) + (if (stringp (car args)) + (setq docstr (prog1 (car args) (setq args (cdr args))))) + (while (and p (not (eq (car p) '&aux))) + (if (eq (car p) '&rest) + (setq p (cdr p) restarg (car p)) + (or (memq (car p) '(&optional &key &allow-other-keys)) + (setq largs (cons (if (consp (car p)) (car (car p)) (car p)) + largs) + temps (cons (intern (format "--%s--temp--" (car largs))) + temps)))) + (setq p (cdr p))) + (setq largs (nreverse largs) temps (nreverse temps)) + (if restarg + (setq largsr (append largs (list restarg)) + rest-temps (intern (format "--%s--temp--" restarg)) + tempsr (append temps (list rest-temps))) + (setq largsr largs tempsr temps)) + (let ((p1 largs) (p2 temps)) + (while p1 + (setq lets1 (cons (list (car p2) + (list 'gensym (format "--%s--" (car p1)))) + lets1) + lets2 (cons (list (car p1) (car p2)) lets2) + p1 (cdr p1) p2 (cdr p2)))) + (if restarg (setq lets2 (cons (list restarg rest-temps) lets2))) + (append (list 'define-setf-method func arg1) + (and docstr (list docstr)) + (list + (list 'let* + (nreverse + (cons (list store-temp + (list 'gensym (format "--%s--" store-var))) + (if restarg + (append + (list + (list rest-temps + (list 'mapcar '(quote gensym) + restarg))) + lets1) + lets1))) + (list 'list ; 'values + (cons (if restarg 'list* 'list) tempsr) + (cons (if restarg 'list* 'list) largsr) + (list 'list store-temp) + (cons 'let* + (cons (nreverse + (cons (list store-var store-temp) + lets2)) + args)) + (cons (if restarg 'list* 'list) + (cons (list 'quote func) tempsr))))))) + (list 'defsetf func '(&rest args) '(store) + (let ((call (list 'cons (list 'quote arg1) + '(append args (list store))))) + (if (car args) + (list 'list '(quote progn) call 'store) + call))))) + +;;; Some standard place types from Common Lisp. +(defsetf aref aset) +(defsetf car setcar) +(defsetf cdr setcdr) +(defsetf elt (seq n) (store) + (list 'if (list 'listp seq) (list 'setcar (list 'nthcdr n seq) store) + (list 'aset seq n store))) +(defsetf get put) +(defsetf get* (x y &optional d) (store) (list 'put x y store)) +(defsetf gethash (x h &optional d) (store) (list 'cl-puthash x store h)) +(defsetf nth (n x) (store) (list 'setcar (list 'nthcdr n x) store)) +(defsetf subseq (seq start &optional end) (new) + (list 'progn (list 'replace seq new ':start1 start ':end1 end) new)) +(defsetf symbol-function fset) +(defsetf symbol-plist setplist) +(defsetf symbol-value set) + +;;; Various car/cdr aliases. Note that `cadr' is handled specially. +(defsetf first setcar) +(defsetf second (x) (store) (list 'setcar (list 'cdr x) store)) +(defsetf third (x) (store) (list 'setcar (list 'cddr x) store)) +(defsetf fourth (x) (store) (list 'setcar (list 'cdddr x) store)) +(defsetf fifth (x) (store) (list 'setcar (list 'nthcdr 4 x) store)) +(defsetf sixth (x) (store) (list 'setcar (list 'nthcdr 5 x) store)) +(defsetf seventh (x) (store) (list 'setcar (list 'nthcdr 6 x) store)) +(defsetf eighth (x) (store) (list 'setcar (list 'nthcdr 7 x) store)) +(defsetf ninth (x) (store) (list 'setcar (list 'nthcdr 8 x) store)) +(defsetf tenth (x) (store) (list 'setcar (list 'nthcdr 9 x) store)) +(defsetf rest setcdr) + +;;; Some more Emacs-related place types. +(defsetf buffer-file-name set-visited-file-name t) +(defsetf buffer-modified-p set-buffer-modified-p t) +(defsetf buffer-name rename-buffer t) +(defsetf buffer-string () (store) + (list 'progn '(erase-buffer) (list 'insert store))) +(defsetf buffer-substring cl-set-buffer-substring) +(defsetf current-buffer set-buffer) +(defsetf current-case-table set-case-table) +(defsetf current-column move-to-column t) +(defsetf current-global-map use-global-map t) +(defsetf current-input-mode () (store) + (list 'progn (list 'apply 'set-input-mode store) store)) +(defsetf current-local-map use-local-map t) +(defsetf current-window-configuration set-window-configuration t) +(defsetf default-file-modes set-default-file-modes t) +(defsetf default-value set-default) +(defsetf documentation-property put) +(defsetf extent-data set-extent-data) +(defsetf extent-face set-extent-face) +(defsetf extent-priority set-extent-priority) +(defsetf extent-end-position (ext) (store) + (list 'progn (list 'set-extent-endpoints (list 'extent-start-position ext) + store) store)) +(defsetf extent-start-position (ext) (store) + (list 'progn (list 'set-extent-endpoints store + (list 'extent-end-position ext)) store)) +(defsetf face-background (f &optional s) (x) (list 'set-face-background f x s)) +(defsetf face-background-pixmap (f &optional s) (x) + (list 'set-face-background-pixmap f x s)) +(defsetf face-font (f &optional s) (x) (list 'set-face-font f x s)) +(defsetf face-foreground (f &optional s) (x) (list 'set-face-foreground f x s)) +(defsetf face-underline-p (f &optional s) (x) + (list 'set-face-underline-p f x s)) +(defsetf file-modes set-file-modes t) +(defsetf frame-height set-screen-height t) +(defsetf frame-parameters modify-frame-parameters t) +(defsetf frame-visible-p cl-set-frame-visible-p) +(defsetf frame-width set-screen-width t) +(defsetf getenv setenv t) +(defsetf get-register set-register) +(defsetf global-key-binding global-set-key) +(defsetf keymap-parent set-keymap-parent) +(defsetf local-key-binding local-set-key) +(defsetf mark set-mark t) +(defsetf mark-marker set-mark t) +(defsetf marker-position set-marker t) +(defsetf match-data store-match-data t) +(defsetf mouse-position (scr) (store) + (list 'set-mouse-position scr (list 'car store) (list 'cadr store) + (list 'cddr store))) +(defsetf overlay-get overlay-put) +(defsetf overlay-start (ov) (store) + (list 'progn (list 'move-overlay ov store (list 'overlay-end ov)) store)) +(defsetf overlay-end (ov) (store) + (list 'progn (list 'move-overlay ov (list 'overlay-start ov) store) store)) +(defsetf point goto-char) +(defsetf point-marker goto-char t) +(defsetf point-max () (store) + (list 'progn (list 'narrow-to-region '(point-min) store) store)) +(defsetf point-min () (store) + (list 'progn (list 'narrow-to-region store '(point-max)) store)) +(defsetf process-buffer set-process-buffer) +(defsetf process-filter set-process-filter) +(defsetf process-sentinel set-process-sentinel) +(defsetf read-mouse-position (scr) (store) + (list 'set-mouse-position scr (list 'car store) (list 'cdr store))) +(defsetf screen-height set-screen-height t) +(defsetf screen-width set-screen-width t) +(defsetf selected-window select-window) +(defsetf selected-screen select-screen) +(defsetf selected-frame select-frame) +(defsetf standard-case-table set-standard-case-table) +(defsetf syntax-table set-syntax-table) +(defsetf visited-file-modtime set-visited-file-modtime t) +(defsetf window-buffer set-window-buffer t) +(defsetf window-display-table set-window-display-table t) +(defsetf window-dedicated-p set-window-dedicated-p t) +(defsetf window-height () (store) + (list 'progn (list 'enlarge-window (list '- store '(window-height))) store)) +(defsetf window-hscroll set-window-hscroll) +(defsetf window-point set-window-point) +(defsetf window-start set-window-start) +(defsetf window-width () (store) + (list 'progn (list 'enlarge-window (list '- store '(window-width)) t) store)) +(defsetf x-get-cutbuffer x-store-cutbuffer t) +(defsetf x-get-cut-buffer x-store-cut-buffer t) ; groan. +(defsetf x-get-secondary-selection x-own-secondary-selection t) +(defsetf x-get-selection x-own-selection t) + +;;; More complex setf-methods. +;;; These should take &environment arguments, but since full arglists aren't +;;; available while compiling cl-macs, we fake it by referring to the global +;;; variable cl-macro-environment directly. + +(define-setf-method apply (func arg1 &rest rest) + (or (and (memq (car-safe func) '(quote function function*)) + (symbolp (car-safe (cdr-safe func)))) + (error "First arg to apply in setf is not (function SYM): %s" func)) + (let* ((form (cons (nth 1 func) (cons arg1 rest))) + (method (get-setf-method form cl-macro-environment))) + (list (car method) (nth 1 method) (nth 2 method) + (cl-setf-make-apply (nth 3 method) (cadr func) (car method)) + (cl-setf-make-apply (nth 4 method) (cadr func) (car method))))) + +(defun cl-setf-make-apply (form func temps) + (if (eq (car form) 'progn) + (list* 'progn (cl-setf-make-apply (cadr form) func temps) (cddr form)) + (or (equal (last form) (last temps)) + (error "%s is not suitable for use with setf-of-apply" func)) + (list* 'apply (list 'quote (car form)) (cdr form)))) + +(define-setf-method nthcdr (n place) + (let ((method (get-setf-method place cl-macro-environment)) + (n-temp (gensym "--nthcdr-n--")) + (store-temp (gensym "--nthcdr-store--"))) + (list (cons n-temp (car method)) + (cons n (nth 1 method)) + (list store-temp) + (list 'let (list (list (car (nth 2 method)) + (list 'cl-set-nthcdr n-temp (nth 4 method) + store-temp))) + (nth 3 method) store-temp) + (list 'nthcdr n-temp (nth 4 method))))) + +(define-setf-method getf (place tag &optional def) + (let ((method (get-setf-method place cl-macro-environment)) + (tag-temp (gensym "--getf-tag--")) + (def-temp (gensym "--getf-def--")) + (store-temp (gensym "--getf-store--"))) + (list (append (car method) (list tag-temp def-temp)) + (append (nth 1 method) (list tag def)) + (list store-temp) + (list 'let (list (list (car (nth 2 method)) + (list 'cl-set-getf (nth 4 method) + tag-temp store-temp))) + (nth 3 method) store-temp) + (list 'getf (nth 4 method) tag-temp def-temp)))) + +(define-setf-method substring (place from &optional to) + (let ((method (get-setf-method place cl-macro-environment)) + (from-temp (gensym "--substring-from--")) + (to-temp (gensym "--substring-to--")) + (store-temp (gensym "--substring-store--"))) + (list (append (car method) (list from-temp to-temp)) + (append (nth 1 method) (list from to)) + (list store-temp) + (list 'let (list (list (car (nth 2 method)) + (list 'cl-set-substring (nth 4 method) + from-temp to-temp store-temp))) + (nth 3 method) store-temp) + (list 'substring (nth 4 method) from-temp to-temp)))) + +;;; Getting and optimizing setf-methods. +(defun get-setf-method (place &optional env) + "Return a list of five values describing the setf-method for PLACE. +PLACE may be any Lisp form which can appear as the PLACE argument to +a macro like `setf' or `incf'." + (if (symbolp place) + (let ((temp (gensym "--setf--"))) + (list nil nil (list temp) (list 'setq place temp) place)) + (or (and (symbolp (car place)) + (let* ((func (car place)) + (name (symbol-name func)) + (method (get func 'setf-method)) + (case-fold-search nil)) + (or (and method + (let ((cl-macro-environment env)) + (setq method (apply method (cdr place)))) + (if (and (consp method) (= (length method) 5)) + method + (error "Setf-method for %s returns malformed method" + func))) + (and (string-match "\\`c[ad][ad][ad]?[ad]?r\\'" name) + (get-setf-method (compiler-macroexpand place))) + (and (eq func 'edebug-after) + (get-setf-method (nth (1- (length place)) place) + env))))) + (if (eq place (setq place (macroexpand place env))) + (if (and (symbolp (car place)) (fboundp (car place)) + (symbolp (symbol-function (car place)))) + (get-setf-method (cons (symbol-function (car place)) + (cdr place)) env) + (error "No setf-method known for %s" (car place))) + (get-setf-method place env))))) + +(defun cl-setf-do-modify (place opt-expr) + (let* ((method (get-setf-method place cl-macro-environment)) + (temps (car method)) (values (nth 1 method)) + (lets nil) (subs nil) + (optimize (and (not (eq opt-expr 'no-opt)) + (or (and (not (eq opt-expr 'unsafe)) + (cl-safe-expr-p opt-expr)) + (cl-setf-simple-store-p (car (nth 2 method)) + (nth 3 method))))) + (simple (and optimize (consp place) (cl-simple-exprs-p (cdr place))))) + (while values + (if (or simple (cl-const-expr-p (car values))) + (cl-push (cons (cl-pop temps) (cl-pop values)) subs) + (cl-push (list (cl-pop temps) (cl-pop values)) lets))) + (list (nreverse lets) + (cons (car (nth 2 method)) (sublis subs (nth 3 method))) + (sublis subs (nth 4 method))))) + +(defun cl-setf-do-store (spec val) + (let ((sym (car spec)) + (form (cdr spec))) + (if (or (cl-const-expr-p val) + (and (cl-simple-expr-p val) (eq (cl-expr-contains form sym) 1)) + (cl-setf-simple-store-p sym form)) + (subst val sym form) + (list 'let (list (list sym val)) form)))) + +(defun cl-setf-simple-store-p (sym form) + (and (consp form) (eq (cl-expr-contains form sym) 1) + (eq (nth (1- (length form)) form) sym) + (symbolp (car form)) (fboundp (car form)) + (not (eq (car-safe (symbol-function (car form))) 'macro)))) + +;;; The standard modify macros. +(defmacro setf (&rest args) + "(setf PLACE VAL PLACE VAL ...): set each PLACE to the value of its VAL. +This is a generalized version of `setq'; the PLACEs may be symbolic +references such as (car x) or (aref x i), as well as plain symbols. +For example, (setf (cadar x) y) is equivalent to (setcar (cdar x) y). +The return value is the last VAL in the list." + (if (cdr (cdr args)) + (let ((sets nil)) + (while args (cl-push (list 'setf (cl-pop args) (cl-pop args)) sets)) + (cons 'progn (nreverse sets))) + (if (symbolp (car args)) + (and args (cons 'setq args)) + (let* ((method (cl-setf-do-modify (car args) (nth 1 args))) + (store (cl-setf-do-store (nth 1 method) (nth 1 args)))) + (if (car method) (list 'let* (car method) store) store))))) + +(defmacro psetf (&rest args) + "(psetf PLACE VAL PLACE VAL ...): set PLACEs to the values VALs in parallel. +This is like `setf', except that all VAL forms are evaluated (in order) +before assigning any PLACEs to the corresponding values." + (let ((p args) (simple t) (vars nil)) + (while p + (if (or (not (symbolp (car p))) (cl-expr-depends-p (nth 1 p) vars)) + (setq simple nil)) + (if (memq (car p) vars) + (error "Destination duplicated in psetf: %s" (car p))) + (cl-push (cl-pop p) vars) + (or p (error "Odd number of arguments to psetf")) + (cl-pop p)) + (if simple + (list 'progn (cons 'setf args) nil) + (setq args (reverse args)) + (let ((expr (list 'setf (cadr args) (car args)))) + (while (setq args (cddr args)) + (setq expr (list 'setf (cadr args) (list 'prog1 (car args) expr)))) + (list 'progn expr nil))))) + +(defun cl-do-pop (place) + (if (cl-simple-expr-p place) + (list 'prog1 (list 'car place) (list 'setf place (list 'cdr place))) + (let* ((method (cl-setf-do-modify place t)) + (temp (gensym "--pop--"))) + (list 'let* + (append (car method) + (list (list temp (nth 2 method)))) + (list 'prog1 + (list 'car temp) + (cl-setf-do-store (nth 1 method) (list 'cdr temp))))))) + +(defmacro remf (place tag) + "(remf PLACE TAG): remove TAG from property list PLACE. +PLACE may be a symbol, or any generalized variable allowed by `setf'. +The form returns true if TAG was found and removed, nil otherwise." + (let* ((method (cl-setf-do-modify place t)) + (tag-temp (and (not (cl-const-expr-p tag)) (gensym "--remf-tag--"))) + (val-temp (and (not (cl-simple-expr-p place)) + (gensym "--remf-place--"))) + (ttag (or tag-temp tag)) + (tval (or val-temp (nth 2 method)))) + (list 'let* + (append (car method) + (and val-temp (list (list val-temp (nth 2 method)))) + (and tag-temp (list (list tag-temp tag)))) + (list 'if (list 'eq ttag (list 'car tval)) + (list 'progn + (cl-setf-do-store (nth 1 method) (list 'cddr tval)) + t) + (list 'cl-do-remf tval ttag))))) + +(defmacro shiftf (place &rest args) + "(shiftf PLACE PLACE... VAL): shift left among PLACEs. +Example: (shiftf A B C) sets A to B, B to C, and returns the old A. +Each PLACE may be a symbol, or any generalized variable allowed by `setf'." + (if (not (memq nil (mapcar 'symbolp (butlast (cons place args))))) + (list* 'prog1 place + (let ((sets nil)) + (while args + (cl-push (list 'setq place (car args)) sets) + (setq place (cl-pop args))) + (nreverse sets))) + (let* ((places (reverse (cons place args))) + (form (cl-pop places))) + (while places + (let ((method (cl-setf-do-modify (cl-pop places) 'unsafe))) + (setq form (list 'let* (car method) + (list 'prog1 (nth 2 method) + (cl-setf-do-store (nth 1 method) form)))))) + form))) + +(defmacro rotatef (&rest args) + "(rotatef PLACE...): rotate left among PLACEs. +Example: (rotatef A B C) sets A to B, B to C, and C to A. It returns nil. +Each PLACE may be a symbol, or any generalized variable allowed by `setf'." + (if (not (memq nil (mapcar 'symbolp args))) + (and (cdr args) + (let ((sets nil) + (first (car args))) + (while (cdr args) + (setq sets (nconc sets (list (cl-pop args) (car args))))) + (nconc (list 'psetf) sets (list (car args) first)))) + (let* ((places (reverse args)) + (temp (gensym "--rotatef--")) + (form temp)) + (while (cdr places) + (let ((method (cl-setf-do-modify (cl-pop places) 'unsafe))) + (setq form (list 'let* (car method) + (list 'prog1 (nth 2 method) + (cl-setf-do-store (nth 1 method) form)))))) + (let ((method (cl-setf-do-modify (car places) 'unsafe))) + (list 'let* (append (car method) (list (list temp (nth 2 method)))) + (cl-setf-do-store (nth 1 method) form) nil))))) + +(defmacro letf (bindings &rest body) + "(letf ((PLACE VALUE) ...) BODY...): temporarily bind to PLACEs. +This is the analogue of `let', but with generalized variables (in the +sense of `setf') for the PLACEs. Each PLACE is set to the corresponding +VALUE, then the BODY forms are executed. On exit, either normally or +because of a `throw' or error, the PLACEs are set back to their original +values. Note that this macro is *not* available in Common Lisp. +As a special case, if `(PLACE)' is used instead of `(PLACE VALUE)', +the PLACE is not modified before executing BODY." + (if (and (not (cdr bindings)) (cdar bindings) (symbolp (caar bindings))) + (list* 'let bindings body) + (let ((lets nil) (sets nil) + (unsets nil) (rev (reverse bindings))) + (while rev + (let* ((place (if (symbolp (caar rev)) + (list 'symbol-value (list 'quote (caar rev))) + (caar rev))) + (value (cadar rev)) + (method (cl-setf-do-modify place 'no-opt)) + (save (gensym "--letf-save--")) + (bound (and (memq (car place) '(symbol-value symbol-function)) + (gensym "--letf-bound--"))) + (temp (and (not (cl-const-expr-p value)) (cdr bindings) + (gensym "--letf-val--")))) + (setq lets (nconc (car method) + (if bound + (list (list bound + (list (if (eq (car place) + 'symbol-value) + 'boundp 'fboundp) + (nth 1 (nth 2 method)))) + (list save (list 'and bound + (nth 2 method)))) + (list (list save (nth 2 method)))) + (and temp (list (list temp value))) + lets) + body (list + (list 'unwind-protect + (cons 'progn + (if (cdr (car rev)) + (cons (cl-setf-do-store (nth 1 method) + (or temp value)) + body) + body)) + (if bound + (list 'if bound + (cl-setf-do-store (nth 1 method) save) + (list (if (eq (car place) 'symbol-value) + 'makunbound 'fmakunbound) + (nth 1 (nth 2 method)))) + (cl-setf-do-store (nth 1 method) save)))) + rev (cdr rev)))) + (list* 'let* lets body)))) + +(defmacro letf* (bindings &rest body) + "(letf* ((PLACE VALUE) ...) BODY...): temporarily bind to PLACEs. +This is the analogue of `let*', but with generalized variables (in the +sense of `setf') for the PLACEs. Each PLACE is set to the corresponding +VALUE, then the BODY forms are executed. On exit, either normally or +because of a `throw' or error, the PLACEs are set back to their original +values. Note that this macro is *not* available in Common Lisp. +As a special case, if `(PLACE)' is used instead of `(PLACE VALUE)', +the PLACE is not modified before executing BODY." + (if (null bindings) + (cons 'progn body) + (setq bindings (reverse bindings)) + (while bindings + (setq body (list (list* 'letf (list (cl-pop bindings)) body)))) + (car body))) + +(defmacro callf (func place &rest args) + "(callf FUNC PLACE ARGS...): set PLACE to (FUNC PLACE ARGS...). +FUNC should be an unquoted function name. PLACE may be a symbol, +or any generalized variable allowed by `setf'." + (let* ((method (cl-setf-do-modify place (cons 'list args))) + (rargs (cons (nth 2 method) args))) + (list 'let* (car method) + (cl-setf-do-store (nth 1 method) + (if (symbolp func) (cons func rargs) + (list* 'funcall (list 'function func) + rargs)))))) + +(defmacro callf2 (func arg1 place &rest args) + "(callf2 FUNC ARG1 PLACE ARGS...): set PLACE to (FUNC ARG1 PLACE ARGS...). +Like `callf', but PLACE is the second argument of FUNC, not the first." + (if (and (cl-safe-expr-p arg1) (cl-simple-expr-p place) (symbolp func)) + (list 'setf place (list* func arg1 place args)) + (let* ((method (cl-setf-do-modify place (cons 'list args))) + (temp (and (not (cl-const-expr-p arg1)) (gensym "--arg1--"))) + (rargs (list* (or temp arg1) (nth 2 method) args))) + (list 'let* (append (and temp (list (list temp arg1))) (car method)) + (cl-setf-do-store (nth 1 method) + (if (symbolp func) (cons func rargs) + (list* 'funcall (list 'function func) + rargs))))))) + +(defmacro define-modify-macro (name arglist func &optional doc) + "(define-modify-macro NAME ARGLIST FUNC): define a `setf'-like modify macro. +If NAME is called, it combines its PLACE argument with the other arguments +from ARGLIST using FUNC: (define-modify-macro incf (&optional (n 1)) +)" + (if (memq '&key arglist) (error "&key not allowed in define-modify-macro")) + (let ((place (gensym "--place--"))) + (list 'defmacro* name (cons place arglist) doc + (list* (if (memq '&rest arglist) 'list* 'list) + '(quote callf) (list 'quote func) place + (cl-arglist-args arglist))))) + + +;;; Structures. + +(defmacro defstruct (struct &rest descs) + "(defstruct (NAME OPTIONS...) (SLOT SLOT-OPTS...)...): define a struct type. +This macro defines a new Lisp data type called NAME, which contains data +stored in SLOTs. This defines a `make-NAME' constructor, a `copy-NAME' +copier, a `NAME-p' predicate, and setf-able `NAME-SLOT' accessors." + (let* ((name (if (consp struct) (car struct) struct)) + (opts (cdr-safe struct)) + (slots nil) + (defaults nil) + (conc-name (concat (symbol-name name) "-")) + (constructor (intern (format "make-%s" name))) + (constrs nil) + (copier (intern (format "copy-%s" name))) + (predicate (intern (format "%s-p" name))) + (print-func nil) (print-auto nil) + (safety (if (cl-compiling-file) cl-optimize-safety 3)) + (include nil) + (tag (intern (format "cl-struct-%s" name))) + (tag-symbol (intern (format "cl-struct-%s-tags" name))) + (include-descs nil) + (include-tag-symbol nil) + (side-eff nil) + (type nil) + (named nil) + (forms nil) + pred-form pred-check) + (if (stringp (car descs)) + (cl-push (list 'put (list 'quote name) '(quote structure-documentation) + (cl-pop descs)) forms)) + (setq descs (cons '(cl-tag-slot) + (mapcar (function (lambda (x) (if (consp x) x (list x)))) + descs))) + (while opts + (let ((opt (if (consp (car opts)) (caar opts) (car opts))) + (args (cdr-safe (cl-pop opts)))) + (cond ((eq opt ':conc-name) + (if args + (setq conc-name (if (car args) + (symbol-name (car args)) "")))) + ((eq opt ':constructor) + (if (cdr args) + (cl-push args constrs) + (if args (setq constructor (car args))))) + ((eq opt ':copier) + (if args (setq copier (car args)))) + ((eq opt ':predicate) + (if args (setq predicate (car args)))) + ((eq opt ':include) + (setq include (car args) + include-descs (mapcar (function + (lambda (x) + (if (consp x) x (list x)))) + (cdr args)) + include-tag-symbol (intern (format "cl-struct-%s-tags" + include)))) + ((eq opt ':print-function) + (setq print-func (car args))) + ((eq opt ':type) + (setq type (car args))) + ((eq opt ':named) + (setq named t)) + ((eq opt ':initial-offset) + (setq descs (nconc (make-list (car args) '(cl-skip-slot)) + descs))) + (t + (error "Slot option %s unrecognized" opt))))) + (if print-func + (setq print-func (list 'progn + (list 'funcall (list 'function print-func) + 'cl-x 'cl-s 'cl-n) t)) + (or type (and include (not (get include 'cl-struct-print))) + (setq print-auto t + print-func (and (or (not (or include type)) (null print-func)) + (list 'progn + (list 'princ (format "#S(%s" name) + 'cl-s)))))) + (if include + (let ((inc-type (get include 'cl-struct-type)) + (old-descs (get include 'cl-struct-slots))) + (or inc-type (error "%s is not a struct name" include)) + (and type (not (eq (car inc-type) type)) + (error ":type disagrees with :include for %s" name)) + (while include-descs + (setcar (memq (or (assq (caar include-descs) old-descs) + (error "No slot %s in included struct %s" + (caar include-descs) include)) + old-descs) + (cl-pop include-descs))) + (setq descs (append old-descs (delq (assq 'cl-tag-slot descs) descs)) + type (car inc-type) + named (assq 'cl-tag-slot descs)) + (if (cadr inc-type) (setq tag name named t)) + (cl-push (list 'pushnew (list 'quote tag) include-tag-symbol) + forms)) + (if type + (progn + (or (memq type '(vector list)) + (error "Illegal :type specifier: %s" type)) + (if named (setq tag name))) + (setq type 'vector named 'true))) + (or named (setq descs (delq (assq 'cl-tag-slot descs) descs))) + (cl-push (list 'defvar tag-symbol) forms) + (setq pred-form (and named + (let ((pos (- (length descs) + (length (memq (assq 'cl-tag-slot descs) + descs))))) + (if (eq type 'vector) + (list 'and '(vectorp cl-x) + (list '>= '(length cl-x) (length descs)) + (list 'memq (list 'aref 'cl-x pos) + tag-symbol)) + (if (= pos 0) + (list 'memq '(car-safe cl-x) tag-symbol) + (list 'and '(consp cl-x) + (list 'memq (list 'nth pos 'cl-x) + tag-symbol)))))) + pred-check (and pred-form (> safety 0) + (if (and (eq (caadr pred-form) 'vectorp) + (= safety 1)) + (cons 'and (cdddr pred-form)) pred-form))) + (let ((pos 0) (descp descs)) + (while descp + (let* ((desc (cl-pop descp)) + (slot (car desc))) + (if (memq slot '(cl-tag-slot cl-skip-slot)) + (progn + (cl-push nil slots) + (cl-push (and (eq slot 'cl-tag-slot) (list 'quote tag)) + defaults)) + (if (assq slot descp) + (error "Duplicate slots named %s in %s" slot name)) + (let ((accessor (intern (format "%s%s" conc-name slot)))) + (cl-push slot slots) + (cl-push (nth 1 desc) defaults) + (cl-push (list* + 'defsubst* accessor '(cl-x) + (append + (and pred-check + (list (list 'or pred-check + (list 'error + (format "%s accessing a non-%s" + accessor name) + 'cl-x)))) + (list (if (eq type 'vector) (list 'aref 'cl-x pos) + (if (= pos 0) '(car cl-x) + (list 'nth pos 'cl-x)))))) forms) + (cl-push (cons accessor t) side-eff) + (cl-push (list 'define-setf-method accessor '(cl-x) + (if (cadr (memq ':read-only (cddr desc))) + (list 'error (format "%s is a read-only slot" + accessor)) + (list 'cl-struct-setf-expander 'cl-x + (list 'quote name) (list 'quote accessor) + (and pred-check (list 'quote pred-check)) + pos))) + forms) + (if print-auto + (nconc print-func + (list (list 'princ (format " %s" slot) 'cl-s) + (list 'prin1 (list accessor 'cl-x) 'cl-s))))))) + (setq pos (1+ pos)))) + (setq slots (nreverse slots) + defaults (nreverse defaults)) + (and predicate pred-form + (progn (cl-push (list 'defsubst* predicate '(cl-x) + (if (eq (car pred-form) 'and) + (append pred-form '(t)) + (list 'and pred-form t))) forms) + (cl-push (cons predicate 'error-free) side-eff))) + (and copier + (progn (cl-push (list 'defun copier '(x) '(copy-sequence x)) forms) + (cl-push (cons copier t) side-eff))) + (if constructor + (cl-push (list constructor + (cons '&key (delq nil (copy-sequence slots)))) + constrs)) + (while constrs + (let* ((name (caar constrs)) + (args (cadr (cl-pop constrs))) + (anames (cl-arglist-args args)) + (make (mapcar* (function (lambda (s d) (if (memq s anames) s d))) + slots defaults))) + (cl-push (list 'defsubst* name + (list* '&cl-defs (list 'quote (cons nil descs)) args) + (cons type make)) forms) + (if (cl-safe-expr-p (cons 'progn (mapcar 'second descs))) + (cl-push (cons name t) side-eff)))) + (if print-auto (nconc print-func (list '(princ ")" cl-s) t))) + (if print-func + (cl-push (list 'push + (list 'function + (list 'lambda '(cl-x cl-s cl-n) + (list 'and pred-form print-func))) + 'custom-print-functions) forms)) + (cl-push (list 'setq tag-symbol (list 'list (list 'quote tag))) forms) + (cl-push (list* 'eval-when '(compile load eval) + (list 'put (list 'quote name) '(quote cl-struct-slots) + (list 'quote descs)) + (list 'put (list 'quote name) '(quote cl-struct-type) + (list 'quote (list type (eq named t)))) + (list 'put (list 'quote name) '(quote cl-struct-print) + print-auto) + (mapcar (function (lambda (x) + (list 'put (list 'quote (car x)) + '(quote side-effect-free) + (list 'quote (cdr x))))) + side-eff)) + forms) + (cons 'progn (nreverse (cons (list 'quote name) forms))))) + +(defun cl-struct-setf-expander (x name accessor pred-form pos) + (let* ((temp (gensym "--x--")) (store (gensym "--store--"))) + (list (list temp) (list x) (list store) + (append '(progn) + (and pred-form + (list (list 'or (subst temp 'cl-x pred-form) + (list 'error + (format + "%s storing a non-%s" accessor name) + temp)))) + (list (if (eq (car (get name 'cl-struct-type)) 'vector) + (list 'aset temp pos store) + (list 'setcar + (if (<= pos 5) + (let ((xx temp)) + (while (>= (setq pos (1- pos)) 0) + (setq xx (list 'cdr xx))) + xx) + (list 'nthcdr pos temp)) + store)))) + (list accessor temp)))) + + +;;; Types and assertions. + +(defmacro deftype (name args &rest body) + "(deftype NAME ARGLIST BODY...): define NAME as a new data type. +The type name can then be used in `typecase', `check-type', etc." + (list 'eval-when '(compile load eval) + (cl-transform-function-property + name 'cl-deftype-handler (cons (list* '&cl-defs ''('*) args) body)))) + +(defun cl-make-type-test (val type) + (if (memq type '(character string-char)) (setq type '(integer 0 255))) + (if (symbolp type) + (cond ((get type 'cl-deftype-handler) + (cl-make-type-test val (funcall (get type 'cl-deftype-handler)))) + ((memq type '(nil t)) type) + ((eq type 'null) (list 'null val)) + ((eq type 'float) (list 'floatp-safe val)) + ((eq type 'real) (list 'numberp val)) + ((eq type 'fixnum) (list 'integerp val)) + (t + (let* ((name (symbol-name type)) + (namep (intern (concat name "p")))) + (if (fboundp namep) (list namep val) + (list (intern (concat name "-p")) val))))) + (cond ((get (car type) 'cl-deftype-handler) + (cl-make-type-test val (apply (get (car type) 'cl-deftype-handler) + (cdr type)))) + ((memq (car-safe type) '(integer float real number)) + (delq t (list 'and (cl-make-type-test val (car type)) + (if (memq (cadr type) '(* nil)) t + (if (consp (cadr type)) (list '> val (caadr type)) + (list '>= val (cadr type)))) + (if (memq (caddr type) '(* nil)) t + (if (consp (caddr type)) (list '< val (caaddr type)) + (list '<= val (caddr type))))))) + ((memq (car-safe type) '(and or not)) + (cons (car type) + (mapcar (function (lambda (x) (cl-make-type-test val x))) + (cdr type)))) + ((memq (car-safe type) '(member member*)) + (list 'and (list 'member* val (list 'quote (cdr type))) t)) + ((eq (car-safe type) 'satisfies) (list (cadr type) val)) + (t (error "Bad type spec: %s" type))))) + +(defun typep (val type) ; See compiler macro below. + "Check that OBJECT is of type TYPE. +TYPE is a Common Lisp-style type specifier." + (eval (cl-make-type-test 'val type))) + +(defmacro check-type (form type &optional string) + "Verify that FORM is of type TYPE; signal an error if not. +STRING is an optional description of the desired type." + (and (or (not (cl-compiling-file)) + (< cl-optimize-speed 3) (= cl-optimize-safety 3)) + (let* ((temp (if (cl-simple-expr-p form 3) form (gensym))) + (body (list 'or (cl-make-type-test temp type) + (list 'signal '(quote wrong-type-argument) + (list 'list (or string (list 'quote type)) + temp (list 'quote form)))))) + (if (eq temp form) (list 'progn body nil) + (list 'let (list (list temp form)) body nil))))) + +(defmacro assert (form &optional show-args string &rest args) + "Verify that FORM returns non-nil; signal an error if not. +Second arg SHOW-ARGS means to include arguments of FORM in message. +Other args STRING and ARGS... are arguments to be passed to `error'. +They are not evaluated unless the assertion fails. If STRING is +omitted, a default message listing FORM itself is used." + (and (or (not (cl-compiling-file)) + (< cl-optimize-speed 3) (= cl-optimize-safety 3)) + (let ((sargs (and show-args (delq nil (mapcar + (function + (lambda (x) + (and (not (cl-const-expr-p x)) + x))) (cdr form)))))) + (list 'progn + (list 'or form + (if string + (list* 'error string (append sargs args)) + (list 'signal '(quote cl-assertion-failed) + (list* 'list (list 'quote form) sargs)))) + nil)))) + +(defmacro ignore-errors (&rest body) + "Execute FORMS; if an error occurs, return nil. +Otherwise, return result of last FORM." + (let ((err (gensym))) + (list 'condition-case err (cons 'progn body) '(error nil)))) + + +;;; Some predicates for analyzing Lisp forms. These are used by various +;;; macro expanders to optimize the results in certain common cases. + +(defconst cl-simple-funcs '(car cdr nth aref elt if and or + - 1+ 1- min max + car-safe cdr-safe progn prog1 prog2)) +(defconst cl-safe-funcs '(* / % length memq list vector vectorp + < > <= >= = error)) + +;;; Check if no side effects, and executes quickly. +(defun cl-simple-expr-p (x &optional size) + (or size (setq size 10)) + (if (and (consp x) (not (memq (car x) '(quote function function*)))) + (and (symbolp (car x)) + (or (memq (car x) cl-simple-funcs) + (get (car x) 'side-effect-free)) + (progn + (setq size (1- size)) + (while (and (setq x (cdr x)) + (setq size (cl-simple-expr-p (car x) size)))) + (and (null x) (>= size 0) size))) + (and (> size 0) (1- size)))) + +(defun cl-simple-exprs-p (xs) + (while (and xs (cl-simple-expr-p (car xs))) + (setq xs (cdr xs))) + (not xs)) + +;;; Check if no side effects. +(defun cl-safe-expr-p (x) + (or (not (and (consp x) (not (memq (car x) '(quote function function*))))) + (and (symbolp (car x)) + (or (memq (car x) cl-simple-funcs) + (memq (car x) cl-safe-funcs) + (get (car x) 'side-effect-free)) + (progn + (while (and (setq x (cdr x)) (cl-safe-expr-p (car x)))) + (null x))))) + +;;; Check if constant (i.e., no side effects or dependencies). +(defun cl-const-expr-p (x) + (cond ((consp x) + (or (eq (car x) 'quote) + (and (memq (car x) '(function function*)) + (or (symbolp (nth 1 x)) + (and (eq (car-safe (nth 1 x)) 'lambda) 'func))))) + ((symbolp x) (and (memq x '(nil t)) t)) + (t t))) + +(defun cl-const-exprs-p (xs) + (while (and xs (cl-const-expr-p (car xs))) + (setq xs (cdr xs))) + (not xs)) + +(defun cl-const-expr-val (x) + (and (eq (cl-const-expr-p x) t) (if (consp x) (nth 1 x) x))) + +(defun cl-expr-access-order (x v) + (if (cl-const-expr-p x) v + (if (consp x) + (progn + (while (setq x (cdr x)) (setq v (cl-expr-access-order (car x) v))) + v) + (if (eq x (car v)) (cdr v) '(t))))) + +;;; Count number of times X refers to Y. Return NIL for 0 times. +(defun cl-expr-contains (x y) + (cond ((equal y x) 1) + ((and (consp x) (not (memq (car-safe x) '(quote function function*)))) + (let ((sum 0)) + (while x + (setq sum (+ sum (or (cl-expr-contains (cl-pop x) y) 0)))) + (and (> sum 0) sum))) + (t nil))) + +(defun cl-expr-contains-any (x y) + (while (and y (not (cl-expr-contains x (car y)))) (cl-pop y)) + y) + +;;; Check whether X may depend on any of the symbols in Y. +(defun cl-expr-depends-p (x y) + (and (not (cl-const-expr-p x)) + (or (not (cl-safe-expr-p x)) (cl-expr-contains-any x y)))) + + +;;; Compiler macros. + +(defmacro define-compiler-macro (func args &rest body) + "(define-compiler-macro FUNC ARGLIST BODY...): Define a compiler-only macro. +This is like `defmacro', but macro expansion occurs only if the call to +FUNC is compiled (i.e., not interpreted). Compiler macros should be used +for optimizing the way calls to FUNC are compiled; the form returned by +BODY should do the same thing as a call to the normal function called +FUNC, though possibly more efficiently. Note that, like regular macros, +compiler macros are expanded repeatedly until no further expansions are +possible. Unlike regular macros, BODY can decide to \"punt\" and leave the +original function call alone by declaring an initial `&whole foo' parameter +and then returning foo." + (let ((p (if (listp args) args (list '&rest args))) (res nil)) + (while (consp p) (cl-push (cl-pop p) res)) + (setq args (nreverse res)) (setcdr res (and p (list '&rest p)))) + (list 'eval-when '(compile load eval) + (cl-transform-function-property + func 'cl-compiler-macro + (cons (if (memq '&whole args) (delq '&whole args) + (cons '--cl-whole-arg-- args)) body)) + (list 'or (list 'get (list 'quote func) '(quote byte-compile)) + (list 'put (list 'quote func) '(quote byte-compile) + '(quote cl-byte-compile-compiler-macro))))) + +(defun compiler-macroexpand (form) + (while + (let ((func (car-safe form)) (handler nil)) + (while (and (symbolp func) + (not (setq handler (get func 'cl-compiler-macro))) + (fboundp func) + (or (not (eq (car-safe (symbol-function func)) 'autoload)) + (load (nth 1 (symbol-function func))))) + (setq func (symbol-function func))) + (and handler + (not (eq form (setq form (apply handler form (cdr form)))))))) + form) + +(defun cl-byte-compile-compiler-macro (form) + (if (eq form (setq form (compiler-macroexpand form))) + (byte-compile-normal-call form) + (byte-compile-form form))) + +(defmacro defsubst* (name args &rest body) + "(defsubst* NAME ARGLIST [DOCSTRING] BODY...): define NAME as a function. +Like `defun', except the function is automatically declared `inline', +ARGLIST allows full Common Lisp conventions, and BODY is implicitly +surrounded by (block NAME ...)." + (let* ((argns (cl-arglist-args args)) (p argns) + (pbody (cons 'progn body)) + (unsafe (not (cl-safe-expr-p pbody)))) + (while (and p (eq (cl-expr-contains args (car p)) 1)) (cl-pop p)) + (list 'progn + (if p nil ; give up if defaults refer to earlier args + (list 'define-compiler-macro name + (list* '&whole 'cl-whole '&cl-quote args) + (list* 'cl-defsubst-expand (list 'quote argns) + (list 'quote (list* 'block name body)) + (not (or unsafe (cl-expr-access-order pbody argns))) + (and (memq '&key args) 'cl-whole) unsafe argns))) + (list* 'defun* name args body)))) + +(defun cl-defsubst-expand (argns body simple whole unsafe &rest argvs) + (if (and whole (not (cl-safe-expr-p (cons 'progn argvs)))) whole + (if (cl-simple-exprs-p argvs) (setq simple t)) + (let ((lets (delq nil + (mapcar* (function + (lambda (argn argv) + (if (or simple (cl-const-expr-p argv)) + (progn (setq body (subst argv argn body)) + (and unsafe (list argn argv))) + (list argn argv)))) + argns argvs)))) + (if lets (list 'let lets body) body)))) + + +;;; Compile-time optimizations for some functions defined in this package. +;;; Note that cl.el arranges to force cl-macs to be loaded at compile-time, +;;; mainly to make sure these macros will be present. + +(put 'eql 'byte-compile nil) +(define-compiler-macro eql (&whole form a b) + (cond ((eq (cl-const-expr-p a) t) + (let ((val (cl-const-expr-val a))) + (if (and (numberp val) (not (integerp val))) + (list 'equal a b) + (list 'eq a b)))) + ((eq (cl-const-expr-p b) t) + (let ((val (cl-const-expr-val b))) + (if (and (numberp val) (not (integerp val))) + (list 'equal a b) + (list 'eq a b)))) + ((cl-simple-expr-p a 5) + (list 'if (list 'numberp a) + (list 'equal a b) + (list 'eq a b))) + ((and (cl-safe-expr-p a) + (cl-simple-expr-p b 5)) + (list 'if (list 'numberp b) + (list 'equal a b) + (list 'eq a b))) + (t form))) + +(define-compiler-macro member* (&whole form a list &rest keys) + (let ((test (and (= (length keys) 2) (eq (car keys) ':test) + (cl-const-expr-val (nth 1 keys))))) + (cond ((eq test 'eq) (list 'memq a list)) + ((eq test 'equal) (list 'member a list)) + ((or (null keys) (eq test 'eql)) + (if (eq (cl-const-expr-p a) t) + (list (if (floatp-safe (cl-const-expr-val a)) 'member 'memq) + a list) + (if (eq (cl-const-expr-p list) t) + (let ((p (cl-const-expr-val list)) (mb nil) (mq nil)) + (if (not (cdr p)) + (and p (list 'eql a (list 'quote (car p)))) + (while p + (if (floatp-safe (car p)) (setq mb t) + (or (integerp (car p)) (symbolp (car p)) (setq mq t))) + (setq p (cdr p))) + (if (not mb) (list 'memq a list) + (if (not mq) (list 'member a list) form)))) + form))) + (t form)))) + +(define-compiler-macro assoc* (&whole form a list &rest keys) + (let ((test (and (= (length keys) 2) (eq (car keys) ':test) + (cl-const-expr-val (nth 1 keys))))) + (cond ((eq test 'eq) (list 'assq a list)) + ((eq test 'equal) (list 'assoc a list)) + ((and (eq (cl-const-expr-p a) t) (or (null keys) (eq test 'eql))) + (if (floatp-safe (cl-const-expr-val a)) + (list 'assoc a list) (list 'assq a list))) + (t form)))) + +(define-compiler-macro adjoin (&whole form a list &rest keys) + (if (and (cl-simple-expr-p a) (cl-simple-expr-p list) + (not (memq ':key keys))) + (list 'if (list* 'member* a list keys) list (list 'cons a list)) + form)) + +(define-compiler-macro list* (arg &rest others) + (let* ((args (reverse (cons arg others))) + (form (car args))) + (while (setq args (cdr args)) + (setq form (list 'cons (car args) form))) + form)) + +(define-compiler-macro get* (sym prop &optional def) + (if def + (list 'getf (list 'symbol-plist sym) prop def) + (list 'get sym prop))) + +(define-compiler-macro typep (&whole form val type) + (if (cl-const-expr-p type) + (let ((res (cl-make-type-test val (cl-const-expr-val type)))) + (if (or (memq (cl-expr-contains res val) '(nil 1)) + (cl-simple-expr-p val)) res + (let ((temp (gensym))) + (list 'let (list (list temp val)) (subst temp val res))))) + form)) + + +(mapcar (function + (lambda (y) + (put (car y) 'side-effect-free t) + (put (car y) 'byte-compile 'cl-byte-compile-compiler-macro) + (put (car y) 'cl-compiler-macro + (list 'lambda '(w x) + (if (symbolp (cadr y)) + (list 'list (list 'quote (cadr y)) + (list 'list (list 'quote (caddr y)) 'x)) + (cons 'list (cdr y))))))) + '((first 'car x) (second 'cadr x) (third 'caddr x) (fourth 'cadddr x) + (fifth 'nth 4 x) (sixth 'nth 5 x) (seventh 'nth 6 x) + (eighth 'nth 7 x) (ninth 'nth 8 x) (tenth 'nth 9 x) + (rest 'cdr x) (endp 'null x) (plusp '> x 0) (minusp '< x 0) + (caar car car) (cadr car cdr) (cdar cdr car) (cddr cdr cdr) + (caaar car caar) (caadr car cadr) (cadar car cdar) + (caddr car cddr) (cdaar cdr caar) (cdadr cdr cadr) + (cddar cdr cdar) (cdddr cdr cddr) (caaaar car caaar) + (caaadr car caadr) (caadar car cadar) (caaddr car caddr) + (cadaar car cdaar) (cadadr car cdadr) (caddar car cddar) + (cadddr car cdddr) (cdaaar cdr caaar) (cdaadr cdr caadr) + (cdadar cdr cadar) (cdaddr cdr caddr) (cddaar cdr cdaar) + (cddadr cdr cdadr) (cdddar cdr cddar) (cddddr cdr cdddr) )) + +;;; Things that are inline. +(proclaim '(inline floatp-safe acons map concatenate notany notevery + cl-set-elt revappend nreconc gethash)) + +;;; Things that are side-effect-free. +(mapcar (function (lambda (x) (put x 'side-effect-free t))) + '(oddp evenp abs expt signum last butlast ldiff pairlis gcd lcm + isqrt floor* ceiling* truncate* round* mod* rem* subseq + list-length get* getf gethash hash-table-count)) + +;;; Things that are side-effect-and-error-free. +(mapcar (function (lambda (x) (put x 'side-effect-free 'error-free))) + '(eql floatp-safe list* subst acons equalp random-state-p + copy-tree sublis hash-table-p)) + + +(run-hooks 'cl-macs-load-hook) + +;;; cl-macs.el ends here
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lisp/emacs-lisp/cl-seq.el Fri Jul 30 20:15:09 1993 +0000 @@ -0,0 +1,920 @@ +;; cl-seq.el --- Common Lisp extensions for GNU Emacs Lisp (part three) + +;; Copyright (C) 1993 Free Software Foundation, Inc. + +;; Author: Dave Gillespie <daveg@synaptics.com> +;; Version: 2.02 +;; Keywords: extensions + +;; This file is part of GNU Emacs. + +;; GNU Emacs is free software; you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation; either version 1, or (at your option) +;; any later version. + +;; GNU Emacs is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with GNU Emacs; see the file COPYING. If not, write to +;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + +;; Commentary: + +;; These are extensions to Emacs Lisp that provide a degree of +;; Common Lisp compatibility, beyond what is already built-in +;; in Emacs Lisp. +;; +;; This package was written by Dave Gillespie; it is a complete +;; rewrite of Cesar Quiroz's original cl.el package of December 1986. +;; +;; This package works with Emacs 18, Emacs 19, and Lucid Emacs 19. +;; +;; Bug reports, comments, and suggestions are welcome! + +;; This file contains the Common Lisp sequence and list functions +;; which take keyword arguments. + +;; See cl.el for Change Log. + + +;; Code: + +(or (memq 'cl-19 features) + (error "Tried to load `cl-seq' before `cl'!")) + + +;;; We define these here so that this file can compile without having +;;; loaded the cl.el file already. + +(defmacro cl-push (x place) (list 'setq place (list 'cons x place))) +(defmacro cl-pop (place) + (list 'car (list 'prog1 place (list 'setq place (list 'cdr place))))) + + +;;; Keyword parsing. This is special-cased here so that we can compile +;;; this file independent from cl-macs. + +(defmacro cl-parsing-keywords (kwords other-keys &rest body) + (cons + 'let* + (cons (mapcar + (function + (lambda (x) + (let* ((var (if (consp x) (car x) x)) + (mem (list 'car (list 'cdr (list 'memq (list 'quote var) + 'cl-keys))))) + (if (eq var ':test-not) + (setq mem (list 'and mem (list 'setq 'cl-test mem) t))) + (if (eq var ':if-not) + (setq mem (list 'and mem (list 'setq 'cl-if mem) t))) + (list (intern + (format "cl-%s" (substring (symbol-name var) 1))) + (if (consp x) (list 'or mem (car (cdr x))) mem))))) + kwords) + (append + (and (not (eq other-keys t)) + (list + (list 'let '((cl-keys-temp cl-keys)) + (list 'while 'cl-keys-temp + (list 'or (list 'memq '(car cl-keys-temp) + (list 'quote + (mapcar + (function + (lambda (x) + (if (consp x) + (car x) x))) + (append kwords + other-keys)))) + '(car (cdr (memq (quote :allow-other-keys) + cl-keys))) + '(error "Bad keyword argument %s" + (car cl-keys-temp))) + '(setq cl-keys-temp (cdr (cdr cl-keys-temp))))))) + body)))) +(put 'cl-parsing-keywords 'lisp-indent-function 2) +(put 'cl-parsing-keywords 'edebug-form-spec '(sexp sexp &rest form)) + +(defmacro cl-check-key (x) + (list 'if 'cl-key (list 'funcall 'cl-key x) x)) + +(defmacro cl-check-test-nokey (item x) + (list 'cond + (list 'cl-test + (list 'eq (list 'not (list 'funcall 'cl-test item x)) + 'cl-test-not)) + (list 'cl-if + (list 'eq (list 'not (list 'funcall 'cl-if x)) 'cl-if-not)) + (list 't (list 'if (list 'numberp item) + (list 'equal item x) (list 'eq item x))))) + +(defmacro cl-check-test (item x) + (list 'cl-check-test-nokey item (list 'cl-check-key x))) + +(defmacro cl-check-match (x y) + (setq x (list 'cl-check-key x) y (list 'cl-check-key y)) + (list 'if 'cl-test + (list 'eq (list 'not (list 'funcall 'cl-test x y)) 'cl-test-not) + (list 'if (list 'numberp x) + (list 'equal x y) (list 'eq x y)))) + +(put 'cl-check-key 'edebug-form-spec 'edebug-forms) +(put 'cl-check-test 'edebug-form-spec 'edebug-forms) +(put 'cl-check-test-nokey 'edebug-form-spec 'edebug-forms) +(put 'cl-check-match 'edebug-form-spec 'edebug-forms) + +(defvar cl-test) (defvar cl-test-not) +(defvar cl-if) (defvar cl-if-not) +(defvar cl-key) + + +(defun reduce (cl-func cl-seq &rest cl-keys) + "Reduce two-argument FUNCTION across SEQUENCE. +Keywords supported: :start :end :from-end :initial-value :key" + (cl-parsing-keywords (:from-end (:start 0) :end :initial-value :key) () + (or (listp cl-seq) (setq cl-seq (append cl-seq nil))) + (setq cl-seq (subseq cl-seq cl-start cl-end)) + (if cl-from-end (setq cl-seq (nreverse cl-seq))) + (let ((cl-accum (cond ((memq ':initial-value cl-keys) cl-initial-value) + (cl-seq (cl-check-key (cl-pop cl-seq))) + (t (funcall cl-func))))) + (if cl-from-end + (while cl-seq + (setq cl-accum (funcall cl-func (cl-check-key (cl-pop cl-seq)) + cl-accum))) + (while cl-seq + (setq cl-accum (funcall cl-func cl-accum + (cl-check-key (cl-pop cl-seq)))))) + cl-accum))) + +(defun fill (seq item &rest cl-keys) + "Fill the elements of SEQ with ITEM. +Keywords supported: :start :end" + (cl-parsing-keywords ((:start 0) :end) () + (if (listp seq) + (let ((p (nthcdr cl-start seq)) + (n (if cl-end (- cl-end cl-start) 8000000))) + (while (and p (>= (setq n (1- n)) 0)) + (setcar p item) + (setq p (cdr p)))) + (or cl-end (setq cl-end (length seq))) + (if (and (= cl-start 0) (= cl-end (length seq))) + (fillarray seq item) + (while (< cl-start cl-end) + (aset seq cl-start item) + (setq cl-start (1+ cl-start))))) + seq)) + +(defun replace (cl-seq1 cl-seq2 &rest cl-keys) + "Replace the elements of SEQ1 with the elements of SEQ2. +SEQ1 is destructively modified, then returned. +Keywords supported: :start1 :end1 :start2 :end2" + (cl-parsing-keywords ((:start1 0) :end1 (:start2 0) :end2) () + (if (and (eq cl-seq1 cl-seq2) (<= cl-start2 cl-start1)) + (or (= cl-start1 cl-start2) + (let* ((cl-len (length cl-seq1)) + (cl-n (min (- (or cl-end1 cl-len) cl-start1) + (- (or cl-end2 cl-len) cl-start2)))) + (while (>= (setq cl-n (1- cl-n)) 0) + (cl-set-elt cl-seq1 (+ cl-start1 cl-n) + (elt cl-seq2 (+ cl-start2 cl-n)))))) + (if (listp cl-seq1) + (let ((cl-p1 (nthcdr cl-start1 cl-seq1)) + (cl-n1 (if cl-end1 (- cl-end1 cl-start1) 4000000))) + (if (listp cl-seq2) + (let ((cl-p2 (nthcdr cl-start2 cl-seq2)) + (cl-n (min cl-n1 + (if cl-end2 (- cl-end2 cl-start2) 4000000)))) + (while (and cl-p1 cl-p2 (>= (setq cl-n (1- cl-n)) 0)) + (setcar cl-p1 (car cl-p2)) + (setq cl-p1 (cdr cl-p1) cl-p2 (cdr cl-p2)))) + (setq cl-end2 (min (or cl-end2 (length cl-seq2)) + (+ cl-start2 cl-n1))) + (while (and cl-p1 (< cl-start2 cl-end2)) + (setcar cl-p1 (aref cl-seq2 cl-start2)) + (setq cl-p1 (cdr cl-p1) cl-start2 (1+ cl-start2))))) + (setq cl-end1 (min (or cl-end1 (length cl-seq1)) + (+ cl-start1 (- (or cl-end2 (length cl-seq2)) + cl-start2)))) + (if (listp cl-seq2) + (let ((cl-p2 (nthcdr cl-start2 cl-seq2))) + (while (< cl-start1 cl-end1) + (aset cl-seq1 cl-start1 (car cl-p2)) + (setq cl-p2 (cdr cl-p2) cl-start1 (1+ cl-start1)))) + (while (< cl-start1 cl-end1) + (aset cl-seq1 cl-start1 (aref cl-seq2 cl-start2)) + (setq cl-start2 (1+ cl-start2) cl-start1 (1+ cl-start1)))))) + cl-seq1)) + +(defun remove* (cl-item cl-seq &rest cl-keys) + "Remove all occurrences of ITEM in SEQ. +This is a non-destructive function; it makes a copy of SEQ if necessary +to avoid corrupting the original SEQ. +Keywords supported: :test :test-not :key :count :start :end :from-end" + (cl-parsing-keywords (:test :test-not :key :if :if-not :count :from-end + (:start 0) :end) () + (if (<= (or cl-count (setq cl-count 8000000)) 0) + cl-seq + (if (or (nlistp cl-seq) (and cl-from-end (< cl-count 4000000))) + (let ((cl-i (cl-position cl-item cl-seq cl-start cl-end + cl-from-end))) + (if cl-i + (let ((cl-res (apply 'delete* cl-item (append cl-seq nil) + (append (if cl-from-end + (list ':end (1+ cl-i)) + (list ':start cl-i)) + cl-keys)))) + (if (listp cl-seq) cl-res + (if (stringp cl-seq) (concat cl-res) (vconcat cl-res)))) + cl-seq)) + (setq cl-end (- (or cl-end 8000000) cl-start)) + (if (= cl-start 0) + (while (and cl-seq (> cl-end 0) + (cl-check-test cl-item (car cl-seq)) + (setq cl-end (1- cl-end) cl-seq (cdr cl-seq)) + (> (setq cl-count (1- cl-count)) 0)))) + (if (and (> cl-count 0) (> cl-end 0)) + (let ((cl-p (if (> cl-start 0) (nthcdr cl-start cl-seq) + (setq cl-end (1- cl-end)) (cdr cl-seq)))) + (while (and cl-p (> cl-end 0) + (not (cl-check-test cl-item (car cl-p)))) + (setq cl-p (cdr cl-p) cl-end (1- cl-end))) + (if (and cl-p (> cl-end 0)) + (nconc (ldiff cl-seq cl-p) + (if (= cl-count 1) (cdr cl-p) + (and (cdr cl-p) + (apply 'delete* cl-item + (copy-sequence (cdr cl-p)) + ':start 0 ':end (1- cl-end) + ':count (1- cl-count) cl-keys)))) + cl-seq)) + cl-seq))))) + +(defun remove-if (cl-pred cl-list &rest cl-keys) + "Remove all items satisfying PREDICATE in SEQ. +This is a non-destructive function; it makes a copy of SEQ if necessary +to avoid corrupting the original SEQ. +Keywords supported: :key :count :start :end :from-end" + (apply 'remove* nil cl-list ':if cl-pred cl-keys)) + +(defun remove-if-not (cl-pred cl-list &rest cl-keys) + "Remove all items not satisfying PREDICATE in SEQ. +This is a non-destructive function; it makes a copy of SEQ if necessary +to avoid corrupting the original SEQ. +Keywords supported: :key :count :start :end :from-end" + (apply 'remove* nil cl-list ':if-not cl-pred cl-keys)) + +(defun delete* (cl-item cl-seq &rest cl-keys) + "Remove all occurrences of ITEM in SEQ. +This is a destructive function; it reuses the storage of SEQ whenever possible. +Keywords supported: :test :test-not :key :count :start :end :from-end" + (cl-parsing-keywords (:test :test-not :key :if :if-not :count :from-end + (:start 0) :end) () + (if (<= (or cl-count (setq cl-count 8000000)) 0) + cl-seq + (if (listp cl-seq) + (if (and cl-from-end (< cl-count 4000000)) + (let (cl-i) + (while (and (>= (setq cl-count (1- cl-count)) 0) + (setq cl-i (cl-position cl-item cl-seq cl-start + cl-end cl-from-end))) + (if (= cl-i 0) (setq cl-seq (cdr cl-seq)) + (let ((cl-tail (nthcdr (1- cl-i) cl-seq))) + (setcdr cl-tail (cdr (cdr cl-tail))))) + (setq cl-end cl-i)) + cl-seq) + (setq cl-end (- (or cl-end 8000000) cl-start)) + (if (= cl-start 0) + (progn + (while (and cl-seq + (> cl-end 0) + (cl-check-test cl-item (car cl-seq)) + (setq cl-end (1- cl-end) cl-seq (cdr cl-seq)) + (> (setq cl-count (1- cl-count)) 0))) + (setq cl-end (1- cl-end))) + (setq cl-start (1- cl-start))) + (if (and (> cl-count 0) (> cl-end 0)) + (let ((cl-p (nthcdr cl-start cl-seq))) + (while (and (cdr cl-p) (> cl-end 0)) + (if (cl-check-test cl-item (car (cdr cl-p))) + (progn + (setcdr cl-p (cdr (cdr cl-p))) + (if (= (setq cl-count (1- cl-count)) 0) + (setq cl-end 1))) + (setq cl-p (cdr cl-p))) + (setq cl-end (1- cl-end))))) + cl-seq) + (apply 'remove* cl-item cl-seq cl-keys))))) + +(defun delete-if (cl-pred cl-list &rest cl-keys) + "Remove all items satisfying PREDICATE in SEQ. +This is a destructive function; it reuses the storage of SEQ whenever possible. +Keywords supported: :key :count :start :end :from-end" + (apply 'delete* nil cl-list ':if cl-pred cl-keys)) + +(defun delete-if-not (cl-pred cl-list &rest cl-keys) + "Remove all items not satisfying PREDICATE in SEQ. +This is a destructive function; it reuses the storage of SEQ whenever possible. +Keywords supported: :key :count :start :end :from-end" + (apply 'delete* nil cl-list ':if-not cl-pred cl-keys)) + +(or (and (fboundp 'delete) (subrp (symbol-function 'delete))) + (defalias 'delete (function (lambda (x y) (delete* x y ':test 'equal))))) +(defun remove (x y) (remove* x y ':test 'equal)) +(defun remq (x y) (if (memq x y) (delq x (copy-list y)) y)) + +(defun remove-duplicates (cl-seq &rest cl-keys) + "Return a copy of SEQ with all duplicate elements removed. +Keywords supported: :test :test-not :key :start :end :from-end" + (cl-delete-duplicates cl-seq cl-keys t)) + +(defun delete-duplicates (cl-seq &rest cl-keys) + "Remove all duplicate elements from SEQ (destructively). +Keywords supported: :test :test-not :key :start :end :from-end" + (cl-delete-duplicates cl-seq cl-keys nil)) + +(defun cl-delete-duplicates (cl-seq cl-keys cl-copy) + (if (listp cl-seq) + (cl-parsing-keywords (:test :test-not :key (:start 0) :end :from-end :if) + () + (if cl-from-end + (let ((cl-p (nthcdr cl-start cl-seq)) cl-i) + (setq cl-end (- (or cl-end (length cl-seq)) cl-start)) + (while (> cl-end 1) + (setq cl-i 0) + (while (setq cl-i (cl-position (cl-check-key (car cl-p)) + (cdr cl-p) cl-i (1- cl-end))) + (if cl-copy (setq cl-seq (copy-sequence cl-seq) + cl-p (nthcdr cl-start cl-seq) cl-copy nil)) + (let ((cl-tail (nthcdr cl-i cl-p))) + (setcdr cl-tail (cdr (cdr cl-tail)))) + (setq cl-end (1- cl-end))) + (setq cl-p (cdr cl-p) cl-end (1- cl-end) + cl-start (1+ cl-start))) + cl-seq) + (setq cl-end (- (or cl-end (length cl-seq)) cl-start)) + (while (and (cdr cl-seq) (= cl-start 0) (> cl-end 1) + (cl-position (cl-check-key (car cl-seq)) + (cdr cl-seq) 0 (1- cl-end))) + (setq cl-seq (cdr cl-seq) cl-end (1- cl-end))) + (let ((cl-p (if (> cl-start 0) (nthcdr (1- cl-start) cl-seq) + (setq cl-end (1- cl-end) cl-start 1) cl-seq))) + (while (and (cdr (cdr cl-p)) (> cl-end 1)) + (if (cl-position (cl-check-key (car (cdr cl-p))) + (cdr (cdr cl-p)) 0 (1- cl-end)) + (progn + (if cl-copy (setq cl-seq (copy-sequence cl-seq) + cl-p (nthcdr (1- cl-start) cl-seq) + cl-copy nil)) + (setcdr cl-p (cdr (cdr cl-p)))) + (setq cl-p (cdr cl-p))) + (setq cl-end (1- cl-end) cl-start (1+ cl-start))) + cl-seq))) + (let ((cl-res (cl-delete-duplicates (append cl-seq nil) cl-keys nil))) + (if (stringp cl-seq) (concat cl-res) (vconcat cl-res))))) + +(defun substitute (cl-new cl-old cl-seq &rest cl-keys) + "Substitute NEW for OLD in SEQ. +This is a non-destructive function; it makes a copy of SEQ if necessary +to avoid corrupting the original SEQ. +Keywords supported: :test :test-not :key :count :start :end :from-end" + (cl-parsing-keywords (:test :test-not :key :if :if-not :count + (:start 0) :end :from-end) () + (if (or (eq cl-old cl-new) + (<= (or cl-count (setq cl-from-end nil cl-count 8000000)) 0)) + cl-seq + (let ((cl-i (cl-position cl-old cl-seq cl-start cl-end))) + (if (not cl-i) + cl-seq + (setq cl-seq (copy-sequence cl-seq)) + (or cl-from-end + (progn (cl-set-elt cl-seq cl-i cl-new) + (setq cl-i (1+ cl-i) cl-count (1- cl-count)))) + (apply 'nsubstitute cl-new cl-old cl-seq ':count cl-count + ':start cl-i cl-keys)))))) + +(defun substitute-if (cl-new cl-pred cl-list &rest cl-keys) + "Substitute NEW for all items satisfying PREDICATE in SEQ. +This is a non-destructive function; it makes a copy of SEQ if necessary +to avoid corrupting the original SEQ. +Keywords supported: :key :count :start :end :from-end" + (apply 'substitute cl-new nil cl-list ':if cl-pred cl-keys)) + +(defun substitute-if-not (cl-new cl-pred cl-list &rest cl-keys) + "Substitute NEW for all items not satisfying PREDICATE in SEQ. +This is a non-destructive function; it makes a copy of SEQ if necessary +to avoid corrupting the original SEQ. +Keywords supported: :key :count :start :end :from-end" + (apply 'substitute cl-new nil cl-list ':if-not cl-pred cl-keys)) + +(defun nsubstitute (cl-new cl-old cl-seq &rest cl-keys) + "Substitute NEW for OLD in SEQ. +This is a destructive function; it reuses the storage of SEQ whenever possible. +Keywords supported: :test :test-not :key :count :start :end :from-end" + (cl-parsing-keywords (:test :test-not :key :if :if-not :count + (:start 0) :end :from-end) () + (or (eq cl-old cl-new) (<= (or cl-count (setq cl-count 8000000)) 0) + (if (and (listp cl-seq) (or (not cl-from-end) (> cl-count 4000000))) + (let ((cl-p (nthcdr cl-start cl-seq))) + (setq cl-end (- (or cl-end 8000000) cl-start)) + (while (and cl-p (> cl-end 0) (> cl-count 0)) + (if (cl-check-test cl-old (car cl-p)) + (progn + (setcar cl-p cl-new) + (setq cl-count (1- cl-count)))) + (setq cl-p (cdr cl-p) cl-end (1- cl-end)))) + (or cl-end (setq cl-end (length cl-seq))) + (if cl-from-end + (while (and (< cl-start cl-end) (> cl-count 0)) + (setq cl-end (1- cl-end)) + (if (cl-check-test cl-old (elt cl-seq cl-end)) + (progn + (cl-set-elt cl-seq cl-end cl-new) + (setq cl-count (1- cl-count))))) + (while (and (< cl-start cl-end) (> cl-count 0)) + (if (cl-check-test cl-old (aref cl-seq cl-start)) + (progn + (aset cl-seq cl-start cl-new) + (setq cl-count (1- cl-count)))) + (setq cl-start (1+ cl-start)))))) + cl-seq)) + +(defun nsubstitute-if (cl-new cl-pred cl-list &rest cl-keys) + "Substitute NEW for all items satisfying PREDICATE in SEQ. +This is a destructive function; it reuses the storage of SEQ whenever possible. +Keywords supported: :key :count :start :end :from-end" + (apply 'nsubstitute cl-new nil cl-list ':if cl-pred cl-keys)) + +(defun nsubstitute-if-not (cl-new cl-pred cl-list &rest cl-keys) + "Substitute NEW for all items not satisfying PREDICATE in SEQ. +This is a destructive function; it reuses the storage of SEQ whenever possible. +Keywords supported: :key :count :start :end :from-end" + (apply 'nsubstitute cl-new nil cl-list ':if-not cl-pred cl-keys)) + +(defun find (cl-item cl-seq &rest cl-keys) + "Find the first occurrence of ITEM in LIST. +Return the matching ITEM, or nil if not found. +Keywords supported: :test :test-not :key :start :end :from-end" + (let ((cl-pos (apply 'position cl-item cl-seq cl-keys))) + (and cl-pos (elt cl-seq cl-pos)))) + +(defun find-if (cl-pred cl-list &rest cl-keys) + "Find the first item satisfying PREDICATE in LIST. +Return the matching ITEM, or nil if not found. +Keywords supported: :key :start :end :from-end" + (apply 'find nil cl-list ':if cl-pred cl-keys)) + +(defun find-if-not (cl-pred cl-list &rest cl-keys) + "Find the first item not satisfying PREDICATE in LIST. +Return the matching ITEM, or nil if not found. +Keywords supported: :key :start :end :from-end" + (apply 'find nil cl-list ':if-not cl-pred cl-keys)) + +(defun position (cl-item cl-seq &rest cl-keys) + "Find the first occurrence of ITEM in LIST. +Return the index of the matching item, or nil if not found. +Keywords supported: :test :test-not :key :start :end :from-end" + (cl-parsing-keywords (:test :test-not :key :if :if-not + (:start 0) :end :from-end) () + (cl-position cl-item cl-seq cl-start cl-end cl-from-end))) + +(defun cl-position (cl-item cl-seq cl-start &optional cl-end cl-from-end) + (if (listp cl-seq) + (let ((cl-p (nthcdr cl-start cl-seq))) + (or cl-end (setq cl-end 8000000)) + (let ((cl-res nil)) + (while (and cl-p (< cl-start cl-end) (or (not cl-res) cl-from-end)) + (if (cl-check-test cl-item (car cl-p)) + (setq cl-res cl-start)) + (setq cl-p (cdr cl-p) cl-start (1+ cl-start))) + cl-res)) + (or cl-end (setq cl-end (length cl-seq))) + (if cl-from-end + (progn + (while (and (>= (setq cl-end (1- cl-end)) cl-start) + (not (cl-check-test cl-item (aref cl-seq cl-end))))) + (and (>= cl-end cl-start) cl-end)) + (while (and (< cl-start cl-end) + (not (cl-check-test cl-item (aref cl-seq cl-start)))) + (setq cl-start (1+ cl-start))) + (and (< cl-start cl-end) cl-start)))) + +(defun position-if (cl-pred cl-list &rest cl-keys) + "Find the first item satisfying PREDICATE in LIST. +Return the index of the matching item, or nil if not found. +Keywords supported: :key :start :end :from-end" + (apply 'position nil cl-list ':if cl-pred cl-keys)) + +(defun position-if-not (cl-pred cl-list &rest cl-keys) + "Find the first item not satisfying PREDICATE in LIST. +Return the index of the matching item, or nil if not found. +Keywords supported: :key :start :end :from-end" + (apply 'position nil cl-list ':if-not cl-pred cl-keys)) + +(defun count (cl-item cl-seq &rest cl-keys) + "Count the number of occurrences of ITEM in LIST. +Keywords supported: :test :test-not :key :start :end" + (cl-parsing-keywords (:test :test-not :key :if :if-not (:start 0) :end) () + (let ((cl-count 0) cl-x) + (or cl-end (setq cl-end (length cl-seq))) + (if (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq))) + (while (< cl-start cl-end) + (setq cl-x (if (consp cl-seq) (cl-pop cl-seq) (aref cl-seq cl-start))) + (if (cl-check-test cl-item cl-x) (setq cl-count (1+ cl-count))) + (setq cl-start (1+ cl-start))) + cl-count))) + +(defun count-if (cl-pred cl-list &rest cl-keys) + "Count the number of items satisfying PREDICATE in LIST. +Keywords supported: :key :start :end" + (apply 'count nil cl-list ':if cl-pred cl-keys)) + +(defun count-if-not (cl-pred cl-list &rest cl-keys) + "Count the number of items not satisfying PREDICATE in LIST. +Keywords supported: :key :start :end" + (apply 'count nil cl-list ':if-not cl-pred cl-keys)) + +(defun mismatch (cl-seq1 cl-seq2 &rest cl-keys) + "Compare SEQ1 with SEQ2, return index of first mismatching element. +Return nil if the sequences match. If one sequence is a prefix of the +other, the return value indicates the end of the shorted sequence. +Keywords supported: :test :test-not :key :start1 :end1 :start2 :end2 :from-end" + (cl-parsing-keywords (:test :test-not :key :from-end + (:start1 0) :end1 (:start2 0) :end2) () + (or cl-end1 (setq cl-end1 (length cl-seq1))) + (or cl-end2 (setq cl-end2 (length cl-seq2))) + (if cl-from-end + (progn + (while (and (< cl-start1 cl-end1) (< cl-start2 cl-end2) + (cl-check-match (elt cl-seq1 (1- cl-end1)) + (elt cl-seq2 (1- cl-end2)))) + (setq cl-end1 (1- cl-end1) cl-end2 (1- cl-end2))) + (and (or (< cl-start1 cl-end1) (< cl-start2 cl-end2)) + (1- cl-end1))) + (let ((cl-p1 (and (listp cl-seq1) (nthcdr cl-start1 cl-seq1))) + (cl-p2 (and (listp cl-seq2) (nthcdr cl-start2 cl-seq2)))) + (while (and (< cl-start1 cl-end1) (< cl-start2 cl-end2) + (cl-check-match (if cl-p1 (car cl-p1) + (aref cl-seq1 cl-start1)) + (if cl-p2 (car cl-p2) + (aref cl-seq2 cl-start2)))) + (setq cl-p1 (cdr cl-p1) cl-p2 (cdr cl-p2) + cl-start1 (1+ cl-start1) cl-start2 (1+ cl-start2))) + (and (or (< cl-start1 cl-end1) (< cl-start2 cl-end2)) + cl-start1))))) + +(defun search (cl-seq1 cl-seq2 &rest cl-keys) + "Search for SEQ1 as a subsequence of SEQ2. +Return the index of the leftmost element of the first match found; +return nil if there are no matches. +Keywords supported: :test :test-not :key :start1 :end1 :start2 :end2 :from-end" + (cl-parsing-keywords (:test :test-not :key :from-end + (:start1 0) :end1 (:start2 0) :end2) () + (or cl-end1 (setq cl-end1 (length cl-seq1))) + (or cl-end2 (setq cl-end2 (length cl-seq2))) + (if (>= cl-start1 cl-end1) + (if cl-from-end cl-end2 cl-start2) + (let* ((cl-len (- cl-end1 cl-start1)) + (cl-first (cl-check-key (elt cl-seq1 cl-start1))) + (cl-if nil) cl-pos) + (setq cl-end2 (- cl-end2 (1- cl-len))) + (while (and (< cl-start2 cl-end2) + (setq cl-pos (cl-position cl-first cl-seq2 + cl-start2 cl-end2 cl-from-end)) + (apply 'mismatch cl-seq1 cl-seq2 + ':start1 (1+ cl-start1) ':end1 cl-end1 + ':start2 (1+ cl-pos) ':end2 (+ cl-pos cl-len) + ':from-end nil cl-keys)) + (if cl-from-end (setq cl-end2 cl-pos) (setq cl-start2 (1+ cl-pos)))) + (and (< cl-start2 cl-end2) cl-pos))))) + +(defun sort* (cl-seq cl-pred &rest cl-keys) + "Sort the argument SEQUENCE according to PREDICATE. +This is a destructive function; it reuses the storage of SEQUENCE if possible. +Keywords supported: :key" + (if (nlistp cl-seq) + (replace cl-seq (apply 'sort* (append cl-seq nil) cl-pred cl-keys)) + (cl-parsing-keywords (:key) () + (if (memq cl-key '(nil identity)) + (sort cl-seq cl-pred) + (sort cl-seq (function (lambda (cl-x cl-y) + (funcall cl-pred (funcall cl-key cl-x) + (funcall cl-key cl-y))))))))) + +(defun stable-sort (cl-seq cl-pred &rest cl-keys) + "Sort the argument SEQUENCE stably according to PREDICATE. +This is a destructive function; it reuses the storage of SEQUENCE if possible. +Keywords supported: :key" + (apply 'sort* cl-seq cl-pred cl-keys)) + +(defun merge (cl-type cl-seq1 cl-seq2 cl-pred &rest cl-keys) + "Destructively merge the two sequences to produce a new sequence. +TYPE is the sequence type to return, SEQ1 and SEQ2 are the two +argument sequences, and PRED is a `less-than' predicate on the elements. +Keywords supported: :key" + (or (listp cl-seq1) (setq cl-seq1 (append cl-seq1 nil))) + (or (listp cl-seq2) (setq cl-seq2 (append cl-seq2 nil))) + (cl-parsing-keywords (:key) () + (let ((cl-res nil)) + (while (and cl-seq1 cl-seq2) + (if (funcall cl-pred (cl-check-key (car cl-seq2)) + (cl-check-key (car cl-seq1))) + (cl-push (cl-pop cl-seq2) cl-res) + (cl-push (cl-pop cl-seq1) cl-res))) + (coerce (nconc (nreverse cl-res) cl-seq1 cl-seq2) cl-type)))) + +;;; See compiler macro in cl-macs.el +(defun member* (cl-item cl-list &rest cl-keys) + "Find the first occurrence of ITEM in LIST. +Return the sublist of LIST whose car is ITEM. +Keywords supported: :test :test-not :key" + (if cl-keys + (cl-parsing-keywords (:test :test-not :key :if :if-not) () + (while (and cl-list (not (cl-check-test cl-item (car cl-list)))) + (setq cl-list (cdr cl-list))) + cl-list) + (if (and (numberp cl-item) (not (integerp cl-item))) + (member cl-item cl-list) + (memq cl-item cl-list)))) + +(defun member-if (cl-pred cl-list &rest cl-keys) + "Find the first item satisfying PREDICATE in LIST. +Return the sublist of LIST whose car matches. +Keywords supported: :key" + (apply 'member* nil cl-list ':if cl-pred cl-keys)) + +(defun member-if-not (cl-pred cl-list &rest cl-keys) + "Find the first item not satisfying PREDICATE in LIST. +Return the sublist of LIST whose car matches. +Keywords supported: :key" + (apply 'member* nil cl-list ':if-not cl-pred cl-keys)) + +(defun cl-adjoin (cl-item cl-list &rest cl-keys) + (if (cl-parsing-keywords (:key) t + (apply 'member* (cl-check-key cl-item) cl-list cl-keys)) + cl-list + (cons cl-item cl-list))) + +;;; See compiler macro in cl-macs.el +(defun assoc* (cl-item cl-alist &rest cl-keys) + "Find the first item whose car matches ITEM in LIST. +Keywords supported: :test :test-not :key" + (if cl-keys + (cl-parsing-keywords (:test :test-not :key :if :if-not) () + (while (and cl-alist + (or (not (consp (car cl-alist))) + (not (cl-check-test cl-item (car (car cl-alist)))))) + (setq cl-alist (cdr cl-alist))) + (and cl-alist (car cl-alist))) + (if (and (numberp cl-item) (not (integerp cl-item))) + (assoc cl-item cl-alist) + (assq cl-item cl-alist)))) + +(defun assoc-if (cl-pred cl-list &rest cl-keys) + "Find the first item whose car satisfies PREDICATE in LIST. +Keywords supported: :key" + (apply 'assoc* nil cl-list ':if cl-pred cl-keys)) + +(defun assoc-if-not (cl-pred cl-list &rest cl-keys) + "Find the first item whose car does not satisfy PREDICATE in LIST. +Keywords supported: :key" + (apply 'assoc* nil cl-list ':if-not cl-pred cl-keys)) + +(defun rassoc* (cl-item cl-alist &rest cl-keys) + "Find the first item whose cdr matches ITEM in LIST. +Keywords supported: :test :test-not :key" + (if (or cl-keys (numberp cl-item)) + (cl-parsing-keywords (:test :test-not :key :if :if-not) () + (while (and cl-alist + (or (not (consp (car cl-alist))) + (not (cl-check-test cl-item (cdr (car cl-alist)))))) + (setq cl-alist (cdr cl-alist))) + (and cl-alist (car cl-alist))) + (rassq cl-item cl-alist))) + +(defun rassoc (item alist) (rassoc* item alist ':test 'equal)) + +(defun rassoc-if (cl-pred cl-list &rest cl-keys) + "Find the first item whose cdr satisfies PREDICATE in LIST. +Keywords supported: :key" + (apply 'rassoc* nil cl-list ':if cl-pred cl-keys)) + +(defun rassoc-if-not (cl-pred cl-list &rest cl-keys) + "Find the first item whose cdr does not satisfy PREDICATE in LIST. +Keywords supported: :key" + (apply 'rassoc* nil cl-list ':if-not cl-pred cl-keys)) + +(defun union (cl-list1 cl-list2 &rest cl-keys) + "Combine LIST1 and LIST2 using a set-union operation. +The result list contains all items that appear in either LIST1 or LIST2. +This is a non-destructive function; it makes a copy of the data if necessary +to avoid corrupting the original LIST1 and LIST2. +Keywords supported: :test :test-not :key" + (cond ((null cl-list1) cl-list2) ((null cl-list2) cl-list1) + ((equal cl-list1 cl-list2) cl-list1) + (t + (or (>= (length cl-list1) (length cl-list2)) + (setq cl-list1 (prog1 cl-list2 (setq cl-list2 cl-list1)))) + (while cl-list2 + (if (or cl-keys (numberp (car cl-list2))) + (setq cl-list1 (apply 'adjoin (car cl-list2) cl-list1 cl-keys)) + (or (memq (car cl-list2) cl-list1) + (cl-push (car cl-list2) cl-list1))) + (cl-pop cl-list2)) + cl-list1))) + +(defun nunion (cl-list1 cl-list2 &rest cl-keys) + "Combine LIST1 and LIST2 using a set-union operation. +The result list contains all items that appear in either LIST1 or LIST2. +This is a destructive function; it reuses the storage of LIST1 and LIST2 +whenever possible. +Keywords supported: :test :test-not :key" + (cond ((null cl-list1) cl-list2) ((null cl-list2) cl-list1) + (t (apply 'union cl-list1 cl-list2 cl-keys)))) + +(defun intersection (cl-list1 cl-list2 &rest cl-keys) + "Combine LIST1 and LIST2 using a set-intersection operation. +The result list contains all items that appear in both LIST1 and LIST2. +This is a non-destructive function; it makes a copy of the data if necessary +to avoid corrupting the original LIST1 and LIST2. +Keywords supported: :test :test-not :key" + (and cl-list1 cl-list2 + (if (equal cl-list1 cl-list2) cl-list1 + (cl-parsing-keywords (:key) (:test :test-not) + (let ((cl-res nil)) + (or (>= (length cl-list1) (length cl-list2)) + (setq cl-list1 (prog1 cl-list2 (setq cl-list2 cl-list1)))) + (while cl-list2 + (if (if (or cl-keys (numberp (car cl-list2))) + (apply 'member* (cl-check-key (car cl-list2)) + cl-list1 cl-keys) + (memq (car cl-list2) cl-list1)) + (cl-push (car cl-list2) cl-res)) + (cl-pop cl-list2)) + cl-res))))) + +(defun nintersection (cl-list1 cl-list2 &rest cl-keys) + "Combine LIST1 and LIST2 using a set-intersection operation. +The result list contains all items that appear in both LIST1 and LIST2. +This is a destructive function; it reuses the storage of LIST1 and LIST2 +whenever possible. +Keywords supported: :test :test-not :key" + (and cl-list1 cl-list2 (apply 'intersection cl-list1 cl-list2 cl-keys))) + +(defun set-difference (cl-list1 cl-list2 &rest cl-keys) + "Combine LIST1 and LIST2 using a set-difference operation. +The result list contains all items that appear in LIST1 but not LIST2. +This is a non-destructive function; it makes a copy of the data if necessary +to avoid corrupting the original LIST1 and LIST2. +Keywords supported: :test :test-not :key" + (if (or (null cl-list1) (null cl-list2)) cl-list1 + (cl-parsing-keywords (:key) (:test :test-not) + (let ((cl-res nil)) + (while cl-list1 + (or (if (or cl-keys (numberp (car cl-list1))) + (apply 'member* (cl-check-key (car cl-list1)) + cl-list2 cl-keys) + (memq (car cl-list1) cl-list2)) + (cl-push (car cl-list1) cl-res)) + (cl-pop cl-list1)) + cl-res)))) + +(defun nset-difference (cl-list1 cl-list2 &rest cl-keys) + "Combine LIST1 and LIST2 using a set-difference operation. +The result list contains all items that appear in LIST1 but not LIST2. +This is a destructive function; it reuses the storage of LIST1 and LIST2 +whenever possible. +Keywords supported: :test :test-not :key" + (if (or (null cl-list1) (null cl-list2)) cl-list1 + (apply 'set-difference cl-list1 cl-list2 cl-keys))) + +(defun set-exclusive-or (cl-list1 cl-list2 &rest cl-keys) + "Combine LIST1 and LIST2 using a set-exclusive-or operation. +The result list contains all items that appear in exactly one of LIST1, LIST2. +This is a non-destructive function; it makes a copy of the data if necessary +to avoid corrupting the original LIST1 and LIST2. +Keywords supported: :test :test-not :key" + (cond ((null cl-list1) cl-list2) ((null cl-list2) cl-list1) + ((equal cl-list1 cl-list2) nil) + (t (append (apply 'set-difference cl-list1 cl-list2 cl-keys) + (apply 'set-difference cl-list2 cl-list1 cl-keys))))) + +(defun nset-exclusive-or (cl-list1 cl-list2 &rest cl-keys) + "Combine LIST1 and LIST2 using a set-exclusive-or operation. +The result list contains all items that appear in exactly one of LIST1, LIST2. +This is a destructive function; it reuses the storage of LIST1 and LIST2 +whenever possible. +Keywords supported: :test :test-not :key" + (cond ((null cl-list1) cl-list2) ((null cl-list2) cl-list1) + ((equal cl-list1 cl-list2) nil) + (t (nconc (apply 'nset-difference cl-list1 cl-list2 cl-keys) + (apply 'nset-difference cl-list2 cl-list1 cl-keys))))) + +(defun subsetp (cl-list1 cl-list2 &rest cl-keys) + "True if LIST1 is a subset of LIST2. +I.e., if every element of LIST1 also appears in LIST2. +Keywords supported: :test :test-not :key" + (cond ((null cl-list1) t) ((null cl-list2) nil) + ((equal cl-list1 cl-list2) t) + (t (cl-parsing-keywords (:key) (:test :test-not) + (while (and cl-list1 + (apply 'member* (cl-check-key (car cl-list1)) + cl-list2 cl-keys)) + (cl-pop cl-list1)) + (null cl-list1))))) + +(defun subst-if (cl-new cl-pred cl-tree &rest cl-keys) + "Substitute NEW for elements matching PREDICATE in TREE (non-destructively). +Return a copy of TREE with all matching elements replaced by NEW. +Keywords supported: :key" + (apply 'sublis (list (cons nil cl-new)) cl-tree ':if cl-pred cl-keys)) + +(defun subst-if-not (cl-new cl-pred cl-tree &rest cl-keys) + "Substitute NEW for elts not matching PREDICATE in TREE (non-destructively). +Return a copy of TREE with all non-matching elements replaced by NEW. +Keywords supported: :key" + (apply 'sublis (list (cons nil cl-new)) cl-tree ':if-not cl-pred cl-keys)) + +(defun nsubst (cl-new cl-old cl-tree &rest cl-keys) + "Substitute NEW for OLD everywhere in TREE (destructively). +Any element of TREE which is `eql' to OLD is changed to NEW (via a call +to `setcar'). +Keywords supported: :test :test-not :key" + (apply 'nsublis (list (cons cl-old cl-new)) cl-tree cl-keys)) + +(defun nsubst-if (cl-new cl-pred cl-tree &rest cl-keys) + "Substitute NEW for elements matching PREDICATE in TREE (destructively). +Any element of TREE which matches is changed to NEW (via a call to `setcar'). +Keywords supported: :key" + (apply 'nsublis (list (cons nil cl-new)) cl-tree ':if cl-pred cl-keys)) + +(defun nsubst-if-not (cl-new cl-pred cl-tree &rest cl-keys) + "Substitute NEW for elements not matching PREDICATE in TREE (destructively). +Any element of TREE which matches is changed to NEW (via a call to `setcar'). +Keywords supported: :key" + (apply 'nsublis (list (cons nil cl-new)) cl-tree ':if-not cl-pred cl-keys)) + +(defun sublis (cl-alist cl-tree &rest cl-keys) + "Perform substitutions indicated by ALIST in TREE (non-destructively). +Return a copy of TREE with all matching elements replaced. +Keywords supported: :test :test-not :key" + (cl-parsing-keywords (:test :test-not :key :if :if-not) () + (cl-sublis-rec cl-tree))) + +(defvar cl-alist) +(defun cl-sublis-rec (cl-tree) ; uses cl-alist/key/test*/if* + (let ((cl-temp (cl-check-key cl-tree)) (cl-p cl-alist)) + (while (and cl-p (not (cl-check-test-nokey (car (car cl-p)) cl-temp))) + (setq cl-p (cdr cl-p))) + (if cl-p (cdr (car cl-p)) + (if (consp cl-tree) + (let ((cl-a (cl-sublis-rec (car cl-tree))) + (cl-d (cl-sublis-rec (cdr cl-tree)))) + (if (and (eq cl-a (car cl-tree)) (eq cl-d (cdr cl-tree))) + cl-tree + (cons cl-a cl-d))) + cl-tree)))) + +(defun nsublis (cl-alist cl-tree &rest cl-keys) + "Perform substitutions indicated by ALIST in TREE (destructively). +Any matching element of TREE is changed via a call to `setcar'. +Keywords supported: :test :test-not :key" + (cl-parsing-keywords (:test :test-not :key :if :if-not) () + (let ((cl-hold (list cl-tree))) + (cl-nsublis-rec cl-hold) + (car cl-hold)))) + +(defun cl-nsublis-rec (cl-tree) ; uses cl-alist/temp/p/key/test*/if* + (while (consp cl-tree) + (let ((cl-temp (cl-check-key (car cl-tree))) (cl-p cl-alist)) + (while (and cl-p (not (cl-check-test-nokey (car (car cl-p)) cl-temp))) + (setq cl-p (cdr cl-p))) + (if cl-p (setcar cl-tree (cdr (car cl-p))) + (if (consp (car cl-tree)) (cl-nsublis-rec (car cl-tree)))) + (setq cl-temp (cl-check-key (cdr cl-tree)) cl-p cl-alist) + (while (and cl-p (not (cl-check-test-nokey (car (car cl-p)) cl-temp))) + (setq cl-p (cdr cl-p))) + (if cl-p + (progn (setcdr cl-tree (cdr (car cl-p))) (setq cl-tree nil)) + (setq cl-tree (cdr cl-tree)))))) + +(defun tree-equal (cl-x cl-y &rest cl-keys) + "T if trees X and Y have `eql' leaves. +Atoms are compared by `eql'; cons cells are compared recursively. +Keywords supported: :test :test-not :key" + (cl-parsing-keywords (:test :test-not :key) () + (cl-tree-equal-rec cl-x cl-y))) + +(defun cl-tree-equal-rec (cl-x cl-y) + (while (and (consp cl-x) (consp cl-y) + (cl-tree-equal-rec (car cl-x) (car cl-y))) + (setq cl-x (cdr cl-x) cl-y (cdr cl-y))) + (and (not (consp cl-x)) (not (consp cl-y)) (cl-check-match cl-x cl-y))) + + +(run-hooks 'cl-seq-load-hook) + +;;; cl-seq.el ends here
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lisp/emacs-lisp/cl.el Fri Jul 30 20:15:09 1993 +0000 @@ -0,0 +1,757 @@ +;; cl.el --- Common Lisp extensions for GNU Emacs Lisp + +;; Copyright (C) 1993 Free Software Foundation, Inc. + +;; Author: Dave Gillespie <daveg@synaptics.com> +;; Version: 2.02 +;; Keywords: extensions + +;; This file is part of GNU Emacs. + +;; GNU Emacs is free software; you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation; either version 1, or (at your option) +;; any later version. + +;; GNU Emacs is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with GNU Emacs; see the file COPYING. If not, write to +;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + +;; Commentary: + +;; These are extensions to Emacs Lisp that provide a degree of +;; Common Lisp compatibility, beyond what is already built-in +;; in Emacs Lisp. +;; +;; This package was written by Dave Gillespie; it is a complete +;; rewrite of Cesar Quiroz's original cl.el package of December 1986. +;; +;; This package works with Emacs 18, Emacs 19, and Lucid Emacs 19. +;; +;; Bug reports, comments, and suggestions are welcome! + +;; This file contains the portions of the Common Lisp extensions +;; package which should always be present. + + +;; Future notes: + +;; Once Emacs 19 becomes standard, many things in this package which are +;; messy for reasons of compatibility can be greatly simplified. For now, +;; I prefer to maintain one unified version. + + +;; Change Log: + +;; Version 2.02 (30 Jul 93): +;; * Added "cl-compat.el" file, extra compatibility with old package. +;; * Added `lexical-let' and `lexical-let*'. +;; * Added `define-modify-macro', `callf', and `callf2'. +;; * Added `ignore-errors'. +;; * Changed `(setf (nthcdr N PLACE) X)' to work when N is zero. +;; * Merged `*gentemp-counter*' into `*gensym-counter*'. +;; * Extended `subseq' to allow negative START and END like `substring'. +;; * Added `in-ref', `across-ref', `elements of-ref' loop clauses. +;; * Added `concat', `vconcat' loop clauses. +;; * Cleaned up a number of compiler warnings. + +;; Version 2.01 (7 Jul 93): +;; * Added support for FSF version of Emacs 19. +;; * Added `add-hook' for Emacs 18 users. +;; * Added `defsubst*' and `symbol-macrolet'. +;; * Added `maplist', `mapc', `mapl', `mapcan', `mapcon'. +;; * Added `map', `concatenate', `reduce', `merge'. +;; * Added `revappend', `nreconc', `tailp', `tree-equal'. +;; * Added `assert', `check-type', `typecase', `typep', and `deftype'. +;; * Added destructuring and `&environment' support to `defmacro*'. +;; * Added destructuring to `loop', and added the following clauses: +;; `elements', `frames', `overlays', `intervals', `buffers', `key-seqs'. +;; * Renamed `delete' to `delete*' and `remove' to `remove*'. +;; * Completed support for all keywords in `remove*', `substitute', etc. +;; * Added `most-positive-float' and company. +;; * Fixed hash tables to work with latest Lucid Emacs. +;; * `proclaim' forms are no longer compile-time-evaluating; use `declaim'. +;; * Syntax for `warn' declarations has changed. +;; * Improved implementation of `random*'. +;; * Moved most sequence functions to a new file, cl-seq.el. +;; * Moved `eval-when' into cl-macs.el. +;; * Moved `pushnew' and `adjoin' to cl.el for most common cases. +;; * Moved `provide' forms down to ends of files. +;; * Changed expansion of `pop' to something that compiles to better code. +;; * Changed so that no patch is required for Emacs 19 byte compiler. +;; * Made more things dependent on `optimize' declarations. +;; * Added a partial implementation of struct print functions. +;; * Miscellaneous minor changes. + +;; Version 2.00: +;; * First public release of this package. + + +;; Code: + +(defvar cl-emacs-type (cond ((or (and (fboundp 'epoch::version) + (symbol-value 'epoch::version)) + (string-lessp emacs-version "19")) 18) + ((string-match "Lucid" emacs-version) 'lucid) + (t 19))) + +(or (fboundp 'defalias) (fset 'defalias 'fset)) + +(defvar cl-optimize-speed 1) +(defvar cl-optimize-safety 1) + + +;;; Keywords used in this package. + +(defconst :test ':test) +(defconst :test-not ':test-not) +(defconst :key ':key) +(defconst :start ':start) +(defconst :start1 ':start1) +(defconst :start2 ':start2) +(defconst :end ':end) +(defconst :end1 ':end1) +(defconst :end2 ':end2) +(defconst :count ':count) +(defconst :initial-value ':initial-value) +(defconst :size ':size) +(defconst :from-end ':from-end) +(defconst :rehash-size ':rehash-size) +(defconst :rehash-threshold ':rehash-threshold) +(defconst :allow-other-keys ':allow-other-keys) + + +(defvar custom-print-functions nil + "This is a list of functions that format user objects for printing. +Each function is called in turn with three arguments: the object, the +stream, and the print level (currently ignored). If it is able to +print the object it returns true; otherwise it returns nil and the +printer proceeds to the next function on the list. + +This variable is not used at present, but it is defined in hopes that +a future Emacs interpreter will be able to use it.") + + +;;; Predicates. + +(defun eql (a b) ; See compiler macro in cl-macs.el + "T if the two args are the same Lisp object. +Floating-point numbers of equal value are `eql', but they may not be `eq'." + (if (numberp a) + (equal a b) + (eq a b))) + + +;;; Generalized variables. These macros are defined here so that they +;;; can safely be used in .emacs files. + +(defmacro incf (place &optional x) + "(incf PLACE [X]): increment PLACE by X (1 by default). +PLACE may be a symbol, or any generalized variable allowed by `setf'. +The return value is the incremented value of PLACE." + (if (symbolp place) + (list 'setq place (if x (list '+ place x) (list '1+ place))) + (list 'callf '+ place (or x 1)))) + +(defmacro decf (place &optional x) + "(decf PLACE [X]): decrement PLACE by X (1 by default). +PLACE may be a symbol, or any generalized variable allowed by `setf'. +The return value is the decremented value of PLACE." + (if (symbolp place) + (list 'setq place (if x (list '- place x) (list '1- place))) + (list 'callf '- place (or x 1)))) + +(defmacro pop (place) + "(pop PLACE): remove and return the head of the list stored in PLACE. +Analogous to (prog1 (car PLACE) (setf PLACE (cdr PLACE))), though more +careful about evaluating each argument only once and in the right order. +PLACE may be a symbol, or any generalized variable allowed by `setf'." + (if (symbolp place) + (list 'car (list 'prog1 place (list 'setq place (list 'cdr place)))) + (cl-do-pop place))) + +(defmacro push (x place) + "(push X PLACE): insert X at the head of the list stored in PLACE. +Analogous to (setf PLACE (cons X PLACE)), though more careful about +evaluating each argument only once and in the right order. PLACE may +be a symbol, or any generalized variable allowed by `setf'." + (if (symbolp place) (list 'setq place (list 'cons x place)) + (list 'callf2 'cons x place))) + +(defmacro pushnew (x place &rest keys) + "(pushnew X PLACE): insert X at the head of the list if not already there. +Like (push X PLACE), except that the list is unmodified if X is `eql' to +an element already on the list. +Keywords supported: :test :test-not :key" + (if (symbolp place) (list 'setq place (list* 'adjoin x place keys)) + (list* 'callf2 'adjoin x place keys))) + +(defun cl-set-elt (seq n val) + (if (listp seq) (setcar (nthcdr n seq) val) (aset seq n val))) + +(defun cl-set-nthcdr (n list x) + (if (<= n 0) x (setcdr (nthcdr (1- n) list) x) list)) + +(defun cl-set-buffer-substring (start end val) + (save-excursion (delete-region start end) + (goto-char start) + (insert val) + val)) + +(defun cl-set-substring (str start end val) + (if end (if (< end 0) (incf end (length str))) + (setq end (length str))) + (if (< start 0) (incf start str)) + (concat (and (> start 0) (substring str 0 start)) + val + (and (< end (length str)) (substring str end)))) + + +;;; Control structures. + +;;; These macros are so simple and so often-used that it's better to have +;;; them all the time than to load them from cl-macs.el. + +(defmacro when (cond &rest body) + "(when COND BODY...): if COND yields non-nil, do BODY, else return nil." + (list 'if cond (cons 'progn body))) + +(defmacro unless (cond &rest body) + "(unless COND BODY...): if COND yields nil, do BODY, else return nil." + (cons 'if (cons cond (cons nil body)))) + +(defun cl-map-extents (&rest cl-args) + (if (fboundp 'next-overlay-at) (apply 'cl-map-overlays cl-args) + (if (fboundp 'map-extents) (apply 'map-extents cl-args)))) + + +;;; Blocks and exits. + +(defalias 'cl-block-wrapper 'identity) +(defalias 'cl-block-throw 'throw) + + +;;; Multiple values. True multiple values are not supported, or even +;;; simulated. Instead, multiple-value-bind and friends simply expect +;;; the target form to return the values as a list. + +(defalias 'values 'list) +(defalias 'values-list 'identity) +(defalias 'multiple-value-list 'identity) +(defalias 'multiple-value-call 'apply) ; only works for one arg +(defalias 'nth-value 'nth) + + +;;; Macros. + +(defvar cl-macro-environment nil) +(defvar cl-old-macroexpand (prog1 (symbol-function 'macroexpand) + (defalias 'macroexpand 'cl-macroexpand))) + +(defun cl-macroexpand (cl-macro &optional cl-env) + (let ((cl-macro-environment cl-env)) + (while (progn (setq cl-macro (funcall cl-old-macroexpand cl-macro cl-env)) + (and (symbolp cl-macro) + (cdr (assq (symbol-name cl-macro) cl-env)))) + (setq cl-macro (cadr (assq (symbol-name cl-macro) cl-env)))) + cl-macro)) + + +;;; Declarations. + +(defvar cl-compiling-file nil) +(defun cl-compiling-file () + (or cl-compiling-file + (and (boundp 'outbuffer) (bufferp (symbol-value 'outbuffer)) + (equal (buffer-name (symbol-value 'outbuffer)) + " *Compiler Output*")))) + +(defvar cl-proclaims-deferred nil) + +(defun proclaim (spec) + (if (fboundp 'cl-do-proclaim) (cl-do-proclaim spec t) + (push spec cl-proclaims-deferred)) + nil) + +(defmacro declaim (&rest specs) + (let ((body (mapcar (function (lambda (x) (list 'proclaim (list 'quote x)))) + specs))) + (if (cl-compiling-file) (list* 'eval-when '(compile load eval) body) + (cons 'progn body)))) ; avoid loading cl-macs.el for eval-when + + +;;; Symbols. + +(defun cl-random-time () + (let* ((time (copy-sequence (current-time-string))) (i (length time)) (v 0)) + (while (>= (decf i) 0) (setq v (+ (* v 3) (aref time i)))) + v)) + +(defvar *gensym-counter* (* (logand (cl-random-time) 1023) 100)) + + +;;; Numbers. + +(defun floatp-safe (x) + "T if OBJECT is a floating point number. +On Emacs versions that lack floating-point support, this function +always returns nil." + (and (numberp x) (not (integerp x)))) + +(defun plusp (x) + "T if NUMBER is positive." + (> x 0)) + +(defun minusp (x) + "T if NUMBER is negative." + (< x 0)) + +(defun oddp (x) + "T if INTEGER is odd." + (eq (logand x 1) 1)) + +(defun evenp (x) + "T if INTEGER is even." + (eq (logand x 1) 0)) + +(defun cl-abs (x) + "Return the absolute value of ARG." + (if (>= x 0) x (- x))) +(or (fboundp 'abs) (defalias 'abs 'cl-abs)) ; This is built-in to Emacs 19 + +(defvar *random-state* (vector 'cl-random-state-tag -1 30 (cl-random-time))) + +;;; We use `eval' in case VALBITS differs from compile-time to load-time. +(defconst most-positive-fixnum (eval '(lsh -1 -1))) +(defconst most-negative-fixnum (eval '(- -1 (lsh -1 -1)))) + +;;; The following are actually set by cl-float-limits. +(defconst most-positive-float nil) +(defconst most-negative-float nil) +(defconst least-positive-float nil) +(defconst least-negative-float nil) +(defconst least-positive-normalized-float nil) +(defconst least-negative-normalized-float nil) +(defconst float-epsilon nil) +(defconst float-negative-epsilon nil) + + +;;; Sequence functions. + +(defalias 'copy-seq 'copy-sequence) + +(defun mapcar* (cl-func cl-x &rest cl-rest) + "Apply FUNCTION to each element of SEQ, and make a list of the results. +If there are several SEQs, FUNCTION is called with that many arguments, +and mapping stops as soon as the shortest list runs out. With just one +SEQ, this is like `mapcar'. With several, it is like the Common Lisp +`mapcar' function extended to arbitrary sequence types." + (if cl-rest + (if (or (cdr cl-rest) (nlistp cl-x) (nlistp (car cl-rest))) + (cl-mapcar-many cl-func (cons cl-x cl-rest)) + (let ((cl-res nil) (cl-y (car cl-rest))) + (while (and cl-x cl-y) + (push (funcall cl-func (pop cl-x) (pop cl-y)) cl-res)) + (nreverse cl-res))) + (mapcar cl-func cl-x))) + + +;;; List functions. + +(defalias 'first 'car) +(defalias 'rest 'cdr) +(defalias 'endp 'null) + +(defun second (x) + "Return the second element of the list LIST." + (car (cdr x))) + +(defun third (x) + "Return the third element of the list LIST." + (car (cdr (cdr x)))) + +(defun fourth (x) + "Return the fourth element of the list LIST." + (nth 3 x)) + +(defun fifth (x) + "Return the fifth element of the list LIST." + (nth 4 x)) + +(defun sixth (x) + "Return the sixth element of the list LIST." + (nth 5 x)) + +(defun seventh (x) + "Return the seventh element of the list LIST." + (nth 6 x)) + +(defun eighth (x) + "Return the eighth element of the list LIST." + (nth 7 x)) + +(defun ninth (x) + "Return the ninth element of the list LIST." + (nth 8 x)) + +(defun tenth (x) + "Return the tenth element of the list LIST." + (nth 9 x)) + +(defun caar (x) + "Return the `car' of the `car' of X." + (car (car x))) + +(defun cadr (x) + "Return the `car' of the `cdr' of X." + (car (cdr x))) + +(defun cdar (x) + "Return the `cdr' of the `car' of X." + (cdr (car x))) + +(defun cddr (x) + "Return the `cdr' of the `cdr' of X." + (cdr (cdr x))) + +(defun caaar (x) + "Return the `car' of the `car' of the `car' of X." + (car (car (car x)))) + +(defun caadr (x) + "Return the `car' of the `car' of the `cdr' of X." + (car (car (cdr x)))) + +(defun cadar (x) + "Return the `car' of the `cdr' of the `car' of X." + (car (cdr (car x)))) + +(defun caddr (x) + "Return the `car' of the `cdr' of the `cdr' of X." + (car (cdr (cdr x)))) + +(defun cdaar (x) + "Return the `cdr' of the `car' of the `car' of X." + (cdr (car (car x)))) + +(defun cdadr (x) + "Return the `cdr' of the `car' of the `cdr' of X." + (cdr (car (cdr x)))) + +(defun cddar (x) + "Return the `cdr' of the `cdr' of the `car' of X." + (cdr (cdr (car x)))) + +(defun cdddr (x) + "Return the `cdr' of the `cdr' of the `cdr' of X." + (cdr (cdr (cdr x)))) + +(defun caaaar (x) + "Return the `car' of the `car' of the `car' of the `car' of X." + (car (car (car (car x))))) + +(defun caaadr (x) + "Return the `car' of the `car' of the `car' of the `cdr' of X." + (car (car (car (cdr x))))) + +(defun caadar (x) + "Return the `car' of the `car' of the `cdr' of the `car' of X." + (car (car (cdr (car x))))) + +(defun caaddr (x) + "Return the `car' of the `car' of the `cdr' of the `cdr' of X." + (car (car (cdr (cdr x))))) + +(defun cadaar (x) + "Return the `car' of the `cdr' of the `car' of the `car' of X." + (car (cdr (car (car x))))) + +(defun cadadr (x) + "Return the `car' of the `cdr' of the `car' of the `cdr' of X." + (car (cdr (car (cdr x))))) + +(defun caddar (x) + "Return the `car' of the `cdr' of the `cdr' of the `car' of X." + (car (cdr (cdr (car x))))) + +(defun cadddr (x) + "Return the `car' of the `cdr' of the `cdr' of the `cdr' of X." + (car (cdr (cdr (cdr x))))) + +(defun cdaaar (x) + "Return the `cdr' of the `car' of the `car' of the `car' of X." + (cdr (car (car (car x))))) + +(defun cdaadr (x) + "Return the `cdr' of the `car' of the `car' of the `cdr' of X." + (cdr (car (car (cdr x))))) + +(defun cdadar (x) + "Return the `cdr' of the `car' of the `cdr' of the `car' of X." + (cdr (car (cdr (car x))))) + +(defun cdaddr (x) + "Return the `cdr' of the `car' of the `cdr' of the `cdr' of X." + (cdr (car (cdr (cdr x))))) + +(defun cddaar (x) + "Return the `cdr' of the `cdr' of the `car' of the `car' of X." + (cdr (cdr (car (car x))))) + +(defun cddadr (x) + "Return the `cdr' of the `cdr' of the `car' of the `cdr' of X." + (cdr (cdr (car (cdr x))))) + +(defun cdddar (x) + "Return the `cdr' of the `cdr' of the `cdr' of the `car' of X." + (cdr (cdr (cdr (car x))))) + +(defun cddddr (x) + "Return the `cdr' of the `cdr' of the `cdr' of the `cdr' of X." + (cdr (cdr (cdr (cdr x))))) + +(defun last (x &optional n) + "Returns the last link in the list LIST. +With optional argument N, returns Nth-to-last link (default 1)." + (if n + (let ((m 0) (p x)) + (while (consp p) (incf m) (pop p)) + (if (<= n 0) p + (if (< n m) (nthcdr (- m n) x) x))) + (while (consp (cdr x)) (pop x)) + x)) + +(defun butlast (x &optional n) + "Returns a copy of LIST with the last N elements removed." + (if (and n (<= n 0)) x + (nbutlast (copy-sequence x) n))) + +(defun nbutlast (x &optional n) + "Modifies LIST to remove the last N elements." + (let ((m (length x))) + (or n (setq n 1)) + (and (< n m) + (progn + (if (> n 0) (setcdr (nthcdr (- (1- m) n) x) nil)) + x)))) + +(defun list* (arg &rest rest) ; See compiler macro in cl-macs.el + "Return a new list with specified args as elements, cons'd to last arg. +Thus, `(list* A B C D)' is equivalent to `(nconc (list A B C) D)', or to +`(cons A (cons B (cons C D)))'." + (cond ((not rest) arg) + ((not (cdr rest)) (cons arg (car rest))) + (t (let* ((n (length rest)) + (copy (copy-sequence rest)) + (last (nthcdr (- n 2) copy))) + (setcdr last (car (cdr last))) + (cons arg copy))))) + +(defun ldiff (list sublist) + "Return a copy of LIST with the tail SUBLIST removed." + (let ((res nil)) + (while (and (consp list) (not (eq list sublist))) + (push (pop list) res)) + (nreverse res))) + +(defun copy-list (list) + "Return a copy of a list, which may be a dotted list. +The elements of the list are not copied, just the list structure itself." + (if (consp list) + (let ((res nil)) + (while (consp list) (push (pop list) res)) + (prog1 (nreverse res) (setcdr res list))) + (car list))) + +(defun cl-maclisp-member (item list) + (while (and list (not (equal item (car list)))) (setq list (cdr list))) + list) + +;;; Define an Emacs 19-compatible `member' for the benefit of Emacs 18 users. +(or (and (fboundp 'member) (subrp (symbol-function 'member))) + (defalias 'member 'cl-maclisp-member)) + +(defalias 'cl-member 'memq) ; for compatibility with old CL package +(defalias 'cl-floor 'floor*) +(defalias 'cl-ceiling 'ceiling*) +(defalias 'cl-truncate 'truncate*) +(defalias 'cl-round 'round*) +(defalias 'cl-mod 'mod*) + +(defun adjoin (cl-item cl-list &rest cl-keys) ; See compiler macro in cl-macs + "Return ITEM consed onto the front of LIST only if it's not already there. +Otherwise, return LIST unmodified. +Keywords supported: :test :test-not :key" + (cond ((or (equal cl-keys '(:test eq)) + (and (null cl-keys) (not (numberp cl-item)))) + (if (memq cl-item cl-list) cl-list (cons cl-item cl-list))) + ((or (equal cl-keys '(:test equal)) (null cl-keys)) + (if (member cl-item cl-list) cl-list (cons cl-item cl-list))) + (t (apply 'cl-adjoin cl-item cl-list cl-keys)))) + +(defun subst (cl-new cl-old cl-tree &rest cl-keys) + "Substitute NEW for OLD everywhere in TREE (non-destructively). +Return a copy of TREE with all elements `eql' to OLD replaced by NEW. +Keywords supported: :test :test-not :key" + (if (or cl-keys (and (numberp cl-old) (not (integerp cl-old)))) + (apply 'sublis (list (cons cl-old cl-new)) cl-tree cl-keys) + (cl-do-subst cl-new cl-old cl-tree))) + +(defun cl-do-subst (cl-new cl-old cl-tree) + (cond ((eq cl-tree cl-old) cl-new) + ((consp cl-tree) + (let ((a (cl-do-subst cl-new cl-old (car cl-tree))) + (d (cl-do-subst cl-new cl-old (cdr cl-tree)))) + (if (and (eq a (car cl-tree)) (eq d (cdr cl-tree))) + cl-tree (cons a d)))) + (t cl-tree))) + +(defun acons (a b c) (cons (cons a b) c)) +(defun pairlis (a b &optional c) (nconc (mapcar* 'cons a b) c)) + + +;;; Miscellaneous. + +(put 'cl-assertion-failed 'error-conditions '(error)) +(put 'cl-assertion-failed 'error-message "Assertion failed") + +;;; This is defined in Emacs 19; define it here for Emacs 18 users. +(defun cl-add-hook (hook func &optional append) + "Add to hook variable HOOK the function FUNC. +FUNC is not added if it already appears on the list stored in HOOK." + (let ((old (and (boundp hook) (symbol-value hook)))) + (and (listp old) (not (eq (car old) 'lambda)) + (setq old (list old))) + (and (not (member func old)) + (set hook (if append (nconc old (list func)) (cons func old)))))) +(or (fboundp 'add-hook) (defalias 'add-hook 'cl-add-hook)) + + +;;; Autoload the other portions of the package. +(mapcar (function + (lambda (set) + (mapcar (function + (lambda (func) + (autoload func (car set) nil nil (nth 1 set)))) + (cddr set)))) + '(("cl-extra" nil + coerce equalp cl-map-keymap maplist mapc mapl mapcan mapcon + cl-map-keymap cl-map-keymap-recursively cl-map-intervals + cl-map-overlays cl-set-frame-visible-p cl-float-limits + gcd lcm isqrt expt floor* ceiling* truncate* round* + mod* rem* signum random* make-random-state random-state-p + subseq concatenate cl-mapcar-many map some every notany + notevery revappend nreconc list-length tailp copy-tree get* getf + cl-set-getf cl-do-remf remprop make-hash-table cl-hash-lookup + gethash cl-puthash remhash clrhash maphash hash-table-p + hash-table-count cl-progv-before cl-prettyexpand + cl-macroexpand-all) + ("cl-seq" nil + reduce fill replace remq remove remove* remove-if remove-if-not + delete delete* delete-if delete-if-not remove-duplicates + delete-duplicates substitute substitute-if substitute-if-not + nsubstitute nsubstitute-if nsubstitute-if-not find find-if + find-if-not position position-if position-if-not count count-if + count-if-not mismatch search sort* stable-sort merge member* + member-if member-if-not cl-adjoin assoc* assoc-if assoc-if-not + rassoc* rassoc rassoc-if rassoc-if-not union nunion intersection + nintersection set-difference nset-difference set-exclusive-or + nset-exclusive-or subsetp subst-if subst-if-not nsubst nsubst-if + nsubst-if-not sublis nsublis tree-equal) + ("cl-macs" nil + gensym gentemp typep cl-do-pop get-setf-method + cl-struct-setf-expander compiler-macroexpand cl-compile-time-init) + ("cl-macs" t + defun* defmacro* function* destructuring-bind eval-when + eval-when-compile load-time-value case ecase typecase etypecase + block return return-from loop do do* dolist dotimes do-symbols + do-all-symbols psetq progv flet labels macrolet symbol-macrolet + lexical-let lexical-let* multiple-value-bind multiple-value-setq + locally the declare define-setf-method defsetf define-modify-macro + setf psetf remf shiftf rotatef letf letf* callf callf2 defstruct + check-type assert ignore-errors define-compiler-macro))) + +;;; Define data for indentation and edebug. +(mapcar (function + (lambda (entry) + (mapcar (function + (lambda (func) + (put func 'lisp-indent-function (nth 1 entry)) + (put func 'lisp-indent-hook (nth 1 entry)) + (or (get func 'edebug-form-spec) + (put func 'edebug-form-spec (nth 2 entry))))) + (car entry)))) + '(((defun* defmacro*) 2) + ((function*) nil + (&or symbolp ([&optional 'macro] 'lambda (&rest sexp) &rest form))) + ((eval-when) 1 (sexp &rest form)) + ((when unless) 1 (&rest form)) + ((declare) nil (&rest sexp)) + ((the) 1 (sexp &rest form)) + ((case ecase typecase etypecase) 1 (form &rest (sexp &rest form))) + ((block return-from) 1 (sexp &rest form)) + ((return) nil (&optional form)) + ((do do*) 2 ((&rest &or symbolp (symbolp &optional form form)) + (form &rest form) + &rest form)) + ((dolist dotimes) 1 ((symbolp form &rest form) &rest form)) + ((do-symbols) 1 ((symbolp form &optional form form) &rest form)) + ((do-all-symbols) 1 ((symbolp form &optional form) &rest form)) + ((psetq setf psetf) nil edebug-setq-form) + ((progv) 2 (&rest form)) + ((flet labels macrolet) 1 + ((&rest (sexp sexp &rest form)) &rest form)) + ((symbol-macrolet lexical-let lexical-let*) 1 + ((&rest &or symbolp (symbolp form)) &rest form)) + ((multiple-value-bind) 2 ((&rest symbolp) &rest form)) + ((multiple-value-setq) 1 ((&rest symbolp) &rest form)) + ((incf decf remf pop push pushnew shiftf rotatef) nil (&rest form)) + ((letf letf*) 1 ((&rest (&rest form)) &rest form)) + ((callf destructuring-bind) 2 (sexp form &rest form)) + ((callf2) 3 (sexp form form &rest form)) + ((loop) nil (&rest &or symbolp form)) + ((ignore-errors) 0 (&rest form)))) + + +;;; This goes here so that cl-macs can find it if it loads right now. +(provide 'cl-19) ; usage: (require 'cl-19 "cl") + + +;;; Things to do after byte-compiler is loaded. +;;; As a side effect, we cause cl-macs to be loaded when compiling, so +;;; that the compiler-macros defined there will be present. + +(defvar cl-hacked-flag nil) +(defun cl-hack-byte-compiler () + (if (and (not cl-hacked-flag) (fboundp 'byte-compile-file-form)) + (progn + (cl-compile-time-init) ; in cl-macs.el + (setq cl-hacked-flag t)))) + +;;; Try it now in case the compiler has already been loaded. +(cl-hack-byte-compiler) + +;;; Also make a hook in case compiler is loaded after this file. +;;; The compiler doesn't call any hooks when it loads or runs, but +;;; we can take advantage of the fact that emacs-lisp-mode will be +;;; called when the compiler reads in the file to be compiled. +;;; BUG: If the first compilation is `byte-compile' rather than +;;; `byte-compile-file', we lose. Oh, well. +(add-hook 'emacs-lisp-mode-hook 'cl-hack-byte-compiler) + + +;;; The following ensures that packages which expect the old-style cl.el +;;; will be happy with this one. + +(provide 'cl) + +(provide 'mini-cl) ; for Epoch + +(run-hooks 'cl-load-hook) + +;;; cl.el ends here