<commit_before>" {{{1 latex#init
let s:initialized = 0
function! latex#init()
  call s:init_environment()
  call s:init_errorformat()

  call latex#toc#init(s:initialized)
  call latex#fold#init(s:initialized)
  call latex#motion#init(s:initialized)
  call latex#change#init(s:initialized)
  call latex#latexmk#init(s:initialized)
  call latex#complete#init(s:initialized)

  "
  " This variable is used to allow a distinction between global and buffer
  " initialization
  "
  let s:initialized = 1
endfunction

" {{{1 latex#info
function! latex#info()
  echo "Buffer data"
  echo printf('           id: %-s', b:latex.id)
  echo printf('fold sections: %-s', string(b:latex.fold_sections))
  echo "\n"

  echo "Latex blobs"
  let n = -1
  for data in g:latex#data
    let n += 1
    if n > 0
      echo "\n"
    endif
    let d = copy(data)
    let d.aux = d.aux()
    let d.out = d.out()
    let d.log = d.log()
    echo printf('%6s: %-s', 'id', n)
    for [key, val] in sort(items(d), "s:info_sort_func")
      if key =~ '\vaux|out|root|log|tex'
        let val = s:truncate(val)
      endif
      echo printf('%6s: %-s', key, val)
    endfor
  endfor
endfunction

" {{{1 latex#help
function! latex#help()
  if g:latex_default_mappings
    echo "Latex mappings"
    nmap <buffer>
    vmap <buffer>
    omap <buffer>
  endif
endfunction

" {{{1 latex#reinit
function! latex#reinit()
  "
  " Stop latexmk processes (if running)
  "
  call latex#latexmk#stop_all()

  "
  " Reset global variables
  "
  let s:initialized = 0
  unlet g:latex#data

  "
  " Reset and reinitialize buffers
  "
  bufdo   if getbufvar('%', '&filetype') == 'tex' |
        \   unlet b:notbslash b:notcomment b:latex |
        \   call latex#init() |
        \ endif
endfunction

" {{{1 latex#view
function! latex#view()
  let outfile = g:latex#data[b:latex.id].out()
  if !filereadable(outfile)
    echomsg "Can't view: Output file is not readable!"
    return
  endif

  silent execute '!' . g:latex_viewer . ' ' . outfile . ' &>/dev/null &'
  if !has("gui_running")
    redraw!
  endif
endfunction
" }}}1

