Rewrite custom-set-face!, add custom-theme-set-face!

custom-set-faces! and custom-theme-set-faces! are now drop-in
replacements for custom-set-faces and custom-theme-set-faces with one
major distinction: the latter will wait for the theme to be loaded
before applying the changes, this allows you to use theme-specific APIs
in your face definitions (like doom-color from doom-themes).

You no longer have to think about load order when using these macros.
This commit is contained in:
Henrik Lissner 2019-07-06 12:29:33 +02:00
parent 3b17d767b8
commit 0f0a8a5744
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
2 changed files with 42 additions and 41 deletions

View file

@ -471,48 +471,50 @@ If NOERROR is non-nil, don't throw an error if the file doesn't exist."
(cdr err)) (cdr err))
e))))))) e)))))))
(defmacro custom-set-faces! (&rest spec-groups) (defmacro custom-theme-set-faces! (theme &rest specs)
"Convenience macro for additively setting face attributes. "Apply a list of face specs as user customizations for THEME.
SPEC-GROUPS is a list of either face specs, or alists mapping a package name to THEME can be a single symbol or list thereof. If nil, apply these settings to
a list of face specs. e.g. all themes. It will apply to all themes once they are loaded.
(custom-set-faces! (custom-theme-set-faces! '(doom-one doom-one-light)
(mode-line :foreground (doom-color 'blue)) `(mode-line :foreground ,(doom-color 'blue))
(mode-line-buffer-id :foreground (doom-color 'fg) :background \"#000000\") `(mode-line-buffer-id :foreground ,(doom-color 'fg) :background \"#000000\")
(mode-line-success-highlight :background (doom-color 'green)) '(mode-line-success-highlight :background \"#00FF00\")
(org '(org-tag :background \"#4499FF\")
(org-tag :background \"#4499FF\") '(org-ellipsis :inherit org-tag)
(org-ellipsis :inherit 'org-tag)) '(which-key-docstring-face :inherit font-lock-comment-face))"
(which-key `(let* ((themes (doom-enlist (or ,theme 'user)))
(which-key-docstring-face :inherit 'font-lock-comment-face))) (fn (gensym (format "doom|customize-%s-" (mapconcat #'symbol-name themes "-")))))
Each face spec must be in the format of (FACE-NAME [:ATTRIBUTE VALUE]...).
Unlike `custom-set-faces', which destructively changes a face's spec, this one
adjusts pre-existing ones."
`(add-hook
'doom-customize-theme-hook
(let ((fn (make-symbol "doom|init-custom-faces")))
(fset fn (fset fn
(lambda () (lambda ()
,@(let (forms) (dolist (theme themes)
(dolist (spec-group spec-groups) (when (or (eq theme 'user)
(if (keywordp (cadr spec-group)) (custom-theme-enabled-p theme))
(cl-destructuring-bind (face . attrs) spec-group (apply #'custom-theme-set-faces 'user
(push `(set-face-attribute ,(if (symbolp face) `(quote ,face) face) (cl-loop for (face . spec) in (list ,@specs)
nil ,@attrs) if (keywordp (car spec))
forms)) collect `(,face ((t ,spec)))
(let ((package (car spec-group)) else collect `(,face ,spec)))))))
(specs (cdr spec-group))) (funcall fn)
(push `(after! ,package (add-hook 'doom-load-theme-hook fn)))
,@(cl-loop for (face . attrs) in specs
collect `(set-face-attribute ,(if (symbolp face) `(quote ,face) face) (defmacro custom-set-faces! (&rest specs)
nil ,@attrs))) "Apply a list of face specs as user customizations.
forms))))
(nreverse forms)))) SPECS is a list of face specs.
fn)
'append)) This is a drop-in replacement for `custom-set-face' that allows for a simplified
face format, e.g.
(custom-set-faces!
`(mode-line :foreground ,(doom-color 'blue))
`(mode-line-buffer-id :foreground ,(doom-color 'fg) :background \"#000000\")
'(mode-line-success-highlight :background \"#00FF00\")
'(org-tag :background \"#4499FF\")
'(org-ellipsis :inherit org-tag)
'(which-key-docstring-face :inherit font-lock-comment-face))"
`(custom-theme-set-faces! 'user ,@specs))
(provide 'core-lib) (provide 'core-lib)
;;; core-lib.el ends here ;;; core-lib.el ends here

View file

@ -130,8 +130,7 @@ behavior). Do not set this directly, this is let-bound in `doom|init-theme'.")
"Set up `doom-load-theme-hook' to run after `load-theme' is called." "Set up `doom-load-theme-hook' to run after `load-theme' is called."
(unless no-enable (unless no-enable
(setq doom-theme theme) (setq doom-theme theme)
(run-hooks 'doom-customize-theme-hook (run-hooks 'doom-load-theme-hook)))
'doom-load-theme-hook)))
(defun doom|protect-fallback-buffer () (defun doom|protect-fallback-buffer ()
"Don't kill the scratch buffer. Meant for `kill-buffer-query-functions'." "Don't kill the scratch buffer. Meant for `kill-buffer-query-functions'."