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

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

Re: marrying braket for ?: operator


From: Alan Mackenzie
Subject: Re: marrying braket for ?: operator
Date: Sun, 31 Jul 2005 12:20:59 +0000
User-agent: tin/1.4.5-20010409 ("One More Nightmare") (UNIX) (Linux/2.0.35 (i686))

Baloff <washdc@wash.edu> wrote on 29 Jul 2005 18:22:26 +1000:

[ .... ]

> now that you know my .emacs and the lot how can I get
> enum colors { to go to a new line and indent like when I type say
> main(){ or for(...) {

First remark:  this works only when you've got "Auto Newline Mode"
enabled, which you obviously already have.  C-c C-a will toggle this
mode.

Second remark: The stuff here is documented in the CC Mode manual on the
pages "Auto-newline Insertion" and "Hanging Braces".

Third remark:  Have a look at a C or C++ source file, and type C-c C-s on
a few lines.  This will show you the "syntactic context", i.e. CC Mode's
syntactic analysis of the line.  Put the { of "enum colors {" on its own
line, and do C-c C-s there.  It will tell you that this sort of brace is
a "brace-list-open".

In your CC Mode configuration, you need to change the setting
`c-hanging-brace-alist'.  While in a C/C++ buffer, do C-h C-v and have a
look at the value of that variable.  It will be something like this:

               ((brace-list-open) <===================
                (block-open after)
                (defun-open after)
                (substatement-open after)
                (block-close . c-snug-do-while))

Each element of this list is itself a list.  The first element (e.g.
defun-open) gives the type of the brace, and the other elements (either
"before", "after", both or none[*]), say where CC Mode is to insert the
newlines for this sort of brace.

[*] For advanced uses, a function can be specified here instead.

For your brace-list-open element, I think you want "after", but you can
change that yourself if need be.

OK!  Let's now go to your Lisp.  Where you've got

(c-add-style "mc394-style"
             '("cc-mode"
               (c-basic-offset . 3)      ; In general, indent 3 spaces
               (c-hanging-braces-alist

, you're creating a style called "mc394-style" which inherits from the
style called "cc-mode".  Since you're changing the style, you probably
want to give it a new name, say "baloff-style", in a new file called
baloff-style.el.  So copy the file and change these lines to this:

(c-add-style "baloff-style"
             '("cc-mode"
               (c-basic-offset . 3)      ; In general, indent 3 spaces
               (c-hanging-braces-alist
  
.  A bit lower down, where you've got:

               (c-hanging-braces-alist
                ; These determine whether a newline appears before
                ; and/or after a brace in various situations.
                (brace-list-open)
                (block-open after)       ; statement block open brace
                (defun-open after)       ; brace that opens a function 
definition
                (substatement-open after); the brace that opens a substatement 
block
                (block-close . c-snug-do-while))

, change "(brace-list-open)" to "(brace-list-open after)".

Load this new file (M-x load-file), Then from a C++ buffer, do C-c .
(that's "control-C dot"), check your new style has been loaded, and set
your buffer to this style.  Check it does what you want, and if it
doesn't, play around with it till it does.  Then go through your .emacs,
replacing all references to "mc394-style" with "baloff-style".

Have fun!

-- 
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]