I would like to adapt the emacs customisation for comment delimiter colour to change according to light or dark background. How is this customarily done?

(defun annot ()
  "Customisation for comments"

  (set-face-attribute 'font-lock-comment-face nil
              :weight 'normal :slant 'italic)

  (set-face-attribute 'font-lock-comment-delimiter-face nil
              :foreground "#00FF00") )

2 Answers 2

(if (eq 'light (frame-parameter nil 'background-mode))
    ;; Do what you like for a frame with a light background
  ;; Do what you like for a frame with a dark background

See the Elisp manual, node Font and Color Parameters.

  • emacs was quiet so tried elsewhere.
    – Dilna
    Feb 18, 2022 at 8:45
  • It cannot be deleted actually.
    – Dilna
    Feb 21, 2022 at 13:48

When you define a face, you can supply a face spec, which is a condition under which the attributes should be used. This allows Emacs to pick different face attributes in different contexts. For example, if you have one light and one dark frame open at the same time, or if you have a GUI emacs runnning and connect to it using a terminal.

The face spec can contain a lot of conditions, one of them is background, whose values an be light and dark. Others are somewhat obsolete like class which could be color, grayscale, and mono (even though if feels like it wasn't that long ago when, as a student, I was running Emacs on monochrome workstations).

You can use a face spec both with defface and with custom-theme-set-faces.

For example:

(defface my-test-face
  '((((background light))
     :foreground "red")
    (((background dark))
     :foreground "blue")
    (t :weight bold))
  "My test face.")

I would recommend you to define your own theme that could contain all your face definitions.

A side note: face definitions like this can be used by packages like e2ansi to determine what the face would have looked in other displays than the current.

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.