1
votes

I have recently started using emacs for editing C source, and have been using the auto-newline feature of cc-mode (c-toggle-auto-newline). This works well for constructs like functions and if/else statements, but seems to act strangely when a closing brace should be followed by a semi-colon.

Using auto-newline in GNU Emacs 23.3 I get:

struct foo
{
    int x;
}
    ;

char int[2] =
  {
    0, 1
  }
  ;

I would like to instead get:

struct foo {
    int x;
};

char int[2] = { 0, 1 };

How can I get the closing semi-colon to remain on the same line as the closing brace?

2

2 Answers

1
votes

I don't think you can go around this problem with auto newline on. It's not a greatly thought-through feature, it simply inserts newlines after certain characters (;, {, etc.). But seriously, how hard is it to press and enter key? Any automation is always error-prone.

0
votes

You can customize the "cleanup" behavior when auto-newline is turned on. This is controlled by the contents of the c-cleanup-list variable. (View help for this within Emacs by entering C-h v c-cleanup-list.

Specifically, adding defun-close-semi to c-cleanup-list will solve your problem.

If you're already defining a custom style within your ~/.emacs file, then you can probably figure out how to do this. Otherwise, the easiest way to change this setting is via Customize. In the help buffer (displayed when you ran C-h v c-cleanup-list), the last line will have a link to customize this variable.