0

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") )
Drew
  • 75,699
  • 9
  • 109
  • 225
Dilna
  • 1,173
  • 3
  • 10

2 Answers2

1
(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.

Drew
  • 75,699
  • 9
  • 109
  • 225
0

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.

Lindydancer
  • 6,095
  • 1
  • 13
  • 25