File: biblatex.vim

package info (click to toggle)
vim-vimtex 2.16-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 8,660 kB
  • sloc: makefile: 367; python: 103
file content (285 lines) | stat: -rw-r--r-- 7,463 bytes parent folder | download
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
" VimTeX - LaTeX plugin for Vim
"
" Maintainer: Karl Yngve LervÄg
" Email:      karl.yngve@gmail.com
"

function! vimtex#qf#biblatex#addqflist(blg) abort " {{{1
  if get(g:vimtex_quickfix_blgparser, 'disable') | return | endif

  try
    call s:qf.addqflist(a:blg)
  catch /biblatex Aborted/
  endtry
endfunction

" }}}1


let s:qf = {
      \ 'file' : '',
      \ 'types' : [],
      \ 'db_files' : [],
      \}

function! s:qf.set_errorformat() abort dict "{{{1
  setlocal errorformat=%+E%.%#\>\ ERROR%m
  setlocal errorformat+=%+W%.%#\>\ WARN\ -\ Duplicate\ entry%m
  setlocal errorformat+=%+W%.%#\>\ WARN\ -\ The\ entry%.%#cannot\ be\ encoded%m
  setlocal errorformat+=%+I%.%#\>\ INFO\ -\ Found\ BibTeX\ data\ source\ %f
  setlocal errorformat+=%-G%.%#
endfunction

" }}}1
function! s:qf.addqflist(blg) abort dict " {{{1
  let self.file = a:blg
  let self.root = fnamemodify(a:blg, ':h')
  if empty(self.file) | throw 'biblatex Aborted' | endif

  let self.types = map(
        \ filter(items(s:), 'v:val[0] =~# ''^type_'''),
        \ 'v:val[1]')
  let self.db_files = []

  call vimtex#qf#u#caddfile(self, fnameescape(self.file))

  call self.fix_paths()
endfunction

" }}}1
function! s:qf.fix_paths() abort dict " {{{1
  let l:qflist = getqflist()
  try
    let l:title = getqflist({'title': 1})
  catch /E118/
    let l:title = 'VimTeX errors'
  endtry

  if has_key(self, '__state_bibfile')
    unlet self.__state_bibfile
  endif

  for l:qf in l:qflist
    for l:type in self.types
      if l:type.fix(self, l:qf) | break | endif
    endfor
  endfor

  call filter(l:qflist, { _, x -> !has_key(x, 'removeThis') })

  call setqflist(l:qflist, 'r')

  " Set title if supported
  try
    call setqflist([], 'r', l:title)
  catch
  endtry
endfunction

" }}}1
function! s:qf.get_db_files() abort dict " {{{1
  if empty(self.db_files)
    let l:preamble = vimtex#parser#preamble(b:vimtex.tex, {
          \ 'root' : b:vimtex.root,
          \})
    let l:files = map(
          \ filter(l:preamble, 'v:val =~# ''\\addbibresource'''),
          \ 'matchstr(v:val, ''{\zs.*\ze}'')')
    let self.db_files = []
    for l:file in l:files
      if filereadable(l:file)
        let self.db_files += [l:file]
      elseif filereadable(expand(l:file))
        let self.db_files += [expand(l:file)]
      else
        let l:cand = vimtex#kpsewhich#run(l:file)
        if len(l:cand) == 1
          let self.db_files += [l:cand[0]]
        endif
      endif
    endfor
  endif

  return self.db_files
endfunction

" }}}1
function! s:qf.get_filename(name) abort dict " {{{1
  if !filereadable(a:name)
    for l:root in [self.root, b:vimtex.root]
      let l:candidate = fnamemodify(simplify(l:root . '/' . a:name), ':.')
      if filereadable(l:candidate)
        return l:candidate
      endif
    endfor
  endif

  return get(self, '__state_bibfile', '')
endfunction

" }}}1
function! s:qf.get_key_pos(key) abort dict " {{{1
  for l:file in self.get_db_files()
    let l:lnum = self.get_key_lnum(a:key, l:file)
    if l:lnum > 0
      return [l:file, l:lnum]
    endif
  endfor

  return []
endfunction

