" Klammertext filetype plugin: comment format, the structural commands, and
" (when Vim has +python3) fast in-process indentation and live delimiter
" match highlighting.  The implementations are in autoload/klammertext.vim;
" the algorithms themselves are the shared Python core
" (klammertext_edit.py — see doc/edit/README.md for how it is located).
"
" Commands (buffer-local):
"   :[range]KlammertextReindent   reindent the range (default: whole buffer)
"   :KlammertextAlign             align the @table enclosing the cursor
"   :KlammertextJumpToMatch       jump between a klammer application's
"                                 opening and closing delimiter
"   :KlammertextCheck             unbalanced/mismatched delimiters -> the
"                                 location list
"
" Default mappings (set g:klammertext_no_mappings to define none):
"   <LocalLeader>i   reindent the current line (visual: the selection)
"   <LocalLeader>a   align the enclosing table
"   <LocalLeader>j   jump to the matching delimiter
"
" Reindentation is EXPLICIT-ONLY: whitespace is content in Klammertext, so
" indentkeys is emptied and nothing reformats as a side effect of typing.
" With +python3 the ftplugin also sets 'indentexpr', so the = operator
" (e.g. ==, gg=G) reindents through the same shared implementation.

if exists("b:did_ftplugin")
  finish
endif
let b:did_ftplugin = 1

setlocal commentstring=#\ %s
setlocal comments=b:#
setlocal indentkeys=

command! -buffer -range=% KlammertextReindent
      \ call klammertext#Reindent(<line1>, <line2>)
command! -buffer KlammertextAlign call klammertext#AlignTable()
command! -buffer KlammertextJumpToMatch call klammertext#JumpToMatch()
command! -buffer KlammertextCheck call klammertext#Check()

if !exists("g:klammertext_no_mappings")
  nnoremap <buffer> <silent> <LocalLeader>i :.KlammertextReindent<CR>
  xnoremap <buffer> <silent> <LocalLeader>i :KlammertextReindent<CR>
  nnoremap <buffer> <silent> <LocalLeader>a :KlammertextAlign<CR>
  nnoremap <buffer> <silent> <LocalLeader>j :KlammertextJumpToMatch<CR>
endif

let b:undo_ftplugin = "setlocal commentstring< comments< indentkeys<"
      \ . " | delcommand KlammertextReindent"
      \ . " | delcommand KlammertextAlign"
      \ . " | delcommand KlammertextJumpToMatch"
      \ . " | delcommand KlammertextCheck"

" With +python3 the shared core runs in-process: 'indentexpr' makes the =
" operator work, and the matching delimiter is highlighted live as the
" cursor sits on one (the show-paren equivalent; a mismatch shows in red
" with a message).  Without +python3 the commands above still work — they
" shell out to python3 — and Neovim users can get live matching from the
" language server instead (see doc/edit/README.md).
if has('python3')
  setlocal indentexpr=klammertext#IndentExpr()
  let b:undo_ftplugin .= " | setlocal indentexpr<"
  call klammertext#SetupMatchHighlight()
endif
