From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: =?ISO-8859-1?Q?Marc_D=FCrner?= Newsgroups: gmane.emacs.help Subject: Re: C++ Indentation and access-labels Date: Mon, 28 Sep 2009 18:39:42 +0200 Message-ID: <52a7da0f0909280939i15539d60ob9d2add6a1a2439c@mail.gmail.com> References: <52a7da0f0909140727i24f47eaem8a36684ef10502f9@mail.gmail.com> <20090918221218.GA5781@muc.de> <52a7da0f0909210708x21945b5ej557d4cd8b77a17cf@mail.gmail.com> <20090925082833.GA2596@muc.de> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: multipart/alternative; boundary=0015174c3648d2dd990474a5f1f7 X-Trace: ger.gmane.org 1254156042 22716 80.91.229.12 (28 Sep 2009 16:40:42 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Mon, 28 Sep 2009 16:40:42 +0000 (UTC) To: Alan Mackenzie , help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Mon Sep 28 18:40:35 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 1MsJGv-0001HY-M6 for geh-help-gnu-emacs@m.gmane.org; Mon, 28 Sep 2009 18:40:22 +0200 Original-Received: from localhost ([127.0.0.1]:46803 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MsJGv-0006ga-6y for geh-help-gnu-emacs@m.gmane.org; Mon, 28 Sep 2009 12:40:21 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1MsJGQ-0006fk-LB for help-gnu-emacs@gnu.org; Mon, 28 Sep 2009 12:39:50 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1MsJGM-0006fV-V9 for help-gnu-emacs@gnu.org; Mon, 28 Sep 2009 12:39:50 -0400 Original-Received: from [199.232.76.173] (port=34440 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MsJGM-0006fS-Hv for help-gnu-emacs@gnu.org; Mon, 28 Sep 2009 12:39:46 -0400 Original-Received: from mail-bw0-f220.google.com ([209.85.218.220]:38416) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1MsJGL-00015w-4N for help-gnu-emacs@gnu.org; Mon, 28 Sep 2009 12:39:46 -0400 Original-Received: by bwz20 with SMTP id 20so3778443bwz.42 for ; Mon, 28 Sep 2009 09:39:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:message-id:subject:from:to:content-type; bh=ggeajzEs5lbTvnJm/nWdFyGr8i6HKg4I1vvYngIeGGo=; b=knKvYx4jzQMjvrKQFqz0nXx6Qi6YmKqS4nAwH0hOE9UWNZ6kpvEFa3xbEdM1uqB7zX Kj2FywOLvZEISALJCZsP+gVn0e7h2TqCR+eDixcIaHe74oJ8N03f1zgQs245hD/y7nBD Q3+XaFZ1K5SS81I7UZACjpYjEHjDleE+9h3z4= DomainKey-Signature: a=rsa-sha1; c=nofws; d=googlemail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; b=SL5neEMZ3sllC7J1kfuL+NMIwbg23w/TkasyEV6AIRH2/TNNoM2p9Q8WrX4Aso9pQ7 3z4LhF2DPzmsKv5Md0gexBNnh/5Z4JGBreFVx6eys/S3w4finbGFntqrxQ9V3zzl2Cmu Bo41VXDIa6Bg+WaEfiaRYGPdeekAdO3dcS7LM= Original-Received: by 10.204.8.79 with SMTP id g15mr3151835bkg.202.1254155982396; Mon, 28 Sep 2009 09:39:42 -0700 (PDT) In-Reply-To: <20090925082833.GA2596@muc.de> X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6 (newer, 2) 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:68511 Archived-At: --0015174c3648d2dd990474a5f1f7 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Hello, Thank you, Alan. This works much better now. Can you help me out with a few remaining problems? 1) The inline-methods in a class or struct are not indented to the same level as topmost-intro Here is an example how it looks like: class c { friend class y; public: c() : _n(8) { } <=3D=3D=3D here void meth() {} <=3D=3D=3D here private: int _n; }; 2) when I type 'void meth()' after typing ')' eight spaces (two levels of indentation) are added to the next line with the cursor positioned at the beginning of the line: class c { public: void method() X....... where X is the cursor position and . are trailing whitespace. The same happens when I close a inline-defun with '}'. When I continue it disappears= , but it is a bit uncomfortable if I would just add a method to a class. Also= , the trailing whitespace stays when I reindent a block with the tab key 3) Another problem is caused by macros before class names. This is somethin= g which is very common: class MY_API MyClass {}; MY_API is some macro which expands to compiler specific attributes like declspec (VC) or __attribute__ (GCC). Many libraries have macros like this. Somehow it confuses the parser and indentation of access-labels is only two spaces (half of what it should be, c-basic-offset is 4).Example: class MY_API c { friend class y; public: c() : _n(8) { } void meth() {} private: int _n; }; Maybe there is another way to tell the parser to simply ignore "MY_API". regards, Marc 2009/9/25 Alan Mackenzie > Hi, Marc! > > On Mon, Sep 21, 2009 at 04:08:30PM +0200, Marc D=FCrner wrote: > > > > # File ~/duerner.el: > > > > ######################################################################### > > > (defsubst md-at-inclass/topmost-intro (sintax) > > > (and > > > (eq (caar sintax) 'inclass) > > > (eq (car (cadr sintax)) 'topmost-intro))) > > > > (defun md-ind-prot-class () > > > "Give an extra level of indentation to class thingies under an acces= s > > > specifier, e.g.: > > > > public > > > A(); <=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D extra level of ind= entation. > > > > This should really be properly implemented in CC Mode, but it's not." > > > (and > > > (md-at-inclass/topmost-intro c-syntactic-context) > > > (let (m-type) > > > (when > > > (save-excursion > > > (back-to-indentation) > > > ;; Go back one "statement" at a time till we reach a label = or > > > something > > > ;; which isn't an inclass/topmost-intro > > > (while > > > (and (eq (setq m-type (c-beginning-of-statement-1)) > > > 'previous) > > > (md-at-inclass/topmost-intro > (c-guess-basic-syntax)))) > > > ;; Have we found "private:", "public": or "protected"? > > > (and (eq m-type 'label) > > > (looking-at > > > (eval-when-compile > > > (c-make-keywords-re nil (c-lang-const > c-protection-kwds > > > c++)))))) > > > (save-excursion > > > (back-to-indentation) > > > (c-shift-line-indentation c-basic-offset)))))) > > > > (defun md-add-hook () > > > (add-hook 'c-special-indent-hook 'md-ind-prot-class)) > > > (add-hook 'c++-mode-hook 'md-add-hook) > > > > ######################################################################### > > > > The above is a fairly rough and ready hack. If it does something > silly, > > > please get back to me with a description of the failure. > > > > Thank you for you help! > > > The script you wrote seems to have no effect (yet): > > Sorry. I hadn't tested things enough. Here's what the problem is: > > > Here is what I added to my .emacs: > > > (require 'cc-mode) > > (setq c-basic-offset 4) > > (setq-default c-tab-always-indent nil) > > Just an aside: (setq c-basic-offset 4) will set the global value, but > this will get overridden by the style system. Either create your own > style. or set this to c++-mode-hook (see below). Yes, it's > overcomplicated. ;-( > > > (add-hook 'c++-mode-hook > > '(lambda () > > (c-set-style "ellemtel") > > (c-set-offset 'access-label '0))) > > The form (c-set-style "ellemtel") in c++-mode-hook wipes clean the > value of c-special-indent-hook. :-( So we need to arrange to do > "ellemtel" before doing "special-indent". > > > (eval-after-load "cc-mode" '(load-file "~/duerner.el")) > > So, please change the following: > (i) in ~/duerner.el, REMOVE the last line, "(add-hook 'c++-mode-hook > 'md-add-hook)". > > (ii) in ~/duerner.el, insert at the top the line "(require 'cc-langs)", > to make sure it compiles properly. > > (ii) Change your .emacs bit to this: > > (require 'cc-mode) > ;; (setq c-basic-offset 4) ; <=3D=3D=3D=3D=3D commented out > (setq-default c-tab-always-indent nil) > > (add-hook 'c++-mode-hook > '(lambda () > (c-set-style "ellemtel") > (add-hook 'c-special-indent-hook 'md-ind-prot-class) ; > <=3D=3D=3D=3D=3D=3D > (c-set-offset 'access-label '0) > (setq c-basic-offset 4))) > > (eval-after-load "cc-mode" '(load-file "~/duerner.el")) > > Hope it works now! > > > regards, > > Marc > > -- > Alan Mackenzie (N=FCrnberg). > --0015174c3648d2dd990474a5f1f7 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Hello,

Thank you, Alan. This works much better now. Can you help me = out with a few remaining problems?

1) The inline-methods in a class = or struct are not indented to the same level as topmost-intro

