<commit_before>" VimTeX - LaTeX plugin for Vim
"
" Maintainer: <NAME>
" Email:      <EMAIL>
"

function! vimtex#fold#envs#new(config) abort " {{{1
  return extend(deepcopy(s:folder), a:config).init()
endfunction

" }}}1


let s:folder = {
      \ 'name' : 'environments',
      \ 're' : {
      \   'start' : g:vimtex#re#not_comment . '\\begin\s*\{.{-}\}',
      \   'end' : g:vimtex#re#not_comment . '\\end\s*\{.{-}\}',
      \   'name' : g:vimtex#re#not_comment . '\\%(begin|end)\s*\{\zs.{-}\ze\}'
      \ },
      \ 'whitelist' : [],
      \ 'blacklist' : [],
      \}
function! s:folder.init() abort dict " {{{1
  " Define the validator as simple as possible
  if empty(self.whitelist) && empty(self.blacklist)
    function! self.validate(env) abort dict
      return 1
    endfunction
  elseif empty(self.whitelist)
    function! self.validate(env) abort dict
      return index(self.blacklist, a:env) < 0
    endfunction
  elseif empty(self.blacklist)
    function! self.validate(env) abort dict
      return index(self.whitelist, a:env) >= 0
    endfunction
  else
    function! self.validate(env) abort dict
      return index(self.whitelist, a:env) >= 0 && index(self.blacklist, a:env) < 0
    endfunction
  endif

  return self
endfunction

" }}}1
function! s:folder.level(line, lnum) abort dict " {{{1
  let l:env = matchstr(a:line, self.re.name)

  if !empty(l:env) && self.validate(l:env)
    if a:line =~# self.re.start
      if a:line !~# '\\end'
        return 'a1'
      endif
    elseif a:line =~# self.re.end
      if a:line !~# '\\begin'
        return 's1'
      endif
    endif
  endif
endfunction

" }}}1
function! s:folder.text(line, level) abort dict " {{{1
  let env = matchstr(a:line, self.re.name)
  if !self.validate(env) | return | endif

  " Set caption/label based on type of environment
  if env ==# 'frame'
    let label = ''
    let caption = self.parse_caption_frame(a:line)
  elseif env ==# 'table'
    let label = self.parse_label()
    let caption = self.parse_caption_table(a:line)
  else
    let label = self.parse_label()
    let caption = self.parse_caption(a:line)
  endif

  let width_ind = len(matchstr(a:line, '^\s*'))
  let width = winwidth(0) - (&number ? &numberwidth : 0) - 4 - width_ind

  let width_env = 19
  let width_lab = len(label) + 2 > width - width_env
        \ ? width - width_env
        \ : len(label) + 2
  let width_cap = width - width_env - width_lab

  if !empty(label)
    let label = printf('(%.*S)', width_lab, label)
  endif

  if !empty(caption)
    if strchars(caption) > width_cap
      let caption = strpart(caption, 0, width_cap - 4) . '...'
    endif
  else
    let width_env += width_cap
    let width_cap = 0
  endif

  if strlen(env) > width_env - 8
    let env = strpart(env, 0, width_env - 11) . '...'
  endif
  let env = '\begin{' . env . '}' . ' ' . matchstr(a:line, '\[\zs.*\ze\]')

  let title = printf('%*S%-*S %-*S  %*S',
        \ width_ind, '',
        \ width_env, env,
        \ width_cap, caption,
        \ width_lab, label)

  return substitute(title, '\s\+$', '', '')
endfunction

" }}}1
function! s:folder.parse_label() abort dict " {{{1
  let i = v:foldend
  while i >= v:foldstart
    if getline(i) =~# '^\s*\\label'
      return matchstr(getline(i), '^\s*\\label\%(\[.*\]\)\?{\zs.*\ze}')
    end
    let i -= 1
  endwhile
  return ''
endfunction

" }}}1
function! s:folder.parse_caption(line) abort dict " {{{1<commit_msg>Added option variable for more flexibility<commit_after>" VimTeX - LaTeX plugin for Vim
"
" Maintainer: <NAME>
" Email:      <EMAIL>
"

