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

function! vimtex#fold#init_buffer() " {{{1
  " b:vimtex_fold is a dictionary used to store dynamic fold information
  " Note: We define this even if folding is disabled, because people might want
  "       to enable folding manually
  let b:vimtex_fold = {}

  if !g:vimtex_fold_enabled | return | endif
  if s:foldmethod_in_modeline() | return | endif

  " Sanity check
  if g:vimtex_fold_preamble
        \ && has_key(s:cmds, 'documentclass')
    let g:vimtex_fold_preamble = 0
    call vimtex#echo#warning('Can''t fold both preamble and documentclass!')
    call vimtex#echo#wait()
  endif

  " Set fold options
  setlocal foldmethod=expr
  setlocal foldexpr=vimtex#fold#level(v:lnum)
  setlocal foldtext=vimtex#fold#text()

  if g:vimtex_fold_manual
    " Remap zx to refresh fold levels
    nnoremap <silent><buffer> zx :call vimtex#fold#refresh('zx')<cr>
    nnoremap <silent><buffer> zX :call vimtex#fold#refresh('zX')<cr>

    " Define commands
    command! -buffer VimtexRefreshFolds call vimtex#fold#refresh('zx')

    " Set options for automatic/manual folding mode
    let s:fold_manual_id = get(s:, 'fold_manual_id', 0) + 1
    let b:fold_manual_augroup = 'vimtex_fold_' . s:fold_manual_id
    execute 'augroup' b:fold_manual_augroup
      autocmd!
      " vint: -ProhibitAutocmdWithNoGroup
      autocmd CursorMoved <buffer> call s:fold_manual_refresh()
      " vint: +ProhibitAutocmdWithNoGroup
    augroup END

    function! s:fold_manual_refresh()
      call vimtex#fold#refresh('zx')
      if exists('b:fold_manual_augroup')
        execute 'autocmd!' b:fold_manual_augroup
        execute 'augroup!' b:fold_manual_augroup
        unlet b:fold_manual_augroup
      endif
    endfunction
  endif
endfunction

" }}}1

function! vimtex#fold#refresh(map) " {{{1
  setlocal foldmethod=expr
  execute 'normal! ' . a:map
  setlocal foldmethod=manual
endfunction

" }}}1
function! vimtex#fold#level(lnum) " {{{1
  " Refresh fold levels for section commands
  call s:refresh_folded_sections()

  " Check for normal lines first (optimization)
  let line = getline(a:lnum)
  if line !~# s:folded | return '=' | endif

  " Fold preamble
  if g:vimtex_fold_preamble && line =~# '^\s*\\documentclass'
    return '>1'
  endif

  " Never fold \begin{document}
  if line =~# '^\s*\\begin\s*{\s*document\s*}'
    return '0'
  endif

  " Fold commands
  for l:cmd in s:cmds_types
    let l:value = l:cmd.level(line, a:lnum)
    if !empty(l:value) | return l:value | endif
  endfor

  " Fold chapters and sections
  for [part, level] in b:vimtex_fold.parts
    if line =~# part
      return '>' . level
    endif
  endfor

  " Fold comments
  if g:vimtex_fold_comments
    if line =~# '^\s*%'
      let l:next = getline(a:lnum-1) !~# '^\s*%'
      let l:prev = getline(a:lnum+1) !~# '^\s*%'
      if l:next && ! l:prev
        return 'a1'
      elseif l:prev && ! l:next
        return 's1'
      endif
    endif
  endif

  " Fold markers
  if line =~# '\v\%.*\{\{\{'
    return 'a1'
  elseif line =~# '\v\%\s*\}\}\}'
    return 's1'
  endif

  " Never fold \end{document}
  if line =~# '^\s*\\end{document}'
    return 0
  endif

  " Fold environments
  if g:vimtex_fold_envs
    if line =~# s:env_start
      if line !~# '\\end'
        return 'a1'
      endif
    elseif line =~# s:env_stop
      if line !~# '\\begin'
        return 's1'
      endif<commit_msg>Fixed (partly) #862: better comment folding<commit_after>" vimtex - LaTeX plugin for Vim
"
" Maintainer: <NAME>
" Email:      <EMAIL>
"

