%D \module
%D   [       file=xetx-ini,
%D        version=2004.09.11,
%D          title=\CONTEXT\ System Macros,
%D       subtitle=\XETEX\ Initializations,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA / Hans Hagen \& Ton Otten}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

%D We moved some code around and now have reorganized the xetex
%D code in the \type {xetx} module namespace.

\unprotect

%D Some defaults.

\ifdefined\XeTeXuseglyphmetrics
    \XeTeXuseglyphmetrics\plusone
\fi

%D Character classes.

\newcount\nofXTXcharinjections

\def\defineXTXcharinjectionclass[#1]%
  {\global\advance\nofXTXcharinjections\plusone
   \setxvalue{@xtx@cc@#1}{\number\nofXTXcharinjections}}

\setxvalue{@xtx@cc@\s!default}{0}

\def\getXTXcharinjectionclass#1%
  {\csname @xtx@cc@\ifcsname @xtx@cc@#1\endcsname#1\else\s!default\fi\endcsname}

\let\currentXTXcharinjection\s!default

\def\startXTXcharinjecxtions[#1]%
  {\pushmacro\currentXTXcharinjection
   \def\currentXTXcharinjection{#1}}

\def\stopXTXcharinjections
  {\popmacro\currentXTXcharinjection}

\def\defineXTXcharinjection #1 #2 %
  {\doifnumberelse{#1}{\edef\XTXclassone{\number#1}}{\edef\XTXclassone{\getXTXcharinjectionclas{#1}}}%
   \doifnumberelse{#2}{\edef\XTXclasstwo{\number#2}}{\edef\XTXclasstwo{\getXTXcharinjectionclas{#2}}}%
   \expanded{\dodefineXTXcharinjection{\XTXclassone}{\XTXclasstwo}}}

\def\setXTXcharcodes #1 #2 #3 % encoding syntax
  {\catcode#1=11 \lccode #1=#2 \uccode #1=#3 }

\def\dosetXTXcharcodes#1#2#3% compact syntax
  {\catcode#1=11 \lccode #1=#2 \uccode #1=#3 }

\ifdefined\XeTeXinterchartoks

    \long\def\dodefineXTXcharinjection#1#2#3%
      {\XeTeXinterchartoks #1 #2 {\XTXcharinjection{#1}{#2}}%
       \setvalue{@xtx@ch@\currentXTXcharinjection @#1@#2@}{#3}}

    \def\setXTXcharacterclass #1 #2 %
      {\doifnumberelse{#2}
         {\XeTeXcharclass#1=#2\relax}
         {\XeTeXcharclass#1=\getXTXcharinjectionclass{#2}\relax}}

    \def\dosetXTXcharacterclass#1% #2 fast one
      {\XeTeXcharclass#1=\getXTXcharinjectionclass}

\else

    \long\def\dodefineXTXcharinjection#1#2#3%
      {\setvalue{@xtx@ch@\currentXTXcharinjection @#1@#2@}{#3}}

    \def\setXTXcharacterclass #1 #2 %
      {}

\fi

\chardef\XTXcharinjectionsmode=1

\letvalue{@xtx@ch@\s!empty}\empty

\def\XTXcharinjection#1#2%
  {\csname @xtx@ch@%
     \ifcase\XTXcharinjectionsmode
       \s!empty
     \or
       \ifcsname @xtx@ch@\currentXTXcharinjection @#1@#2@\endcsname \currentXTXcharinjection @#1@#2@\fi
     \or
       \ifcsname @xtx@ch@\currentXTXcharinjection @#1@#2@\endcsname \currentXTXcharinjection @#1@#2@\else
       \ifcsname @xtx@ch@\s!default               @#1@#2@\endcsname \s!default               @#1@#2@\else
         \s!empty
       \fi\fi
     \else
       \s!empty
     \fi
   \endcsname}

\def\enableXTXcharinjections[#1]%
  {\def\currentXTXcharinjection{#1}}

\protect \endinput

\starttext

\startXTXcharinjecxtions[default]
    \defineXTXcharinjection 10 40 {[default]}
\stopXTXcharinjections

\startXTXcharinjecxtions[whatever]
    \defineXTXcharinjection 20 40 {[whatever]}
\stopXTXcharinjections

\chardef\XTXcharinjectionsmode=0

\enableXTXcharinjections[default]  A\XTXcharinjection{10}{40}B\XTXcharinjection{20}{40}C
\enableXTXcharinjections[whatever] A\XTXcharinjection{10}{40}B\XTXcharinjection{20}{40}C

\chardef\XTXcharinjectionsmode=1

\enableXTXcharinjections[default]  A\XTXcharinjection{10}{40}B\XTXcharinjection{20}{40}C
\enableXTXcharinjections[whatever] A\XTXcharinjection{10}{40}B\XTXcharinjection{20}{40}C

\chardef\XTXcharinjectionsmode=2

\enableXTXcharinjections[default]  A\XTXcharinjection{10}{40}B\XTXcharinjection{20}{40}C
\enableXTXcharinjections[whatever] A\XTXcharinjection{10}{40}B\XTXcharinjection{20}{40}C

\stoptext