function! vimtex#fold#envs#new(config) abort " {{{1
  return extend(deepcopy(s:folder), a:config).init()
endfunction

" }}}1


let s:folder = {
      \ 'name' : 'environments',
      \ 're' : {
      \   'start' : g:vimtex#re#not_comment . '\\begin\s*\{.{-}\}',
      \   'end' : g:vimtex#re#not_comment . '\\end\s*\{.{-}\}',
      \   'name' : g:vimtex#re#not_comment . '\\%(begin|end)\s*\{\zs.{-}\ze\}'
      \ },
      \ 'whitelist' : [],
      \ 'blacklist' : [],
      \}
function! s:folder.init() abort dict " {{{1
  " Define the validator as simple as possible
  if empty(self.whitelist) && empty(self.blacklist)
    function! self.validate(env) abort dict
      return 1
    endfunction
  elseif empty(self.whitelist)
    function! self.validate(env) abort dict
      return index(self.blacklist, a:env) < 0
    endfunction
  elseif empty(self.blacklist)
    function! self.validate(env) abort dict
      return index(self.whitelist, a:env) >= 0
    endfunction
  else
    function! self.validate(env) abort dict
      return index(self.whitelist, a:env) >= 0 && index(self.blacklist, a:env) < 0
    endfunction
  endif

  return self
endfunction

" }}}1
function! s:folder.level(line, lnum) abort dict " {{{1
  let l:env = matchstr(a:line, self.re.name)

  if !empty(l:env) && self.validate(l:env)
    if a:line =~# self.re.start
      if a:line !~# '\\end'
        return 'a1'
      endif
    elseif a:line =~# self.re.end
      if a:line !~# '\\begin'
        return 's1'
      endif
    endif
  endif
endfunction

" }}}1
function! s:folder.text(line, level) abort dict " {{{1
  let env = matchstr(a:line, self.re.name)
  if !self.validate(env) | return | endif

  " Set caption/label based on type of environment
  if env ==# 'frame'
    let option = ''
    let label = ''
    let caption = self.parse_caption_frame(a:line)
  elseif env ==# 'table'
    let option = ''
    let label = self.parse_label()
    let caption = self.parse_caption_table(a:line)
  else
    let option = matchstr(a:line, '\[\zs.*\ze\]')
    let label = self.parse_label()
    let caption = self.parse_caption(a:line)
  endif

  let width_ind = len(matchstr(a:line, '^\s*'))
  let width = winwidth(0) - (&number ? &numberwidth : 0) - 4 - width_ind

  let width_env = 19
  let width_lab = len(label) + 2 > width - width_env
        \ ? width - width_env
        \ : len(label) + 2
  let width_cap = width - width_env - width_lab

  if !empty(label)
    let label = printf('(%.*S)', width_lab, label)
  endif

  if !empty(caption)
    if strchars(caption) > width_cap
      let caption = strpart(caption, 0, width_cap - 4) . '...'
    endif
  else
    let width_env += width_cap
    let width_cap = 0
  endif

  if strlen(env) > width_env - 8
    let env = strpart(env, 0, width_env - 11) . '...'
  endif
  let env = '\begin{' . env . '}' . ' ' . option

  let title = printf('%*S%-*S %-*S  %*S',
        \ width_ind, '',
        \ width_env, env,
        \ width_cap, caption,
        \ width_lab, label)

  return substitute(title, '\s\+$', '', '')
endfunction

" }}}1
function! s:folder.parse_label() abort dict " {{{1
  let i = v:foldend
  while i >= v:foldstart
    if getline(i) =~# '^\s*\\label'
      return matchstr(getline(i), '^\s*\\label\%(\[.*\]\)\?{\zs.*\ze}')
    end
    let i -= 1
  endwhile
  return ''
endfunction

" }}}1
function! s:folder.parse_caption(line) abort dict " {{{1
{'fraction_non_alphanumeric': 0.17043653652211496, 'fraction_numerical': 0.009508716323296355, 'mean_word_length': 2.6421825813221407, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 19, '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': '1335996', 'n_tokens_mistral': 2636, 'n_tokens_neox': 2543, 'n_words': 611}