Skip to content

Commit

Permalink
feat: add vimtex#cite#get_entry api function
Browse files Browse the repository at this point in the history
refer: #2946
  • Loading branch information
lervag committed May 4, 2024
1 parent e4e659b commit 137f5bd
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
20 changes: 20 additions & 0 deletions autoload/vimtex/cite.vim
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,26 @@
" 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)
Expand Down
14 changes: 1 addition & 13 deletions autoload/vimtex/context/cite.vim
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,7 @@ endfunction

" }}}1
function! s:handler.get_actions() abort dict " {{{1
" 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()

let l:entry = get(
\ filter(copy(l:entries), {_, x -> x.key ==# self.selected}), 0, {})
let l:entry = vimtex#cite#get_entry(self.selected)

if empty(l:entry)
call vimtex#log#warning('Cite key not found: ' .. self.selected)
Expand Down
6 changes: 6 additions & 0 deletions doc/vimtex.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6236,6 +6236,12 @@ users for customization.

Note: This reference is currently a work in progress!

*vimtex#cite#get_entry*
Returns a citation entry. It takes an option `key` argument. If a `key` is
supplied, then the corresponding bib entry for that key is returned (a
|Dict|). Else it returns the entry for the key under the cursor. If no key
is found, an empty |Dict| is returned.

*vimtex#cite#get_key*
Returns the citation key under the cursor. Can be useful e.g. to create
a function to open a citation in another progrem such as BibDesk or Zotero.
Expand Down

0 comments on commit 137f5bd

Please sign in to comment.