Here i= s an example how it looks like:

class c
{
=A0=A0=A0 friend class y;

=A0=A0=A0 public:
= =A0=A0=A0=A0=A0=A0=A0 c()
=A0=A0=A0=A0=A0=A0=A0 : _n(8)
=A0=A0=A0 { }= <=3D=3D=3D here

=A0=A0=A0=A0=A0=A0=A0 void meth()
=A0=A0=A0 {= } <=3D=3D=3D here

=A0=A0=A0 private:
=A0=A0=A0=A0=A0=A0=A0 int= _n;
};

2) when I type 'void meth()' after typing ')' eight spaces (tw= o levels of indentation) are added to the next line with the cursor positioned at the beginning of the line:

class c
{
=A0=A0=A0 public:
=A0=A0=A0=A0=A0=A0=A0 void method(= )
X.......

where X is the cursor position and . are trailing whitespace. The same happens when I close a inline-defun with '}'. When I continue it disappears, but it is a bit uncomfortable if I would just add a method to a class. Also, the trailing whitespace stays when I reindent a block with the tab key

3) Another problem is caused by macros before class names. This is some= thing which is very common:

class MY_API MyClass
{};

MY_AP= I is some macro which expands to compiler specific attributes like declspec (VC) or __attribute__ (GCC). Many libraries have macros like this. Somehow it confuses the parser and indentation of access-labels is only two spaces (half of what it should be, c-basic-offset is 4).Example:

