4

I am trying to set for example font-lock-comment-face to Blue for csharp-mode and for c++-mode to Red is this possible or not?

Right now im using:

(set-face-attribute 'font-lock-comment-face nil :foreground "#57a64a")
(set-face-attribute 'font-lock-keyword-face nil :foreground "#569cd6")

but this sets the value globally and not only for the mode.

Forgot to add the version im using: GNU Emacs 24.4.1 (i686-pc-mingw32) of 2014-10-24 on LEG570 on Windows 8

Julius
  • 192
  • 1
  • 9

1 Answers1

1

Wow! Thank you, I've thought it's not possible, but then i've found this: http://www.emacswiki.org/emacs/FacesPerBuffer

Just look at the example from wiki, seems like exactly what you need:

 (make-face 'php-comment-face)
 (set-face-foreground 'php-comment-face "LightGrey")
 (add-hook 'php-mode-hook 
       (lambda ()
        ;; ...
        (set (make-local-variable 'font-lock-comment-face)
             'php-comment-face)
        ;; ...

Thanks to this question from related: Set Emacs defaut font face per-buffer/mode

UPD

to win the cc-mode bindings, you should put the (add-hook csharp-mode-hook ... after (add-hook c-mode-hook ..., like this:

(make-face 'c-comment-face)
(set-face-foreground 'c-comment-face "Red")

(add-hook 'c-mode-hook
       (lambda ()
        ;; ...
        (set (make-local-variable 'font-lock-comment-face)
             'c-comment-face)))


(make-face 'cs-comment-face)
(set-face-foreground 'cs-comment-face "Blue")

(add-hook 'csharp-mode-hook
       (lambda ()
        ;; ...
        (set (make-local-variable 'font-lock-comment-face)
             'cs-comment-face)))

If you have hook codes in separate files, you should load csharp-mode settings after c-mode. Don't forget to (remove-hook ... to try this out.

Community
  • 1
  • 1
zarkone
  • 1,335
  • 10
  • 16
  • 1
    doesnt seem to work. ive used this line (set (make-local-variable 'font-lock-comment-face) 'c-comment-face) in my c-mode-hook but in the csharp file the comments were also red. – Julius Jan 05 '15 at 18:12
  • as for me, everything works fine. I bet this is because "It’s based on cc-mode" (https://github.com/josteink/csharp-mode) – zarkone Jan 05 '15 at 18:22
  • i have a splitted window, on one side the c file and on the other side a csharp file both opend at the same time. the mode loaded recently sets the color for the comment – Julius Jan 05 '15 at 18:58
  • hm, may be i do not understand what you need.. look at this pic, is this look like what you want to get? http://oi59.tinypic.com/xglfme.jpg – zarkone Jan 05 '15 at 19:09
  • yea exactly that left side and right side with different colors for lets say a comment – Julius Jan 05 '15 at 20:24
  • so, this what i've got after evaluated the code from scratch buffer in that order, as i said earlier. May be you should clean hooks or simply restart emacs and re-eval this code – zarkone Jan 05 '15 at 20:31
  • i dont know why it didnt work the first time but now it works cheers :) – Julius Jan 05 '15 at 21:37