6

I have a language which uses statement like

WHILE ... DO
..
END_WHILE

I can do the following in the corresponding synthax file

syn keyword LangKeys WHILE DO END_WHILE
syn region  LangBlock start="WHILE" end="END_WHILE" fold transparent

which allows me to highlight correctly the different keywords as well as defining a given region.

But now, I would like to be sure that the DO is only highlighted in the context of the loop. But I don't want to affect anything else. I tried

syn keyword LangKeys WHILE END_WHILE
syn keyword InsideKeys DO contained
syn region LangBlock start="WHILE" end="END_WHILE" fold transparent contained=InsideKeys

also without the transparent option. I tried a few variations of that. But it does not produce the expected result.

How can I get the highlight to be applied only when the keywords are within a given context?

Alternatively, is there a better way to handle that?

2
  • Which language is this? Perhaps there's already a syntax file floating around somewhere? Commented Nov 9, 2015 at 13:48
  • In this case, it is the Structure Control Language (SCL) for Siemens S7 PLC. But I have met a similar problem, for example for modelica. Commented Nov 9, 2015 at 13:55

2 Answers 2

1

The difference isn't so large, but the following seems to be working

syn keyword LangKeys   contained WHILE END_WHILE
syn keyword InsideKeys contained DO
syn region  LangBlock  fold transparent 
        \ matchgroup=LangKeys 
        \ start="\<WHILE\_s" end="\<END_WHILE\>" 
        \ contains=InsideKeys
hi def link LangKeys   Statement
hi def link InsideKeys Statement

That way, a DO inside the WHILE group will be highlighted but not separated.

2
  • The keyword LangKeys part should not be required. Commented Nov 11, 2015 at 15:22
  • @Vitor, why not? I use them as matchgroup, and in my tests, without it, you would have DO highlighted, but WHILE and ENd_WHILE would take on the transparent feature... Commented Nov 12, 2015 at 6:11
1
syn region LangBlock matchgroup=LangKeys start="\<WHILE\>" end="\<END_WHILE\>" fold transparent contains=InsideKeys
syn keyword InsideKeys DO contained

Things that I changed in the syntax code you provided:

  1. The \< \> are required to only match words, such that WHILE is not found inside END_WHILE.
  2. The matchgroup argument sets the syntax group name to be used in the match start/end words.
  3. Remove the keywords for WHILE and END_WHILE, because the highlight is done by the respective syn-match command.

Obviously, both LangKeys and InsideKeys syntax groups need to be defined using the highlight command with something like the following:

highlight link LangKeys   Statement
highlight link InsideKeys Statement

For more information please check:

:help syn-match
:help hi-link
8
  • You have LangKeys in your matchgroup, where do you define them? Commented Nov 12, 2015 at 12:35
  • The original question does not include highlight commands and it is said that it works, so I assume that there is no doubt regarding that part. Commented Nov 12, 2015 at 12:56
  • I don't think you understand what I want to say. Indeed the question does not specify highlighting. Nevertheless, in your example, you have syn region LangBlock matchgroup=LangKeys .... I might be mistaken, but I think that this calls for another syn line with LangKeys defined. Doesn't it? Commented Nov 12, 2015 at 13:01
  • It doesn't and I explain that in my answer in the second point. Commented Nov 12, 2015 at 13:04
  • No, you don't really explain it. At least not clearly enough. So you could have matchgroup=FooBar instead, it would just work the same? Commented Nov 12, 2015 at 13:08

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.