all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: "ishikawa,chiaki" <ishikawa@yk.rim.or.jp>
To: help-gnu-emacs@gnu.org
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	[thread overview]
Message-ID: <4A79C983.6090502@yk.rim.or.jp> (raw)

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 )













             reply	other threads:[~2009-08-05 18:03 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-08-05 18:03 ishikawa,chiaki [this message]
     [not found] <mailman.4006.1249531659.2239.help-gnu-emacs@gnu.org>
2009-08-06  9:04 ` C++-mode indentation and how to apply it to all the files under a directory Dmitry Dzhus
2009-08-06 16:04 ` Alan Mackenzie
  -- strict thread matches above, loose matches on Subject: below --
2009-08-06 15:49 ishikawa,chiaki

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=4A79C983.6090502@yk.rim.or.jp \
    --to=ishikawa@yk.rim.or.jp \
    --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.