%D \module
%D   [       file=math-del,
%D        version=2007.07.19,
%D          title=\CONTEXT\ Math Macros,
%D       subtitle=Delimiters,
%D         author=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.

\writestatus{loading}{ConTeXt Math Macros / Delimiters}

\unprotect

%D Old code that will be redone:
%D
%D \macros
%D   {checkdelimiters, fakeleftdelimiter, fakerightdelimiter}
%D
%D Handy for non matching situations (as with mathml):
%D
%D \starttyping
%D \checkdelimiters{... bla bla ...}
%D \fakeleftdelimiter
%D  ... bla bla ...
%D \fakerightdelimiter
%D \stoptyping

% Actually we can do better now with lua hacks.

\newcount\c_math_delimiter_nesting

\def\leftfakedelimiter {\advance\c_math_delimiter_nesting\minusone\gobbleoneargument}
\def\rightfakedelimiter{\advance\c_math_delimiter_nesting\plusone \gobbleoneargument}

\def\checkdelimiters#1%
  {\c_math_delimiter_nesting\zerocount
   \setbox\scratchbox\hbox\bgroup
     \let\left \leftfakedelimiter
     \let\right\rightfakedelimiter
     \normalstartimath#1\expandafter\normalstopimath\expandafter
   \egroup
   \expandafter\c_math_delimiter_nesting\the\c_math_delimiter_nesting\relax}

\def\fakeleftdelimiter {\ifnum\c_math_delimiter_nesting>\zerocount\left .\fi}
\def\fakerightdelimiter{\ifnum\c_math_delimiter_nesting<\zerocount\right.\fi}

%D The following macros are used in the MathML interpreter, so
%D there is a good change of them never being documented for
%D other usage.

\let\normalordelimiter\secondoftwoarguments
\let\normalorfiller   \firstoftwoarguments

\unexpanded\def\enabledelimiter {\let\normalordelimiter\secondoftwoarguments}
\unexpanded\def\disabledelimiter{\let\normalordelimiter\firstoftwoarguments}

\unexpanded\def\enablefiller    {\let\normalorfiller\secondoftwoarguments}
\unexpanded\def\disablefiller   {\let\normalorfiller\firstoftwoarguments}

% \def\mathopnolimits#1{\mathop{\mr\mathrm#1}\nolimits} % was \rm, which follows text fonts (used in mml parser)
% \def\mathopdolimits#1{\mathop{\mr\mathrm#1}}          % was \rm, which follows text fonts (used in mml parser)

\def\mathopnolimits#1{\mathop{\mathrm#1}\nolimits} % was \rm, which follows text fonts (used in mml parser, check!)
\def\mathopdolimits#1{\mathop{\mathrm#1}}          % was \rm, which follows text fonts (used in mml parser, check!)

% A new experiment:

% Hm, we already have \definemathextensible so we need to preserve that one. We now
% use \definemathdelimiter.

\installcorenamespace {mathdelimiter}

\installcommandhandler \??mathdelimiter {mathdelimiter} \??mathdelimiter

\let\setupmathdelimiters\setupmathdelimiter

\setupmathdelimiters
  [\c!symbol=0,
   \c!command=\v!yes,
   \c!factor=1.5,
   \c!axis=\v!yes,
 % \c!exact=\v!yes
   \c!height=\exheight,
   \c!depth=\exheight]

\appendtoks
    \edef\p_command{\mathdelimiterparameter\c!command}%
    \ifx\p_command\v!yes
        \setuevalue\currentmathdelimiter{\math_fenced_extensible[\currentmathdelimiter]}%
    \fi
\to \everydefinemathdelimiter

\unexpanded\def\math_fenced_extensible
  {\dodoubleempty\math_fenced_extensible_indeed}

\unexpanded\def\math_fenced_extensible_indeed[#1][#2]%
  {\mathop
     {\edef\currentmathdelimiter{#1}%
      \edef\p_factor{\mathdelimiterparameter\c!factor}%
      \ifsecondargument
        \doifassignmentelse{#2}
          {\setupcurrentmathdelimiter[#2]%
           \edef\p_factor{\mathdelimiterparameter\c!factor}}%
          {\edef\p_factor{#2}}%
      \else
        \edef\p_factor{\mathdelimiterparameter\c!factor}%
      \fi
      \edef\p_exact{\mathdelimiterparameter\c!exact}%
      \edef\p_axis {\mathdelimiterparameter\c!axis}%
      \edef\p_leftoffset{\mathdelimiterparameter\c!leftoffset}%
      \edef\p_rightoffset{\mathdelimiterparameter\c!rightoffset}%
      \ifx\p_leftoffset\empty\else
        \mskip\p_leftoffset
      \fi
      \Uvextensible
         \ifx\p_exact\v!yes exact \fi
         \ifx\p_axis \v!yes axis  \fi
         height \p_factor\dimexpr\mathdelimiterparameter\c!height\relax
         depth  \p_factor\dimexpr\mathdelimiterparameter\c!depth \relax
         \Udelimiter\zerocount\zerocount\mathdelimiterparameter\c!symbol
      \relax
      \ifx\p_rightoffset\empty\else
        \mskip\p_rightoffset
      \fi}}

\let\mathdelimiter\math_fenced_extensible

\definemathdelimiter[integral][\c!symbol="222B]

% \setupmathdelimiter[integral][rightoffset=-3mu,exact=yes,factor=2]
%
% \let\inlineint \int
% \let\displayint\integral
%
% \unexpanded\def\int{\ifmmode\inlineordisplaymath\inlineint\displayint\else\normalint\fi}
%
% \startlines
% \ruledhbox{$\integral           f\frac{1}{2}$}
% \ruledhbox{$\integral[factor=1] f\frac{1}{2}$}
% \ruledhbox{$\integral[factor=3] f\frac{1}{2}$}
% \ruledhbox{$\int                f\frac{1}{2}$}
% \stoplines

\unexpanded\def\autointegral#1#2#3%
  {\ifmmode
     \setbox\nextbox\mathstylehbox{#3}%
     \scratchdimen\ifdim\nextboxht>\nextboxdp\nextboxht\else\nextboxdp\fi
     \mathlimop{%
       \Uvextensible
         height \scratchdimen
         depth \scratchdimen
         exact%
         axis%
         \Udelimiter \plusfour \zerocount "222B%
     }%
     \limits % nolimits needs more work: kerning and so
     \normalsuperscript{#1}%
     \normalsubscript{#2}%
     \box\nextbox
   \else
     \char"222B\relax
   \fi}

% \startformula
%     a =
%     \autointegral{t}{b}1 +
%     \autointegral{t}{b}{\frac{\frac{3}{4}}{\frac{1}{2}}} +
%     \autointegral{t}{b}{\frac{\frac{\frac{\frac{1}{2}}{2}}{2}}{2}}
% \stopformula

\protect \endinput

