unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Auto-indentation
@ 2006-02-10 13:27 Marcus Husar
  2006-02-10 17:30 ` Auto-indentation Kevin Rodgers
  0 siblings, 1 reply; 14+ messages in thread
From: Marcus Husar @ 2006-02-10 13:27 UTC (permalink / raw)



[-- Attachment #1.1: Type: text/plain, Size: 769 bytes --]

I no searched 2 hours for auto-indentation in emacs. I couldn't find anything. Could someone please tell me how it works?

here is my .emacs:

(setq case-fold-search t)
(global-font-lock-mode t)
(mouse-wheel-mode t)
(setq auto-fill-mode 1)
(setq display-time-day-and-date t)
(display-time)

(setq c-mode-hook
      (function (lambda ()
		  (c-set-style "K&R")
		  (setq c-basic-offset 8))))
(setq objc-mode-hook
      (function (lambda ()
		  (c-set-style "K&R")
		  (setq c-basic-offset 8))))
(setq c++-mode-hook
      (function (lambda ()
		  (c-set-style "K&R")
		  (setq c-basic-offset 8))))
(setq java-mode-hook
      (function (lambda ()
		  (c-set-style "K&R")
		  (setq c-basic-offset 8))))

-- 
Marcus Husar: mail@marcus-husar.de

[-- Attachment #1.2: Digital signature --]
[-- Type: application/pgp-signature, Size: 189 bytes --]

[-- Attachment #2: Type: text/plain, Size: 152 bytes --]

_______________________________________________
help-gnu-emacs mailing list
help-gnu-emacs@gnu.org
http://lists.gnu.org/mailman/listinfo/help-gnu-emacs

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

* Re: Auto-indentation
       [not found] <mailman.124.1139578896.2885.help-gnu-emacs@gnu.org>
@ 2006-02-10 14:02 ` Zhang Wei
  2006-02-10 17:34 ` Auto-indentation Stefan Monnier
  2006-02-10 19:24 ` Auto-indentation Alan Mackenzie
  2 siblings, 0 replies; 14+ messages in thread
From: Zhang Wei @ 2006-02-10 14:02 UTC (permalink / raw)


Why not just use M-x c-toggle-auto-newline in CC mode?

-- 
Zhang Wei or Brep
<brep@smth.org>

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

* Re: Auto-indentation
  2006-02-10 13:27 Auto-indentation Marcus Husar
@ 2006-02-10 17:30 ` Kevin Rodgers
  0 siblings, 0 replies; 14+ messages in thread
From: Kevin Rodgers @ 2006-02-10 17:30 UTC (permalink / raw)


Marcus Husar wrote:
 > I no searched 2 hours for auto-indentation in emacs. I couldn't find
 > anything. Could someone please tell me how it works?

Where did you look?  There is a node in the Emacs manual called
Custom[izing] C Indent[ation], which has a link to the CC Mode manual's
Customizing Indentation node.

-- 
Kevin Rodgers

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

* Re: Auto-indentation
       [not found] <mailman.124.1139578896.2885.help-gnu-emacs@gnu.org>
  2006-02-10 14:02 ` Auto-indentation Zhang Wei
@ 2006-02-10 17:34 ` Stefan Monnier
  2006-02-10 19:24 ` Auto-indentation Alan Mackenzie
  2 siblings, 0 replies; 14+ messages in thread
From: Stefan Monnier @ 2006-02-10 17:34 UTC (permalink / raw)


> I no searched 2 hours for auto-indentation in emacs.  I couldn't find
> anything.  Could someone please tell me how it works?

I suspect you're victim of a misunderstanding.
Emacs's auto-indentation is just always present.  You don't have to look
for it.  The way it works: you hit TAB and it automatically finds the right
spot to indent the current line.

If you want all the code to be automatically indented all the time without
you hitting any special key, it's something else and Emacs doesn't do that
in general.


        Stefan

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

* Re: Auto-indentation
       [not found] <mailman.124.1139578896.2885.help-gnu-emacs@gnu.org>
  2006-02-10 14:02 ` Auto-indentation Zhang Wei
  2006-02-10 17:34 ` Auto-indentation Stefan Monnier
