1 ;;; cl.el --- Compatibility aliases for the old CL library. -*- lexical-binding: t -*-
3 ;; Copyright (C) 2012-2017 Free Software Foundation, Inc.
5 ;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
6 ;; Keywords: extensions
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; This is a compatibility file which provides the old names provided by CL
26 ;; before we cleaned up its namespace usage.
34 ;; (defun cl--rename ()
37 ;; (case-fold-search nil)
38 ;; (files '("cl.el" "cl-macs.el" "cl-seq.el" "cl-extra.el")))
39 ;; (dolist (file files)
40 ;; (with-current-buffer (find-file-noselect file)
41 ;; (goto-char (point-min))
42 ;; (while (re-search-forward
43 ;; "^(\\(def[^ \t\n]*\\) +'?\\(\\(\\sw\\|\\s_\\)+\\)" nil t)
44 ;; (let ((name (match-string-no-properties 2))
45 ;; (type (match-string-no-properties 1)))
46 ;; (unless (string-match-p "\\`cl-" name)
48 ;; ((member type '("defvar" "defconst"))
49 ;; (unless (member name vdefs) (push name vdefs)))
50 ;; ((member type '("defun" "defsubst" "defalias" "defmacro"))
51 ;; (unless (member name fdefs) (push name fdefs)))
52 ;; ((member type '("def-edebug-spec" "defsetf" "define-setf-method"
53 ;; "define-compiler-macro"))
55 ;; (t (error "Unknown type %S" type))))))))
56 ;; (let ((re (concat "\\_<" (regexp-opt (append vdefs fdefs)) "\\_>"))
58 ;; (dolist (file files)
59 ;; (with-current-buffer (find-file-noselect file)
60 ;; (goto-char (point-min))
61 ;; (while (re-search-forward re nil t)
62 ;; (replace-match "cl-\\&"))
64 ;; (with-current-buffer (find-file-noselect "cl-rename.el")
65 ;; (dolist (def vdefs)
66 ;; (insert (format "(defvaralias '%s 'cl-%s)\n" def def)))
67 ;; (dolist (def fdefs)
68 ;; (insert (format "(defalias '%s 'cl-%s)\n" def def)))
71 ;; (defun cl--unrename ()
72 ;; ;; Taken from "Naming Conventions" node of the doc.
73 ;; (let* ((names '(defun* defsubst* defmacro* function* member*
74 ;; assoc* rassoc* get* remove* delete*
75 ;; mapcar* sort* floor* ceiling* truncate*
76 ;; round* mod* rem* random*))
77 ;; (files '("cl.el" "cl-lib.el" "cl-macs.el" "cl-seq.el" "cl-extra.el"))
78 ;; (re (concat "\\_<cl-" (regexp-opt (mapcar #'symbol-name names))
80 ;; (dolist (file files)
81 ;; (with-current-buffer (find-file-noselect file)
82 ;; (goto-char (point-min))
83 ;; (while (re-search-forward re nil t)
84 ;; (delete-region (1- (point)) (point)))
87 (defun cl-unload-function ()
88 "Stop unloading of the Common Lisp extensions."
89 (message "Cannot unload the feature `cl'")
90 ;; Stop standard unloading!
93 ;;; Aliases to cl-lib's features.
104 ;; lambda-list-keywords
105 float-negative-epsilon
107 least-negative-normalized-float
108 least-positive-normalized-float
113 ;; custom-print-functions
115 (defvaralias var
(intern (format "cl-%s" var
))))
119 (random* . cl-random
)
123 (truncate* . cl-truncate
)
124 (ceiling* . cl-ceiling
)
126 (rassoc* . cl-rassoc
)
128 (member* . cl-member
)
129 (delete* . cl-delete
)
130 (remove* . cl-remove
)
131 (defsubst* . cl-defsubst
)
133 (function* . cl-function
)
134 (defmacro* . cl-defmacro
)
136 (mapcar* . cl-mapcar
)
214 define-compiler-macro
228 (define-setf-method . define-setf-expander
)
292 (let ((new (if (consp fun
) (prog1 (cdr fun
) (setq fun
(car fun
)))
293 (intern (format "cl-%s" fun
)))))
296 (defun cl--wrap-in-nil-block (fun &rest args
)
297 `(cl-block nil
,(apply fun args
)))
298 (advice-add 'dolist
:around
#'cl--wrap-in-nil-block
)
299 (advice-add 'dotimes
:around
#'cl--wrap-in-nil-block
)
301 (defun cl--pass-args-to-cl-declare (&rest specs
)
302 (macroexpand `(cl-declare ,@specs
)))
303 (advice-add 'declare
:after
#'cl--pass-args-to-cl-declare
)
305 ;;; Features provided a bit differently in Elisp.
307 ;; First, the old lexical-let is now better served by `lexical-binding', tho
308 ;; it's not 100% compatible.
310 (defvar cl-closure-vars nil
)
311 (defvar cl--function-convert-cache nil
)
313 (defun cl--function-convert (f)
314 "Special macro-expander for special cases of (function F).
315 The two cases that are handled are:
316 - closure-conversion of lambda expressions for `lexical-let'.
317 - renaming of F when it's a function defined via `cl-labels' or `labels'."
319 (declare-function cl--expr-contains-any
"cl-macs" (x y
))
320 (declare-function cl--labels-convert
"cl-macs" (f))
321 (defvar cl--labels-convert-cache
)
323 ;; ¡¡Big Ugly Hack!! We can't use a compiler-macro because those are checked
324 ;; *after* handling `function', but we want to stop macroexpansion from
325 ;; being applied infinitely, so we use a cache to return the exact `form'
326 ;; being expanded even though we don't receive it.
327 ((eq f
(car cl--function-convert-cache
)) (cdr cl--function-convert-cache
))
328 ((eq (car-safe f
) 'lambda
)
329 (let ((body (mapcar (lambda (f)
330 (macroexpand-all f macroexpand-all-environment
))
332 (if (and cl-closure-vars
333 (cl--expr-contains-any body cl-closure-vars
))
334 (let* ((new (mapcar 'cl-gensym cl-closure-vars
))
335 (sub (cl-pairlis cl-closure-vars new
)) (decls nil
))
336 (while (or (stringp (car body
))
337 (eq (car-safe (car body
)) 'interactive
))
338 (push (list 'quote
(pop body
)) decls
))
339 (put (car (last cl-closure-vars
)) 'used t
)
340 `(list 'lambda
'(&rest --cl-rest--
)
341 ,@(cl-sublis sub
(nreverse decls
))
344 #'(lambda ,(append new
(cadr f
))
345 ,@(cl-sublis sub body
)))
346 ,@(nconc (mapcar (lambda (x) `(list 'quote
,x
))
348 '((quote --cl-rest--
))))))
349 (let* ((newf `(lambda ,(cadr f
) ,@body
))
350 (res `(function ,newf
)))
351 (setq cl--function-convert-cache
(cons newf res
))
354 (cl--labels-convert f
))))
356 (defmacro lexical-let
(bindings &rest body
)
357 "Like `let', but lexically scoped.
358 The main visible difference is that lambdas inside BODY will create
359 lexical closures as in Common Lisp.
360 \n(fn BINDINGS BODY)"
361 (declare (indent 1) (debug let
))
362 (let* ((cl-closure-vars cl-closure-vars
)
363 (vars (mapcar (function
365 (or (consp x
) (setq x
(list x
)))
366 (push (make-symbol (format "--cl-%s--" (car x
)))
368 (set (car cl-closure-vars
) [bad-lexical-ref
])
369 (list (car x
) (cadr x
) (car cl-closure-vars
))))
375 `(,(car x
) (symbol-value ,(nth 2 x
))))
378 (cons (cons 'function
#'cl--function-convert
)
379 macroexpand-all-environment
))))
380 (if (not (get (car (last cl-closure-vars
)) 'used
))
381 ;; Turn (let ((foo (cl-gensym)))
382 ;; (set foo <val>) ...(symbol-value foo)...)
383 ;; into (let ((foo <val>)) ...(symbol-value 'foo)...).
384 ;; This is good because it's more efficient but it only works with
385 ;; dynamic scoping, since with lexical scoping we'd need
386 ;; (let ((foo <val>)) ...foo...).
388 ,@(mapcar (lambda (x) `(defvar ,(nth 2 x
))) vars
)
389 (let ,(mapcar (lambda (x) (list (nth 2 x
) (nth 1 x
))) vars
)
390 ,(cl-sublis (mapcar (lambda (x)
395 `(let ,(mapcar (lambda (x)
397 `(make-symbol ,(format "--%s--" (car x
)))))
399 (setf ,@(apply #'append
401 (list `(symbol-value ,(nth 2 x
)) (nth 1 x
)))
405 (defmacro lexical-let
* (bindings &rest body
)
406 "Like `let*', but lexically scoped.
407 The main visible difference is that lambdas inside BODY, and in
408 successive bindings within BINDINGS, will create lexical closures
409 as in Common Lisp. This is similar to the behavior of `let*' in
411 \n(fn BINDINGS BODY)"
412 (declare (indent 1) (debug let
))
413 (if (null bindings
) (cons 'progn body
)
414 (setq bindings
(reverse bindings
))
416 (setq body
(list `(lexical-let (,(pop bindings
)) ,@body
))))
419 ;; This should really have some way to shadow 'byte-compile properties, etc.
420 (defmacro flet
(bindings &rest body
)
421 "Make temporary overriding function definitions.
422 This is an analogue of a dynamically scoped `let' that operates on the function
423 cell of FUNCs rather than their value cell.
424 If you want the Common-Lisp style of `flet', you should use `cl-flet'.
425 The FORMs are evaluated with the specified function definitions in place,
426 then the definitions are undone (the FUNCs go back to their previous
427 definitions, or lack thereof).
429 \(fn ((FUNC ARGLIST BODY...) ...) FORM...)"
430 (declare (indent 1) (debug cl-flet
)
431 (obsolete "use either `cl-flet' or `cl-letf'." "24.3"))
434 (if (or (and (fboundp (car x
))
435 (eq (car-safe (symbol-function (car x
))) 'macro
))
436 (cdr (assq (car x
) macroexpand-all-environment
)))
437 (error "Use `labels', not `flet', to rebind macro names"))
438 (let ((func `(cl-function
440 (cl-block ,(car x
) ,@(cddr x
))))))
441 (when (cl--compiling-file)
442 ;; Bug#411. It would be nice to fix this.
443 (and (get (car x
) 'byte-compile
)
444 (error "Byte-compiling a redefinition of `%s' \
445 will not work - use `labels' instead" (symbol-name (car x
))))
446 ;; FIXME This affects the rest of the file, when it
447 ;; should be restricted to the flet body.
448 (and (boundp 'byte-compile-function-environment
)
449 (push (cons (car x
) (eval func
))
450 byte-compile-function-environment
)))
451 (list `(symbol-function ',(car x
)) func
)))
455 (defmacro labels
(bindings &rest body
)
456 "Make temporary function bindings.
457 Like `cl-labels' except that the lexical scoping is handled via `lexical-let'
458 rather than relying on `lexical-binding'."
459 (declare (indent 1) (debug cl-flet
) (obsolete cl-labels
"24.3"))
460 (let ((vars nil
) (sets nil
) (newenv macroexpand-all-environment
))
461 (dolist (binding bindings
)
462 ;; It's important that (not (eq (symbol-name var1) (symbol-name var2)))
463 ;; because these var's *names* get added to the macro-environment.
464 (let ((var (make-symbol (format "--cl-%s--" (car binding
)))))
466 (push `(cl-function (lambda .
,(cdr binding
))) sets
)
468 (push (cons (car binding
)
469 `(lambda (&rest cl-labels-args
)
470 (cl-list* 'funcall
',var
473 (macroexpand-all `(lexical-let ,vars
(setq ,@sets
) ,@body
) newenv
)))
475 ;; Generalized variables are provided by gv.el, but some details are
476 ;; not 100% compatible: not worth the trouble to add them to cl-lib.el, but we
477 ;; still need to support old users of cl.el.
479 (defun cl--gv-adapt (cl-gv do
)
480 ;; This function is used by all .elc files that use define-setf-expander and
481 ;; were compiled with Emacs>=24.3.
482 (let ((vars (nth 0 cl-gv
))
486 ;; Use cl-sublis as was done in cl-setf-do-modify.
488 (if (macroexp-copyable-p (car vals
))
489 (push (cons (pop vars
) (pop vals
)) substs
)
490 (push (list (pop vars
) (pop vals
)) binds
)))
493 (funcall do
(cl-sublis substs
(nth 4 cl-gv
))
494 ;; We'd like to do something like
495 ;; (lambda ,(nth 2 cl-gv) ,(nth 3 cl-gv)).
497 (macroexp-let2 macroexp-copyable-p v exp
498 (cl-sublis (cons (cons (car (nth 2 cl-gv
)) v
)
502 (defmacro define-setf-expander
(name arglist
&rest body
)
503 "Define a `setf' method.
504 This method shows how to handle `setf's to places of the form
505 \(NAME ARGS...). The argument forms ARGS are bound according to
506 ARGLIST, as if NAME were going to be expanded as a macro, then
507 the BODY forms are executed and must return a list of five elements:
508 a temporary-variables list, a value-forms list, a store-variables list
509 \(of length one), a store-form, and an access- form.
511 See `gv-define-expander', and `gv-define-setter' for better and
512 simpler ways to define setf-methods."
514 (&define name cl-lambda-list cl-declarations-or-string def-body
)))
516 ,@(if (stringp (car body
))
517 (list `(put ',name
'setf-documentation
,(pop body
))))
518 (gv-define-expander ,name
520 (lambda (do ,@arglist
)
521 (cl--gv-adapt (progn ,@body
) do
))))))
523 (defmacro defsetf
(name arg1
&rest args
)
524 "Define a `setf' method.
525 This macro is an easy-to-use substitute for `define-setf-expander'
526 that works well for simple place forms.
528 In the simple `defsetf' form, `setf's of the form (setf (NAME
529 ARGS...) VAL) are transformed to function or macro calls of the
530 form (FUNC ARGS... VAL). For example:
534 You can replace this form with `gv-define-simple-setter'.
536 Alternate form: (defsetf NAME ARGLIST (STORE) BODY...).
538 Here, the above `setf' call is expanded by binding the argument
539 forms ARGS according to ARGLIST, binding the value form VAL to
540 STORE, then executing BODY, which must return a Lisp form that
541 does the necessary `setf' operation. Actually, ARGLIST and STORE
542 may be bound to temporary variables which are introduced
543 automatically to preserve proper execution order of the arguments.
546 (defsetf nth (n x) (v) \\=`(setcar (nthcdr ,n ,x) ,v))
548 You can replace this form with `gv-define-setter'.
550 \(fn NAME [FUNC | ARGLIST (STORE) BODY...])"
553 [&or
[symbolp
&optional stringp
]
554 [cl-lambda-list
(symbolp)]]
555 cl-declarations-or-string def-body
)))
556 (if (and (listp arg1
) (consp args
))
557 ;; Like `gv-define-setter' but with `cl-function'.
558 `(gv-define-expander ,name
559 (lambda (do &rest args
)
560 (gv--defsetter ',name
562 (lambda (,@(car args
) ,@arg1
) ,@(cdr args
)))
564 `(gv-define-simple-setter ,name
,arg1
,(car args
))))
566 ;; FIXME: CL used to provide a setf method for `apply', but I haven't been able
567 ;; to find a case where it worked. The code below tries to handle it as well.
568 ;; (defun cl--setf-apply (form last-witness last)
570 ;; ((not (consp form)) form)
571 ;; ((eq (ignore-errors (car (last form))) last-witness)
572 ;; `(apply #',(car form) ,@(butlast (cdr form)) ,last))
573 ;; ((and (memq (car form) '(let let*))
574 ;; (rassoc (list last-witness) (cadr form)))
575 ;; (let ((rebind (rassoc (list last-witness) (cadr form))))
576 ;; `(,(car form) ,(remq rebind (cadr form))
577 ;; ,@(mapcar (lambda (form) (cl--setf-apply form (car rebind) last))
579 ;; (t (mapcar (lambda (form) (cl--setf-apply form last-witness last)) form))))
580 ;; (gv-define-setter apply (val fun &rest args)
581 ;; (pcase fun (`#',(and (pred symbolp) f) (setq fun f))
582 ;; (_ (error "First arg to apply in setf is not #'SYM: %S" fun)))
583 ;; (let* ((butlast (butlast args))
584 ;; (last (car (last args)))
585 ;; (last-witness (make-symbol "--cl-tailarg--"))
586 ;; (setter (macroexpand `(setf (,fun ,@butlast ,last-witness) ,val)
587 ;; macroexpand-all-environment)))
588 ;; (cl--setf-apply setter last-witness last)))
591 ;; FIXME: CL used to provide get-setf-method, which was used by some
592 ;; setf-expanders, but now that we use gv.el, it is a lot more difficult
593 ;; and in general impossible to provide get-setf-method. Hopefully, it
594 ;; won't be needed. If needed, we'll have to do something nasty along the
596 ;; (defun get-setf-method (place &optional env)
597 ;; (let* ((witness (list 'cl-gsm))
598 ;; (expansion (gv-letplace (getter setter) place
599 ;; `(,witness ,getter ,(funcall setter witness)))))
600 ;; ...find "let prefix" of expansion, extract getter and setter from
601 ;; ...the rest, and build the 5-tuple))
602 (make-obsolete 'get-setf-method
'gv-letplace
"24.3")
604 (declare-function cl--arglist-args
"cl-macs" (args))
606 (defmacro define-modify-macro
(name arglist func
&optional doc
)
607 "Define a `setf'-like modify macro.
608 If NAME is called, it combines its PLACE argument with the other
609 arguments from ARGLIST using FUNC. For example:
611 (define-modify-macro incf (&optional (n 1)) +)
613 You can replace this macro with `gv-letplace'."
615 (&define name cl-lambda-list
;; should exclude &key
616 symbolp
&optional stringp
)))
617 (if (memq '&key arglist
)
618 (error "&key not allowed in define-modify-macro"))
619 (require 'cl-macs
) ;For cl--arglist-args.
620 (let ((place (make-symbol "--cl-place--")))
621 `(cl-defmacro ,name
(,place
,@arglist
)
623 (,(if (memq '&rest arglist
) #'cl-list
* #'list
)
624 #'cl-callf
',func
,place
625 ,@(cl--arglist-args arglist
)))))
627 ;;; Additional compatibility code.
628 ;; For names that were clean but really aren't needed any more.
630 (define-obsolete-function-alias 'cl-macroexpand
'macroexpand
"24.3")
631 (define-obsolete-variable-alias 'cl-macro-environment
632 'macroexpand-all-environment
"24.3")
633 (define-obsolete-function-alias 'cl-macroexpand-all
'macroexpand-all
"24.3")
636 ;; This is just kept for compatibility with code byte-compiled by Emacs-20.
638 ;; No idea if this might still be needed.
639 (defun cl-not-hash-table (x &optional y
&rest _z
)
640 (declare (obsolete nil
"24.3"))
641 (signal 'wrong-type-argument
(list 'cl-hash-table-p
(or y x
))))
643 (defvar cl-builtin-gethash
(symbol-function 'gethash
))
644 (make-obsolete-variable 'cl-builtin-gethash nil
"24.3")
645 (defvar cl-builtin-remhash
(symbol-function 'remhash
))
646 (make-obsolete-variable 'cl-builtin-remhash nil
"24.3")
647 (defvar cl-builtin-clrhash
(symbol-function 'clrhash
))
648 (make-obsolete-variable 'cl-builtin-clrhash nil
"24.3")
649 (defvar cl-builtin-maphash
(symbol-function 'maphash
))
651 (make-obsolete-variable 'cl-builtin-maphash nil
"24.3")
652 (define-obsolete-function-alias 'cl-map-keymap
'map-keymap
"24.3")
653 (define-obsolete-function-alias 'cl-copy-tree
'copy-tree
"24.3")
654 (define-obsolete-function-alias 'cl-gethash
'gethash
"24.3")
655 (define-obsolete-function-alias 'cl-puthash
'puthash
"24.3")
656 (define-obsolete-function-alias 'cl-remhash
'remhash
"24.3")
657 (define-obsolete-function-alias 'cl-clrhash
'clrhash
"24.3")
658 (define-obsolete-function-alias 'cl-maphash
'maphash
"24.3")
659 (define-obsolete-function-alias 'cl-make-hash-table
'make-hash-table
"24.3")
660 (define-obsolete-function-alias 'cl-hash-table-p
'hash-table-p
"24.3")
661 (define-obsolete-function-alias 'cl-hash-table-count
'hash-table-count
"24.3")
663 (define-obsolete-function-alias 'cl-map-keymap-recursively
664 'cl--map-keymap-recursively
"24.3")
665 (define-obsolete-function-alias 'cl-map-intervals
'cl--map-intervals
"24.3")
666 (define-obsolete-function-alias 'cl-map-extents
'cl--map-overlays
"24.3")
667 (define-obsolete-function-alias 'cl-set-getf
'cl--set-getf
"24.3")
669 (defun cl-maclisp-member (item list
)
670 (declare (obsolete member
"24.3"))
671 (while (and list
(not (equal item
(car list
)))) (setq list
(cdr list
)))
674 ;; Used in the expansion of the old `defstruct'.
675 (defun cl-struct-setf-expander (x name accessor pred-form pos
)
676 (declare (obsolete nil
"24.3"))
677 (let* ((temp (make-symbol "--cl-x--")) (store (make-symbol "--cl-store--")))
678 (list (list temp
) (list x
) (list store
)
681 (list `(or ,(cl-subst temp
'cl-x pred-form
)
683 "%s storing a non-%s"
685 ,(if (eq (car (get name
'cl-struct-type
)) 'vector
)
686 `(aset ,temp
,pos
,store
)
690 (while (>= (setq pos
(1- pos
)) 0)
691 (setq xx
`(cdr ,xx
)))
693 `(nthcdr ,pos
,temp
))
695 (list accessor temp
))))
699 (run-hooks 'cl-load-hook
)