From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Alan Mackenzie Newsgroups: gmane.emacs.bugs Subject: bug#33601: 26; Add macro `with-hook-added' Date: Tue, 4 Dec 2018 20:10:48 +0000 Message-ID: <20181204201048.GA13646@ACM> References: <20181204184614.2049.qmail@mail.muc.de> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: blaine.gmane.org 1543954454 484 195.159.176.226 (4 Dec 2018 20:14:14 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Tue, 4 Dec 2018 20:14:14 +0000 (UTC) User-Agent: Mutt/1.10.1 (2018-07-13) Cc: 33601@debbugs.gnu.org To: Drew Adams Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Tue Dec 04 21:14:09 2018 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1gUH57-0008RH-8r for geb-bug-gnu-emacs@m.gmane.org; Tue, 04 Dec 2018 21:14:09 +0100 Original-Received: from localhost ([::1]:58844 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gUH7D-0005XW-MY for geb-bug-gnu-emacs@m.gmane.org; Tue, 04 Dec 2018 15:16:19 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:47723) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gUH70-0005Wa-Pr for bug-gnu-emacs@gnu.org; Tue, 04 Dec 2018 15:16:07 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gUH6w-0006RU-UM for bug-gnu-emacs@gnu.org; Tue, 04 Dec 2018 15:16:06 -0500 Original-Received: from debbugs.gnu.org ([208.118.235.43]:56738) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gUH6w-0006Po-A8 for bug-gnu-emacs@gnu.org; Tue, 04 Dec 2018 15:16:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1gUH6w-0001Q4-0z for bug-gnu-emacs@gnu.org; Tue, 04 Dec 2018 15:16:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Alan Mackenzie Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Tue, 04 Dec 2018 20:16:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 33601 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: Original-Received: via spool by 33601-submit@debbugs.gnu.org id=B33601.15439545132998 (code B ref 33601); Tue, 04 Dec 2018 20:16:01 +0000 Original-Received: (at 33601) by debbugs.gnu.org; 4 Dec 2018 20:15:13 +0000 Original-Received: from localhost ([127.0.0.1]:60996 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1gUH68-0000lx-LH for submit@debbugs.gnu.org; Tue, 04 Dec 2018 15:15:12 -0500 Original-Received: from colin.muc.de ([193.149.48.1]:58470 helo=mail.muc.de) by debbugs.gnu.org with smtp (Exim 4.84_2) (envelope-from ) id 1gUH66-0000iJ-2L for 33601@debbugs.gnu.org; Tue, 04 Dec 2018 15:15:10 -0500 Original-Received: (qmail 27375 invoked by uid 3782); 4 Dec 2018 20:15:08 -0000 Original-Received: from acm.muc.de (p2E5D5BD9.dip0.t-ipconnect.de [46.93.91.217]) by colin.muc.de (tmda-ofmipd) with ESMTP; Tue, 04 Dec 2018 21:15:07 +0100 Original-Received: (qmail 14338 invoked by uid 1000); 4 Dec 2018 20:10:48 -0000 Content-Disposition: inline In-Reply-To: X-Delivery-Agent: TMDA/1.1.12 (Macallan) X-Primary-Address: acm@muc.de X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 208.118.235.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.org gmane.emacs.bugs:153078 Archived-At: Hello, Drew. On Tue, Dec 04, 2018 at 11:18:29 -0800, Drew Adams wrote: > > > Enhancement request: Please consider adding a macro such as this, to > > > facilitate temporary use of a hook function. > > > (defmacro with-hook-added (hook function &rest body) > > > "Evaluate forms in BODY with FUNCTION added to HOOK, then remove FUNCTION." > > > (declare (indent 1) (debug t)) > > > `(unwind-protect (progn (add-hook ',hook ',function) ,@body) > > > (remove-hook ',hook ',function))) > > Just a big point: you need to test whether FUNCTION is already on HOOK > > at the start, and if so, not remove it at the end. > Hi Alan, > A big point? Need to? I think so, yes. The essence of the `with-...' macros is that they temporarily change something, then evaluate ,@body, and at the end, the something is restored to what it was. If with-hook-added didn't preserve the hook, it would be an anomaly, an outlier, and quite possibly a PITA. > That wasn't the behavior I had in mind for this, but it's > another possibility. I intended to provide only for the > behavior of always removing at the end. Why? What's the use case? > There are in fact several different possibilities for such > a macro. We could provide also for `add-hook' args APPEND > and LOCAL. I left that out as well. > Choices: > 1. Provide a single macro for all such possibilities, with > 3 (mandatory) args for APPEND, LOCAL and whether to remove > FUNCTION at the end if it was already present at the outset. > 2. Provide multiple macros, each specific for a given case. > #2 would mean 8 macros, to cover all the combinations > (nil or t for each of the 3 args). How many of these would actually be of any use? > Another possibility would be to accept a single arg for > the BODY code, instead of that being a &rest parameter, > and so be able to provide those 3 behavior-specifying > args as optional. In that case, we'd want to decide on > the best order among those args, e.g., based on which we > expect to be used most often. > I'm not sure what the right approach is. I think the > most common use case would be the one I wrote up (but > I don't know that): > . Always remove FUNCTION at the end > . Prepend, not append. > . Global, not local. -- Alan Mackenzie (Nuremberg, Germany).