9.1. Emacs as an IDE


Emacs provides a number of features that appeal to developers. You can edit code quickly with font support and auto-completion of function and variable names; you can compile the program and even run a debugger all without leaving your "editor." While you don't have some of the graphical tools commonly found in commercial integrated development environments (IDEs), almost every other feature of those IDEs can be found in Emacs for every language you could imagine working in.

Of course, there will always be occasions when you need to view your documents without the bells and whistles some language modes attach. You can always switch to plain text (M-x text-mode) or, more to the point, fundamental mode (M-x fundamental-mode).

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,[1] 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:

[1] The -k option overrides make's default of stopping after a job returns an error. Instead, make continues on branches of the dependency tree that do not depend on the branch where the error occurred.

(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.[2] 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.

[2] Unfortunately, Emacs won't understand error messages generated by make itself, such as those due to syntax errors in your Makefile.

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.[3] The same happens as with M-x compile; you can type C-x ` to have Emacs visit the next matched line in its file.

[3] If grep -n is run on only one file, it just prints line numbers; Emacs forces it to print the filename as well in this case by appending the dummy file /dev/null to the grep command.



Learning GNU Emacs
Learning GNU Emacs, Third Edition
ISBN: 0596006489
EAN: 2147483647
Year: 2003
Pages: 161

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net