" {{{1 s:init_environment
function! s:init_environment()
  "
  " Initialize global and local data blobs
  "
  call latex#util#set_default('g:latex#data', [])
  call latex#util#set_default('b:latex', {})

  "
  " Initialize some common patterns
  "
  call latex#util#set_default('b:notbslash', '\%(\\\@<!\%(\\\\\)*\)\@<=')
  call latex#util#set_default('b:notcomment',
        \ '\%(\%(\\\@<!\%(\\\\\)*\)\@<=%.*\)\@<!')

  "
  " Create new or link to old blob
  "
  let main = s:get_main_tex()
  let id   = s:get_id(main)
  if id >= 0
    let b:latex.id = id
  else
    let data = {}
    let data.tex  = main
    let data.root = fnamemodify(data.tex, ':h')
    let data.base = fnamemodify(data.tex, ':t')
    let data.name = fnamemodify(data.tex, ':t:r')
    function data.aux() dict
      return s:get_main_ext(self, 'aux')
    endfunction
    function data.log() dict
      return s:get_main_ext(self, 'log')
    endfunction
    function data.out() dict
      return s:get_main_ext(self, g:latex_latexmk_output)
    endfunction

    call add(g:latex#data, data)
    let b:latex.id = len(g:latex#data) - 1
  endif

  if g:latex_default_mappings
    nnoremap <silent><buffer> <localleader>li :call latex#info()<cr>
    nnoremap <silent><buffer> <localleader>lh :call latex#help()<cr>
    nnoremap <silent><buffer> <localleader>lv :call latex#view()<cr>
    nnoremap <silent><buffer> <LocalLeader>lR :call latex#reinit()<cr>
  endif
endfunction

" {{{1 s:init_errorformat<commit_msg>Add some utility mappings<commit_after>" {{{1 latex#init
let s:initialized = 0
function! latex#init()
  call s:init_environment()
  call s:init_errorformat()

  call latex#toc#init(s:initialized)
  call latex#fold#init(s:initialized)
  call latex#motion#init(s:initialized)
  call latex#change#init(s:initialized)
  call latex#latexmk#init(s:initialized)
  call latex#complete#init(s:initialized)

  "
  " This variable is used to allow a distinction between global and buffer
  " initialization
  "
  let s:initialized = 1
endfunction

" {{{1 latex#info
function! latex#info()
  echo "Buffer data"
  echo printf('           id: %-s', b:latex.id)
  echo printf('fold sections: %-s', string(b:latex.fold_sections))
  echo "\n"

  echo "Latex blobs"
  let n = -1
  for data in g:latex#data
    let n += 1
    if n > 0
      echo "\n"
    endif
    let d = copy(data)
    let d.aux = d.aux()
    let d.out = d.out()
    let d.log = d.log()
    echo printf('%6s: %-s', 'id', n)
    for [key, val] in sort(items(d), "s:info_sort_func")
      if key =~ '\vaux|out|root|log|tex'
        let val = s:truncate(val)
      endif
      echo printf('%6s: %-s', key, val)
    endfor
  endfor
endfunction

" {{{1 latex#help
function! latex#help()
  if g:latex_default_mappings
    echo "Latex mappings"
    nmap <buffer>
    vmap <buffer>
    omap <buffer>
  endif
endfunction

" {{{1 latex#reinit
function! latex#reinit()
  "
  " Stop latexmk processes (if running)
  "
  call latex#latexmk#stop_all()

  "
  " Reset global variables
  "
  let s:initialized = 0
  unlet g:latex#data

  "
  " Reset and reinitialize buffers
  "
  bufdo   if getbufvar('%', '&filetype') == 'tex' |
        \   unlet b:notbslash b:notcomment b:latex |
        \   call latex#init() |
        \ endif
endfunction

" {{{1 latex#view
function! latex#view()
  let outfile = g:latex#data[b:latex.id].out()
  if !filereadable(outfile)
    echomsg "Can't view: Output file is not readable!"
    return
  endif

  silent execute '!' . g:latex_viewer . ' ' . outfile . ' &>/dev/null &'
  if !has("gui_running")
    redraw!
  endif
endfunction
" }}}1

" {{{1 s:init_environment
function! s:init_environment()
  "
  " Initialize global and local data blobs
  "
  call latex#util#set_default('g:latex#data', [])
  call latex#util#set_default('b:latex', {})

  "
  " Initialize some common patterns
  "
  call latex#util#set_default('b:notbslash', '\%(\\\@<!\%(\\\\\)*\)\@<=')
  call latex#util#set_default('b:notcomment',
        \ '\%(\%(\\\@<!\%(\\\\\)*\)\@<=%.*\)\@<!')

  "
  " Create new or link to old blob
  "
  let main = s:get_main_tex()
  let id   = s:get_id(main)
  if id >= 0
    let b:latex.id = id
  else
    let data = {}
    let data.tex  = main
    let data.root = fnamemodify(data.tex, ':h')
    let data.base = fnamemodify(data.tex, ':t')
    let data.name = fnamemodify(data.tex, ':t:r')
    function data.aux() dict
      return s:get_main_ext(self, 'aux')
    endfunction
    function data.log() dict
      return s:get_main_ext(self, 'log')
    endfunction
    function data.out() dict
      return s:get_main_ext(self, g:latex_latexmk_output)
    endfunction

    call add(g:latex#data, data)
    let b:latex.id = len(g:latex#data) - 1
  endif

  if g:latex_default_mappings
    nnoremap <silent><buffer> <localleader>li :call latex#info()<cr>
    nnoremap <silent><buffer> <localleader>lh :call latex#help()<cr>
    nnoremap <silent><buffer> <localleader>lv :call latex#view()<cr>
    nnoremap <silent><buffer> <LocalLeader>lR :call latex#reinit()<cr>

    inoremap <silent><buffer> <m-i>           \item 
  endif
endfunction

" {{{1 s:init_errorformat
{'fraction_non_alphanumeric': 0.15508615897720957, 'fraction_numerical': 0.005002779321845469, 'mean_word_length': 3.22855464159812, 'pattern_counts': {'":': 0, '<': 54, '<?xml version=': 0, '>': 50, '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': '1411285', 'n_tokens_mistral': 2838, 'n_tokens_neox': 2708, 'n_words': 634}