Skip to content

Commit

Permalink
fix: ensure data exists before using it
Browse files Browse the repository at this point in the history
We don't want vimtex#bib#files to throw an error, we instead want it to
return an empty list.

refer: micangl/cmp-vimtex#19
  • Loading branch information
lervag committed Feb 26, 2024
1 parent 88eca56 commit afac402
Showing 1 changed file with 28 additions and 24 deletions.
52 changes: 28 additions & 24 deletions autoload/vimtex/bib.vim
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,40 @@
"

function! vimtex#bib#files() abort " {{{1
if has_key(b:vimtex.packages, 'biblatex')
let l:file = b:vimtex.compiler.get_file('bcf')
if !exists('b:vimtex') | return [] | endif

if has_key(b:vimtex, 'compiler')
if has_key(b:vimtex.packages, 'biblatex')
let l:file = b:vimtex.compiler.get_file('bcf')
if filereadable(l:file)
let l:bibs = map(
\ filter(readfile(l:file), "v:val =~# 'bcf:datasource'"),
\ {_, x -> matchstr(x, '<[^>]*>\zs[^<]*')})
for l:f in filter(copy(l:bibs), {_, x -> x =~# '[*?{[]' })
let l:bibs += glob(l:f, 0, 1)
endfor
if !empty(l:bibs) | return s:validate(l:bibs) | endif
endif
endif

let l:file = b:vimtex.compiler.get_file('blg')
if filereadable(l:file)
let l:bibs = map(
\ filter(readfile(l:file), "v:val =~# 'bcf:datasource'"),
\ {_, x -> matchstr(x, '<[^>]*>\zs[^<]*')})
for l:f in filter(copy(l:bibs), {_, x -> x =~# '[*?{[]' })
let l:bibs += glob(l:f, 0, 1)
endfor
if !empty(l:bibs) | return s:validate(l:bibs) | endif
endif
endif
\ filter(readfile(l:file), 'v:val =~# ''^Database file #\d'''),
\ {_, x -> matchstr(x, '#\d\+: \zs.*\ze\.bib$')})

let l:file = b:vimtex.compiler.get_file('blg')
if filereadable(l:file)
let l:bibs = map(
\ filter(readfile(l:file), 'v:val =~# ''^Database file #\d'''),
\ {_, x -> matchstr(x, '#\d\+: \zs.*\ze\.bib$')})
" Ignore '{name}-blx.bib' file (created by biblatex)
if has_key(b:vimtex.packages, 'biblatex')
call filter(l:bibs, 'v:val !~# ''-blx$''')
endif

" Ignore '{name}-blx.bib' file (created by biblatex)
if has_key(b:vimtex.packages, 'biblatex')
call filter(l:bibs, 'v:val !~# ''-blx$''')
endif
" Ignore '{name}Notes.bib' file (created by revtex4)
if b:vimtex.documentclass =~# '^revtex4'
call filter(l:bibs, 'v:val !~# ''.Notes$''')
endif

" Ignore '{name}Notes.bib' file (created by revtex4)
if b:vimtex.documentclass =~# '^revtex4'
call filter(l:bibs, 'v:val !~# ''.Notes$''')
if !empty(l:bibs) | return s:validate(l:bibs) | endif
endif

if !empty(l:bibs) | return s:validate(l:bibs) | endif
endif

return s:validate(s:files_manual())
Expand Down

0 comments on commit afac402

Please sign in to comment.