简体   繁体   中英

Emacs: font-lock explanation

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!

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.

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 Mx font-lock-mode a couple times to watch it switch on and off.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM