bibl-bib.mkxl /size: 32 Kb    last modification: 2023-12-21 09:44
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\newconditional\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\normalexpanded{\bibgotoDOI{\@@pb@thekey}{\@@pb@doi}}#2}{#3}
282    *doi*
283\stopxmlsetups
284
285\startxmlsetups bibtex:format:doi
286%   \bibdoifelse{\@@pb@biburl}{#1\normalexpanded{\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 \newinteger\bibtexblock   \fi \bibtexblock\plusone
348\ifdefined\bibtexcounter \else \newinteger\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       \lettonothing\@@pbalternative
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\advanceby\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\newdimension\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     {}%
637   \ctxlua{bibtex.hacks.flush("\bibtexpublicationsparameter\c!sorttype")}%
638   \stoppacked
639   \endgroup}
640
641\protected\def\typesetbibtexpublication#1%
642  {\edef\currentbibtexsessiontag{#1}%
643   \ifempty\currentbibtexsessiontag
644     % can't really happen
645   \orelse\ifx\currentbibtexcriterium\v!all
646     \dotypesetbibtexpublication % was \doplacepublicationindeed
647   \else
648     \ctxlua{bibtex.hacks.doifalreadyplaced("\currentbibtexsessiontag")}
649       \donothing
650       \dotypesetbibtexpublication
651   \fi}
652
653\def\dotypesetbibtexpublication
654  {\doifelsebibreferencefound\currentbibtexsessiontag
655     {\global\advanceby\bibtexcounter\plusone
656      \ctxlua{bibtex.hacks.registerplaced("\currentbibtexsessiontag")}%
657      \let\currentlist\currentbibtexsession
658      \let\currentlistentrynumber\bibtexcounter
659      \let\currentlistentrytitle\thebibtexpublicationlistelement
660      \lettonothing\currentlistentrypagenumber
661      \strc_lists_apply_renderingsetup}
662     {}} % invalid
663
664\def\thebibtexpublicationlistelement
665   {\strut
666    \normalexpanded{\reference[\bibtexrefprefix\currentbibtexsessiontag]{\number\bibtexcounter}}%
667    \dotypesetabibtexpublication\currentbibtexsessiontag
668    \strut}
669
670\def\dotypesetabibtexpublication#1%
671  {\begingroup
672   \ignorespaces
673   \xmlfilter{bibtex:\currentbibtexsession}{/bibtex/entry[@tag='#1']/command(bibtex:format)}%
674   \removeunwantedspaces
675%  \ignorespaces
676%  \bibalternative{\bibgetvart{#1}}%
677%  \removeunwantedspaces
678   \endgroup}
679
680\def\doprocessbibtexentry#1{\typesetbibtexpublication{#1}}
681
682% citations
683
684\protected\def\bibtexcitation[#1]%
685  {\edef\currentbibtexsession{#1}%
686   \strictdoifelsenextoptional\dobibtexcitation\dobibtexref}
687
688\def\dobibtexref#1%
689  {\dodobibtexcitation[#1][]}
690
691\def\dobibtexcitation[#1]%
692  {\strictdoifelsenextoptional{\dodobibtexcitation[#1]}{\dodobibtexcitation[#1][]}}
693
694\def\dodobibtexcitation[#1][#2]%
695  {\dontleavehmode
696   \begingroup
697   \doifelsenothing{#2}\secondargumentfalse\secondargumenttrue
698   \ifsecondargument
699     \dowhateverbibtexcitation{#1}{#2}%
700   \else
701     \donumberedbibtexcitation{#1}%
702   \fi
703   \endgroup}
704
705\def\dowhatevercitation#1#2%
706  {\processcommalist[#2]\dobibtexcitationindeed
707   \setupinteraction[\c!style=]% use flag instead
708   \doifelseassignment{#1}
709     {\getparameters[\??pb\??pb][\c!alternative=,\c!extras=,#1]%
710      \edef\currentbibtexvariant{\@@pb@@pbalternative}%
711      \ifempty\currentbibtexvariant
712        \edef\currentbibtexvariant{\bibtexpublicationparameter\c!refcommand}%
713      \fi
714      \ifempty\@@pb@@pbextras
715        \setupcite[\currentbibtexvariant][#1]%
716      \else
717        \edef\@@pb@@pbextras{{\@@pb@@pbextras\ifdefined\@@pb@@pbright\@@pb@@pbright\else\bibtexpublicationparameter\c!right\fi}}%
718        \normalexpanded{\setupcite[\currentbibtexvariant][#1,\c!right=\@@pb@@pbextras]}%
719      \fi}%
720     {\def\currentbibtexvariant{#1}}%
721   \getvalue{bibtex\currentbibtexvariant ref}[#2]}
722
723\def\donumberedbibtexcitation#1%
724  {\processcommalist[#1]\dobibtexcitationindeed
725   \setupinteraction[\c!style=]%
726   \edef\currentbibtexvariant{\bibtexcitationparameter\c!refcommand}%
727   \getvalue{bibtex\currentbibtexvariant ref}[#1]}
728
729\def\dobibtexcitationindeed#1%
730  {\iftrialtypesetting \else
731     \normalexpanded{\writedatatolist[\currentbibtexsession][bibref=#1]}%
732   \fi}
733
734\def\nobibtexcitation[#1]%
735  {\processcommalist[#1]\dobibtexcitationindeed}
736
737\def\bibtexnumref[#1]%
738  {\dontleavehmode
739   \begingroup
740   \bibtexcitationparameter\v!left
741   \penalty\plustenthousand
742   \ctxlua{bibtex.hacks.resolve("","\number\bibtexblock","#1")}%
743   \bibtexcitationparameter\v!right
744   \endgroup}
745
746\def\dowithbibtexnumrefconnector#1#2%
747  {\ifnum#1>\plusone
748     \ifnum#2>\plusone
749       \ifnum#2=#1\relax
750         \bibtexpublicationsparameter\c!lastpubsep
751       \else
752         \bibtexpublicationsparameter\c!pubsep
753       \fi
754     \fi
755   \fi}
756
757\def\dowithbibtexnumref#1#2#3#4#5% n, i, prefix block ref
758  {\dowithbibtexnumrefconnector{#1}{#2}%
759   \def\bibtexrefprefix{#4:}%
760   \inbiblink[#5]}
761
762\def\dowithbibtexnumrefrange#1#2#3#4#5#6#7% n, i, prefix block ref
763  {\dowithbibtexnumrefconnector{#1}{#2}%
764   \def\bibtexrefprefix{#4:}%
765   \inbiblink[#5]%
766   \endash
767   \def\bibtexrefprefix{#6:}%
768   \inbiblink[#7]}
769
770\def\nobibtexnumref#1%
771  {[#1]}
772
773% hm
774
775%     \def\@@pbinumbercommand{\executeifdefined{\??pb:\c!numbercommand:\@@pbnumbering}\firstofoneargument}
776
777%     \letvalue{\??pb:\c!numbercommand:\v!yes  }\firstofoneargument
778%     \letvalue{\??pb:\c!numbercommand:\v!no   }\gobbleoneargument
779%     \setvalue{\??pb:\c!numbercommand:\v!short}{\bibtexgetshort\currentpublicationtag\gobbleoneargument}
780%     \setvalue{\??pb:\c!numbercommand:\v!bib  }{\bibtexgetnumber\currentpublicationtag\gobbleoneargument}
781
782%     \def\bibalternative#1{\csname\??pv\@@currentalternative#1\endcsname}
783
784% basic setup
785
786% parent -> publicationlist
787%
788% \setuplist
789%   [\currentbibtexsession]
790%   [\c!samplesize={AA99},
791%    \c!alternative=a,
792%    \c!interaction=,
793%    \c!pagenumber=\v!no,
794%    #1,
795%    \c!command=]
796
797% \setuppublicationlist
798%   [\c!title=,
799%    \c!command=\dospecialbibinsert,
800%    \c!maybeyear=\v!on]
801
802\setupbibtexpublications
803  [\c!monthconversion=,
804   \c!alternative=apa,
805   \c!method=\v!global,
806   \c!refcommand=num,
807   \c!numbercommand=\bibtexleftnumber]
808
809\setupbibtexcitations % command ?
810  [\c!refcommand=num]
811
812% helpers
813
814\protected\def\doifelsebibtexinteraction
815  {\iflocation
816     \edef\temp{\bibtexcitationparameter\c!interaction}%
817     \ifx\temp\v!stop
818       \doubleexpandafter\secondoftwoarguments
819     \else
820       \doubleexpandafter\firstoftwoarguments
821     \fi
822   \else
823     \expandafter\secondoftwoarguments
824   \fi}
825
826\let\doifbibtexinteractionelse\doifelsebibtexinteraction
827
828% variants
829
830% todo: lastsep here
831
832\newconditional\firstbibtexrefsep
833
834\def\bibtexresetrefsep
835  {\firstbibtexrefsep\conditionaltrue}
836
837\def\bibtexinsertrefsep
838  {\ifconditional\firstbibtexrefsep
839     \firstbibtexrefsep\conditionalfalse
840   \else
841     \bibtexcitationparameter\c!pubsep
842   \fi}
843
844\def\inbibtexlink#1#2%
845  {\doifelsereferencefound{\bibtexrefprefix#1}
846     {\goto{#2}[\bibtexrefprefix#1]}
847     {!#1!}}
848
849\def\dobibtexgotolink#1#2%
850  {\doifelsereferencefound{\bibtexrefprefix#1}
851     {\goto{#2}[\bibtexrefprefix#1]}
852     {!#1!}}
853
854\def\dobibattexlink#1#2%
855  {\doifelsereferencefound{\bibtexrefprefix#1}
856    {\at{#2}[\bibtexrefprefix#1]}
857    {!#1!}}
858
859\def\dobibtexurllink#1#2%
860  {\normalexpanded{\useURL[bibtex:url:#1][#2]}%
861   \doifelsebibtexinteraction
862     {\goto{\url[bibtex:url:#1]}[url(bibtex:url:#1)]}
863     {\url[bibtex:url:#1]}}
864
865% todo: style, color
866
867\protected\def\bibtexdataref       {\dodoubleargument\dobibtexdataref}
868\protected\def\bibtextyperef       {\dodoubleargument\dobibtextyperef}
869\protected\def\bibtexkeyref        {\dodoubleargument\dobibtexkeyref}
870\protected\def\bibtexserialref     {\dodoubleargument\dobibtexserialref}
871\protected\def\bibtexurlref        {\dodoubleargument\dobibtexurlref}
872\protected\def\bibtexdoiref        {\dodoubleargument\dobibtexdoiref}
873\protected\def\bibtexpageref       {\dodoubleargument\dobibtexpageref}
874\protected\def\bibtexnoneref       {\dodoubleargument\dobibtexnoneref}
875\protected\def\bibtexshortref      {\dodoubleargument\dobibtexshortref}
876\protected\def\bibtexyearref       {\dodoubleargument\dobibtexyearref}
877\protected\def\bibtexauthorref     {\dodoubleargument\dobibtexauthorref}
878\protected\def\bibtexauthoryearref {\dodoubleargument\dobibtexauthoryearref}
879\protected\def\bibtexauthoryearsref{\dodoubleargument\dobibtexauthoryearsref}
880
881\def\dobibtexdataref       {\doprocessbibtexref\dodobibtexdataref       {ref}}    % [#1][#2]
882\def\dobibtextyperef       {\doprocessbibtexref\dodobibtextyperef       {type}}   % [#1][#2]
883\def\dobibtexkeyref        {\doprocessbibtexref\dodobibtexkeyref        {key}}    % [#1][#2]
884\def\dobibtexserialref     {\doprocessbibtexref\dodobibtexserialref     {serial}} % [#1][#2]
885\def\dobibtexurlref        {\doprocessbibtexref\dodobibtexurlref        {url}}    % [#1][#2]
886\def\dobibtexdoiref        {\doprocessbibtexref\dodobibtexdoiref        {doi}}    % [#1][#2]
887\def\dobibtexpageref       {\doprocessbibtexref\dodobibtexpageref       {page}}   % [#1][#2]
888\def\dobibtexnoneref       {\doprocessbibtexref\dodobibtexnoneref       {none}}   % [#1][#2]
889\def\dobibtexshortref      {\doprocessbibtexref\dodobibtexshortref      {short}}  % [#1][#2]
890\def\dobibtexyearref       {\doprocessbibtexref\dodobibtexyearref       {year}}   % [#1][#2]
891\def\dobibtexauthorref     {\doprocessbibtexref\dodobibtexauthorref     {author}} % [#1][#2]
892\def\dobibtexauthoryearref {\doprocessbibtexref\dodobibtexauthoryearref {authoryear}} % [#1][#2]
893\def\dobibtexauthoryearsref{\doprocessbibtexref\dodobibtexauthoryearsref{authoryears}} % [#1][#2]
894
895\def\doprocessbibtexref#1#2[#3][#4]%
896  {\edef\currentbibtexsession{#3}%
897   \edef\currentbibtexvariant{#2}%
898   \def\dodoprocessbibtexref##1%
899     {% test for existence
900      \edef\currentbibtextag{##1}%
901      \bibtexinsertrefsep
902      #1{##1}}%
903   \bibtexresetrefsep
904   \bibtexcitationparameter\v!left
905   \processcommalist[#4]\dodoprocessbibtexref\relax
906   \bibtexcitationparameter\v!right}
907
908\def\dodobibtexdataref#1%
909  {\dotypesetabibtexpublication{#1}}
910
911\def\dodobibtextyperef#1%
912  {\edef\bibtexrefcontent{\xmlfilter{bibtex:\currentbibtexsession}{/bibtex/entry[@tag='#1']/attribute('category')}}%
913   \bibtexrefcontent}
914
915\def\dodobibtexkeyref#1%
916  {\edef\bibtexrefcontent{\xmlfilter{bibtex:\currentbibtexsession}{/bibtex/entry[@tag='key']/context()}}%
917   \dobibtexgotolink{#1}{\bibtexrefcontent}}
918
919\def\dodobibtexserialref#1%
920  {\edef\bibtexrefcontent{\xmlfilter{bibtex:\currentbibtexsession}{/bibtex/entry[@tag='#1']/match()}}%
921   \dobibtexgotolink{#1}{\bibtexrefcontent}}
922
923\def\dodobibtexurlref#1%
924  {\edef\bibtexrefcontent{\xmlfilter{bibtex:\currentbibtexsession}{/bibtex/entry[@tag='#1']/field[@name='url']/context()}}%
925   \dobibtexurllink{#1}{\bibtexrefcontent}}
926
927\def\dodobibtexdoiref#1%
928  {\edef\bibtexrefcontent{\xmlfilter{bibtex:\currentbibtexsession}{/bibtex/entry[@tag='#1']/field[@name='doi']/context()}}%
929   \dobibtexurllink{#1}{http://dx.doi.org/\bibtexrefcontent}}
930
931\def\dodobibtexpageref#1%
932  {\dobibtexatlink{#1}{}} % second argument can become 'page'
933
934\def\dodobibtexnoneref#1%
935  {}
936
937\def\dodobibtexshortref#1%
938  {\edef\bibtexrefcontent{\xmlfilter{bibtex:\currentbibtexsession}{/bibtex/entry[@tag='#1']/bibtexshort()}}%
939   \dobibtexgotolink{#1}{\bibtexrefcontent}}
940
941\def\dodobibtexyearref#1%
942  {\edef\bibtexrefcontent{\xmlfilter{bibtex:\currentbibtexsession}{/bibtex/entry[@tag='#1']/field[@name='year']/context()}}%
943   \bibtexrefcontent}
944
945% \def\bibmaybeinteractive#1#2%
946%   {\doifelsevalue{@@pv\@@currentalternative\c!compress}
947%      {\ifbibinteractionelse{\gotobiblink{#2}[#1]}{#2}}
948%      {#2}}
949
950% \def\bibauthornumref[#1]%
951%   {\getcommalistsize[#1]%
952%    \global\bibitemcounter\commalistsize
953%    \bibresetrefsep
954%    \processcommalist[#1]\dobibauthornumref }
955%
956% \def\dobibauthornumref#1%
957%  {\bibinsertrefsep
958%   \doifbibreferencefoundelse{#1}
959%     {\begingroup
960%      \bibgetvara{#1}%
961%      \bibalternative\c!inbetween
962%      \setuppublications[\c!refcommand=num]%
963%      \cite[#1]%
964%      \endgroup}
965%     {}}
966
967% compress years
968% andtext namesep
969% otherstext authoretallimit
970
971% we will use context.* instead at the lua end because it saves us passing settings
972
973% \def\thebibtexpubsep           {\bibtexpublicationsparameter\c!pubsep}
974% \def\thebibtexlastpubsep       {\bibtexpublicationsparameter\c!lastpubsep}
975% \def\thebibtexfinalpubseparator{\bibtexpublicationsparameter\c!lastpubsep}
976
977\def\dodobibtexauthorref     #1{\ctxlua{bibtex.authorref     ("bibtex:\currentbibtexsession","#1","normal","author")}}
978\def\dodobibtexauthoryearref #1{\ctxlua{bibtex.authoryearref ("bibtex:\currentbibtexsession","#1","normal","author")}}
979\def\dodobibtexauthoryearsref#1{\ctxlua{bibtex.authoryearsref("bibtex:\currentbibtexsession","#1","normal","author")}}
980
981\protected\def\bibtexsingularplural#1#2{\ctxlua{bibtex.singularorplural(\!!bs#1\!!es,\!!bs#2\!!es)}}
982
983\protect \endinput
984
985