Next: , Previous: Compilation, Up: Building

27.2 Compilation Mode

The *compilation* buffer uses a major mode called Compilation mode. Compilation mode turns each error message in the buffer into a hyperlink; you can move point to it and type <RET>, or click on it with the mouse (see Mouse References), to visit the locus of the error message in a separate window. The locus is the specific position in a file where that error occurred.

If you change the variable compilation-auto-jump-to-first-error to a non-nil value, Emacs automatically visits the locus of the first error message that appears in the *compilation* buffer.

Compilation mode provides the following additional commands. These commands can also be used in *grep* buffers, where the hyperlinks are search matches rather than error messages (see Grep Searching).

M-g M-n
M-g n
C-x `
Visit the locus of the next error message or match (next-error).
M-g M-p
M-g p
Visit the locus of the previous error message or match (previous-error).
M-n
Move point to the next error message or match, without visiting its locus (compilation-next-error).
M-p
Move point to the previous error message or match, without visiting its locus (compilation-previous-error).
M-}
Move point to the next error message or match occurring in a different file (compilation-next-file).
M-{
Move point to the previous error message or match occurring in a different file (compilation-previous-file).
C-c C-f
Toggle Next Error Follow minor mode, which makes cursor motion in the compilation buffer produce automatic source display.
g
Re-run the last command whose output is shown in the *compilation* buffer.

To visit errors sequentially, type C-x ` (next-error), or equivalently M-g M-n or M-g n. This command can be invoked from any buffer, not just a Compilation mode buffer. The first time you invoke it after a compilation, it visits the locus of the first error message. Each subsequent C-x ` visits the next error, in a similar fashion. If you visit a specific error with <RET> or a mouse click in the *compilation* buffer, subsequent C-x ` commands advance from there. When C-x ` finds no more error messages to visit, it signals an error. C-u C-x ` starts again from the beginning of the compilation buffer, and visits the first locus.

M-g M-p or M-g p (previous-error) iterates through errors in the opposite direction.

The next-error and previous-error commands don't just act on the errors or matches listed in *compilation* and *grep* buffers; they also know how to iterate through error or match lists produced by other commands, such as M-x occur (see Other Repeating Search). If the current buffer contains error messages or matches, these commands will iterate through them; otherwise, Emacs looks for a buffer containing error messages or matches amongst the windows of the selected frame, then for any buffer that next-error or previous-error previously visited, and finally all other buffers. Any buffer these commands iterate through that is not currently displayed in a window will be displayed.

By default, the next-error and previous-error commands skip less important messages. The variable compilation-skip-threshold controls this. The default value, 1, means to skip anything less important than a warning. A value of 2 means to skip anything less important than an error, while 0 means not to skip any messages.

When Emacs visits the locus of an error message, it momentarily highlights the relevant source line. The duration of this highlight is determined by the variable next-error-highlight.

If the *compilation* buffer is shown in a window with a left fringe (see Fringes), the locus-visiting commands put an arrow in the fringe, pointing to the current error message. If the window has no left fringe, such as on a text terminal, these commands scroll the window so that the current message is at the top of the window. If you change the variable compilation-context-lines to an integer value n, these commands scroll the window so that the current error message is n lines from the top, whether or not there is a fringe; the default value, nil, gives the behavior described above.

To parse messages from the compiler, Compilation mode uses the variable compilation-error-regexp-alist which lists various error message formats and tells Emacs how to extract the locus from each. A similar variable, grep-regexp-alist, tells Emacs how to parse output from a grep command (see Grep Searching).

Compilation mode also defines the keys <SPC> and <DEL> to scroll by screenfuls; M-n (compilation-next-error) and M-p (compilation-previous-error) to move to the next or previous error message; and M-{ (compilation-next-file) and M-} (compilation-previous-file) to move to the next or previous error message for a different source file.

You can type C-c C-f to toggle Next Error Follow mode. In this minor mode, ordinary cursor motion in the compilation buffer automatically updates the source buffer, i.e., moving the cursor over an error message causes the locus of that error to be displayed.

The features of Compilation mode are also available in a minor mode called Compilation Minor mode. This lets you parse error messages in any buffer, not just a normal compilation output buffer. Type M-x compilation-minor-mode to enable the minor mode. For instance, in an Rlogin buffer (see Remote Host), Compilation minor mode automatically accesses remote source files by FTP (see File Names).