From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Alan Mackenzie Newsgroups: gmane.emacs.help 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) Organization: muc.de e.V. Message-ID: References: NNTP-Posting-Host: lo.gmane.org X-Trace: ger.gmane.org 1249576886 11106 80.91.229.12 (6 Aug 2009 16:41:26 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Thu, 6 Aug 2009 16:41:26 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Thu Aug 06 18:41:20 2009 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1MZ61m-0006UJ-Hx for geh-help-gnu-emacs@m.gmane.org; Thu, 06 Aug 2009 18:41:19 +0200 Original-Received: from localhost ([127.0.0.1]:48622 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MZ61l-0007K1-U6 for geh-help-gnu-emacs@m.gmane.org; Thu, 06 Aug 2009 12:41:17 -0400 Original-Path: news.stanford.edu!newsfeed.stanford.edu!news.tele.dk!news.tele.dk!small.news.tele.dk!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!news2.euro.net!newsfeed.freenet.de!news.space.net!news.muc.de!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 80 Original-NNTP-Posting-Host: marvin.muc.de Original-X-Trace: colin2.muc.de 1249574664 54695 2001:608:1000::2 (6 Aug 2009 16:04:24 GMT) Original-X-Complaints-To: news-admin@muc.de Original-NNTP-Posting-Date: Thu, 6 Aug 2009 16:04:24 +0000 (UTC) User-Agent: tin/1.6.2-20030910 ("Pabbay") (UNIX) (FreeBSD/4.11-RELEASE (i386)) Original-Xref: news.stanford.edu gnu.emacs.help:171690 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:66857 Archived-At: ishikawa,chiaki 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).