class MY_API c
{
=A0=A0=A0 friend class y;

=A0 public:
= =A0=A0=A0 c()
=A0=A0=A0=A0=A0=A0=A0 : _n(8)
=A0=A0=A0 { }

=A0= =A0=A0 void meth()
=A0=A0=A0 {}

=A0 private:
=A0=A0=A0 int _n;=
};

Maybe there is another way to tell the parser to simply ignor= e "MY_API".

regards,
Marc

2009/9/25 Alan Macke= nzie <acm@muc.de>=
Hi, Marc!

On Mon, Sep 21, 2009 at 04:08:30PM +0200, Marc D=FCrner wrote:

> > # File ~/duerner.el:
> > #################################################################= ########
> > (defsubst md-at-inclass/topmost-intro (sintax)
> > =A0(and
> > =A0 (eq (caar sintax) 'inclass)
> > =A0 (eq (car (cadr sintax)) 'topmost-intro)))

> > (defun md-ind-prot-class ()
> > =A0"Give an extra level of indentation to class thingies und= er an access
> > specifier, e.g.:

> > =A0 =A0public
> > =A0 =A0 =A0 =A0A(); =A0 =A0 =A0 =A0<=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D extra level of indentation.

> > This should really be properly implemented in CC Mode, but it'= ;s not."
> > =A0(and
> > =A0 (md-at-inclass/topmost-intro c-syntactic-context)
> > =A0 (let (m-type)
> > =A0 =A0 (when
> > =A0 =A0 =A0 =A0 (save-excursion
> > =A0 =A0 =A0 =A0 =A0 (back-to-indentation)
> > =A0 =A0 =A0 =A0 =A0 ;; Go back one "statement" at a tim= e till we reach a label or
> > something
> > =A0 =A0 =A0 =A0 =A0 ;; which isn't an inclass/topmost-intro > > =A0 =A0 =A0 =A0 =A0 (while
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 (and (eq (setq m-type (c-beginning-of= -statement-1))
> > 'previous)
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0(md-at-inclass/topmost-int= ro (c-guess-basic-syntax))))
> > =A0 =A0 =A0 =A0 =A0 ;; Have we found "private:", "= public": or "protected"?
> > =A0 =A0 =A0 =A0 =A0 (and (eq m-type 'label)
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0(looking-at
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 (eval-when-compile
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0(c-make-keywords-re nil (c-lan= g-const c-protection-kwds
> > c++))))))
> > =A0 =A0 =A0 (save-excursion
> > =A0 =A0 =A0 =A0(back-to-indentation)
> > =A0 =A0 =A0 =A0(c-shift-line-indentation c-basic-offset))))))

> > (defun md-add-hook ()
> > =A0(add-hook 'c-special-indent-hook 'md-ind-prot-class))<= br> > > (add-hook 'c++-mode-hook 'md-add-hook)
> > #################################################################= ########

> > The above is a fairly rough and ready hack. =A0If it does somethi= ng silly,
> > please get back to me with a description of the failure.


> Thank you for you help!

> The script you wrote seems to have no effect (yet):

Sorry. =A0I hadn't tested things enough. =A0Here's what= the problem is:

> Here is what I added to my .emacs:

> (require 'cc-mode)
> (setq c-basic-offset 4)
> (setq-default c-tab-always-indent nil)

Just an aside: (setq c-basic-offset 4) will set the global value, but=
this will get overridden by the style system. =A0Either create your own
style. or set this to c++-mode-hook (see below). =A0Yes, it's
overcomplicated. =A0;-(

> (add-hook 'c++-mode-hook
> =A0 =A0 =A0 =A0 =A0 '(lambda ()
> =A0 =A0 =A0 =A0 =A0 =A0 =A0(c-set-style "ellemtel")
> =A0 =A0 =A0 =A0 =A0 =A0 =A0(c-set-offset 'access-label '0)))
The form (c-set-style "ellemtel") in c++-mode-hook wipes cl= ean the
value of c-special-indent-hook. =A0:-( =A0So we need to arrange to do
"ellemtel" before doing "special-indent".

> (eval-after-load "cc-mode" '(load-file "~/duerner.e= l"))

So, please change the following:
(i) in ~/duerner.el, REMOVE the last line, "(add-hook 'c++-mode-ho= ok
=A0'md-add-hook)".

(ii) in ~/duerner.el, insert at the top the line "(require 'cc-lan= gs)",
=A0to make sure it compiles properly.

(ii) Change your .emacs bit to this:

=A0(require 'cc-mode)
=A0;; (setq c-basic-offset 4) =A0; <=3D=3D=3D=3D=3D commented out
=A0(setq-default c-tab-always-indent nil)

=A0(add-hook 'c++-mode-hook
=A0 =A0 =A0 =A0 =A0 =A0'(lambda ()
=A0 =A0 =A0 =A0 =A0 =A0 =A0 (c-set-style "ellemtel")
=A0 =A0 =A0 =A0 =A0 =A0 =A0 (add-hook 'c-special-indent-hook = 9;md-ind-prot-class) ; <=3D=3D=3D=3D=3D=3D
=A0 =A0 =A0 =A0 =A0 =A0 =A0 (c-set-offset 'access-la= bel '0)
=A0 =A0 =A0 =A0 =A0 =A0 =A0 (setq c-basic-offset 4)))

=A0(eval-after-load "cc-mode" '(load-file "~/duerner.el= "))

Hope it works now!

> regards,
> Marc

--
Alan Mackenzie (N=FCrnberg).

--0015174c3648d2dd990474a5f1f7--