summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/biblatex/latex/blx-bibtex.def
blob: c333e5f17d30b3fadb40f355e85eadc4ad6b2a7d (plain)
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
\ProvidesFile{blx-bibtex.def}
[\abx@cptid]

\blx@warning@noline{%
  Using fall-back BibTeX(8) backend:\MessageBreak
  functionality may be reduced/unavailable%
}

% Initialisation of backend-related switches, variables, etc.
\newtoggle{blx@runbtx}

% No interface provided for this but can be set 'in emergency' by user
\def\blx@maxline{79}

% Rename write stream so use is clear
\let\blx@write\blx@bcfout

% Data file initialisation
\edef\blx@auxfile{\jobname}
\let\blx@aux\@mainaux
\newcommand*{\blxauxsuffix}{-blx}

\begingroup
\def\blx@tempa#1"#2{%
  #1\ifx#2\@empty\else
    \expandafter\blx@tempa
  \fi#2}
\edef\blx@ctrlfile{%
  \noexpand\blx@tempa
  \expandafter\blx@tempa\jobname"\@empty
  \space\noexpand\@empty}
\def\blx@tempa#1 #2{%
  #1\ifx#2\@empty\else
    \string_\expandafter\blx@tempa
  \fi#2}
\xdef\blx@ctrlfile{\blx@ctrlfile}
\endgroup

\def\blx@auxinit#1{%
  \blx@auxwrite\blx@aux
    {\def\do##1{,\blx@stripbib{##1}}}
    {\ifx\blx@aux\@mainaux
     \else
       \blx@msg@aux
     \fi
     \string\bibstyle{biblatex}\blx@nl
     \string\bibdata{%
       \blx@ctrlfile\blxauxsuffix
       \ifx#1\@empty
       \else
         \dolistloop#1%
       \fi}\blx@nl
     \string\citation{biblatex-control}}}

\def\blx@sig@bib{@Comment{$ biblatex control file $}}
\edef\blx@ver@bib{%
  @Comment{$ biblatex bcf format version \blx@bcfversion\space $}}

\let\blx@sig@aux\blx@sig@bbl
\let\blx@ver@aux\blx@ver@bbl

\edef\blx@msg@text{%
  \@percentchar\space Do not modify this file!\blx@nl
  \@percentchar\blx@nl
  \@percentchar\space This is an auxiliary file
  used by the 'biblatex' package.\blx@nl
  \@percentchar\space This file may safely be deleted.
  It will be recreated as\blx@nl
  \@percentchar\space required.\blx@nl}

\edef\blx@msg@aux{%
  \blx@sig@aux\blx@nl
  \blx@ver@aux\blx@nl
  \blx@msg@text
  \@percentchar\blx@nl\string\relax\blx@nl}
\edef\blx@msg@bib{%
  \blx@sig@bib\blx@nl
  \blx@ver@bib\blx@nl
  \blx@msg@text\blx@nl}

% User feedback

