[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Last use of defadvice in Emacs
From: |
Alan Mackenzie |
Subject: |
Re: Last use of defadvice in Emacs |
Date: |
Fri, 8 Apr 2022 17:10:54 +0000 |
Hello, Stefan.
On Thu, Apr 07, 2022 at 14:37:14 -0400, Stefan Monnier wrote:
> > I want more than to "perform the test" at compile time. I want a Lisp
> > form that will check whether that variable is bound, and if so, not even
> > compile the sub-form. Something like C's #ifndef preprocessor form. It
> > would look something like
> > (hash-if (not
> > (boundp 'font-lock-extend-after-change-region-function))
> > (progn
> > (defmacro c-advise-fl-for-region (function) .....)
> > (c-advise-fl-for-region ....)
> > ....
> > ))
> > .. Here the progn form would be neither evaluated nor compiled if that
> > font-lock-... variable were boundp. We don't have this at the moment.
> > Not that it's all that important in the current case, but it might be
> > handy to have, perhaps, in other version dependent code.
> The patch I send does obey the requirement that the `defadvice` will be
> "neither evaluated nor compiled" if the variable exists at compile-time.
That's different from, and less demanding than, the "challenge" I set, I
think. It's not at all important to Emacs's health, but .....
> It is not a separate "hash-if", OTOH. We can define such a "hash-if",
> as seen for example in the `url-http-ntlm` GNU ELPA package:
> (defmacro url-http-ntlm--if-when-compile (cond &rest body)
> (declare (debug t) (indent 1))
> (when (eval cond)
> `(progn ,@body)))
> ;; Remove authorization after redirect.
> (url-http-ntlm--if-when-compile
> (and (boundp 'emacs-major-version)
> (< emacs-major-version 25))
> ...
> ... Various code, including, incidentally, a `defadvice` ...
> ...)
Here, a piece of `byte-code' gets compiled for the
url-http-ntlm--if-when-compile call. This is useless code without any
useful function. (I've just tried it.)
The action I asked for (or, at least, meant to ask for) was for _no_ code
to get compiled when a condition was not met. I still believe this is
not possible at the moment. But the C preprocessor can do it with
#ifndef.
One use for this could be writing unit tests in the same file.el as what
they're testing. They would get compiled only for a test run.
> Along similar lines, there's AUCTeX's `TeX--if-macro-fboundp`:
> (defmacro TeX--if-macro-fboundp (name then &rest else)
> "Execute THEN if macro NAME is bound and ELSE otherwise.
> Essentially,
> (TeX--if-macro-fboundp name then else...)
> is equivalent to
> (if (fboundp 'name) then else...)
> but takes care of byte-compilation issues where the byte-code for
> the latter could signal an error if it has been compiled with
> emacs 24.1 and is then later run by emacs 24.5."
> (declare (indent 2) (debug (symbolp form &rest form)))
> (if (fboundp name) ;If macro exists at compile-time, just
> use it.
> then
> `(if (fboundp ',name) ;Else, check if it exists at
> run-time.
> (eval ',then) ;If it does, then run the then
> code.
> ,@else)))
I haven't been able to find AUCTeX's source code yet, but I'm not sure
this macro would meet the "no code at all" criterion, either.
> No such macro has reached ELisp's core yet, probably because the
> precise requirements tend to be subtly different (often depending on
> the authors's own preferences about what they want to consider as
> legitimate or important use-cases, as is the case in the cc-mode code:
> what should happen when compiled on Emacs-NN but run on Emacs-MM?).
> Stefan
--
Alan Mackenzie (Nuremberg, Germany).
- Re: Last use of defadvice in Emacs, (continued)
- Re: Last use of defadvice in Emacs, T.V Raman, 2022/04/06
- Re: Last use of defadvice in Emacs, Stefan Monnier, 2022/04/06
- Re: Last use of defadvice in Emacs, Eli Zaretskii, 2022/04/07
- Re: Last use of defadvice in Emacs, Stefan Monnier, 2022/04/07
- Re: Last use of defadvice in Emacs, T.V Raman, 2022/04/07
- Re: Last use of defadvice in Emacs, Stefan Monnier, 2022/04/07
- Re: Last use of defadvice in Emacs, T.V Raman, 2022/04/08
- Re: Last use of defadvice in Emacs, Eli Zaretskii, 2022/04/08
- Re: Last use of defadvice in Emacs, Alan Mackenzie, 2022/04/07
- Re: Last use of defadvice in Emacs, Stefan Monnier, 2022/04/07
- Re: Last use of defadvice in Emacs,
Alan Mackenzie <=
- Re: Last use of defadvice in Emacs, Stefan Monnier, 2022/04/08
- Re: Last use of defadvice in Emacs, Alan Mackenzie, 2022/04/08