diff --git a/lisp/subr.el b/lisp/subr.el index f7eac75305..3fed3bc436 100644 --- a/lisp/subr.el +++ b/lisp/subr.el @@ -5453,10 +5453,15 @@ flatten-tree This always returns a list containing all the elements of TREE. \(flatten-tree \\='(1 (2 3 (4 5 (6))) 7)) => (1 2 3 4 5 6 7)" - (cond ((null tree) nil) - ((consp tree) (append (flatten-tree (car tree)) - (flatten-tree (cdr tree)))) - (t (list tree)))) + (let (elems) + (setq tree (list tree)) + (while (let ((elem (pop tree))) + (cond ((consp elem) + (setq tree (cons (car elem) (cons (cdr elem) tree)))) + (elem + (push elem elems))) + tree)) + (nreverse elems))) ;; Technically, `flatten-list' is a misnomer, but we provide it here ;; for discoverability: