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: Fri, 12 Feb 2016 11:02:03 -0500 Message-ID: References: <87a8ne2k6v.fsf@web.de> <87bn7n15ki.fsf@web.de> <87vb5vys7n.fsf@web.de> <871t8idnpq.fsf@web.de> <87k2mac8l7.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 1455292993 31302 80.91.229.3 (12 Feb 2016 16:03:13 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Fri, 12 Feb 2016 16:03:13 +0000 (UTC) Cc: Help Gnu Emacs mailing list To: Michael Heerdegen Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Fri Feb 12 17:03:09 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 1aUGBY-0006L2-HE for geh-help-gnu-emacs@m.gmane.org; Fri, 12 Feb 2016 17:03:08 +0100 Original-Received: from localhost ([::1]:33913 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aUGBU-0002fp-2U for geh-help-gnu-emacs@m.gmane.org; Fri, 12 Feb 2016 11:03:04 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:37372) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aUGBC-0002ey-AC for help-gnu-emacs@gnu.org; Fri, 12 Feb 2016 11:02:50 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aUGBA-00050v-Hc for help-gnu-emacs@gnu.org; Fri, 12 Feb 2016 11:02:46 -0500 Original-Received: from mail-ob0-x232.google.com ([2607:f8b0:4003:c01::232]:36330) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aUGBA-00050r-AF for help-gnu-emacs@gnu.org; Fri, 12 Feb 2016 11:02:44 -0500 Original-Received: by mail-ob0-x232.google.com with SMTP id gc3so26211141obb.3 for ; Fri, 12 Feb 2016 08:02:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=S2YHhvZ3r1VEyrUdngDsFOd7sBcX0/Ibsuo1rX1ITEw=; b=O92g2f4zTwxpXbGzmUbEKNz5UjEBcEn65nSGddxgL9FKCZyO9i3HDCl/0c6YshKxsx TGDqe5dTDA1oX/MyKJSyhJcuvGsv0pW57J6JR2o3k6tf/z6TjJ6NV0mJdmwrLPwHv+mv IVM/nNXk+L9ZaGsXp04hf1Em6bUQGFXiXi3v0rTK16jW+lA8Yys5aRb0sxG5JegT39t3 DwmDmIfb9hLNsWc5zylyTqY+El/FckGThi6zaF/qqQ5LDPI1PzButyoW92YA6KHeZxIN NQPVbWAkkZyk6lZg+AvR7gnLXRhmC/7P+zLy2HASamOAw6Bt6dQXj7ZIhJCEy0SSx2RK hnMg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc:content-type; bh=S2YHhvZ3r1VEyrUdngDsFOd7sBcX0/Ibsuo1rX1ITEw=; b=YOXx46ZUjMGcx5u2PBZJZRc3SFojtvGdsHDDBPvzKOcI0zNf6i2k8DrqdUGtn9uDkX uvacWECQ9dLLzug4IZZs0LFfwjY6wlFFPxnWHzJHL9RS0QS3W99gwXY/doiigdlYelnz RAnrQtjq24o9AuhjqMbiExNtzPJuA+BvMauIAqlyUfrqTZo0kBTKlNOHvMwZ16Ca9LLo xM+bWUQtzNmpJtTgG/gwC3ZdmEdxV2EJN+FaYHFl2iPZ72gRHjiVYmWppM+d6E6FnOHz RMy0SM0OuKW8e24497Fg7ByAc1YW1qsua13bvDCIixakliRIxCPRU8qatcvYxXASGVza 7buQ== X-Gm-Message-State: AG10YOQPA+00DSL68Hc8n/ulkqtJAdFjjtky30U0pePYSXPq9sSrApKtkH/WQO+1kGsyswnmF0xM9BlYjq2rkg== X-Received: by 10.182.137.198 with SMTP id qk6mr1860135obb.5.1455292963389; Fri, 12 Feb 2016 08:02:43 -0800 (PST) Original-Received: by 10.202.201.73 with HTTP; Fri, 12 Feb 2016 08:02:03 -0800 (PST) In-Reply-To: <87k2mac8l7.fsf@web.de> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2607:f8b0:4003:c01::232 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:109137 Archived-At: 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 :)