function! vimtex#fold#init_buffer() " {{{1
  " b:vimtex_fold is a dictionary used to store dynamic fold information
  " Note: We define this even if folding is disabled, because people might want
  "       to enable folding manually
  let b:vimtex_fold = {}

  if !g:vimtex_fold_enabled | return | endif
  if s:foldmethod_in_modeline() | return | endif

  " Sanity check
  if g:vimtex_fold_preamble
        \ && has_key(s:cmds, 'documentclass')
    let g:vimtex_fold_preamble = 0
    call vimtex#echo#warning('Can''t fold both preamble and documentclass!')
    call vimtex#echo#wait()
  endif

  " Set fold options
  setlocal foldmethod=expr
  setlocal foldexpr=vimtex#fold#level(v:lnum)
  setlocal foldtext=vimtex#fold#text()

  if g:vimtex_fold_manual
    " Remap zx to refresh fold levels
    nnoremap <silent><buffer> zx :call vimtex#fold#refresh('zx')<cr>
    nnoremap <silent><buffer> zX :call vimtex#fold#refresh('zX')<cr>

    " Define commands
    command! -buffer VimtexRefreshFolds call vimtex#fold#refresh('zx')

    " Set options for automatic/manual folding mode
    let s:fold_manual_id = get(s:, 'fold_manual_id', 0) + 1
    let b:fold_manual_augroup = 'vimtex_fold_' . s:fold_manual_id
    execute 'augroup' b:fold_manual_augroup
      autocmd!
      " vint: -ProhibitAutocmdWithNoGroup
      autocmd CursorMoved <buffer> call s:fold_manual_refresh()
      " vint: +ProhibitAutocmdWithNoGroup
    augroup END

    function! s:fold_manual_refresh()
      call vimtex#fold#refresh('zx')
      if exists('b:fold_manual_augroup')
        execute 'autocmd!' b:fold_manual_augroup
        execute 'augroup!' b:fold_manual_augroup
        unlet b:fold_manual_augroup
      endif
    endfunction
  endif
endfunction

" }}}1

function! vimtex#fold#refresh(map) " {{{1
  setlocal foldmethod=expr
  execute 'normal! ' . a:map
  setlocal foldmethod=manual
endfunction

" }}}1
function! vimtex#fold#level(lnum) " {{{1
  " Refresh fold levels for section commands
  call s:refresh_folded_sections()

  " Check for normal lines first (optimization)
  let line = getline(a:lnum)
  if line !~# s:folded | return '=' | endif

  " Fold preamble
  if g:vimtex_fold_preamble && line =~# '^\s*\\documentclass'
    return '>1'
  endif

  " Never fold \begin{document}
  if line =~# '^\s*\\begin\s*{\s*document\s*}'
    return '0'
  endif

  " Fold commands
  for l:cmd in s:cmds_types
    let l:value = l:cmd.level(line, a:lnum)
    if !empty(l:value) | return l:value | endif
  endfor

  " Fold chapters and sections
  for [part, level] in b:vimtex_fold.parts
    if line =~# part
      return '>' . level
    endif
  endfor

  " Fold markers
  if line =~# '\v\%.*\{\{\{'
    let s:fold_markers = 1
    return 'a1'
  elseif line =~# '\v\%\s*\}\}\}'
    let s:fold_markers = 0
    return 's1'
  endif

  " Fold long comments
  if g:vimtex_fold_comments && !get(s:, 'fold_markers')
    if line =~# '^\s*%'
      let l:next = getline(a:lnum-1) !~# '^\s*%'
      let l:prev = getline(a:lnum+1) !~# '^\s*%'
      if l:next && ! l:prev
        return 'a1'
      elseif l:prev && ! l:next
        return 's1'
      endif
    endif
  endif

  " Never fold \end{document}
  if line =~# '^\s*\\end{document}'
    return 0
  endif

  " Fold environments
  if g:vimtex_fold_envs
    if line =~# s:env_start
      if line !~# '\\end'
        return 'a1'
      endif
    elseif line =~# s:env_stop
      if line !~# '\\begin'
        return 's1'
      endif
{'fraction_non_alphanumeric': 0.12668846864780323, 'fraction_numerical': 0.006114033840466373, 'mean_word_length': 3.157210401891253, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 25, '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': '3793743', 'n_tokens_mistral': 2720, 'n_tokens_neox': 2558, 'n_words': 637}