%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 unexpanded btx:cite:inject #content
    \ifconditional\btxinteractive
        \ifx\currentbtxinternal\empty
            #content
        \else
            \goto {
                #content
            } [
                \s!internal(\currentbtxinternal)
            ]
        \fi
    \else
        #content
    \fi
\stoptexdefinition

\starttexdefinition unexpanded 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
    \setbtxparameterset{\s!cite}{lefttext}
    \btxparameter\c!left
    \currentbtxlefttext
    \btxparameter\c!right
  \fi
\stopsetups

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

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

\starttexdefinition unexpanded btx:cite:concat
    \btxparameter{\c!separator:\number\currentbtxconcat}
\stoptexdefinition

% 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
%     \texdefinition{\s!btx:\s!cite:concat}
%     \currentbtxlefttext
%     \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
%           \btxflushsuffix
%         \else
%             \btxparameter\c!inbetween
%             \texdefinition {\s!btx:\s!cite:inject} {
%                 \btxusecommand [\currentbtxspecification:cite:\currentbtxcitealternative] {
%                     \currentbtxsecond
%                  }
%             }
%             % quite likely an error
%             \btxflushsuffix
%         \fi
%     \fi
%     \currentbtxrighttext
% \stopsetups

\startsetups btx:cite:normal
    \texdefinition{\s!btx:\s!cite:concat}
    %\currentbtxlefttext
    \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
      %  \texdefinition {\s!btx:\s!cite:inject} {
      %      \btxusecommand [\currentbtxspecification:cite:\currentbtxcitealternative] {
      %          \currentbtxsecond
      %       }
      %  }
      %  \btxflushsuffix
      %
      % probably better:
        \texdefinition {\s!btx:\s!cite:inject} {
            \btxcitereference
            \btxusecommand [\currentbtxspecification:cite:\currentbtxcitealternative] {
                \currentbtxfirst
                \btxparameter\c!inbetween
                \currentbtxsecond
                \btxflushsuffix
             }
        }
    \fi\fi
    %\currentbtxrighttext
    \fastsetup{\s!btx:\s!cite:righttext}
\stopsetups

\startsetups btx:cite:range
    \texdefinition{\s!btx:\s!cite:concat}
    %\currentbtxlefttext
    \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
    %\currentbtxrighttext
    \fastsetup{\s!btx:\s!cite:righttext}
\stopsetups

% somehow related to keywords:

\startsetups btx:cite:listelement
    \texdefinition{\s!btx:\s!cite:concat}
    %\currentbtxlefttext
    \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
    %\currentbtxrighttext
    \fastsetup{\s!btx:\s!cite:righttext}
\stopsetups

\startsetups \s!btx:\s!cite:entry
    \texdefinition{\s!btx:\s!cite:concat}
    %\currentbtxlefttext
    \fastsetup{\s!btx:\s!cite:lefttext}
    \btxhandleciteentry
    %\currentbtxrighttext
    \fastsetup{\s!btx:\s!cite:righttext}
\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\ifconditional\btxinteractive
        \goto {
            \btxcitereference
            \hyphenatedurl{\currentbtxfirst}
        } [
            url(\currentbtxfirst)
        ]
    \else
        \btxcitereference
        \hyphenatedurl{\currentbtxfirst}
    \fi\fi
\stopsetups

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

\protect
