Hugh Sasse wrote:
On Thu, 26 Oct 2006, A. S. Budden wrote:

On 26/10/06, Hugh Sasse <[EMAIL PROTECTED]> wrote:
[snip]
So, we run into a problem: how does one syntax highlight mixed
language code?
[snip]

I don't know whether this is of any use, but I have often found
situations where I need to embed code from one language in the comments
of another.  For example, I have Matlab code that scans through its own
source for some perl code, writes it to disk and then uses it to process
text files (as Perl is MUCH better at text file handling).  This saves

Interesting idea.

having multiple scripts for a single (relatively simple task).  In
.vim/after/syntax/matlab.vim, I have (three lines):

unlet! b:current_syntax
syn include @matlabPerlScript $VIMRUNTIME/syntax/perl.vim
syn region matlabPerlRegion matchgroup=matlabScriptDelim start=+^%
PERL\d*: +hs=s+8 end=+$+ [EMAIL PROTECTED]

OK, this seems to make sense to me from what I have read so far.
So any lines starting "% PERL: " (with an optional number after PERL)
are highlighted as perl code.  The number allows me to have multiple

matching to the end of the line. yes..

perl scripts embedded in a single Matlab script.  I have done similar
things for C and others.

Well, in my .vimrc I now have:

" To MetaProgram C using Ruby
function RubyMetaC()
 :unlet! b:current_syntax
 :syntax include @CSTUFF syntax/c.vim

Method I.

        syntax include @CSTUFF $VIMRUNTIME/syntax/c.vim

This doesn't pick any "local additions" to C syntax, nor does it pick the "local syntax script" if you've completely replaced the default C syntax script by something else placed earllier in 'runtimepath'.

Method II: if you want to search all 'vimruntime' directories

        if has("unix")
                silent! syn include @CSTUFF ~/.vim/syntax/c.vim
        else
                silent! syn include @CSTUFF ~/vimfiles/syntax/c.vim
        endif
        silent! syn include @CSTUFF $VIM/vimfiles/syntax/c.vim
        syn include @CSTUFF $VIMRUNTIME/syntax/c.vim
        silent! syn include @CSTUFF $VIM/vimfiles/after/syntax/c.vim
        if has("unix")
                silent! syn include @CSTUFF ~/.vim/after/syntax/c.vim
        else
                silent! syn include @CSTUFF ~/vimfiles/after/syntax/c.vim
        endif

Method III (recommended): "include" into your @CSTUFF cluster a file containing only the single line

        runtime! syntax/c.vim

This would be equivalent (with fewer keystrokes) to Method II above.

 :syntax region rubyC1 matchgroup=String start=+%Q{+ end=+}+ keepend [EMAIL 
PROTECTED]
 :syntax region rubyC2 matchgroup=String start=+%Q(+ end=+)+ keepend [EMAIL 
PROTECTED]
 :syntax region rubyC3 matchgroup=String start=+%Q<+ end=+>+ keepend [EMAIL 
PROTECTED]
 :syntax on
endfunction


The three :syntax region lines are needed to distinguish %Q<>, %Q() and %Q{} all of which are quoted strings in ruby. I could not figure out
how to use \z to let me find the matching {angle,round,curly} bracket
for the one I'd opened.  I suspect that is possible....

Without the syntax on line, the contents of the quoted string show up
as white, unhighlighted, when I do
:call RubyMetaC()

Adding the syntax on line just gave me Ruby string quoting, and no C highlighting at all. I was back at square 1.

Adding the :unlet! line made no difference at that stage.

I am now somewhat confused, but feel that I am nearer than I was.

I saw in the tip#856 (earlier in the thread) the use of :hi-link, but can't figure out what to link it to so that it picks up all
C code as C.
Hope that is of some interest,

Al

        Thank you
        Hugh



You can probably leave the C highlight groups alone. If you were creating a new language, let's say "foobar", you would define a number of syntax groups for your language, all of them with camel-case names starting "foobar", and then use

        hi default link foobarFunction Function
        hi default link foobarIdentifier Identifier
        hi default link foobarComment Comment

etc.


Best regards,
Tony.

Reply via email to