all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Alan Mackenzie <acm@muc.de>
To: marc.duerner@gmail.com
Cc: help-gnu-emacs@gnu.org
Subject: Re: C++ Indentation and access-labels
Date: Fri, 18 Sep 2009 22:12:18 +0000	[thread overview]
Message-ID: <20090918221218.GA5781@muc.de> (raw)
In-Reply-To: <52a7da0f0909140727i24f47eaem8a36684ef10502f9@mail.gmail.com>

Hi, Marc!

On Mon, Sep 14, 2009 at 04:27:11PM +0200, Marc Dürner wrote:
> Hello,

> I am trying to set up semantic indentation for C++ and can't find a way
> to configure the following indentation rule:

> class A
> {
>     friend class X;

>     int x;

>     public:
>         A();    <===========
>         ~A();   <===========
> };

> I want to have class members and friend declarations indented by one level
> if no access-label is present. If an acces-label is present it should be
> indented and the class members that follow that access-label should be
> indented further. It seems the is no way to handle class memers differently
> if they are following an access-label. In the example above 'x' and 'A();'
> are both classified as inclass/topmost-intro.

> Is there a way to configure this?

No.  There should be, though.  The first member after "public:" ought to
get a special syntactical symbol, say "protection-clause-intro", somewhat
analagous to "statement-block-intro", the first statement inside a
compound statement (brace block).  Maybe I'll fix this sometime.

(If the previous paragraph doesn't make sense, either ignore it or look
it all up on page "Syntactic Symbols" in the CC Mode manual.)

However, CC Mode is flexible enough for workarounds to be possible.  So,
for the meantime, here's a workaround.  Put the file below somewhere
convenient (I've called it ~/duerner.el; you probably want to byte
compile it), and add this line to your .emacs:

   (eval-after-load "cc-mode" '(load-file "~/duerner.el))
[ or ................................................elc)) ]

# 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 access
specifier, e.g.:

    public
        A();        <========== extra level of indentation.

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.


> regards,
> Marc

Sincerely,

-- 
Alan Mackenzie (Nürnberg).




  reply	other threads:[~2009-09-18 22:12 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-09-14 14:27 C++ Indentation and access-labels Marc Dürner
2009-09-18 22:12 ` Alan Mackenzie [this message]
     [not found]   ` <52a7da0f0909210708x21945b5ej557d4cd8b77a17cf@mail.gmail.com>
     [not found]     ` <20090925082833.GA2596@muc.de>
2009-09-28 16:39       ` Marc Dürner
  -- strict thread matches above, loose matches on Subject: below --
2009-09-18 10:32 Marc Dürner

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=20090918221218.GA5781@muc.de \
    --to=acm@muc.de \
    --cc=help-gnu-emacs@gnu.org \
    --cc=marc.duerner@gmail.com \
    /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.