From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: akater Newsgroups: gmane.emacs.devel Subject: defmacro with built-in gensym declaration and initialization Date: Wed, 20 Jan 2021 08:15:46 +0000 Message-ID: <87bldk6n2l.fsf@gmail.com> Mime-Version: 1.0 Content-Type: multipart/signed; boundary="=-=-="; micalg=pgp-sha512; protocol="application/pgp-signature" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="4030"; mail-complaints-to="usenet@ciao.gmane.io" To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Wed Jan 20 09:27:54 2021 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 1l28qH-0000vn-UY for ged-emacs-devel@m.gmane-mx.org; Wed, 20 Jan 2021 09:27:54 +0100 Original-Received: from localhost ([::1]:43510 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1l28qG-0001cL-VW for ged-emacs-devel@m.gmane-mx.org; Wed, 20 Jan 2021 03:27:52 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:36168) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1l28p6-0000YN-Lk for emacs-devel@gnu.org; Wed, 20 Jan 2021 03:26:41 -0500 Original-Received: from mail-pl1-x632.google.com ([2607:f8b0:4864:20::632]:34643) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1l28p4-0007tf-8V for emacs-devel@gnu.org; Wed, 20 Jan 2021 03:26:40 -0500 Original-Received: by mail-pl1-x632.google.com with SMTP id t6so12125477plq.1 for ; Wed, 20 Jan 2021 00:26:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:mime-version; bh=edtNmdsSj0tSlhKbES2W7s2XT+Wx9V33D1lmXQsRVHU=; b=V1CMVDhhJ7Pd7xNi9awJjc10+L9D0DsxiNg3KFvjy26DZn49yo9FQsmCbmH7pFtYF7 V9J934CQSyxwLdUv6EJPPiUDoxKpQ2ihVzxHfp4GHpAqG0YIpQWqdF4D3iJ5xQP5noud yuZP4+Ed++Qzwoo1yQu0n6FRz9DsJbWN28KxbY3In+qaBBEQMUQHXru4amVaEyO9Y4VM ParU/Xb3MB12lSYV9Y81aaPe2FHdYcpgN4Hqp4Xtd1OWDQtUGqX+cV9nIo8ugfyRy3vi gMf0fo6sdnthRPkDTZ+pvXLEOGdDR6u+bUKPTFzyo6f6Xe8c7Oj3t6dWd6REvJvKrZMw AszA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:mime-version; bh=edtNmdsSj0tSlhKbES2W7s2XT+Wx9V33D1lmXQsRVHU=; b=Xj9spQYC6ZcYZWE1QDxJG9J3vMng4WAKr2zdQStfa9/mYtmUoAqKG6iuPlO5wNAOFC zxf/CYJuHSTa7c/mDK1a+30X1h4bhN9wihCoftgIil9X8d+mbHKoAsjUneiJlkVQ20rv Y3NlrYaeNTNmvLgErvFC1fwkGnskYGDdTfcK6P9YRf7O2j/f7OMNyXEI6yBh9Zs8qXim EJ4VTpT39OPsLAsM4Zk3FVdgPkuNoHN/6hEtfJ4FqUpsNAQhN7jRCP9qKrVHZ2pRIRqZ 2w3zOGxhuUM6QY9jmhWzHqfkvA5bL0feRue080iFvtNrJU+F7Tl3Z+490i0UIYCAzK5S M+NQ== X-Gm-Message-State: AOAM5338yEBrfU+F0ANJGH/JSQ5pUQvWs4OYYwILqy0O3kDcujUYhUtG oS8yRX3bKHCFzKNMEwz5efSyYg6W0w0dAQ== X-Google-Smtp-Source: ABdhPJxjate4idepI0ylYOqhwuR/E0p5Slwqm7vTHxj/AedDq/GGzQbGphOmGqnurFUpw79vNhHmCw== X-Received: by 2002:a17:90a:34c8:: with SMTP id m8mr4551809pjf.103.1611131196301; Wed, 20 Jan 2021 00:26:36 -0800 (PST) Original-Received: from localhost (tor-exit.las1.1800867.xyz. [209.141.50.178]) by smtp.googlemail.com with ESMTPSA id f64sm1448657pfb.146.2021.01.20.00.26.34 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Jan 2021 00:26:35 -0800 (PST) Received-SPF: pass client-ip=2607:f8b0:4864:20::632; envelope-from=nuclearspace@gmail.com; helo=mail-pl1-x632.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: emacs-devel@gnu.org X-Mailman-Version: 2.1.23 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:263196 Archived-At: --=-=-= Content-Type: text/plain I suggest extending ~defmacro~ to support ~&gensym~ keyword in its lambda list, for convenient declaration and initialization of gensyms. Below there's a link to working implementation, and to specification. A realistic example from popular package =dash= #+begin_example emacs-lisp (defmacro --partition-by (form list) "Anaphoric form of `-partition-by'." (declare (debug (form form))) (let ((r (make-symbol "result")) (s (make-symbol "sublist")) (v (make-symbol "value")) (n (make-symbol "new-value")) (l (make-symbol "list"))) `(let ((,l ,list)) (when ,l (let* ((,r nil) (it (car ,l)) (,s (list it)) (,v ,form) (,l (cdr ,l))) (while ,l (let* ((it (car ,l)) (,n ,form)) (unless (equal ,v ,n) (!cons (nreverse ,s) ,r) (setq ,s nil) (setq ,v ,n)) (!cons it ,s) (!cdr ,l))) (!cons (nreverse ,s) ,r) (nreverse ,r)))))) #+end_example could then be rewritten as #+begin_example emacs-lisp (defmacro --partition-by ( form list &gensym result sublist value new-value list) "Anaphoric form of `-partition-by'." (declare (debug (form form))) `(when ,list (let* ((,result nil) (it (car ,list)) (,sublist (list it)) (,value ,form) (,list (cdr ,list))) (while ,list (let* ((it (car ,list)) (,new-value ,form)) (unless (equal ,value ,new-value) (!cons (nreverse ,sublist) ,result) (setq ,sublist nil) (setq ,value ,new-value)) (!cons it ,sublist) (!cdr ,list))) (!cons (nreverse ,sublist) ,result) (nreverse ,result)))) #+end_example Lambda list keyword ~&gensym~, as implemented below, also provides ~once-only~ functionality. For example, the definition #+begin_example emacs-lisp (defmacro with-file-buffer (filename &rest body) "Visit FILENAME unless already visited. Set the buffer as current, evaluate BODY forms. Kill the buffer if it did not exist initially." (declare (indent 1)) (let ((o-o-filename (gensym "filename-")) (exists-g (gensym "exists-")) (buffer-g (gensym "buffer-"))) `(let* ((,o-o-filename ,filename) (,exists-g (get-file-buffer ,o-o-filename)) (,buffer-g (or ,exists-g (find-file-noselect ,o-o-filename)))) (unwind-protect (with-current-buffer ,buffer-g ,@body) (unless ,exists-g (kill-buffer ,buffer-g)))))) #+end_example could be rewritten as #+begin_example emacs-lisp (defmacro with-file-buffer ( filename &rest body &gensym filename (exists (get-file-buffer filename)) (buffer (or exists (find-file-noselect filename)))) "Visit FILENAME unless already visited. Set the buffer as current, evaluate BODY forms. Kill the buffer if it did not exist initially." (declare (indent 1)) `(unwind-protect (with-current-buffer ,buffer ,@body) (unless ,exists (kill-buffer ,buffer)))) #+end_example This ~&gensym~ facility eliminates the need for ~with-gensyms~ and ~once-only~ in cases when gensyms are created unconditionally by macro function (in Common Lisp parlance). An implementation ~defmacro/&gensym~ is accessible at git@gitlab.com:akater/defmacro-gensym.git It is a fairly lengthy (26.6k words) Org file with exposition, motivation, somewhat extensive tests (for the macro, its variations and essential dependencies), examples, a ~cl-demacro/&gensym~ version that supports destructuring lambda lists, and variations which expand constants early. There are 118 tests and examples for all the variations in total. The file can be tangled to a self-contained elisp file that provides the macros in question. Patch not provided because my implementation depends significantly on ~cl-symbol-macrolet~ which doesn't look like it could be used in such a low-level code. --=-=-= Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQJLBAEBCgA1FiEEgu5SJRdnQOF34djNsr6xYbHsf0QFAmAH5r0XHG51Y2xlYXJz cGFjZUBnbWFpbC5jb20ACgkQsr6xYbHsf0QDOQ//fi+oSqjruJm5uxIpOINtCfEo xKZzkc2UFHmYHy2Y0pDYCC1AlUOtSnOnRMmdguTpXfJHTQf8pjhWPqfNgrb6mUZw 07ewnVKeE1vuHd25ge42WB21iRECXSMb0QfSOBpVFdx3wKGRvABYNfjABMd5xvI3 eXAF2ijykRLZHx46etD13lKRPWyB8YWyHIwOQnYMh0suD4FslJI/FI11f91SDi26 BjwUqJSbkR2W40+co5j+nQw0BqOZ6YhHB1llUINzA/+57zxRvTDGXb/ex/9yYWQd ojGy9opLABHKdfp/ylkM2DtJDfn9y2j04yKGlJNPaNs8H1ZKlfn9qknz2gfVy8WQ fwmUUugBrfYd5sSiX53b3grUFTjDefH4zyGfjH8clUDHEwsIYV5eYlTQuOl+8ti1 6GW8K7WHXeAFgUnqUcJAF7v8h8MAwZYnN9+rv0zViKpCasxIajkUmWFxvA8It4fn LcGBY+YpoVGywXIWwbSt4Tq23GBRIzi6hsNPFH3WlpQInjwwpTeHjOdFnp6/lSPA TI2rzH1/yptYfy6HMhvRYzPKjGBC9bcotFwpCGtWSnu/y5oqvWhZmGPUDcGXmdZ7 THpXrVMEFy0EBCqZkTYwVtlzrCIPpa6GLy+igf+pgiGgTcmpLV6gSR/j98kUjBk0 UYSNSYw0xkjozp8Z4X4= =ZiGp -----END PGP SIGNATURE----- --=-=-=--