1

I defined a custom face

(defface my-face '((t . (:background "green"))) "text")

that I want to use for font-lock syntax highlighting.

(defvar my-font-lock-keywords
 `(
   ("^\\*.*\\|;.*" . font-lock-comment-face)
   ("'[^']*'" . font-lock-string-face)
   ( ,(regexp-opt my-keywords 'symbols) . font-lock-keyword-face)
   ( ,(regexp-opt my-specials 'symbols) . my-face)
   (">[0-9A-F]+\\>\\|\\<[0-9]+\\>" . font-lock-constant-face)
))

The entire thing works fine if I replace my-face with font-lock-doc-face or similar, but loading my mode with this definition yields the error message

Error during redisplay: (jit-lock-function 1) signaled (void-variable my-face)

What is wrong with my face and the font-lock definition? Note that describe-face knows my-face and correctly shows its defined properties.

1 Answer 1

1

The "face" field is evaluted by font-lock. Hence, you need to quote the name of the face:

("^\\*.*\\|;.*" . 'font-lock-comment-face)

Note: Some standard faces like font-lock-keyword-face have variables with the same name as the face, whose value is the name of the face. This allows you to use those standard faces without a quote. However, this practice is deprecated.

Note 2: You can utilise the fact that the field is evaluated, e.g. by mapping a regexp to different faces depending on what is matched.

Note 3: Typically, strings and comments are not highlighted using font-lock keywords. Instead, they are highlighted according to the syntax table of the major mode.

2
  • 1
    Thanks for the answer, I was really going crazy! I do have a syntax table, but omitted it here for brevity.
    – larsb
    Nov 27, 2021 at 15:34
  • 1
    @larsb, good luck with the project you are working on. I've written some tools for helping me write font-lock packages, including a debugger that allows me to single-step matches and rules, so that I can see what happens in the buffer. It might be of use for you as well -- you can get it from github.com/Lindydancer/font-lock-studio. Nov 27, 2021 at 16:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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