%D \module
%D   [       file=math-tex,
%D        version=2001.04.12,
%D          title=\CONTEXT\ Math Macros,
%D       subtitle=Definitions,
%D         author={Hans Hagen, Taco Hoekwater \& Aditya Mahajan},
%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 / Definitions}

\unprotect

% this will be done at the lua end

\startluacode
    mathematics.define(\number\defaultmathfamily)
 -- mathematics.xml.registerentities()
\stopluacode

\activatemathcharacters

% will go to math-ext (if used at all)

\Umathchardef\braceld=0 \defaultmathfamily "FF07A
\Umathchardef\bracerd=0 \defaultmathfamily "FF07B
\Umathchardef\bracelu=0 \defaultmathfamily "FF07C
\Umathchardef\braceru=0 \defaultmathfamily "FF07D

% ctx specific

% \bgroup
% \catcode`|=\othercatcode
% \global\let\|=|
% \egroup

% The \mfunction macro is an alternative for \hbox with a
% controlable font switch.

\definemathcommand [arccos]  [nolop] {\mfunctionlabeltext{arccos}}
\definemathcommand [arcsin]  [nolop] {\mfunctionlabeltext{arcsin}}
\definemathcommand [arctan]  [nolop] {\mfunctionlabeltext{arctan}}
\definemathcommand [arg]     [nolop] {\mfunctionlabeltext{arg}}
\definemathcommand [cosh]    [nolop] {\mfunctionlabeltext{cosh}}
\definemathcommand [cos]     [nolop] {\mfunctionlabeltext{cos}}
\definemathcommand [coth]    [nolop] {\mfunctionlabeltext{coth}}
\definemathcommand [cot]     [nolop] {\mfunctionlabeltext{cot}}
\definemathcommand [csc]     [nolop] {\mfunctionlabeltext{csc}}
\definemathcommand [deg]     [nolop] {\mfunctionlabeltext{deg}}
\definemathcommand [det]     [limop] {\mfunctionlabeltext{det}}
\definemathcommand [dim]     [nolop] {\mfunctionlabeltext{dim}}
\definemathcommand [exp]     [nolop] {\mfunctionlabeltext{exp}}
\definemathcommand [gcd]     [limop] {\mfunctionlabeltext{gcd}}
\definemathcommand [hom]     [nolop] {\mfunctionlabeltext{hom}}
\definemathcommand [inf]     [limop] {\mfunctionlabeltext{inf}}
\definemathcommand [injlim]  [limop] {\mfunctionlabeltext{injlim}}
\definemathcommand [ker]     [nolop] {\mfunctionlabeltext{ker}}
\definemathcommand [lg]      [nolop] {\mfunctionlabeltext{lg}}
\definemathcommand [liminf]  [limop] {\mfunctionlabeltext{liminf}}
\definemathcommand [limsup]  [limop] {\mfunctionlabeltext{limsup}}
\definemathcommand [lim]     [limop] {\mfunctionlabeltext{lim}}
\definemathcommand [ln]      [nolop] {\mfunctionlabeltext{ln}}
\definemathcommand [log]     [nolop] {\mfunctionlabeltext{log}}
\definemathcommand [median]  [limop] {\mfunctionlabeltext{median}}
\definemathcommand [max]     [limop] {\mfunctionlabeltext{max}}
\definemathcommand [min]     [limop] {\mfunctionlabeltext{min}}
\definemathcommand [mod]     [limop] {\mfunctionlabeltext{mod}}
%definemathcommand [div]     [limop] {\mfunctionlabeltext{div}}                              % overloads \div symbol
\definemathcommand [projlim] [limop] {\mfunctionlabeltext{projlim}}
\definemathcommand [Pr]      [limop] {\mfunctionlabeltext{Pr}}
\definemathcommand [sec]     [nolop] {\mfunctionlabeltext{sec}}
\definemathcommand [sinh]    [nolop] {\mfunctionlabeltext{sinh}}
\definemathcommand [sin]     [nolop] {\mfunctionlabeltext{sin}}
\definemathcommand [sup]     [limop] {\mfunctionlabeltext{sup}}
\definemathcommand [tanh]    [nolop] {\mfunctionlabeltext{tanh}}
\definemathcommand [tan]     [nolop] {\mfunctionlabeltext{tan}}

% \definemathcommand [integers]      {{\mathblackboard Z}}
% \definemathcommand [reals]         {{\mathblackboard R}}
% \definemathcommand [rationals]     {{\mathblackboard Q}}
% \definemathcommand [naturalnumbers]{{\mathblackboard N}}
% \definemathcommand [complexes]     {{\mathblackboard C}}
% \definemathcommand [primes]        {{\mathblackboard P}}

\let\normalmatharg\arg % todo: maybe automatically

% using attributes

\setnewconstant\bigmathdelimitermethod\plusone

\def\plainbigdelimiters % traditional method
  {\bigmathdelimitermethod\plustwo}

\plainbigdelimiters % is default for the moment but not so nice

\def\doplainbigmath#1#2%
  {{\hbox{$%
      \nulldelimiterspace\zeropoint\relax
      \mathsurround\zeropoint
  $}}}

\def\doleftbigmath #1{\ifx#1\relax\else\left#1\expandafter\doleftbigmath \fi}
\def\dorightbigmath#1{\ifx#1\relax\else\right.\expandafter\dorightbigmath\fi}

\installcorenamespace{mathbig}

\unexpanded\def\choosemathbig#1#2% so we accent \big{||} as well
  {{\hbox{$%
      \ifcase\bigmathdelimitermethod
        \doleftbigmath#2\relax
        \dorightbigmath#2\relax
      \or
        \attribute\mathsizeattribute#1\relax
        \doleftbigmath#2\relax
        \dorightbigmath#2\relax
      \else
        \doleftbigmath#2\relax
        \vbox to\getvalue{\??mathbig\number#1}\bodyfontsize{}%
        \dorightbigmath#2\relax
      \fi
      \nulldelimiterspace\zeropoint\relax
      \mathsurround\zeropoint
  $}}}

\definemathcommand [big]  {\choosemathbig\plusone  } \setvalue{\??mathbig1}{0.85}
\definemathcommand [Big]  {\choosemathbig\plustwo  } \setvalue{\??mathbig2}{1.15}
\definemathcommand [bigg] {\choosemathbig\plusthree} \setvalue{\??mathbig3}{1.45}
\definemathcommand [Bigg] {\choosemathbig\plusfour } \setvalue{\??mathbig4}{1.75}

\definemathcommand [bigl]  [open]  [one] {\big}
\definemathcommand [bigm]  [rel]   [one] {\big}
\definemathcommand [bigr]  [close] [one] {\big}
\definemathcommand [Bigl]  [open]  [one] {\Big}
\definemathcommand [Bigm]  [rel]   [one] {\Big}
\definemathcommand [Bigr]  [close] [one] {\Big}
\definemathcommand [biggl] [open]  [one] {\bigg}
\definemathcommand [biggm] [rel]   [one] {\bigg}
\definemathcommand [biggr] [close] [one] {\bigg}
\definemathcommand [Biggl] [open]  [one] {\Bigg}
\definemathcommand [Biggm] [rel]   [one] {\Bigg}
\definemathcommand [Biggr] [close] [one] {\Bigg}

%D This needs checking:

\def\setoperatorlimits#1#2% operator limits
  {\savenormalmeaning{#1}%
   \def#1{\csname normal\strippedcsname#1\endcsname#2}}

\setoperatorlimits\int              \intlimits
\setoperatorlimits\iint             \intlimits
\setoperatorlimits\iiint            \intlimits
\setoperatorlimits\oint             \intlimits
\setoperatorlimits\oiint            \intlimits
\setoperatorlimits\oiiint           \intlimits
\setoperatorlimits\intclockwise     \intlimits
\setoperatorlimits\ointclockwise    \intlimits
\setoperatorlimits\ointctrclockwise \intlimits

%D This is a temporary hack until we figure out how to do this correctly,
%D preferably using math parameters but we can also consider doing some
%D node juggling here.

\unexpanded\def\implies  {\mathrel{\;\Longrightarrow\;}}
\unexpanded\def\impliedby{\mathrel{\;\Longleftarrow\;}}
\unexpanded\def\And      {\mathrel{\;\internalAnd\;}}
%unexpanded\def\iff      {\;\Longleftrightarrow\;}
\setuvalue          {iff}{\;\Longleftrightarrow\;} % nicer for if checker

% todo: virtual in math-vfu

% \definemathcommand [mapsto]         {\mapstochar\rightarrow}
% \definemathcommand [hookrightarrow] {\lhook\joinrel\rightarrow}
% \definemathcommand [hookleftarrow]  {\leftarrow\joinrel\rhook}
% \definemathcommand [bowtie]         {\mathrel\triangleright\joinrel\mathrel\triangleleft}
% \definemathcommand [models]         {\mathrel|\joinrel=}
% \definemathcommand [iff]            {\;\Longleftrightarrow\;}

% hm

% ldots = 2026
% vdots = 22EE
% cdots = 22EF
% ddots = 22F1
% udots = 22F0

% \def\PLAINldots{\ldotp\ldotp\ldotp}
% \def\PLAINcdots{\cdotp\cdotp\cdotp}

% \def\PLAINvdots
%   {\vbox{\baselineskip.4\bodyfontsize\lineskiplimit\zeropoint\kern.6\bodyfontsize\hbox{.}\hbox{.}\hbox{.}}}

% \def\PLAINddots
%   {\mkern1mu%
%    \raise.7\bodyfontsize\vbox{\kern.7\bodyfontsize\hbox{.}}%
%    \mkern2mu%
%    \raise.4\bodyfontsize\relax\hbox{.}%
%    \mkern2mu%
%    \raise.1\bodyfontsize\hbox{.}%
%    \mkern1mu}

% \definemathcommand [ldots] [inner]   {\PLAINldots}
% \definemathcommand [cdots] [inner]   {\PLAINcdots}
% \definemathcommand [vdots] [nothing] {\PLAINvdots}
% \definemathcommand [ddots] [inner]   {\PLAINddots}

%D \starttyping
%D $\sqrt[3]{10}$
%D \stoptyping

\def\rootradical{\Uroot \defaultmathfamily "221A } % can be done in char-def

\def\root#1\of{\rootradical{#1}} % #2

\unexpanded\def\sqrt{\doifnextoptionalelse\rootwithdegree\rootwithoutdegree}

\def\rootwithdegree   [#1]{\rootradical{#1}}
\def\rootwithoutdegree    {\rootradical  {}}

\definemathcommand [mathstrut] {\vphantom{(}}
\definemathcommand [joinrel]   {\mathrel{\mkern-3mu}}

\unexpanded\def\{{\mathortext\lbrace\letterleftbrace } % or maybe a chardef
\unexpanded\def\}{\mathortext\rbrace\letterrightbrace} % or maybe a chardef
\unexpanded\def\|{\mathortext\vert  \letterbar       } % or maybe a chardef

%D The following colon related definitions are provided by Aditya
%D Mahajan who derived them from \type {mathtools.sty} and \type
%D {colonequals.sty}.

%D \macros
%D   {centercolon, colonminus, minuscolon, colonequals, equalscolon,
%D    colonapprox, approxcolon, colonsim, simcolon, coloncolon,
%D    coloncolonminus, minuscoloncolon, coloncolonequals,
%D    equalscoloncolon, coloncolonapprox, approxcoloncolon,
%D    colonsim, simcoloncolon}
%D
%D In $a := b$ the colon is not vertically centered with the equal
%D to. Also the distance between colon and equal to is a bit large.
%D So, we define a vertically centered colon \tex {centercolon} and
%D a few macros for colon and double colon relation symbols.
%D
%D \startlines
%D \formula {A \centercolon       B}
%D \formula {A \colonminus        B}
%D \formula {A \minuscolon        B}
%D \formula {A \colonequals       B}
%D \formula {A \equalscolon       B}
%D \formula {A \colonapprox       B}
%D \formula {A \approxcolon       B}
%D \formula {A \colonsim          B}
%D \formula {A \simcolon          B}
%D \formula {A \coloncolon        B}
%D \formula {A \coloncolonminus   B}
%D \formula {A \minuscoloncolon   B}
%D \formula {A \coloncolonequals  B}
%D \formula {A \equalscoloncolon  B}
%D \formula {A \coloncolonapprox  B}
%D \formula {A \approxcoloncolon  B}
%D \formula {A \colonsim          B}
%D \formula {A \simcoloncolon     B}
%D \stoplines

%D The next macros take care of the space between the colon and the
%D relation symbol.

\definemathcommand  [colonsep]        {\mkern-1.2mu}
\definemathcommand  [doublecolonsep]  {\mkern-0.9mu}

%D Now we define all the colon relations .. needs checking with char-def.lua:

\definemathcommand [centercolon]      [rel] {\mathstylevcenteredhbox\colon}
\definemathcommand [colonminus]       [rel] {\centercolon\colonsep\mathrel{-}}
\definemathcommand [minuscolon]       [rel] {\mathrel{-}\colonsep\centercolon}
\definemathcommand [colonequals]      [rel] {\centercolon\colonsep=}
\definemathcommand [equalscolon]      [rel] {=\centercolon\colonsep}
\definemathcommand [colonapprox]      [rel] {\centercolon\colonsep\approx}
\definemathcommand [approxcolon]      [rel] {\approx\centercolon\colonsep}
\definemathcommand [colonsim]         [rel] {\centercolon\colonsep\sim}
\definemathcommand [simcolon]         [rel] {\sim\centercolon\colonsep}

\definemathcommand [coloncolon]       [rel] {\centercolon\doublecolonsep\centercolon}
\definemathcommand [coloncolonminus]  [rel] {\coloncolon\colonsep\mathrel{-}}
\definemathcommand [minuscoloncolon]  [rel] {\mathrel{-}\colonsep\coloncolon}
\definemathcommand [coloncolonequals] [rel] {\coloncolon\colonsep=}
\definemathcommand [equalscoloncolon] [rel] {=\coloncolon\colonsep}
\definemathcommand [coloncolonapprox] [rel] {\coloncolon\colonsep\approx}
\definemathcommand [approxcoloncolon] [rel] {\approx\coloncolon\colonsep}
\definemathcommand [colonsim]         [rel] {\coloncolon\colonsep\sim}
\definemathcommand [simcoloncolon]    [rel] {\sim\coloncolon\colonsep}

%D Goodies. We might move this elsewhere.

% Be careful in choosing what accents you take (the code below uses a
% combining one):
%
% \startbuffer
% % $\Umathaccent top        0 0 "20D7 {example}$
% % $\Umathaccent top  fixed 0 0 "20D7 {example}$
% $\Umathaccent              0 0 "20D7 {example}$
% $\Umathaccent        fixed 0 0 "20D7 {example}$
% $\Umathaccent bottom       0 0 "20D7 {example}$
% $\Umathaccent bottom fixed 0 0 "20D7 {example}$
% $\Umathaccent both         0 0 "20D7
%                            0 0 "20D7 {example}$
% $\Umathaccent both fixed   0 0 "20D7
%                    fixed   0 0 "20D7 {example}$
% $\Umathaccent both         0 0 "20D7
%                    fixed   0 0 "20D7 {example}$
% $\Umathaccent both fixed   0 0 "20D7
%                            0 0 "20D7 {example}$
% \stopbuffer
%
% \setupbodyfont[modern]  \getbuffer
% \setupbodyfont[xits]    \getbuffer
% \setupbodyfont[cambria] \getbuffer

\unexpanded\def\underleftarrow #1{\mathop{\Uunderdelimiter \defaultmathfamily "2190 {#1}}}
\unexpanded\def\overleftarrow  #1{\mathop{\Uoverdelimiter  \defaultmathfamily "2190 {#1}}}
\unexpanded\def\underrightarrow#1{\mathop{\Uunderdelimiter \defaultmathfamily "2192 {#1}}}
\unexpanded\def\overrightarrow #1{\mathop{\Uoverdelimiter  \defaultmathfamily "2192 {#1}}}

% watch out: here we have a class (zero):

\unexpanded\def\normaldoublebrace {\Umathaccents 0 \defaultmathfamily "23DE 0 \defaultmathfamily "23DF }
\unexpanded\def\normaldoubleparent{\Umathaccents 0 \defaultmathfamily "23DC 0 \defaultmathfamily "23DD }

% let's keep this

\def\Umathbotaccent{\Umathaccent \s!bottom }
\def\Umathaccents  {\Umathaccent \s!both   }

\let\normaloverbrace      \overbrace
\let\normalunderbrace     \underbrace
\let\normaloverparent     \overparent
\let\normalunderparent    \underparent
\let\normaloverbracket    \overbracket
\let\normalunderbracket   \underbracket
\let\normalunderleftarrow \underleftarrow
\let\normaloverleftarrow  \overleftarrow
\let\normalunderrightarrow\underrightarrow
\let\normaloverrightarrow \overrightarrow

\unexpanded\def\mathopwithlimits#1#2{\mathop{#1{#2}}\limits}
\unexpanded\def\stackrel        #1#2{\mathrel{\mathop{#2}\limits^{#1}}}

\unexpanded\def\overbrace      {\mathopwithlimits\normaloverbrace      }
\unexpanded\def\underbrace     {\mathopwithlimits\normalunderbrace     }
\unexpanded\def\doublebrace    {\mathopwithlimits\normaldoublebrace    }
\unexpanded\def\overparent     {\mathopwithlimits\normaloverparent     }
\unexpanded\def\underparent    {\mathopwithlimits\normalunderparent    }
\unexpanded\def\overbracket    {\mathopwithlimits\normaloverbracket    }
\unexpanded\def\underbracket   {\mathopwithlimits\normalunderbracket   }
\unexpanded\def\doubleparent   {\mathopwithlimits\normaldoubleparent   }
\unexpanded\def\underleftarrow {\mathopwithlimits\normalunderleftarrow }
\unexpanded\def\overleftarrow  {\mathopwithlimits\normaloverleftarrow  }
\unexpanded\def\underrightarrow{\mathopwithlimits\normalunderrightarrow}
\unexpanded\def\overrightarrow {\mathopwithlimits\normaloverrightarrow }

\let\lceil \lceiling
\let\rceil \rceiling

\let\normalsurd\surd

\unexpanded\def\surd{\normalsurd{}}

% Some special characters:

\unexpanded\def\nabla{∇} % this one adapts

%
% todo mathclass=punctuation ord

% \Umathcode"02C="6 \defaultmathfamily "02C
% \Umathcode"02E="0 \defaultmathfamily "02E

% tricky .. todo

% \appendtoks
%     \def\over{\primitive\over}%
% \to \everymathematics

\protect \endinput
