[Top][All Lists]

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

Re: C-mode electric braces

From: Klaus Berndl
Subject: Re: C-mode electric braces
Date: 11 Sep 2002 13:45:01 +0200
User-agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.1

On Wed, 11 Sep 2002, Carsten Burstedde wrote:

>   >> 2. After having typed
>   >> for (i = 0; i < 3; ++i)
>   >> I want that hitting the opening brace places it on the next line,
>   >> then inserts an empty line and the closing brace, and places the
>   >> cursor in >>between:
>   >>  {
>   >>    <point>
>   >>  }
>   >> Any suggestions for my .emacs?
>   > I'm afraid you'll have to hack it yourself.  There is (AFAIK) no
>   > predefined functionality in CC mode for doing this.
>   >
>   > You could look at the function that gets called when you hit a brace
>   > and then write a new function that looks whether newlines should be
>   > inserted.
>  hmm, I am already happy enough that I can set some key bindings and
>  variables in my .emacs, this kind of stuff is probably beyond my el
>  skills. Anyone feels challenged to code this?

Hmm, this is also an answer to the follow previous before.
It is also dangerous to hack the eletric-functions of cc-mode. It is also not
very smart to bind the braces-keys to something self-written functions because
then you lost all of the nifty electric-behavior and it's customization
facilities of cc-mode.

So, what you can do is something like follows:

| (local-set-key (kbd "C-7") #'(lambda ()
|                               (interactive)
|                               ;; inserting the first electric { with
|                               ;; behavior as customized with cc-mode
|                               ;; options
|                               (execute-kbd-macro "{")
|                               (save-excursion
|                                 ;; inserting a temporary ; so the following
|                                 ;; closing brace is working correctly
|                                 (execute-kbd-macro ";")
|                                 ;; inserting the closing electric } with
|                                 ;; behavior as customized with cc-mode
|                                 ;; options
|                                 (execute-kbd-macro "}"))
|                               ;; now we remove the temp. inserted ;
|                               (delete-char 1)))

Add this for example to your c-mode-common-hook and maybe replace the key with
(kbd "{")); i have rebind this only because it is easier to hit with my

This does what you want and preserves all electric stuff of cc-mode.
Note: It is always bad for example to bind code like (insert "{") to
the brace key because then you would lose al the electric stuff of cc-mode.

Other idea: Use tempo.el which gives you templates which can also do what you
want. But if you want to use tempo in combination with eletric behaviour of
cc-mode you probably need my extension of tempo.el because the original
tempo.el works be inserting hard "{" etc.. so the electric behavior is lost.

>  Concerning my first question (quote):
>  1. When I define array constants, I do not want emacs to put the
>  closing braces on an extra line, which it currently does. When I hit }
>  after
>  int a[3] = { 1, 2, 3
>  the closing brace jumps to the next line. How can I switch this off?
>  (/quote)
>  This might be simple, any ideas?
>  Thanks,
>  Carsten

Klaus Berndl                    mailto: address@hidden
sd&m AG               
software design & management    
Thomas-Dehler-Str. 27, 81737 München, Germany
Tel +49 89 63812-392, Fax -220

reply via email to

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