bibl-bib.mkxl /size: 32 Kb    last modification: 2021-10-28 13:50
1%D \module
2%D   [       file=bibl-bib,
3%D        version=2007.08.17,
4%D          title=\CONTEXT\ Bibliography Support,
5%D       subtitle=Initialization,
6%D         author=Hans Hagen \& Taco Hoekwater,
7%D           date=\currentdate,
8%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
9%C
10%C This module is part of the \CONTEXT\ macro||package and is
11%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
12%C details.
13
14%D This is really obsolete in \LMTX. For now I keep it but it will either go
15%D away. The code is not upgraded, we only deal with macro protection. I will
16%D only update the bits that annoy me in grepping for possible optimizations.
17
18\writestatus{loading}{ConTeXt Bibliography Support / Experimental BibTeX}
19
20\registerctxluafile{bibl-bib}{}
21
22\unprotect
23
24% todo: et al limiters
25% todo: split: citationvariant and publicationvariant
26
27%D This interface is under development. As I don't use \BIBTEX\ myself I need some
28%D motivation to spend time on it, and an occasional question on the list can be a
29%D reason. A few examples. As \BIBTEX\ databases can be poluted by local commands,
30%D we need to catch:
31%D
32%D \startbuffer
33%D \defbibtexcommand\MF  {MF}
34%D \defbibtexcommand\MP  {MP}
35%D \defbibtexcommand\TUB {TUGboat}
36%D \defbibtexcommand\Mc  {Mac}
37%D \defbibtexcommand\sltt{\tt}
38%D \defbibtexcommand\<#1>{\type{#1}}
39%D \defbibtexcommand\acro#1{#1}
40%D \stopbuffer
41%D
42%D \typebuffer
43%D
44%D Let's define a session and load a few databases. We convert to \UTF\ and strip
45%D commands.
46%D
47%D \startbuffer
48%D \definebibtexsession  [somebibtex]
49%D \registerbibtexfile   [somebibtex] [tugboat.bib]
50%D \registerbibtexfile   [somebibtex] [komoedie.bib]
51%D \preparebibtexsession [somebibtex] [convert,strip]
52%D \stopbuffer
53%D
54%D \typebuffer
55%D
56%D This loads an mapping (work in progress):
57%D
58%D \startbuffer
59%D \def\currentbibtexformat{apa} \input bxml-\currentbibtexformat.mkiv
60%D \stopbuffer
61%D
62%D \typebuffer
63%D
64%D There are several ways to handle the \XML. It helps if you're a bit familiar with
65%D \XML\ processing in \MKIV.
66%D
67%D Here we regular setups. Three elements are mapped but only one is actually used
68%D and applied to root element \type {/bibtex}.
69%D
70%D \startbuffer
71%D \startxmlsetups bibtex
72%D     \xmlregistereddocumentsetups{#1}{}
73%D     \xmlsetsetup{#1}{bibtex|entry|field}{bibtex:*}
74%D     \xmlmain{#1}
75%D \stopxmlsetups
76%D
77%D \startxmlsetups bibtex:bibtex
78%D     \xmlfilter{#1}{
79%D         /entry[@category='article']
80%D         /field[@name='author' and (find(text(),'Hagen') or find(text(),'Hoekwater'))]
81%D         /../command(bibtex:format)
82%D     }
83%D \stopxmlsetups
84%D
85%D \applytobibtexsession[somebibtex][bibtex]
86%D \stopbuffer
87%D
88%D \typebuffer
89%D
90%D A simpler setup is given next. Here we just apply a setup to the root element
91%D directly:
92%D
93%D \startbuffer
94%D \startxmlsetups bibtex:list
95%D     \xmlfilter{#1}{/bibtex/entry/command(bibtex:format)}
96%D \stopxmlsetups
97%D
98%D \applytobibtexsession[somebibtex][bibtex:list]
99%D \stopbuffer
100%D
101%D \typebuffer
102%D
103%D A slightly more complex expression:
104%D
105%D \startbuffer
106%D \startxmlsetups bibtex:filter
107%D     \xmlfilter{#1}{
108%D         /bibtex
109%D         /entry[@category='article']
110%D         /field[@name='author' and (find(text(),'Hagen') or find(text(),'Hoekwater'))]
111%D         /../command(bibtex:format)
112%D     }
113%D \stopxmlsetups
114%D
115%D \applytobibtexsession[somebibtex][bibtex:filter]
116%D \stopbuffer
117%D
118%D \typebuffer
119
120\newtoks  \everydefinebibtexsession
121\newtoks  \everypreparebibtexsession
122\newtoks  \everysetupbibtexsession
123\setfalse \tracebibtexformat
124
125\protected\def\definebibtexsession {\dosingleargument\dodefinebibtexsession}
126          \def\preparebibtexsession{\dodoubleempty   \dopreparebibtexsession}
127\protected\def\setupbibtexsession  {\dodoubleargument\dosetupbibtexsession}
128
129\def\dodefinebibtexsession     [#1]{\edef\currentbibtexsession{#1}%
130                                    \ctxcommand{definebibtexsession("#1")}%
131                                    \the\everydefinebibtexsession}
132
133\def\dopreparebibtexsession[#1][#2]{\edef\currentbibtexsession{#1}%
134                                    \ctxcommand{preparebibtexsession("#1","bibtex:#1","#2")}%
135                                    \the\everypreparebibtexsession}
136
137\def\dosetupbibtexsession  [#1][#2]{\edef\currentbibtexsession{#1}%
138                                    \getparameters[\??pb#1][#2]%
139                                    \the\everysetupbibtexsession}
140
141\def\registerbibtexfile            {\dodoubleargument\doregisterbibtexfile}
142\def\registerbibtexentry           {\dodoubleargument\doregisterbibtexentry}
143\def\applytobibtexsession          {\dodoubleargument\doapplytobibtexsession}
144
145\def\doregisterbibtexfile  [#1][#2]{\ctxcommand{registerbibtexfile("#1","#2")}}
146\def\doregisterbibtexentry [#1][#2]{\ctxcommand{registerbibtexentry("#1","#2")}}
147\def\doapplytobibtexsession[#1][#2]{\xmlprocess{bibtex:#1}{#2}{#2}}
148
149\protected\def\bibtexcommand#1%
150  {\ifcsname\??pb:c:#1\endcsname \else
151     \fakebibtexcommand{#1}%
152   \fi
153   \csname\??pb:c:#1\endcsname}
154
155\def\fakebibtexcommand#1%
156  {\ifcsname#1\endcsname
157     \writestatus{bibtex}{unknown command: #1, using built-in context variant}%
158     \setugvalue{\??pb:c:#1}{\dosomebibtexcommand{#1}}%
159   \else
160     \writestatus{bibtex}{unknown command: #1}%
161     \setugvalue{\??pb:c:#1}{\dofakebibtexcommand{#1}}%
162   \fi}
163
164\let\dosomebibtexcommand  \getvalue
165\def\dofakebibtexcommand#1{{\tttf#1}}
166\def\defbibtexcommand   #1{\setuvalue{\strippedcsname#1}}
167
168\def\bibxmldoifelse#1{\xmldoifelse\currentbibxmlnode{/field[@name='#1']}}
169\def\bibxmldoif    #1{\xmldoif    \currentbibxmlnode{/field[@name='#1']}}
170\def\bibxmldoifnot #1{\xmldoifnot \currentbibxmlnode{/field[@name='#1']}}
171\def\bibxmlflush   #1{\xmlcontext \currentbibxmlnode{/field[@name='#1']}}
172\def\bibxmlsetup     {\xmlsetup   \currentbibxmlnode} % {#1}
173
174\def\currentbibtexformat{apa}    % how to interface this, maybe split loading and key
175\def\currentbibxmlnode  {unset}
176\def\currentbibxmltag   {unset}
177
178\startxmlsetups bibtex
179    \xmlregistereddocumentsetups{#1}{}
180    \xmlsetsetup{#1}{bibtex|entry|field}{bibtex:*}
181    \xmlmain{#1}
182\stopxmlsetups
183
184\startxmlsetups bibtex:format
185    \bibtexpublicationsparameter\c!before\relax % prevents lookahead
186    \edef\currentbibxmlnode    {#1}
187    \edef\currentbibxmltag     {\xmlatt{#1}{tag}}
188    \edef\currentbibxmlcategory{\xmlatt{#1}{category}}
189    \ifconditional\tracebibtexformat
190        \tracedbibxmlintro\currentbibxmltag
191        \tracedbibxmlintro\currentbibxmlcategory
192    \fi
193    \ignorespaces
194    \xmlcommand{#1}{.}{bibtex:\currentbibtexformat:\currentbibxmlcategory}
195    \removeunwantedspaces
196    \bibtexpublicationsparameter\c!after\relax % prevents lookahead
197\stopxmlsetups
198
199\startxmlsetups bibtex:list
200    \xmlfilter{#1}{/bibtex/entry/command(bibtex:format)}
201\stopxmlsetups
202
203\startxmlsetups bibtex:bibtex
204    \xmlfilter{#1}{/entry/command(bibtex:format)}
205\stopxmlsetups
206
207% formatters
208
209\let\normalbibxmlflush\bibxmlflush
210
211\definecolor[bibtextracecolor:field]   [darkred]
212\definecolor[bibtextracecolor:crossref][darkblue]
213\definecolor[bibtextracecolor:key]     [darkgreen]
214
215\def\tracedbibxmlintro   #1{{\tttf#1 -> }}
216\def\tracedbibxmlflush   #1{\color[bibtextracecolor:field]   {\tttf[#1]}}
217\def\tracedbibxmltexts   #1{\color[bibtextracecolor:field]   {\tttf<#1>}}
218\def\tracedbibxmlcrossref#1{\color[bibtextracecolor:crossref]{\tttf#1}}
219\def\tracedbibxmlkey     #1{\color[bibtextracecolor:key]     {\tttf#1}}
220
221\def\tracedbibxmltext
222  {\ifconditional\tracebibtexformat
223     \expandafter\tracedbibxmltexts % plural
224   \else
225     \expandafter\firstofoneargument
226   \fi}
227
228\def\bibxmlflush
229  {\ifconditional\tracebibtexformat
230     \expandafter\tracedbibxmlflush
231   \else
232     \expandafter\normalbibxmlflush
233   \fi}
234
235\startxmlsetups bibtex:format:crossref
236    \ifconditional\tracebibtexformat
237        \tracedbibxmlcrossref{\xmlfirst\currentbibxmlnode{/field[@name='crossref']/lower()}}
238    \else
239        \cite[\xmlfirst\currentbibxmlnode{/field[@name='crossref']/lower()}]
240    \fi
241\stopxmlsetups
242
243\startxmlsetups bibtex:format:key
244    \ifconditional\tracebibtexformat
245        \tracedbibxmlkey{\normalbibxmlflush{key}}
246    \else
247        \bibxmlflush{key}
248    \fi
249\stopxmlsetups
250
251\startxmlsetups bibtex:format:common:author
252   \ifconditional\tracebibtexformat
253        \bibxmlflush\currentbibtexvariant
254   \else
255        \xmlfilter{#1}{/field[@name='\currentbibtexvariant']/bibtexconcat('\currentbibtexvariant')}
256    \fi
257\stopxmlsetups
258
259\startxmlsetups bibtex:format:author
260    \begingroup
261        \def\currentbibtexvariant{author}
262        \xmlsetup{#1}{bibtex:format:common:author}
263    \endgroup
264\stopxmlsetups
265
266\startxmlsetups bibtex:format:artauthor
267    \begingroup
268        \def\currentbibtexvariant{artauthor}
269        \xmlsetup{#1}{bibtex:format:common:author}
270    \endgroup
271\stopxmlsetups
272
273\startxmlsetups bibtex:format:editor
274    \begingroup
275        \def\currentbibtexvariant{editor}
276        \xmlsetup{#1}{bibtex:format:common:author}
277    \endgroup
278\stopxmlsetups
279
280\startxmlsetups bibtex:format:doi
281%   \bibdoifelse{\@@pb@doi}{#1\expanded{\bibgotoDOI{\@@pb@thekey}{\@@pb@doi}}#2}{#3}
282    *doi*
283\stopxmlsetups
284
285\startxmlsetups bibtex:format:doi
286%   \bibdoifelse{\@@pb@biburl}{#1\expanded{\bibgotoURL{\@@pb@thekey}{\@@pb@biburl}}#2}{#3}
287    *url*
288\stopxmlsetups
289
290\startxmlsetups bibtex:format:month
291%   {\bibdoifelse\@@pb@month
292%      {#1\doifnumberelse\@@pb@month
293%        {\doifconversiondefinedelse\@@pbmonthconversion
294%           {\convertnumber\@@pbmonthconversion\@@pb@month}{\@@pb@month}}%
295%           {\@@pb@month}#2}%
296%      {#3}
297    *month*
298\stopxmlsetups
299
300% lists
301
302\def\bibtexlistprocessor
303  {\ctxlua{bibtex.hacks.add(structures.lists.uservalue("\currentlist",\currentlistindex,"bibref"),\currentlistindex)}}
304
305\appendtoks
306    \definelist[\currentbibtexsession]%
307    \setuplist[\currentbibtexsession][\c!state=\v!start,\c!width=]%
308    \installstructurelistprocessor{\currentbibtexsession:userdata}{\bibtexlistprocessor}%
309\to \everydefinebibtexsession
310
311% \def\installbibtexsorter#1#2%
312%   {\setvalue{\??pb:\c!sort:#1}{#2}}
313
314% \installbibtexsorter\v!no     {no}
315% \installbibtexsorter\v!author {au}
316% \installbibtexsorter\v!title  {ti}
317% \installbibtexsorter\v!short  {ab}
318% \installbibtexsorter\empty    {no}
319% \installbibtexsorter\s!default{no}
320
321% \setupbibtex
322%   [\c!sorttype=\v!cite,
323%    \c!sort=no]
324
325% \protected\def\startpublication#1\stoppublication
326%   {\blank
327%    todo
328%    \blank}
329
330% \let\stoppublication\relax
331
332\protected\def\bibtexspace   {\removeunwantedspaces\space}
333\protected\def\bibtexperiod  {\removeunwantedspaces.\space}
334\protected\def\bibtexcomma   {\removeunwantedspaces,\space}
335\protected\def\bibtexlparent {\removeunwantedspaces\space(}
336\protected\def\bibtexrparent {\removeunwantedspaces)\space}
337\protected\def\bibtexlbracket{\removeunwantedspaces\space[}
338\protected\def\bibtexrbracket{\removeunwantedspaces]\space}
339
340% interfacing
341
342% todo : lang en language
343% todo : directions
344
345% variables
346
347\ifdefined\bibtexblock   \else \newcount\bibtexblock   \fi \bibtexblock\plusone
348\ifdefined\bibtexcounter \else \newcount\bibtexcounter \fi
349
350\newtoks \everysetupbibtexpublications
351\newtoks \everysetupbibtexcitations
352
353\def\bibtexrefprefix{\number\bibtexblock:}
354
355\let\currentbibtexsession\s!default
356\let\currentbibtexvariant\s!default
357
358% parameters: session+variant variant session shared
359
360\def\bibtexpublicationsparameter#1%
361  {\csname
362     \ifcsname\??pb\currentbibtexsession:\currentbibtexvariant#1\endcsname
363       \??pb\currentbibtexsession:\currentbibtexvariant#1%
364     \orelse\ifcsname\??pb:\currentbibtexvariant#1\endcsname
365       \??pb:\currentbibtexvariant#1%
366     \orelse\ifcsname\??pb\currentbibtexsession#1\endcsname
367       \??pb\currentbibtexsession#1%
368     \orelse\ifcsname\??pb#1\endcsname
369       \??pb#1%
370     \else
371       \s!empty
372     \fi
373   \endcsname}
374
375\def\bibtexcitationparameter#1%
376  {\csname
377     \ifcsname\??pv\currentbibtexsession:\currentbibtexvariant#1\endcsname
378       \??pv\currentbibtexsession:\currentbibtexvariant#1%
379     \orelse\ifcsname\??pv:\currentbibtexvariant#1\endcsname
380       \??pv:\currentbibtexvariant#1%
381     \orelse\ifcsname\??pv\currentbibtexsession#1\endcsname
382       \??pv\currentbibtexsession#1%
383     \orelse\ifcsname\??pv#1\endcsname
384       \??pv#1%
385     \else
386       \s!empty
387     \fi
388   \endcsname}
389
390% setup commands
391
392\protected\def\setupbibtexpublications
393  {\let\currentpublicationclass\??pb
394   \let\everysetupbibtexwhatever\everysetupbibtexpublications
395   \dosetupbibtexwhatever}
396
397\protected\def\setupbibtexcitations
398  {\let\currentpublicationclass\??pv
399   \let\everysetupbibtexwhatever\everysetupbibtexcitations
400   \dosetupbibtexwhatever}
401
402\protected\def\setupbibtexpublicationvariants
403  {\let\currentpublicationclass\??pb
404   \let\everysetupbibtexwhatever\everysetupbibtexpublications
405   \dosetupbibtexwhatevervariant}
406
407\protected\def\setupbibtexcitationvariants
408  {\let\currentpublicationclass\??pv
409   \let\everysetupbibtexwhatever\everysetupbibtexcitations
410   \dosetupbibtexwhatevervariant}
411
412\tolerant\def\dosetupbibtexwhatever[#1]#*[#2]% [sessionlist] [setup]
413  {\ifarguments\or
414     % setups
415     \getparameters[\currentpublicationclass][#1]%
416     \the\everysetupbibtexwhatever
417   \or
418     % sessions setups
419     \def\dobtxcommand##1{\getparameters[\currentpublicationclass##1][#2]}%
420     \processcommalist[#1]\dobtxcommand
421   \fi}
422
423\tolerant\def\dosetupbibtexwhatevervariant[#1]#*[#2]#*[#3]% [sessionlist] [variantlist] [setup]
424  {\ifarguments\or
425     % setups
426     \getparameters[\currentpublicationclass][#1]%
427     \the\everysetupbibtexwhatever
428   \or
429     % variants setups
430     \def\dobtxcommand##1{\getparameters[\currentpublicationclass:##1][#2]}%
431     \processcommalist[#1]\dobtxcommand
432   \or
433     % sessions variants setups
434     \def\dobtxcommand##1%
435       {\def\dodobtxcommand####1{\getparameters[\currentpublicationclass##1:####1][#3]}%
436        \processcommalist[#2]\dodobtxcommand}%
437     \processcommalist[#1]\docbtxommand
438   \fi}
439
440% some initializations
441
442\setupbibtexcitationvariants
443  [author,authoryear,authoryears]
444  [\c!namesep={, }]
445
446% loading alternatives (apa etc)
447
448\def\doloadbibtexpublicationalternative
449  {\ifproductionrun
450     \edef\bibtexpublicationsalternative{\@@pbalternative}% parent
451     \ifempty\bibtexpublicationsalternative \else
452       \processcommacommand[\bibtexpublicationsalternative]\dodoloadbibtexpublicationalternative
453       \let\@@pbalternative\empty
454     \fi
455   \fi}
456
457\def\dodoloadbibtexpublicationalternative#1%
458  {\doonlyonce{#1}
459     {\startreadingfile
460      \readsysfile{bxml-#1.mkiv}
461        {\showmessage\m!publications{6}{bxml-#1}}
462        {\showmessage\m!publications{1}{bxml-#1}}%
463      \stopreadingfile}}
464
465\appendtoks
466    \doloadbibtexpublicationalternative
467\to \everysetupbibtexpublications
468
469% we expect at least one invocation of the setup commands
470% because otherwise we always load the apa style even if
471% no publications are used
472%
473% \appendtoks
474%     \doloadbibtexpublicationalternative
475% \to \everystarttext
476
477% whatever, should be key
478
479\def\bibtexleftnumber#1{#1\hfill~}
480
481% testing
482
483% \showmessage\m!publications{5}{#1 is unknown}\secondoftwoarguments}
484
485\let\doifbibreferencefoundelse\secondofthreearguments
486
487% lists
488
489\newtoks\everysetupbibtexlistplacement
490
491% this will change as we need it too often .. we will use context.thebibtexnamesep
492
493\appendtoks
494    \ctxlua {bibtex.authors.setsettings {
495        namesep      = \!!bs\bibtexpublicationsparameter\c!namesep\!!es,
496        lastnamesep  = \!!bs\bibtexpublicationsparameter\c!lastnamesep\!!es,
497        finalnamesep = \!!bs\bibtexpublicationsparameter\c!finalnamesep\!!es,
498        firstnamesep = \!!bs\bibtexpublicationsparameter\c!firstnamesep\!!es,
499        juniorsep    = \!!bs\bibtexpublicationsparameter\c!juniorsep\!!es,
500        vonsep       = \!!bs\bibtexpublicationsparameter\c!vonsep\!!es,
501        surnamesep   = \!!bs\bibtexpublicationsparameter\c!surnamesep\!!es,
502        namesep      = \!!bs\bibtexpublicationsparameter\c!namesep\!!es,
503        lastnamesep  = \!!bs\bibtexpublicationsparameter\c!lastnamesep\!!es,
504        finalnamesep = \!!bs\bibtexpublicationsparameter\c!finalnamesep\!!es,
505        author = {
506            etallimit   = \!!bs\bibtexpublicationsparameter\c!authoretallimit\!!es,
507            etaldisplay = \!!bs\bibtexpublicationsparameter\c!authoretaldisplay\!!es,
508            etaltext    = \!!bs\bibtexpublicationsparameter\c!authoretaltext\!!es,
509        },
510        editor = {
511            etallimit   = \!!bs\bibtexpublicationsparameter\c!editoretallimit\!!es,
512            etaldisplay = \!!bs\bibtexpublicationsparameter\c!editoretaldisplay\!!es,
513            etaltext    = \!!bs\bibtexpublicationsparameter\c!editoretaltext\!!es,
514        },
515        artauthor = {
516            etallimit   = \!!bs\bibtexpublicationsparameter\c!artauthoretallimit\!!es,
517            etaldisplay = \!!bs\bibtexpublicationsparameter\c!artauthoretaldisplay\!!es,
518            etaltext    = \!!bs\bibtexpublicationsparameter\c!artauthoretaltext\!!es,
519        },
520    } }%
521\to \everysetupbibtexlistplacement
522
523\def\completebibtexpublications{\dodoubleempty\docompletebibtexpublications}
524\protected\def\placebibtexpublications   {\dodoubleempty\doplacebibtexpublications}
525
526\def\docompletebibtexpublications[#1][#2]% title might become obsolete, just headtext
527  {\begingroup
528   \edef\currentbibtexsession{#1}%
529   \let\currentlist\currentbibtexsession
530   \setuplist[\currentbibtexsession][\c!criterium=\v!previous,#2]
531   \edef\currentbibtexsessiontitle{\namedlistparameter\currentbibtexsession\c!title}%
532   \ifempty\currentbibtexsessiontitle
533     \normalexpanded{\startnamedsection[\v!chapter][\c!reference=\currentbibtexsession,\c!title={\headtext{\currentbibtexsession}}]}%
534   \else
535     \normalexpanded{\startnamedsection[\v!chapter][\c!reference=\currentbibtexsession,\c!title={\currentbibtexsessiontitle}]}%
536   \fi
537   \dodoplacebibtexpublications
538   \stopnamedsection
539   \endgroup}
540
541\def\doplacebibtexpublications[#1][#2]%
542  {\begingroup
543   \edef\currentbibtexsession{#1}%
544   \let\currentlist\currentbibtexsession
545   \setuplist[\currentbibtexsession][\c!criterium=\v!previous,#2]%
546   \dodoplacebibtexpublications
547   \endgroup}
548
549\def\dodoplacebibtexpublications
550  {\determinelistcharacteristics[\currentbibtexsession]%
551   \the\everysetupbibtexlistplacement
552   \forgetall
553   \typesetbibtexlist
554   \global\advance\bibtexblock\plusone}
555
556\setvalue{\??pb:\c!numbering:\v!short}#1% todo var s -> short tag
557  {\bibtexlistnumberbox{\bibtexpublicationsparameter\c!numbercommand{\bibtexgetshort\currentpublicationtag}}}
558
559\setvalue{\??pb:\c!numbering:\v!bib}#1% todo var n -> number
560  {\bibtexlistnumberbox{\bibtexpublicationsparameter\c!numbercommand{\bibtexgetnumber\currentpublicationtag}}}
561
562\setvalue{\??pb:\c!numbering:\s!unknown}#1%
563  {\bibtexlistnumberbox{\bibtexpublicationsparameter\c!numbercommand{#1}}}
564
565\def\@@pblimitednumber % name
566  {\csname\??pb:\c!numbering:%
567     \ifcsname\??pb:\c!numbering:\currentbibtexnumbering\endcsname
568       \currentbibtexnumbering
569     \else
570       \s!unknown
571     \fi
572   \endcsname}
573
574\appendtoks
575   \edef\currentbibtexnumbering{\bibtexpublicationsparameter\c!numbering}%
576   \ifx\currentbibtexnumbering\v!no
577     \setuplist[\currentbibtexsession][\c!numbercommand=,\c!symbol=\v!none,\c!textcommand=\outdented]%
578   \else
579     \setuplist[\currentbibtexsession][\c!numbercommand=\@@pblimitednumber]%
580   \fi
581\to \everysetupbibtexlistplacement
582
583\newdimen\bibtexnumberwidth
584
585\def\bibtexlistnumberbox{\hbox \ifcase\bibtexnumberwidth\else to \bibtexnumberwidth\fi}
586
587\appendtoks
588   \doifelse{\bibtexpublicationsparameter\c!autohang}\v!yes
589     {\ifx\currentbibtexnumbering\v!short
590        \setbox\scratchbox\hbox{\bibtexpublicationsparameter\c!numbercommand{\bibtexpublicationsparameter\c!samplesize}}%
591      \else
592        \setbox\scratchbox\hbox{\bibtexpublicationsparameter\c!numbercommand{\ctxlua{tex.write(structures.lists.size())}}}%
593      \fi
594      \bibtexnumberwidth\wd\scratchbox
595      \setuplist[\currentbibtexsession][\c!distance=\zeropoint]}
596     {\doifelsenothing{\bibtexpublicationsparameter\c!width}
597        {\bibtexnumberwidth\zeropoint}
598        {\bibtexnumberwidth\bibtexpublicationsparameter\c!width}}%
599   \setuplist[\currentbibtexsession][\c!width=\bibtexnumberwidth]%
600\to \everysetupbibtexlistplacement
601
602\appendtoks
603    \let\maybeyear\gobbleoneargument
604    \let\noopsort \gobbleoneargument
605\to \everysetupbibtexlistplacement
606
607\appendtoks
608   \doifelse{\bibtexpublicationsparameter\c!maybeyear}\v!off
609     {\let\maybeyear\gobbleoneargument}
610     {\let\maybeyear\firstofoneargument}%
611\to \everysetupbibtexlistplacement
612
613\appendtoks
614   \doifnot{\bibtexpublicationsparameter\c!option}\v!continue
615     {\global\bibtexcounter\zerocount}%
616\to \everysetupbibtexlistplacement
617
618\appendtoks
619    \edef\currentbibtexcriterium{\namedlistparameter\currentbibtexsession\c!criterium}%
620\to \everysetupbibtexlistplacement
621
622\def\typesetbibtexlist
623  {\begingroup
624   \startpacked[\v!blank]%
625   \doif{\namedlistparameter\currentbibtexsession\c!criterium}\v!cite
626     {\setuplist[\currentbibtexsession][\c!criterium=\v!here]}%
627   \doifelse{\bibtexpublicationsparameter\c!method}\v!local
628     {\ctxlua{bibtex.hacks.reset(1)}}% function can take method
629     {\ctxlua{bibtex.hacks.reset(2)}}%
630   \strc_lists_place_current
631     {\currentbibtexsession}
632     {\currentbibtexcriterium}
633     {}%
634     {\namedlistparameter\currentbibtexsession\c!extras}%
635     {\namedlistparameter\currentbibtexsession\c!order}%
636   \ctxlua{bibtex.hacks.flush("\bibtexpublicationsparameter\c!sorttype")}%
637   \stoppacked
638   \endgroup}
639
640\protected\def\typesetbibtexpublication#1%
641  {\edef\currentbibtexsessiontag{#1}%
642   \ifempty\currentbibtexsessiontag
643     % can't really happen
644   \orelse\ifx\currentbibtexcriterium\v!all
645     \dotypesetbibtexpublication % was \doplacepublicationindeed
646   \else
647     \ctxlua{bibtex.hacks.doifalreadyplaced("\currentbibtexsessiontag")}
648       \donothing
649       \dotypesetbibtexpublication
650   \fi}
651
652\def\dotypesetbibtexpublication
653  {\doifelsebibreferencefound\currentbibtexsessiontag
654     {\global\advance\bibtexcounter\plusone
655      \ctxlua{bibtex.hacks.registerplaced("\currentbibtexsessiontag")}%
656      \let\currentlist\currentbibtexsession
657      \let\currentlistentrynumber    \bibtexcounter
658      \let\currentlistentrytitle     \thebibtexpublicationlistelement
659      \let\currentlistentrypagenumber\empty
660      \strc_lists_apply_renderingsetup}
661     {}} % invalid
662
663\def\thebibtexpublicationlistelement
664   {\strut
665    \expanded{\reference[\bibtexrefprefix\currentbibtexsessiontag]{\number\bibtexcounter}}%
666    \dotypesetabibtexpublication\currentbibtexsessiontag
667    \strut}
668
669\def\dotypesetabibtexpublication#1%
670  {\begingroup
671   \ignorespaces
672   \xmlfilter{bibtex:\currentbibtexsession}{/bibtex/entry[@tag='#1']/command(bibtex:format)}%
673   \removeunwantedspaces
674%  \ignorespaces
675%  \bibalternative{\bibgetvart{#1}}%
676%  \removeunwantedspaces
677   \endgroup}
678
679\def\doprocessbibtexentry#1{\typesetbibtexpublication{#1}}
680
681% citations
682
683\protected\def\bibtexcitation[#1]%
684  {\edef\currentbibtexsession{#1}%
685   \strictdoifelsenextoptional\dobibtexcitation\dobibtexref}
686
687\def\dobibtexref#1%
688  {\dodobibtexcitation[#1][]}
689
690\def\dobibtexcitation[#1]%
691  {\strictdoifelsenextoptional{\dodobibtexcitation[#1]}{\dodobibtexcitation[#1][]}}
692
693\def\dodobibtexcitation[#1][#2]%
694  {\dontleavehmode
695   \begingroup
696   \doifelsenothing{#2}\secondargumentfalse\secondargumenttrue
697   \ifsecondargument
698     \dowhateverbibtexcitation{#1}{#2}%
699   \else
700     \donumberedbibtexcitation{#1}%
701   \fi
702   \endgroup}
703
704\def\dowhatevercitation#1#2%
705  {\processcommalist[#2]\dobibtexcitationindeed
706   \setupinteraction[\c!style=]% use flag instead
707   \doifelseassignment{#1}
708     {\getparameters[\??pb\??pb][\c!alternative=,\c!extras=,#1]%
709      \edef\currentbibtexvariant{\@@pb@@pbalternative}%
710      \ifempty\currentbibtexvariant
711        \edef\currentbibtexvariant{\bibtexpublicationparameter\c!refcommand}%
712      \fi
713      \ifempty\@@pb@@pbextras
714        \setupcite[\currentbibtexvariant][#1]%
715      \else
716        \edef\@@pb@@pbextras{{\@@pb@@pbextras\ifdefined\@@pb@@pbright\@@pb@@pbright\else\bibtexpublicationparameter\c!right\fi}}%
717        \expanded{\setupcite[\currentbibtexvariant][#1,\c!right=\@@pb@@pbextras]}%
718      \fi}%
719     {\def\currentbibtexvariant{#1}}%
720   \getvalue{bibtex\currentbibtexvariant ref}[#2]}
721
722\def\donumberedbibtexcitation#1%
723  {\processcommalist[#1]\dobibtexcitationindeed
724   \setupinteraction[\c!style=]%
725   \edef\currentbibtexvariant{\bibtexcitationparameter\c!refcommand}%
726   \getvalue{bibtex\currentbibtexvariant ref}[#1]}
727
728\def\dobibtexcitationindeed#1%
729  {\iftrialtypesetting \else
730     \expanded{\writedatatolist[\currentbibtexsession][bibref=#1]}%
731   \fi}
732
733\def\nobibtexcitation[#1]%
734  {\processcommalist[#1]\dobibtexcitationindeed}
735
736\def\bibtexnumref[#1]%
737  {\dontleavehmode
738   \begingroup
739   \bibtexcitationparameter\v!left
740   \penalty\plustenthousand
741   \ctxlua{bibtex.hacks.resolve("","\number\bibtexblock","#1")}%
742   \bibtexcitationparameter\v!right
743   \endgroup}
744
745\def\dowithbibtexnumrefconnector#1#2%
746  {\ifnum#1>\plusone
747     \ifnum#2>\plusone
748       \ifnum#2=#1\relax
749         \bibtexpublicationsparameter\c!lastpubsep
750       \else
751         \bibtexpublicationsparameter\c!pubsep
752       \fi
753     \fi
754   \fi}
755
756\def\dowithbibtexnumref#1#2#3#4#5% n, i, prefix block ref
757  {\dowithbibtexnumrefconnector{#1}{#2}%
758   \def\bibtexrefprefix{#4:}%
759   \inbiblink[#5]}
760
761\def\dowithbibtexnumrefrange#1#2#3#4#5#6#7% n, i, prefix block ref
762  {\dowithbibtexnumrefconnector{#1}{#2}%
763   \def\bibtexrefprefix{#4:}%
764   \inbiblink[#5]%
765   \endash
766   \def\bibtexrefprefix{#6:}%
767   \inbiblink[#7]}
768
769\def\nobibtexnumref#1%
770  {[#1]}
771
772% hm
773
774%     \def\@@pbinumbercommand{\executeifdefined{\??pb:\c!numbercommand:\@@pbnumbering}\firstofoneargument}
775
776%     \letvalue{\??pb:\c!numbercommand:\v!yes  }\firstofoneargument
777%     \letvalue{\??pb:\c!numbercommand:\v!no   }\gobbleoneargument
778%     \setvalue{\??pb:\c!numbercommand:\v!short}{\bibtexgetshort\currentpublicationtag\gobbleoneargument}
779%     \setvalue{\??pb:\c!numbercommand:\v!bib  }{\bibtexgetnumber\currentpublicationtag\gobbleoneargument}
780
781%     \def\bibalternative#1{\csname\??pv\@@currentalternative#1\endcsname}
782
783% basic setup
784
785% parent -> publicationlist
786%
787% \setuplist
788%   [\currentbibtexsession]
789%   [\c!samplesize={AA99},
790%    \c!alternative=a,
791%    \c!interaction=,
792%    \c!pagenumber=\v!no,
793%    #1,
794%    \c!command=]
795
796% \setuppublicationlist
797%   [\c!title=,
798%    \c!command=\dospecialbibinsert,
799%    \c!maybeyear=\v!on]
800
801\setupbibtexpublications
802  [\c!monthconversion=,
803   \c!alternative=apa,
804   \c!method=\v!global,
805   \c!refcommand=num,
806   \c!numbercommand=\bibtexleftnumber]
807
808\setupbibtexcitations % command ?
809  [\c!refcommand=num]
810
811% helpers
812
813\protected\def\doifelsebibtexinteraction
814  {\iflocation
815     \edef\temp{\bibtexcitationparameter\c!interaction}%
816     \ifx\temp\v!stop
817       \doubleexpandafter\secondoftwoarguments
818     \else
819       \doubleexpandafter\firstoftwoarguments
820     \fi
821   \else
822     \expandafter\secondoftwoarguments
823   \fi}
824
825\let\doifbibtexinteractionelse\doifelsebibtexinteraction
826
827% variants
828
829% todo: lastsep here
830
831\newconditional\firstbibtexrefsep
832
833\def\bibtexresetrefsep
834  {\settrue\firstbibtexrefsep}
835
836\def\bibtexinsertrefsep
837  {\ifconditional\firstbibtexrefsep
838     \setfalse\firstbibtexrefsep
839   \else
840     \bibtexcitationparameter\c!pubsep
841   \fi}
842
843\def\inbibtexlink#1#2%
844  {\doifelsereferencefound{\bibtexrefprefix#1}
845     {\goto{#2}[\bibtexrefprefix#1]}
846     {!#1!}}
847
848\def\dobibtexgotolink#1#2%
849  {\doifelsereferencefound{\bibtexrefprefix#1}
850     {\goto{#2}[\bibtexrefprefix#1]}
851     {!#1!}}
852
853\def\dobibattexlink#1#2%
854  {\doifelsereferencefound{\bibtexrefprefix#1}
855    {\at{#2}[\bibtexrefprefix#1]}
856    {!#1!}}
857
858\def\dobibtexurllink#1#2%
859  {\expanded{\useURL[bibtex:url:#1][#2]}%
860   \doifelsebibtexinteraction
861     {\goto{\url[bibtex:url:#1]}[url(bibtex:url:#1)]}
862     {\url[bibtex:url:#1]}}
863
864% todo: style, color
865
866\protected\def\bibtexdataref       {\dodoubleargument\dobibtexdataref}
867\protected\def\bibtextyperef       {\dodoubleargument\dobibtextyperef}
868\protected\def\bibtexkeyref        {\dodoubleargument\dobibtexkeyref}
869\protected\def\bibtexserialref     {\dodoubleargument\dobibtexserialref}
870\protected\def\bibtexurlref        {\dodoubleargument\dobibtexurlref}
871\protected\def\bibtexdoiref        {\dodoubleargument\dobibtexdoiref}
872\protected\def\bibtexpageref       {\dodoubleargument\dobibtexpageref}
873\protected\def\bibtexnoneref       {\dodoubleargument\dobibtexnoneref}
874\protected\def\bibtexshortref      {\dodoubleargument\dobibtexshortref}
875\protected\def\bibtexyearref       {\dodoubleargument\dobibtexyearref}
876\protected\def\bibtexauthorref     {\dodoubleargument\dobibtexauthorref}
877\protected\def\bibtexauthoryearref {\dodoubleargument\dobibtexauthoryearref}
878\protected\def\bibtexauthoryearsref{\dodoubleargument\dobibtexauthoryearsref}
879
880\def\dobibtexdataref       {\doprocessbibtexref\dodobibtexdataref       {ref}}    % [#1][#2]
881\def\dobibtextyperef       {\doprocessbibtexref\dodobibtextyperef       {type}}   % [#1][#2]
882\def\dobibtexkeyref        {\doprocessbibtexref\dodobibtexkeyref        {key}}    % [#1][#2]
883\def\dobibtexserialref     {\doprocessbibtexref\dodobibtexserialref     {serial}} % [#1][#2]
884\def\dobibtexurlref        {\doprocessbibtexref\dodobibtexurlref        {url}}    % [#1][#2]
885\def\dobibtexdoiref        {\doprocessbibtexref\dodobibtexdoiref        {doi}}    % [#1][#2]
886\def\dobibtexpageref       {\doprocessbibtexref\dodobibtexpageref       {page}}   % [#1][#2]
887\def\dobibtexnoneref       {\doprocessbibtexref\dodobibtexnoneref       {none}}   % [#1][#2]
888\def\dobibtexshortref      {\doprocessbibtexref\dodobibtexshortref      {short}}  % [#1][#2]
889\def\dobibtexyearref       {\doprocessbibtexref\dodobibtexyearref       {year}}   % [#1][#2]
890\def\dobibtexauthorref     {\doprocessbibtexref\dodobibtexauthorref     {author}} % [#1][#2]
891\def\dobibtexauthoryearref {\doprocessbibtexref\dodobibtexauthoryearref {authoryear}} % [#1][#2]
892\def\dobibtexauthoryearsref{\doprocessbibtexref\dodobibtexauthoryearsref{authoryears}} % [#1][#2]
893
894\def\doprocessbibtexref#1#2[#3][#4]%
895  {\edef\currentbibtexsession{#3}%
896   \edef\currentbibtexvariant{#2}%
897   \def\dodoprocessbibtexref##1%
898     {% test for existence
899      \edef\currentbibtextag{##1}%
900      \bibtexinsertrefsep
901      #1{##1}}%
902   \bibtexresetrefsep
903   \bibtexcitationparameter\v!left
904   \processcommalist[#4]\dodoprocessbibtexref\relax
905   \bibtexcitationparameter\v!right}
906
907\def\dodobibtexdataref#1%
908  {\dotypesetabibtexpublication{#1}}
909
910\def\dodobibtextyperef#1%
911  {\edef\bibtexrefcontent{\xmlfilter{bibtex:\currentbibtexsession}{/bibtex/entry[@tag='#1']/attribute('category')}}%
912   \bibtexrefcontent}
913
914\def\dodobibtexkeyref#1%
915  {\edef\bibtexrefcontent{\xmlfilter{bibtex:\currentbibtexsession}{/bibtex/entry[@tag='key']/context()}}%
916   \dobibtexgotolink{#1}{\bibtexrefcontent}}
917
918\def\dodobibtexserialref#1%
919  {\edef\bibtexrefcontent{\xmlfilter{bibtex:\currentbibtexsession}{/bibtex/entry[@tag='#1']/match()}}%
920   \dobibtexgotolink{#1}{\bibtexrefcontent}}
921
922\def\dodobibtexurlref#1%
923  {\edef\bibtexrefcontent{\xmlfilter{bibtex:\currentbibtexsession}{/bibtex/entry[@tag='#1']/field[@name='url']/context()}}%
924   \dobibtexurllink{#1}{\bibtexrefcontent}}
925
926\def\dodobibtexdoiref#1%
927  {\edef\bibtexrefcontent{\xmlfilter{bibtex:\currentbibtexsession}{/bibtex/entry[@tag='#1']/field[@name='doi']/context()}}%
928   \dobibtexurllink{#1}{http://dx.doi.org/\bibtexrefcontent}}
929
930\def\dodobibtexpageref#1%
931  {\dobibtexatlink{#1}{}} % second argument can become 'page'
932
933\def\dodobibtexnoneref#1%
934  {}
935
936\def\dodobibtexshortref#1%
937  {\edef\bibtexrefcontent{\xmlfilter{bibtex:\currentbibtexsession}{/bibtex/entry[@tag='#1']/bibtexshort()}}%
938   \dobibtexgotolink{#1}{\bibtexrefcontent}}
939
940\def\dodobibtexyearref#1%
941  {\edef\bibtexrefcontent{\xmlfilter{bibtex:\currentbibtexsession}{/bibtex/entry[@tag='#1']/field[@name='year']/context()}}%
942   \bibtexrefcontent}
943
944% \def\bibmaybeinteractive#1#2%
945%   {\doifelsevalue{@@pv\@@currentalternative\c!compress}
946%      {\ifbibinteractionelse{\gotobiblink{#2}[#1]}{#2}}
947%      {#2}}
948
949% \def\bibauthornumref[#1]%
950%   {\getcommalistsize[#1]%
951%    \global\bibitemcounter\commalistsize
952%    \bibresetrefsep
953%    \processcommalist[#1]\dobibauthornumref }
954%
955% \def\dobibauthornumref#1%
956%  {\bibinsertrefsep
957%   \doifbibreferencefoundelse{#1}
958%     {\begingroup
959%      \bibgetvara{#1}%
960%      \bibalternative\c!inbetween
961%      \setuppublications[\c!refcommand=num]%
962%      \cite[#1]%
963%      \endgroup}
964%     {}}
965
966% compress years
967% andtext namesep
968% otherstext authoretallimit
969
970% we will use context.* instead at the lua end because it saves us passing settings
971
972% \def\thebibtexpubsep           {\bibtexpublicationsparameter\c!pubsep}
973% \def\thebibtexlastpubsep       {\bibtexpublicationsparameter\c!lastpubsep}
974% \def\thebibtexfinalpubseparator{\bibtexpublicationsparameter\c!lastpubsep}
975
976\def\dodobibtexauthorref     #1{\ctxlua{bibtex.authorref     ("bibtex:\currentbibtexsession","#1","normal","author")}}
977\def\dodobibtexauthoryearref #1{\ctxlua{bibtex.authoryearref ("bibtex:\currentbibtexsession","#1","normal","author")}}
978\def\dodobibtexauthoryearsref#1{\ctxlua{bibtex.authoryearsref("bibtex:\currentbibtexsession","#1","normal","author")}}
979
980\protected\def\bibtexsingularplural#1#2{\ctxlua{bibtex.singularorplural(\!!bs#1\!!es,\!!bs#2\!!es)}}
981
982\protect \endinput
983
984