From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Michael Heerdegen Newsgroups: gmane.emacs.help Subject: Re: Help setting nadvice for indent-region Date: Thu, 11 Feb 2016 19:10:21 +0100 Message-ID: <87bn7n15ki.fsf@web.de> References: <87a8ne2k6v.fsf@web.de> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1455214262 10963 80.91.229.3 (11 Feb 2016 18:11:02 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Thu, 11 Feb 2016 18:11:02 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Thu Feb 11 19:10:52 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 1aTvhc-0006CT-7Q for geh-help-gnu-emacs@m.gmane.org; Thu, 11 Feb 2016 19:10:52 +0100 Original-Received: from localhost ([::1]:52405 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aTvhb-0000JW-DQ for geh-help-gnu-emacs@m.gmane.org; Thu, 11 Feb 2016 13:10:51 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:48417) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aTvhM-0000In-53 for help-gnu-emacs@gnu.org; Thu, 11 Feb 2016 13:10:37 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aTvhI-0002KP-9m for help-gnu-emacs@gnu.org; Thu, 11 Feb 2016 13:10:35 -0500 Original-Received: from plane.gmane.org ([80.91.229.3]:37090) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aTvhI-0002KC-1I for help-gnu-emacs@gnu.org; Thu, 11 Feb 2016 13:10:32 -0500 Original-Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1aTvhE-0005ub-D3 for help-gnu-emacs@gnu.org; Thu, 11 Feb 2016 19:10:28 +0100 Original-Received: from dslb-092-074-178-250.092.074.pools.vodafone-ip.de ([92.74.178.250]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 11 Feb 2016 19:10:28 +0100 Original-Received: from michael_heerdegen by dslb-092-074-178-250.092.074.pools.vodafone-ip.de with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 11 Feb 2016 19:10:28 +0100 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 53 Original-X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: dslb-092-074-178-250.092.074.pools.vodafone-ip.de User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.0.90 (gnu/linux) Cancel-Lock: sha1:fFAy7vSJ2bWb2qyFGT1AvDctcO4= X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 80.91.229.3 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:109107 Archived-At: Kaushal Modi 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.