%D \module
%D   [       file=math-frc,
%D        version=2013.04.06, % 2007.07.19,
%D          title=\CONTEXT\ Math Macros,
%D       subtitle=Fractions,
%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 / Fractions}

\unprotect

%D todo: struts ... depends on demand

%D This module is reimplemented in \MKIV\ style.

\registerctxluafile{math-frc}{1.001}

%D \macros
%D   {frac, xfrac, xxfrac}
%D
%D This is another one Tobias asked for. It replaces the primitive \type
%D {\over}. We also take the opportunity to handle math style restoring,
%D which makes sure units and chemicals come out ok. The \type {\frac}
%D macro kind of replaces the awkward \type {\over} primitive. Say that
%D we have the following formulas:
%D
%D \startbuffer[sample]
%D test $\frac  {1}{2}$ test $$1 + \frac  {1}{2} = 1.5$$
%D test $\xfrac {1}{2}$ test $$1 + \xfrac {1}{2} = 1.5$$
%D test $\xxfrac{1}{2}$ test $$1 + \xxfrac{1}{2} = 1.5$$
%D \stopbuffer
%D
%D \typebuffer[sample]
%D
%D With the most straightforward definitions, we get:
%D
%D \startbuffer[code]
%D \def\dofrac#1#2#3{\relax\mathematics{{{#1{#2}}\over{#1{#3}}}}}
%D
%D \def\frac  {\dofrac\mathstyle}
%D \def\xfrac {\dofrac\scriptstyle}
%D \def\xxfrac{\dofrac\scriptscriptstyle}
%D \stopbuffer
%D
%D \typebuffer[code] \getbuffer[code,sample]
%D
%D Since this does not work well, we can try:
%D
%D \startbuffer[code]
%D \def\xfrac #1#2{\hbox{$\dofrac\scriptstyle      {#1}{#2}$}}
%D \def\xxfrac#1#2{\hbox{$\dofrac\scriptscriptstyle{#1}{#2}$}}
%D \stopbuffer
%D
%D \typebuffer[code] \getbuffer[code,sample]
%D
%D This for sure looks better than:
%D
%D \startbuffer[code]
%D \def\xfrac #1#2{{\scriptstyle      \dofrac\relax{#1}{#2}}}
%D \def\xxfrac#1#2{{\scriptscriptstyle\dofrac\relax{#1}{#2}}}
%D \stopbuffer
%D
%D \typebuffer[code] \getbuffer[code,sample]
%D
%D So we stick to the next definitions (watch the local overloading of
%D \type {\xfrac}).
%D
%D In the meantime, in \LUATEX, we have better control over styles so the
%D following macros are different from the \MKII\ ones.

% obsolete, is now c!mathstyle
%
% 0=auto, 1=displaystyle, 2=textstyle, 3=scriptstyle, 4=scriptscriptstyle, 5=mathstyle
%
% $\mathfracmode0 \frac{1}{2}$
% $\mathfracmode1 \frac{1}{2}$
% $\mathfracmode2 \frac{1}{2}$
% $\mathfracmode3 \frac{1}{2}$
% $\mathfracmode4 \frac{1}{2}$
% $\mathfracmode5 \frac{1}{2}$
%
% we keep the constant for a while

\setnewconstant\mathfracmode\zerocount

\installcorenamespace{mathfractions}
\installcorenamespace{mathfractionstyle}
\installcorenamespace{mathfractionalternative}

\installcommandhandler \??mathfractions {mathfraction} \??mathfractions

\let\setupmathfractions\setupmathfraction

% color only applies to rule, use regular color for rest

\setupmathfractions
  [\c!mathstyle=,
   \c!alternative=\v!inner,
   \c!rulethickness=.25\exheight,
   \c!left=0x2E,
   \c!right=0x2E,
   \c!rule=\v!auto]

\appendtoks
    \setuevalue{\currentmathfraction}{\math_frac{\currentmathfraction}}%
\to \everydefinemathfraction

