Function: hydra--sort-heads

hydra--sort-heads is a byte-compiled function defined in hydra.el.

Signature

(hydra--sort-heads NORMALIZED-HEADS)

Documentation

Return a list of heads with non-nil doc grouped by column property.

Each head of NORMALIZED-HEADS must have a column property.

Source Code

;; Defined in ~/.emacs.d/elpa/hydra-20250316.1254/hydra.el
(defun hydra--sort-heads (normalized-heads)
  "Return a list of heads with non-nil doc grouped by column property.
Each head of NORMALIZED-HEADS must have a column property."
  (let* ((heads-wo-nil-doc (cl-remove-if-not (lambda (head) (nth 2 head)) normalized-heads))
         (columns-list (delete-dups (mapcar (lambda (head) (hydra--head-property head :column))
                                            normalized-heads)))
         (get-col-index-fun (lambda (head) (cl-position (hydra--head-property head :column)
                                                        columns-list
                                                        :test 'equal)))
         (heads-sorted (cl-sort heads-wo-nil-doc (lambda (it other)
                                                   (< (funcall get-col-index-fun it)
                                                      (funcall get-col-index-fun other))))))
    ;; this operation partition the sorted head list into lists of heads with same column property
    (cl-loop for head in heads-sorted
       for column-name = (hydra--head-property head :column)
       with prev-column-name = (hydra--head-property (nth 0 heads-sorted) :column)
       unless (equal prev-column-name column-name) collect heads-one-column into heads-all-columns
       and do (setq heads-one-column nil)
       collect head into heads-one-column
       do (setq prev-column-name column-name)
       finally return (append heads-all-columns (list heads-one-column)))))