all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* auto-newline before opening brace following "if","else" and "for"
@ 2005-10-19  6:19 ozark
  2005-10-23  9:36 ` auto-newline before opening brace following "if", "else" " Alan Mackenzie
  0 siblings, 1 reply; 2+ messages in thread
From: ozark @ 2005-10-19  6:19 UTC (permalink / raw)


I'm using C++-mode, with auto-newline turned on ("C++/ah Abbrev", it
shows).
c-set-style is set to gnu.
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?

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: auto-newline before opening brace following "if", "else" and "for"
  2005-10-19  6:19 auto-newline before opening brace following "if","else" and "for" ozark
@ 2005-10-23  9:36 ` Alan Mackenzie
  0 siblings, 0 replies; 2+ messages in thread
From: Alan Mackenzie @ 2005-10-23  9:36 UTC (permalink / raw)


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").

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2005-10-23  9:36 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-10-19  6:19 auto-newline before opening brace following "if","else" and "for" ozark
2005-10-23  9:36 ` auto-newline before opening brace following "if", "else" " Alan Mackenzie

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.