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
Electric C

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.

Compiling and Testing

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

Spell Checking

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.

Debugging with Emacs

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.

______________________

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