Sams Teach Yourself Emacs in 24 Hours

ContentsIndex

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

Previous HourNext Hour

Sections in this Hour:

 

Navigating C Preprocessor Directives

When working with C, C++, and Objective C you will most likely encounter the C preprocessor. This preprocessor is the program that adds the #include commands that permit your source code to access prototypes from other source code files. It also includes #if, #ifdef, #else, and #endif commands. Although it is common to see these directives close together, they can be difficult to deal with when they cover wide blocks of code, providing compile-time configuration, or platform-specific details.

Emacs provides no fewer than three methods of viewing your directives. Fortunately, the basics of navigation are provided once by the default C mode. To begin, edit a file that contains some directives or add #ifdef #else #endif around some code where you can see all three elements. You will notice that the indenting engine knows to always bring these commands to the first column. Next, you can use the forward and backward conditional commands. Position the cursor at the beginning of the opening #if and use forward conditional command C-c C-n. Next try C-c C-p. This moves the cursor across the condition and skips across the #else directive.

Note - Notice the use of C-n and C-p with a C-c prefix. As you learned earlier, C-c prefixes mode specific commands, and C-n and C-p are used for navigating the Next and Previous lines. This should keep these commands easy to remember.


Next, if you are deep inside several nested conditionals, you can go up to the most-recently-entered conditional with the up conditional command bound to C-c C-u. Up conditional can also be reversed to move to the end of the current conditional with a negative argument. Thus, you can go to the #endif part with C-u - C-c C-u.

Sams Teach Yourself Emacs in 24 Hours

ContentsIndex

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

Previous HourNext Hour

Sections in this Hour: