Function: hierarchy-add-list

hierarchy-add-list is a byte-compiled function defined in hierarchy.el.gz.

Signature

(hierarchy-add-list HIERARCHY LIST &optional WRAP CHILDRENFN)

Documentation

Add to HIERARCHY the sub-lists in LIST.

If WRAP is non-nil, allow duplicate items in LIST by wrapping each item in a cons (id . item). The root's id is 1.

CHILDRENFN is a function (defaults to cdr) taking LIST as a parameter which should return LIST's children (a list). Each child is (recursively) passed as a parameter to CHILDRENFN to get its own children. Because of this parameter, LIST can be anything, not necessarily a list.

Source Code

;; Defined in /usr/src/emacs/lisp/emacs-lisp/hierarchy.el.gz
(defun hierarchy-add-list (hierarchy list &optional wrap childrenfn)
  "Add to HIERARCHY the sub-lists in LIST.

If WRAP is non-nil, allow duplicate items in LIST by wrapping each
item in a cons (id . item).  The root's id is 1.

CHILDRENFN is a function (defaults to `cdr') taking LIST as a
parameter which should return LIST's children (a list).  Each
child is (recursively) passed as a parameter to CHILDRENFN to get
its own children.  Because of this parameter, LIST can be
anything, not necessarily a list."
  (let* ((childrenfn (or childrenfn #'cdr))
         (id 0)
         (wrapfn (lambda (item)
                   (if wrap
                       (cons (setq id (1+ id)) item)
                     item)))
         (unwrapfn (if wrap #'cdr #'identity)))
    (hierarchy-add-tree
     hierarchy (funcall wrapfn list) nil
     (lambda (item)
       (mapcar wrapfn (funcall childrenfn
                               (funcall unwrapfn item)))))
    hierarchy))