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

function! vimtex#imaps#init_options() " {{{1
  call vimtex#util#set_default('g:vimtex_imaps_enabled', 1)
  call vimtex#util#set_default('g:vimtex_imaps_leader', '`')
  call vimtex#util#set_default('g:vimtex_imaps_disabled', [])
  call vimtex#util#set_default('g:vimtex_imaps_snippet_engine', 'ultisnips')
  call vimtex#util#set_default('g:vimtex_imaps_list', s:default_maps())
endfunction

" }}}1
function! vimtex#imaps#init_script() " {{{1
  let l:rtp = split(&rtp, ',')
  let s:has_ultisnips = len(filter(copy(l:rtp), "v:val =~? 'ultisnips'")) > 0
  let s:has_neosnippet = len(filter(copy(l:rtp), "v:val =~? 'neosnippet'")) > 0
endfunction

" }}}1
function! vimtex#imaps#init_buffer() " {{{1
  if !g:vimtex_imaps_enabled | return | endif

  for l:map in g:vimtex_imaps_list
    call vimtex#imaps#add_map(l:map)
  endfor
endfunction

" }}}1

function! vimtex#imaps#add_map(map) " {{{1
  let l:lhs = a:map.lhs_rhs[0]
  let l:rhs = a:map.lhs_rhs[1]
  let l:leader = get(a:map, 'leader', g:vimtex_imaps_leader)
  let l:wrapper = get(a:map, 'wrapper', '')

  " Don't create map if it is disabled
  if index(g:vimtex_imaps_disabled, l:lhs) >= 0 | return | endif

  " Don't create map if snippet feature is not active/available
  if !s:test_snippet_requirement(l:wrapper) | return | endif

  " Escape leader if it exists
  if l:leader !=# '' && !hasmapto(l:leader, 'i')
    silent execute 'inoremap <silent><buffer>' l:leader . l:leader l:leader
  endif

  " Apply wrapper
  if l:wrapper !=# '' && exists('*' . l:wrapper)
    let l:rhs = call(l:wrapper, [l:lhs, l:rhs])
  endif

  " Add mapping
  silent execute 'inoremap <silent><buffer>' l:leader . l:lhs l:rhs
endfunction

" }}}1

"
" Wrappers
"
function! s:wrap_math(lhs, rhs) " {{{1
  return '<c-r>=<sid>is_math() ? ' . string(a:rhs)
        \ . ' : ' . string(a:lhs) . '<cr>'
endfunction

" }}}1
function! s:wrap_snippet(lhs, rhs) " {{{1
  if g:vimtex_imaps_snippet_engine ==# 'neosnippet'
    return '<c-r>=neosnippet#anonymous(''' . a:rhs . ''')<cr>'
  else
    return a:lhs . '<c-r>=UltiSnips#Anon('''
          \ . a:rhs . ''', ''' . a:lhs . ''', '''', ''i'')<cr>'
  endif
endfunction

" }}}1
function! s:wrap_math_snippet(lhs, rhs) " {{{1
  if g:vimtex_imaps_snippet_engine ==# 'neosnippet'
    return '<c-r>=<sid>is_math() ? neosnippet#anonymous(''' . a:rhs . ''')'
          \ . ' : ' . string(a:lhs) . '<cr>'
  else
    return a:lhs . '<c-r>=<sid>is_math() ? '
          \ . 'UltiSnips#Anon(''' . a:rhs . ''', ''' . a:lhs . ''', '''', ''i'')'
          \ . ': ''''<cr>'
  endif
endfunction

" }}}1

"
" Helper functions
"<commit_msg>imap_mappings: do not swallow leader outside math<commit_after>" vimtex - LaTeX plugin for Vim
"
" Maintainer: <NAME>
" Email:      <EMAIL>
"

function! vimtex#imaps#init_options() " {{{1
  call vimtex#util#set_default('g:vimtex_imaps_enabled', 1)
  call vimtex#util#set_default('g:vimtex_imaps_leader', '`')
  call vimtex#util#set_default('g:vimtex_imaps_disabled', [])
  call vimtex#util#set_default('g:vimtex_imaps_snippet_engine', 'ultisnips')
  call vimtex#util#set_default('g:vimtex_imaps_list', s:default_maps())
endfunction

" }}}1
function! vimtex#imaps#init_script() " {{{1
  let l:rtp = split(&rtp, ',')
  let s:has_ultisnips = len(filter(copy(l:rtp), "v:val =~? 'ultisnips'")) > 0
  let s:has_neosnippet = len(filter(copy(l:rtp), "v:val =~? 'neosnippet'")) > 0
endfunction

" }}}1
function! vimtex#imaps#init_buffer() " {{{1
  if !g:vimtex_imaps_enabled | return | endif

  for l:map in g:vimtex_imaps_list
    call vimtex#imaps#add_map(l:map)
  endfor
endfunction

" }}}1

function! vimtex#imaps#add_map(map) " {{{1
  let l:lhs = a:map.lhs_rhs[0]
  let l:rhs = a:map.lhs_rhs[1]
  let l:leader = get(a:map, 'leader', g:vimtex_imaps_leader)
  let l:wrapper = get(a:map, 'wrapper', '')

  " Don't create map if it is disabled
  if index(g:vimtex_imaps_disabled, l:lhs) >= 0 | return | endif

  " Don't create map if snippet feature is not active/available
  if !s:test_snippet_requirement(l:wrapper) | return | endif

  " Escape leader if it exists
  if l:leader !=# '' && !hasmapto(l:leader, 'i')
    silent execute 'inoremap <silent><buffer>' l:leader . l:leader l:leader
  endif

  " Apply wrapper
  if l:wrapper !=# '' && exists('*' . l:wrapper)
    let l:rhs = call(l:wrapper, [l:leader . l:lhs, l:rhs])
  endif

  " Add mapping
  silent execute 'inoremap <silent><buffer>' l:leader . l:lhs l:rhs
endfunction

" }}}1

"
" Wrappers
"
function! s:wrap_math(lhs, rhs) " {{{1
  return '<c-r>=<sid>is_math() ? ' . string(a:rhs)
        \ . ' : ' .  string(a:lhs) . '<cr>'
endfunction

" }}}1
function! s:wrap_snippet(lhs, rhs) " {{{1
  if g:vimtex_imaps_snippet_engine ==# 'neosnippet'
    return '<c-r>=neosnippet#anonymous(''' . a:rhs . ''')<cr>'
  else
    return a:lhs . '<c-r>=UltiSnips#Anon('''
          \ . a:rhs . ''', ''' . a:lhs . ''', '''', ''i'')<cr>'
  endif
endfunction

" }}}1
function! s:wrap_math_snippet(lhs, rhs) " {{{1
  if g:vimtex_imaps_snippet_engine ==# 'neosnippet'
    return '<c-r>=<sid>is_math() ? neosnippet#anonymous(''' . a:rhs . ''')'
          \ . ' : ' . string(a:lhs) . '<cr>'
  else
    return a:lhs . '<c-r>=<sid>is_math() ? '
          \ . 'UltiSnips#Anon(''' . a:rhs . ''', ''' . a:lhs . ''', '''', ''i'')'
          \ . ': ''''<cr>'
  endif
endfunction

" }}}1

"
" Helper functions
"
{'fraction_non_alphanumeric': 0.19937292511988197, 'fraction_numerical': 0.0073773515308004425, 'mean_word_length': 4.101599247412982, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 47, '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': '4479266', 'n_tokens_mistral': 2372, 'n_tokens_neox': 2308, 'n_words': 423}