This is a follow-up question to Adding syntax highlighting for a comment section in Emacs.

I have the following face definition adapted from the definition of font-lock-warning-face in font-lock.el:

; (defface font-lock-warning-face
;   '((t :inherit error))
;   "Font Lock mode face used to highlight warnings."
;   :group 'font-lock-faces)
(defface my-code-section-face
  '((t :inherit font-lock-warning-face))
  "Face used for comment guards")

(defun add-c-syntax-highlighting ()
  (font-lock-add-keywords nil '(
    ("//\\-+" 0 (if t font-lock-warning-face
                  my-code-section-face) prepend))))

When I use t (font-lock-warning-face), the function works. When nil (to use my-code-section-face), it does not. I can only assume that it's a problem with how I'm defining the face, but I can't find an issue. (Using ` vs. ' makes no effective difference.)

  • In fact, I just noticed that when using nil, all font lock fails. – Sean Allred Oct 20 '14 at 15:29
  • @Downvoter, have the common courtesy to leave a suggestion as to how to make this question better. This is on-topic for this site. – Sean Allred Oct 20 '14 at 18:05
  • 1
    I could be wrong, but normally a single quote inhibits evaluation inside a list, whereas a backtick permits evaluation -- when using the backtick, elements inside the list that need to be evaluated normally are preceded with a comma. When using broad tags such as syntax-highlighting, you are bound to reach an audience unfamiliar with Emacs. – lawlist Oct 20 '14 at 21:55
  • @lawlist Good to know, thanks. I don't think that'd have an impact in this case though. – Sean Allred Oct 20 '14 at 21:56
  • @lawlist Well, I did include the [emacs] tag :( font-lock isn't one of them, as I recall – Sean Allred Oct 20 '14 at 21:57

Looks to me like you need to quote prepend: 'prepend, and move it outside the list:

(font-lock-add-keywords nil '(("//\\-+" 0 whatever)) 'prepend)

And for testing, at least, get rid of all the stuff in whatever - just use something like (0 'my-code-section-face t).

So something like this:

(font-lock-add-keywords nil '(("//\\-+" (0 'my-code-section-face t))) 'prepend)

And you can always see whether your defface works the way you want, by using M-x list-faces-display.

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.