0

Is vim's syntax highlighting limited by regexes or can you, for example, highlight [ words_and_maybe_keywords... [ words_and_maybe_keywords... ] words_and_maybe_keywords... ] all the same (e.g., green) all based on this region being demarcated by some outermost [/] pair?

IOW, can it handle regexes + depth?

If this is supported, how can I highlight such a nestable region?

5
  • 1
    Yes; help syntax, probably especially regions and contained
    – D. Ben Knoble
    Feb 28 at 17:38
  • @D.BenKnoble Thanks. That's encouraging. But shouldn't then there be syntax files utilizing that ability? I've tried looking at the syntax files for pascal and matlab, both of which, I'm told, have nestable comments, but the comments, when they have nested, aren't highlighted correctly for either language. :/ (I've added a pascal example to the question).
    – PSkocik
    Mar 1 at 22:38
  • Well, it works fine in other langs; perhaps those syntax rules are broken? Not even SE’s highlighter works too well on that example
    – D. Ben Knoble
    Mar 1 at 22:39
  • @D.BenKnoble I take it back contains=pascalTodo,pascalSpaceError,pascalComment does make it work, I was invoking it wrong. Thanks for the help, D. Ben Knoble!
    – PSkocik
    Mar 1 at 22:42
  • 1
    np; add an answer if you’ve solved your problem. And, for the future, maybe start with the extra details :)
    – D. Ben Knoble
    Mar 1 at 22:43
0

I've tried looking at a syntax file which might be using the feature: pascal.vim, because pascal, as I've learned, has nested comment sections. Unfortunately, the highlighting for the comment section is wrong when there's nesting in the comments.

Example hello_world.pas:

program HelloWorld(output);
begin
    Write('Hello, World!')
    {No ";" is required after the last statement of a block -
    { nested comment }
        adding one adds a "null statement" to the program, which is ignored by the compiler.}
end.

This compiles and runs, but the comment section with nesting is highlighted wrong, even here on the StackOverflow.

I found the relevant line in the syntax file:

syn region pascalComment    start="(\*\|{"  end="\*)\|}" contains=pascalTodo,pascalSpaceError

and just naively added ,pascalComment to the contains attribute` and it did make vim highlight the comment region correctly.

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.