[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: c preprocessor and indent problem in c-mode?
From: |
Alan Mackenzie |
Subject: |
Re: c preprocessor and indent problem in c-mode? |
Date: |
Mon, 26 Jan 2009 09:00:47 +0000 |
User-agent: |
Mutt/1.5.9i |
Hi, there,
On Sun, Jan 25, 2009 at 04:46:38PM -0800, YOUNG wrote:
> Hi,
> If there is a following code, emacs does not present indent correctly.
> #incude <stdio.h>
> int main(void) {
> int a = 10;
> #ifdef AAA
> if (a == 10) { <====================== A
> printf("a:%d\n", a);
> #else
> if (b == 10) { <====================== B
> printf("b=10\n");
> #endif
> }
> #endif
> }
> If the curly brace is not closed before the preprocessor, emacs seems
> to confuse to make indent.
Yes.
> Is there anyone who know this problem and how to resolve?
Yes. CC Mode doesn't take account of the #ifdef and #else when parsing
braces. So it looks like the brace on line B is nested within the brace
on line A. This is a longstanding bug, which needs fundamental work in
CC Mode (and possibly the Emacs core) to fix. It may get fixed in the
medium future. Sorry!
A reasonable workaround, if the project you're working on will let you,
is to keep braces in #ifdefs (and #defines) balanced. You could thus
write this instead:
#ifdef AAA
if (a == 10) {
printf("a:%d\n", a);
}
#else
if (b == 10) {
printf("b=10\n");
}
#endif
--
Alan Mackenzie (Nuremberg, Germany).