GNU Emacs Manual - Electric C

Go to the first, previous, next, last section, table of contents.

### 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.

Go to the first, previous, next, last section, table of contents.