" }}}1
function! s:qf.get_key_lnum(key, filename) abort dict " {{{1
  if !filereadable(a:filename) | return 0 | endif

  let l:lines = readfile(a:filename)
  let l:lnums = range(len(l:lines))
  let l:annotated_lines = map(l:lnums, '[v:val, l:lines[v:val]]')
  let l:matches = filter(l:annotated_lines,
        \ {_, x -> x[1] =~# '^\s*@\w*{\s*\V' . a:key})

  return len(l:matches) > 0 ? l:matches[-1][0]+1 : 0
endfunction

" }}}1
function! s:qf.get_entry_key(filename, lnum) abort dict " {{{1
  for l:file in self.get_db_files()
    if fnamemodify(l:file, ':t') !=# a:filename | continue | endif

    let l:entry = get(filter(readfile(l:file, 0, a:lnum), 'v:val =~# ''^@'''), -1)
    if empty(l:entry) | continue | endif

    return matchstr(l:entry, '{\v\zs.{-}\ze(,|$)')
  endfor

  return ''
endfunction

" }}}1

"
" Parsers for the various warning types
"

let s:type_parse_data_source = {}
function! s:type_parse_data_source.fix(ctx, entry) abort " {{{1
  if a:entry.text =~# 'INFO - Found BibTeX data source'
    let a:entry.removeThis = 1

    let l:file = matchstr(a:entry.text, 'BibTeX data source ''\zs.*\ze''$')
    let a:ctx.__state_bibfile = l:file

    return 1
  endif
endfunction

" }}}1

let s:type_parse_error = {}
function! s:type_parse_error.fix(ctx, entry) abort " {{{1
  if a:entry.text =~# 'ERROR - BibTeX subsystem.*expected end of entry'
    let l:matches = matchlist(a:entry.text, '\v(\S*\.%(bib|utf8)).*line (\d+)')
    if empty(l:matches) | return 1 | endif

    let a:entry.filename = a:ctx.get_filename(fnamemodify(l:matches[1], ':t'))
    let a:entry.lnum = l:matches[2]

    if empty(a:entry.filename)
      unlet a:entry.filename
      let l:key = ''
    else
      " Use filename and line number to get entry name
      let l:key = a:ctx.get_entry_key(a:entry.filename, a:entry.lnum)
    endif

    if !empty(l:key)
      let a:entry.text = 'biblatex: Error parsing entry with key "' . l:key . '"'
    else
      let a:entry.text = 'biblatex: Error parsing entry ('
            \ . matchstr(a:entry.text, '\vsyntax error: \zs.*\ze \(skipping')
            \ . ')'
    endif

    return 1
  endif
endfunction

" }}}1

let s:type_duplicate = {}
function! s:type_duplicate.fix(ctx, entry) abort " {{{1
  if a:entry.text =~# 'WARN - Duplicate entry'
    let l:matches = matchlist(a:entry.text, '\v: ''(\S*)'' in file ''(.{-})''')
    let l:key = l:matches[1]
    let a:entry.filename = a:ctx.get_filename(l:matches[2])
    let a:entry.lnum = a:ctx.get_key_lnum(l:key, a:entry.filename)
    let a:entry.text = 'biblatex: Duplicate entry key "' . l:key . '"'
    return 1
  endif
endfunction

" }}}1

let s:type_no_driver = {}
function! s:type_no_driver.fix(ctx, entry) abort " {{{1
  if a:entry.text =~# 'No driver for entry type'
    let l:key = matchstr(a:entry.text, 'entry type ''\v\zs.{-}\ze''')
    let a:entry.text = 'biblatex: Using fallback driver for ''' . l:key . ''''

    let l:pos = a:ctx.get_key_pos(l:key)
    if !empty(l:pos)
      let a:entry.filename = a:ctx.get_filename(l:pos[0])
      let a:entry.lnum = l:pos[1]
      if has_key(a:entry, 'bufnr')
        unlet a:entry.bufnr
      endif
    endif

    return 1
  endif
endfunction

" }}}1

let s:type_not_found = {}
function! s:type_not_found.fix(ctx, entry) abort " {{{1
  if a:entry.text =~# 'The following entry could not be found'
    let l:key = split(a:entry.text, ' ')[-1]
    let a:entry.text = 'biblatex: Entry with key ''' . l:key . ''' not found'

    for [l:file, l:lnum, l:line] in vimtex#parser#tex(b:vimtex.tex)
      if l:line =~# g:vimtex#re#not_comment . '\\\S*\V' . l:key
        let a:entry.lnum = l:lnum
        let a:entry.filename = l:file
        unlet a:entry.bufnr
        break
      endif
    endfor

    return 1
  endif
endfunction

" }}}1

let s:type_encoding = {}
function! s:type_encoding.fix(ctx, entry) abort " {{{1
  if a:entry.text =~# 'The entry .* has characters which cannot'
    let l:key = matchstr(a:entry.text, 'The entry ''\v\zs.{-}\ze''')
    let a:entry.text = 'biblatex: Entry with key ''' . l:key . ''' has non-ascii characters'

    let l:pos = a:ctx.get_key_pos(l:key)
    if !empty(l:pos)
      let a:entry.filename = a:ctx.get_filename(l:pos[0])
      let a:entry.lnum = l:pos[1]
      if has_key(a:entry, 'bufnr')
        unlet a:entry.bufnr
      endif
    endif

    return 1
  endif
endfunction

" }}}1