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