@ 2006-02-10 19:24 ` Alan Mackenzie
  2006-02-13  0:26   ` Auto-indentation Paul Whitfield
  2 siblings, 1 reply; 14+ messages in thread
From: Alan Mackenzie @ 2006-02-10 19:24 UTC (permalink / raw)


Marcus Husar <mail@marcus-husar.de> wrote on Fri, 10 Feb 2006 14:27:32 +0100:

> I no searched 2 hours for auto-indentation in emacs. I couldn't find
> anything. Could someone please tell me how it works?

Assuming you're talking about C, C++, Objective-C and Java, (as suggested
by your .emacs), then an existing line of code gets re-indented when

o - you press the TAB key.
o - you type an "electric" character, such as ";" or "{".
o - you run some other indentation command, such as C-M-q, C-c C-q, or
    C-M-\.

When you type <ret>, the newline doesn't get indented.  You can make this
happen by typing C-j instead of <ret>.  If you really want <ret> to
indent the new line, then put this into your .emacs:

        (defun my-make-CR-do-indent ()
          (define-key c-mode-base-map "\C-m" 'c-context-line-break))
        (add-hook 'c-initialization-hook 'my-make-CR-do-indent)


> here is my .emacs:

> (setq case-fold-search t)
> (global-font-lock-mode t)
> (mouse-wheel-mode t)
> (setq auto-fill-mode 1)
> (setq display-time-day-and-date t)
> (display-time)

> (setq c-mode-hook
>       (function (lambda ()
> 		  (c-set-style "K&R")
> 		  (setq c-basic-offset 8))))
> (setq objc-mode-hook
>       (function (lambda ()
> 		  (c-set-style "K&R")
> 		  (setq c-basic-offset 8))))
> (setq c++-mode-hook
>       (function (lambda ()
> 		  (c-set-style "K&R")
> 		  (setq c-basic-offset 8))))
> (setq java-mode-hook
>       (function (lambda ()
> 		  (c-set-style "K&R")
> 		  (setq c-basic-offset 8))))

Nothing to do with what you asked for, but a comment all the same.  You
can achieve the effect of the above 16 lines more simply by code like
this in your .emacs:

        (setq c-default-style "K&R")
        (defun my-set-basic-offset ()
          (setq c-basic-offset 8))
        (add-hook 'c-mode-common-hook 'my-set-basic-offset)

> Marcus Husar: mail@marcus-husar.de

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

* Re: Auto-indentation
  2006-02-10 19:24 ` Auto-indentation Alan Mackenzie
@ 2006-02-13  0:26   ` Paul Whitfield
  2006-02-13  0:30     ` Auto-indentation Paul Whitfield
  2006-02-13  8:07     ` Auto-indentation Alan Mackenzie
  0 siblings, 2 replies; 14+ messages in thread
From: Paul Whitfield @ 2006-02-13  0:26 UTC (permalink / raw)


Alan Mackenzie wrote:
> Marcus Husar <mail@marcus-husar.de> wrote on Fri, 10 Feb 2006 14:27:32 +0100:
> 
>> I no searched 2 hours for auto-indentation in emacs. I couldn't find
>> anything. Could someone please tell me how it works?
> 
> Assuming you're talking about C, C++, Objective-C and Java, (as suggested
> by your .emacs), then an existing line of code gets re-indented when
> 
> o - you press the TAB key.
> o - you type an "electric" character, such as ";" or "{".
> o - you run some other indentation command, such as C-M-q, C-c C-q, or
>     C-M-\.
> 
> When you type <ret>, the newline doesn't get indented.  You can make this
> happen by typing C-j instead of <ret>.  If you really want <ret> to
> indent the new line, then put this into your .emacs:
> 
>         (defun my-make-CR-do-indent ()
>           (define-key c-mode-base-map "\C-m" 'c-context-line-break))
>         (add-hook 'c-initialization-hook 'my-make-CR-do-indent)
> 

Alternatively:

(defun my-c-mode-common-hook ()
   (define-key c-mode-base-map "\C-m" 'newline-and-indent))

(add-hook 'c-mode-common-hook' my-c-mode-common-hook)



This uses the generic newline-and-indent which should work
for even non "c" type languages.

Adding to the common hook means that all languages that
make use of CC-mode will be "auto" indented.


regards

Paul

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

* Re: Auto-indentation
  2006-02-13  0:26   ` Auto-indentation Paul Whitfield
@ 2006-02-13  0:30     ` Paul Whitfield
  2006-02-13  8:07     ` Auto-indentation Alan Mackenzie
  1 sibling, 0 replies; 14+ messages in thread
From: Paul Whitfield @ 2006-02-13  0:30 UTC (permalink / raw)


Paul Whitfield wrote:
> Alan Mackenzie wrote:
>> Marcus Husar <mail@marcus-husar.de> wrote on Fri, 10 Feb 2006 14:27:32 
>> +0100:
>>
>>> I no searched 2 hours for auto-indentation in emacs. I couldn't find
>>> anything. Could someone please tell me how it works?
>>
>> Assuming you're talking about C, C++, Objective-C and Java, (as suggested
>> by your .emacs), then an existing line of code gets re-indented when
>>
>> o - you press the TAB key.
>> o - you type an "electric" character, such as ";" or "{".
>> o - you run some other indentation command, such as C-M-q, C-c C-q, or
>>     C-M-\.
>>
>> When you type <ret>, the newline doesn't get indented.  You can make this
>> happen by typing C-j instead of <ret>.  If you really want <ret> to
>> indent the new line, then put this into your .emacs:
>>
>>         (defun my-make-CR-do-indent ()
>>           (define-key c-mode-base-map "\C-m" 'c-context-line-break))
>>         (add-hook 'c-initialization-hook 'my-make-CR-do-indent)
>>
> 
> Alternatively:
> 
> (defun my-c-mode-common-hook ()
>   (define-key c-mode-base-map "\C-m" 'newline-and-indent))
> 
> (add-hook 'c-mode-common-hook' my-c-mode-common-hook)
> 
> 
> 
> This uses the generic newline-and-indent which should work
> for even non "c" type languages.
> 
> Adding to the common hook means that all languages that
> make use of CC-mode will be "auto" indented.

Oops... just re-read the documentation, c-initialization-hook is run
first, so ignore my advice about using the common-hook instead of the
initialisation hook.

regards
Paul

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

* Re: Auto-indentation
  2006-02-13  0:26   ` Auto-indentation Paul Whitfield
  2006-02-13  0:30     ` Auto-indentation Paul Whitfield
@ 2006-02-13  8:07     ` Alan Mackenzie
  2006-02-14  1:04       ` Auto-indentation Paul Whitfield
  1 sibling, 1 reply; 14+ messages in thread
From: Alan Mackenzie @ 2006-02-13  8:07 UTC (permalink / raw)


Paul Whitfield <NoSpam@iinet.net.au> wrote on Mon, 13 Feb 2006 08:26:43 +0800:
> Alan Mackenzie wrote:

[  .... ]

>> When you type <ret>, the newline doesn't get indented.  You can make
>> this happen by typing C-j instead of <ret>.  If you really want <ret>
>> to indent the new line, then put this into your .emacs:

>>         (defun my-make-CR-do-indent ()
>>           (define-key c-mode-base-map "\C-m" 'c-context-line-break))
>>         (add-hook 'c-initialization-hook 'my-make-CR-do-indent)
>> 

> Alternatively:

> (defun my-c-mode-common-hook ()
>    (define-key c-mode-base-map "\C-m" 'newline-and-indent))

> (add-hook 'c-mode-common-hook' my-c-mode-common-hook)



> This uses the generic newline-and-indent which should work
> for even non "c" type languages.

Yes, but the binding suggested is in CC Mode's main keymap
(c-mode-base-map), so this is irrelevant.  The command
c-context-line-break, unlike newline-and-indent, continues comments and
multi-line macros with comment prefixes (e.g. " * " at beginning of line)
and backslashed newlines.

> Adding to the common hook means that all languages that
> make use of CC-mode will be "auto" indented.

Yes.  I've seen your follow-up post.  ;-)  The thing about
c-mode-common-hook is that it's executed every time you visit a new
buffer.  You only need to bind key sequences once.  Binding them in the
common hook isn't wrong, and doesn't noticeably delay anything, but it's
kind of klunky.  This klunkiness was actually encouraged by the CC Mode
manual, but newer editions have corrected this.

> regards

> Paul

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

* Re: Auto-indentation
  2006-02-13  8:07     ` Auto-indentation Alan Mackenzie
@ 2006-02-14  1:04       ` Paul Whitfield
  0 siblings, 0 replies; 14+ messages in thread
From: Paul Whitfield @ 2006-02-14  1:04 UTC (permalink / raw)


Alan Mackenzie wrote:
> Paul Whitfield <NoSpam@iinet.net.au> wrote on Mon, 13 Feb 2006 08:26:43 +0800:
>> Alan Mackenzie wrote:
> 
> [  .... ]
> 
>>> When you type <ret>, the newline doesn't get indented.  You can make
>>> this happen by typing C-j instead of <ret>.  If you really want <ret>
>>> to indent the new line, then put this into your .emacs:
> 
>>>         (defun my-make-CR-do-indent ()
>>>           (define-key c-mode-base-map "\C-m" 'c-context-line-break))
>>>         (add-hook 'c-initialization-hook 'my-make-CR-do-indent)
>>>
> 
>> Alternatively:
> 
>> (defun my-c-mode-common-hook ()
>>    (define-key c-mode-base-map "\C-m" 'newline-and-indent))
> 
>> (add-hook 'c-mode-common-hook' my-c-mode-common-hook)
> 
> 
> 
>> This uses the generic newline-and-indent which should work
>> for even non "c" type languages.
> 
> Yes, but the binding suggested is in CC Mode's main keymap
> (c-mode-base-map), so this is irrelevant.  The command
> c-context-line-break, unlike newline-and-indent, continues comments and
> multi-line macros with comment prefixes (e.g. " * " at beginning of line)
> and backslashed newlines.

Oh... I think I will try that out!

>> Adding to the common hook means that all languages that
>> make use of CC-mode will be "auto" indented.
> 
> Yes.  I've seen your follow-up post.  ;-)  The thing about
> c-mode-common-hook is that it's executed every time you visit a new
> buffer.  You only need to bind key sequences once.  Binding them in the
> common hook isn't wrong, and doesn't noticeably delay anything, but it's
> kind of klunky.  This klunkiness was actually encouraged by the CC Mode
> manual, but newer editions have corrected this.

Goes to show you should re-visit manuals!

Thank you for correcting my wrong headedness :-)

Regards
Paul.

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

* Auto-indentation
@ 2012-04-20 12:57 Richard H Lee
  2012-04-21  8:39 ` Auto-indentation Mark Skilbeck
  0 siblings, 1 reply; 14+ messages in thread
From: Richard H Lee @ 2012-04-20 12:57 UTC (permalink / raw)
  To: help-gnu-emacs

I would like to use auto-indentation in emacs.

When I C-j in C syntax it add an extra indent if it is expecting an 
extra code block. It also does so if I type an opening curly bracket.

E.g

for(;;;)
   {

I just want emacs to keep the indent of the line I'm on, that is all. I 
don't need emacs to predict indentation for me.

Richard



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

* Re: Auto-indentation
  2012-04-20 12:57 Auto-indentation Richard H Lee
@ 2012-04-21  8:39 ` Mark Skilbeck
  2012-04-21 12:58   ` Auto-indentation Richard H Lee
  0 siblings, 1 reply; 14+ messages in thread
From: Mark Skilbeck @ 2012-04-21  8:39 UTC (permalink / raw)
  To: Richard H Lee; +Cc: help-gnu-emacs

On Fri, Apr 20, 2012 at 01:57:14PM +0100, Richard H Lee wrote:
> I would like to use auto-indentation in emacs.
> 
> When I C-j in C syntax it add an extra indent if it is expecting an
> extra code block. It also does so if I type an opening curly
> bracket.
> 
> E.g
> 
> for(;;;)
>   {
> 
> I just want emacs to keep the indent of the line I'm on, that is
> all. I don't need emacs to predict indentation for me.
> 
> Richard

See c-set-style. Typically bound to "C-c ."

- mgsk.



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

* Re: Auto-indentation
  2012-04-21  8:39 ` Auto-indentation Mark Skilbeck
@ 2012-04-21 12:58   ` Richard H Lee
  2012-04-21 13:51     ` Auto-indentation Richard H Lee
  0 siblings, 1 reply; 14+ messages in thread
From: Richard H Lee @ 2012-04-21 12:58 UTC (permalink / raw)
  To: help-gnu-emacs

On 21/04/12 09:39, Mark Skilbeck wrote:
> See c-set-style. Typically bound to "C-c ."

Thanks for pointing me in the right direction.

I just set c-offsets-alist to nil, but I'm not sure if that's overkill.



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

* Re: Auto-indentation
  2012-04-21 12:58   ` Auto-indentation Richard H Lee
@ 2012-04-21 13:51     ` Richard H Lee
  2012-04-21 22:38       ` Auto-indentation Ludwig, Mark
  0 siblings, 1 reply; 14+ messages in thread
From: Richard H Lee @ 2012-04-21 13:51 UTC (permalink / raw)
  To: help-gnu-emacs

On 21/04/12 13:58, Richard H Lee wrote:
> c-offsets-alist to nil

I still had problems.

c-syntactic-indentation did the trick



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

* RE: Auto-indentation
  2012-04-21 13:51     ` Auto-indentation Richard H Lee
@ 2012-04-21 22:38       ` Ludwig, Mark
  0 siblings, 0 replies; 14+ messages in thread
From: Ludwig, Mark @ 2012-04-21 22:38 UTC (permalink / raw)
  To: Richard H Lee, help-gnu-emacs@gnu.org

> From: Richard H Lee
> Sent: Saturday, April 21, 2012 8:52 AM
> To: help-gnu-emacs@gnu.org
> Subject: Re: Auto-indentation
> 
> On 21/04/12 13:58, Richard H Lee wrote:
> > c-offsets-alist to nil
> 
> I still had problems.
> 
> c-syntactic-indentation did the trick

If this is all you need to do, great.  To fit existing code I maintain, I found I needed to extensively customize c-offsets-list, but also others such as c-basic-offset and c-comment-only-line-offset; the easiest way to do it all together is to create your own "style" and add it using c-add-style.

(I did this so I can impress my colleagues by positioning point in front of the opening brace of an entire C function or Java method and press C-M-q.)

Cheers,
Mark




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

end of thread, other threads:[~2012-04-21 22:38 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-04-20 12:57 Auto-indentation Richard H Lee
2012-04-21  8:39 ` Auto-indentation Mark Skilbeck
2012-04-21 12:58   ` Auto-indentation Richard H Lee
2012-04-21 13:51     ` Auto-indentation Richard H Lee
2012-04-21 22:38       ` Auto-indentation Ludwig, Mark
     [not found] <mailman.124.1139578896.2885.help-gnu-emacs@gnu.org>
2006-02-10 14:02 ` Auto-indentation Zhang Wei
2006-02-10 17:34 ` Auto-indentation Stefan Monnier
2006-02-10 19:24 ` Auto-indentation Alan Mackenzie
2006-02-13  0:26   ` Auto-indentation Paul Whitfield
2006-02-13  0:30     ` Auto-indentation Paul Whitfield
2006-02-13  8:07     ` Auto-indentation Alan Mackenzie
2006-02-14  1:04       ` Auto-indentation Paul Whitfield
  -- strict thread matches above, loose matches on Subject: below --
2006-02-10 13:27 Auto-indentation Marcus Husar
2006-02-10 17:30 ` Auto-indentation Kevin Rodgers

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).