Embedded C code in Ruby strings

Jonathan Paisley jp-www at dcs.gla.ac.uk
Mon Nov 15 08:55:00 EST 2004


Hi,

I've been playing with RubyInline recently, and have been using here docs 
for the embedded C code. e.g.,

class SomeClass
   inline do |builder|
     builder.c <<-EOC
       int min(int x,int y) { return x < y ? x : y; }
     EOC
   end
end

I wanted to highlight in vim the embedded C code as C, so I added extra 
definitions for the ruby here doc which match against the tag 'EOC' 
(end-of-C).

I'd be interested to hear other people's comments on what the best way to 
do this is, and whether some support could be added to the standard 
distribution.


Below is the added code (in ~/.vim/after/syntax/ruby.vim). I just copied 
the existing here doc regions, renamed them and specified the explicit 
string 'EOC'. Hopefully it won't get horribly wrapped!

====

let s:save_syntax = b:current_syntax
unlet b:current_syntax
syntax include @rubyC syntax/c.vim
let b:current_syntax = s:save_syntax

syn region rubyCString matchgroup=rubyStringDelimiter start=+\%(\%(class\s*\|\%(\.\|::\)\)\_s*\)\@<!<<\z(EOC\)\ze\s*$+hs=s+2 end=+^\z1$+ contains=@rubyStringSpecial fold

syn region rubyCString matchgroup=rubyStringDelimiter start=+\%(\%(class\s*\|\%(\.\|::\)\)\_s*\)\@<!<<"\z(EOC\)"\ze\s*$+hs=s+2  end=+^\z1$+ contains=@rubyStringSpecial fold

syn region rubyCString matchgroup=rubyStringDelimiter start=+\%(\%(class\s*\|\%(\.\|::\)\)\_s*\)\@<!<<'\z(EOC\)'\ze\s*$+hs=s+2  end=+^\z1$+ contains=@rubyC fold

syn region rubyCString matchgroup=rubyStringDelimiter start=+\%(\%(class\s*\|\%(\.\|::\)\)\_s*\)\@<!<<`\z(EOC\)`\ze\s*$+hs=s+2  end=+^\z1$+ contains=@rubyStringSpecial, at rubyC fold


syn region rubyCString matchgroup=rubyStringDelimiter start=+\%(\%(class\s*\|\%(\.\|::\)\)\_s*\)\@<!<<-\z(EOC\)\ze\s*$+hs=s+3 end=+^\s*\zs\z1$+ contains=@rubyStringSpecial, at rubyC fold

syn region rubyCString matchgroup=rubyStringDelimiter start=+\%(\%(class\s*\|\%(\.\|::\)\)\_s*\)\@<!<<-"\z(EOC\)"\ze\s*$+hs=s+3  end=+^\s*\zs\z1$+ contains=@rubyStringSpecial, at rubyC fold

syn region rubyCString matchgroup=rubyStringDelimiter start=+\%(\%(class\s*\|\%(\.\|::\)\)\_s*\)\@<!<<-'\z(EOC\)'\ze\s*$+hs=s+3  end=+^\s*\zs\z1$+ contains=@rubyC fold

syn region rubyCString matchgroup=rubyStringDelimiter start=+\%(\%(class\s*\|\%(\.\|::\)\)\_s*\)\@<!<<-`\z(EOC\)`\ze\s*$+hs=s+3  end=+^\s*\zs\z1$+ contains=@rubyStringSpecial, at rubyC fold






More information about the vim-ruby-devel mailing list