Emacs: the Free Software IDE
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.
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).
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).
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.
Fast/Flexible Linux OS Recovery
On Demand Now
In this live one-hour webinar, learn how to enhance your existing backup strategies for complete disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible full-system recovery solution for UNIX and Linux systems.
Join Linux Journal's Shawn Powers and David Huffman, President/CEO, Storix, Inc.
Free to Linux Journal readers.Register Now!
|Working with Command Arguments||May 28, 2016|
|Secure Desktops with Qubes: Installation||May 28, 2016|
|CentOS 6.8 Released||May 27, 2016|
|Secure Desktops with Qubes: Introduction||May 27, 2016|
|Chris Birchall's Re-Engineering Legacy Software (Manning Publications)||May 26, 2016|
|ServersCheck's Thermal Imaging Camera Sensor||May 25, 2016|
- Tips for Optimizing Linux Memory Usage
- Working with Command Arguments
- Secure Desktops with Qubes: Introduction
- Secure Desktops with Qubes: Installation
- Download "Linux Management with Red Hat Satellite: Measuring Business Impact and ROI"
- CentOS 6.8 Released
- The Italian Army Switches to LibreOffice
- Linux Mint 18
- Oracle vs. Google: Round 2
- Chris Birchall's Re-Engineering Legacy Software (Manning Publications)
Until recently, IBM’s Power Platform was looked upon as being the system that hosted IBM’s flavor of UNIX and proprietary operating system called IBM i. These servers often are found in medium-size businesses running ERP, CRM and financials for on-premise customers. By enabling the Power platform to run the Linux OS, IBM now has positioned Power to be the platform of choice for those already running Linux that are facing scalability issues, especially customers looking at analytics, big data or cloud computing.
￼Running Linux on IBM’s Power hardware offers some obvious benefits, including improved processing speed and memory bandwidth, inherent security, and simpler deployment and management. But if you look beyond the impressive architecture, you’ll also find an open ecosystem that has given rise to a strong, innovative community, as well as an inventory of system and network management applications that really help leverage the benefits offered by running Linux on Power.Get the Guide