From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Sean Whitton Newsgroups: gmane.emacs.devel Subject: [PATCH] Adding with-gensyms and once-only to subr-x Date: Mon, 11 Apr 2022 09:26:36 -0700 Message-ID: <87a6crr3gj.fsf@athena.silentflame.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="13730"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Emacs/29.0.50 (x86_64-pc-linux-gnu) To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Mon Apr 11 18:29:30 2022 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ndwut-0003K2-Fz for ged-emacs-devel@m.gmane-mx.org; Mon, 11 Apr 2022 18:29:29 +0200 Original-Received: from localhost ([::1]:60008 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ndwus-0001oO-FI for ged-emacs-devel@m.gmane-mx.org; Mon, 11 Apr 2022 12:29:26 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:38170) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ndwsI-0000AX-3Z for emacs-devel@gnu.org; Mon, 11 Apr 2022 12:26:46 -0400 Original-Received: from wout2-smtp.messagingengine.com ([64.147.123.25]:51139) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ndwsE-0007xt-BB for emacs-devel@gnu.org; Mon, 11 Apr 2022 12:26:45 -0400 Original-Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.west.internal (Postfix) with ESMTP id E9DF4320091A for ; Mon, 11 Apr 2022 12:26:38 -0400 (EDT) Original-Received: from mailfrontend2 ([10.202.2.163]) by compute3.internal (MEProxy); Mon, 11 Apr 2022 12:26:39 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=spwhitton.name; h=cc:content-type:date:date:from:from:in-reply-to:message-id :mime-version:reply-to:sender:subject:subject:to:to; s=fm3; t= 1649694398; x=1649780798; bh=ObgMq6TgRi6valjx3WtIx5Gv8yyLv5SZc/Z wjfSvqFw=; b=rtbot+9OheYGlE7JtjU0n32JnGWhK8kFKjNDGqGsRIHvIWBVDLy o2H9PSS1r1RsP05NhCk+5F90hBJWbEmpfmjb6AC0GhHYHcTqv6CfGU04JVUsVaf4 0+LC8ZqJ47zvj7ujpINBimH9/tNBrnES5Ib+GlmPjuOSTlin1w4Dy25mhbsJJqu5 FKiuDTSklpw3ZmrG4Bgn/oru/3/M4Z+gR1USqTzWPySENKjuckIihSivD3NcCyDo C5b/0DLanHl4AU4Ea+AgY3vRKIxczHYNgU7eoS+CFj6WlKiNVtbon+YVViYP0PQO /ZI3hg7cKxFVcx+61f3i3ikjnsh5ZXeEs3Q== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:date:date:from:from :in-reply-to:message-id:mime-version:reply-to:sender:subject :subject:to:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm3; t=1649694398; x=1649780798; bh=ObgMq6TgRi6va ljx3WtIx5Gv8yyLv5SZc/ZwjfSvqFw=; b=Z8iqMumAGpeHwlhGM/k4m4+cRHlNo LCxfMiK2OxL/Vj9So6BVzs8+07DhqTtMUywG+InsSM4YBUgQW6AbP/m/ADnsYX7p XJe/8ccu8IMoh5wbOa0mBBWnbeLBSSihg0mML8SbWrZaSEMJNPDKmId7EHo1YN1N Uo6tx0GHZWzUezG81rhaewjOZ4Um1HIQ4q/Oo6XmhsCWPByC+m7fAm0o8rNb1jrM dEWS63qpfaR3ik0lxOTGu79AevJSxZ4ljMk5A3dNi4p5ej+ox0/l+Nvw01WKO3KU kHwxkU7qovIacIYp0LnuhL5uFSgw+NnnlY+9jefNUyyQZUcNFrwfzyU2w== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvvddrudekiedguddtvdcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecunecujfgurhephffvufgffffkgggtsehmtddtre dtredtnecuhfhrohhmpefuvggrnhcuhghhihhtthhonhcuoehsphifhhhithhtohhnsehs phifhhhithhtohhnrdhnrghmvgeqnecuggftrfgrthhtvghrnhepkeeggeeujeeludegve evveeljeffhfelhfegfffhudehhffhkedvheeftedvueelnecuvehluhhsthgvrhfuihii vgeptdenucfrrghrrghmpehmrghilhhfrhhomhepshhpfihhihhtthhonhesshhpfihhih htthhonhdrnhgrmhgv X-ME-Proxy: Original-Received: by mail.messagingengine.com (Postfix) with ESMTPA for ; Mon, 11 Apr 2022 12:26:38 -0400 (EDT) Original-Received: by athena.silentflame.com (Postfix, from userid 1000) id A07A31B7978; Mon, 11 Apr 2022 16:26:36 +0000 (UTC) Received-SPF: pass client-ip=64.147.123.25; envelope-from=spwhitton@spwhitton.name; helo=wout2-smtp.messagingengine.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.io gmane.emacs.devel:288223 Archived-At: --=-=-= Content-Type: text/plain Hello, Lately I have been finding myself wanting with-gensyms and once-only, two classic macro-writing macros, available in core Elisp. (There is already org-with-gensyms, but it's not in the least bit Org-specific.) Here is my implementation for review. -- Sean Whitton --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=0001-lisp-emacs-lisp-subr-x.el-with-gensyms-once-only-New.patch >From 89e86db1ccb97ce3f91d5b4beb5d7b461311d196 Mon Sep 17 00:00:00 2001 From: Sean Whitton Date: Mon, 11 Apr 2022 09:20:35 -0700 Subject: [PATCH] * lisp/emacs-lisp/subr-x.el (with-gensyms, once-only): New macros. --- lisp/emacs-lisp/subr-x.el | 43 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/lisp/emacs-lisp/subr-x.el b/lisp/emacs-lisp/subr-x.el index abf85ab6c6..c74bf7f5f0 100644 --- a/lisp/emacs-lisp/subr-x.el +++ b/lisp/emacs-lisp/subr-x.el @@ -526,6 +526,49 @@ read-process-name (error "No process selected")) process))) +(defmacro with-gensyms (names &rest body) + "Bind each of NAMES to an uninterned symbol and evaluate BODY." + (declare (debug (sexp body)) (indent 1)) + `(let ,(cl-loop for name in names collect + `(,name (gensym (symbol-name ',name)))) + ,@body)) + +(defmacro once-only (names &rest body) + "Generate code to evaluate each of NAMES just once in BODY. + +This macro helps with writing other macros. Each of names is +either (NAME FORM) or NAME, which latter means (NAME NAME). +During macroexpansion, each NAME is bound to an uninterned +symbol. The expansion evaluates each FORM and binds it to the +corresponding uninterned symbol. + +For example, consider this macro: + + (defmacro my-cons (x) + (once-only (x) + `(cons ,x ,x))) + +Consider the call (my-cons (pop y)). This will expand to +something like this: + + (let ((g1 (pop y))) + (cons g1 g1)) + +This ensures that the pop is performed only once, as wanted." + (declare (debug (sexp body)) (indent 1)) + (setq names (mapcar #'ensure-list names)) + (let ((our-gensyms (cl-loop for name in names collect (gensym)))) + ;; During macroexpansion, obtain a gensym for each NAME. + `(let ,(cl-loop for sym in our-gensyms collect `(,sym (gensym))) + ;; Evaluate each FORM and bind to the corresponding gensym. + `(let (,,@(cl-loop for name in names and gensym in our-gensyms + for to-eval = (or (cadr name) (car name)) + collect ``(,,gensym ,,to-eval))) + ;; During macroexpansion, bind each NAME to its gensym. + ,(let ,(cl-loop for name in names and gensym in our-gensyms + collect `(,(car name) ,gensym)) + ,@body))))) + (provide 'subr-x) ;;; subr-x.el ends here -- 2.30.2 --=-=-=--