\unexpanded\def\math_frac#1%
  {\begingroup
   \edef\currentmathfraction{#1}%
   \edef\p_math_fractions_color{\mathfractionparameter\c!color}%
   \ifx\p_math_fractions_color\empty
     \expandafter\math_frac_normal
   \else
     \expandafter\math_frac_colored
   \fi}

\unexpanded\def\math_frac_colored#1#2%
  {\savecolor
   \colo_helpers_activate\p_math_fractions_color
   \math_frac_normal{\restorecolor#1}{\restorecolor#2}}

\unexpanded\def\math_frac_normal
  {\expandnamespaceparameter\??mathfractionalternative\mathfractionparameter\c!alternative\v!inner}

% we use utfchar anyway so we can as well do all at the lua end

\def\math_frac_command
  {\ctxcommand{math_frac(%
    "\mathfractionparameter\c!rule",%
    \number\mathfractionparameter\c!left,%
    \number\mathfractionparameter\c!right,%
    \number\dimexpr\mathfractionparameter\c!rulethickness%
  )}}

\setvalue{\??mathfractionalternative\v!inner}#1#2%
  {\Ustack{%
     {\usemathstyleparameter\mathfractionparameter{#1}}% we should store this one
     \math_frac_command
     {\usemathstyleparameter\mathfractionparameter{#2}}% and reuse it here
   }\endgroup}

\setvalue{\??mathfractionalternative\v!outer}#1#2%
  {\Ustack{%
     \usemathstyleparameter\mathfractionparameter
     {{#1}\math_frac_command{#2}}%
   }\endgroup}

\definemathfraction[frac][\c!mathstyle=]

\unexpanded\def\xfrac {\begingroup\let\xfrac\xxfrac\math_frac_alternative\scriptstyle}
\unexpanded\def\xxfrac{\begingroup                 \math_frac_alternative\scriptscriptstyle}

%D The \type {xx} variant looks still ugly, so maybe it's best to say:

\unexpanded\def\xxfrac#1#2%
  {\begingroup
   \math_frac_alternative\scriptscriptstyle{#1}{\raise.25\exheight\hbox{$\scriptscriptstyle#2$}}}

%D \macros
%D   {dfrac, tfrac, frac, dbinom, tbinom, binom}
%D
%D \startbuffer
%D $\dfrac {1}{2} \tfrac {1}{2} \frac {1}{2}$
%D $\dbinom{1}{2} \tbinom{1}{2} \binom{1}{2}$
%D \stopbuffer
%D
%D \typebuffer
%D \getbuffer

% $$
%     {{a}\over{b}} +
%     {{a}\overwithdelims(){b}} +
%     {{a}\atopwithdelims(){b}} +
%     {{a}\abovewithdelims()\zeropoint{b}} +
%     \left({{a}\over{b}}\right)
% $$

% \dorecurse {10} { % weird
%     $$ {{a}\abovewithdelims()#1pt{b}} $$
% }

% \unexpanded\def\dfrac #1#2{{\displaystyle     {{#1}\normalover                       {#2}}}}
% \unexpanded\def\tfrac #1#2{{\textstyle        {{#1}\normalover                       {#2}}}}

\definemathfraction[dfrac][\c!alternative=\v!outer,\c!mathstyle=\s!display]
\definemathfraction[tfrac][\c!alternative=\v!outer,\c!mathstyle=\s!text]
\definemathfraction[sfrac][\c!alternative=\v!outer,\c!mathstyle=\s!script]

% \definemathfraction[ddfrac][\c!mathstyle=\s!display]
% \definemathfraction[ttfrac][\c!mathstyle=\s!text]
% \definemathfraction[ssfrac][\c!mathstyle=\s!script]

% \unexpanded\def\binom #1#2{{\Ustack      {{#1}\normalabovewithdelims()\zeropoint{#2}}}}
% \unexpanded\def\dbinom#1#2{{\displaystyle{{#1}\normalabovewithdelims()\zeropoint{#2}}}}
% \unexpanded\def\tbinom#1#2{{\textstyle   {{#1}\normalabovewithdelims()\zeropoint{#2}}}}

\definemathfraction[binom] [\c!alternative=\v!outer,\c!rule=\v!no,\c!left=0x28,\c!right=0x29,\c!mathstyle=\s!auto]
\definemathfraction[dbinom][\c!alternative=\v!outer,\c!rule=\v!no,\c!left=0x28,\c!right=0x29,\c!mathstyle=\s!display]
\definemathfraction[tbinom][\c!alternative=\v!outer,\c!rule=\v!no,\c!left=0x28,\c!right=0x29,\c!mathstyle=\s!text]

%D \macros
%D   {cfrac}
%D
%D \startbuffer
%D $\cfrac{12}{3} \cfrac[l]{12}{3} \cfrac[c]{12}{3} \cfrac[r]{12}{3}$
%D $\cfrac{1}{23} \cfrac[l]{1}{23} \cfrac[c]{1}{23} \cfrac[r]{1}{23}$
%D \stopbuffer
%D
%D \typebuffer
%D
%D \getbuffer
%D
%D Now we can align every combination we want:
%D
%D \startbuffer
%D $\cfrac{12}{3} \cfrac[l]{12}{3} \cfrac[c]{12}{3} \cfrac[r]{12}{3}$
%D $\cfrac{1}{23} \cfrac[l]{1}{23} \cfrac[c]{1}{23} \cfrac[r]{1}{23}$
%D $\cfrac[cl]{12}{3} \cfrac[cc]{12}{3} \cfrac[cr]{12}{3}$
%D $\cfrac[lc]{1}{23} \cfrac[cc]{1}{23} \cfrac[rc]{1}{23}$
%D \stopbuffer
%D
%D \typebuffer
%D
%D \getbuffer

\unexpanded\def\cfrac
  {\doifnextoptionalelse\math_cfrac_yes\math_cfrac_nop}

\def\math_cfrac_nop    {\math_cfrac_indeed[cc]}
\def\math_cfrac_yes[#1]{\math_cfrac_indeed[#1cc]}

\def\math_cfrac_indeed[#1#2#3]#4#5%
  {{\displaystyle
    \frac
      {\strut
       \ifx r#1\hfill\fi#4\ifx l#1\hfill\fi}%
      {\ifx r#2\hfill\fi#5\ifx l#2\hfill\fi}%
    \kern-\nulldelimiterspace}}

%D \macros
%D   {splitfrac, splitdfrac}
%D
%D Occasionally one needs to typeset multi||line fractions.
%D These commands use \tex{genfrac} to create such fractions.
%D
%D \startbuffer
%D \startformula
%D      a=\frac{
%D          \splitfrac{xy + xy + xy + xy + xy}
%D                    {+ xy + xy + xy + xy}
%D        }
%D        {z}
%D      =\frac{
%D          \splitdfrac{xy + xy + xy + xy + xy}
%D                    {+ xy + xy + xy + xy}
%D        }
%D        {z}
%D \stopformula
%D \stopbuffer
%D
%D \typebuffer \getbuffer
%D
%D These macros are based on Michael J.~Downes posting on
%D comp.text.tex on 2001/12/06 but adapted a bit.

\unexpanded\def\splitfrac #1#2%
  {{\textstyle   {{\textstyle#1\quad\hfill}\normalabove\zeropoint{\textstyle\hfill\quad\mathstrut#2}}}}

\unexpanded\def\splitdfrac#1#2%
  {{\displaystyle{{          #1\quad\hfill}\normalabove\zeropoint{          \hfill\quad\mathstrut#2}}}}

\protect \endinput

% I have no clue what \mthfrac and \mthsqrt are supposed to do but
% I guess that it can be done with tweaking luatex's math parameters.
% Otherwise I'll write something from scratch.

% \def\math_stylebuilders_frac#1#2#3#4#5#6#7%
%   {\begingroup
%    \mathsurround\zeropoint
%    \setbox0\hbox{$#1 #6$}%
%    \setbox2\hbox{$#1 #7$}%
%    \dimen0\wd\ifdim\wd2>\wd0 2\else 0\fi
%    \setbox4\hbox to \dimen0{\leaders\hbox{#4}\hss#5}%
%    \mathord{\vcenter{{\offinterlineskip
%      \hbox to \dimen0{\hss\box0\hss}%
%      \kern\ht4%
%      \hbox to \dimen0{\hss\copy4\hss}%
%      \kern\ht4%
%      \hbox to \dimen0{\hss\box2\hss}}}}%
%    \endgroup}
%
% \def\math_stylebuilders_sqrt#1#2#3#4#5%
%   {\begingroup
%    \mathsurround\zeropoint
%    \setbox0\hbox{$#1 #5$}%
%    \ht0\dimexpr1.05\ht0+\onepoint\relax
%    \dp0\dimexpr1.05\dp0+\onepoint\relax
%    \setbox4\hbox to \wd0{\mr#2\leaders\hbox{#3}\hfill#4}%
%    \delimitershortfall\zeropoint
%    \nulldelimiterspace\zeropoint
%    \setbox2\hbox{$\left\delimiter"0270370 \vrule \s!height\ht0 \s!depth \dp0 \s!width\zeropoint\right.$}% is this the right code point?
%    \mathord{\vcenter{\hbox{\copy2\rlap{\raise\dimexpr\ht2-\ht4\relax\copy4}\copy0}}}%
%    \endgroup}
%
% \def\mthfrac#1#2#3#4#5{\mathchoice
%   {\math_stylebuilders_frac\displaystyle     \textface        {#1}{#2}{#3}{#4}{#5}}%
%   {\math_stylebuilders_frac\textstyle        \textface        {#1}{#2}{#3}{#4}{#5}}%
%   {\math_stylebuilders_frac\scriptstyle      \scriptface      {#1}{#2}{#3}{#4}{#5}}%
%   {\math_stylebuilders_frac\scriptscriptstyle\scriptscriptface{#1}{#2}{#3}{#4}{#5}}}
%
% \def\mthsqrt#1#2#3{\mathchoice
%   {\math_stylebuilders_sqrt\displaystyle     \textface{#1}{#2}{#3}}%
%   {\math_stylebuilders_sqrt\textstyle        \textface{#1}{#2}{#3}}%
%   {\math_stylebuilders_sqrt\scriptstyle      \textface{#1}{#2}{#3}}%
%   {\math_stylebuilders_sqrt\scriptscriptstyle\textface{#1}{#2}{#3}}}

% \unexpanded\def\mthfrac#1#2#3{[mthfrac: #1 #2 #3]}
% \unexpanded\def\mthsqrt#1#2#3{[mthsqrt: #1 #2 #3]}
