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

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

Re: Adding keywords to cc-mode


From: Le Wang
Subject: Re: Adding keywords to cc-mode
Date: 23 Jun 2006 21:37:51 -0700
User-agent: G2/0.2

Le Wang wrote:
> Hi,
>
> I'd like to get the following to indent properly somehow.
>
>     for each (i in collection) {
>         // do stuff
>     };

The problem is that the "each" starts another statement (I don't know
if I'm using the right word), so another indentation level is added.
The solution would be to recognize "for each" as a group or something
similar.  I've come up with this hack that just decrements the indent:

<elisp>

(defvar le::for-each-regex
  "for\\(?:[ \t\n]\\)+each")

(defun le::for-each-intro-indent (langelem)
  (save-excursion
    (goto-char (cdr langelem))
    (if (looking-at for-each-regex)
        0
      '+)))

(defun le::for-each-close-indent (langelem)
  (save-excursion
    (goto-char (cdr langelem))
    (if (looking-at for-each-regex)
        '-
      0)))


(c-set-offset 'statement-block-intro 'le::for-each-intro-indent)
(c-set-offset 'block-close 'le::for-each-close-indent)

</elisp>

Add hook as appropriate for C++/CLI files.

Someone please still chime in with the "proper" solution.

--
Le



reply via email to

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