help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: auto-newline before opening brace following "if", "else" and "for"


From: Alan Mackenzie
Subject: Re: auto-newline before opening brace following "if", "else" and "for"
Date: Sun, 23 Oct 2005 09:36:31 +0000
User-agent: tin/1.4.5-20010409 ("One More Nightmare") (UNIX) (Linux/2.0.35 (i686))

ozark <shreevatsa@gmail.com> wrote on 18 Oct 2005 23:19:12 -0700:
> I'm using C++-mode, with auto-newline turned on ("C++/ah Abbrev", it
> shows).
> c-set-style is set to gnu.

I'm assuming here you've got CC Mode 5.28 (M-x c-version).

> A newline is automatically inserted before and after an opening brace
> if it follows a function name. However, when the brace is after an "if"
> or an "else" or a "for", it places the brace on the same line and only
> inserts a newline after it, i.e, it doesn't insert the newline before
> the brace.

> Is this default behaviour? How do I change it?

This is a bug in CC Mode 5.28, which is still present in the latest
released version (5.30.10).  :-(  Thanks for drawing it to our attention!

The variable concerned is `c-hanging-braces-alist'.  This is a "style
variable", a buffer local variable which goes through all the contortions
of CC Mode's style system for its initalisation.

Because this variable is not explicitly specified in the "gnu" style, it
is inheriting its value from the "user" style.  You can see this value by
doing C-h v c-hanging-brace-alist from your C Mode buffer.  It will give
this value:

((brace-list-open)
 (brace-entry-open)
 (substatement-open after)         <==========================
 (block-close . c-snug-do-while)
 (extern-lang-open after)
 (inexpr-class-open after)
 (inexpr-class-close before))

The substatement-open entry is the problem.  It should read
(substatement-open . (before after)), (or, alternatively be absent, since
before + after is the default).

I suggest you work around this bug by setting c-hanging-braces-alist in a
hook function.  Put this (or something like it) into your .emacs:

(defun oz-c-mode-common-hook ()
  (setq c-hanging-braces-alist
    '((brace-list-open)
      (brace-entry-open)
      (substatement-open before after) ; <============
      (block-close . c-snug-do-while)
      (extern-lang-open after)
      (inexpr-class-open after)
      (inexpr-class-close before))))
(add-hook 'c-mode-common-hook 'oz-c-mode-common-hook)

-- 
Alan Mackenzie (Munich, Germany)
Email: aacm@muuc.dee; to decode, wherever there is a repeated letter
(like "aa"), remove half of them (leaving, say, "a").



reply via email to

[Prev in Thread] Current Thread [Next in Thread]