I'm interested in making a major mode to display PGN files nicely. I'm trying to create my own faces since the defaults don't seem super-appropriate.

My mode definition looks like this:

(defface my-r-face `((t (:foreground "red")))  "Red highlight")

(setq pgn-highlights
      '(("Pi\\|Infinity" . font-lock-constant-face)
    ("Sin\\|Cos" . font-lock-string-face)
    ("[0-9]+\\." . font-lock-keyword-face)
    ;; ("%.*" . my-r-face) ;; This doesn't show up (the relevant text appears just like the default text
    ("%.*" . font-lock-comment-face) ;; works great - relevant text looks like the comment face

(define-derived-mode pgn-mode fundamental-mode "mymath"
  "major mode for editing mymath language code."
  (setq font-lock-defaults '(pgn-highlights)))

A typical file that I'd like to use this on is:

[Event "?"]
[Site "?"]
[Date "2019.6.3"]
[Round "?"]
[White "Mike"]
[Black "Mikey"]
[Result "*"]
[WhiteELO "?"]
[BlackELO "?"]

%Created by Caissa's Web PGN Editor
1. e4 e5 2. Nf3 Nc6 3. Bc4 Bc5 4. O-O Nf6 5. d3 d6 6. Nc3 a6 7. a3 Be6 8.
Bxe6 fxe6 9. h3 O-O 10. Be3 Ba7 11. Ra2 Qe8 12. Qc1 Nh5 13. Qd2 Nf4 14. g3
Nxh3+ 15. Kg2 Qh5 16. Rh1 Qxf3+ 17. Kh2 Nxf2 18. Rf1 Qh5+ 19. Kg2 Qf3+ 20.
Kg1 Qh1#

The issue is the line with the comment (%Created by...). If I use the built-in font-lock-comment-face then everything is highlighted nicely and the comment looks like the normal comment. But if I change it to use my-r-facethen the comment is not highlighted, and instead appears be be the default font face.

What am I doing wrong here?


Just add this, after the defface, to define a variable of the same name, whose value is the face symbol:

(defvar my-r-face 'my-r-face
  "Variable for face `my-r-face'.)

font-lock-defaults in the case of just (pgn-highlights) defines font-lock-keywords as the list that is the value of variable pgn-highlights.

The doc for font-lock-keywords tells you about this case, which is the one you are using:


In this kind of element, FACESPEC is an expression whose value specifies the face to use for highlighting. In the simplest case, FACESPEC is a Lisp variable (a symbol) whose value is a face name.

     ;; Highlight occurrences of ‘fubar’,
     ;; using the face which is the value of ‘fubar-face’.
     ("fubar" . fubar-face)

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.