all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Alan Mackenzie <acm@muc.de>
To: YOUNG <breadncup@gmail.com>
Cc: help-gnu-emacs@gnu.org
Subject: Re: c preprocessor and indent problem in c-mode?
Date: Mon, 26 Jan 2009 09:00:47 +0000	[thread overview]
Message-ID: <20090126090047.GA1417@muc.de> (raw)
In-Reply-To: <035566ec-3070-441e-8040-6f44c5c0d96c@p36g2000prp.googlegroups.com>

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




      parent reply	other threads:[~2009-01-26  9:00 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-01-26  0:46 c preprocessor and indent problem in c-mode? YOUNG
2009-01-26  2:03 ` YOUNG
2009-01-26  9:00 ` Alan Mackenzie [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20090126090047.GA1417@muc.de \
    --to=acm@muc.de \
    --cc=breadncup@gmail.com \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.