Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How do I make emacs indent treat #ifdef and #endif as if they were { and }?

76 views
Skip to first unread message

Jules Colding

unread,
Feb 3, 2012, 4:49:35 PM2/3/12
to help-gn...@gnu.org
Hi,

I've tried to figure out how to make emacs indent #ifdef blocks just like it would curly brace blocks. With a twist though… I'd prefer if I could convince emacs to indent include statement half the amount of whitespace that normal code would be indented.

My preference is to indent code with 8 spaces (or one tab of that size) and then use 4 spaces for include statements. Within code my preference would be if "#ifdef" was treated exactly as a "{" and "#endif" like a "}", indentation wise.

Like this:

#include <ifaddrs.h>
#ifdef __linux__
#include <sys/socket.h>
#endif

int
func(void)
{
int foo = 0;

#ifdef DO_STUFF
foo = do_stuff();
#endif

return foo;
}


instead of the current affair:


#include <ifaddrs.h>
#ifdef __linux__
#include <sys/socket.h>
#endif

int
func(void)
{
int foo = 0;

#ifdef DO_STUFF
foo = do_stuff();
#endif

return foo;
}

Messing around with the indentation functions in emacs seems like a black art. Likely because I don't do lisp well...

Would anyone have code that solves this or pointers to something that is close to what I want?

Thanks a lot,
jules




Alan Mackenzie

unread,
Feb 3, 2012, 11:10:36 PM2/3/12
to
Jules Colding <col...@venalicium.dk> wrote:
> Hi,

> I've tried to figure out how to make emacs indent #ifdef blocks just like
> it would curly brace blocks. With a twist though? I'd prefer if I could
> convince emacs to indent include statement half the amount of whitespace
> that normal code would be indented.

I'm afraid this simply isn't possible at the moment.

Sorry.

[ .... ]

> Messing around with the indentation functions in emacs seems like a black
> art. Likely because I don't do lisp well...

It's described in full detail in the CC Mode manual, chapter "Customizing
Indentation".

> Thanks a lot,
> jules

--
Alan Mackenzie (Nuremberg, Germany).

Ken Goldman

unread,
Feb 8, 2012, 4:57:56 PM2/8/12
to help-gn...@gnu.org
If you care about portability, some older compilers do not permit white
space before the #.

On 2/3/2012 4:49 PM, Jules Colding wrote:
>
> I've tried to figure out how to make emacs indent #ifdef blocks just
> like it would curly brace blocks. With a twist though… I'd prefer if

Jules Colding

unread,
Feb 8, 2012, 5:01:41 PM2/8/12
to help-gn...@gnu.org

On Feb 8, 2012, at 10:57 PM, Ken Goldman wrote:

> If you care about portability, some older compilers do not permit white space before the #.

I don't. I just want the source to look as readable as possible. I don't care if it can't compile in some old compiler. Most, if not all, recent compilers are happy about spaces before the #.

Cheers,
jules



David Combs

unread,
Mar 10, 2012, 9:14:11 PM3/10/12
to
In article <mailman.3503.132873852...@gnu.org>,
Aren't there well-known (unix) programs whose sole purpose is
to "beautify" (via indenting) various kinds of files?

Maybe "indent" is the name of one.

I recall looking at either that or another one once,
but it had so many complicated options that it overwhelmed
me.

Maybe things have improved since then -- I don't know.

David


Dan Espen

unread,
Mar 10, 2012, 11:08:34 PM3/10/12
to
Last time I used it I noticed a huge number of options.

So, I just used it once, looked at the results,
then searched the man page for things I wanted to change.


--
Dan Espen

David Combs

unread,
Apr 4, 2012, 3:41:21 AM4/4/12
to
Maybe someone here uses that program. If so, and you're one
of the indent users, perhaps you could show us some indent runs,
with options, and for each run (or just invocation),
say what you wanted to accomplish, and
why you used those options, and maybe how you came up with
that set of options.

That would *really* be useful, would make indent a *lot* more
likely to get used.


Thanks!

David

0 new messages