help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Help setting nadvice for indent-region


From: Michael Heerdegen
Subject: Re: Help setting nadvice for indent-region
Date: Thu, 11 Feb 2016 19:10:21 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/25.0.90 (gnu/linux)

Kaushal Modi <kaushal.modi@gmail.com> writes:

> I have just one concern.. that this will not work for advising
> functions like eval-defun.

What exactly did you try to do? - `eval-defun' has only one argument
that is not related to the region, so the advises that were discussed
yet are not applicable here.  In what way do you want to change the
behavior of `eval-defun'?

But in general, what Stefan pointed out was important: changing the
semantics of the functions is not a good idea.

> (defun modi/advice-region-or-whole (orig-fn &rest args)
>   (interactive) ; Required to override the "r" argument of `interactive'
>                                         ; in functions like `indent-region'
>                                         ; so that that function can be
> called
>                                         ; without an active region.
>   (let (msg)
>     ;; Execute the original SYMBOL function if it is called indirectly.
>     ;; Example: We do not want to modify the ARGS if `eval-region'
>     ;;          is called via `eval-defun', because in that case, the
>     ;;          ARGS are set by the wrapper function `eval-defun'.
>     (when (null args)
>       (if (use-region-p) ; when region is selected
>           (setq args (list (region-beginning) (region-end)))
>         (progn
>           (setq args (list (point-min) (point-max)))
>           (setq msg (format "Executed %s on the whole buffer."
>                             (propertize (symbol-name this-command)
>                                         'face
>                                         'font-lock-function-name-face))))))
>     (apply orig-fn args)
>     (when msg
>       (message msg))))

Be careful: this changes the return value of the advised function to the
value returned by `when' -- this is not `defadvice'!

And BTW, (just a hint) you also don't need to `setq' the ARGS variable
(of course you can), just do

  (apply orig-fun (calculate-new-args-somehow-here-using-the args))

In advice.el, you had some weird mechanism to modify function arguments,
but nadvice is different, as it works by simply composing functions as
described in the doc of `add-function'.


Regards,

Michael.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]