Next: , Previous: Compilation, Up: Building


32.2 Compilation Mode

The ‘*compilation*’ buffer uses a special major mode, Compilation mode, whose main feature is to provide a convenient way to look at the source line where the error happened.

M-g M-n
M-g n
C-x `
Visit the locus of the next compiler error message or grep match.
M-g M-p
M-g p
Visit the locus of the previous compiler error message or grep match.
<RET>
Visit the locus of the error message that point is on. This command is used in the compilation buffer.
Mouse-2
Visit the locus of the error message that you click on.
M-n
Find and highlight the locus of the next error message, without selecting the source buffer.
M-p
Find and highlight the locus of the previous error message, without selecting the source buffer.
M-}
Move point to the next error for a different file than the current one.
M-{
Move point to the previous error for a different file than the current one.
C-c C-f
Toggle Next Error Follow minor mode, which makes cursor motion in the compilation buffer produce automatic source display.

You can visit the source for any particular error message by moving point in the ‘*compilation*’ buffer to that error message and typing <RET> (compile-goto-error). Alternatively, you can click Mouse-2 on the error message; you need not switch to the ‘*compilation*’ buffer first.

To parse the compiler error messages sequentially, type C-x ` (next-error). The character following the C-x is the backquote or “grave accent,” not the single-quote. This command is available in all buffers, not just in ‘*compilation*’; it displays the next error message at the top of one window and source location of the error in another window. It also momentarily highlights the relevant source line. You can change the behavior of this highlighting with the variable next-error-highlight.

The first time C-x ` is used after the start of a compilation, it moves to the first error's location. Subsequent uses of C-x ` advance down to subsequent errors. If you visit a specific error message with <RET> or Mouse-2, subsequent C-x ` commands advance from there. When C-x ` gets to the end of the buffer and finds no more error messages to visit, it fails and signals an Emacs error.

By default, C-x ` skips less important messages. The variable compilation-skip-threshold controls this. If its value is 2, C-x ` skips anything less than error, 1 skips anything less than warning, and 0 doesn't skip any messages.

When the left fringe is displayed, an arrow points to the current message in the compilation buffer. The variable compilation-context-lines controls the number of lines of leading context in the window before the current message. If it is nil and the left fringe is displayed, the window doesn't scroll. If there is no left fringe, no arrow is displayed and a value of nil means display the message at the top of the window.

If you're not in the compilation buffer when you run next-error, Emacs will look for a buffer that contains error messages. First, it looks for one displayed in the selected frame, then for one that previously had next-error called on it, and then at the current buffer. Finally, Emacs looks at all the remaining buffers. next-error signals an error if it can't find any such buffer.

C-u C-x ` starts scanning from the beginning of the compilation buffer. This is one way to process the same set of errors again.

To parse messages from the compiler, Compilation mode uses the variable compilation-error-regexp-alist which lists various formats of error messages and tells Emacs how to extract the source file and the line number from the text of a message. If your compiler isn't supported, you can tailor Compilation mode to it by adding elements to that list. A similar variable grep-regexp-alist tells Emacs how to parse output of a grep command.

Compilation mode also redefines the keys <SPC> and <DEL> to scroll by screenfuls, and M-n (compilation-next-error) and M-p (compilation-previous-error) to move to the next or previous error message. You can also use M-{ (compilation-next-file and M-} (compilation-previous-file) to move up or down to an 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. For instance, moving the cursor to the next error message causes the location of that error to be displayed immediately.

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. This defines the keys <RET> and Mouse-2, as in the Compilation major mode.

Compilation minor mode works in any buffer, as long as the contents are in a format that it understands. In an Rlogin buffer (see Remote Host), Compilation minor mode automatically accesses remote source files by FTP (see File Names).