%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.

% todo : mathclass=\mathnothingcode, % todo also accept string

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

\unprotect

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

\registerctxluafile{math-frc}{autosuffix}

%D \macros
%D   {frac, xfrac, xxfrac}
%D
%D This is another one Tobias asked for. It replaces the primitive \type {\over}. We
%D also take the opportunity to handle math style restoring, which makes sure units
%D and chemicals come out ok. The \type {\frac} macro kind of replaces the awkward
%D \type {\over} primitive. Say that 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 following
%D macros are different from the \MKII\ ones.

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

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

\aliased\let\setupmathfractions\setupmathfraction

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

\integerdef\math_frac_no_delim "2E % or just \zerocount

\setupmathfractions
  [\c!mathstyle=,
   \c!method=\v!vertical,
   \c!mathnumeratorstyle=\mathfractionparameter\c!mathstyle,
   \c!mathdenominatorstyle=\mathfractionparameter\c!mathstyle,
   \c!mathmeaning=\currentglobalmathfraction,
   \c!mathclass=\mathfractioncode,
   \c!alternative=\v!inner,
   \c!margin=\zeropoint,
 % \c!rulethickness=.1\exheight, % was .25 for above / atop
   \c!rulethickness=\v!font,
   \c!left="2E,
   \c!right="2E,
   \c!middle="2F,
   \c!color=,
   \c!topcolor=\mathfractionparameter\c!textcolor,
   \c!bottomcolor=\mathfractionparameter\c!textcolor,
   \c!textcolor=\mathfractionparameter\c!color,
   \c!symbolcolor=\mathfractionparameter\c!color,
   \c!strut=\v!yes,
   \c!topdistance=,
   \c!bottomdistance=,
   \c!source=\zerocount,
   \c!hfactor=\plusfivehundred,% skewed
   \c!vfactor=\plusonehundredtwentyfive,% skewed
   \c!rule=\v!auto]

%D We now default to nice bars:

\integerdef\fractionbarextenderuc \privatecharactercode{fraction bar extender}

\setupmathfractions
  [\c!rule=\v!symbol,
   \c!symbol=\fractionbarextenderuc]

\appendtoks
    \protected\frozen\instance\edefcsname\currentmathfraction\endcsname{\math_frac{\currentmathfraction}}%
\to \everydefinemathfraction

% Sometimes users want control over the distances:

% So far for control.

% no    : \ruledhbox{\m{\frac[strut=no]   {28}{8\times29}}} \par
% tight : \ruledhbox{\m{\frac[strut=tight]{28}{8\times29}}} \par
% yes   : \ruledhbox{\m{\frac[strut=yes]  {28}{8\times29}}} \par
% math  : \ruledhbox{\m{\frac[strut=math] {28}{8\times29}}} \par
% text  : \ruledhbox{\m{\frac[strut=text] {28}{8\times29}}} \par

\installcorenamespace{mathfractionstrut}

\let\m_fractions_strut_top\relax
\let\m_fractions_strut_bot\relax

\def\math_frac_no_strut
  {\enforced\let\m_fractions_strut_top\relax
   \enforced\let\m_fractions_strut_bot\relax}

\defcsname\??mathfractionstrut\v!yes\endcsname
  {\enforced\let\m_fractions_strut_top\mathstrut
   \enforced\let\m_fractions_strut_bot\mathstrut}

\defcsname\??mathfractionstrut\v!math\endcsname
  {\enforced\let\m_fractions_strut_top\mathstrut
   \enforced\let\m_fractions_strut_bot\mathstrut}

\letcsname\??mathfractionstrut\v!no\endcsname\math_frac_no_strut

\defcsname\??mathfractionstrut\v!tight\endcsname
  {\enforced\let\m_fractions_strut_top\mathdepthstrut
   \enforced\let\m_fractions_strut_bot\mathheightstrut}

\defcsname\??mathfractionstrut\v!text\endcsname
  {\enforced\let\m_fractions_strut_top\strut
   \enforced\let\m_fractions_strut_bot\strut}

\math_frac_no_strut

\newdimension\d_math_fraction_margin

\def\math_fraction_margin_kern_indeed{\kern\d_math_fraction_margin}

\let\math_fraction_margin_kern\relax

