custom--sort-vars
code: custom.el
(defun custom--sort-vars (vars)
"Sort VARS based on custom dependencies.
VARS is a list whose elements have the same form as the ARGS
arguments to `custom-theme-set-variables'. Return the sorted
list, in which A occurs before B if B was defined with a
:set-after' keyword specifying A (see defcustom')."
(let ((custom--sort-vars-table (make-hash-table))
(dependants (make-hash-table))
(custom--sort-vars-result nil)
last)
;; Construct a pair of tables keyed with the symbols of VARS.
(dolist (var vars)
(puthash (car var) (cons t var) custom--sort-vars-table)
(puthash (car var) var dependants))
;; From the second table, remove symbols that are depended-on.
(dolist (var vars)
(dolist (dep (get (car var) 'custom-dependencies))
(remhash dep dependants)))
;; If a variable is "stand-alone", put it last if it's a minor
;; mode or has a :require flag. This is not really necessary, but
;; putting minor modes last helps ensure that the mode function
;; sees other customized values rather than default values.
(maphash (lambda (sym var)
(when (and (null (get sym 'custom-dependencies))
(or (nth 3 var)
(eq (get sym 'custom-set)
'custom-set-minor-mode)))
(remhash sym dependants)
(push var last)))
dependants)
;; The remaining symbols depend on others but are not
;; depended-upon. Do a depth-first topological sort.
(nreverse (append last custom--sort-vars-result))))