Re: Advicing the `interactive' form of a command

Stefan Monnier via Users list for the GNU Emacs text editor <[email protected]>
Newsgroups gmane.emacs.help
Message-ID <[email protected]>
> How can one advice the `interactive' form of a command with advice-add?
> The elisp manual describes the different values for the WHERE argument
> of `add-function' and `advice-add' and how they are equivalent to
> lambda forms.  For example, :around advice is equivalent to

The ELisp doc has the following to say about it (in "Core Advising
Primitives"):

     If FUNCTION is not interactive, then the combined function will
     inherit the interactive spec, if any, of the original function.
     Else, the combined function will be interactive and will use the
     interactive spec of FUNCTION.  One exception: if the interactive
     spec of FUNCTION is a function (i.e., a ‘lambda’ expression or an
     ‘fbound’ symbol rather than an expression or a string), then the
     interactive spec of the combined function will be a call to that
     function with the interactive spec of the original function as sole
     argument.  To interpret the spec received as argument, use
     ‘advice-eval-interactive-spec’.

     Note: The interactive spec of FUNCTION will apply to the combined
     function and should hence obey the calling convention of the
     combined function rather than that of FUNCTION.  In many cases, it
     makes no difference since they are identical, but it does matter
     for ‘:around’, ‘:filter-args’, and ‘:filter-return’, where FUNCTION
     receives different arguments than the original function stored in
     PLACE.

>   (advice-add
>    'switch-to-buffer :around
>    (lambda (fun &rest args)
>      (let ((confirm-nonexistent-file-or-buffer t))
>        (apply fun args))))

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))


=== Stefan
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.