Function: copy-tree
copy-tree is a byte-compiled function defined in subr.el.gz.
Signature
(copy-tree TREE &optional VECP)
Documentation
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.
Other relevant functions are documented in the list group.
Probably introduced at or before Emacs version 22.1.
Shortdoc
;; list
(copy-tree '(1 (2 3) 4))
=> (1 (2 3) 4)
Aliases
gnus-copy-sequence (obsolete since 27.1)
cl-copy-tree (obsolete since 24.3)
eshell-copy-tree (obsolete since 28.1)
Source Code
;; Defined in /usr/src/emacs/lisp/subr.el.gz
(defun copy-tree (tree &optional vecp)
"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."
(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)))
(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))
(let ((i (length (setq tree (copy-sequence tree)))))
(while (>= (setq i (1- i)) 0)
(aset tree i (copy-tree (aref tree i) vecp)))
tree)
tree)))