t-bib.tex / last modification: 2008-10-27 10:28
%D \module
%D   [       file=t-bib,
%D        version=2008.10.27,
%D          title=\CONTEXT\ Publication Module,
%D       subtitle=Publications,
%D         author=Taco Hoekwater,
%D           date=\currentdate,
%D      copyright=Public Domain]
%C
%C Donated to the public domain.


%D The original was developed independantly by Taco Hoekwater while still working for Kluwer
%D Academic publishers (it still used the dutch interface then). Development continued after
%D he left Kluwer, and in Januari 2005, the then already internationalized file was merged
%D with the core distribution by Hans Hagen.  The current version is once again by Taco.
%D
%D More documentation and additional resources can be found on the contextgarden:
%D \hyphenatedurl{http://wiki.contextgarden.net//Bibliography}.

%D \subject{DONE (dd/mm/yyyy)}
%D
%D \startitemize
%D \item add author definition (and associated system variable) (26/05/2005)
%D \item add finalnamesep support for Oxford comma (17/09/2005)
%D \item add \type{\insert...} for: doi, eprint, howpublished (19/09/2005)
%D \item allow a defaulted \type{\setupcite} (19/11/2005)
%D \item renamed citation type 'number' to 'serial' (19/11/2005)
%D \item better definition of \type{\inverted...author} (19/11/2005)
%D \item don't reset [numbercommand] in \type {\setuppublication} by default (20/11/2005)
%D \item don't disable other \type {\setuppublication} keys if alternative is present (20/11/2005)
%D \item drop \type{\sanitizeaccents} (20/11/2005)
%D \item added \type{\nocite} and \type{\cite[none]} (21/11/2005)
%D \item added headtext for it  (23/11/2005)
%D \item make \type{\cite[url]} and \type{\cite[doi]} interactive (23/11/2005)
%D \item make right-aligned labels in the list work even when autohang=no
%D \item use 'et al.' instead of 'et.al.'. Pointed out by Peter Münster (30/12/2005)
%D \item added headtext for cz (31/12/2005)
%D \item Keep whitespace after \type{\cite} with single argument (31/12/2005)
%D \item Fix broken \type{\cite{}} support (31/12/2005)
%D \item Use \type{\readfile} inside \type{\usepublications} instead of \type{\readsysfile} (12/01/2006)
%D \item Use \type{\currentbibyear} and \type{\currentbibauthor} instead of \type{\YR} and \type{\AU} (05/02/2006)
%D \item Fix compressed version of authoryear style (05/02/2006)
%D \item Rename the clashing data fields \type{\url} and \type{\type} to \type{\biburl} and \type{\bibtype} (05/02/2006)
%D \item Added two french bibl files from Renaud Aubin (06/02/2006)
%D \item Five new bib class and eight extra bib fields, for IEEEtran (07/02/2006)
%D \item French keyword translation, provided by Renaud (08/02/2006)
%D \item fix underscores in undefined keys (22/02/2006)
%D \item Destroy interactivity in labels of the publication list (13/03/2006)
%D \item fix multi-cite list compression (11/4/2006)
%D \item fix \type{\getcitedata} (11/4/2006)
%D \item magic for chapter bibs (18-25/4/2006)
%D \item language setting (25/4/2006)
%D \item use \type{\hyphenatedurl} for \type{\inserturl} (25/4/2006)
%D \item Add \type{\docitation} to \type{\nocite}(26/4/2006)
%D \item patents can have numbers, added to bst files (26/4/2006)
%D \item \type{\docitation} needs a \type{\iftrialtypesetting} (27/4/2006)
%D \item \type{\filllocalpublist}'s loop is bound by definedness, not resolvedness (27/4/2006)
%D \item \type{\setuppublications[monthconversion=]} added (15/5/2006)
%D \item use \type{\undefinedreference} instead of bare question marks (15/5/2006)
%D \item add grouping around \type{\placepublications} commands (16/5/2006)
%D \item fix a bug in \type{\cite{<item>}} (17/5/2006)
%D \item support \type{\cite[authornum]} (18/5/2006)
%D \item make \type{\cite} unexpandable (20/6/2006)
%D \item allow hyperlinks in author\&year combo's
%D       (cite list compression has to be off) (20/6/2006)
%D \item fix duplicate labels for per-chapter style (20/6/2006)
%D \item allow \type{\setupcite[interaction=(start|stop)]}
%D \item fix the item number in the publication list with 'numbering=yes' (22/6/2006)
%D \item make the default criterium for \type{\placepublications} be \type{previous} (23/6/2006)
%D \item fix \type{\normalauthor} and \type{\normalshortauthor} spacing (29/6/2006)
%D \item do not typeset empty arguments to \type{\typesetapublication} (29/6/2006)
%D \item add \type{symbol=none} to \type{\setuplist} in unnumbered
%D       mode to prevent typesetting of bare numbers (29/6/2006)
%D \item remove two incorrect spaces from bibl-num.tex (1/7/2006)
%D \item reset font styles within \type{\cite}, so that font switches
%D       in \type{left} stay in effect (12/7/2006)
%D \item guard added against loading bbl files multiple times (13/7/2006)
%D \item fix \type{\cite[num]} with compression is on. (14/7/2006)
%D \item test \type{\iflocation} before deciding to use the
%D       interactive version of cite (18/7/2006)
%D \item support \type{\setupcite[authoretallimit=1]} (18/7/2006)
%D \item support use of \type{\cite} within titles and captions by
%D       saveguarding the list item extraction and reference placement
%D       code (19/7/2006)
%D \item support \type{\setuppublicationlist[title=\chapter]} (4/8/2006)
%D \item use the expansion of \type{\headtext{pubs}} (4/8/2006)
%D \item hook added for repeated authors in publication list
%D       \type{\setuppublicationlist[artauthorcommand=\mythreeargscommand]}
%D       (4/8/2006)
%D \item make the bracketed arguments of \type{\artauthor}, \type{\author}
%D        and \type{\editor} (bbl commands) optional (4/8/2006)
%D \item the constants \type{sorttype}, \type{compress} and
%D        \type{autohang} have moved to the core (8/8/2006)
%D \item bibtex is now registered as a program to be run by texexec (8/8/2006)
%D \item fix a bug in \type{\setupcite[authoretallimit=1]} (9/8/2006)
%D \item fix a bug inside citations that prevented lastpubsep from ever being
%D       used due to a volatile \type{\commalistsize} (25/8/2006).
%D \item added the possibility of \type{\placepublications[option=continue]}
%D       (6/9/2006)
%D \item Mojca translated Master's Thesis to Masterarbeit (bibl-apa-de.tex)
%D       (12/9/2006)
%D \item Added \type{\setuppublicationlist[maybeyear=off]} by request from
%D       Thomas Schmitz (15/9/2006)
%D \item Removed some spurious spaces pointed out by willi egger (19/9/2006)
%D \item Add configuration of bibtex executable name (4/11/2006)
%D \item Fix numbering=short and numbering=bib (spotted by Matthias Wächter) (4/11/2006)
%D \item third attempt to get a correct release (5/11/2006)
%D \item fix a few missing dots in bibl-num.tex  (7/12/2006)
%D \item Patch for DOI's by Tobias Burnus (17/4/2007)
%D \item Patch for \type{\insertbiburl} and \type{\insertdoi} for  Tobias Burnus (18/4/2007)
%D \item Added a missing \type{\relax} in \type{\dospecialbibinsert},
%D       that made the space before the {\it et al.} text disappear.  (18/4/2007)
%D \item Attempt to fix percent signs in bbl files. As a side-effect,
%D       this prohibits comments in \tex{startpublication} blocks! (17/4/2008)
%D \item Patch from Matthias W\"achter that allows arbitrary .bst
%D        files to be used with \tex{setupbibtex} (25/9/2008)
%D \item Extended for the new multilingual setups for the Oct 2008 current of ConTeXt (23/10/2008)
%D \item Multilingual setups needed another fix (27/10/2008)
%D \item Two fixes for bibl-apa by Michael Green (27/10/2008)
%D \stopitemize
%D
%D \subject{WISHLIST}
%D
%D \startitemize
%D \item link back from publication list to citation
%D \item export \type {\citation{<cited item>}}
%D \item support mlbibtex
%D \item don't load the whole lot, but filter entries instead
%D \stopitemize

\unprotect

%D start with a temp hack the file will still work with pre-Oct 2008
%D versions of ConTeXt:


\def\startinterface #1
  {\doifnot{#1}{all}{\doifnotinset\currentinterface{#1}{\gobbleuntil\stopinterface}}}

\let\checksetvalue\gobbletwoarguments


%\defineinterfacevariable {title}  {title}
%\defineinterfacevariable {short}  {short}
%\defineinterfacevariable {cite}   {cite}
%\defineinterfacevariable {bbl}    {bbl}
%\defineinterfacevariable {bib}    {bib}
%\defineinterfacevariable {author} {author}

%D A few new shortcuts:

\definesystemvariable  {pv}  % PublicationVariable
\definesystemvariable  {pb}  % PuBlication

\definemessageconstant {bib}
\definefileconstant    {bibextension} {bbl}

%D Some user information messages.

\startmessages all library: bib
    title: publications
    1: file -- not found, unknown style ignored
    2: file -- not found, waiting for bibtex
    3: wrote a new auxiliary file \jobname.aux
    4: loading database from --
    5: warning: cite argument -- on \the\inputlineno
    6: loading formatting style from --
\stopmessages

%D Some constants for the multi-lingual interface

\startinterface all
    \setinterfaceconstant {database}             {database}
    \setinterfaceconstant {artauthor}            {artauthor}
    \setinterfaceconstant {editor}               {editor}
    \setinterfaceconstant {authoretallimit}      {authoretallimit}
    \setinterfaceconstant {artauthoretallimit}   {artauthoretallimit}
    \setinterfaceconstant {editoretallimit}      {editoretallimit}
    \setinterfaceconstant {authoretaldisplay}    {authoretaldisplay}
    \setinterfaceconstant {artauthoretaldisplay} {artauthoretaldisplay}
    \setinterfaceconstant {editoretaldisplay}    {editoretaldisplay}
    \setinterfaceconstant {authoretaltext}       {authoretaltext}
    \setinterfaceconstant {artauthoretaltext}    {artauthoretaltext}
    \setinterfaceconstant {editoretaltext}       {editoretaltext}
    \setinterfaceconstant {otherstext}           {otherstext}
    \setinterfaceconstant {andtext}              {andtext}
    \setinterfaceconstant {totalnumber}          {totalnumber}
    \setinterfaceconstant {firstnamesep}         {firstnamesep}
    \setinterfaceconstant {vonsep}               {vonsep}
    \setinterfaceconstant {juniorsep}            {juniorsep}
    \setinterfaceconstant {surnamesep}           {surnamesep}
    \setinterfaceconstant {lastnamesep}          {lastnamesep}
    \setinterfaceconstant {finalnamesep}         {finalnamesep}
    \setinterfaceconstant {namesep}              {namesep}
    \setinterfaceconstant {pubsep}               {pubsep}
    \setinterfaceconstant {lastpubsep}           {lastpubsep}
    \setinterfaceconstant {refcommand}           {refcommand}
    \setinterfaceconstant {samplesize}           {samplesize}
\stopinterface

\startinterface dutch
    \setinterfacevariable {title}  {titel}
    \setinterfacevariable {short}  {kort}
    \setinterfacevariable {cite}   {cite}
    \setinterfacevariable {bbl}    {bbl}
    \setinterfacevariable {bib}    {bib}
    \setinterfacevariable {author} {auteur}
\stopinterface

\startinterface english
    \setinterfacevariable {title}  {title}
    \setinterfacevariable {short}  {short}
    \setinterfacevariable {cite}   {cite}
    \setinterfacevariable {bbl}    {bbl}
    \setinterfacevariable {bib}    {bib}
    \setinterfacevariable {author} {author}
\stopinterface

\startinterface  german
    \setinterfacevariable {title}  {titel}
    \setinterfacevariable {short}  {kurz}
    \setinterfacevariable {cite}   {cite}
    \setinterfacevariable {bbl}    {bbl}
    \setinterfacevariable {bib}    {bib}
    \setinterfacevariable {author} {autor}
\stopinterface

\startinterface czech
    \setinterfacevariable {title}  {titul}
    \setinterfacevariable {short}  {short}
    \setinterfacevariable {cite}   {cite}
    \setinterfacevariable {bbl}    {bbl}
    \setinterfacevariable {bib}    {bib}
    \setinterfacevariable {author} {autor}
\stopinterface

\startinterface italian
    \setinterfacevariable {title}  {titolo}
    \setinterfacevariable {short}  {short}
    \setinterfacevariable {cite}   {cite}
    \setinterfacevariable {bbl}    {bbl}
    \setinterfacevariable {bib}    {bib}
    \setinterfacevariable {author} {autore}
\stopinterface

\startinterface romanian
    \setinterfacevariable {title}  {titlu}
    \setinterfacevariable {short}  {short}
    \setinterfacevariable {cite}   {cite}
    \setinterfacevariable {bbl}    {bbl}
    \setinterfacevariable {bib}    {bib}
    \setinterfacevariable {author} {autor}
\stopinterface

\startinterface french
    \setinterfacevariable {title}  {titre}
    \setinterfacevariable {short}  {short}
    \setinterfacevariable {cite}   {cite}
    \setinterfacevariable {bbl}    {bbl}
    \setinterfacevariable {bib}    {bib}
    \setinterfacevariable {author} {auteur}
\stopinterface

\def\biblistname{pubs} % for compatibility

%D how to load the references. There is some new stuff here
%D to support Idris' (incorrect :-)) use of projects

\let\preloadbiblist\relax

\ifx\currentcomponent\v!text
  % single file
  \edef\temp{\the\everystarttext}%
  \ifx\temp\empty
    % post-starttext
    \def\preloadbiblist{\dousepublications\jobname }%
  \else
    % pre-starttext
    \appendtoks \dousepublications\jobname \to \everystarttext
  \fi
  %
\else \ifx\currentcomponent\v!project
  % a project file, have to set up the partial products!
  \def\startproduct #1 %
    {\doateverystarttext
     \dousepublications{#1}%
     \donextlevel\v!product\currentproduct
     \doexecutefileonce\doexecutefileonce
     \donotexecutefile\doexecutefile#1\\}%
  %
\else \ifx\currentcomponent\v!product
  % a product file
  \def\preloadbiblist{\dousepublications\jobname }%
  %
\else
  % a component? not sure what to do
  \def\preloadbiblist{\dousepublications\jobname }%
  %
\fi \fi \fi

\definelist[pubs]
\setuplist[pubs][\c!width=]


%D The text string for the publication list header

\setupheadtext[en][pubs=References]
\setupheadtext[nl][pubs=Literatuur]
\setupheadtext[de][pubs=Literatur]
\setupheadtext[it][pubs=Bibliografia]
\setupheadtext[sl][pubs=Literatura]
\setupheadtext[fr][pubs=Bibliographie]

%D \macros{bibdoif,bibdoifnot,bibdoifelse}
%D
%D Here are a few small helpers that are used a lot
%D in all the typesetting commands
%D (\type{\insert...}) we will encounter later.

\long\def\bibdoifelse#1%
  {\@EA\def\@EA\!!stringa\@EA{#1}%
   \ifx\!!stringa\empty
     \expandafter\secondoftwoarguments
   \else
     \expandafter\firstoftwoarguments
   \fi}

\long\def\bibdoifnot#1%
  {\@EA\def\@EA\!!stringa\@EA{#1}%
   \ifx\!!stringa\empty
     \expandafter\firstofoneargument
   \else
     \expandafter\gobbleoneargument
   \fi}

\long\def\bibdoif#1%
  {\@EA\def\@EA\!!stringa\@EA{#1}%
   \ifx\!!stringa\empty
     \expandafter\gobbleoneargument
   \else
     \expandafter\firstofoneargument
   \fi}


%D Bibtex settings separated out

%D No point in writing the aux file if there is no database...

\def\setupbibtex{\dosingleempty\dosetupbibtex}

\def\dosetupbibtex[#1]%
  {\let\@@pbdatabase\empty
   \getparameters[\??pb][sort=\s!default,#1]%
   \expanded{\processaction[\@@pbsort]}
        [      \v!no=>\def\bibstyle{cont-no},
           \v!author=>\def\bibstyle{cont-au},
            \v!title=>\def\bibstyle{cont-ti},
            \v!short=>\def\bibstyle{cont-ab},
          \s!default=>\def\bibstyle{cont-no},
          \s!unknown=>\def\bibstyle{\@@pbsort}]%
   \ifx\@@pbdatabase\empty\else \writeauxfile \fi}

\dosetupbibtex[bibtex=bibtex]

%D \macros{writeauxfile}
%D
%D Unfortunately, \BIBTEX\ is not the best configurable program
%D around. The names of the commands it parses as well as the \type{.aux}
%D extension to the file name are both hardwired.
%D
%D This means \CONTEXT\ has to write a \LATEX-style auxiliary file, yuk!
%D The good news is that it can be rather short. We'll just ask
%D \BIBTEX\ to output the entire database(s) into the \type{bbl} file.
%D
%D The \type{\bibstyle} command controls how the \type{bbl} file will
%D be sorted. The possibilities are:
%D
%D \startitemize[packed]
%D \item by author (+year, title): cont-au.bst
%D \item by title  (+author, year): cont-ti.bst
%D \item by short key as in abbrev.bst: cont-ab.bst
%D \item not sorted at all: cont-no.bst
%D \stopitemize

\def\writeauxfile
  {\doifmode{*first}
    {\openout \scratchwrite \jobname.aux
     \write   \scratchwrite {\string\citation{*}}%
     \write   \scratchwrite {\string\bibstyle{\bibstyle}}%
     \write   \scratchwrite {\string\bibdata{\@@pbdatabase}}%
     \closeout\scratchwrite
     \showmessage\m!bib{3}{}%
     \expanded{\installprogram{\@@pbbibtex\space\jobname}}}}

%D \macros{ifsortbycite,iftypesetall,ifautohang,ifbibcitecompress}
%D
%D The module needs some new \type{\if} statements.

%D Default sort order of the reference list is by citation.

\newif\ifsortbycite        \sortbycitetrue

%D By default, only referenced publications are typeset

\newif\iftypesetall        \typesetallfalse

%D Hanging indentation of the publication list
%D will not adjust itself according to the width of the label.

\newif\ifautohang          \autohangfalse

%D Cite lists are compressed, if possible.

\newif\ifbibcitecompress   \bibcitecompresstrue

\def\setuppublications
  {\dosingleargument\dosetuppublications}

\def\bibleftnumber#1%
  {#1\hfill~}


\def\dosetuppublications[#1]%
  {\getparameters
     [\??pb]
     [\c!alternative=,#1]%
   \doifsomething\@@pbalternative
      {\readsysfile
         {bibl-\@@pbalternative.tex}
         {\showmessage\m!bib{6}{bibl-\@@pbalternative}\let\@@pbalternative\empty}
         {\showmessage\m!bib{1}{bibl-\@@pbalternative}\let\@@pbalternative\empty}}%
  \getparameters
     [\??pb]
     [#1]%
    \processaction
     [\@@pbcriterium]
     [    \v!all=>\typesetalltrue,
      \s!unknown=>\typesetallfalse]%
   \processaction
     [\@@pbautohang]
     [    \v!yes=>\autohangtrue,
      \s!unknown=>\autohangfalse]%
   \processaction
     [\@@pbsorttype]
     [   \v!cite=>\sortbycitetrue,
          \v!bbl=>\sortbycitefalse,
      \s!default=>\sortbycitetrue,
      \s!unknown=>\sortbycitefalse]%
   \processaction
     [\@@pbnumbering]
     [    \v!yes=>\let\@@pbinumbercommand\firstofoneargument,
           \v!no=>\let\@@pbinumbercommand\gobbleoneargument,
        \v!short=>\def\@@pbinumbercommand##1{\getvalue{pbds-\@@pbk}},
          \v!bib=>\def\@@pbinumbercommand##1{\getvalue{pbdn-\@@pbk}},
      \s!unknown=>\let\@@pbinumbercommand\firstofoneargument]%
   \processaction
     [\@@pbrefcommand]
     [\s!default=>\edef\@@citedefault{\@@pbrefcommand},
      \s!unknown=>\edef\@@citedefault{\@@pbrefcommand}]}

% initialize

\def\@@pbrefcommand{num}
\def\@@pbnumbercommand{\bibleftnumber}

%D \macros{usepublications}
%D
%D We need \type{\usereferences} so that it is possible to
%D refer to page and/or appearance number for publications
%D in the other document.

\def\usepublications[#1]%
  {\usereferences[#1]\processcommalist[#1]\dousepublications}

\def\dousepublications#1%
  {\doonlyonce
     {#1.\f!bibextension}
     {\readfile{#1.\f!bibextension}
       {\showmessage\m!bib{4}{#1.\f!bibextension}}
       {\showmessage\m!bib{2}{#1.\f!bibextension}}}}

%D \macros{setuppublicationlist}
%D
%D This will be the first command in (\BIBTEX-generated) \type{bbl}
%D files. `samplesize' is a sample value (in case of \BIBTEX-generated
%D files, this will be the longest `short' key). `totalnumber'
%D is the total number of entries that will follow in this
%D file.

%D Both values are only needed for the label calculation
%D if `autohang' is `true', so by default the command is
%D not even needed, and therefore I saw no need to give
%D it it's own system variable and it just re-uses \type{pb}.

\def\setuppublicationlist
  {\dosingleempty\dosetuppublicationlist}

\def\dosetuppublicationlist[#1]%
  {\getparameters[\??pv data][#1]%
   \setuplist
    [pubs]
    [\c!samplesize={AA99},\c!totalnumber={99},
     \c!alternative=a,\c!interaction=,\c!pagenumber=\v!no,#1]}

\def\setuppublicationlayout[#1]#2%
  {\setvalue{\??pv data#1}{#2\unskip}}

%D \macros{bibalternative}
%D
%D A nice little shorthand that will be used so we don't have to
%D key in the weird \type{\@@pv} parameter names all the time.

\def\bibalternative#1%
  {\getvalue{\??pv\@@currentalternative#1}}

%D \macros{simplebibdef,bibcommandlist}
%D
%D \type{\simplebibdef} defines \type{bib@#1}, which in turn will
%D use one argument that is stored in \type{@@pb@#1}.
%D
%D \type{\simplebibdef} also defines \type{insert#1}, which can be
%D used in the argument of \type{\setuppublicationlayout} to fetch
%D one of the \type{@@pb@} data entries. \type{insert#1} then has
%D three arguments: \type{#1} are commands to be executed before the
%D data, \type{#2} are commands to be executed after the data, and
%D \type{#3} are commands to be executed if the data is not found.

%D \type{\bibcommandlist} is the list of commands that is affected
%D by this approach. Later on, it will be used to do a series
%D of assignments from \type{#1} to \type{bib@#1}: e.g
%D \type{\title} becomes \type{\bib@title} when used within
%D a publication.

\def\simplebibdef#1% hh: funny expansion ?
  {\@EA\long\@EA\def\csname bib@#1\endcsname##1%
     {\setvalue{\??pb @#1}{##1}%
      \ignorespaces}%
      \@EA\def\csname insert#1\endcsname##1##2##3%
        {\@EA\bibdoifelse
           \@EA{\csname @@pb@#1\endcsname}%
           {##1\csname @@pb@#1\endcsname##2}%
           {##3}%
      }}

\def\bibcommandlist
  {abstract, annotate, arttitle, assignee, bibnumber, bibtype, biburl, chapter, city,
   comment, country, day, dayfiled, doi, edition, eprint, howpublished, isbn, issn,
   issue, journal, keyword, keywords, lastchecked, month, monthfiled, names, nationality,
   note, notes, organization, pages, pubname, pubyear, revision, series, size, thekey,
   title, volume, yearfiled}


\processcommacommand[\bibcommandlist]\simplebibdef

\def\insertdoi#1#2#3%
    {{\bibdoifelse{\@@pb@doi}%
       {\edef\ascii{\@EA\detokenize\@EA{\@@pb@doi}}%
        #1\expanded{\gotoDOI{\@@pb@thekey}{\ascii}}#2}{#3}}}

\def\insertbiburl#1#2#3%
    {{\bibdoifelse{\@@pb@biburl}%
       {\edef\ascii{\@EA\detokenize\@EA{\@@pb@biburl}}%
        #1\expanded{\gotoURL{\@@pb@thekey}{\ascii}}#2}{#3}}}

\def\insertmonth#1#2#3%
  {\bibdoifelse{\@@pb@month}%
    {#1\doifnumberelse{\@@pb@month}%
       {\doifconversiondefinedelse\@@pbmonthconversion
          {\convertnumber\@@pbmonthconversion{\@@pb@month}}{\@@pb@month}}%
       {\@@pb@month}#2}{#3}}

\let\inserturl \insertbiburl  % for backward compat.
\let\inserttype\insertbibtype % for backward compat.

\def\newbibfield[#1]%
  {\simplebibdef{#1}%
   \edef\bibcommandlist{\bibcommandlist,#1}}

%D \macros{bib@crossref}
%D
%D \type{\crossref} is used in database files to point to another
%D entry. Because of this special situation, it has to be defined
%D separately. Since this command will not be seen until at
%D \type{\placepublications}, it may force extra runs. The same is
%D true for \type{\cite} commands inside of publications.

\def\bib@crossref#1%
  {\setvalue{\??pb @crossref}{#1}\ignorespaces}

\def\insertcrossref#1#2#3%
  {\bibdoifelse{\@@pb@crossref}
     {#1\@EA\cite\@EA[\@@pb@crossref]#2}
     {#3}}

%D \macros{complexbibdef,specialbibinsert}
%D
%D The commands \type{\artauthor}, \type{\author} and
%D \type{\editor} are more complex than the other commands.
%D Their argument lists have this form:
%D
%D \type{\author[junior]{firstnames}[inits]{von}{surname}}
%D
%D (bracketed stuff is optional)
%D
%D And not only that, but there also might be more than one of each of
%D these commands. This is why a special command is needed to insert
%D them, as well as one extra counter for each command.

%D All of these \type{\@EA}'s and \type{\csnames} make this code
%D look far more complex than it really is. For example, the argument
%D \type{author} defines the macro \type{\bib@author} to do two
%D things: increment the counter \type{\author@num} (let's say to 2)
%D and next store it's arguments in the macro \type{\@@pb@author2}.
%D And it defines \type{\insertauthors} to expand into
%D \starttyping
%D \specialbibinsert{author}{\author@num}{<before>}{<after>}{<not>}
%D \stoptyping

\def\docomplexbibdef#1%
   {\def\currentype{#1}%
    \dosingleempty\dodocomplexbibdef}

\def\dodocomplexbibdef[#1]#2%
   {\def\firstarg{#1}\def\secondarg{#2}%
    \dosingleempty\dododocomplexbibdef}

\def\dododocomplexbibdef[#1]#2#3%
  {\@EA\increment\csname \currentype @num\endcsname
      \setevalue{\??pb @\currentype\csname \currentype @num\endcsname}%
        {{\secondarg}{#2}{#3}{#1}{\firstarg}}\ignorespaces}%

\def\complexbibdef#1%
  {\@EA\newcounter\csname #1@num\endcsname
   \@EA\def\csname bib@#1\endcsname{\docomplexbibdef{#1}}%
   \@EA\def\csname insert#1s\endcsname##1##2##3%
     {\specialbibinsert{#1}{\csname #1@num\endcsname}{##1}{\unskip ##2}{##3}}}

\processcommalist[author,artauthor,editor]\complexbibdef

%D Another level of indirection is needed to control the
%D typesetting of all of these arguments.

%D Btw, there is a conflict between `author' and the predefined interface
%D variable `auteur'. The old version is overruled `auteur' is
%D overruled by the systemconstant definition at the top of this file!


\newcount\etallimitcounter
\newcount\etaldisplaycounter
\newcount\todocounter

\def\specialbibinsert#1#2#3#4#5%
  {\bgroup
   \ifnum#2>\zerocount
     \etallimitcounter  =0\bibalternative{#1etallimit}\relax
     \etaldisplaycounter=0\bibalternative{#1etaldisplay}\relax
     \ifnum #2>\etallimitcounter
       \todocounter\etaldisplaycounter
       % just in case ...
	   \ifnum\todocounter>\etallimitcounter
         \todocounter\etallimitcounter
       \fi
     \else
       \todocounter#2\relax
     \fi
     \ifnum\todocounter>\zerocount
       % find the current author list
       \let\templist\empty
       \dorecurse{#2}
          {\toks0=\@EA\@EA\@EA{\csname @@pb@#1\recurselevel\endcsname}%
           \ifx\templist\empty    \edef\templist{\the\toks0}%
           \else        \edef\templist{\templist,\the\toks0}\fi }%
       \doifdefinedelse
          {\??pv data#1\c!command}
          {\doifemptyelsevalue
             {\??pv data#1\c!command}
             {#3\dospecialbibinsert{#1}{\todocounter}{\templist}#4}%
             {#3\getvalue{\??pv data#1\c!command}{#1}{\todocounter}{\templist}#4}}%
          {#3\dospecialbibinsert{#1}{\todocounter}{\templist}#4}%
     \else
       #5%
     \fi
   \else
     #5%
   \fi
   \egroup}

%D This macro does the hard work of inserting a list of people in the
%D output, with proper regard of all the inbetween strings that can
%D arise depending on length of the list of people.

%D \#1 = type
%D \#2 = number of items to be typeset
%D \#3 = commacommand containing authors

\def\dospecialbibinsert#1#2#3%
    {\getcommacommandsize[#3]%
     \scratchcounter 0
     \def\processauthoritem##1%
       {\advance\scratchcounter1
       \ifnum \numexpr\scratchcounter-1\relax<#2\relax
          \getvalue{\??pv data#1}##1%
          \ifnum \scratchcounter=#2\relax
             \ifnum\etallimitcounter<\commalistsize\relax \bibalternative{#1etaltext}\fi
          \else \ifnum\numexpr\scratchcounter+1 = #2\relax
             \ifnum \commalistsize > \plustwo  \bibalternative\c!finalnamesep
             \else                            \bibalternative\c!lastnamesep \fi
          \else
              \bibalternative\c!namesep
          \fi \fi
        \fi}%
     \processcommacommand[#3]\processauthoritem }


%D \macros{invertedauthor,normalauthor,invertedshortauthor,normalshortauthor}
%D
%D Just some commands that can be used in \type{\setuppublicationparameters}
%D If you want to write an extension to the styles, you might
%D as well define some of these commands yourself.
%D
%D The argument liust has been reordered here, and the meanings
%D are:
%D
%D {\obeylines\parskip0pt
%D \type{#1} firstnames
%D \type{#2} von
%D \type{#3} surname
%D \type{#4} inits
%D \type{#5} junior
%D }
%D

\def\normalauthor#1#2#3#4#5%
  {\bibdoif{#1}{#1\bibalternative\c!firstnamesep}%
   \bibdoif{#2}{#2\bibalternative\c!vonsep}%
   #3%
   \bibdoif{#5}{\bibalternative\c!surnamesep#5\unskip}}

\def\normalshortauthor#1#2#3#4#5%
  {\bibdoif{#4}{#4\bibalternative\c!firstnamesep}%
   \bibdoif{#2}{#2\bibalternative\c!vonsep}%
   #3%
   \bibdoif{#5}{\bibalternative\c!surnamesep#5\unskip}}

\def\invertedauthor#1#2#3#4#5%
  {\bibdoif{#2}{#2\bibalternative\c!vonsep}%
   #3%
   \bibdoif{#5}{\bibalternative\c!juniorsep #5}%
   \bibdoif{#1}{\bibalternative\c!surnamesep #1\unskip}}

\def\invertedshortauthor#1#2#3#4#5%
  {\bibdoif{#2}{#2\bibalternative\c!vonsep}%
   #3%
   \bibdoif{#5}{\bibalternative\c!juniorsep #5}%
   \bibdoif{#4}{\bibalternative\c!surnamesep #4\unskip}}

%D \macros{clearbibitem,clearbibitemtwo,bibitemdefs}
%D
%D These are used in \type{\typesetapublication} to do
%D initializations and cleanups.

\def\clearbibitem#1{\setvalue{\??pb @#1}{}}%

\def\clearbibitemtwo#1%
  {\letvalue{#1@num}\!!zerocount
   \scratchcounter\plusone
   \doloop
     {\doifdefinedelse{\??pb @#1\the\scratchcounter}
        {\letvalue{\??pb @#1\the\scratchcounter}\empty
         \advance\scratchcounter\plusone}%
        {\exitloop}}}

\def\bibitemdefs#1%
   {\@EA\let\@EA\tempa \csname bib@#1\endcsname
    \@EA\let\csname #1\endcsname \tempa }

%D \macros{startpublication}
%D
%D We are coming to the end of this module, to the macros that
%D do typesetting and read the \type{bbl} file.

\newcounter\bibcounter

%D Just a \type{\dosingleempty} is the most friendly
%D of doing this: there need not even be an argument
%D to \type{\startpublication}. Of course, then there
%D is no key either, and it had better be an
%D article (otherwise the layout will be all screwed up).
%D
%D Now prohibits comments, so % can be used for urls

\def\startpublication
  {\edef\bibmodsavedpercent{\the\catcode`\%}%
   \catcode`\%=12
   \dosingleempty\dostartpublication}

\def\stoppublication
  {} % the \catcode of % is reset below

%D Only specifying the key in the argument is also
%D legal. In storing this stuff into macros, some trickery with
%D token registers is needed to fix the expansion problems. Even so,
%D this appears to not always be 100\% safe, so people are
%D urgently advised to use \ETEX\ instead of traditional \TEX.
%D
%D In \ETEX, all expansion problems are conviniently solved by
%D the primitive \type{\protected}. To put that another way:
%D
%D It's not a bug in this module if it does not appear in \ETEX!

\long\def\dostartpublication[#1]#2\stoppublication%
  {\increment\bibcounter
   \bgroup
   \doifassignmentelse{#1}%
     {\getparameters[\??pb][k=,t=article,n=,s=,a=,y=,o=,u=,#1]}%
     {\getparameters[\??pb][k=#1,t=article,n=,s=,a=,y=,o=,u=]}%
   \@EA\toks\@EA2\@EA{\@@pba}%
   \@EA\toks\@EA4\@EA{\@@pbs}%
   \toks0={\ignorespaces #2}%
   \setxvalue{pbdk-\@@pbk}{\@@pbk}
   \setxvalue{pbda-\@@pbk}{\the\toks2}
   \setxvalue{pbdy-\@@pbk}{\@@pby}
   \setxvalue{pbds-\@@pbk}{\the\toks4}
   \setxvalue{pbdn-\@@pbk}{\@@pbn}
   \setxvalue{pbdt-\@@pbk}{\@@pbt}
   \setxvalue{pbdo-\@@pbk}{\@@pbo}
   \setxvalue{pbdu-\@@pbk}{\@@pbu}
   \setxvalue{pbdd-\@@pbk}{\the\toks0}
   \xdef\allrefs{\allrefs,\@@pbk}%
   \egroup
   \catcode`\%=\bibmodsavedpercent\relax }

% intialization of the order-list:

\let\allrefs\empty

%D The next macro is needed because the number command of the
%D publist sometimes needs to fetch something from the current
%D item (like the 'short' key). For this, the ID of the current
%D item is passed in the implict parameter \type{\@@pbk}

\def\makepbkvalue#1{\def\@@pbk{#1}}

%D

\newif\ifinpublist

% from Hans

\def\ignoresectionconversion
  {\let\@@sectionconversion\secondoftwoarguments}

\let\normaldosetfilterlevel\dosetfilterlevel

\def\patcheddosetfilterlevel#1#2% beware: this one is \let
  {\bgroup
   \ignoresectionconversion
   \edef\askedlevel{#1}%
   \edef\askedfilter{#2}%
%   \message{ASKD: \meaning\askedlevel}%
%   \message{PREV: \meaning\v!previous}%
   \ifx\askedlevel\v!current
     \dosetcurrentlevel\askedlevel
   \else\ifx\askedlevel\v!previous
     \dosetpreviouslevel\askedlevel
   \else\ifx\askedlevel\v!all
     \global\chardef\alltoclevels\plusone
   \else\ifx\askedlevel\v!text
     \global\chardef\alltoclevels\plusone
   \else
     \edef\byaskedlevel{\csname\??by\askedlevel\endcsname}%
     \ifx\byaskedlevel\v!text
       \dosettextlevel\askedlevel
     \else
       \dosetotherlevel\askedlevel
     \fi
   \fi\fi\fi\fi
   % experiment
   \ifx\askedfilter\empty \else
     \xdef\currentlevel{\currentlevel\sectionseparator\askedfilter}%
   \fi
   \egroup}


\unless\ifcsname currentlocationrefence\endcsname

\let\currentlocationreference\empty

\def\dogetreferenceelements#1#2#3#4#5%
  {\chardef\currentreferencetype=\ifx#1\relax0\else#1\fi\relax
   \ifnum\currentreferencetype<2
     \edef\currentpagereference{#2}%
     \let \currentdatareference\empty
     \edef\currentlocationreference{#2}%
     \ifx\currentpagereference   \empty
       \let\currentfolioreference\folio
     \else
       \def \currentpagereference {\referencepagenumber[#2]}%
       \edef\currentfolioreference{\dosplitofffoliopart[#2]}%
     \fi
     \edef\currentrealreference{#3}%
     \settextreferences#4\end
     \ifnum0#5<\crossreferencenumber
       \forwardreferencetrue
     \else
       \forwardreferencefalse
     \fi
   \else
     \let \currentlocationreference\empty
     \edef\currentrealreference {#3}%
     \def \currentdatareference {#2}%
     \let \currentfolioreference\folio
     \settextreferences#4\end
     \forwardreferencefalse
   \fi
   \ifodd\currentreferencetype
     \realreferencepagefalse
   \else
     \docheckrealreferencepage\currentrealreference
     \ifrealreferencepage \else
       \docheckrealreferencepage\currentdatareference
     \fi
   \fi}

\fi

\def\filllocalpublist%
  {\let\dosetfilterlevel\patcheddosetfilterlevel
   \dosettoclevel\??li{pubs}%
   \let\dosetfilterlevel\normaldosetfilterlevel
   \global\let\glocalpublist\empty
   \doloop
     {\doifdefinedelse
        {\r!cross cite-\jobname-\recurselevel}
        {\doifreferencefoundelse
           {cite-\jobname-\recurselevel}
           {\@EA\doifreglevelelse\@EA[\currentlocationreference]
              {\@EA\doglobal\@EA\addtocommalist\@EA
                {\currenttextreference}\glocalpublist}{}}
           {}}%
        {\exitloop}}%
    \let\localpublist\glocalpublist}

%
\def\typesetpubslist
  {\dobeginoflist
   \edef\askedlevel{\csname \??li pubs\c!criterium\endcsname}%
   \ifx\askedlevel\v!all
      \def\bibrefprefix{}%
   \else %
      \preparebibrefprefix
   \fi
   \ifsortbycite
     \filllocalpublist
     \iftypesetall
       \let\localallrefs\allrefs
        \processcommacommand[\localpublist]\typesetapublication
        \def\removefromallrefs##1%
           {\removefromcommalist{##1}\localallrefs }%
        \processcommacommand[\localpublist]\removefromallrefs
        \processcommacommand[\localallrefs]\typesetapublication
      \else
        \processcommacommand[\localpublist]\typesetapublication
     \fi
   \else
     \iftypesetall
       \processcommacommand[\allrefs]\typesetapublication
     \else
       %
       \filllocalpublist
       \processcommacommand[\allrefs]\maybetypesetapublication
     \fi
   \fi
   \doendoflist }

\newif\ifinpublist
\def\maybetypesetapublication#1%
  {\global\inpublistfalse
   \def\test{#1}%
   \def\runtest##1%
      {\def\tempa{##1}\ifx \test\tempa \global\inpublisttrue \fi}%
   \processcommacommand[\localpublist]\runtest
   \ifinpublist \typesetapublication{#1}\fi}

\def\initializepubslist
  {\edef\@@pbnumbering{\@@pbnumbering}%
   \ifautohang
     \ifx\@@pbnumbering\v!short
       \setbox\scratchbox\hbox{\@@pbnumbercommand{\getvalue{\??li pubs\c!samplesize}}}%
     \else\iftypesetall
       \setbox\scratchbox\hbox{\@@pbnumbercommand{\getvalue{\??li pubs\c!totalnumber}}}%
     \else
       \setbox\scratchbox\hbox{\@@pbnumbercommand{\numreferred}}%
     \fi\fi
     \edef\samplewidth{\the\wd\scratchbox}%
     \setuplist[pubs][\c!width=\samplewidth,\c!distance=0pt]%
     \ifx\@@pbnumbering\v!short
       \def\@@pblimitednumber##1{\hbox to \samplewidth
           {\@@pbnumbercommand{\getvalue{pbds-\@@pbk}}}}%
     \else \ifx \@@pbnumbering\v!bib
       \def\@@pblimitednumber##1{\hbox to \samplewidth
           {\@@pbnumbercommand{\getvalue{pbdn-\@@pbk}}}}%
     \else
       \def\@@pblimitednumber##1{\hbox to \samplewidth{\@@pbnumbercommand{##1}}}%
     \fi \fi
   \else
     \ifx\@@pbnumbering\v!short
     \doifemptyelse
        {\getvalue{\??li pubs\c!width}}
        {\def\@@pblimitednumber##1{\hbox
            {\@@pbnumbercommand{\getvalue{pbds-\@@pbk}}}}}%
        {\def\@@pblimitednumber##1{\hbox to \getvalue{\??li pubs\c!width}%
            {\@@pbnumbercommand{\getvalue{pbds-\@@pbk}}}}}%
     \else \ifx \@@pbnumbering\v!bib
        \doifemptyelse
          {\getvalue{\??li pubs\c!width}}
          {\def\@@pblimitednumber##1{\hbox
            {\@@pbnumbercommand{\getvalue{pbdn-\@@pbk}}}}}%
          {\def\@@pblimitednumber##1{\hbox to \getvalue{\??li pubs\c!width}%
            {\@@pbnumbercommand{\getvalue{pbdn-\@@pbk}}}}}%
       \else
       \doifemptyelse
          {\getvalue{\??li pubs\c!width}}
          {\def\@@pblimitednumber##1{\hbox{\@@pbnumbercommand{##1}}}}%
          {\def\@@pblimitednumber##1{\hbox to \getvalue{\??li pubs\c!width}{\@@pbnumbercommand{##1}}}}%
        \fi
     \fi
   \fi
   \ifx\@@pbnumbering\v!no
     \setuplist[pubs][\c!numbercommand=,\c!symbol=\v!none,\c!textcommand=\outdented]%
   \else
     \setuplist[pubs][\c!numbercommand=\@@pblimitednumber]%
   \fi
   \doifelsevalue
      {\??pv datamaybeyear}{\v!off}{\def\maybeyear##1{}}{\def\maybeyear##1{##1}}%
   \forgetall % bugfix 2005/03/18
}


\def\outdented#1% move to supp-box ?
  {\hskip -\hangindent #1}


%D The full list of publications

\def\completepublications
  {\dosingleempty\docompletepublications}

\def\bibdogetupsometextprefix#1#2#3%
  {\ifcsname#2#1#3\endcsname
     \csname#2#1#3\endcsname
   \else\@EA\ifx\csname\??la#1\c!default\endcsname\empty
     \ifcsname#2#3\endcsname
       \csname#2#3\endcsname
     \else\ifcsname#2\s!en#3\endcsname
       \csname#2\s!en#3\endcsname
     \else
       \reporttextprefixerror{#1}{#2}{#3}%
     \fi\fi
   \else
     \bibdogetupsometextprefix{\csname\??la#1\c!default\endcsname}{#2}{#3}%
   \fi\fi}

\def\docompletepublications[#1]%
  {\begingroup
   \setuplist[pubs][\c!criterium=\v!previous,#1]
   \begingroup
   \let\handletextprefix\firstoftwoarguments
   \edef\headtextpubs{\bibdogetupsometextprefix\headlanguage\c!title{pubs}}%
   \doifdefinedelse
      {\??pv data\v!title}
      {\doifemptyelsevalue
          {\??pv data\v!title}
          {\expanded{\systemsuppliedtitle[pubs]{\headtextpubs}}}%
          {\expanded{\getvalue{\??pv data\v!title}{\headtextpubs}}}%
       }%
      {\expanded{\systemsuppliedtitle[pubs]{\headtextpubs}}}%
   \endgroup
   \dodoplacepublications   }

%D And the portion with the entries only.

\def\placepublications
  {\dosingleempty\doplacepublications}

\def\doplacepublications[#1]%
  {%\getparameters[\??pv data][#1]
   \begingroup
   \setuplist[pubs][\c!criterium=\v!previous,#1]%
   \dodoplacepublications }%

\def\dodoplacepublications%
  {\initializepubslist
   \doifelsevalue
      {\??li pubs\c!option}{\v!continue}%
      {}%
      {\global\let\bibcounter\!!zerocount }%
   \inpublisttrue
   \typesetpubslist
   \inpublistfalse
   \endgroup }


%D \subsubject{What's in a publication}
%D

\unexpanded\def\typesetapublication#1%
  {\doifsomething{#1}{\doglobal\increment\bibcounter
    \bgroup
    \makepbkvalue{#1}%
    \ifgridsnapping
      \snaptogrid\vbox{\dodolistelement{pubs}{}{\bibcounter}%
        {\expanded{\reference[\bibrefprefix#1]{\bibcounter}}%
         \strut \dotypesetapublication{#1}\strut }{}{}}%
    \else
      \dodolistelement{pubs}{}{\bibcounter}%
        {\expanded{\reference[\bibrefprefix#1]{\bibcounter}}%
         \strut \dotypesetapublication{#1}\strut }{}{}%
    \fi
    \egroup }}

\def\dotypesetapublication#1%
  {\bgroup
   \def\@@currentalternative{data}%
   \processcommacommand[\bibcommandlist,crossref]\clearbibitem
   \processcommalist   [artauthor,author,editor]\clearbibitemtwo
   \processcommacommand[\bibcommandlist]\bibitemdefs
   \processcommalist   [artauthor,author,editor,crossref]\bibitemdefs
   \let\biblanguage\empty
   \getvalue{pbdd-#1}%
   \ifcsname pbdt-#1\endcsname \bibalternative{\getvalue{pbdt-#1}}\fi
   \egroup }


%D An afterthought

\def\maybeyear#1{}

%D An another

\def\noopsort#1{}

%D This is the result of bibtex's `language' field.

\def\setbiblanguage#1#2{\setvalue{\??pb @lang@#1}{#2}}

\def\lang#1%
   {\def\biblanguage{#1}%
    \ifcsname \??pb @lang@#1\endcsname
        \expanded{\language[\getvalue{\??pb @lang@#1}]}%
     \fi \ignorespaces}


%D \subject{Citations}

%D \macros{cite,bibref}
%D
%D The indirection with \type{\dobibref} allows \LATEX\ style
%D \type{\cite} commands with a braced argument (these might appear
%D in included data from the \type{.bib} file).

\unexpanded\def\cite
  {\doifnextcharelse{[}
     {\dodocite}
     {\dobibref}}

\def\dobibref#1%
  {\docite[#1][]}

\def\dodocite[#1]%
  {\startstrictinspectnextcharacter
   \dodoubleempty\dododocite[#1]}

\def\dododocite[#1][#2]{%
   \stopstrictinspectnextcharacter
   \docite[#1][#2]}

\def\docite[#1][#2]%
  {\begingroup
   \setupinteraction[\c!style=]