all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Michael Heerdegen <michael_heerdegen@web.de>
To: help-gnu-emacs@gnu.org
Subject: Re: Help setting nadvice for indent-region
Date: Fri, 12 Feb 2016 20:04:41 +0100	[thread overview]
Message-ID: <874mddzr5i.fsf@web.de> (raw)
In-Reply-To: CAFyQvY1vbheCVKqCYpBZbBXS+E0z-kckMQau5MrinDkCEP7GEA@mail.gmail.com

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.




  reply	other threads:[~2016-02-12 19:04 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-02-05 23:49 Help setting nadvice for indent-region Kaushal Modi
2016-02-05 23:58 ` Kaushal Modi
2016-02-06  0:00   ` Kaushal Modi
2016-02-06  0:30   ` Emanuel Berg
2016-02-06  3:31     ` Kaushal Modi
2016-02-06 10:43 ` Michael Heerdegen
2016-02-07  3:12   ` Kaushal Modi
2016-02-07 17:46     ` Kaushal Modi
2016-02-07 18:51       ` John Mastro
2016-02-08  0:03         ` Emanuel Berg
2016-02-08  4:22           ` Kaushal Modi
2016-02-08 17:05             ` Eli Zaretskii
2016-02-08 17:27               ` Kaushal Modi
2016-02-09  3:07             ` Emanuel Berg
2016-02-08 20:03           ` John Mastro
2016-02-08 23:13             ` Emanuel Berg
2016-02-11 14:02         ` Stefan Monnier
2016-02-11 17:36           ` Kaushal Modi
2016-02-11 18:10             ` Michael Heerdegen
2016-02-11 18:47               ` Kaushal Modi
2016-02-11 18:56                 ` Kaushal Modi
2016-02-11 19:14                   ` Michael Heerdegen
2016-02-11 20:15                     ` Kaushal Modi
2016-02-11 20:38                       ` Kaushal Modi
2016-02-12 14:09                         ` Michael Heerdegen
2016-02-12 14:21                           ` Michael Heerdegen
2016-02-12 16:02                             ` Kaushal Modi
2016-02-12 19:04                               ` Michael Heerdegen [this message]
2016-02-12 13:57                       ` Michael Heerdegen
2016-02-11 19:03                 ` Michael Heerdegen
2016-02-07 23:48       ` Emanuel Berg

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=874mddzr5i.fsf@web.de \
    --to=michael_heerdegen@web.de \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.