%D \module
%D   [       file=publ-imp-cite,
%D        version=2013.12.24,
%D          title=\CONTEXT\ Publication Support,
%D       subtitle=Citations,
%D         author=Alan Braslau and Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

\unprotect

\starttexdefinition mutable protected btx:cite:inject #content
    \texdefinition{btx:format:gotointernal} {
        #content
    }
\stoptexdefinition

\starttexdefinition mutable protected btx:cite:checkconcat
    \ifnum\currentbtxcount>\zerocount
        \let\currentbtxinternal\empty
        \let\currentbtxbacklink\empty
    \fi
\stoptexdefinition

% The null case:

\startsetups btx:cite:none
    \btxcitereference
    % dummy
\stopsetups

\startsetups btx:cite:nocite
    \dontleavehmode
    \btxcitereference
\stopsetups

\startsetups btx:cite:unknown
    \begingroup
        \tttf
        \btxcitereference
        \currentbtxfirst
    \endgroup
\stopsetups

\startsetups btx:cite:empty
    \btxcitereference
    <empty>
\stopsetups

\startsetups btx:cite:lefttext
  \ifx\currentbtxlefttext\empty \else
    \startbtxrunningstyleandcolor
        \setbtxparameterset{\s!cite}{lefttext}
        \btxparameter\c!left
            \currentbtxlefttext
        \btxparameter\c!right
    \stopbtxrunningstyleandcolor
  \fi
\stopsetups

\startsetups btx:cite:righttext
    \ifx\currentbtxrighttext\empty \else
        \startbtxrunningstyleandcolor
            \setbtxparameterset{\s!cite}{righttext}
            \btxparameter\c!left
                \currentbtxrighttext
            \btxparameter\c!right
        \stopbtxrunningstyleandcolor
    \fi
\stopsetups

\startsetups btx:cite:invalid
     \btxcitereference
   % {\tt <\currentbtxreference>}
     {\tt <\currentbtxmissingreference>}
\stopsetups

\startsetups btx:cite:concat
    \startbtxrunningstyleandcolor
        \btxparameter{\c!separator:\number\currentbtxconcat}
    \stopbtxrunningstyleandcolor
\stopsetups

% when we have an author-year combination, the first and seconds is not
% fields data but something more complex (that itself calls for a setup)

\startsetups btx:cite:normal
    \fastsetup{\s!btx:\s!cite:concat}
    \fastsetup{\s!btx:\s!cite:lefttext}
    \ifx\currentbtxfirst\empty
        \fastsetup{\s!btx:\s!cite:\s!empty}
    \else\ifx\currentbtxsecond\empty
        \texdefinition {\s!btx:\s!cite:inject} {
            \btxcitereference
            \btxusecommand [\currentbtxspecification:cite:\currentbtxcitealternative] {
                \currentbtxfirst
                \btxflushsuffix
            }
        }
    \else
        \texdefinition {\s!btx:\s!cite:inject} {
            \btxcitereference
            \btxusecommand [\currentbtxspecification:cite:\currentbtxcitealternative] {
                \currentbtxfirst
                \btxparameter\c!inbetween
                \currentbtxsecond
                \btxflushsuffix
             }
        }
    \fi\fi
    \fastsetup{\s!btx:\s!cite:righttext}
\stopsetups

\startsetups btx:cite:range
    \fastsetup{\s!btx:\s!cite:concat}
    \fastsetup{\s!btx:\s!cite:lefttext}
    \ifx\currentbtxfirst\empty
        \fastsetup{\s!btx:\s!cite:\s!empty}
    \else
        \texdefinition {\s!btx:\s!cite:inject} {
            \btxcitereference
            \btxusecommand [\currentbtxspecification:cite:\currentbtxcitealternative] {
                \currentbtxfirst
                \ifx\currentbtxsecond\empty \else
                    \btxparameter\c!range
                    \currentbtxsecond
                \fi
                \btxflushsuffix
            }
        }
    \fi
    \fastsetup{\s!btx:\s!cite:righttext}
\stopsetups

% somehow related to keywords:

\startsetups btx:cite:listelement
    \fastsetup{\s!btx:\s!cite:concat}
    \fastsetup{\s!btx:\s!cite:lefttext}
    \ifx\currentbtxfirst\empty
        \fastsetup{\s!btx:\s!cite:\s!empty}
    \else
       \texdefinition {\s!btx:\s!cite:inject} {
           \btxcitereference
           \currentbtxfirst
       }
    \fi
    \fastsetup{\s!btx:\s!cite:righttext}
\stopsetups

\startsetups \s!btx:\s!cite:entry
    \fastsetup{\s!btx:\s!cite:concat}
    \fastsetup{\s!btx:\s!cite:lefttext}
    \btxhandleciteentry
    \fastsetup{\s!btx:\s!cite:righttext}
\stopsetups

\startsetups \s!btx:\s!cite:footnote
    \startfootnote
        \fastsetup{btx:cite:entry}
    \stopfootnote
\stopsetups

% these three are goodies to get something but are not set up as it makes no
% sense to have something root for combinations like this (esp not because one
% gets default anyway

% AB: not so sure about that. Why define them in default rather than here?

\startsetups \s!btx:\s!cite:author
    \fastsetup{\s!btx:\s!cite:normal}
\stopsetups
\startsetups \s!btx:\s!cite:authoryear
    \fastsetup{\s!btx:\s!cite:normal}
\stopsetups
\startsetups \s!btx:\s!cite:authoryears
    \fastsetup{\s!btx:\s!cite:normal}
\stopsetups
\startsetups \s!btx:\s!cite:authornum
    \fastsetup{\s!btx:\s!cite:normal}
\stopsetups
\startsetups \s!btx:\s!cite:num
    \fastsetup{\s!btx:\s!cite:range}
\stopsetups
\startsetups \s!btx:\s!cite:default
    \fastsetup{\s!btx:\s!cite:num}
\stopsetups
\startsetups \s!btx:\s!cite:textnum
    \fastsetup{\s!btx:\s!cite:num}
\stopsetups
\startsetups \s!btx:\s!cite:year
    \fastsetup{\s!btx:\s!cite:range}
\stopsetups
\startsetups \s!btx:\s!cite:author:num
    \fastsetup{\s!btx:\s!cite:range}
\stopsetups
\startsetups \s!btx:\s!cite:author:year
    \fastsetup{\s!btx:\s!cite:range}
\stopsetups
\startsetups \s!btx:\s!cite:author:years
    \fastsetup{\s!btx:\s!cite:range}
\stopsetups

\startsetups \s!btx:\s!cite:index
    \fastsetup{\s!btx:\s!cite:range}
\stopsetups
\startsetups \s!btx:\s!cite:category
    \fastsetup{\s!btx:\s!cite:normal}
\stopsetups
\startsetups \s!btx:\s!cite:short
    \fastsetup{\s!btx:\s!cite:normal}
\stopsetups
\startsetups \s!btx:\s!cite:tag
    \fastsetup{\s!btx:\s!cite:normal}
\stopsetups

% the following correspond to fields, but can be used in many renderings

\startsetups \s!btx:\s!cite:keywords
    \fastsetup{\s!btx:\s!cite:list}
\stopsetups
\startsetups \s!btx:\s!cite:title
    \fastsetup{\s!btx:\s!cite:normal}
\stopsetups
\startsetups \s!btx:\s!cite:pages
    \fastsetup{\s!btx:\s!cite:range}
\stopsetups

% is the next one used?
% Yes, bibtex is a mess and one can have pages or sometimes page

\startsetups \s!btx:\s!cite:page
    \fastsetup{\s!btx:\s!cite:normal}
\stopsetups

% the following are kind of specific, but can be used in many renderings

\startsetups btx:cite:url
    \ifx\currentbtxfirst\empty
        \fastsetup{\s!btx:\s!cite:\s!empty}
    \else
        \texdefinition {btx:format:goto} {
            url(\currentbtxfirst)
        } {
            \btxcitereference
            \hyphenatedurl{\currentbtxfirst}
        }
    \fi
\stopsetups

\startsetups btx:cite:doi
    \ifx\currentbtxfirst\empty
        \fastsetup{\s!btx:\s!cite:\s!empty}
    \else
        \texdefinition {btx:format:goto} {
            url(http://dx.doi.org/\currentbtxfirst)
        } {
            \btxcitereference
            \hyphenatedurl{doi:\currentbtxfirst}
        }
    \fi
\stopsetups

\protect

