File: cite.vim

package info (click to toggle)
vim-vimtex 2.17-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 8,844 kB
  • sloc: makefile: 360; python: 103
file content (55 lines) | stat: -rw-r--r-- 1,463 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
" VimTeX - LaTeX plugin for Vim
"
" Maintainer: Karl Yngve LervÄg
" Email:      karl.yngve@gmail.com
"

function! vimtex#cite#get_entry(...) abort " {{{1
  let l:key = a:0 > 0 ? a:1 : vimtex#cite#get_key()
  if empty(l:key) | return {} | endif

  " Ensure we're at the root directory when locating bib files
  call vimtex#paths#pushd(b:vimtex.root)
  let l:entries = []
  for l:file in vimtex#bib#files()
    let l:entries += vimtex#parser#bib(
          \ l:file,
          \ {'backend': has('nvim') ? 'lua' : 'vim'}
          \)
  endfor
  call vimtex#paths#popd()

  " Return entry with the given key
  return l:entries->filter({_, x -> x.key ==# l:key})->get(0, {})
endfunction

" }}}1
function! vimtex#cite#get_key(...) abort " {{{1
  let l:cmd = a:0 > 0 ? a:1 : vimtex#cmd#get_current()
  if empty(l:cmd)
        \ || l:cmd.name[1:] !~# g:vimtex#re#cite_cmd
        \ || len(l:cmd.args) < 1
        \ || len(l:cmd.args) > 2
    return ''
  endif

  let l:current_word = a:0 > 1 ? a:2 : expand('<cword>')
  let l:cites = l:cmd.args->map({_, x -> x.text})->join(',')->split(',\s*')

  return index(l:cites, l:current_word) >= 0
        \ ? l:current_word
        \ : l:cites[0]
endfunction

" }}}1
function! vimtex#cite#get_key_at(line, col) abort " {{{1
  let l:pos_saved = vimtex#pos#get_cursor()

  call vimtex#pos#set_cursor(a:line, a:col)
  let l:key = vimtex#cite#get_key()
  call vimtex#pos#set_cursor(l:pos_saved)

  return l:key
endfunction

" }}}1