%D \starttyping
%D $\Uover{2}{3} ^4$ % double script error but no longer because we use the prescripts
%D ${\over{2}{3}}^4$ % no issue anyway because we wrap in {} (goes unnoticed)
%D $\frac {2}{3} ^4$ % we wrap in \mathfrac so we can also use prescripts
%D \stoptyping

\installlocalcurrenthandler\??mathfractions {mathfraction}

\lettonothing\p_math_fraction_fences
\lettonothing\p_math_fraction_strut

\tolerant\protected\def\math_frac#1#*[#S#2]%%
  {\beginmathgroup
   %
   \setlocalmathfractioncurrent{#1}% \edef\currentmathfraction{#1}%
   %
   \ifparameter#2\or\setupcurrentmathfraction[#2]\fi
   %
   \edef\p_math_fraction_fences{\mathfractionparameter\c!fences}%
   \dostarttagged\t!mfractionstack{\mathfractionparameter\c!mathmeaning}%
   %
   \math_atom_by_parameter\mathfractionparameter\bgroup
   \ifempty\p_math_fraction_fences \else
     \math_fenced_fenced_start\p_math_fraction_fences
   \fi
   %
   \d_math_fraction_margin{\mathfractionparameter\c!margin}%
   \ifcase\d_math_fraction_margin
      \let\math_fraction_margin_kern\relax
   \else
      \let\math_fraction_margin_kern\math_fraction_margin_kern_indeed
   \fi
   %
   \edef\p_math_fraction_strut{\mathfractionparameter\c!strut}%
   \ifcsname\??mathfractionstrut\p_math_fraction_strut\endcsname
     \lastnamedcs
   \else
     \math_frac_no_strut
   \fi
   %
   % This only adds a level of structure so that we don't get the everthing tagged
   % fractionstack.
   \dostarttagged\t!mfraction\currentglobalmathfraction
   \expandnamespaceparameter\??mathfractionalternative\mathfractionparameter\c!alternative\v!inner}

\protected\def\math_frac_wrapup
  {\ifempty\p_math_fraction_fences \else
     \math_fenced_fenced_stop\p_math_fraction_fences
   \fi
   \dostoptagged
   \egroup
   \dostoptagged
   \endmathgroup}

% Having a \withmarginornot{#1}{#2} makes not much sense nor do 4 tests or 4 redundant
% kerns (longer node lists plus possible interference). A split in normal and margin
% also makes testing easier. When left and right margins are needed we might merge the
% variants again. After all, these are not real installers.
%
% The denominator is normally in cramped mode!
%
% See \MKIV\ for the old (more messy) implementation where we have to fight the over
% related syntax (we now use a lmtx primitive).

% timestamp: 01/2022 : (re)watching eNerd⁴ - Time Capsule 2020 a.k.a. Farewell Party
% for Normalcy (UHD) with the soekris/neumanns combination

% Todo: \Umathfrac for all of them as we're keyword driven anyway.

\installcorenamespace{fractionmethods}

\defcsname\??fractionmethods\v!vertical  \v!no     \v!none\endcsname{\Uatop}
\defcsname\??fractionmethods\v!vertical  \v!no            \endcsname{\Uatopwithdelims
                                                                     \Umathchar\zerocount\fam\scratchcounterone
                                                                     \Umathchar\zerocount\fam\scratchcountertwo}
\defcsname\??fractionmethods\v!vertical  \v!yes    \v!none\endcsname{\Uabove}
\defcsname\??fractionmethods\v!vertical  \v!yes           \endcsname{\Uabovewithdelims
                                                                     \Umathchar\zerocount\fam\scratchcounterone
                                                                     \Umathchar\zerocount\fam\scratchcountertwo}
\defcsname\??fractionmethods\v!vertical  \v!hidden \v!none\endcsname{\Uabove}
\defcsname\??fractionmethods\v!vertical  \v!hidden        \endcsname{\Uabovewithdelims
                                                                     \Umathchar\zerocount\fam\scratchcounterone
                                                                     \Umathchar\zerocount\fam\scratchcountertwo}
\defcsname\??fractionmethods\v!vertical  \v!auto   \v!none\endcsname{\Uover}
\defcsname\??fractionmethods\v!vertical  \v!auto          \endcsname{\Uoverwithdelims
                                                                     \Umathchar\zerocount\fam\scratchcounterone
                                                                     \Umathchar\zerocount\fam\scratchcountertwo}
\defcsname\??fractionmethods\v!vertical  \v!symbol \v!none\endcsname{\Ustretched
                                                                     \Umathchar\zerocount\fam\scratchcounterthree}
\defcsname\??fractionmethods\v!vertical  \v!symbol        \endcsname{\Ustretchedwithdelims
                                                                     \Umathchar\zerocount\fam\scratchcounterone
                                                                     \Umathchar\zerocount\fam\scratchcountertwo
                                                                     \Umathchar\zerocount\fam\scratchcounterthree}
\defcsname\??fractionmethods\v!horizontal          \v!none\endcsname{\Uskewed
                                                                     \Umathchar\zerocount\fam\scratchcounterthree
                                                                     \s!hfactor {\mathfractionparameter\c!hfactor}}
\defcsname\??fractionmethods\v!horizontal                 \endcsname{\Uskewedwithdelims
                                                                     \Umathchar\zerocount\fam\scratchcounterone
                                                                     \Umathchar\zerocount\fam\scratchcountertwo
                                                                     \Umathchar\zerocount\fam\scratchcounterthree
                                                                     \s!hfactor {\mathfractionparameter\c!hfactor}}
\defcsname\??fractionmethods\v!line                \v!none\endcsname{\Uskewed
                                                                     \Umathchar\zerocount\fam\scratchcounterthree
                                                                     \s!noaxis \relaxedspace
                                                                     \s!hfactor {\mathfractionparameter\c!hfactor}}
\defcsname\??fractionmethods\v!line                       \endcsname{\Uskewedwithdelims
                                                                     \Umathchar\zerocount\fam\scratchcounterone
                                                                     \Umathchar\zerocount\fam\scratchcountertwo
                                                                     \Umathchar\zerocount\fam\scratchcounterthree
                                                                     \s!noaxis \relaxedspace
                                                                     \s!hfactor {\mathfractionparameter\c!hfactor}}

\def\math_frac_common
  {\edef\p_method     {\mathfractionparameter\c!method}%
   \edef\p_rule       {\mathfractionparameter\c!rule}%
   \edef\p_thickness  {\mathfractionparameter\c!rulethickness}%
   \scratchcounterone  \mathfractionparameter\c!left  \relax
   \scratchcountertwo  \mathfractionparameter\c!right \relax
   \scratchcounterthree
      \ifx\p_method\v!horizontal
        \mathfractionparameter\c!middle
    % \orelse\ifx\p_method\v!mp
    %   \mathfractionparameter\c!middle
      \orelse\ifx\p_method\v!line
        \mathfractionparameter\c!middle
      \else
        \mathfractionparameter\c!symbol
      \fi
   \relax
   %
   \ifcstok{\mathfractionparameter\c!mathstyle}\s!identity
     \frozen\Umathnumeratorvariant  \allmathstyles\mathstylevariantidentity
     \frozen\Umathdenominatorvariant\allmathstyles\mathstylevariantidentity
     \resetmathfractionparameter\c!mathstyle
   \fi
   %
   \ifcstok{\mathfractionparameter\c!plugin}\v!mp
     \edef\p_mp{\mathfractionparameter\c!mp}%
     \ifempty\p_mp\else
       \clf_set_extensible_data
           mp     {\p_mp}
           left   \scratchcounterone
           right  \scratchcountertwo
           middle \scratchcounterthree
        \relax
     \fi
   \else
     \lettonothing\p_mp
   \fi
   %
   \ifcsname
      \??fractionmethods
      \p_method
      \ifx\p_method\v!horizontal
    % \orelse\ifx\p_method\v!mp
      \orelse\ifx\p_method\v!line
      \orelse\ifcsname\??fractionmethods\p_method\p_rule\endcsname % \v!vertical
        \p_rule
      \else
        \v!auto
      \fi
      \ifnum\scratchcounterone=\math_frac_no_delim
        \ifnum\scratchcountertwo=\math_frac_no_delim
          \v!none
        \fi
      \fi
   \endcsname \lastnamedcs \else \csname\??fractionmethods\v!vertical\v!auto\endcsname \fi
   \usedsymbolcolorparameterattributes{\mathfractionparameter\c!symbolcolor}%
   \s!thickness
     \ifx\p_thickness\v!font
       \zeropoint
       \s!font\relaxedspace
     \else
       {\p_thickness}%
     \fi
   \s!class   \math_class_by_parameter\mathfractionparameter\c!mathclass
   \s!vfactor {\mathfractionparameter\c!vfactor}%
   \s!source  {\namedboxanchor{\mathfractionparameter\c!source}}%
   \ifempty\p_mp\else
     \s!usecallback\relaxedspace
   \fi}

\installcorenamespace{mathalignsimple} % this should move to math-ali

\defcsname\??mathalignsimple\v!right     \endcsname#1{#1\hfill}
\defcsname\??mathalignsimple\v!flushleft \endcsname#1{#1\hfill}
\defcsname\??mathalignsimple\v!left      \endcsname#1{\hfill#1}
\defcsname\??mathalignsimple\v!flushright\endcsname#1{\hfill#1}
\defcsname\??mathalignsimple\v!middle    \endcsname#1{\hfill#1\hfill}

\protected\def\math_frac_aligned_top{\begincsname\??mathalignsimple\mathfractionparameter\c!topalign   \endcsname}
\protected\def\math_frac_aligned_bot{\begincsname\??mathalignsimple\mathfractionparameter\c!bottomalign\endcsname}

% \protected\def\math_frac_aligned_top{\begincsname\??mathalignsimple\mathfractionparameter\c!align\endcsname}
% \protected\def\math_frac_aligned_bot{\begincsname\??mathalignsimple\mathfractionparameter\c!align\endcsname}

\protected\defcsname\??mathfractionalternative\v!inner\endcsname#1#2%
  {%\beginmathgroup
   \math_frac_common
     {\usemathfractioncolorparameter\c!topcolor
      \math_fraction_margin_kern
      \usemathstyleparameter\mathfractionparameter\c!mathnumeratorstyle
      \m_fractions_strut_top\math_frac_aligned_top{#1}%
      \math_fraction_margin_kern}%
     {\usemathfractioncolorparameter\c!bottomcolor
      \math_fraction_margin_kern
      \usemathstyleparameter\mathfractionparameter\c!mathdenominatorstyle
      \m_fractions_strut_bot\math_frac_aligned_bot{#2}%
      \math_fraction_margin_kern}%
   %\endmathgroup
   \math_frac_wrapup}

\protected\defcsname\??mathfractionalternative\v!outer\endcsname#1#2%
  {\beginmathgroup
   \usemathstyleparameter\mathfractionparameter\c!mathstyle
   \math_frac_common
     {\usemathfractioncolorparameter\c!topcolor
      \math_fraction_margin_kern
      \m_fractions_strut_top\math_frac_aligned_top{#1}%
      \math_fraction_margin_kern}%
     {\usemathfractioncolorparameter\c!bottomcolor
      \math_fraction_margin_kern
      \m_fractions_strut_bot\math_frac_aligned_bot{#2}%
      \math_fraction_margin_kern}%
   \endmathgroup
   \math_frac_wrapup}

\protected\defcsname\??mathfractionalternative\v!both\endcsname#1#2%
  {\beginmathgroup
   \usemathstyleparameter\mathfractionparameter\c!mathstyle
   \math_frac_common
     {\usemathfractioncolorparameter\c!topcolor
      \math_fraction_margin_kern
      \usemathstyleparameter\mathfractionparameter\c!mathnumeratorstyle
      \m_fractions_strut_top\math_frac_aligned_top{#1}%
      \math_fraction_margin_kern}%
     {\usemathfractioncolorparameter\c!bottomcolor
      \math_fraction_margin_kern
      \usemathstyleparameter\mathfractionparameter\c!mathdenominatorstyle
      \m_fractions_strut_bot\math_frac_aligned_bot{#2}%
      \math_fraction_margin_kern}%
   \endmathgroup
   \math_frac_wrapup}

\nulldelimiterspace\zeropoint % was 1.2pt

\definemathfraction[xfrac] [\c!alternative=\v!inner,\c!mathstyle=\s!script]
\definemathfraction[xxfrac][\c!alternative=\v!inner,\c!mathstyle=\s!scriptscript]

\aliased\let\normalxfrac\xfrac

\permanent\protected\def\xfrac#1#2{\normalxfrac{\enforced\let\xfrac\xxfrac#1}{\enforced\let\xfrac\xxfrac#2}}

%D Spacing (sensitive for definition of \triggermathstyle:

\installcorenamespace{mathfractiondistance}

\defcsname\??mathfractiondistance\v!none\endcsname
  {\Umathfractionnumup    \allmathstyles\zeropoint
   \Umathfractiondenomdown\allmathstyles\zeropoint}

\defcsname\??mathfractiondistance\v!no\endcsname
  {\Umathfractionnumup    \allmathstyles\zeropoint
   \Umathfractiondenomdown\allmathstyles\zeropoint}

\defcsname\??mathfractiondistance\v!overlay\endcsname
  {\Umathfractionnumup    \allmathstyles\zeropoint
   \Umathfractionnumvgap  \allmathstyles\zeropoint
   \Umathfractiondenomvgap\allmathstyles\zeropoint
   \Umathfractiondenomdown\allmathstyles\zeropoint}

\defcsname\??mathfractiondistance\v!top\endcsname
  {\Umathfractionnumvgap  \allmathstyles{\mathfractionparameter\c!topdistance}%
   \relax}

\defcsname\??mathfractiondistance\v!bottom\endcsname
  {\Umathfractiondenomvgap\allmathstyles{\mathfractionparameter\c!bottomdistance}%
   \relax}

\defcsname\??mathfractiondistance\v!both\endcsname
  {\Umathfractionnumvgap  \allmathstyles{\mathfractionparameter\c!topdistance}%
   \Umathfractiondenomvgap\allmathstyles{\mathfractionparameter\c!bottomdistance}%
   \relax}

\setupmathfractions
  [\c!distance=\v!none,
   \c!topdistance=\zeropoint,
   \c!bottomdistance=\zeropoint]

% we need to keep it local, so not everybodyfont

\appendtoks
    \edef\p_distance{\rootmathfractionparameter\c!distance}%
    \ifempty\p_distance\else
        \begincsname\??mathfractiondistance\p_distance\endcsname
    \fi
\to \everymathematics

% theshold is new!

\let\math_fraction_set_threshold_inline \relax
\let\math_fraction_set_threshold_display\relax

\appendtoks
    \math_fraction_set_threshold_inline
    \math_fraction_set_threshold_display
\to \everymathematics

\appendtoks
    \ifempty\currentmathfraction
      \edef\p_threshold{\mathfractionparameter\c!inlinethreshold}%
      \ifempty\p_threshold
        \let\math_fraction_set_threshold_inline\math_fraction_set_threshold_inline_auto
      \orelse\ifx\p_threshold\v!auto
        \let\math_fraction_set_threshold_inline\math_fraction_set_threshold_inline_auto
      \else
        \let\math_fraction_set_threshold_inline\math_fraction_set_threshold_inline_ratio
      \fi
      \edef\p_threshold{\mathfractionparameter\c!displaythreshold}%
      \ifempty\p_threshold
        \let\math_fraction_set_threshold_display\math_fraction_set_threshold_display_auto
      \orelse\ifx\p_threshold\v!auto
        \let\math_fraction_set_threshold_display\math_fraction_set_threshold_display_auto
      \else
        \let\math_fraction_set_threshold_display\math_fraction_set_threshold_display_ratio
      \fi
    \fi
\to \everysetupmathfraction

\def\math_fraction_set_threshold_inline_auto
   {\Umathfractiondelsize\allmathstyles\maxdimen}

\def\math_fraction_set_threshold_display_auto
   {\Umathfractiondelsize\alldisplaystyles\maxdimen}

% \dimexpr#1*\plusthousand/\glyphscale\relax

\def\math_fraction_set_threshold_inline_ratio % watch the reverse compensation
  {\edef\p_threshold{\mathfractionparameter\c!inlinethreshold}%
   \Umathfractiondelsize\alltextstyles        \p_threshold\glyphscaled\textface
   \Umathfractiondelsize\allscriptstyles      \p_threshold\glyphscaled\scriptface
   \Umathfractiondelsize\allscriptscriptstyles\p_threshold\glyphscaled\scriptscriptface}

\def\math_fraction_set_threshold_display_ratio % watch the reverse compensation
  {\edef\p_threshold{\mathfractionparameter\c!displaythreshold}%
   \Umathfractiondelsize\alldisplaystyles\p_threshold\glyphscaled\textface}

\setupmathfractions
  [\c!inlinethreshold=1.2,      % no unit but fraction
   \c!displaythreshold=\v!auto, % no unit but fraction
   \c!threshold=.25\exheight]

\appendtoks
    \Umathskeweddelimitertolerance\allmathstyles\mathfractionparameter\c!threshold
\to \everymathematics % else original lost: \to \everybodyfont

%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}} $$
% }

\definemathfraction[i:frac] [\c!alternative=\v!inner,\c!mathstyle=\v!small]
\definemathfraction[i:tfrac][\c!alternative=\v!inner,\c!mathstyle=\s!script]
\definemathfraction[i:sfrac][\c!alternative=\v!inner,\c!mathstyle=\s!scriptscript]
\definemathfraction[i:dfrac][\c!alternative=\v!inner,\c!mathstyle=\s!display]

\definemathfraction[d:frac] [\c!alternative=\v!inner,\c!mathstyle=\s!cramped]
\definemathfraction[d:tfrac][\c!alternative=\v!inner,\c!mathstyle={\s!cramped,\s!script}]
\definemathfraction[d:sfrac][\c!alternative=\v!inner,\c!mathstyle={\s!cramped,\s!scriptscript}]
\definemathfraction[d:dfrac][\c!alternative=\v!both,\c!mathstyle=\s!display]

%D \protected\def\ShowMathFractions#1#2%
%D   {\dontleavehmode
%D    \begingroup
%D    \showmathstruts
%D    \mathematics{x+\tfrac{#1}{#2}+1+\frac{#1}{#2}+2+\sfrac{#1}{#2}+g}%
%D    \endgroup}
%D
%D The default \type {tfrac}, \type {frac} and \type \sfrac} look like this:
%D
%D \blank
%D \ShowMathFractions{a}{a}\par
%D \ShowMathFractions{1}{x}\par
%D \ShowMathFractions{a}{b}\par
%D \ShowMathFractions{1}{b}\par
%D \blank

%D There is no real need to support ams styling any longer so we removed the
%D related code.

\permanent\protected\def\frac {\csname\inlineordisplaymath id:frac\endcsname}
\permanent\protected\def\tfrac{\csname\inlineordisplaymath id:tfrac\endcsname}
\permanent\protected\def\sfrac{\csname\inlineordisplaymath id:sfrac\endcsname}
\permanent\protected\def\dfrac{\csname\inlineordisplaymath id:dfrac\endcsname}

\definemathfraction
  [d:binom]
  [d:frac]
  [\c!rule=\v!no,
   \c!mathmeaning=binom,
 % \c!mathstyle=\s!display,
   \c!left="28,
   \c!right="29]

\definemathfraction
  [i:binom]
  [i:frac]
  [\c!rule=\v!no,
   \c!mathmeaning=binom,
  %\c!mathstyle=\s!text,
   \c!left="28,
   \c!right="29]

\permanent\protected\def\binom{\csname\inlineordisplaymath id:binom\endcsname}

\definemathfraction
  [dbinom]
  [d:binom]
  [\c!mathstyle=\s!display]

\definemathfraction
  [tbinom]
  [i:binom]
  [\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

% % we forget about this ams one ... no one uses it anyway
%
% \permanent\protected\def\cfrac
%   {\doifelsenextoptionalcs\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}%
%     \ifzeropt\nulldelimiterspace\else
%       \kern-\nulldelimiterspace
%     \fi}}
%
% instead we predefine one:

\definemathfraction
  [cfrac]
  [\c!mathstyle=\v!text]

% so users can do this:
%
% \setupmathfraction
%   [cfrac]
%   [topalign=flushright]
%
% or define an child instance that presets it.

%D \macros
%D   {splitfrac, splitdfrac}
%D
%D Occasionally one needs to typeset multi||line fractions. These commands use \type
%D {\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

\defcsname\??mathalignsimple\v!split:\v!flushleft\endcsname#1%
  {#1%
   \ifchkdimension\mathfractionparameter\c!distance\or\hkern\lastchkdimension\fi
   \hfill}

\defcsname\??mathalignsimple\v!split:\v!flushright\endcsname#1%
  {\hfill
   \ifchkdimension\mathfractionparameter\c!distance\or\hkern\lastchkdimension\fi
   \mathatom \s!class \mathordcode \s!unroll {}% prevents +/- seen as sign, unroll prevents empty box
   #1}

\definemathfraction
  [splitfrac]
  [\c!mathnumeratorstyle=\v!text,
   \c!mathdenominatorstyle=\v!text,
   \c!topalign=\v!split:\v!flushleft,
   \c!bottomalign=\v!split:\v!flushright,
   \c!distance=\emwidth,
   \c!rule=\v!no]

\definemathfraction
  [splitdfrac]
  [splitfrac]
  [\c!mathnumeratorstyle=\v!display,
   \c!mathdenominatorstyle=\v!display]

%D More fracking:
%D
%D \starttyping
%D \m {\mathaxisbelow\vfrac             {1}{x}}
%D \m {\mathaxisbelow\vfrac[method=line]{1}{x}}
%D \stoptyping

\definemathfraction
  [\s!vfrac]
  [\c!method=\v!horizontal]

% \definemathfraction
%   [hfrac] % for ton's asciimath
%   [\c!method=\v!line]

%D And a cheat (for Mikael):

% \definemathfraction
%   [afrac]
%   [\c!mathclass=\mathrelationcode,
%    \c!rule=\v!symbol,
%    \c!middle="2192]

%D New stretched fraction with middle symnbol:

% \enableexperiments[math.arrows] % enforce construction of these in math-act
%
% {
%     tweak = "addarrows",
%     list  = { [0x3D] = { squeeze = .85, yoffset = .0975 } } -- bonum
% },

\definemathfraction
  [relfraction]
  [\c!mathclass=\mathrelationcode,
   \c!margin=.25\emwidth,
   \c!rule=\v!symbol]

\definemathfraction [bartext]                [relfraction] [\c!symbol="203E]
\definemathfraction [leftarrowtext]          [relfraction] [\c!symbol="2190]
\definemathfraction [rightarrowtext]         [relfraction] [\c!symbol="2192]
\definemathfraction [twoheadleftarrowtext]   [relfraction] [\c!symbol="219E]
\definemathfraction [twoheadrightarrowtext]  [relfraction] [\c!symbol="21A0]
\definemathfraction [reltext]                [relfraction] [\c!symbol="002D]
\definemathfraction [equaltext]              [relfraction] [\c!symbol="003D]
\definemathfraction [mapstotext]             [relfraction] [\c!symbol="21A6]
\definemathfraction [hookleftarrowtext]      [relfraction] [\c!symbol="21A9]
\definemathfraction [hookrightarrowtext]     [relfraction] [\c!symbol="21AA]
\definemathfraction [leftharpoondowntext]    [relfraction] [\c!symbol="21BD]
\definemathfraction [leftharpoonuptext]      [relfraction] [\c!symbol="21BC]
\definemathfraction [rightharpoondowntext]   [relfraction] [\c!symbol="21C1]
\definemathfraction [rightharpoonuptext]     [relfraction] [\c!symbol="21C0]
\definemathfraction [rightoverleftarrowtext] [relfraction] [\c!symbol="21C4]
\definemathfraction [leftrightharpoonstext]  [relfraction] [\c!symbol="21CB]
\definemathfraction [rightleftharpoonstext]  [relfraction] [\c!symbol="21CC]
\definemathfraction [triplereltext]          [relfraction] [\c!symbol="2261]
\definemathfraction [leftrightarrowtext]     [relfraction] [\c!symbol="27F7]
\definemathfraction [rightleftarrowtext]     [relfraction] [\c!symbol="27F7]
\definemathfraction [Leftarrowtext]          [relfraction] [\c!symbol="27F8]
\definemathfraction [Rightarrowtext]         [relfraction] [\c!symbol="27F9]
\definemathfraction [Leftrightarrowtext]     [relfraction] [\c!symbol="27FA]
\definemathfraction [Rightleftarrowtext]     [relfraction] [\c!symbol="27FA]

\protect \endinput
