unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Adding keywords to cc-mode
@ 2006-06-24  3:22 Le Wang
  2006-06-24  4:37 ` Le Wang
  0 siblings, 1 reply; 5+ messages in thread
From: Le Wang @ 2006-06-24  3:22 UTC (permalink / raw)


Hi,

I'd like to get the following to indent properly somehow.

    for each (i in collection) {
        // do stuff
    };

The "for each" syntax is valid in c++/cli.  I'm hopefully lost after
looking at cc-*.el and the various modes based on it, csharp-mode, etc.

I've tried adding this to my mode:

(c-lang-defconst c-block-stmt-2-kwds
  c++cli '("for" "if" "switch" "while" "catch" "for each"
	   "checked" "unchecked" "lock"))

This gets the font-locking right, but everything after "{" is still
doubly indented.  Can someone point me to some resources?

Thanks.

--
Le

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

* Re: Adding keywords to cc-mode
  2006-06-24  3:22 Adding keywords to cc-mode Le Wang
@ 2006-06-24  4:37 ` Le Wang
  0 siblings, 0 replies; 5+ messages in thread
From: Le Wang @ 2006-06-24  4:37 UTC (permalink / raw)


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

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

* Adding keywords to cc-mode
@ 2007-06-13 16:02 Stefan Arentz
       [not found] ` <mailman.2114.1181760059.32220.help-gnu-emacs@gnu.org>
  2007-06-13 20:06 ` Alan Mackenzie
  0 siblings, 2 replies; 5+ messages in thread
From: Stefan Arentz @ 2007-06-13 16:02 UTC (permalink / raw)
  To: help-gnu-emacs


(I'm on Emacs 22.1)

Is it possible to add new keywords to cc-mode for highlighting? I'm working on
some simple 'Core Foundation' code for OS X and this C/C++ code typically has
types with names like CFSomethingRef or kCFFooConstant. Is there an easy way
to add regular expressions to cc-mode to recognize these and highlight them in
a specific color?

 S.

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

* Re: Adding keywords to cc-mode
       [not found] ` <mailman.2114.1181760059.32220.help-gnu-emacs@gnu.org>
@ 2007-06-13 20:03   ` Stefan Arentz
  0 siblings, 0 replies; 5+ messages in thread
From: Stefan Arentz @ 2007-06-13 20:03 UTC (permalink / raw)
  To: help-gnu-emacs

Alan Mackenzie <acm@muc.de> writes:

> Hi, Stefan!
> 
> On Wed, Jun 13, 2007 at 06:02:33PM +0200, Stefan Arentz wrote:
> > 
> > (I'm on Emacs 22.1)
>  
> Good Man!
>  
> > Is it possible to add new keywords to cc-mode for highlighting? I'm working on
> > some simple 'Core Foundation' code for OS X and this C/C++ code typically has
> > types with names like CFSomethingRef or kCFFooConstant. Is there an easy way
> > to add regular expressions to cc-mode to recognize these and highlight them in
> > a specific color?
> 
> As long as "specific color" means "font-lock-type-face", then yes.  Set
> one or both of the variables called c-font-lock-extra-types,
> c++-font-lock-extra-types to a regexp to recognise the type.  Something
> like this (untestested):
> 
>    (setq c-font-lock-extra-types
>     (concat "\_<"
>      (regexp-opt '("CFSomethingRef" "kCFFooConstant") t)
>      "\_>"))
> 
> should do the trick.  It's documented on page "Font Locking
> Preliminaries" of the (new) CC Mode manual.

Got it. That regexp-opt trick is nice. How predictable is that? If I
give it CFFooRef and CFBarRef is it then smart enough to make CF*Ref
match or wil it just match what you give it as an example?

There is no way to have a difference face for these extra types?

 S.

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

* Re: Adding keywords to cc-mode
  2007-06-13 16:02 Stefan Arentz
       [not found] ` <mailman.2114.1181760059.32220.help-gnu-emacs@gnu.org>
@ 2007-06-13 20:06 ` Alan Mackenzie
  1 sibling, 0 replies; 5+ messages in thread
From: Alan Mackenzie @ 2007-06-13 20:06 UTC (permalink / raw)
  To: Stefan Arentz; +Cc: help-gnu-emacs

Hi, Stefan!

On Wed, Jun 13, 2007 at 06:02:33PM +0200, Stefan Arentz wrote:
> 
> (I'm on Emacs 22.1)
 
Good Man!
 
> Is it possible to add new keywords to cc-mode for highlighting? I'm working on
> some simple 'Core Foundation' code for OS X and this C/C++ code typically has
> types with names like CFSomethingRef or kCFFooConstant. Is there an easy way
> to add regular expressions to cc-mode to recognize these and highlight them in
> a specific color?

As long as "specific color" means "font-lock-type-face", then yes.  Set
one or both of the variables called c-font-lock-extra-types,
c++-font-lock-extra-types to a regexp to recognise the type.  Something
like this (untestested):

   (setq c-font-lock-extra-types
    (concat "\_<"
     (regexp-opt '("CFSomethingRef" "kCFFooConstant") t)
     "\_>"))

should do the trick.  It's documented on page "Font Locking
Preliminaries" of the (new) CC Mode manual.

>  S.

-- 
Alan Mackenzie (Ittersbach, Germany).

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

end of thread, other threads:[~2007-06-13 20:06 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-06-24  3:22 Adding keywords to cc-mode Le Wang
2006-06-24  4:37 ` Le Wang
  -- strict thread matches above, loose matches on Subject: below --
2007-06-13 16:02 Stefan Arentz
     [not found] ` <mailman.2114.1181760059.32220.help-gnu-emacs@gnu.org>
2007-06-13 20:03   ` Stefan Arentz
2007-06-13 20:06 ` Alan Mackenzie

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).