Objective-C++ syntax files
- Hi. I'd like to solicit some advice from you ObjC programmers out there.
I've been doing a little work to get syntax highlighting working for
ObjC++ files (ObjC++ is Apple's way of letting C++ programmers use
their ObjC API). For the most part, this is pretty straightforward;
however, there are some nitty-gritty details that require a little care.
The main issue is that some keywords in ObjC++ are context-sensitive.
For instance, "class" is obviously a C++ keyword, but it can (and is)
also used as a method name in ObjC classes (for instance, NSObject uses
it as such). Technically, "class" shouldn't be highlighted when it is
used as a method name.
The following is my attempt at a solution (with some much appreciated
guidance from Bram):
" Vim syntax file
" Language: ObjC++
" Maintainer: ?
" Last change: 2003 January 28
" For version 5.x: Clear all syntax items
" For version 6.x: Quit when a syntax file was already loaded
if version < 600
" Read in C++ and ObjC syntax files
if version < 600
" If version < 508, we can't override old highlights,
" so we give up
if version >= 508
syn region objCFunc start="^[\t\s]*[-+]" end="$"
syn keyword objCppNonStructure class contained
let b:current_syntax = "objcpp"
The effect of this is to source both the cpp.vim and objc.vim syntax
files (NOTE: I take care in the ObjC syntax file to ensure that c.vim
only gets sourced once) and then do some tweaking to clear up the
"class" keyword problem.
I've invented the term objCppNonStructure to indicate that "class" is
in a context where it is not actually a keyword. I also declare the
region objCFunc (the definition or declaration of a method) as just
such a context.
This actually works amazingly well (as far as I can tell). Aside from
just method declarations, we also get stuff like
for free (the first because it is contained in the bracketed region,
and the latter because it's contained in the quoted region).
There are some other ObjC++ keywords that are also case-sensitive
("new", "delete", and "template", for instance), but they don't seem as
much of a problem (anyone using "template" in an objC class is asking
Does this look like a decent syntax file? Can anyone see any special
cases that should be dealt with? I'd appreciate any input you have.