You save your .emacs just as you normally save any file. To test any changes you've made, though, you'll have to do one of two things. The sure-fire method is to quit Emacs and launch it again. If everything comes up the way you expected, you're good to go.
You can also run M-x load-file. You'll be prompted for the name of the file. Just type in ~/.emacs Enterand you should be able to check your changes.
Tip
Be careful here: it's entirely possible that something in your current session will interact with your new .emacs file. For example, if you have already set a default value for a variable, commenting out that line of your .emacs file will not remove the value unless you also remove the default value by hand. If you've got a fairly simple configuration, though, you should be fine. Reloading .emacs is certainly faster that restarting Emacs!
Either way, once you have verified that your configuration works the way you want, you can forget about this file. Until you want to make more changes, of course!
10.3 Modifying Fonts and Colors
Emacs on certain platforms (Windows, Mac OS X, and Unix) can display text in multiple fixed-width fonts. It doesn't yet handle proportional-spacing fonts well, although future releases are expected to address that issue. Emacs can display text in as many combinations of foreground and background colors as your system supports. We'll take a look at your options for changing fonts. You can make quick, interactive changes in any buffer. You can also customize the fonts and colors used by automatic highlight features such as Isearch and font-lock mode.
And just in case you want to use Emacs to edit rudimentary styled-text documents, we'll also look at how to save and load files that have font and color enriched text.
10.3.1 Changing Fonts Interactively
Both Custom and the Edit menu in Emacs provide you with a way to change the current font and color by picking a new one from the Text Properties menu.
To understand the Text Properties menu, you'll find it useful to know that Emacs thinks internally in terms of faces . A face is a font and color combination. The Text Properties menu presents you with a small set of premixed faces and the option to specify others by name.
We'll go into more detail about faces, how to name them, and the related Lisp programming constructs later in this chapter. For now, consider simply that every character in a buffer may have a different face invisibly associated with it (though in practice it would be quite surprising if face changes were that frequent!).
Holding down the Shiftkey while clicking the left mouse button takes you to a menu of fonts. Selecting one of these instantly changes the Emacs font for the current frame and redisplays the frame. This is an easy way to experiment with different fonts to see how well they trade screen space for readability on your display.
10.3.2 Automatic Highlighting and Coloring
A number of modules in Emacs feature text highlighting and syntax coloring. The various programming and markup language modes (Lisp mode, Java mode, HTML mode, and so on) have such highlighting. How you customize those fonts and colors depends heavily on the individual module.
The Isearch facility in Emacs has undergone a few changes as it has matured. It uses font faces and coloring to highlight a document when you search for words or expressions. You may find the default choices a bit, well, stark. You can customize the group by typing M-x customize-groupEnter isearch-facesEnter to change them.
Incidentally, you might just try changing the face it uses to highlight the secondary matches, so that it's less intrusive.
10.3.2.2 Buffer highlighting
The easiest way to use fonts and colors is to load the Lisp package font-lock.el (included with the Emacs distribution). This mode tries to highlight interesting features of your text buffers using color and different faces. As an example, try picking out comments in C and Lisp buffers, and painting them in a color that contrasts with the basic black of the code.
;; Turn on font lock mode every time Emacs initializes a buffer
;; for Lisp or C.
;;
(add-hook 'emacs-lisp-mode-hook 'turn-on-font-lock)
(add-hook 'c-mode-hook 'turn-on-font-lock)
Font-lock mode tends to be especially helpful for colorizing programming language code or outline mode text but also gives useful results for HTML files and Dired buffers. In fact, we find it useful in so you may want to turn it on globally instead, as we did in "A Sample .emacs file" earlier in this chapter. If you want more examples using font-lock mode, refer back to Chapter 9on some of the various programming language modes supported by Emacs.
10.3.3 Customizing Fonts Through Custom
Now that you know how to work with Custom, you can also go that route to edit and alter fonts and colors. The easy way to get started in Custom is to run M-x customize-groupand enter facesfor the group name. ( Figure 10-10shows a sample of the groups you'll see.)
Figure 10-10. Font face groups available in Custom (Mac OS X)
But what if you just want to change the default foreground and background colors? Well, that turns out to be quite simple. You can use the M-x set-foreground-colorand M-x set-background-colorcommands to pick simple colors (based on their names such as black, white, yellow, blue, red, etc.). Be careful, though, because Emacs has no qualms about letting you set these values to garish—or even impossible—combinations! While black text on a black background may provide some level of security from anyone peeking over your shoulder, it's not the most productive combination in the long run.
To see the range of colors available, run M-x set-foreground-color. When it prompts you for a color, just press Tab to get a completion list of the possible colors—you should get quite a few! These names can also be typed into the foreground and background fields (or any other color-based field) in Custom.
You can also use Custom to control all aspects (including the foreground and background colors) of the "default" font. Figure 10-11shows the Custom screen for just that font after switching the colors to green and black.
Figure 10-11. Changes to the default font colors effectively set the foreground and background colors for Emacs (Mac OS X)
You can go through the usual channels discussed previously to customize this face, or come here directly with M-x customize-faceand then enter defaultat the prompt.
10.3.4.1 Changing the cursor color
Don't forget about the cursor! You can also use set-cursor-colorto change the color of the cursor. That can be especially useful if you want a black background—the default black cursor can easily get lost.
10.3.5 Saving Font- and Color-Enriched Text
The astute reader will have noticed that, although the highlighting machinery allows us to set up enriched text in a buffer, we haven't shown a way to save text properties along with text between sessions. This is a significant issue. As long as there is no way to save properties along with text, all the font and color machinery remains little more than a display hack, good for decorating buffers but adding little to Emacs's editing power.
Читать дальше