24.19. C and Related Modes

This section gives a brief description of the special features available in C, C++, Objective-C, Java, CORBA IDL, and Pike modes. (These are called "C mode and related modes.") , for a more extensive description of these modes and their special features.

24.19.1. C Mode Motion Commands

This section describes commands for moving point, in C mode and related modes.

C-c C-u

Move point back to the containing preprocessor conditional, leaving the mark behind. A prefix argument acts as a repeat count. With a negative argument, move point forward to the end of the containing preprocessor conditional. When going backwards, #elif is treated like #else followed by #if. When going forwards, #elif is ignored.

C-c C-p

Move point back over a preprocessor conditional, leaving the mark behind. A prefix argument acts as a repeat count. With a negative argument, move forward.

C-c C-n

Move point forward across a preprocessor conditional, leaving the mark behind. A prefix argument acts as a repeat count. With a negative argument, move backward.

M-a

Move point to the beginning of the innermost C statement (c-beginning-of-statement). If point is already at the beginning of a statement, move to the beginning of the preceding statement. With prefix argument n, move back n − 1 statements.

If point is within a string or comment, or next to a comment (only whitespace between them), this command moves by sentences instead of statements.

When called from a program, this function takes three optional arguments: the numeric prefix argument, a buffer position limit (don't move back before that place), and a flag that controls whether to do sentence motion when inside of a comment.

M-e

Move point to the end of the innermost C statement; like M-a except that it moves in the other direction (c-end-of-statement).

M-x c-backward-into-nomenclature

Move point backward to beginning of a C++ nomenclature section or word. With prefix argument n, move n times. If n is negative, move forward. C++ nomenclature means a symbol name in the style of NamingSymbolsWithMixedCaseAndNoUnderlines; each capital letter begins a section or word.

In the GNU project, we recommend using underscores to separate words within an identifier in C or C++, rather than using case distinctions.

M-x c-forward-into-nomenclature

Move point forward to end of a C++ nomenclature section or word. With prefix argument n, move n times.

24.19.2. Electric C Characters

In C mode and related modes, certain printing characters are "electric"--in addition to inserting themselves, they also reindent the current line and may insert newlines. This feature is controlled by the variable c-auto-newline. The "electric" characters are {, }, :, #, ;, ,, <, >, /, *, (, and ).

Electric characters insert newlines only when the auto-newline feature is enabled (indicated by /a in the mode line after the mode name). This feature is controlled by the variable c-auto-newline. You can turn this feature on or off with the command C-c C-a:

C-c C-a

Toggle the auto-newline feature (c-toggle-auto-state). With a prefix argument, this command turns the auto-newline feature on if the argument is positive, and off if it is negative.

The colon character is electric because that is appropriate for a single colon. But when you want to insert a double colon in C++, the electric behavior of colon is inconvenient. You can insert a double colon with no reindentation or newlines by typing C-c ::

C-c :

Insert a double colon scope operator at point, without reindenting the line or adding any newlines (c-scope-operator).

The electric # key reindents the line if it appears to be the beginning of a preprocessor directive. This happens when the value of c-electric-pound-behavior is (alignleft). You can turn this feature off by setting c-electric-pound-behavior to nil.

The variable c-hanging-braces-alist controls the insertion of newlines before and after inserted braces. It is an association list with elements of the following form: (syntactic-symbol . nl-list). Most of the syntactic symbols that appear in c-offsets-alist are meaningful here as well.

The list nl-list may contain either of the symbols before or after, or both; or it may be nil. When a brace is inserted, the syntactic context it defines is looked up in c-hanging-braces-alist; if it is found, the nl-list is used to determine where newlines are inserted: either before the brace, after, or both. If not found, the default is to insert a newline both before and after braces.

The variable c-hanging-colons-alist controls the insertion of newlines before and after inserted colons. It is an association list with elements of the following form: (syntactic-symbol . nl-list). The list nl-list may contain either of the symbols before or after, or both; or it may be nil.

When a colon is inserted, the syntactic symbol it defines is looked up in this list, and if found, the nl-list is used to determine where newlines are inserted: either before the brace, after, or both. If the syntactic symbol is not found in this list, no newlines are inserted.

Electric characters can also delete newlines automatically when the auto-newline feature is enabled. This feature makes auto-newline more acceptable, by deleting the newlines in the most common cases where you do not want them. Emacs can recognize several cases in which deleting a newline might be desirable; by setting the variable c-cleanup-list, you can specify which of these cases that should happen. The variable's value is a list of symbols, each describing one case for possible deletion of a newline. Here are the meaningful symbols, and their meanings:

brace-catch-brace

Clean up } catch (condition) { constructs by placing the entire construct on a single line. The clean-up occurs when you type the {, if there is nothing between the braces aside from catch and condition.

brace-else-brace

Clean up } else { constructs by placing the entire construct on a single line. The clean-up occurs when you type the { after the else, but only if there is nothing but white space between the braces and the else.

brace-elseif-brace

Clean up } else if (…) { constructs by placing the entire construct on a single line. The clean-up occurs when you type the {, if there is nothing but white space between the } and { aside from the keywords and the if-condition.

empty-defun-braces

Clean up empty defun braces by placing the braces on the same line. Clean-up occurs when you type the closing brace.

defun-close-semi

Clean up the semicolon after a struct or similar type declaration, by placing the semicolon on the same line as the closing brace. Clean-up occurs when you type the semicolon.

list-close-comma

Clean up commas following braces in array and aggregate initializers. Clean-up occurs when you type the comma.

scope-operator

Clean up double colons which may designate a C++ scope operator, by placing the colons together. Clean-up occurs when you type the second colon, but only when the two colons are separated by nothing but whitespace.

24.19.3. Hungry Delete Feature in C

When the hungry-delete feature is enabled (indicated by /h or /ah in the mode line after the mode name), a single DEL command deletes all preceding whitespace, not just one space. To turn this feature on or off, use C-c C-d:

C-c C-d

Toggle the hungry-delete feature (c-toggle-hungry-state). With a prefix argument, this command turns the hungry-delete feature on if the argument is positive, and off if it is negative.

C-c C-t

Toggle the auto-newline and hungry-delete features, both at once (c-toggle-auto-hungry-state).

The variable c-hungry-delete-key controls whether the hungry-delete feature is enabled.

24.19.4. Other Commands for C Mode

C-M-h

Put mark at the end of a function definition, and put point at the beginning (c-mark-function).

M-q

Fill a paragraph, handling C and C++ comments (c-fill-paragraph). If any part of the current line is a comment or within a comment, this command fills the comment or the paragraph of it that point is in, preserving the comment indentation and comment delimiters.

C-c C-e

Run the C preprocessor on the text in the region, and show the result, which includes the expansion of all the macro calls (c-macro-expand). The buffer text before the region is also included in preprocessing, for the sake of macros defined there, but the output from this part isn't shown.

When you are debugging C code that uses macros, sometimes it is hard to figure out precisely how the macros expand. With this command, you don't have to figure it out; you can see the expansions.

C-c C-\

Insert or align \ characters at the ends of the lines of the region (c-backslash-region). This is useful after writing or editing a C macro definition.

If a line already ends in \, this command adjusts the amount of whitespace before it. Otherwise, it inserts a new \. However, the last line in the region is treated specially; no \ is inserted on that line, and any \ there is deleted.

M-x cpp-highlight-buffer

Highlight parts of the text according to its preprocessor conditionals. This command displays another buffer named *CPP Edit*, which serves as a graphic menu for selecting how to display particular kinds of conditionals and their contents. After changing various settings, click on [A]pply these settings (or go to that buffer and type a) to rehighlight the C mode buffer accordingly.

C-c C-s

Display the syntactic information about the current source line (c-show-syntactic-information). This is the information that directs how the line is indented.

M-x cwarn-mode, M-x global-cwarn-mode

CWarn minor mode highlights certain suspicious C and C++ constructions:

  • Assignments inside expressions.

  • Semicolon following immediately after if, for, and while (except after a do … while statement);

  • C++ functions with reference parameters.

You can enable the mode for one buffer with the command M-x cwarn-mode, or for all suitable buffers with the command M-x global-cwarn-mode or by customizing the variable global-cwarn-mode. You must also enable Font Lock mode to make it work.

M-x hide-ifdef-mode

Hide-ifdef minor mode hides selected code within #if and #ifdef preprocessor blocks. See the documentation string of hide-ifdef-mode for more information.

M-x ff-find-related-file

Find a file "related" in a special way to the file visited by the current buffer. Typically this will be the header file corresponding to a C/C++ source file, or vice versa. The variable ff-related-file-alist specifies how to compute related file names.

24.19.5. Comments in C Modes

C mode and related modes use a number of variables for controlling comment format.

c-comment-only-line-offset

Extra offset for line which contains only the start of a comment. It can be either an integer or a cons cell of the form (non-anchored-offset . anchored-offset), where non-anchored-offset is the amount of offset given to non-column-zero anchored comment-only lines, and anchored-offset is the amount of offset to give column-zero anchored comment-only lines. Just an integer as value is equivalent to (val . 0).

c-comment-start-regexp

This buffer-local variable specifies how to recognize the start of a comment.

c-hanging-comment-ender-p

If this variable is nil, c-fill-paragraph leaves the comment terminator of a block comment on a line by itself. The default value is t, which puts the comment-end delimiter */ at the end of the last line of the comment text.

c-hanging-comment-starter-p

If this variable is nil, c-fill-paragraph leaves the starting delimiter of a block comment on a line by itself. The default value is t, which puts the comment-start delimiter /* at the beginning of the first line of the comment text.