Emacs: the Free Software IDE

“Many people waited a long time to have nice IDEs for Linux. While everyone else waited I just used Emacs.” —Thanyne Harbaugh, President, Provo Linux Users Group
Compilation Debugging

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 ;.

Pretty Printing

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

Electric Comments and Auto-Fill Mode

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.

Version Control with Emacs

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

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.

______________________

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Emacs 22 reference card

Marc's picture

An Emacs 22 reference card - PDF generated from the refcard.tex TeX source file that comes in the Emacs distribution.

A GUD (GDB) tip

Marc's picture

You forgot to mention in regards to GUD (GDB) that C-x SPC will set a breakpoint on a given line.

White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState