Search code examples
vimvim-syntax-highlighting

vimscript syntax not pattern


I have this vimscript:

syn region      fdocCommand     start="^@" end="$" contained 
syn region      fdocComment     start="^@" end="^@tangle " contains=fdocCommand keepend
" syn region      fdocComment     start="^@" end="^@felix" contains=fdocCommand keepend

but what i need is the region defined by: start="^@" followed by anything except "tangle" or "felix" and ended by either "@tangle" or "@felix". A weaker form would actually do, either the @tangle line above OR the commented out @felix line.

The actual use is a literate programming tool with @commands like @h1 heading, @title, etc, and code is introduced by @felix or @tangle and ended by the next @command. As it happens my webserver can display this with syntax highlighting and hyperlinking, and the real set of code commands include @python, @ocaml, @c++ etc. For Felix, the @..@felix sequence is actually recognised by the compiler as a comment, so the literate code can be compiled directly.


Solution

  • To start a region only with @commands that are not @tangle, you need negative lookahead via \@!: ^@\%(tangle\)\@!. To ensure that it's really only tangle, not something starting with tangle, you need an additional $ assertion; and to not match felix, too, another branch: ^@\%(\%(tangle\|felix\)$\)\@!

    So, this should do:

    syn region      fdocComment     start="^@\%(\%(tangle\|felix\)$\)\@!" end="^@\%(tangle\|felix\)$" contains=fdocCommand keepend