From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Kaushal Modi Newsgroups: gmane.emacs.help Subject: Re: Help setting nadvice for indent-region Date: Thu, 11 Feb 2016 17:36:40 +0000 Message-ID: References: <87a8ne2k6v.fsf@web.de> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: ger.gmane.org 1455212226 7773 80.91.229.3 (11 Feb 2016 17:37:06 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Thu, 11 Feb 2016 17:37:06 +0000 (UTC) To: Stefan Monnier , help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Thu Feb 11 18:37:05 2016 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1aTvAv-0008S1-4N for geh-help-gnu-emacs@m.gmane.org; Thu, 11 Feb 2016 18:37:05 +0100 Original-Received: from localhost ([::1]:52159 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aTvAu-0007Bk-FT for geh-help-gnu-emacs@m.gmane.org; Thu, 11 Feb 2016 12:37:04 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:39883) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aTvAi-0007Be-MA for help-gnu-emacs@gnu.org; Thu, 11 Feb 2016 12:36:53 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aTvAh-0001pz-9R for help-gnu-emacs@gnu.org; Thu, 11 Feb 2016 12:36:52 -0500 Original-Received: from mail-ob0-x22a.google.com ([2607:f8b0:4003:c01::22a]:33301) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aTvAh-0001pf-1s for help-gnu-emacs@gnu.org; Thu, 11 Feb 2016 12:36:51 -0500 Original-Received: by mail-ob0-x22a.google.com with SMTP id is5so83833911obc.0 for ; Thu, 11 Feb 2016 09:36:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :content-type; bh=D0/Z49wEN8xEYSddm+7q+yeAgxq+u085WISIXmjwFcs=; b=mrroFHyj6ZnbIoR85ziMf4rlR20dONEwOweS3q+jy9b/ueJaCxYaem+HjraMXqnb0b 3faTqwZnqyUIK0RrQTCtHYQKcF1KzFXdd+jEL3Qfmymu50dler7WzRBlf5H7HglQJneE +3AhPIY20OkdMNbjBmHooI+GKFlY0Ku/c6BOtRVZoz8cofsC2r1RGL9bkai5S8EosDd3 xXYPyNvJpB2xxPFExhfVHGlV0Q0VIWV3duz8Dr5k9KB431Fgm81GNAzNpXpEuj71n6OR 51GK8yH554yckTNlikaFiRG7cpPzTrjGp86c07j/YkntDDyO3kDYsR1rkC8CZ6jJbzZI RP6Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:content-type; bh=D0/Z49wEN8xEYSddm+7q+yeAgxq+u085WISIXmjwFcs=; b=WNgy0ZcfNgS04YYJ/M0YvY9zXzvCiaoAvMR/KBqBtbd+bDxKYFo6YCYkjsLARaCad8 vP7EJJYGUOHQK64c4PwgWx/RvJy4TRjkymVaT1BOSJh3w4gDT8bNZjfXU4X/NN0avz6Z VOZ/IMTnE+z5TtlvRLvcNYlqQPSFtJBkq0IpSXepEjBAtCdDQXVu5dnSHR/He29RE0Pz OKQTyDNXaDTKz8zhf/6vgMd8L1BqU2IRTjnzoj8vKPD/FPYXg6sJn2qj7OJoAo+Crt5e dlD57BGrlT9iP93Fk2LSNCKktqgqRdIHg9dU7GV/lxXixxjSbkqXfxFRv8XTr8RKGaK9 2pvw== X-Gm-Message-State: AG10YOShxQPfCsnmUGkzKzk6+kQQrVF+qOt36dRcWb6Za7FyXyjJDxfr+u9ytUkClvhW34O8H+WEYn6n0gt+OQ== X-Received: by 10.60.116.169 with SMTP id jx9mr46149481oeb.30.1455212210249; Thu, 11 Feb 2016 09:36:50 -0800 (PST) In-Reply-To: X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2607:f8b0:4003:c01::22a X-Content-Filtered-By: Mailman/MimeDel 2.1.14 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:109106 Archived-At: Thanks for the reply Stefan. I have just one concern.. that this will not work for advising functions like eval-defun. I tried it out and when I do C-M-x while a point is in a function (without any region selected), it evaluates the whole buffer instead of just the defun. So I have to use the :around advice so that I can access the args and then do a (null args) check to control when to modify the args. Does that look like a fair way to implement this? Or is it possible to somehow check the value of provided args inside the (interactive ..) form? I now have something that's a result of a mix of suggestions given by everyone over here :) - avoided using macro as it's not quite needed - Using this-command to print the current command name as that works for this case (where I intend to advice the functions when when called directly as commands). (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)))) (dolist (fn modi/region-or-whole-fns) (advice-add fn :around #'modi/advice-region-or-whole)) On Thu, Feb 11, 2016 at 9:05 AM Stefan Monnier wrote: > > (defun modi/region-or-whole-advice (orig &rest _) > > (interactive) > > (if (use-region-p) > > (funcall orig (region-beginning) (region-end)) > > (funcall orig (point-min) (point-max)))) > > (dolist (fn modi/region-or-whole-fns) > > (advice-add fn :around #'modi/region-or-whole-advice)) > > This will affect all calls to these functions and is hence sure to cause > some breakage somewhere. You want something more like: > > (defun modi/region-or-whole-advice (&rest _) > (interactive > (if (use-region-p) > (list (region-beginning) (region-end)) > (list (point-min) (point-max)))) > nil) > (dolist (fn modi/region-or-whole-fns) > (advice-add fn :before #'modi/region-or-whole-advice)) > > So you only modify the interactive spec (i.e. change the *command*'s > behavior) without changing the *function*'s behavior. > > > -- Stefan > > >