\renewrobustcmd*{\RequireBiber}[1][2]{%
  \ifnumgreater{#1}\blx@reqbiber
    {\numgdef\blx@reqbiber{#1}}
    {}%
    \blx@checkbackend{style}}
\@onlypreamble\RequireBiber
\let\blx@reqbiber\z@

\def\blx@checkbackend#1{%
  \ifnum\blx@reqbiber>2 %
     \blx@error
       {Biber backend is required by #1}
       {The selected style or one of the .bib files
        requires Biber.\MessageBreak It will
        not work at all with BibTeX.}%
   \fi}

\def\blx@check@logreq{%
  \begingroup
  \ltxrequest{biblatex}{{\iftoggle{blx@runltx}{1}{0}}}{%
    \provides[type=dynamic]{
      \def\do##1{\file{##1.aux}}
      \dolistloop\blx@list@inactive
      \file{\blx@ctrlfile\blxauxsuffix.bib}
    }
    \requires[type=dynamic]{
      \def\do##1{\file{##1.bbl}}
      \dolistloop\blx@list@inactive
    }
    \ifdef\blx@list@req@edit
      {\requires[type=editable]{
         \forlistloop\file\blx@list@req@edit
       }}
      {}
    \ifdef\blx@list@req@stat
      {\requires[type=static]{
         \forlistloop\file\blx@list@req@stat
       }}
      {}
  }%
  \def\do##1{%
    \ifinlist{##1}{\blx@list@active}
      {\blx@logreq@bibtex{1}{##1}}
      {\blx@logreq@bibtex{0}{##1}}}%
  \dolistloop\blx@list@inactive
  \endgroup}

\def\blx@logreq@bibtex#1#2{%
  \logrequest[package=biblatex,priority=5,active=#1]{%
    \generic{bibtex}
    \cmdline{%
      \iftoggle{blx@bibtex8}
        {\binary{bibtex8}
         \option{--wolfgang}
         \option{--min\string_crossrefs \blx@mincrossrefs}}
        {\binary{bibtex}
         \option{-min-crossrefs \blx@mincrossrefs}}%
      \infile{#2}
     }
     \input{
       \file{#2.aux}
     }
     \output{
       \file{#2.bbl}
     }
      \provides[type=dynamic]{
        \file{#2.bbl}
      }
      \requires[type=dynamic]{
        \file{#2.aux}
        \file{\blx@ctrlfile\blxauxsuffix.bib}
      }
      \requires[type=editable]{
        \ifcsdef{blx@list@bibfiles@#2}
          {\def\do{\file}
           \dolistcsloop{blx@list@bibfiles@#2}}
          {}
      }
      \requires[type=static]{
        \file{biblatex.bst}}
  }%
}

\def\blx@logreq@active#1{%
  \xifinlist{\blx@auxfile}{\blx@list@active}
    {}
    {\listxadd\blx@list@active{\blx@auxfile}}%
  \ifblank{#1}
    {}
    {\@latex@warning{#1}}%
  \blx@rerun@latex
  \blx@rerun@bibtex}

\def\blx@logreq@inactive{%
  \xifinlist{\blx@auxfile}{\blx@list@inactive}
    {}
    {\listxadd\blx@list@inactive{\blx@auxfile}}}

\def\blx@rerun@bibtex{%
  \global\toggletrue{blx@runbtx}%
  \global\let\blx@rerun@bibtex\relax}

% Only the toggle name changes here
\patchcmd{\blx@check@rerun}{blx@runbiber}{blx@runbtx}{}{}

\def\blx@warn@auxlist{%
  \begingroup
  \edef\blx@tempa{Please (re)run BibTeX on the file(s):}%
  \def\do##1{\appto\blx@tempa{\MessageBreak##1}}%
  \dolistloop\blx@list@active
  \blx@warning@noline{%
    \blx@tempa\MessageBreak
    and rerun LaTeX afterwards}%
  \endgroup}

\protected\def\blx@bibreq#1{%
  \blx@auxwrite\blx@aux{}{\string\citation{#1}}}

% bibtex doesn't parse ISO8601 dates so these overrides do nothing
\appto\blx@blxinit{%
  \def\iflabeldateera#1{\@secondoftwo}%
  \def\iflabelenddateera#1{\@secondoftwo}%
  \let\iflabeldatecirca\@secondoftwo
  \let\iflabelenddatecirca\@secondoftwo
  \let\iflabeldateuncertain\@secondoftwo
  \let\iflabelenddateuncertain\@secondoftwo}

% {<string>}
% This mustn't be robust as it's likely to be used mainly in tests and so
% needs to be expandable
\renewcommand*\rangelen[1]{%
  \ifblank{#1}
    {0}%
    {\expandafter\blx@rangelen@range\romannumeral-`\q%
      #1\bibrangedash\bibrangedash&%
    }}

\def\blx@rangelen@range#1\bibrangedash#2\bibrangedash#3&{%
  \ifblank{#3}
    {\blx@rangelen@hyphen#1--&}
    {\blx@rangelen@check{#1}{#2}}%
}
\def\blx@rangelen@hyphen#1-#2-#3&{%
  \ifblank{#3}
    {1}% No range at all: assume one page
    {\blx@rangelen@check{#1}{#2}}%
}
\def\blx@rangelen@check#1#2{%
  \expandafter\blx@rangelen@check@aux
    \number\numexpr
      \blx@rangelen@check@int{#2}
      -
      \blx@rangelen@check@int{#1}
      + 1
    \relax
    &\stop
}
\def\blx@rangelen@check@aux#1&#2\stop{%
  \ifblank{#2}
    {#1}
    {-1}%
}
\def\blx@rangelen@check@int#1{%
  \ifblank{#1}
    {0&}
    {%
      \if\number\numexpr0#1-0#1\relax0
        #1
      \else
        0&
      \fi
    }%
}

% Alter message text
\patchcmd{\blx@bbl@warn}{Biber}{BibTeX}{}{}

%% Data input

\patchcmd{\blx@bblinput}{\blx@bblfile}{\blx@bblfile\blx@bblsecs}{}{}

\def\blx@bblfile{%
  \blx@secinit
  \begingroup
  \blx@bblstart
  \ifnum\c@refsection>\z@
    \edef\blx@auxfile{\jobname\the\c@refsection\blxauxsuffix}%
  \else
    \edef\blx@auxfile{\jobname}%
  \fi
  \blx@ifsigned{\blx@auxfile}{bbl}
    {\InputIfFileExists{\blx@auxfile.bbl}
       {\blx@info@noline{... file '\blx@auxfile.bbl' found}%
        \global\toggletrue{blx@bbldone}}
       {\blx@info@noline{... file '\blx@auxfile.bbl' not found}%
        \typeout{No file \blx@auxfile.bbl.}%
        \global\togglefalse{blx@bbldone}}}
    {}%
  \blx@bblend
  \endgroup
  \csnumgdef{blx@labelnumber@\the\c@refsection}{0}}

\def\blx@bblsecs{%
  \advance\c@refsection\@ne
  \ifnum\c@refsection>\blx@maxsection
  \else
    \blx@bblfile
    \expandafter\blx@bblsecs
  \fi}

\def\blx@refsection@ii{%
  \xdef\blx@auxfile{\jobname\the\c@refsection\blxauxsuffix}%
  \blx@regbibfiles\blx@auxfile\blx@bibfiles
  \blx@logreq@inactive
  \blx@ifsigned{\blx@auxfile}{aux}
    {\immediate\openout\blx@write\blx@auxfile.aux\relax
     \global\let\blx@aux\blx@write
     \blx@auxinit\blx@bibfiles}
    {}}

\def\blx@endrefsection{%
  \blx@endrefsegment
  \ifx\blx@aux\blx@write
    \immediate\closeout\blx@write
    \global\let\blx@aux\@mainaux
    \xdef\blx@auxfile{\jobname}%
  \fi
  \global\c@refsection\z@}

% xref support

% {<entrykey>}
\protected\def\blx@bbl@xref#1{%
  \ifcsdef{blx@refp@\the\c@refsection @\detokenize{#1}}
    {}
    {\listcsxadd{blx@refs@\the\c@refsection}{\detokenize{#1}}}%
  \listcsxadd{blx@refp@\the\c@refsection @\detokenize{#1}}{\abx@field@entrykey}%
  \csxdef{blx@refc@\the\c@refsection @\abx@field@entrykey}{\detokenize{#1}}}

\def\blx@addxref#1{%
  \blx@ifdata{#1}
    {\begingroup
     \def\do##1{%
       \csgappto{blx@data@\the\c@refsection @##1}{%
         \def\abx@field@xref{#1}}}%
     \dolistcsloop{blx@refp@\the\c@refsection @#1}%
     \endgroup}
    {}%
  \global\csundef{blx@refp@\the\c@refsection @#1}}

\def\blx@bblend{%
  \ifcsdef{blx@refs@\the\c@refsection}%
    {\begingroup
     \let\do\blx@addxref
     \dolistcsloop{blx@refs@\the\c@refsection}%
     \endgroup
     \global\csundef{blx@refs@\the\c@refsection}}
    {}%
  \iftoggle{blx@addset}
    {\blx@addset
     \global\togglefalse{blx@addset}}
    {}}

\def\blx@citation#1#2{%
  \ifbool{citerequest}
    {\ifcsdef{blx@setp@\the\c@refsection @#1}
       {\blx@citation@set{#1}{#2}}
       {\ifcsdef{blx@setc@\the\c@refsection @#1}
         {\blx@citation@inset{#1}{#2}}
         {\blx@citation@entry{#1}{#2}}}%
     \ifcsdef{blx@refc@\the\c@refsection @#1}%
       {\blx@citation@xref{#1}}
       {}}
    {}}

\def\blx@citation@set#1#2{%
  \blx@citation@entry{#1}{#2}%
  \begingroup
  \def\do##1{\blx@citation@entry{##1}\blx@msg@cundef}%
  \expandafter\expandafter\expandafter\docsvlist
  \expandafter\expandafter\expandafter{%
    \csname blx@setp@\the\c@refsection @#1\endcsname}%
  \endgroup}

\def\blx@citation@inset#1#2{%
  \blx@citation@entry{#1}{#2}%
  \expandafter\expandafter\expandafter\blx@citation@inset@i
  \expandafter\expandafter\expandafter{%
    \csname blx@setc@\the\c@refsection @#1\endcsname}\blx@msg@cundef}

\def\blx@citation@inset@i#1{%
  \ifcsdef{blx@setp@\the\c@refsection @#1}
    {\blx@citation@set{#1}}
    {\blx@citation@entry{#1}}}

\def\blx@citation@xref#1{%
  \begingroup
  \edef\blx@tempa{blx@refp@\the\c@refsection @%
          \csname blx@refc@\the\c@refsection @#1\endcsname}%
  \ifcsdef\blx@tempa
    {\ifinlistcs{#1}\blx@tempa
       {}
       {\listcsxadd\blx@tempa{#1}}%
     \blx@tempcnta\z@
     \def\do##1{\advance\blx@tempcnta\@ne}%
     \dolistcsloop\blx@tempa}
    {\listcsxadd\blx@tempa{#1}%
     \blx@tempcnta\@ne}%
  \expandafter\endgroup\ifnum\blx@tempcnta<\blx@minxrefs\relax
  \else
    \expandafter\expandafter\expandafter\blx@citation@entry
    \expandafter\expandafter\expandafter{%
      \csname blx@refc@\the\c@refsection @#1\endcsname}\blx@msg@cundef
  \fi}

% refcontexts are out, but we can try to save labelprefix

% Luckily all refcontext names use \blx@refcontext@labelprefix, so if we
% don't modify that macro, we don't actually change the refcontext.
% Remember that \blx@refcontext@labelprefix contains a hash of the labelprefix
% and that \blx@refcontext@labelprefix@real contains the value.
% Effectively we always stay in \blx@sorting/global/global//global/global,
% and additionally carry around the labelprefix value
% in \blx@refcontext@labelprefix@real and pass it to the aux file.
% There are differences to the result obtained with Biber,
% but it should be good enough for very simple applications.
\define@key{blx@refcontext}{labelprefix}{%
  \xdef\blx@refcontext@labelprefix@real{#1}%
  \iftoggle{blx@defernumbers}
    {}%
    {\iftoggle{blx@labelnumber}
       {\blx@warning{%
          Option 'labelprefix' requires global\MessageBreak
          'defernumbers=true'}}
       {}}%
  \ifundef{\blx@bibtexwarn@labelprefix@used}
    {\blx@warning@noline{%
       The option 'labelprefix' is limited\MessageBreak
       when used with BibTeX.\MessageBreak
       Consider using Biber instead}%
     \global\let\blx@bibtexwarn@labelprefix@used\@empty}
    {}}

\def\blx@warn@bibtexrefcontext#1{%
  \blx@warning{%
    The refcontext option\MessageBreak
    '#1'\MessageBreak
    is not supported by the BibTeX backend.\MessageBreak
    Consider switching to Biber instead.\MessageBreak
    The option will be ignored}}

\def\blx@bibtexrefcontext@disable#1{%
  \define@key{blx@refcontext}{#1}{%
    \blx@warn@bibtexrefcontext{#1}}}

\forcsvlist{\blx@bibtexrefcontext@disable}{%
  nametemplates,sorting,sortingnamekeytemplatename,uniquenametemplatename,
  labelalphanametemplatename}

\def\blx@assignrefcontext@disable#1{%
  \blx@warning{%
    BibTeX does not support refcontext assignments with\MessageBreak
    '\string\assignrefcontext#1'.\MessageBreak
    Consider using Biber instead.\MessageBreak
    Ignoring the refconext assignment}}

\def\blx@assignrefcontextkeyws[#1][#2]#3{%
  \blx@assignrefcontext@disable{keyws}}
\def\blx@assignrefcontextcats[#1][#2]#3{%
  \blx@assignrefcontext@disable{cats}}
\def\blx@assignrefcontextentries[#1][#2]#3{%
  \blx@assignrefcontext@disable{entries}}

\apptocmd{\blx@setdefaultrefcontext}
  {\csxdef{blx@assignedlabelprefix@\the\c@refsection @#1}{%
     \blx@refcontext@labelprefix@real}}
  {}
  {\blx@error
     {Failed to patch '\string\blx@setdefaultrefcontext'}
     {Patching '\string\blx@setdefaultrefcontext' failed for some reason.
      \MessageBreak
      Please report this bug}}

\def\abx@aux@defaultlabelprefix#1#2#3{%
  \csgdef{blx@assignedlabelprefix@#1@#2}{#3}}

\apptocmd{\blx@bbl@entry}
  {\ifcsundef{blx@assignedlabelprefix@\the\c@refsection @\thefield{entrykey}}
     {}
     {\blx@bbl@addfield{labelprefix}{%
        \csuse{blx@assignedlabelprefix@\the\c@refsection
          @\thefield{entrykey}}}}}
  {}
  {\blx@error
     {Failed to patch '\string\blx@bbl@entry'}
     {Patching '\string\blx@bbl@entry' failed for some reason.
      \MessageBreak
      Please report this bug}}

\AtEndDocument{%
  % always add default labelprefix declarations to .aux
  \iftoggle{blx@bbldone}
    {\def\do#1{%
       \begingroup
       \def\do##1{%
         \blx@auxwrite\@mainaux{}{\string\abx@aux@defaultlabelprefix{#1}{##1}{%
           \csuse{blx@assignedlabelprefix@#1@##1}}}}%
       \dolistcsloop{blx@defaultrefcontexts@#1}%
       \endgroup}
     \dolistloop\blx@allrefsections}
    {}}


% Translate sorting schemes to BibTeX form
\def\blx@sorting@none{0}
\def\blx@sorting@nty{1}
\def\blx@sorting@nyt{2}
\def\blx@sorting@nyvt{3}
\def\blx@sorting@anyt{12}
\def\blx@sorting@anyvt{13}
\def\blx@sorting@ynt{21}
\def\blx@sorting@ydnt{22}
\def\blx@sorting@debug{99}

% Control file

\edef\blx@ctrl{%
  \blx@msg@bib
  @Control\string{biblatex-control,\blx@nl
  \space\space options = \string{%
    \blx@bcfversion:%
    \noexpand\iftoggle{blx@debug}{1}{0}:%
    \noexpand\iftoggle{blx@bibtex8}{1}{0}:%
    \noexpand\iftoggle{blx@sortcase}{1}{0}:%
    \noexpand\iftoggle{blx@useprefix}{1}{0}:%
    \noexpand\iftoggle{blx@useauthor}{1}{0}:%
    \noexpand\iftoggle{blx@useeditor}{1}{0}:%
    \noexpand\iftoggle{blx@usetranslator}{1}{0}:%
    \noexpand\iftoggle{blx@labelalpha}{1}{0}:%
    \noexpand\iftoggle{blx@labeldateparts}{1}{0}:%
    \noexpand\iftoggle{blx@singletitle}{1}{0}:%
    \noexpand\csuse{blx@sorting@\noexpand\blx@sorting}:%
    \noexpand\blx@maxcitenames:%
    \noexpand\blx@mincitenames:%
    \noexpand\blx@maxsortnames:%
    \noexpand\blx@minsortnames:%
    \noexpand\iftoggle{blx@nohashothers}{1}{0}:%
    \noexpand\iftoggle{blx@nosortothers}{1}{0}:%
    \noexpand\blx@maxalphanames:%
    \noexpand\blx@minalphanames:%
    \noexpand\blx@maxline:%
    \noexpand\detokenize\noexpand\expandafter{\noexpand\labelalphaothers}:%
    \noexpand\detokenize\noexpand\expandafter{\noexpand\blx@sortalphaothers}:%
    \noexpand\detokenize\noexpand\expandafter{\noexpand\blx@sorting}%
  \string},\blx@nl
  \string}%
}

\def\blx@ctrlwrite{%
  % \sortalphaothers should only contain safe characters,
  % so we should be able to \edef it
  \edef\blx@sortalphaothers{\sortalphaothers}%
  \immediate\openout\blx@write\blx@ctrlfile\blxauxsuffix.bib\relax
  \blx@auxwrite\blx@write{}{\blx@ctrl}%
  \immediate\closeout\blx@write}

% Disable some interfaces

\renewrobustcmd*{\DeclareSortingScheme}[3][]{}
\renewrobustcmd*{\DeclareExtradate}[1]{}

\def\blx@checkencoding{}

\AtBeginDocument{%
  \blx@checkencoding
  \if@filesw
    \blx@auxinit\blx@bibfiles
  \fi}

\endinput