Subject: bug#77928: 31.0.50; ebcde0f90f6 (bug#77928) breaks :custom-face for themed faces

From: Steven Allen <[email-masked]>
Date: Sun, 08 Jun 2025 15:46:38 -0700

Steven Allen <[email-masked]> writes:
> The former (custom theme) sounds like what use-package does with :custom
> (it defines a use-package theme). Maybe just implement :custom-face the same way :custom is
> implemented? I.e.:
>
>
>     (defun use-package-handler/:custom-face (name _keyword args rest state)
>       (use-package-concat
>        (if (bound-and-true-p use-package-use-theme)
>            `((let ((custom--inhibit-theme-enable nil))
>                ;; Declare the theme here so use-package can be required inside
>                ;; eval-and-compile without warnings about unknown theme.
>                (unless (memq 'use-package custom-known-themes)
>                  (deftheme use-package)
>                  (enable-theme 'use-package)
>                  (setq custom-enabled-themes (remq 'use-package custom-enabled-themes)))
>                (custom-theme-set-faces
>                 'use-package
>                 ,@(mapcar
>                    #'(lambda (def)
>                        (let ((face (nth 0 def))
>                              (spec (nth 1 def))
>                              (comment (nth 2 def)))
>                          (unless (and comment (stringp comment))
>                            (setq comment (format "Customized with use-package %s" name)))
>                          `(backquote (,face ,spec nil ,comment))))
>                    args))))
>          (mapcar
>           #'(lambda (def)
>               (let ((face (nth 0 def))
>                     (spec (nth 1 def))
>                     (comment (nth 2 def)))
>                 (unless (and comment (stringp comment))
>                   (setq comment (format "Customized with use-package %s" name)))
>                 `(custom-set-faces (backquote (,face ,spec t ,comment)))))
>           args))
>        (use-package-process-keywords name rest state)))

This version has a bug: if a new theme is enabled (e.g., the user
switches themes at runtime), that new theme will take precedence over
this "use-package" theme. This differs from the behavior of
`custom-set-faces' which ALWAYS takes precedence.

If there's a way to use `custom-set-faces' with no custom themes, that
would be better.

I CAN fix (hack) this with a hook to reapply the theme as follows:

    (defun use-package--reapply-theme (theme)
      (unless (or (memq theme '(use-package user))
                  ;; prevents recursion
                  (memq 'use-package custom-enabled-themes))
        (enable-theme 'use-package)
        (setq custom-enabled-themes (remq 'use-package custom-enabled-themes))))
    (add-hook 'enable-theme-functions #'use-package--reapply-theme)

This mimic's the behavior of the "user" theme (the theme used by
`custom-set-faces') in `enable-theme' (reproduced below):

    (defun enable-theme (theme)
      ...
      (unless (eq theme 'user)
        (setq custom-enabled-themes
    	  (cons theme (remq theme custom-enabled-themes)))
        ;; Give the `user' theme the highest priority.
        (enable-theme 'user))
      ;; Allow callers to react to the enabling.
      (run-hook-with-args 'enable-theme-functions theme))

HOWEVER, it will cause the "user" theme to be applied twice (because
we're calling `enable-theme' from within `enable-theme'), which isn't
great.