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 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.
) 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 makecommand: 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 etagsfacility, 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).
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 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.
). 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 makeutility 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-commandin your .emacs file. For example, to use the Java build tool antas 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.
Читать дальше