+(defvar elpher-bookmark-group-status (make-hash-table :test 'equal))
+
+(defun elpher-make-bookmark-group-status (group-path)
+ (let ((existing (gethash group-path elpher-bookmark-group-status)))
+ (unless existing
+ (puthash group-path (list nil nil nil) elpher-bookmark-group-status))))
+
+(defun elpher-bookmark-group-start (group-path)
+ (car (gethash group-path elpher-bookmark-group-status)))
+
+(defun elpher-bookmark-group-end (group-path)
+ (cadr (gethash group-path elpher-bookmark-group-status)))
+
+(defun elpher-bookmark-group-expanded-p (group-path)
+ (caddr (gethash group-path elpher-bookmark-group-status)))
+
+(defun elpher-bookmark-group-expanded-toggle (group-path)
+ (setcar (cddr (gethash group-path elpher-bookmark-group-status))
+ (not (elpher-bookmark-group-expanded-p group-path))))
+
+(defun elpher--bookmark-indent (group-path)
+ (insert (make-string (+ 1 (* 2 (length group-path))) ?\s)))
+
+(defun elpher--bookmark-group-starts-here (group-path)
+ (setcar (gethash group-path elpher-bookmark-group-status) (point)))
+
+(defun elpher--bookmark-group-ends-here (group-path)
+ (setcar (cdr (gethash group-path elpher-bookmark-group-status)) (point)))
+
+(defun elpher--update-bookmark-group-visibility (group-path)
+ (let ((start (elpher-bookmark-group-start group-path))
+ (end (elpher-bookmark-group-end group-path))
+ (inhibit-read-only t))
+ (put-text-property start end 'invisible
+ (not (elpher-bookmark-group-expanded-p group-path)))))
+
+(defun elpher--expand-or-collapse-bookmark-group (button)
+ (let ((group-path (button-get button 'elpher-bookmark-group-path)))
+ (elpher-bookmark-group-expanded-toggle group-path)
+ (elpher--update-bookmark-group-visibility group-path)))
+
+(defun elpher--insert-bookmark (bookmark &optional group-path)
+ (let* ((display-string (elpher-bookmark-display-string bookmark))
+ (address (elpher-address-from-url (elpher-bookmark-url bookmark)))
+ (type (if address (elpher-address-type address) nil))
+ (type-map-entry (cdr (assoc type elpher-type-map))))
+ (if type-map-entry
+ (let* ((face (elt type-map-entry 3))
+ (filtered-display-string (ansi-color-filter-apply display-string))
+ (page (elpher-make-page filtered-display-string address)))
+ (insert-text-button filtered-display-string
+ 'face face
+ 'elpher-page page
+ 'action #'elpher-click-link
+ 'follow-link t
+ 'help-echo #'elpher--page-button-help))
+ (insert (propertize display-string 'face 'elpher-unknown)))
+ (insert "\n")))
+
+(defun elpher--insert-bookmark-group (group-entries &optional group-path)
+ (if group-entries
+ (dolist (entry group-entries)
+ (elpher--bookmark-indent group-path)
+ (if (elpher-bookmark-p entry)
+ (elpher--insert-bookmark entry group-path)
+ (let* ((subgroup-name (elpher-bookmark-group-name entry))
+ (subgroup-entries (elpher-bookmark-group-entries entry))
+ (subgroup-path (cons subgroup-name group-path)))
+ (elpher-make-bookmark-group-status subgroup-path)
+ (insert-text-button subgroup-name
+ 'action #'elpher--expand-or-collapse-bookmark-group
+ 'elpher-bookmark-group-path subgroup-path
+ 'follow-link t
+ 'help-echo "Expand or collapse group.")
+ (insert "\n")
+ (elpher--bookmark-group-starts-here subgroup-path)
+ (elpher--insert-bookmark-group subgroup-entries subgroup-path)
+ (elpher--bookmark-group-ends-here subgroup-path)
+ (elpher--update-bookmark-group-visibility subgroup-path))))
+ (elpher--bookmark-indent group-path)
+ (insert "No bookmarks found.\n")))
+