<commit_before>let s:handlers = []

function! s:_vital_loaded(V) abort
  let s:Prompt = a:V.import('Vim.Prompt')
  call s:register(s:get_default_handler())
endfunction

function! s:_vital_depends() abort
  return ['Vim.Prompt']
endfunction

function! s:_throw(category, msg) abort
  if a:category ==# 'Info'
    let v:statusmsg = a:msg
  elseif a:category ==# 'Warning'
    let v:warningmsg = a:msg
  elseif a:category =~# '^\%(Error\|Critical\)$'
    let v:errmsg = a:msg
  endif
  return printf(
        \ 'vital: Vim.Exception: %s: %s',
        \ a:category,
        \ a:msg,
        \)
endfunction

function! s:info(msg) abort
  return s:_throw('Info', a:msg)
endfunction

function! s:warn(msg) abort
  return s:_throw('Warning', a:msg)
endfunction

function! s:error(msg) abort
  return s:_throw('Error', a:msg)
endfunction

function! s:critical(msg) abort
  return s:_throw('Critical', a:msg)
endfunction

function! s:handle(...) abort
  let l:exception = get(a:000, 0, v:exception)
  for Handler in s:handlers
    if call(Handler, [l:exception])
      return
    endif
  endfor
  throw l:exception
endfunction

function! s:call(funcref, args, ...) abort
  let instance = get(a:000, 0, 0)
  try
    if type(instance) == type({})
      return call(a:funcref, a:args, instance)
    else
      return call(a:funcref, a:args)
    endif
  catch /^vital: Vim\.Exception: /
    call s:handle()
  endtry
endfunction

function! s:register(handler) abort
  call add(s:handlers, a:handler)
endfunction

function! s:unregister(handler) abort
  let index = index(s:handlers, a:handler)
  if index != -1
    call remove(s:handlers, index)
  endif
endfunction

function! s:get_default_handler() abort
  return function('s:default_handler')
endfunction


" Handler --------------------------------------------------------------------
function! s:default_handler(exception) abort
  let m = matchlist(a:exception, '^vital: Vim\.Exception: \(\w\+\): \(.*\)')
  if len(m)
    let category = m[1]
    let message = m[2]
    if category ==# 'Info'
      call s:Prompt.echo('None', message)
      call s:Prompt.debug(v:throwpoint)
      return 1
    elseif category ==# 'Warning'
      call s:Prompt.warn(message)
      call s:Prompt.debug(v:throwpoint)
      return 1
    elseif category ==# 'Error'
      call s:Prompt.error(message)
      call s:Prompt.debug(v:throwpoint)
      return 1<commit_msg>Vim.Exception: Make default_handler private<commit_after>let s:handlers = []

function! s:_vital_loaded(V) abort
  let s:Prompt = a:V.import('Vim.Prompt')
  call s:register(s:get_default_handler())
endfunction

function! s:_vital_depends() abort
  return ['Vim.Prompt']
endfunction

function! s:_throw(category, msg) abort
  if a:category ==# 'Info'
    let v:statusmsg = a:msg
  elseif a:category ==# 'Warning'
    let v:warningmsg = a:msg
  elseif a:category =~# '^\%(Error\|Critical\)$'
    let v:errmsg = a:msg
  endif
  return printf(
        \ 'vital: Vim.Exception: %s: %s',
        \ a:category,
        \ a:msg,
        \)
endfunction

function! s:info(msg) abort
  return s:_throw('Info', a:msg)
endfunction

function! s:warn(msg) abort
  return s:_throw('Warning', a:msg)
endfunction

function! s:error(msg) abort
  return s:_throw('Error', a:msg)
endfunction

function! s:critical(msg) abort
  return s:_throw('Critical', a:msg)
endfunction

function! s:handle(...) abort
  let l:exception = get(a:000, 0, v:exception)
  for Handler in s:handlers
    if call(Handler, [l:exception])
      return
    endif
  endfor
  throw l:exception
endfunction

function! s:call(funcref, args, ...) abort
  let instance = get(a:000, 0, 0)
  try
    if type(instance) == type({})
      return call(a:funcref, a:args, instance)
    else
      return call(a:funcref, a:args)
    endif
  catch /^vital: Vim\.Exception: /
    call s:handle()
  endtry
endfunction

function! s:register(handler) abort
  call add(s:handlers, a:handler)
endfunction

function! s:unregister(handler) abort
  let index = index(s:handlers, a:handler)
  if index != -1
    call remove(s:handlers, index)
  endif
endfunction

function! s:get_default_handler() abort
  return function('s:_default_handler')
endfunction


" Handler --------------------------------------------------------------------
function! s:_default_handler(exception) abort
  let m = matchlist(a:exception, '^vital: Vim\.Exception: \(\w\+\): \(.*\)')
  if len(m)
    let category = m[1]
    let message = m[2]
    if category ==# 'Info'
      call s:Prompt.echo('None', message)
      call s:Prompt.debug(v:throwpoint)
      return 1
    elseif category ==# 'Warning'
      call s:Prompt.warn(message)
      call s:Prompt.debug(v:throwpoint)
      return 1
    elseif category ==# 'Error'
      call s:Prompt.error(message)
      call s:Prompt.debug(v:throwpoint)
      return 1
{'fraction_non_alphanumeric': 0.16409829237817575, 'fraction_numerical': 0.006247396917950854, 'mean_word_length': 3.2242744063324538, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1446525', 'n_tokens_mistral': 1692, 'n_tokens_neox': 1645, 'n_words': 404}