From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Joseph Turner via "Bug reports for GNU Emacs, the Swiss army knife of text editors" Newsgroups: gmane.emacs.bugs Subject: bug#63509: [PATCH] Make copy-tree work with records Date: Thu, 18 May 2023 12:05:57 -0700 Message-ID: <87pm6x31bt.fsf@breatheoutbreathe.in> References: <87fs7yfdg2.fsf@breatheoutbreathe.in> <83lehp25ug.fsf@gnu.org> <87sfbxtoib.fsf@breatheoutbreathe.in> <838rdlx648.fsf@gnu.org> Reply-To: Joseph Turner 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="23538"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 63509@debbugs.gnu.org, monnier@iro.umontreal.ca To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Thu May 18 21:08:17 2023 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 1pziz3-0005t4-MW for geb-bug-gnu-emacs@m.gmane-mx.org; Thu, 18 May 2023 21:08:17 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pziyq-0000fl-6f; Thu, 18 May 2023 15:08:04 -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 1pziyo-0000dl-TQ for bug-gnu-emacs@gnu.org; Thu, 18 May 2023 15:08:02 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pziyo-0003cX-L1 for bug-gnu-emacs@gnu.org; Thu, 18 May 2023 15:08:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1pziyo-0003z5-22 for bug-gnu-emacs@gnu.org; Thu, 18 May 2023 15:08:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Joseph Turner Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Thu, 18 May 2023 19:08:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 63509 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 63509-submit@debbugs.gnu.org id=B63509.168443687915307 (code B ref 63509); Thu, 18 May 2023 19:08:02 +0000 Original-Received: (at 63509) by debbugs.gnu.org; 18 May 2023 19:07:59 +0000 Original-Received: from localhost ([127.0.0.1]:54620 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pziyl-0003yp-7E for submit@debbugs.gnu.org; Thu, 18 May 2023 15:07:59 -0400 Original-Received: from out-33.mta0.migadu.com ([91.218.175.33]:43921) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pziyh-0003yd-Cp for 63509@debbugs.gnu.org; Thu, 18 May 2023 15:07:58 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=breatheoutbreathe.in; s=key1; t=1684436873; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=eulJZzP+Mz/in2KPutonpuGU8bXfVaHMv2sEvsjKgeo=; b=mPuQqQklvueYSALBqFF+B2NGrJdAKVjgqO6oNWRKYVwkna3xRT+x5PMp/ya824WHYNxIgt TYmOsfC+5rGAe6gXM1EnW/1XI28dyRYEL3Vp2SEp8dJiEkYgaCYVlNNOPnpXR9uiv/AxOL hU7O5Co5qRdFyS/avLpwo57GYytr/q4= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. In-reply-to: <838rdlx648.fsf@gnu.org> X-Migadu-Flow: FLOW_OUT 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:261955 Archived-At: --=-=-= Content-Type: text/plain Eli Zaretskii writes: > Yes, this new feature will be installed on the master branch, which > will become Emacs 30. Moved note from NEWS.29 to NEWS. >> --- a/doc/lispref/records.texi >> +++ b/doc/lispref/records.texi >> @@ -81,6 +81,18 @@ This function returns a new record with type @var{type} and >> @end example >> @end defun >> >> +@defun copy-tree tree &optional vector-like-p >> +This function copies a record when @var{vector-like-p} is >> +non-@code{nil}. >> + >> +@example >> +@group >> +(copy-tree (record 'foo "a")) >> + @result{} #s(foo "a") >> +@end group >> +@end example >> +@end defun > > This addition is redundant. We don't describe the same function in > more than one place. If there are reasons to mention it in other > places, we just add there a short note with a cross-reference to the > detailed description. Replaced @defun with a short sentence with @pxref. >> ++++ >> +** 'copy-tree' now correctly copies records when its optional second > > The "correctly" part hints that the previous behavior was a bug, which > it wasn't (and we don't mention bugfixes in NEWS anyway). So I would > rephrase > > 'copy-tree' can now copy records as well, when its optional... > >> +argument is non-nil. The second argument has been renamed from VECP >> +to VECTOR-LIKE-P since it now works with both vectors and records. > > The last sentence should be removed: we don't mention such minor > details in NEWS, unless the change is an incompatible change. Done. > Last, but not least: please always accompany your changes with > ChageLog-style commit log messages describing the changes. You can > find more information about this in the file CONTRIBUTE in the Emacs > tree, and you can see many examples by typing "git log" in the > repository. Done. Please let me know if any further changes need to be made! Best, Joseph --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=0001-Make-copy-tree-work-with-records.patch >From 0ae16ca89e581d3c732607b2daa700d8316a71e3 Mon Sep 17 00:00:00 2001 From: Joseph Turner Date: Sun, 14 May 2023 21:02:15 -0700 Subject: [PATCH] Make copy-tree work with records * doc/lispref/lists.texi (Building Cons Cells and Lists): Document new behavior of copy-tree. * doc/lispref/records.texi (Record Functions): Cross-reference to lists.texi. * etc/NEWS: Mention change. (Bug#63509) * lisp/emacs-lisp/shortdoc.el: Add copy-tree example to vector group. * lisp/subr.el (copy-tree): Recurse into records as well as vectors when optional second argument is non-nil. Rename second argument to from vecp to vector-like-p. * test/lisp/subr-tests.el: Test new behavior. --- doc/lispref/lists.texi | 9 +++++---- doc/lispref/records.texi | 3 +++ etc/NEWS | 3 +++ lisp/emacs-lisp/shortdoc.el | 2 ++ lisp/subr.el | 14 +++++++------- test/lisp/subr-tests.el | 31 +++++++++++++++++++++++++++++++ 6 files changed, 51 insertions(+), 11 deletions(-) diff --git a/doc/lispref/lists.texi b/doc/lispref/lists.texi index 22a5f7f1239..16ed0358974 100644 --- a/doc/lispref/lists.texi +++ b/doc/lispref/lists.texi @@ -696,16 +696,17 @@ not a list, the sequence's elements do not become elements of the resulting list. Instead, the sequence becomes the final @sc{cdr}, like any other non-list final argument. -@defun copy-tree tree &optional vecp +@defun copy-tree tree &optional vector-like-p This function returns a copy of the tree @var{tree}. If @var{tree} is a cons cell, this makes a new cons cell with the same @sc{car} and @sc{cdr}, then recursively copies the @sc{car} and @sc{cdr} in the same way. Normally, when @var{tree} is anything other than a cons cell, -@code{copy-tree} simply returns @var{tree}. However, if @var{vecp} is -non-@code{nil}, it copies vectors too (and operates recursively on -their elements). This function cannot cope with circular lists. +@code{copy-tree} simply returns @var{tree}. However, if +@var{vector-like-p} is non-@code{nil}, it copies vectors and records +too (and operates recursively on their elements). This function +cannot cope with circular lists. @end defun @defun flatten-tree tree diff --git a/doc/lispref/records.texi b/doc/lispref/records.texi index 26c6f30a6b5..d2c80a27f98 100644 --- a/doc/lispref/records.texi +++ b/doc/lispref/records.texi @@ -81,6 +81,9 @@ This function returns a new record with type @var{type} and @end example @end defun +@code{copy-tree} works with records when its optional second argument +is non-@code{nil} (@pxref{Building Lists}). + @node Backward Compatibility @section Backward Compatibility diff --git a/etc/NEWS b/etc/NEWS index ce865c9904d..c5063a718b9 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -585,6 +585,9 @@ Since circular alias chains now cannot occur, 'function-alias-p', 'indirect-function' and 'indirect-variable' will never signal an error. Their 'noerror' arguments have no effect and are therefore obsolete. ++++ +** 'copy-tree' now copies records when its optional argument is non-nil. + * Changes in Emacs 30.1 on Non-Free Operating Systems diff --git a/lisp/emacs-lisp/shortdoc.el b/lisp/emacs-lisp/shortdoc.el index 9a6f5dd12ce..6580e0e4e0c 100644 --- a/lisp/emacs-lisp/shortdoc.el +++ b/lisp/emacs-lisp/shortdoc.el @@ -833,6 +833,8 @@ A FUNC form can have any number of `:no-eval' (or `:no-value'), (seq-subseq :eval (seq-subseq [1 2 3 4 5] 1 3) :eval (seq-subseq [1 2 3 4 5] 1)) + (copy-tree + :eval (copy-tree [1 2 3 4])) "Mapping Over Vectors" (mapcar :eval (mapcar #'identity [1 2 3])) diff --git a/lisp/subr.el b/lisp/subr.el index 03d3324f3d8..83735933963 100644 --- a/lisp/subr.el +++ b/lisp/subr.el @@ -824,26 +824,26 @@ of course, also replace TO with a slightly larger value next (+ from (* n inc))))) (nreverse seq)))) -(defun copy-tree (tree &optional vecp) +(defun copy-tree (tree &optional vector-like-p) "Make a copy of TREE. If TREE is a cons cell, this recursively copies both its car and its cdr. Contrast to `copy-sequence', which copies only along the cdrs. With second -argument VECP, this copies vectors as well as conses." +argument VECTOR-LIKE-P, this copies vectors and records as well as conses." (declare (side-effect-free error-free)) (if (consp tree) (let (result) (while (consp tree) (let ((newcar (car tree))) - (if (or (consp (car tree)) (and vecp (vectorp (car tree)))) - (setq newcar (copy-tree (car tree) vecp))) + (if (or (consp (car tree)) (and vector-like-p (or (vectorp (car tree)) (recordp (car tree))))) + (setq newcar (copy-tree (car tree) vector-like-p))) (push newcar result)) (setq tree (cdr tree))) (nconc (nreverse result) - (if (and vecp (vectorp tree)) (copy-tree tree vecp) tree))) - (if (and vecp (vectorp tree)) + (if (and vector-like-p (or (vectorp tree) (recordp tree))) (copy-tree tree vector-like-p) tree))) + (if (and vector-like-p (or (vectorp tree) (recordp tree))) (let ((i (length (setq tree (copy-sequence tree))))) (while (>= (setq i (1- i)) 0) - (aset tree i (copy-tree (aref tree i) vecp))) + (aset tree i (copy-tree (aref tree i) vector-like-p))) tree) tree))) diff --git a/test/lisp/subr-tests.el b/test/lisp/subr-tests.el index 8f46c2af136..4ebb68556be 100644 --- a/test/lisp/subr-tests.el +++ b/test/lisp/subr-tests.el @@ -1206,5 +1206,36 @@ final or penultimate step during initialization.")) (should (equal a-dedup '("a" "b" "a" "b" "c"))) (should (eq a a-dedup)))) +(ert-deftest subr--copy-tree () + (should (eq (copy-tree nil) nil)) + (let* ((a (list (list "a") "b" (list "c") "g")) + (copy1 (copy-tree a)) + (copy2 (copy-tree a t))) + (should (equal a copy1)) + (should (equal a copy2)) + (should-not (eq a copy1)) + (should-not (eq a copy2))) + (let* ((a (list (list "a") "b" (list "c" (record 'foo "d")) (list ["e" "f"]) "g")) + (copy1 (copy-tree a)) + (copy2 (copy-tree a t))) + (should (equal a copy1)) + (should (equal a copy2)) + (should-not (eq a copy1)) + (should-not (eq a copy2))) + (let* ((a (record 'foo "a" (record 'bar "b"))) + (copy1 (copy-tree a)) + (copy2 (copy-tree a t))) + (should (equal a copy1)) + (should (equal a copy2)) + (should (eq a copy1)) + (should-not (eq a copy2))) + (let* ((a ["a" "b" ["c" ["d"]]]) + (copy1 (copy-tree a)) + (copy2 (copy-tree a t))) + (should (equal a copy1)) + (should (equal a copy2)) + (should (eq a copy1)) + (should-not (eq a copy2)))) + (provide 'subr-tests) ;;; subr-tests.el ends here -- 2.40.1 --=-=-=--