help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: C++-mode indentation and how to apply it to all the files under a di


From: Alan Mackenzie
Subject: Re: C++-mode indentation and how to apply it to all the files under a directory.
Date: Thu, 6 Aug 2009 16:04:24 +0000 (UTC)
User-agent: tin/1.6.2-20030910 ("Pabbay") (UNIX) (FreeBSD/4.11-RELEASE (i386))

ishikawa,chiaki <ishikawa@yk.rim.or.jp> wrote:
> This is a rather long post, apologies in advance.

> I have a couple of questions for which I appreciate tips from the
> experienced users.

> The first question is regarding the the setting of C++-mode for a
> particular style of indention of "{", following the "if" or "for" on
> the next line.

> The second question is how to apply the setting to all C++ files
> under a certain directory tree, but not in other places.

This is dealt with on page "Config Basics" in the CC Mode manual.  Under
the heading "Hooks with Styles" is an example which does exactly what
you're asking for.  Here is that example:

          (defun my-c-mode-hook ()
            (c-set-style
             (if (and (buffer-file-name)
                      (string-match "/usr/src/linux" (buffer-file-name)))
                 "linux"
               "free-group-style")))
          (add-hook 'c-mode-hook 'my-c-mode-hook)

Other nearby pages in this manual tell you how to create a CC Mode
"style".

> Q-1: How to place "{" at the same indention level as "if" and "for" when
>     "{" is placed on the next line using C++mode.

> E.g.

>  if (foobar)
>  {
>    int i, j;
>    j = 0;
>    for (i = 0; i < 10; i++)
>    {
>      j += test( i * i );
>    }
>    gazonk = j;
>  }

> This style is adopted by Mozilla foundation for coding FireFox browser
> and ThunderBird mailer.

> They prefer 2 space for indentation and suggest that we place the following
> in each C++ source file at the beginning.

> /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- 
> */

> See:
> https://developer.mozilla.org/En/Mozilla_Coding_Style_Guide
> There is a section titled "Mode Line".

You really want to set the style variable c-basic-offset to 2.  This is
what determines the indentation.  Rather than squeezing all these
variables onto the files' first lines, you'd be better creating a style
for them.

> Well, it is suggested that "{" be placed on the same line as "if" and
> "for", then all is fine. But if we place "{" on the next line, the
> desired indentation level ought to be as the example I gave above. I
> found this out from the discussion in a bugzilla report.
> See comment # 74 and onward in the following bug report.
> https://bugzilla.mozilla.org/show_bug.cgi?id=387502

> But the mode line is not enough for the placement of "{" on the next
> line of "if" and "for". A more or less vanilla Emacs in C++-mode
> indents the code as follows. Indentation uses two-spaces, but not at
> the desired column.

You probably want to create your own style based on "bsd".  See page
"Styles" in the CC Mode manual.  This stuff is probably more complicated
than it really needs to be, but it does make sense if you persevere.

-- 
Alan Mackenzie (Nuernberg).



reply via email to

[Prev in Thread] Current Thread [Next in Thread]