1

I'm writing a color syntax highlighting file for a derivative of Pick Basic. Two of the oddities that this language allows is periods in variable names and embedding a keyword in a variable name, surrounding by periods. So for example, AND is a keyword, but FOO.AND.BAR is a variable name. I'd like to prevent the AND in the middle of a variable name from being highlighted, but my attempts are failing.

I've defined my set of keywords as contained, but the container definition isn't working, and I'm not quite sure why.

Two different attempts (different keyword sets, but the same basic idea). The only difference in these two examples is that some builtin language functions contain periods (and thus need to be in the middle character class), while operators do not.

syn keyword universeBasicFunctionName  ABORTM ABS ABSS ACOS ADDS ALL ALPHA AM ANDS ARG. ASCII ASIN ASSIGN ASSIGNED ATAN AUTHORIZATION contained
syn match universeBasicFunction /\M\( \)\@<=\zs[A-Z\.]\+\ze\([ (]\)\@=/ contains=universeBasicFunctionName
syn keyword universeBasicOperatorName      AND EQ GT LT MATCH MATCHES NE NOT OFF ON OR contained
syn match universeBasicOperator /\(\.\)@<![A-Z]\+\(\.\)@!/ contains=universeBasicOperatorName
7
  • I am not sure I understand completely, but if you define your keywords using syn keyword I think they should not match inside FOO.AND.BAR . You can configure the syntax keyword definition using :syn iskeyword setting May 22 at 6:47
  • Yes, that's how I define the keywords. Here's the line for operators, which is much shorter: "syn keyword universeBasicOperator AND EQ GT LT MATCH MATCHES NE NOT OFF ON OR" But unfortunately, FOO.AND.BAR matches the AND unless I contain it. I believe this is because in many other languages (like Perl and PHP), a period is the concatenation operator.
    – Corydon76
    May 22 at 15:05
  • I think you need to use :syn iskeyword syn iskeyword @,48-57,_,192-255,. to make the . part of a keyword match. I'll post an answer soon May 22 at 15:30
  • When I add that to the syntax file, I get: "Error detected while processing /usr/share/vim/vim74/syntax/universebasic.vim: line 18: E410: Invalid :syntax subcommand: iskeyword"
    – Corydon76
    May 22 at 19:24
  • too old vim. Why are you using such an old Vim? May 22 at 19:59

1 Answer 1

0

You basically want to have the . be considered part of syntax keywords, so that AND won't be matched by the Keyword FOO.AND.BAR.

The proper way to configure this for syntax rules is to make use of the :syn iskeyword setting, in your case, to just add the . to it, simply put (at the top of your syntax rule):

syn iskeyword @,48-57,_,192-255,.

This uses the default 'iskeyword' setting (and adds the . at the end to it).

The syn-iskeyword command, only affects what is considered a keyword by the syntax engine (e.g. only for syn keyword foobar FOO BAR FOOBAR), it does not change anything related to actual matching using e.g. :syn match or :syn region commands).

I obtained the default for the syn-iskeyword option, by quering the 'iskeyword' option value using e.g. :echo &isk.

Note: Both settings are closely related (and in fact in older Vims, there did not exist the :syn iskeyword feature).

For syntax rules however, you should always use :syn iskeyword simply because the option 'iskeyword' affects a whole lot of other (possibly unrelated) things, like word movement and other Vim commands. And for that reason, the :syn iskeyword command was added later on, to de-couple setting this for syntax highlighting (in 7.4.1142)

4
  • Almost there. It needs to be: set iskeyword=@,48-57,_,192-255,.
    – Corydon76
    May 22 at 19:23
  • No, for the reasons mentioned above, you should not direclty modify the 'iskeyword' setting, because it affects too many other vim commands. That's why use :syn iskeyword May 22 at 19:58
  • When it's put in the syntax file for a particular language, shouldn't it only affect vim when that language is being used?
    – Corydon76
    May 23 at 12:06
  • yes and no. provided that you used setl iskeyword it will only affect that particular buffer. But still at the same time, it will have consequences for normal commands (like word movements), which might be annoying for the user. May 23 at 14:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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