eatonphil
eatonphil

Reputation: 13672

Add and reference new face in Emacs

I defined a new face in Emacs, but the coloring does not take effect. Here is the face and mode definition in ~/.emacs:

(defface sml-highlight-operator-face
  '((t (:foreground "red")))
  "SML operator highlighting"
  :group 'basic-faces)

(defvar sml-font-lock-keywords
   ((,(regexp-opt '("fun" "fn" "let" "val" "datatype" "type" "case" "of" "end" "structure" "struct" "signature" "sig"))
     (0 font-lock-keyword-face))
    ("[][=|><-+;,{}():]" (0 sml-highlight-operator-face))))

;;;###autoload
(define-derived-mode sml-mode prog-mode "SML"
  "SML major mode."
  (set (make-local-variable 'comment-start) "(* ")
  (set (make-local-variable 'comment-end) " *)")
  (set (make-local-variable 'font-lock-defaults)
       '(sml-font-lock-keywords)))

However, when I use font-lock-builtin-face instead of sml-highlight-operator-face those characters are highlighted (albeit with a color I don't want). What am I doing incorrectly?

Upvotes: 1

Views: 428

Answers (1)

Stefan
Stefan

Reputation: 28531

The element (0 sml-highlight-operator-face) in your font-lock-keywords does not say "use face sml-highlight-operator-face for sub-match 0" but "use the result of evaluating the expression sml-highlight-operator-face as the face to put on sub-match 0".

IOW, you need to use (0 'sml-highlight-operator-face).

BTW, the convention nowadays is to not use a -face suffix for faces (tho such a suffix is still used of course for variables holding faces), tho we haven't bothered to rename the font-lock-foo-face faces to font-lock-foo yet (even though it would very much help the confusion you're seeing where a lot of font-lock rules say things like (0 font-lock-foo-face) and people think it refers to the font-lock-foo-face face whereas it refers to the font-lock-foo-face variable (whose value holds the font-lock-foo-face face).

Upvotes: 5

Related Questions