: Learning GNU Emacs, 3rd Edition

10.7 Starting Modes via Auto-Mode Customization

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 namenot just its suffixcan 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.

: 0.728. /Cache: 3 / 1