10

The following Emacs Lisp you can set the background color of face whitespace-trailing:

(set-face-attribute 'whitespace-trailing nil :background "#ff0000" :foreground nil))

How can a face attribute be read back?


Asking since I'd like to temporarily change a face background color:
https://emacs.stackexchange.com/a/29166/2418

1 Answer 1

14

You can use face-attribute.

To match the example given, the following expression gets the trailing space background color:

(face-attribute 'whitespace-trailing :background)

face-attribute is a compiled Lisp function in `faces.el'.

(face-attribute FACE ATTRIBUTE &optional FRAME INHERIT)

:around advice: `ad-Advice-face-attribute'

Return the value of FACE's ATTRIBUTE on FRAME. If the optional argument FRAME is given, report on face FACE in that frame. If FRAME is t, report on the defaults for face FACE (for new frames). If FRAME is omitted or nil, use the selected frame.

If INHERIT is nil, only attributes directly defined by FACE are considered, so the return value may be unspecified, or a relative value. If INHERIT is non-nil, FACE's definition of ATTRIBUTE is merged with the faces specified by its :inherit attribute; however the return value may still be unspecified or relative. If INHERIT is a face or a list of faces, then the result is further merged with that face (or faces), until it becomes specified and absolute.

To ensure that the return value is always specified and absolute, use a value of default for INHERIT; this will resolve any unspecified or relative values by merging with the default face (which is always completely specified).

5
  • this does not return themed face-attributes. how can I do that?
    – Gangula
    Commented Jun 20 at 7:53
  • @Gangula, What do you mean? I tried it with one of the faces I had defined in a theme and it returned the correct value. Anyway, if you need to dig deeper into faces you could use the github.com/Lindydancer/face-explorer library. It can find the attributes a face would have under any circumstance (like, what would it look like on a 16 bit TTY). (Reservation: I'm the author of that package.) Commented Jun 21 at 8:38
  • I'm using doom-themes in my config and it has custom face attributes for all org headers. I tried to get the foreground color of my org-level-1 header using (face-attribute 'org-level-1 :foreground), but it returned unspecified. And when I check the face using M-x customize-face RET org-level-1 I see that the foreground is inherited.
    – Gangula
    Commented Jun 21 at 8:47
  • @Gangula -- have you passed in a non-nil value for the INHERIT argument? (See the last paragraph in the text I cited in the answer.) Commented yesterday
  • okay yes, that was missing, thank you. now I got the correct result using (face-attribute 'org-level-1 :foreground nil t). check out the documentation
    – Gangula
    Commented yesterday

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.