Next: Hanging Colons, Previous: Custom Auto-newlines, Up: Custom Auto-newlines

### 8.1 Hanging Braces

To specify which kinds of braces you want auto-newlines put around, you set the style variable `c-hanging-braces-alist`. Its structure and semantics are described in this section. Details of how to set it up, and its relationship to CC Mode's style system are given in Style Variables.

Say you wanted an auto-newline after (but not before) the following ‘`{`’:

         if (foo < 17) {

First you need to find the syntactic context of the brace—type a <RET> before the brace to get it on a line of its own1, then type `C-c C-s`. That will tell you something like:

         ((substatement-open 1061))

So here you need to put the entry `(substatement-open . (after))` into `c-hanging-braces-alist`.

If you don't want any auto-newlines for a particular syntactic symbol, put this into `c-hanging-braces-alist`:

         (brace-entry-open)

If some brace syntactic symbol is not in `c-hanging-brace-alist`, its entry is taken by default as `(before after)`—insert a newline both before and after the brace. In place of a “before/after” list you can specify a function in this alist—this is useful when the auto newlines depend on the code around the brace.

— User Option: c-hanging-braces-alist

> This variable is an association list which maps syntactic symbols to lists of places to insert a newline. See Association Lists. The key of each element is the syntactic symbol, the associated value is either `nil`, a list, or a function.

> The Key - the syntactic symbol
>   The syntactic symbols that are useful as keys in this list are `brace-list-intro`, `statement-cont`, `inexpr-class-open`, `inexpr-class-close`, and all the `*-open` and `*-close` symbols. See Syntactic Symbols, for a more detailed description of these syntactic symbols, except for `inexpr-class-open` and `inexpr-class-close`, which aren't actual syntactic symbols. Elements with any other value as a key get ignored.

>   The braces of anonymous inner classes in Java are given the special symbols `inexpr-class-open` and `inexpr-class-close`, so that they can be distinguished from the braces of normal classes2.

>   Note that the aggregate constructs in Pike mode, ‘`({`’, ‘`})`’, ‘`([`’, ‘`])`’, and ‘`(<`’, ‘`>)`’, do not count as brace lists in this regard, even though they do for normal indentation purposes. It's currently not possible to set automatic newlines on these constructs.

> The associated value - the “ACTION” list or function
>   The value associated with each syntactic symbol in this association list is called an action, which can be either a list or a function which returns a list. See Custom Braces, for how to use a function as a brace hanging action.

>   The list action (or the list returned by action when it's a function) contains some combination of the symbols `before` and `after`, directing CC Mode where to put newlines in relationship to the brace being inserted. Thus, if the list contains only the symbol `after`, then the brace hangs on the right side of the line, as in:

>                      // here, open braces always `hang'
>                      void spam( int i ) {
>                          if( i == 7 ) {
>                              dosomething(i);
>                          }
>                      }

>   When the list contains both `after` and `before`, the braces will appear on a line by themselves, as shown by the close braces in the above example. The list can also be empty, in which case newlines are added neither before nor after the brace.

> If a syntactic symbol is missing entirely from `c-hanging-braces-alist`, it's treated in the same way as an action with a list containing `before` and `after`, so that braces by default end up on their own line.

> For example, the default value of `c-hanging-braces-alist` is:

>               ((brace-list-open)
>                (brace-entry-open)
>                (statement-cont)
>                (substatement-open after)
>                (block-close . c-snug-do-while)
>                (extern-lang-open after)
>                (namespace-open after)
>                (module-open after)
>                (composition-open after)
>                (inexpr-class-open after)
>                (inexpr-class-close before))

> which says that `brace-list-open`, `brace-entry-open` and `statement-cont` 3 braces should both hang on the right side and allow subsequent text to follow on the same line as the brace. Also, `substatement-open`, `extern-lang-open`, and `inexpr-class-open` braces should hang on the right side, but subsequent text should follow on the next line. The opposite holds for `inexpr-class-close` braces; they won't hang, but the following text continues on the same line. Here, in the `block-close` entry, you also see an example of using a function as an action. In all other cases, braces are put on a line by themselves.

#### Footnotes

[1] Also insert a ‘`\`’ at the end of the previous line if you're in AWK Mode.

[2] The braces of anonymous classes produce a combination of `inexpr-class`, and `class-open` or `class-close` in normal indentation analysis.

[3] Brace lists inside statements, such as initializers for static array variables inside functions in C, are recognized as `statement-cont`. All normal substatement blocks are recognized with other symbols.