Re: Advicing the `interactive' form of a command

Roland Winkler <[email protected]>
Newsgroups gmane.emacs.help
Message-ID <[email protected]>
On Mon, May 04 2026, Stefan Monnier wrote:
> So if you want to apply the let binding to the interactive spec, you
> can do:
>
>     (advice-add 'switch-to-buffer :before
>       (lambda (&rest _)
>         (interactive
>           (lambda (ispec)
>             (let ((confirm-nonexistent-file-or-buffer t))
>               (advice-eval-interactive-spec ispec))))
>         nil))

Sometimes I wish manuals like the elisp manual would talk more not only
about _what_ can be accomplished with certain things, but they looked at
things from the perspective of problem solving (solving _my_ problems
that make me look into the manual).

In the above example, giving `interactive' an argument that is a
function looks odd if you simply wonder "what's that??".  The doc for
interactive won't help you with that.  But if my goal for looking into
these things is to modify the interactive spec of a function, this weird
construct suddenly makes a lot of sense.  The advice machinery needs to
dissect a function to decorate it with advice in various ways.  The
FUNCTION of :around advice can access and execute the body of the
original function via its first arg.  In the construct

      (advice-add 'foo :before
        (interactive
          (lambda (ispec)
            ...
            (advice-eval-interactive-spec ispec)
            ...)))

the interactive form is probably never evaluated as such, but it may
likewise get dissected, and maybe the whole thing could be replaced by
something like

      (advice-add 'foo :interactive
        (lambda (ispec)
          ...
          (advice-eval-interactive-spec ispec)
          ...)))

So (advice-eval-interactive-spec ispec) is essentially an (eval ispec)
[or (apply ispec), in the spirit of what :around does to the body of a
function].  But advice-eval-interactive-spec is maybe less important to
advertise this entire feature than (interactive (lambda (ispec) ...)) or
:interactive.  (While trying to solve my problem, at some point I was
looking for something like :interactive because this appeared to me like
a natural strategy to address my problem.)

Of course, you know so much more about these things (say, what really
happens under the hood of add-function) that you may be amused by my
naive views!
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.