all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* C-mode electric braces
@ 2002-09-10  9:47 Carsten Burstedde
  0 siblings, 0 replies; 7+ messages in thread
From: Carsten Burstedde @ 2002-09-10  9:47 UTC (permalink / raw)


Hi,

I want to customize the electric brace behaviour in C-mode. I looked 
through the threads of the last months, but did not find a matching subject.

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?

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?

Thanks,

Carsten

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

* Re: C-mode electric braces
       [not found] <mailman.1031651288.3988.help-gnu-emacs@gnu.org>
@ 2002-09-10 12:11 ` Kai Großjohann
  0 siblings, 0 replies; 7+ messages in thread
From: Kai Großjohann @ 2002-09-10 12:11 UTC (permalink / raw)


Carsten Burstedde <bursted@iam.uni-bonn.de> writes:

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

kai
-- 
A large number of young women don't trust men with beards.  (BFBS Radio)

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

* Re: C-mode electric braces
       [not found] <20020910141602.24672.83238.Mailman@monty-python.gnu.org>
@ 2002-09-11  9:37 ` Carsten Burstedde
       [not found] ` <mailman.1031737090.30427.help-gnu-emacs@gnu.org>
  1 sibling, 0 replies; 7+ messages in thread
From: Carsten Burstedde @ 2002-09-11  9:37 UTC (permalink / raw)


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

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

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

* Re: C-mode electric braces
       [not found] ` <mailman.1031737090.30427.help-gnu-emacs@gnu.org>
@ 2002-09-11 11:45   ` Klaus Berndl
  2002-09-11 17:54   ` Alan Mackenzie
  1 sibling, 0 replies; 7+ messages in thread
From: Klaus Berndl @ 2002-09-11 11:45 UTC (permalink / raw)


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
keyboard.

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.

Ciao,
Klaus
  
>  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: klaus.berndl@sdm.de
sd&m AG				http://www.sdm.de
software design & management	
Thomas-Dehler-Str. 27, 81737 München, Germany
Tel +49 89 63812-392, Fax -220

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

* Re: C-mode electric braces
       [not found] <20020911141602.25522.35321.Mailman@monty-python.gnu.org>
@ 2002-09-11 15:07 ` Carsten Burstedde
       [not found] ` <mailman.1031756888.28531.help-gnu-emacs@gnu.org>
  1 sibling, 0 replies; 7+ messages in thread
From: Carsten Burstedde @ 2002-09-11 15:07 UTC (permalink / raw)


 > 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)))
`----

Thanks, this does precisely what I wanted!

Just curious: How do I make execute-kbd-macro simulate a return press? I 
made several tries, but it did not work. And what does the # in front of 
the '(lambda ?

Carsten

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

* Re: C-mode electric braces
       [not found] ` <mailman.1031756888.28531.help-gnu-emacs@gnu.org>
@ 2002-09-11 15:46   ` Klaus Berndl
  0 siblings, 0 replies; 7+ messages in thread
From: Klaus Berndl @ 2002-09-11 15:46 UTC (permalink / raw)


On Wed, 11 Sep 2002, Carsten Burstedde wrote:

>  Just curious: How do I make execute-kbd-macro simulate a return press?
>  I made several tries, but it did not work. And what does the # in
>  front of the '(lambda ?

(execute-kbd-macro (kbd "<RET>"))

But just for your information: If you want to add some RETURNs in the
brace-behavior you asked for you should not insert RETs hardly neither with
(insert...)  nor with (execute-kbd-macro ...) but you should customize with
the cc-mode options if and when RETs should be inserted before or after
inserting braces (this is the electric behavior!).

AFAIK #'(lambda... is the same as (function (lambda... Read the documentation
of *function*. It helps the byte-compiler but AFAIK with recent Emacs-version
21.X this is no longer necessary...but don't know exactly.

Ciao,
Klaus

-- 
Klaus Berndl			mailto: klaus.berndl@sdm.de
sd&m AG				http://www.sdm.de
software design & management	
Thomas-Dehler-Str. 27, 81737 München, Germany
Tel +49 89 63812-392, Fax -220

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

* Re: C-mode electric braces
       [not found] ` <mailman.1031737090.30427.help-gnu-emacs@gnu.org>
  2002-09-11 11:45   ` Klaus Berndl
@ 2002-09-11 17:54   ` Alan Mackenzie
  1 sibling, 0 replies; 7+ messages in thread
From: Alan Mackenzie @ 2002-09-11 17:54 UTC (permalink / raw)


Carsten Burstedde <bursted@iam.uni-bonn.de> wrote on Wed, 11 Sep 2002
11:37:12 +0200:

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

Go to the cc-mode info page, then find the page "Hanging Braces".  What
this says is that there is an association list called
"c-hanging-braces-alist".  In that alist, somehow, you'll have an entry
something like (brace-list-close before) which says "if the } just typed
was syntactically a brace-list-close, insert an extra newline _before_
the }".

Remove or modify this, and your problem will be solved (as opposed to
merely being work around).

By the way, to discover that the } was actually a "brace-list-close", you
make it the first symbol on a line (if it weren't there already :-) then
type C-c C-s.

> Carsten

-- 
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] 7+ messages in thread

end of thread, other threads:[~2002-09-11 17:54 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2002-09-10  9:47 C-mode electric braces Carsten Burstedde
     [not found] <mailman.1031651288.3988.help-gnu-emacs@gnu.org>
2002-09-10 12:11 ` Kai Großjohann
     [not found] <20020910141602.24672.83238.Mailman@monty-python.gnu.org>
2002-09-11  9:37 ` Carsten Burstedde
     [not found] ` <mailman.1031737090.30427.help-gnu-emacs@gnu.org>
2002-09-11 11:45   ` Klaus Berndl
2002-09-11 17:54   ` Alan Mackenzie
     [not found] <20020911141602.25522.35321.Mailman@monty-python.gnu.org>
2002-09-11 15:07 ` Carsten Burstedde
     [not found] ` <mailman.1031756888.28531.help-gnu-emacs@gnu.org>
2002-09-11 15:46   ` Klaus Berndl

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.