Tuesday, January 8, 2013

Re: help on syntax file for fixed-width record files

Dan,

Your solution isn't working because you're tryting to match 3 items
starting with "^A". When there are several match or region patterns
only the one defined last will match (look :help syn-priority),
therefore ARecord_RecordType matches for you. Once again, when
something matches in current position, this segment normally isn't
tried for the matches with other syntax elements.

Have a look at :help syn-nextgroup.

:syntax match ARecord_RecordType "^A" nextgroup=ARecord_RecordCount
:syntax match ARecord_RecordCount ".\{9}" nextgroup=ARecord_OriginatorID
:syntax match ARecord_OriginatorID ".\{10}"

:syntax match CRecord_RecordType "^C" nextgroup=CRecord_RecordCount
:syntax match CRecord_RecordCount ".\{9}" nextgroup=CRecord_OriginatorID
:syntax match CRecord_OriginatorID ".\{14}"

:highlight link ARecord_RecordType RecordType
:highlight link CRecord_RecordType RecordType

:highlight link ARecord_RecordCount RecordCount
:highlight link CRecord_RecordCount RecordCount

:highlight link ARecord_RecordOriginatorID RecordOriginatorID
:highlight link CRecord_RecordOriginatorID RecordOriginatorID

:highlight RecordType ctermfg=blue
:highlight RecordCount ctermfg=red
:highlight RecordOriginatorID ctermfg=yellow

etc.

I suggest reading :help usr_44.txt to better understand syntax basics.
Also there is a good habbit of prepending a language suffix to all
names of your syntactical groups like the following
langARecord_RecordType.

Regards,
Boris

On Tue, Jan 8, 2013 at 8:33 AM, Vlad Irnov <vlad.irnov@gmail.com> wrote:
> On 1/7/13, Dan Wierenga <dwierenga@gmail.com> wrote:
>> I'm trying to create a syntax file for a file containing fixed-width
>> records [1], so that whenever I edit a file, the records are highlighted so
>> that each field is easily distinguishable by color.
>>
>> My file looks like this:
>>
>> A000000001IMFOO23456
>> C000000002IMFOO23456
>> Z000000003IMFOO23456
>>
>>
>> The file has several record types. A record type is declared by the first
>> character of the line i.e. A, C, Z, etc. The lines are 1464 characters
>> long (!) so I included just the start of them for brevity. Each record
>> contains different elements, i.e. an A record has an element from position
>> 2 through 10, 11 through 20, etc., but a C Record has elements from 2
>> through 10 and 11 through 24. So, the syntax file has to account for the
>> first character of each line.
>> I'm starting on the A records, and here's what I have so far:
>>
>>
>> :syntax region ARecord_OriginatorID start="^A" end=".{20}"
>> :syntax region ARecord_RecordCount start="^A" end=".{10}"
>> :syntax match ARecord_RecordType "^A" oneline
>>
>>
>>
>> :hi ARecord_RecordOriginatorID ctermfg=yellow
>> :hi ARecord_RecordCount ctermfg=red
>> :hi ARecord_RecordType ctermfg=blue
>>
>>
>> However, this results in only the first character (i.e. "A") being blue.
>> My goal is that the A is blue, the next nine characters at positions 2
>> through 10 are red, and the next 10 characters are yellow.
>
>
> syntax match ARecord_RecordType /^A/ contains=ALL
> syntax match ARecord_RecordCount /^A.\{9}/ contains=ALL
> syntax match ARecord_RecordOriginatorID /^A.\{19}/ contains=ALL
>
> hi ARecord_RecordType ctermfg=blue
> hi ARecord_RecordCount ctermfg=red
> hi ARecord_RecordOriginatorID ctermfg=yellow
>
>
> HTH,
> Vlad
>
> --
> 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

--
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: