Vim Syntax Highlighting for Mozilla C++ Files
By Kazé on Thursday, August 11 2011, 17:50 - Permalink
I’ve tweaked the cpp.vim file that comes with Vim 7.3 to highlight most Mozilla-specific keywords when working on the editor core. A lot of Mozilla-specific types and that can be added manually but the task gets bigger when it comes to nsI* interfaces or NS_* macros…
Most nsI* interfaces can be grabbed with find/grep/sed:
find src/mozilla -regex ".*\.\(idl\|h\)" -exec grep "^\(class\|interface\)\s*nsI" '{}' \; | sed 's/\(:\|;\|,\|{\).*$//' | sed 's/^.*nsI/nsI/' | sed 's/\s*$//' | sort -u
same thing for NS_ERROR* / NS_IMPL* macros and constants:
find src/mozilla -regex ".*\.\(idl\|h\)" -exec grep "^#define\s*NS_ERROR" '{}' \; | sed 's/^#define\s*//' | sed 's/\s.*$//' | sed 's/(.*$//' | sort -u
find src/mozilla -regex ".*\.\(idl\|h\)" -exec grep "^#define\s*NS_IMPL" '{}' \; | sed 's/^#define\s*//' | sed 's/\s.*$//' | sed 's/(.*$//' | sort -u
Here’s the resulting cpp.vim file including the ~900 Mozilla-specific lines (ouch!). Copy it to your ~/.vim/syntax/ directory and voilà, your C++ files should be much more colorful.
Now it’d be really great if:
- we had omni-completion for the nsI* interfaces instead of just the keywords;
- we had a similar file (keywords + omni-completion) for JavaScript — mostly for the DOM API
- this file could be generated automatically — say, with DXR;
- this file could be included in the Mozilla tree (e.g. a .vimrc file in the top source dir).
To all Vim fanboys among the Mozilla community: I’d love to get your input about that. Maybe we could start a “vim-moz-syntax” project on github or something?
EDIT: (2013-05-19)
- this work is now available on github: https://github.com/mozfr/mozilla.vim
- there’s been an article about this in Russian: http://softdroid.net/Vim-Syntax-Highlighting
Comments
It is even possible to use this new syntax for Mozilla C++ sources only, while keeping the default for other sources. It is a little more complicated, though:
1. Drop his script as $VIM/vimfiles/syntax/mozcpp.vim
2. Create three additional one-line scripts (I'm giving each file's path/name first, followed by its contents)
$VIM/vimfiles/ftplugin/mozcpp.vim
runtime! ftplugin/cpp.vim
$VIM/vimfiles/indent/mozcpp.vim
runtime! indent/cpp.vim
[the last one will vary depending on the location of your source clone: I'm assuming here that your clone(s) is/are at or below ~/.build/mozilla/, and I'm breaking the file's single line because otherwise it would be quite long]
$VIM/vimfiles/ftdetect/mozcpp.vim
autocmd BufRead,BufNewFile
\ ~/.build/mozilla/**/*.cpp,~/.build/mozilla/**/*.h
\ setlocal filetype=mozcpp
Create any directories which don't yet exist, and the $VIM environment variable might exist only in Vim or in programs run from within Vim. Typical values for it are /usr/local/share/vim (on Unix) or C:\Program Files\Vim (on Windows) but on your system it could be different — type ":echo $VIM" (without the quotes) in a running Vim to be sure.