Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Using M-x customize-face RET RET when the cursor is on a type name (this opens the customization of font-lock-type-face) in a c++ code, I managed to make the face of type names bold. Next, when I opened a .tex file, the face of the text of a caption was boldded as well... Seems like the two are sharing something (font-lock-type-face?) in common.

How can I set the font-lock-type-face to have one face when the mode is c++, and another face when the mode is LaTeX?

share|improve this question

2 Answers 2

Just change the hooks to whatever mode suits your needs, and, of course, you can change all of the font attributes. To set the default font for other modes, there is a function called my-add-to-multiple-hooks where additional mode hooks can be specified.

(add-hook 'c++-mode-hook (lambda ()
  (custom-set-faces
    '(font-lock-type-face ((t
      (:bold t :underline "black" :foreground "red" :background "yellow")))) )))

(add-hook 'LaTeX-mode-hook (lambda ()
  (custom-set-faces
    '(font-lock-type-face ((t
      (:slant italic :foreground "yellow" :background "black")))) )))

(defun my-add-to-multiple-hooks (function hooks)
  (mapc (lambda (hook)
          (add-hook hook function))
        hooks))

(defun default-font-lock-type-face ()
  (custom-set-faces
    '(font-lock-type-face ((t
      (:foreground "ForestGreen"))))))

(my-add-to-multiple-hooks
  'default-font-lock-type-face
    '(
        text-mode-hook
        c-mode-hook
        tex-mode-hook
    ))
share|improve this answer
1  
Not too great, IMHO. That leaves the face as last defined by one of those modes. Better would be to also return the face to its standard or user-saved definition when you leave each mode. And even better would be to get the source code changed so such modes do not just reuse font-lock-type-face for something unrelated to a "type" and instead use a mode-specific face (whose definition can inherit from font-lock-type-face, if that appearance is desired). –  Drew Sep 2 '13 at 18:35
    
Given the global nature of faces, I borrowed an idea from another thread and adopted it to the default face defined be the user: stackoverflow.com/questions/7398216/… –  lawlist Sep 2 '13 at 22:46
    
This is nice, but not perfect. If I have two buffers, one with TeX and one with C++ then font-lock-type-face is determined according to the last file type which was visited. I guess this is what @Drew meant. –  Dror Sep 3 '13 at 5:25
    
You are correct -- short of using a different face definition and defining it to trump, there is not much else that can be done except for hacking the source or trying to convince the Emacs team to change it. I frequently use the type of code referenced in the comment underneath Drew's answer to trump other face definitions. I only hack the actual Emacs source occasionally, and then I use a defalias for functions in my .emacs file so that I can update Emacs versions without losing my modifications. –  lawlist Sep 3 '13 at 6:00
    
Too bad. Can you provide me with a pointer where LaTeX-mode defines its face locks? Can you elaborate on this defalias approach? –  Dror Sep 3 '13 at 6:45

I don't have the code for tex mode with me now, but yes, it is possible, in fact all too common, IMHO, that the same font-lock face is reused for something quite different. If that is the case here, i.e., if the tex-mode code hard-codes the face it uses as the same face, then you are out of luck (modulo rewriting that code to use a different face).

To see what face is actually used, in your tex-mode buffer, put the cursor on the text in question and hit C-u C-x =. That will display lots of info about the position and character, and near the bottom it will tell you about any text properties, including property face, that are on that character. If it says font-lock-type-face then yes, the same face is used.

If that is not the case, so you see a different face name, then the problem is perhaps only that that face's definition inherits from font-lock-type-face. In that case, you're in luck: just customize that face, whatever it is.

In the out-of-luck case, consider also using M-x report-emacs-bug to suggest that Emacs Dev not simply reuse the same face, font-lock-type-face, but instead they create a tex mode-specific face for this. They can have that face inherit from font-lock-type-face if they want, but they should not just reuse that face. This is my opinion, not necessarily shared by Emacs Dev -- just letting you know.

share|improve this answer
    
Seems like I'm out of luck :) –  Dror Sep 2 '13 at 11:49
    
You can trump it with your own defvar | set-face-attribute | font-lock-add-keywords: stackoverflow.com/questions/17981738/… For example, I have colorized my own begin/end document in latex-mode to make it stand out better. –  lawlist Sep 2 '13 at 16:05
    
It should be pretty clear. If the same face is used in the code, you can either (a) change the code (or request that Emacs Dev do that) or (b) change the face appearance here and there, now and again, as @lawlist suggested. There is no miracle here. Faces are not mode-local or buffer-local. You might not like the answer but that's it. Take your pick. But first verify the guesses about the same face being used. If two different faces are used and it's just that one inherits from the other, then there is no problem --- just customize one or the other. –  Drew Sep 5 '13 at 4:33
    
As far as I can tell the same face is used. See my comment above. –  Dror Sep 6 '13 at 11:37
    
In that case, either change the code (or request that it be changed in Emacs) or follow @lawlist's suggestion. You say "as far as I can tell". Did you use C-u C-x = as I suggested? That will tell you exactly which faces are used -- no guessing needed. –  Drew Sep 6 '13 at 15:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.