Vim syntax highlight length-delimited fields

vim

I'm trying to write a vim syntax file for SVN dump files. The part I can't figure out is the section like this:

Fooprop: Val1
Text-content-length: 20
Barprop: Val2
                                       <- Blank line
abcdefghi
abcdefghi

Next-item-prop: Val3

How do I set up a syntax rule that says "N characters after the first blank line following 'Text-content-length: N' where N is a number"?

Best Answer

You would need to reference the text length in a different regular expression. Because Vim (unlike Perl) doesn't allow the evaluation of Vimscript expressions inside regular expressions, you need to resort to meta programming, i.e. build the syntax rules on the fly:

:execute 'syntax match svndumpData /^\n\_.\{' . length . '}/'

This simple sketch uses \_. to match any character (including newlines) for \{N} times. You need to be careful about the length of the newline (CR-LF vs. LF) and encodings (number of characters is not necessarily equal to the number of bytes to represent them!)

This works because a syntax plugin is sourced on each detection, and the syntax then applies to the current buffer.

Alternative

Since this is rather complex, and may not work if you have a byte count (for which no regular expression atom exists) instead of character length, there's always the pragmatic 80% solution that ignores the contained character count and just defines the syntax region on the surrounding property sections. It won't be correct in all cases, but probably works well for most data.

Related Question