" Vim syntax highlighting for Klammertext (.kt and .k files).
" The Vim counterpart of doc/edit/emacs/klammertext-mode.el's highlighting
" and doc/edit/sublime/Klammertext.sublime-syntax.
"
" What it highlights (the same token classes as the other editors):
"
"   Text removal (#):
"     #  ...            remove to end of line   (marker + removed text)
"     ## ...            remove to end of file   (marker + removed text)
"     #[ ... ]#         remove enclosed text, nestable (markers + removed)
"     #- #+ #/          whitespace operators: NOT removals, left unhighlighted
"
"   Klammer applications (@),  definitions (@@),  system commands (@@@):
"     @name  @@name  @@@name        opening  (@ and name are one unit)
"     name@  name@@  name@@@        named closing
"     @      @@      @@@            bare closing
"
"   Escapes:  ^@ ^# ^| ^^  — the caret makes the next character literal; the
"     two characters are consumed as one (unhighlighted) unit, so the escaped
"     character is never read as a delimiter.  A run of carets pairs
"     left-to-right, reproducing the language's parity rule.
"
"   Literal klammers:  @code ... code@ and @c ... c@ — the interior is verbatim (no # or @
"     interpreted).  SYNC: the literal-klammer set's source of truth is
"     LITERAL_KLAMMERS in doc/edit/shared/klammertext_edit.py; a static
"     syntax file cannot read it, so when you add a literal klammer 'foo',
"     copy the klammertextVerbatim region below with code -> foo (and mirror
"     it in the Emacs, Sublime, and VS Code artifacts; all are seeded with
"     'code' and 'c').
"
" How open vs. close is decided (the same rule as every other integration):
" a delimiter whose NAME follows the @-run (@name) is an OPENING; a bare
" @-run, or one whose NAME precedes it (name@), is a CLOSING.  The
" look-ahead \%(\w\|@\)\@! on every closing keeps 'foo@bar' correct: that @
" is followed by a name, so it opens @bar and 'foo' stays plain text.  The
" abbreviated @name-arg form colors only @name (the name ends at the first
" hyphen), exactly like the other editors.
"
" Colors come from the shared Klammertext palette
" (notes/klammertext_palette.md in the development tree): application blue,
" definition green, system orange; each opening bright and its close the
" same hue darker; full intensity on dark backgrounds, deepened (0.60x) on
" light.  All groups are `hi def`, so :highlight in your vimrc overrides.
" Delimiter matching (jump + live highlight) is not a tokenizer concern —
" it lives in the ftplugin/autoload files.

if exists("b:current_syntax")
  finish
endif

" --- escapes: ^X makes X literal; consumed so # / @ are not delimiters ----
" (Defined first; it wins by the earlier-start rule, since the ^ precedes.)
syn match klammertextEscape /\^./

" --- text removal (#) -----------------------------------------------------
" Order matters: at the same start position, the LAST defined item wins.
syn match klammertextRemovedLine /#.*$/ contains=klammertextMarkerLine
syn match klammertextMarkerLine /#/ contained
" whitespace operators #- #+N #/N : not removals, left unhighlighted
syn match klammertextWhitespaceOp "#[-+/]\d*"
syn region klammertextRemovedBlock matchgroup=klammertextMarker start=/#\[/ end=/\]#/ contains=klammertextRemovedBlock
syn region klammertextRemovedFile matchgroup=klammertextMarker start=/##/ end=/\%$/

" --- system / target commands  @@@ ----------------------------------------
syn match klammertextSysOpen  /@\@1<!@@@\w\+/
syn match klammertextSysClose /@\@1<!@@@\%(\w\|@\)\@!/
syn match klammertextSysClose /@\@1<!\w\+@@@\%(\w\|@\)\@!/

" --- klammer definitions  @@ ----------------------------------------------
syn match klammertextDefOpen  /@\@1<!@@\w\+/
syn match klammertextDefClose /@\@1<!@@\%(\w\|@\)\@!/
syn match klammertextDefClose /@\@1<!\w\+@@\%(\w\|@\)\@!/

" --- klammer applications  @ ----------------------------------------------
syn match klammertextAppOpen  /@\@1<!@\w\+/
syn match klammertextAppClose /@\@1<!@\%(\w\|@\)\@!/
syn match klammertextAppClose /@\@1<!\w\+@\%(\w\|@\)\@!/

" --- literal klammers: interior verbatim (seeded: @code and @c) -----------
" Defined AFTER the @-tier matches: in Vim, when several items match at the
" same position the LAST defined wins, and this region must beat the plain
" klammertextAppOpen match at '@code'.  (Sublime's tokenizer picks the FIRST
" listed rule — the opposite convention; don't copy that ordering here.)
syn region klammertextVerbatim matchgroup=klammertextAppOpen start=/@\@1<!@code\%(\w\)\@!/ matchgroup=klammertextAppClose end=/code@/
" @c is the inline form of @code (literal since 2026-08-16).  The \w\@1<!
" guard on the close keeps a word ending in c ("basic@") from ending the
" region — essential for a single-letter name.
syn region klammertextVerbatim matchgroup=klammertextAppOpen start=/@\@1<!@c\%(\w\)\@!/ matchgroup=klammertextAppClose end=/\w\@1<!c@/

" --- colors ---------------------------------------------------------------
" The shared palette, dark and light values (see the header).  cterm values
" are the nearest xterm-256 approximations.
if &background ==# 'light'
  hi def klammertextAppOpen  guifg=#528599 ctermfg=66
  hi def klammertextAppClose guifg=#426a7a ctermfg=60
  hi def klammertextDefOpen  guifg=#758b55 ctermfg=101
  hi def klammertextDefClose guifg=#5e7044 ctermfg=101
  hi def klammertextSysOpen  guifg=#996743 ctermfg=94
  hi def klammertextSysClose guifg=#7a5236 ctermfg=94
  hi def klammertextMarker   guifg=#994040 ctermfg=131
  hi def klammertextRemoved  guifg=#9a9a9a ctermfg=247
else
  hi def klammertextAppOpen  guifg=#89ddff ctermfg=117
  hi def klammertextAppClose guifg=#6eb1cc ctermfg=74
  hi def klammertextDefOpen  guifg=#c3e88d ctermfg=150
  hi def klammertextDefClose guifg=#9cba71 ctermfg=107
  hi def klammertextSysOpen  guifg=#ffab70 ctermfg=216
  hi def klammertextSysClose guifg=#cc895a ctermfg=173
  hi def klammertextMarker   guifg=#ff6b6b ctermfg=210
  hi def klammertextRemoved  guifg=#8a8272 ctermfg=101
endif
hi def link klammertextMarkerLine   klammertextMarker
hi def link klammertextRemovedLine  klammertextRemoved
hi def link klammertextRemovedBlock klammertextRemoved
hi def link klammertextRemovedFile  klammertextRemoved

let b:current_syntax = "klammertext"
