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.
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).
Special Reports: DevOps
Have projects in development that need help? Have a great development operation in place that can ALWAYS be better? Regardless of where you are in your DevOps process, Linux Journal can help!
With deep focus on Collaborative Development, Continuous Testing and Release & Deployment, we offer here the DEFINITIVE DevOps for Dummies, a mobile Application Development Primer, advice & help from the experts, plus a host of other books, videos, podcasts and more. All free with a quick, one-time registration. Start browsing now...
- System Status as SMS Text Messages
- SUSE – “Will not diverge from its Open Source roots!”
- Dealing with Boundary Issues
- Vagrant Simplified
- Libreboot on an X60, Part I: the Setup
- Bluetooth Hacks
- Disney's Linux Light Bulbs (Not a "Luxo Jr." Reboot)
- New Products
- October 2015 Issue of Linux Journal: Raspberry Pi
- October 2015 Video Preview