Thursday, September 16, 2010

Re: problems with 'syntax include' for nested Perl and R code regions

This is a normal text file, except it has included code regions.

src-Perl from somewhere
sub asdf {
my ($var, $param) = @_;
print "Got $var and $param\n";
}
end-Perl

This is back to regular text?

src-Python
class y(Object):
def foo(self, x=None):
if x:
print "Got %s" % (x)
end-Python

Some comments about the above Python. (It sucks.)

src-R
library(stats)
tocor <- data.frame(read.table("some-data.tab"))
attach(tocor)
normalcor <- cor(V2,V3)
end-R

What's that R doing?

src-XML
<?xml version="1.0" encoding="UTF-8"?>
<some>
<x:test data="&amp;ersand"/>
<y:a><![CDATA[
foo <> &
]]></y:a>
</some>
end-XML

PHP syntax highlighting handles embedding well (since it already has to handle
PHP-within-HTML).

src-PHP
<?php
function something($a, $var) {
echo "Got a variable? $a and $var\n";
}
$sql = <<<SQL
select id, max(dimension) as maxdim
from nonexistent
where dimension > 20
SQL;
?><html>
<head><title>A PHP page</title></head>
<body><p>In a document! <?= something(1,2) ?></p></body>
</html>
end-PHP

" vim:set ft=combined ts=3 sts=3 sw=3 et:
let s:langs = [ 'Perl', 'Python', 'R', 'PHP', 'XML' ]
let s:lower = map(copy(s:langs), 'tolower(v:val)')

" load the languages to include
for l in s:lower
unlet! b:current_syntax
exe "syn include @inc-".l." syntax/".l.".vim"
endfor

" set up the src-Lang/end-Lang regions
for L in s:langs
let l = tolower(L)
exe "syn region org".L." matchgroup=OrgLang"
\ "start=/^src-".L.".*$/ end=/^end-".L.".*$/"
\ "contains=@inc-".l
\ "fold"
endfor

hi def link OrgLang Comment

let b:current_syntax = 'combined'
On Wed, 15 Sep 2010, hsitz wrote:

> On Sep 15, 7:16 pm, hsitz wrote:
>>
>> 1.  The code in my Python region highlights perfectly.
>> 2.  The code in my Perl region highlights properly, but text in the
>> main document after the Perl region has some Perl highlighting
>> applied (mostly pink or magena, I think, I forget which syntax
>> element).
>> 3.  The code in my R region doesn't get highlighted at all, although
>> it gets highlighted fine when it's in its own separate 'rcode.r'
>> document.
>>
>> Are these bugs, limitations, or am I doing something wrong?  Do the
>> syntax files themselves have to be written a certain way to be
>> compatible with 'syntax include'?
>>
>
> In playing a bit more, it seems my problem with R may be that only one
> 'syntax include' is allowed at a time. When the R 'syntax include' is
> the only one in my main syntax file the nested R region gets
> highlighted properly. When the R 'syntax include' is the second
> 'syntax include' after the Python one, then only the Python nested
> region gets highlighted. So it seems only the first 'syntax include'
> is processed or recognized. Does that sound right? Only one nested
> language can be highlighted at a time?

That's the gist -- most syntax files are designed to be "main" syntax
files. But, it's not too hard to work around. Most of them set a
'b:current_syntax' variable to prevent other syntax files from being
loaded. So, you can just 'unlet' it.

==> ~/.vim/syntax/combined.vim <==

syn include @inc-perl syntax/perl.vim
unlet b:current_syntax

syn include @inc-python syntax/python.vim
unlet b:current_syntax

syn include @inc-r syntax/r.vim
unlet b:current_syntax

syn region orgPerl matchgroup=OrgLang
\ start=/^src-Perl.*$/ end=/^end-Perl.*$/
\ contains=@inc-perl
\ fold

syn region orgPython matchgroup=OrgLang
\ start=/^src-Python.*$/ end=/^end-Python.*$/
\ contains=@inc-python
\ fold

syn region orgR matchgroup=OrgLang
\ start=/^src-R.*$/ end=/^end-R.*$/
\ contains=@inc-r
\ fold

hi link OrgLang Comment

let b:current_syntax = 'combined'
==================================


But that's an awful lot of repeated code. Adding a few syntax types,
and only adding the languages in an array:

==> ~/.vim/syntax/combined.vim <== (attached)
let s:langs = [ 'Perl', 'Python', 'R', 'PHP', 'XML' ]
let s:lower = map(copy(s:langs), 'tolower(v:val)')

" load the languages to include
for l in s:lower
unlet! b:current_syntax
exe "syn include @inc-".l." syntax/".l.".vim"
endfor

" set up the src-Lang/end-Lang regions
for L in s:langs
let l = tolower(L)
exe "syn region org".L." matchgroup=OrgLang"
\ "start=/^src-".L.".*$/ end=/^end-".L.".*$/"
\ "contains=@inc-".l
\ "fold"
endfor

hi def link OrgLang Comment

let b:current_syntax = 'combined'
==================================

Works well for me on the attached testfile.combined.


> Still have the problem with the Perl highlighting continuing in
> non-Perl text down beyond the Perl region.

I'm not seeing extra highlighting. But I am seeing bizarre colors. The
'src-R' and 'src-Perl' sections in particular. Not sure what's up with
that. Perhaps someone else will comment.

--
Best,
Ben

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments:

Post a Comment