Sams Teach Yourself Emacs in 24 Hours

ContentsIndex

Hour 12: Visible Editing Utilities

Previous HourNext Hour

Sections in this Hour:

 

Highlighting Syntax Using Fonts and Colors


Today's monitors have the capability to show text in different colors, fonts, and shapes. Why not use this to make the text more readable?

Note - The font, the color, and the shape are, together, referred to as the face.


Using the face to help show the meaning of the text is what font-lock mode does. Using information built in to the major mode with which you edit a given text, font-lock mode knows enough about your text to show keywords in one face, comments in another, text literals in a third, and so on. An example of this can be seen in Figure 12.7. To make is easy for you to see it in a black-and-white printed book, I configured, for this figure, font-lock mode to show text literals in italic, keywords in bold, and comments in inverse video. In the default setup for font-lock-mode .ifferent colors would be used instead.

The colors used in GNU Emacs 20 can be configured by customizing the group called font-lock-highlighting-faces; in GNU Emacs 19 and in XEmacs, you have to stick with the default fonts chosen, because configuring in these setups is much more difficult than using customize.

Figure 12.7
Font-lock mode works with information built in to your major modes.

Starting Font-lock Mode

If you want font-lock mode enabled for all the major modes that know about font-lock and you are using GNU Emacs, simply insert the following line into your .emacs file:


(global-font-lock-mode)

If, on the other hand, you do not want it loaded for all modes or you use XEmacs, you have to explicitly enable it for each major mode, using a construct like the following:


(add-hook 'c-mode-hook 'turn-on-font-lock)

c-mode should be replaced with the major mode that you want to start font-lock mode for by default. Thus, if you want font-lock mode to be started by default in html-helper-mode, c-mode, and emacs-lisp-mode, insert the following into your .emacs file:


(add-hook 'html-helper-mode-hook 'turn-on-font-lock) 
(add-hook 'c-mode-hook 'turn-on-font-lock) 
(add-hook 'emacs-lisp-mode-hook 'turn-on-font-lock) 

Note - This method is described in detail in Hour 24, "Installing Emacs Add-Ons." It does require that the given major mode define a hook called major-mode-hook, but this should always be the case.

Font-lock mode can be turned on or off explicitly by pressing M-x and typing font-lock-mode.

Asking for Refontification

To make your buffer look nice, font-lock mode must do some pretty hard work. In most situations you will hardly notice, but if you have a very slow computer or a very large file, this might be a problem for you. To make everyone happy, font-lock mode offers different levels of fontification. What is fontified at a given level depends on the major mode. To find the truth, you have to either look in the Lisp file or try it out. By default, font-lock mode works in full-fontification-mode, but using the variable font-lock-maximum-decoration you can change this. To set font-lock mode to a low fontification level, insert the following into your .emacs file:


(setq font-lock-maximum-decoration 1)

Tip - A more advanced method for getting cheap font-lock can be obtained using the minor modes called fast-lock-mode and lazy-lock-mode. Use C-h f (describe-function) for a discussion of what they do.


Even with fast computers, font-lock mode can be slow given, for example, a file with the size of 100MB. Thus font-lock mode can be configured to not do fontifications when a new file is opened with a size above a given value. This value is configured using the variable font-lock-maximum-size. Its default value is 256KB, which should be enough in most situations.

If you, however, sometimes read a file into Emacs that's larger than this size, you can either set the value higher or force Emacs to fontify your buffer, using the function font-lock-fontify-buffer.

The fontification can sometimes be done wrong due to the fact that font-lock mode tries to be as lazy as possible (that is, whenever you insert a character, it does not fontify your whole buffer). In these situations, press M-g M-g (font-lock-fontify-block), which should fix it. In situations where this also fails, use the function font-lock-fontify-buffer.

Sams Teach Yourself Emacs in 24 Hours

ContentsIndex

Hour 12: Visible Editing Utilities

Previous HourNext Hour

Sections in this Hour: