5

I am trying to set up a major mode in Emacs where I would like to highlight certain keywords. Using a template from this page: http://ergoemacs.org/emacs/elisp_syntax_coloring.html I tried:

(setq testing-font-lock-keywords
  `((font-lock-keyword-face)
))

(define-derived-mode testing-mode fundamental-mode
  "testing file mode"
  "Major mode for editing test files"

  (setq font-lock-defaults '(testing-font-lock-keywords))
  (setq mode-name "testing")
)

(provide 'testing-mode)

If I use this mode on a simple test file, and type "hello" the text hello is marked in different color. That is, any text within double quotes is highlighted. Why is this happening?

I think it is related to the variable font-lock-keyword-face. But if I type C-h v and font-lock-keyword-face it says:

font-lock-keyword-face is a variable defined in `font-lock.el'.
Its value is font-lock-keyword-face

Update

It seems like it is not related to font-lock-keyword-face anyway, since defining testing-font-lock-keywords like:

(setq test-keywords '("TEST"))
(setq testing-font-lock-keywords
  `((,test-keywords)))

gives the same behavior.

Lindydancer
  • 25,428
  • 4
  • 49
  • 68
Håkon Hægland
  • 39,012
  • 21
  • 81
  • 174
  • looks like your `testing-font-lock-keywords` definition is broken; check the link you gave again and also `C-h v` `font-lock-defaults` to see the format it expects – assem Apr 02 '14 at 06:59
  • @assem See my updated question. So maybe the behavior is due to `font-lock-defaults`? – Håkon Hægland Apr 02 '14 at 07:44
  • 1
    If you are writing new font-lock keywords, why not try my new font-lock debugger [*font-lock-studio*](https://github.com/Lindydancer/font-lock-studio) – Lindydancer Apr 02 '14 at 08:27

2 Answers2

2

This is directed by variable `font-lock-syntactic-face-function'

Andreas Röhler
  • 4,804
  • 14
  • 18
2

Emacs fontifies two things: 1) Syntactic, this includes comments and strings as declared in the syntax table. 2) Keywords.

Typically, you want the first phase to run, but you might need to update your syntax table to match the syntax of the language.

In addition, font-lock keywords can be written so that they overwrite existing colors, so that you can highlight text inside pre-colored comments and string. See the OVERRIDE flag in font-lock-keywords.

Lindydancer
  • 25,428
  • 4
  • 49
  • 68