[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Help setting nadvice for indent-region
From: |
Kaushal Modi |
Subject: |
Re: Help setting nadvice for indent-region |
Date: |
Fri, 12 Feb 2016 11:02:03 -0500 |
Hi Michael,
>> So with a let-bound variable 'ret', I can have something like
>> (setq ret (apply orig-fn args))
>> and return 'ret' at the end of the :around advice function
definition. Right?
> Sure.
I now use prog1! :)
> If you want to show the name of the adviced function instead of the
> current command: you know it when you install the advice (in your
> dolist) - use it when defining the advice. Then you can't use the same
> constantpiece of advice for all functions, of course.
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.
> But if it's only for debugging, you don't need this anymore, I think.
I'd love to have that message always shown (not just for debug) for a piece
of mind and a good feedback that something got applied on the whole buffer
without me selecting the whole buffer. eval-region, especially provides no
feedback. So it is good to see that message when eval-region happens on the
whole buffer.
> If you need to debug, I recommend to use trace.el instead:
M-x trace-function your-adviced-function-here
Thanks. I will be using that for future debugs.
> The nil return value is insignificant. Stefan used it only to avoid
an empty function body in his advice.
Thanks for that info.
> Oh, and `first', `second' are defined in the old cl.el. It's
> recommended to use `cl-lib' now, and thus `cl-first', `cl-second'.
> Or simply `car' and `cadr'.
Noted, thanks! :)
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))
=====
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 :)
- Re: Help setting nadvice for indent-region, (continued)
- Re: Help setting nadvice for indent-region, Stefan Monnier, 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/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 <=
- 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, Michael Heerdegen, 2016/02/11
- Re: Help setting nadvice for indent-region, Emanuel Berg, 2016/02/07