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

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

C++-mode indentation and how to apply it to all the files under a direct


From: ishikawa,chiaki
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
User-agent: Thunderbird 2.0.0.22 (Windows/20090605)

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 via email to

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