Metaprogramming, mixed languages, and Vim.

Hugh Sasse hgs at dmu.ac.uk
Tue Oct 31 15:05:48 EST 2006


Here is what I ended up using, thanks to "A.J.Mechelynck" and "A.
S. Budden" on the Vim list and Doug Kearns (what he called "a bit of
a '2 AM solution'" -- and it works fine!) on the Vim-Ruby list.  The
first function highlights the %Q{....} quotes in Ruby as C code, the
second does it for here documents of the form

<<-CSTRING
   ...
CSTRING

Here they are:

" To MetaProgram C using Ruby
function RubyMetaC1()
  :unlet! b:current_syntax
  :syntax include @CSTUFF ~/.vimrc_include_c_syn
  :syntax region rubyC1 matchgroup=String start=+%Q{+ end=+}+ keepend contains=@CSTUFF
  :syntax region rubyC2 matchgroup=String start=+%Q(+ end=+)+ keepend contains=@CSTUFF
  :syntax region rubyC3 matchgroup=String start=+%Q<+ end=+>+ keepend contains=@CSTUFF
"  :syntax on
endfunction
function RubyMetaC2()
  unlet b:current_syntax
  syn include @cTop syntax/c.vim
  let b:current_syntax = "ruby"

  syn region rubyCString start=+\%(\%(class\s*\|\%(\.\|::\)\)\_s*\)\@<!<<CSTRING\ze+hs=s+2    matchgroup=rubyStringDelimiter end=+^CSTRING$+ contains=rubyHeredocStart, at rubyStringSpecial, at cTop fold keepend
  syn region rubyCString start=+\%(\%(class\s*\|\%(\.\|::\)\)\_s*\)\@<!<<"CSTRING"\ze+hs=s+2  matchgroup=rubyStringDelimiter end=+^CSTRING$+ contains=rubyHeredocStart, at rubyStringSpecial, at cTop fold keepend
  syn region rubyCString start=+\%(\%(class\s*\|\%(\.\|::\)\)\_s*\)\@<!<<'CSTRING'\ze+hs=s+2  matchgroup=rubyStringDelimiter end=+^CSTRING$+ contains=rubyHeredocStart, at cTop		      fold keepend
  syn region rubyCString start=+\%(\%(class\s*\|\%(\.\|::\)\)\_s*\)\@<!<<`CSTRING`\ze+hs=s+2  matchgroup=rubyStringDelimiter end=+^CSTRING$+ contains=rubyHeredocStart, at rubyStringSpecial, at cTop fold keepend

  syn region rubyCString start=+\%(\%(class\s*\|\%(\.\|::\)\)\_s*\)\@<!<<-CSTRING\ze+hs=s+3    matchgroup=rubyStringDelimiter end=+^\s*\zsCSTRING$+ contains=rubyHeredocStart, at rubyStringSpecial, at cTop fold keepend
  syn region rubyCString start=+\%(\%(class\s*\|\%(\.\|::\)\)\_s*\)\@<!<<-"CSTRING"\ze+hs=s+3  matchgroup=rubyStringDelimiter end=+^\s*\zsCSTRING$+ contains=rubyHeredocStart, at rubyStringSpecial, at cTop fold keepend
  syn region rubyCString start=+\%(\%(class\s*\|\%(\.\|::\)\)\_s*\)\@<!<<-'CSTRING'\ze+hs=s+3  matchgroup=rubyStringDelimiter end=+^\s*\zsCSTRING$+ contains=rubyHeredocStart, at cTop		     fold keepend
  syn region rubyCString start=+\%(\%(class\s*\|\%(\.\|::\)\)\_s*\)\@<!<<-`CSTRING`\ze+hs=s+3  matchgroup=rubyStringDelimiter end=+^\s*\zsCSTRING$+ contains=rubyHeredocStart, at rubyStringSpecial, at cTop fold keepend

  syn cluster cCommentGroup contains=cTodo,rubyInterpolation
endfunction


and ~/.vimrc_include_c_syn is just:

runtime! syntax/c.vim

These seem to do much the same thing, i.e, they seem to handle C
equally well, so I may be able to recombine them, but it works for
me at the moment so I am content. And there may be subtleties in
behaviour I have so far not encountered. 

I have them as functions so I can :call them as needed.
I suspect the first may be easy to parameterize for other languages,
but for now I'll leave that for those who enjoy exploring such matters.

Thank you all again, this was way above my present skill level.
        Hugh


More information about the vim-ruby-devel mailing list