cl: Obsolete Macros
D.2 Obsolete Macros
===================
The following macros are obsolete, and are replaced by versions with a
‘cl-’ prefix that do not behave in exactly the same way. Consequently,
the ‘cl.el’ versions are not simply aliases to the ‘cl-lib.el’ versions.
-- Macro: flet (bindings...) forms...
This macro is replaced by ‘cl-flet’ (Function Bindings),
which behaves the same way as Common Lisp’s ‘flet’. This ‘flet’
takes the same arguments as ‘cl-flet’, but does not behave in
precisely the same way.
While ‘flet’ in Common Lisp establishes a lexical function binding,
this ‘flet’ makes a dynamic binding (it dates from a time before
Emacs had lexical binding). The result is that ‘flet’ affects
indirect calls to a function as well as calls directly inside the
‘flet’ form itself.
This will even work on Emacs primitives, although note that some
calls to primitive functions internal to Emacs are made without
going through the symbol’s function cell, and so will not be
affected by ‘flet’. For example,
(flet ((message (&rest args) (push args saved-msgs)))
(do-something))
This code attempts to replace the built-in function ‘message’ with
a function that simply saves the messages in a list rather than
displaying them. The original definition of ‘message’ will be
restored after ‘do-something’ exits. This code will work fine on
messages generated by other Lisp code, but messages generated
directly inside Emacs will not be caught since they make direct
C-language calls to the message routines rather than going through
the Lisp ‘message’ function.
For those cases where the dynamic scoping of ‘flet’ is desired,
‘cl-flet’ is clearly not a substitute. The most direct replacement
would be instead to use ‘cl-letf’ to temporarily rebind
‘(symbol-function 'FUN)’. But in most cases, a better substitute
is to use advice, such as:
(defvar my-fun-advice-enable nil)
(add-advice 'FUN :around
(lambda (orig &rest args)
(if my-fun-advice-enable (do-something)
(apply orig args))))
so that you can then replace the ‘flet’ with a simple dynamically
scoped binding of ‘my-fun-advice-enable’.
Note that many primitives (e.g., ‘+’) have special byte-compile
handling. Attempts to redefine such functions using ‘flet’,
‘cl-letf’, or advice will fail when byte-compiled.
-- Macro: labels (bindings...) forms...
This macro is replaced by ‘cl-labels’ (Function Bindings),
which behaves the same way as Common Lisp’s ‘labels’. This
‘labels’ takes the same arguments as ‘cl-labels’, but does not
behave in precisely the same way.
This version of ‘labels’ uses the obsolete ‘lexical-let’ form
(Obsolete Lexical Binding), rather than the true lexical
binding that ‘cl-labels’ uses.