From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: "ishikawa,chiaki" Newsgroups: gmane.emacs.help Subject: C++-mode indentation and how to apply it to all the files under a directory. Date: Thu, 06 Aug 2009 03:03:47 +0900 Message-ID: <4A79C983.6090502@yk.rim.or.jp> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-2022-JP Content-Transfer-Encoding: 7bit X-Trace: ger.gmane.org 1249531838 7188 80.91.229.12 (6 Aug 2009 04:10:38 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Thu, 6 Aug 2009 04:10:38 +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 06:10:31 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 1MYuJC-00059z-Sb for geh-help-gnu-emacs@m.gmane.org; Thu, 06 Aug 2009 06:10:31 +0200 Original-Received: from localhost ([127.0.0.1]:42697 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MYuJC-0001eo-90 for geh-help-gnu-emacs@m.gmane.org; Thu, 06 Aug 2009 00:10:30 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1MYl9e-0001Qc-JB for help-gnu-emacs@gnu.org; Wed, 05 Aug 2009 14:24:02 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1MYl9a-0001QN-Rl for help-gnu-emacs@gnu.org; Wed, 05 Aug 2009 14:24:02 -0400 Original-Received: from [199.232.76.173] (port=47702 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MYl9a-0001QK-Mk for help-gnu-emacs@gnu.org; Wed, 05 Aug 2009 14:23:58 -0400 Original-Received: from mbos141-212.alpenstock.jp ([220.156.141.212]:41384 helo=brasilia.mbos.jp) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1MYl9a-00025T-1E for help-gnu-emacs@gnu.org; Wed, 05 Aug 2009 14:23:58 -0400 Original-Received: from [172.16.30.42] ([172.16.30.42]) by brasilia.mbos.jp ([172.16.30.52]) with ESMTP id 2009080603:03:49:259129.815.2550541200 for ; Thu, 06 Aug 2009 03:03:49 +0900 (JST) Original-Received: (qmail 14676 invoked from network); 6 Aug 2009 03:03:49 +0900 Original-Received: from nttkyo944079.tkyo.nt.ftth.ppp.infoweb.ne.jp (HELO [192.168.0.112]) (ishikawa_yk@smp.mbos.jp@[115.177.130.79]) (envelope-sender ) by beijing.mbos (qmail-ldap-1.03) with SMTP for ; 6 Aug 2009 03:03:49 +0900 User-Agent: Thunderbird 2.0.0.22 (Windows/20090605) X-TERRACE-SPAMMARK: NOT spam-marked. (by Terrace) X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6 (newer, 3) X-Greylist: delayed 1205 seconds by postgrey-1.27 at monty-python; Wed, 05 Aug 2009 14:23:56 EDT X-Mailman-Approved-At: Thu, 06 Aug 2009 00:07:37 -0400 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:66829 Archived-At: 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. 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". 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. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */ test() { if (foobar) { int i, j; j = 0; for (i = 0; i < 10; i++) { j += test( i * i ); } gazonk = j; } } I thought initially that adding (list (brace-open . 0) (brace-close . 0)) to the beginning of c-offsets-alist (according to the documentation, a buffer-local variable) would do the trick, but it doesn't. It indents everything in the "{", and "}" at the same level. (Hmm, I can't reproduce the result anymore. How strange.) Can anyone suggest the proper setting? (OK, my latest trial suggests that adding (list (cons 'substatement-open 0) (cons 'block-close 0))) may do the trick, but I am not sure what the side effects, if any, are.) Any experienced tips will be appreciated. After reading through help-gnu-emacs archive, I noticed there is c-set-style function, but not for "mozilla". Q-2: The second question is how to apply the setting to all c++ files under a certain directory, but not elsewhere. I wrote a following emacs code (still testing and message output with sit-for wait) to do this. It essentially adds a hook function to C++-mode-hook and check if the filename associated with the current buffer is under a certain directory, I apply proper tweak to variables of C++mode. But I have to admit it is rather kludgy and ad-hoc. Is there a framework for applying some editing/mode preferences to files under a directory tree or a group of trees, etc.? BTW, in the following code, I used save-excursion to (re-?) visit the current buffer since, during testing, I noticed a temporary buffer name "**lose**" appear on the mode line during the sit-for wait and I am not sure if c-offsets-alist of the desired buffer is set (and not that of "**lose**" . (I am trying to see the current setting of c-offsets-alist using C-h v, but it seems that it returns nil all the time. Is it trying to show the value of c-offsets-alist which is local to the minibuffer? AHA, NO. The document string for c-offsets-alist is so long that I have to scroll down a few pages before I can read the value.) Again, any tips to clean up the following code will be appreciated. (Sometimes I got file specification error if the path doesn't match? Do we need to return a certain value from the hook function?) ;; c++-mode-hook ;; if current buffer is associated with a file path that starts ;; with the prefix of my own mozilla source tree, then ;; add extra assoc list to c-offset-alist is added. (defun add-mozilla-style-hook () (let* ( (filename (buffer-file-name)) ;;(pairs (list (cons 'block-open 0) (cons 'block-close 0))) (pairs (list (cons 'substatement-open 0) (cons 'block-close 0))) (path "/extra/ishikawa/download") ;; (path "c:/download") ;; (prefix (substring filename 0 5)) (cbuf (current-buffer)) ) (message "filename %s" filename) (sit-for 2) (message "current buffer %S" cbuf) (sit-for 2) (message "match result: %S" (string-match path filename)) (sit-for 2) (if (= 0 (string-match path filename)) ;; ;; OK, the file is inside my mozilla source tree, ;; let us apply mozilla-specific C++-mode setting. ;; (progn (message "pairs %S" pairs) (sit-for 2) (if (boundp 'c-offsets-alist ) (progn (message "setting a list to c-offsets-alist") (sit-for 1) (message "buffer is %S" (current-buffer)) (sit-for 1) (save-excursion (switch-to-buffer cbuf) (message "new alist is %S" (append pairs c-offsets-alist)) (sit-for 1) (setq c-offsets-alist (append pairs c-offsets-alist)) ) ) (progn (message "c-offsets-alist was unbound") (setq c-offsets-alist pairs) )))) ) ) ;;; (add-hook 'c-mode-hook 'add-mozilla-style-hook ) (add-hook 'c++-mode-hook 'add-mozilla-style-hook )