Emacs: font-lock explanation
Asked Answered
F

2

8

I'm using Emacs since a long time and I've customized it quite a lot. I've even written simple ELisp functions and now I'm starting to use macros more and more. I'm not anywhere near a knowledgable Emacs user but I'm not either a complete beginner.

I'm using nxhtml, mumamo, dired, ido, custom keybindings and so many things I added throughout the years that I don't remember all of them ; )

But there's something I really never understood: what is the "font-lock" mode and why should I care?

For example, I've read that nxhtml and nxml, if I recall correctly, do not use font-locking. Maybe I didn't understand that part well but in either way: what does it change for me, as a user?

Or take for example this description about WhiteSpace:

Note that when WhiteSpace is turned on, WhiteSpace saves the font-lock state, that is, if font-lock is on or off. And WhiteSpace restores the font-lock state when it is turned off. So, if WhiteSpace is turned on and font-lock is off, WhiteSpace also turns on the font-lock to highlight blanks, but the font-lock will be turned off when WhiteSpace is turned off. Thus, turn on font-lock before WhiteSpace is on, if you want that font-lock continues on after WhiteSpace is turned off.

OK, fine. I understand that. But what does it change if font-lock continues after or not?

Basically I just don't "get it", no matter how much I read about the subject.

Any example/explanation as to what "font-lock" is under Emacs and why it concerns me would be most welcome!

Foran answered 19/3, 2012 at 17:48 Comment(1)
font locking is everything related to the effects applied on characters to make them looking different than their regular appearance. The whitespace-mode description that you are quoting tells you that whitespace-mode does not violate the minor modes code of conduct, which states that minor modes should not disturb one another. Even though whitespace-mode needs to activate font locking to make spaces looking different, it remembers your previous font locking state to put everything back in place when you deactivate it.Chafe
C
10

font-lock-mode == colorful syntax highligthing. global-font-lock-mode enables font-locking for each and every mode in Emacs. Older Emacs version required you to enable it manually, newer version (23+ I think) enable this by default. I can't imagine that many people would want to turn off this feature, since it makes some type of content (like source code) immensely more readable. Every mode that uses font-lock relies on a certain set of faces - a combination of font properties like size, bold, underline, italic and color. The basic faces that most modes use are:

font-lock-builtin-face
font-lock-comment-face
font-lock-comment-delimiter-face
font-lock-constant-face
font-lock-doc-face
font-lock-doc-string-face
font-lock-function-name-face
font-lock-keyword-face
font-lock-negation-char-face
font-lock-preprocessor-face
font-lock-string-face
font-lock-type-face
font-lock-variable-name-face
font-lock-warning-face

You can easily override their values if you'd like them (or use a different color-theme). Some modes happen to define additional faces as well.

Candidacandidacy answered 20/3, 2012 at 10:1 Comment(0)
L
4

It displays your code with all the different colors (red for comments, green for class names, etc). If it's off, you get black-and-white code instead. Open up some code and execute M-x font-lock-mode a couple times to watch it switch on and off.

Larrigan answered 19/3, 2012 at 18:0 Comment(2)
ok but then how comes some mode like nxml or nxhtml say they're not using font-lock and yet display things in a lot of different colors?Foran
Is that what you're seeing? When I bring up an xml file in nxml mode, I can see the colors switch on and off when I toggle font-lock-mode.Larrigan

© 2022 - 2024 — McMap. All rights reserved.