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. |
Spaceand Delare handy screen-scrolling commands found in various read-only Emacs modes.
Note that M-nand M-pdo 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 lintC 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 compileon some code that you know contains an error; if you type C-x `, and Emacs claims that there are no more errors
, the next-errorfeature 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-alistthat fits your compiler's error message format. We'll show you an example of this in Chapter 11.
The compilepackage 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 grepwith the -noption, 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.
We have already seen various examples of Emacs modes, including text mode (see Chapter 2) and shell mode (see Chapter 5 Chapter 5. Emacs as a Work Environment Many of the everyday things you do from a command prompt can be done from within Emacs. You can execute commands, work with directories, and print files—all without leaving Emacs. Changing tasks is as simple as jumping between buffers. What's important about this? Of course, it's nice to be able to move between tasks easily. What's even more important is that you have the same editing environment no matter what you're doing: you can use all of the Emacs editing commands to work on a file, give shell commands, then start up Dired, the directory editor, to do some file maintenance. It is simple to move text from one window to another. You can execute a command and then use Emacs commands to cut and paste the results into a file. If you're trying to compile a program and keep getting error messages, you can save the interactive session as a file and confer with someone about the problem. Despite the many advantages of modern window systems, Emacs often provides the best way to integrate the many kinds of work you do daily. Much of the information in this chapter involves integration between Emacs and the operating system. Emacs is most commonly a Unix editor, so forgive us for a bias in that direction. But we are happy to report that for users of GNU Emacs on other platforms, integration with the operating system is still available; you can use shell mode to run commands and can edit directories with Dired. There's no reason to leave Emacs no matter what your platform is.
). Special functionality like the buffer list (see Chapter 4) and Dired (see Chapter 5 Chapter 5. Emacs as a Work Environment Many of the everyday things you do from a command prompt can be done from within Emacs. You can execute commands, work with directories, and print files—all without leaving Emacs. Changing tasks is as simple as jumping between buffers. What's important about this? Of course, it's nice to be able to move between tasks easily. What's even more important is that you have the same editing environment no matter what you're doing: you can use all of the Emacs editing commands to work on a file, give shell commands, then start up Dired, the directory editor, to do some file maintenance. It is simple to move text from one window to another. You can execute a command and then use Emacs commands to cut and paste the results into a file. If you're trying to compile a program and keep getting error messages, you can save the interactive session as a file and confer with someone about the problem. Despite the many advantages of modern window systems, Emacs often provides the best way to integrate the many kinds of work you do daily. Much of the information in this chapter involves integration between Emacs and the operating system. Emacs is most commonly a Unix editor, so forgive us for a bias in that direction. But we are happy to report that for users of GNU Emacs on other platforms, integration with the operating system is still available; you can use shell mode to run commands and can edit directories with Dired. There's no reason to leave Emacs no matter what your platform is.
) are actually modes as well. All modes have two basic components: an Emacs Lisp package that implements the mode and a function that invokes it.
The version of Emacs on which this book is based (21.3.5) comes with language modes for Ada, assembly, awk, C, C++, Common Lisp, Fortran, ICON, Java, Lisp, MIM, Modula-2, Objective-C, Pascal, Pike, Perl, PROLOG, Python, Scheme, SGML, Simula, and SQL; future versions will undoubtedly add more. Many—but not all—of the language modes are "hooked" into Emacs so that if you visit a file with the proper filename suffix, you will automatically be put in the correct mode. To find out whether Emacs does this for the language you use, look up your language in the table of Emacs Lisp packages in Appendix B. If one or more suffixes is listed in the right-hand column, Emacs invokes the mode for files with those suffixes.
Читать дальше