Sams Teach Yourself Emacs in 24 Hours

ContentsIndex

Hour 18: Editing C, C++, and Java Files

Previous HourNext Hour

Sections in this Hour:

 

Hour 18
Editing C, C++, and Java Files

This hour assumes a good working knowledge of Java, C, C++, or Objective C. Working with a programming language such as Java or C in Emacs is like editing any other type of text file. The advantage is that for each language, there is a specialized programming mode which provides easy access to the following:

Java, C, C++, and Objective C, while different in many ways, have the same keybindings and language feature support. For convenience in this hour, generic features are discussed using examples in C unless the feature is available only for Java or C++.

Advanced C-Based Language Editing

When working with a C-based language, it is important to make sure that the buffer is in the correct mode. This should be handled automatically by Emacs, but if Emacs gets it wrong, you can run the command M-x c-mode for C code, M-x c++-mode for C++, M-x java-mode for Java code, or M-x objc-mode for Objective C. When you are in the correct mode, you can proceed to work with these languages more efficiently.

You have already been exposed to navigation and marking and deleting text based on some primitive constructs such as characters, lines, and words. In the C-based languages, there are even more advanced constructs that can be manipulated such as functions, lists, and syntactic expressions. It might seem like an impossibility to remember even more key sequences for navigation, but the keybindings are organized so they are easy to remember. For example, C-a is beginning of line, and M-a is beginning of sentence. Because a sentence doesn't make too much sense in C, M-a means beginning of command. This shows that control is used for geometric style units, and many Meta-key commands are used for items based on a language's syntax. Next, C-M-a means beginning of function. In this case, it moves the cursor to the opening { of your C function. By the same token, you can assume that M-e moves to the end of a command, and C-M-e moves to the end of a function definition.

Mixing familiar commands with these new commands in a table helps in remembering them:

Key

Control

Meta

Control-Meta

a, e

Beginning, end of line

Beginning, end of statement

Beginning, end of function

F, b

Forward, back character

Forward, back word

Forward, back syntactic expression

t

Transpose characters

Transpose words

Transpose syntactic expressions

k

Kill line

Kill sentence

Kill syntactic expression

n, p

Next, previous


Forward, back list

h


Mark paragraph

Mark function

l

Recenter on line


Recenter on function


Caution - It would seem logical during experimentation to extend other keys as well, such as Backspace or Delete for removal of syntactic expressions for a buffer. After all, Delete removes a character, and M-Delete deletes a word. Be warned that on some platforms Control-Alt-Delete runs system shutdown. Control-Alt-Backspace exits the XFree86 version of X Window, which is common under Linux.


One of the new navigation features introduced is the syntactic expression. A syntactic expression is a group of characters that represents some structure, including symbols, strings, comments, and lists. Text, such as words, can be syntactic expressions, but is often an element of a larger structure, such as a symbol. In a C language file, navigate over the symbol new_symbol.using M-f and M-b. Now try it with C-M-f and C-M-b. When navigating words, the cursor will stop on the underscore (_) character, but not when navigating as a syntactic expression. Try navigating over code blocks, strings, and comments with these new commands as well.

Some other types of things Emacs knows about are all clumped under the syntactic expression when it comes to keybindings. A string that starts and ends with a double quote (") character represents the string. A character in C-based languages starts and ends with a single quote (') character, but Emacs treats these as strings as well. A comment in C starts with /* and ends with */. In C++ and Objective C, the additional comment start of //, which comments to the end of the line, is also known.

What is especially interesting about strings and comments is that, when the cursor is outside the bounds of these objects, they are treated as one large element, regardless of their contents. This permits the forward syntactic expression command to skip over them, even if they contain characters that would normally terminate the current syntactic expression. If, on the other hand, the cursor is in a string or comment, such elements as parentheses matching and moving over syntactic expressions work as if the string or comment were not there.

Tip - In command names under Emacs, a syntactic expression is referred to as a sexp.

When using apropos as discussed in Hour 10, "The Emacs Help System and Configuration System," you can get a list of all syntactic expression related functions using sexp as the search element.


The other useful language features Emacs knows about are lists. Because much of Emacs has been implemented in Lisp, a list-based language, this makes a lot of sense. In C-based languages, a list starts with an open parenthesis, brace, or bracket, and continues until a closing character. Fortunately, Emacs is smart enough to skip such characters that are in strings or comments, so there is no fear of errors while using navigation or editing keys.

A list is also classified as a syntactic expression, so you can use C-M-f and C-M-b to navigate over them, but using C-M-n and C-M-p has the advantage of skipping over all text between the cursor and the list and moves over the list as well. Another useful command is called down-list and is bound to C-M-d. This skips from the cursor position to the next available list and stops after the opening character. You can pass a universal argument of -1 to move backward to the previous list as well.

Tip - All the navigation commands introduced in this hour accept the universal argument. This argument (passed with C-u or C-M--) lets you repeat a given operation or reverse it easily.


Sams Teach Yourself Emacs in 24 Hours

ContentsIndex

Hour 18: Editing C, C++, and Java Files

Previous HourNext Hour

Sections in this Hour: