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-theme-set-faces! '(doom-one doom-one-light)
`(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))"
`(let* ((themes (doom-enlist (or ,theme 'user)))
(fn (gensym (format "doom|customize-%s-" (mapconcat #'symbol-name themes "-")))))
(fset fn
(lambda ()
(dolist (theme themes)
(when (or (eq theme 'user)
(custom-theme-enabled-p theme))
(apply #'custom-theme-set-faces 'user
(cl-loop for (face . spec) in (list ,@specs)
if (keywordp (car spec))
collect `(,face ((t ,spec)))
else collect `(,face ,spec)))))))
(funcall fn)
(add-hook 'doom-load-theme-hook fn)))
(defmacro custom-set-faces! (&rest specs)
"Apply a list of face specs as user customizations.
SPECS is a list of face specs.
This is a drop-in replacement for `custom-set-face' that allows for a simplified
face format, e.g.
(custom-set-faces! (custom-set-faces!
(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 `(custom-theme-set-faces! 'user ,@specs))
(which-key-docstring-face :inherit 'font-lock-comment-face)))
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
(lambda ()
,@(let (forms)
(dolist (spec-group spec-groups)
(if (keywordp (cadr spec-group))
(cl-destructuring-bind (face . attrs) spec-group
(push `(set-face-attribute ,(if (symbolp face) `(quote ,face) face)
nil ,@attrs)
forms))
(let ((package (car spec-group))
(specs (cdr spec-group)))
(push `(after! ,package
,@(cl-loop for (face . attrs) in specs
collect `(set-face-attribute ,(if (symbolp face) `(quote ,face) face)
nil ,@attrs)))
forms))))
(nreverse forms))))
fn)
'append))
(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'."