4

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"?

  • Vim has no generic concept of that. Vim can highlight keywords, patterns and stuff between two patterns. You are aiming for context sensitive highlighting rules, for patterns that adapt when matching. I never hacked vim, but theoretically you can get what you want by making vim first parse the file, then create specialized highlighting rules from a template and finally have vim use the specialized rules. – Bananguin May 10 '13 at 19:34
2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.