unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Re: C++-mode indentation and how to apply it to all the files under a directory.
@ 2009-08-06 15:49 ishikawa,chiaki
  0 siblings, 0 replies; 4+ messages in thread
From: ishikawa,chiaki @ 2009-08-06 15:49 UTC (permalink / raw)
  To: dima, help-gnu-emacs

Dmirty Dzhus wrote:

>> Q-1: How to place "{" at the same indention level as "if" and "for" when
>>      "{" is placed on the next line using C++mode.
> 
> Set your C style to "bsd". You'll still need to adjust offset to 2
> (because it's 8 by default for bsd).

It did the trick. It seems that the mode setting in per-file variables
sets the offset to 2. So I am done!
(I have not done much editing yet, and so if something comes up again,
I may  post again , but now I know more about the various settings and can
probably handle the rest.)

>> Q-2: The second question is how to apply the setting to all c++ files
>> under a certain directory, but not elsewhere.
> 
> (info "(emacs) Directory Variables")

This is a new feature in 23.1, isn't it? I installed emacs 23.1 and
all is well now (!).

It has been years since I read Emacs manual from cover to cover.
Maybe I should again.  The per-directory variable section  in Info manual is
section 57 and so the whole manual has more sections these days. Maybe my
summer holidays are not long enough to read it through...

> -- 
> Happy Hacking.
> 

Thank you and happy hacking!







^ permalink raw reply	[flat|nested] 4+ messages in thread
* C++-mode indentation and how to apply it to all the files under a directory.
@ 2009-08-05 18:03 ishikawa,chiaki
  0 siblings, 0 replies; 4+ messages in thread
From: ishikawa,chiaki @ 2009-08-05 18:03 UTC (permalink / raw)
  To: help-gnu-emacs

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 )













^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2009-08-06 16:04 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [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
2009-08-06 15:49 ishikawa,chiaki
  -- strict thread matches above, loose matches on Subject: below --
2009-08-05 18:03 ishikawa,chiaki

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).