10

I would like to change the syntax highlighting in my lua-mode.el without affecting other major modes.

Just as an example, I would like "key words" like if, then, else to be in bold and blue typeface when in lua-mode (instead of the default pink) without having the same highlighting style while editing a .tex file with AUCTeX.

So far I have tried to put the following code in my .emacs and then also in my lua-mode.el:

(custom-set-faces
  '(font-lock-builtin-face ((t (:foreground "maroon3"))))
  '(font-lock-comment-face ((t (:foreground "green4"))))
  '(font-lock-keyword-face ((t (:foreground "dark blue" :weight bold))))
  '(font-lock-string-face ((t (:foreground "dark cyan")))))

but this way I get the same syntax highlighting for every mode I work with.

This question might be related: Change syntax highlighting without changing major mode?

Is there a (hopefully simple and general) way to do this?

Pier Paolo
  • 275
  • 3
  • 10

1 Answers1

9

Faces are global so changing its attributes anywhere changes it everywhere, as you've noticed. To change it locally, make a copy of the face, change the attributes in the copy and then use a mode hook to locally set the old face to the copy on a per-buffer basis. The sample below does it for font-lock-comment-face, but the same incantation will work for any face.

(make-variable-buffer-local 'font-lock-comment-face)
(copy-face 'font-lock-comment-face 'lua-comment-face)
(set-face-foreground 'lua-comment-face "green4")

(add-hook 'lua-mode-hook
          (lambda ()
            (setq font-lock-comment-face 'lua-comment-face)            
            ))
erikstokes
  • 12,686
  • 2
  • 34
  • 56