[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: |
Fri, 12 Feb 2016 20:04:41 +0100 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/25.0.90 (gnu/linux) |
Kaushal Modi <kaushal.modi@gmail.com> writes:
> Right, that's why my first approach was to use macro to generate an
> individual advice fn for each fn I was advising. But for my purpose,
> this-command works well.
FWIW, you can achieve this also without using a macro.
> With that, I now use the below as the solution which helps achieve these
> goals:
> (1) Apply indent-region/eval-region over the whole buffer if a region is
> not selected (and even if (mark) returns nil).
> (2) Print an assuring message that the function was applied over the whole
> buffer AFTER applying the orig-fun. This is because `indent-region` prints
> out an "Indenting region .." message, and if I want to display my "Executed
> .. on the whole buffer." message, I need to do it after ORIG-FUN is
> applied. So I cannot do that in the (interactive ..) form.
>
> ==== Current solution =====
>
> (defvar modi/region-or-whole-fns '(indent-region
> eval-region)
> "List of functions to act on the whole buffer if no region is selected.")
>
> (defun modi/advice-region-or-whole (orig-fun &rest args)
> "Advice function that applies ORIG-FUN to the whole buffer if no region is
> selected.
> http://thread.gmane.org/gmane.emacs.help/109025/focus=109102 "
> ;; Required to override the "r" argument of `interactive' in functions
> like
> ;; `indent-region' so that they can be called without an active region.
> (interactive (if (use-region-p)
> (list (region-beginning) (region-end))
> (list (point-min) (point-max))))
> (prog1 ; Return value of the advising fn needs to be the same as ORIG-FUN
> (apply orig-fun args)
> (when (and (called-interactively-p 'interactive)
> (not (use-region-p)))
> (message "Executed %s on the whole buffer."
> (propertize (symbol-name this-command)
> 'face 'font-lock-function-name-face)))))
>
> (dolist (fn modi/region-or-whole-fns)
> (advice-add fn :around #'modi/advice-region-or-whole))
That looks good.
> It's good to see my initial buggy overly complicated macro-based
> solution boil down to this more robust (, apparently bug-free),
> canonical and a relatively simpler one :)
The approach was ok, though I would have tried to avoid using a macro.
But well, it also changed the functions, not the interactive specs...
Michael.
- Re: Help setting nadvice for indent-region, (continued)
- Re: Help setting nadvice for indent-region, Kaushal Modi, 2016/02/11
- Re: Help setting nadvice for indent-region, Michael Heerdegen, 2016/02/11
- Re: Help setting nadvice for indent-region, Kaushal Modi, 2016/02/11
- Re: Help setting nadvice for indent-region, Kaushal Modi, 2016/02/11
- Re: Help setting nadvice for indent-region, Michael Heerdegen, 2016/02/11
- Re: Help setting nadvice for indent-region, Kaushal Modi, 2016/02/11
- Re: Help setting nadvice for indent-region, Kaushal Modi, 2016/02/11
- Re: Help setting nadvice for indent-region, Michael Heerdegen, 2016/02/12
- Re: Help setting nadvice for indent-region, Michael Heerdegen, 2016/02/12
- Re: Help setting nadvice for indent-region, Kaushal Modi, 2016/02/12
- Re: Help setting nadvice for indent-region,
Michael Heerdegen <=
- Re: Help setting nadvice for indent-region, Michael Heerdegen, 2016/02/12
- Re: Help setting nadvice for indent-region, Michael Heerdegen, 2016/02/11
- Re: Help setting nadvice for indent-region, Emanuel Berg, 2016/02/07