From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Thuna Newsgroups: gmane.emacs.bugs Subject: bug#72344: [PATCH] Add a version of cl-once-only which handles lists of forms Date: Sun, 28 Jul 2024 23:17:31 +0200 Message-ID: <87y15l1aj8.fsf@gmail.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="9521"; mail-complaints-to="usenet@ciao.gmane.io" To: 72344@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sun Jul 28 23:18:08 2024 Return-path: Envelope-to: geb-bug-gnu-emacs@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 1sYBHM-0002LZ-6r for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 28 Jul 2024 23:18:08 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sYBH6-0001CV-Qu; Sun, 28 Jul 2024 17:17:52 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sYBH5-0001CJ-Ap for bug-gnu-emacs@gnu.org; Sun, 28 Jul 2024 17:17:51 -0400 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sYBH5-00074s-1b for bug-gnu-emacs@gnu.org; Sun, 28 Jul 2024 17:17:51 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=debbugs.gnu.org; s=debbugs-gnu-org; h=MIME-Version:Date:From:To:Subject; bh=sIIGmNK6k2IzqDKYJ1M0naYRvufu5OLWXuBNK2CSMhA=; b=TlGl8IaHaXpCHgKDLfYR1m1SH+0VWy9TYPb+5yipE1/wg+yJY6/jSEOBOzQpo6QDqDhjEp92EPTTsNQRbtnvut4ZRkhtl8k7/GTLZhHVm8vH6R+JQRIwK21Qct9EjwL6mOxvFS4h3Ntk8x6tj6SJQNQNTVa++YKT5cCdZ335Ds9PCflMSPxFO8tynDkmEPXjbWyXG0wfwm3wIF7hlwtFI3iX3h5ZChjzTTnlyQnFPusAAomzVywrZ3r6ZftHGxmkSmTOxFxexvTA4gOempO9bjg1S9fl8UJV57EP6+YQDLSG/1BJ19scTBjGB0PYDXbmVBwkSG206rK0mEPfNeHyHQ==; Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1sYBHG-0004HD-De for bug-gnu-emacs@gnu.org; Sun, 28 Jul 2024 17:18:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Thuna Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sun, 28 Jul 2024 21:18:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 72344 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch X-Debbugs-Original-To: bug-gnu-emacs@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.172220147316421 (code B ref -1); Sun, 28 Jul 2024 21:18:02 +0000 Original-Received: (at submit) by debbugs.gnu.org; 28 Jul 2024 21:17:53 +0000 Original-Received: from localhost ([127.0.0.1]:44398 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1sYBH6-0004Gn-Ga for submit@debbugs.gnu.org; Sun, 28 Jul 2024 17:17:52 -0400 Original-Received: from lists.gnu.org ([209.51.188.17]:41018) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1sYBH3-0004Gd-OB for submit@debbugs.gnu.org; Sun, 28 Jul 2024 17:17:51 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sYBGr-00019w-DS for bug-gnu-emacs@gnu.org; Sun, 28 Jul 2024 17:17:37 -0400 Original-Received: from mail-wr1-x42b.google.com ([2a00:1450:4864:20::42b]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sYBGp-0006z8-IO for bug-gnu-emacs@gnu.org; Sun, 28 Jul 2024 17:17:37 -0400 Original-Received: by mail-wr1-x42b.google.com with SMTP id ffacd0b85a97d-367990aaef3so1061114f8f.0 for ; Sun, 28 Jul 2024 14:17:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722201453; x=1722806253; darn=gnu.org; h=mime-version:message-id:date:subject:to:from:from:to:cc:subject :date:message-id:reply-to; bh=sIIGmNK6k2IzqDKYJ1M0naYRvufu5OLWXuBNK2CSMhA=; b=S/w49QGbL8ayfol8y0+eQ+wKTWDWqyIA+1QtTQJLutW2IfQshcea0nFl6UN7EKbHAr 9ht5npNe2n5qKsXgSw/pyGJWhxs548rD3m7TVVr4ony/pSbz9Rc2EKE9lLwNlFrWUv+N +TdiImgreND5UFKlai5ONJp+87sXbP0ZB0lDrAXVz/8RXWCyZbsFSEkULIEbzlLFwuCs mQQgmwfUpZ/STSVen25Agu+uPs4z2AnbwtsA3pxpNxPWiyIoRPiCH2nGlHjADDfq4xLM N167IZ/UG8tQuZls1VJcel/lkHd845hH9XOcZQJvxBqn/wCkEIkyvZzAmfijd40vjh7Z ldsw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722201453; x=1722806253; h=mime-version:message-id:date:subject:to:from:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=sIIGmNK6k2IzqDKYJ1M0naYRvufu5OLWXuBNK2CSMhA=; b=HVbQTbhHKJU35y6bwi/pZdR/KZQ/4O47G2kAD66b1ySng8XZmPe8ewMpNXV4VaT64W pw4ITNNYhITqqzFiETxSFHRXko+Q00mTDn+HmCUiJ3aLLXpeBp3fqNPRTCQmzFfOf+xT fvRt9TFTVYwpuEzv0pj8suC9hup13nXz2qNtaXoaf8OdFts6+sgwOsloh+2hYyEQy8Wa etHsM1ABabmsbv5vD/Ux4UmVZm9yUbsmGFXiVXZlPv0bGCf/Q+RfnwyS6JlTZhdgAs35 D4qC89rwb3oKKppW9yH/8yvs3l0+oLkV4vro7uW5VSsy2EJ8cNbTQLYCJ1cNSQ+E6zTU 9vcQ== X-Gm-Message-State: AOJu0YwZJo8FvVv+YX/AgMsPCiWl5Y8U4xFo+2GZUJ39G23D2y+vPFML TOclYALrI3I23mH3hBPyFouvDbn2ThP/5ha3nDzwXzNrYb+Tby884pEa8w== X-Google-Smtp-Source: AGHT+IFTwxqBgDFFwTWsptAZPwuzjHSpKhats60LnBsHX5Q2EbDkkED5KVxqiGzx5uNzcwJUfZbO7w== X-Received: by 2002:a5d:52c5:0:b0:360:7c4b:58c3 with SMTP id ffacd0b85a97d-36b5d0c2df7mr3304840f8f.54.1722201453077; Sun, 28 Jul 2024 14:17:33 -0700 (PDT) Original-Received: from thuna-lis3 ([85.106.105.81]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-5af65c0fedcsm3393309a12.55.2024.07.28.14.17.32 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 28 Jul 2024 14:17:32 -0700 (PDT) Received-SPF: pass client-ip=2a00:1450:4864:20::42b; envelope-from=thuna.cing@gmail.com; helo=mail-wr1-x42b.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 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, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list 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-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:289496 Archived-At: --=-=-= Content-Type: text/plain Attached is the macro `cl-once-only*', which takes a symbol which is bound to a list of form (such as those contained in a `&rest args' in `defmacro') and binds that variable to a list of symbols where each symbol is bound to the result of evaluating (in order) the corresponding form. The docstring provides an example which might illustrate what it does better then I did here. I don't expect this to be used too often but it could be somewhat handy in macros with a `&rest args' which are supposed to be evaluated. (My motivation for this is to use this macro in another patch which I didn't want to make longer with out-of-scope additions. If this is considered not worth polluting the `cl-' namespace over I can just write the forms by hand so it's fine either way.) --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0001-Add-a-version-of-cl-once-only-which-handles-lists-of.patch >From e8af4374454965d541ad3b8062e6793217d4e86f Mon Sep 17 00:00:00 2001 From: Thuna Date: Sun, 28 Jul 2024 22:26:41 +0200 Subject: [PATCH] Add a version of cl-once-only which handles lists of forms * lisp/emacs-lisp/cl-macs.el (cl-once-only*): Define macro. --- lisp/emacs-lisp/cl-macs.el | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/lisp/emacs-lisp/cl-macs.el b/lisp/emacs-lisp/cl-macs.el index 2e501005bf7..70d66b3a250 100644 --- a/lisp/emacs-lisp/cl-macs.el +++ b/lisp/emacs-lisp/cl-macs.el @@ -2544,6 +2544,43 @@ cl-once-only collect `(,(car name) ,gensym)) ,@body))))) +(defmacro cl-once-only* (listvar &rest body) + "Generate code to evaluate the list of forms LISTVAR just once in BODY. + +This is a macro to be used while defining other macros. LISTVAR should +be a variable which holds a list of forms, such as ARGS in a function +call of the form \\=`(,head ,@args). Within BODY, the variable LISTVAR +will be bound to a list of uninterned symbols of the same length, all of +which are variables in the final macroexpansion which are bound to the +result of evaluating the corresponding form. + +For example, the following macro + + (defmacro my-list (head &rest args) + (cl-once-only* args + \\=`(list (,head ,@args) ,@args))) + +when called like + + (let ((x \\='(1 5 4))) + (my-list + (pop x) (1+ (pop x)) (1- (pop x)))) + +will expand into + + (let ((x \\='(1 5 4))) + (let* ((arg1 (pop x)) (arg2 (1+ (pop x))) (arg3 (1- (pop x)))) + (list (+ arg1 arg2 arg3) arg1 arg2 arg3))) + +and the arguments will be evaluated only once and in order." + (declare (debug (arg body)) (indent 1)) + (let ((args (gensym (symbol-name listvar)))) + `(let ((,args + (cl-loop for i from 1 to (length ,listvar) collect + (make-symbol (format "%s$%d" ',(symbol-name args) i))))) + `(let* ,(cl-mapcar #'list ,args ,listvar) + ,(let ((,listvar ,args)) + ,@body))))) + ;;; Multiple values. ;;;###autoload -- 2.44.2 --=-=-=--