As many programmers know, the task of programming usually breaks down into a cycle of think-write-debug. If you have used Unix (or various other operating systems) for programming, you have probably become accustomed to using separate tools for each phase of the cycle, for example, a text editor for writing, a compiler for compiling, and the operating system itself for running programs. You would undoubtedly find an environment much more productive if the boundaries between the cycle phases—and the tools that support them—were erased.
Emacs provides considerable support for writing, running, and debugging programs written in a wide variety of languages, and it integrates this support into a smooth framework. You never have to leave Emacs when developing programs, so you will find it easier to concentrate on the actual programming task (i.e., the "think" part of the cycle) because you won't have to spend lots of time going from one tool to another.
When you write code, you can use one of Emacs's programming language modes; these turn Emacs into a spiffy syntax-directed or language-sensitive editor that knows about the syntax of the language. That makes it easier for you to write code in a uniform, easy-to-read, customizable style. Language modes exist for several different programming languages.
Emacs also supports running and debugging programs. Shell mode (see Chapter 5) and multiple windows (see Chapter 4) allow you to run your code while editing it. Emacs has a powerful facility for interfacing to many compilers and the Unix make command: Emacs can interpret compilers' error messages and visit files where errors occur at the appropriate line number. Indeed, many tools (such as the Java build tool, ant) include command-line options to format their output in an Emacs-friendly way.
In this chapter, we cover the features of language modes in general such as compiling and debugging programs, comments, indentation, and syntax highlighting. We also spend a bit of time upfront looking at the etags facility, which is a great help to programmers who work on large, multifile projects. These features apply to all language modes. We then delve into Emacs's support for various languages, including C, C++, Java, Perl, SQL, and Lisp.
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).
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:
(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.
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.
[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.
[2] Unfortunately, Emacs won't understand error messages generated by make itself, such as those due to syntax errors in your Makefile.
[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.