Выбрать главу

As you might expect, you can set both the default and local values of such variables. When you set the value of a variable such as left-margin or case-fold-search with setq, you are actually setting the local value. The way to set default values is to use setq-default instead of setq, as in:

(setq-default left-margin 4)

Unfortunately, there is no general way to tell whether a variable has just one global value or has default and local values (except, of course, by looking at the Lisp code for the mode). Therefore the best strategy is to use a plain setq, unless you find from experience that a particular variable doesn't seem to take on the value you setq it to—in which case you should use setq-default. For example, if you put the line:

(setq case-fold-search nil)

in your .emacs file, you will find that Emacs still ignores case differences in search commands as if this variable were still t; instead, you should use setq-default.

10.6 Finding Emacs Lisp Packages

Emacs contains lots of Lisp code; in fact, as we will see in Chapter 11, the majority of Emacs' built-in functionality is written in Lisp. Emacs also comes with several extra Lisp packages (also known as libraries) that you can bring in (or load) to add more features. Lisp packages are being added to Emacs all the time, and sometimes your system administrator will add packages obtained from sources other than the Free Software Foundation.

Appendix B lists the most useful built-in Lisp packages, along with explanations of how to use them. You can also get information about which packages are available on your system by typing C-h p (for finder-by-keyword). Briefly, the built-in packages do the following kinds of things:

• Support programming in C, Lisp, Perl, Java, and several other languages (see Chapter 9).

• Support text processing with TEX, LATEX, XML, and HTML (see Chapter 8).

• Emulate other editors (vi, EDT, and Gosling Emacs).

• Interface to operating system utilities, such as the shell (see Chapter 5).

• Provide editing support functions, such as spell checking (see Chapter 3) and outline editing (see Chapter 7) as well as text sorting, command history editing, Emacs variable setting (see Appendix A), and much more.

• Play various games and provide other forms of amusement.

See Appendix B for more details.

10.7 Starting Modes via Auto-Mode Customization

The tables in Appendix B list several major modes that are automatically invoked when you visit a file whose name ends in the appropriate suffix. Look for "suffix" in the right-hand columns of the tables to see many of the associations between filename suffixes and major modes that Emacs sets up by default. These associations are contained in the special Emacs variable auto-mode-alist. auto-mode-alist is a list of pairs (regexp . mode), where regexp is a regular expression (see Chapter 3 and Chapter 11) and mode is the name of a function that invokes a major mode. When Emacs visits a file, it searches this list (from the beginning) for a regular expression that matches the file's suffix. If it finds one, it runs the associated mode function. Notice that any part of a file's name—not just its suffix—can actually be associated with a major mode.

You can add your own associations to auto-mode-alist, although the syntax is weird if you are not used to Lisp (see Chapter 11 for the gory details). If you are programming in the Ada language, and your Ada compiler expects files with suffix .ada, you can get Emacs to put your files in Ada mode whenever you visit them by putting the following line in your .emacs file:

(setq auto-mode-alist (cons '("\\.ada$" . ada-mode) auto-mode-alist))

Make sure you include the single quote after the term cons and the dot between "\\.ada$" and ada-mode. The notation '(x . y) is just Lisp syntax for "make x and y a pair." The string "\\.ada$" is a regular expression that means "anything with .ada at the end of it," that is, $ matches the end of the string (as opposed to the end of the line, which is what it matches during regular expression search and replace). The entire line of Lisp basically means "add the pair ("\\.ada$", 'ada-mode) to the front of the auto-mode-alist." Note that, because Emacs searches auto-mode-alist from the beginning and stops when it finds a match, you can use the above cons construct to override existing mode associations.[72]

As another example, let's say you save certain mail messages in files whose names begin with msg-, and you want to edit these files in text mode. Here is the way to do it:

(setq auto-mode-alist (cons '("^msg-" . text-mode) auto-mode-alist))

Notice that in this case we are matching the beginning, rather than the end, of the filename. The regular expression operator (^) means beginning of string, so the entire regular expression means "anything beginning with msg-."

Finally, if the name of a file you are editing does not match any of the regular expressions in auto-mode-alist, Emacs puts it into the mode whose name is the value of the variable default-major-mode. This mode is normally fundamental mode, a basic mode without special functionality. However, many people like to set their default mode to text mode, accomplished by adding a line like this to .emacs: (setq default-major-mode 'text-mode)

Although we have covered many useful ways to customize Emacs in this chapter, we have really only scratched the surface. To find out more, turn to Chapter 11 and find out about Lisp programming, the key to getting Emacs to do just about anything you want.

10.8 Making Emacs Work the Way You Think It Should

Emacs not only has per-user customizations; it can also have sitewide customizations. If Emacs isn't doing what you expect it to, you might want to try inhibiting any global customization file by starting Emacs with no customization.

You can do that by using one of these command-line options when you invoke Emacs.

• --no-init-file, -q load neither ~/.emacs nor default.el

• --no-site-file do not load site-start.el

If you normally start Emacs from an icon, it's helpful to learn how to start it from the command-line for cases like this. (You may also want to use the -debug option sometime to help you figure out what's wrong with your .emacs file if it is messed up following a change.) Chapter 13 describes how to start Emacs from the command-line for Mac OS X and Windows users.

вернуться

72

Lisp programmers will understand that there are other ways to add to auto-mode-alist, such as append.