Emacs: the Free Software IDE
Emacs is well known as an editor, but calling Emacs an editor is like calling the Queen Mary a boat. The source RPM for Emacs 21.1 is some 20MB—huge for an editor. You don't have to use all of it, but sometimes it's nice to know it's there.
Emacs is heavily customizable, which makes it very flexible. For example, I am writing this article with Emacs and will spell check it with Emacs. “Well”, I hear you saying, “if it's good for writing articles, it can't be very good for developing software.” That's where Emacs' flexibility and customization come in; you can use it for either or for something completely different. Want to see a shrink, play Tetris or manipulate dates in the Mayan calendar? Emacs.
Emacs' customization comes in packages, called modes. Major modes set Emacs up to do a particular sort of thing, such as the C mode to edit C and C++ source or the psgml mode (www.lysator.liu.se/projects/about_psgml.html) to edit the documentation after you've written your program. Minor customizations provide added facilities to major modes. Show-paren mode shows matching parentheses, and auto-fill mode allows you to enter filled text without explicitly entering line breaks. Most modes have variables to control their behavior, and you can modify them temporarily or permanently.
Most users store their customizations in ~/.emacs, called the “dot Emacs” file. For the curious, mine is on the Web at w3.trib.com/~ccurley/emacs.init.html. Others can be found at “the very unofficial .emacs home”, www.dotemacs.de.
This article covers GNU Emacs version 21.1 (www.fsf.org/software/emacs/emacs.html). XEmacs (www.xemacs.org) is also available on most Linux distributions and will probably do everything I describe here.
What I will do to show off Emacs as an integrated development environment (IDE) is walk through the development of a short C program and show you how to do it. We'll write the infamous Hello World program known and loved by C programmers everywhere.
While this article shows C development, Emacs has support for other languages as well, including C++, assembly, Scheme, Java, Ada, IDL, Makefiles, Lisp (including Emacs Lisp, the language in which much of Emacs is written) and FORTRAN. And that's just what comes with Emacs. Add-on packages abound; there is even one for Forth.
In addition, you can use Emacs for many other types of files. When it comes time to document your program, you can use Emacs' psgml mode to make editing the documentation source much easier. There are modes for HTML and TeX as well.
The first thing we do is use Emacs' dired mode to create a directory to put the program in. In dired mode, we type a +, and then enter the name of the directory to create in the minibuffer at the bottom of the editor (Figure 1). Press Return and Emacs creates the directory for us.
Figure 1. Using Emacs' dired mode to create a directory for the program—we've shrunk the window down to save space in the magazine, but the essentials are there.
The next thing we do is enter that directory, then create the file hello.c with the usual keystrokes for visiting a file, Ctrl-X Ctrl-F. Type the filename, hello.c, into the minibuffer and press Return. We now see Emacs ready with a new buffer for our program.
The first thing any C programmer should do upon opening a new file is enter a comment to say what the file does. So we press M-; (M- for meta, or Alt on most PC keyboards) and get a C++-style comment delimiter and the cursor ready to take our comment. The reason I get a C++ comment is because I have customized Emacs to put me into C++ mode for C. You will probably get a C-style comment delimiter, /* */, with the cursor in the middle ready for you to type in your comment. If you'd rather use C++ comments, do M-X C++-mode.
Then we add a timestamp, all defined by Emacs' timestamp minor mode. Again, this is something I have customized; you may not get it to work.
A C program has to have a main function, and so we type it in GNU style: the return type on one line, followed by the function declaration on the next. Notice as we type away that syntax highlighting takes effect (Figure 2).
Figure 2. Entering the function declaration for main in Emacs. Note the syntax highlighting and also the parentheses matching.
Now for one of the really fun things about Emacs: Electric C. Press certain characters, and Emacs takes care of the indentation and the pretty printing for us. Having completed the function declaration, in a normal editor one would press Return, type a left brace ({), then press Return and Tab. Not in Emacs; just type a left brace and Emacs does the rest. (If it doesn't work the first time, undo the brace with Ctrl-_, then press Ctrl-C Ctrl-A to toggle electrification.)
Now we type in a line of code. Type in printf, then a space. Now it's time for a parenthesis. While we're at it, to keep our parentheses balanced, it's also time for a closed parentheses. So press M-(, that's Alt-( on a PC keyboard. Emacs puts in the pair of parentheses and moves back a character so we can type code inside. And, we type in "Hello, wolrd.\n", typo and all (we'll come back to that).
Instead of pressing Return, we enter another electric character, Ctrl-J. The astute reader has already noticed that we left out a semicolon. Watch what happens. Ctrl-J should move us down a line and indent to just below the “p” in “printf”. In this case, it doesn't. Emacs assumes that we are continuing from the line above, so it indents further. Even to a software engineer who has only had his first cup of coffee for the day, this should be an alert that something is wrong. Yes, indeed, we missed our semicolon. We undo the Ctrl-J with Ctrl-Shift-- (that's Control-Shift-Hyphen) and type in the semicolon. Hey! Semicolons are electric, too! But notice how Emacs prevented a bug from happening? That's even cooler!
Now, of course, it's time to enter a right brace in order to end the function main. Because our electric semicolon indented for us, do we have to backspace back to the left column? Nope, just type the right brace.
Now that we have our program written, we save it (Ctrl-X Ctrl-S) and run a test compile. Since this is a single-source file program, we don't need a make file. (But there is a make file mode, which I will leave as an exercise for the student.) Instead, we compile from within Emacs, M-X compile. This results in a proposed compile command line, in this case the default make -k. Since we don't have a make file, we use the down arrow key and write our own compile command:
gcc -g -o hello hello.c
The -g option compiles in information for GDB, the GNU Debugger, and the -o option tells the linker that the output file is to be named “hello”. And we get, much to our amazement, a perfect compile (Figure 3).
Figure 3. A Successful Compile in Emacs
Now to test it. Feeling brave—or lucky—we fire up a shell under Emacs with M-X shell and execute the program (Figure 4). However, something isn't quite right. We misspelled “world”. Oops.
Figure 4. Executing the Program from a Shell inside Emacs
But rather than just correct that error, we can spell check our comments and strings. To do that, we go back to the source buffer with Ctrl-X Ctrl-B Return. Now either use the pull-down menu (Tools—>Spell-Checking—>Spell-Check Comments) or do M-X ispell-comments-and-strings (now is a good time to see the Sidebar on Tab completion). We press the spacebar to bypass IDE and 0 to accept world instead of wolrd (Figure 5). Then Ctrl-X Ctrl-X to save our changes.
Figure 5. Spell Checking Source Code in Emacs
Now that we have that done, it's time to compile again. Like Bash, Emacs remembers our command history, so M-X followed by one or more up arrow keys gets us back to the compile command. Press Return. Emacs shows us our last compile command line. It looks good, so press Return again.
Emacs is also a front end for GDB. To enter GDB mode, do M-X gdb. Emacs comes up with a proposed command line, “gdb”. We add the name of the executable file to debug, hello, and press Return. This tosses us into gdb mode, with the gdb prompt ready for action. We set a breakpoint at the function “main” with the gdb command break main. (Tab completion works here, too.) Then type run to run the program to our first breakpoint.
GDB shows us the input values to the program. Emacs, however, has the other window set up to show the source. As we step through the program, Emacs will indicate the next line to execute with an arrow in the source window. We can even switch buffers (Ctrl-X Ctrl-O) and edit the source as the fancy strikes. Pressing N executes one statement. In this case, that statement produces output, which we see in the GDB window. One more N and GDB informs us that the program exited with a return value of, well, garbage.
We recall that the successful completion of a program should generally return zero. We should add that to the program. So we quit GDB and switch to the source-code window. The cursor is just in front of the final brace of the program. To add the return code, we enter return (0).
We won't get very fancy in our show-and-tell. But for anything more sophisticated than what we do here, you will find a lot of repetition before getting to the interesting part of the program execution. Therefore, you can prepare an init file for GDB, which is a list of GDB commands that are executed in the order GDB encounters them in the file. So you can automate anything GDB can do—which is quite a lot.
Being in a hurry, we neglected our semicolon and pressed Return instead of Ctrl-J. Try a compile. Do M-X and the up arrow keys until you have “compile” in the minibuffer, then press Return. The command line is good, so press Return again.
Oops! We have an error. To locate the error in the source, we press Ctrl-X `. This puts the first error message at the top of the compile output buffer and the cursor at the offending line. The error is before the brace, so we look at the end of the previous line. How terrible; we've committed the classic newbie programmer's error and left out the semicolon. So we press the left arrow key and add the semicolon. Then we save our work with Ctrl-X Ctrl-S.
Emacs' compilation mode is set up to detect error messages from a great many compilers (Microsoft Visual C++, for example) and not only C compilers. Emacs also recognizes the output from weblint, an HTML checker.
Notice that the electric semicolon did two things: it indented our line for us, which was nice of it, but it also inserted a line between the “return” statement and the closing brace, positioning the cursor ready to add another line. Well, that isn't what we wanted. So we undo it with M-Shift--. Now we override the electric semicolon and insert a semicolon with Ctrl-Q ;.
It would be nice to have the program all properly indented, so we use Emacs' indentation tools. First we select a region to indent. To select the entire program, Ctrl-X H. Then we indent with Ctrl-M-\ (that's Control meta-backslash). To look at the results, we hide the compilation buffer with Ctrl-X 1 and admire our handiwork (Figure 6). That done, we compile once more (M-X compile Return Return). This time we are successful.
Figure 6. Pretty Printing with Emacs
Of course, any programmer knows to comment their code copiously. First, we need a general comment to tell what the program does. So we enter that in the source file right after the timestamp (Figure 7). Start with Emacs' command to start a comment, M-;. Notice as you type that the text simply wraps around (as indicated by the arrows in the left and right margins). That's ugly, so we press M-Q to “fill” the paragraph. Notice that the C++ comment delimiters are inserted at the beginning of each line (Figure 8).
Figure 7. Entering a Comment (or Other Text) into Emacs
Figure 8. The Same Comment, Now “Filled”
We can arrange to fill text automatically with M-X auto-fill-mode, but this will fill our C source as well, which is not what we want. If you like, you can turn auto-fill mode on for comments and off again for source.
You can also comment a line of code. Put the cursor anywhere on the line, press M-; and continue. Emacs will put in the comment delimiter at the end of the line and move the cursor to the appropriate place to enter the comment. That will be the end of the line for C++-style comments, but between the delimiters for C-style comments, /* ... */.
And, don't forget to spell check your comments with M-X ispell-comments-and-strings.
Emacs has a front end for CVS, RCS or SCCS, called VC. The first two are free-software version control systems and come with many Linux distributions. VC is smart enough to figure out which one you are using. Most VC functions are handled with the key sequence Ctrl-X Ctrl-Q or Ctrl-X V V. Depending on the present state of the file, VC will check it in or out. If the file has never been added to the version control system, Emacs will determine that and check it in for you. When you check in a change, Emacs will make a temporary buffer for the change comment, so you have no excuse for ignoring that good programming habit.
This front end works with any document under version control. For example, the Linux Documentation Project uses remote CVS to control their linuxdoc and docbook source files. Even though I contribute over a 56KBps dial-up line, I can use Emacs to make the CVS process transparent.
Tags are a database of function, and optionally, typedef names are created with the program etags and stored in the file TAGS. They are stored together with their locations, making it easy to examine the definition of a function. To create a tags file for C programs, run etags -t *.[ch] from any shell. You can use tags for many programming languages; run etags --help from a shell for a list.
Our minimal program has only one file and only one function. Other developers should be so lucky; many C projects spread out across multiple files in multiple directories. Tags will cover complex situations like that as well. In fact, the more complex the project, the better tags shine.
To examine the source for a function, use M-. (that's meta-period). Start typing the name of the function and use tab completion. If you haven't already specified a tags file to use, Emacs will ask. Usually the default is exactly what you want (Figure 9).
Figure 9. Using the Tags Facility to Locate a Function
Notice that the default function name is the word next to the cursor in the source window. Tags mode lets you edit one file, place the cursor over the name of a function to examine and go edit that function with minimal keystrokes.
You can also display the source for the function in another window, with Ctrl-X 4 .. This lets you examine multiple functions simultaneously, as many functions as you can fit on the screen. If you want to see every reference to a function, use M-X tags-search. To continue the search, use M-,.
Also, tags are very useful for searching and replacing function names within projects. For example, if I have a function named Frodo and I want to rename it Gollum, I not only need to change the function declaration, but I have to get every prototype and every reference. So M-X tags-query-replace and away I go. Both of these search functions use regular expressions, making them very powerful. You can browse C++ classes with Ebrowse.
Diffing is comparing two files, say two versions of a source file. Most programmers are familiar with diff and patch. Emacs provides a powerful front end for diff and patch. For one thing, Emacs' ediff mode shows the two (or three) files in the editor one above the other. The differences are highlighted on any display capable of color (Figure 10). The entire line is indicated, and the exact differences are indicated in different colors.
Figure 10. Showing the Difference between Two Files
Not only that, but you can use single-character commands to move differences from one file to the other; a or b moves the line from the A or B buffer, respectively, to the other. This allows you to walk through two files, compare them and accept or reject each difference.
To step through the two files one difference at a time, put the ediff control panel in focus. Use the spacebar to advance and the P key to go back (Figure 11). Ediff mode can keep up with changes you make on the fly. Try adding a line to goodbye.c right after the printf line, a call to flush();.
Figure 11. The Control Panel for Emacs' Ediff Mode
Emacs has a huge library of built-in help. The gateway to Emacs' help is Ctrl-H. If you aren't familiar with Emacs at all, get started with the tutorial, Ctrl-H T. If you are already familiar with Emacs, you should walk through the tutorial; there's always something to learn about Emacs.
If you are familiar with the GNU program Info, you already know how to use Emacs' help system. Ctrl-H I gets you to the top menu of the Info system. From there, Memacs gets you to the Emacs documentation. And, of course, the documentation for Info is available from the top-level Info menu.
If you want documentation for other programs or for Linux function calls, you can use Emacs as an Info reader. Or, you can read the man pages from within Emacs. You can use Emacs as a front end for Man with the command M-X manual-entry. Or you can have Emacs interpret the man page and display it for you with the woman (WithOut MAN) package. Since the Cygwin tools for Windows (www.cygwin.com) include man pages but not a program to read them, woman is just the ticket.
For example, to see the man page for printf, put the cursor over the word printf in your source code. Enter M-X woman and press Return. Emacs will propose printf as the default manual entry. There are two printf man pages, one in man 3 and one in man 1. Tell Emacs you want man 3 by pressing 3 Tab. Emacs will complete the filename, then press Return. Emacs will show you the man page for printf.
Emacs is a fully integrated development environment. While a front end for external programs, Emacs uses free software for the back end. In that sense, it is better integrated into Linux than some proprietary IDEs. There is very little missing from Emacs, but if something is missing, Emacs is open-source and free software. You can write it.
email: ccurley@trib.com
Charles Curley (w3.trib.com/~ccurley) lives in Wyoming. He has 23 years' experience with computers, much of that as a software developer. He has worked for Hughes Aircraft, Hewlett-Packard, Microsoft and the Jet Propulsion Lab. He contributed to Sams' Teach Yourself Emacs in 24 Hours (ISBN: 0-672-31594-7).