Книга: Learning GNU Emacs, 3rd Edition

9.1.1 Compiling and Debugging

9.1.1 Compiling and Debugging

As mentioned at the beginning of this chapter, Emacs's support for programmers does not end when you are done writing the code. A typical strategy for using Emacs when working on a large programming project is to log in, go to the directory where your source files reside, and invoke Emacs on the source files (e.g., emacs Makefile myproj*.[ch] for C programmers). While you are editing your code, you can compile it using the commands described later—as you will see, you need not even worry about saving your changes. You can also test your compiled code in a shell using shell mode (see Chapter 5). The bottom line is that you should rarely—if ever—have to leave Emacs throughout your session.

Emacs provides an interface to compilers and the Unix make utility that is more direct and powerful than shell mode. At the heart of this facility is the command M-x compile Enter. This command causes a series of events to occur. First, it prompts you for a compilation command. The default command is make -k,[56] but if you type another command, that new command becomes the default for subsequent invocations during your Emacs session. You can change the default by setting the variable compile-command in your .emacs file. For example, to use the Java build tool ant as your default compile command, just add this line:

(setq 'compile-command "ant -emacs")

After you have typed the command, Emacs offers to save all unsaved file buffers, thus relieving you of the responsibility of making sure your changes have been saved. It then creates a buffer called *compilation* and an associated window. It runs the compilation command (as a subprocess, just like the shell in shell mode), with output going to the *compilation* buffer. While the command runs, the minibuffer says Compiling: run; it says exit when the compile job finishes.

Now the fun begins. If the compilation resulted in an error, you can type C-x ` (for next-error; this is a backquote, not a single quote). Emacs reads the first error message, figures out the file and line number of the error, and visits the file at that line number. After you have corrected the error, you can type C-x ` again to visit subsequent error locations. Each time you type C-x `, Emacs scrolls the *compilation* window so that the current error message appears at the top.

To start at the first error message again, type C-x ` with a prefix argument (i.e., C-u C-x `). A nice thing about C-x ` is that you can use it as soon as an error is encountered; you do not have to wait for the compilation to finish.

The mode of the *compilation* buffer (compilation mode) supports a few other useful commands for navigating through the error messages as summarized in Table 9-1.

Table 9-1. Compilation mode commands

Keystrokes Command name Action
C-x ` next-error Move to the next error message and visit the corresponding source code.
M-n compilation-next-error Move to the next error message.
M-p compilation-previous-error Move to the previous error message.
C-c C-c compilation-goto-error Visit the source code for the current error message.
Space scroll-down Scroll down one screen.
Del scroll-up Scroll up one screen.

Space and Del are handy screen-scrolling commands found in various read-only Emacs modes.

Note that M-n and M-p do not visit the source code corresponding to the error message; they simply allow you to move easily through error messages that may take up more than one line each. However, you can visit the source code from any error message by typing C-c C-c.

How does Emacs interpret the error message? It uses the variable compilation-error-regexp-alist, which is a list of regular expressions designed to match the error messages of a wide variety of C and C++ compilers and the lint C code checking program.[57] It should also work with compilers for languages for which Emacs has language modes, such as Java, Fortran, Ada, and Modula-2. Emacs tries to parse (analyze) an error message with each of the regular expressions in the list until it finds one that extracts the filename and line number where the error occurred.

There is a chance that the error message parser won't work with certain compilers, especially if you are using Emacs on a non-Unix system. You can find out by trying M-x compile on some code that you know contains an error; if you type C-x `, and Emacs claims that there are no more errors, the next-error feature does not work with your compiler.

If the parser doesn't work for you, you may want to try adding a regular expression to compilation-error-regexp-alist that fits your compiler's error message format. We'll show you an example of this in Chapter 11.

The compile package also includes similar support for the Unix grep (search files) command, thus effectively giving Emacs a multifile search capability. If you type M-x grep, you are prompted for arguments to send to grep—that is, a search pattern and filename(s). Emacs runs grep with the -n option, which tells it to print filenames and line numbers of matching lines.[58] The same happens as with M-x compile; you can type C-x ` to have Emacs visit the next matched line in its file.

Оглавление книги

Оглавление статьи/книги

Генерация: 0.911. Запросов К БД/Cache: 3 / 0
поделиться
Вверх Вниз