%D \module
%D   [      file=t-amsl,
%D        version=2004.11.18,
%D          title=\CONTEXT\ User Module,
%D       subtitle=AMS-LaTeX compatibility,
%D         author=Giuseppe Bilotta,
%D           date=\currentdate,
%D      copyright={PRAGMA / Giuseppe Bilotta}]

%D This ConTeXt module is intended to provide AMS-LaTeX-like
%D functionality to ConTeXt

% \doifdefined{\amslatexmath}{\message{\amslatexmath}\endinput}
% \def\amslatexmath{AMS-LaTeX compatibility module already loaded}

\usemodule[newmat]

\def\stackrel#1#2{\mathrel{\mathop{#2}\limits^{#1}}}
\def\underset#1#2{\mathrel{\mathop{#2}\limits_{#1}}}

%D Formulas

% \setupformulas[indentnext=no]
% The following requires a patch to work correctly:

\setupformulas[indentnext=auto]

\let\[\startformula
\def\]{\stopformula\ignorespaces}

\def\(#1\){\mathematics{#1}}

\def\startalign{\grabuntil{stopalign}\dostarteqalign}

\def\dostarteqalign#1%
  {\startformula
   \let\\\cr
   \eqalign{#1\crcr}
   \stopformula}
   
\let\stopalign\relax

\def\startgather{\grabuntil{stopgather}\dostartgather}

\def\dostartgather#1%
  {\startformula
   \def\\{\cr&}%
   \eqalign{&#1\crcr}
   \stopformula}

\let\stopgather\relax

%D Blocks used in formulas

%D LaTeX "array"

\unprotect

\newdimen\arraycolsep
\newdimen\prearraycolsep
\newdimen\postarraycolsep

\newcount\amp@count

\arraycolsep1ex % todo: hook into everymath 

\newdimen\arrayrowsep
\arrayrowsep1ex % todo: hook into everymath

% \let\@arraycr\crcr

\def\@arraycr{\crcr\noalign{\hrule height\arrayrowsep width 0pt}}

\newtoks\arraypreamble

\def\startarray#1%
  {\begingroup% needed? erroneous?
   % TODO scan array preamble
   % preamble is assumed to be a sequence of
   % l,r,c which determine whether the specific column should be
   % left-aligned, right-aligned or centered
   \arraypreamble{\tabskip\arraycolsep}%
   \amp@count0
   \let\arrayboxcommand\vcenter
   \scanarraypreamble#1\relax}

\def\scanpreamblenext
  {\afterassignment\doscanarraypreamble\let\next=}
\def\scanarraypreamble
  {\let\array!l=l
   \let\array!r=r
   \let\array!c=c
   \let\array!d=d
   \let\array!t=t
   \scanpreamblenext}

\def\arrayaddleftaligned {\appendtoks&$##$\hss\to\arraypreamble}
\def\arrayaddrightaligned{\appendtoks&\hss$##$\to\arraypreamble}
\def\arrayaddcentered    {\appendtoks&\hss$##$\hss\to\arraypreamble}
\def\arrayaddtextual     {\appendtoks&$\text{##}$\hss\to\arraypreamble}
\def\arrayadddisplayed   {\appendtoks&$\displaystyle##$\hss\to\arraypreamble}

\def\doscanarraypreamble
  {\ifx\next\array!l
     \def\!!next
       {\arrayaddleftaligned
        \advance\amp@count1
        \scanpreamblenext}%
   \else\ifx\next\array!r
     \def\!!next
       {\arrayaddrightaligned
        \advance\amp@count1
        \scanpreamblenext}%
    \else\ifx\next\array!c
      \def\!!next
        {\arrayaddcentered
         \advance\amp@count1
         \scanpreamblenext}%
    \else\ifx\next\array!t
      \def\!!next
        {\arrayaddtextual
         \advance\amp@count1
         \scanpreamblenext}%
    \else\ifx\next\array!d
      \def\!!next
        {\arrayadddisplayed
         \advance\amp@count1
         \scanpreamblenext}%
    \else\if\next\relax
      \def\!!next
        {\appendtoks\tabskip\postarraycolsep\cr\to\arraypreamble
         \dostartarray}%
     \else
       % TODO error
       \message{\meaning\next\space unexpected in preamble; replacing with 'c'}
       \def\!!next  
         {\arrayaddcentered
          \advance\amp@count1
          \scanpreamblenext}%
      \fi \fi \fi \fi \fi \fi
  \!!next}

\def\dostartarray
  {\arrayboxcommand\bgroup % TODO or vcenter or vtop
   \let\\\@arraycr
   % \showthe\arraypreamble
   \tabskip\prearraycolsep
   \expandafter\halign\expandafter\bgroup\the\arraypreamble}

\def\stoparray
  {\crcr\egroup
   \egroup
   \endgroup}

\def\startcases
  {\left\{\startarray{ll}}

\def\stopcases
  {\stoparray\right.}

\def\startbigcases
  {\left\{\startarray{dl}}

\def\stopbigcases
  {\stoparray\right.}

\def\cases#1
  {\left\{\startarray{lt}#1\crcr\stoparray\right.}

%D Font Stuff
%D
%D First of all, we need Fraktur to be loaded correctly, something
%D which ConTeXt doesn't do by default (at least by now)
%D Can't find a better, faster, cleaner way to do it;
%D it should probably be added to ConTeXt default for AMS, anyway

\definebodyfont [12pt,11pt,10pt,9pt] [mm] [mc=eufm10 sa 1]
\definebodyfont [8pt,7pt,6pt]        [mm] [mc=eufm7 sa 1]
\definebodyfont [5pt]                [mm] [mc=eufm5]

%D to make sure they get loaded, we make a font change
%D we restore rm afterwards, not having a way to decide which one
%D was active (anyway, rm is a pretty safe bet when loading
%D a module \dots)

\switchtobodyfont[mm]
\switchtobodyfont[rm]

%D Next we restore LaTeX-like behaviour for \cal, \Bbd, \frak

\definefamilysynonym[default][calligraphic][sy]
\definefamilysynonym[default][blackboard]  [mb]

%D The next one requires eufm to be loaded properly, as above

\definefamilysynonym[default][fraktur][mc]

\def\!mathcal {\fam\purefamily{calligraphic}}
\def\!mathBbd {\fam\purefamily{blackboard}}
\def\!mathfrak{\fam\purefamily{fraktur}}

\def\mathcal #1{{\!mathcal #1}}
\def\mathbb  #1{{\!mathBbd #1}}
\def\mathfrak#1{{\!mathfrak#1}}

\appendtoks
  \let\cal \!mathcal
  \let\Bbd \!mathBbd
  \let\frak\!mathfrak
\to\everymath

\appendtoks
  \let\cal \!mathcal
  \let\Bbd \!mathBbd
  \let\frak\!mathfrak
\to\everydisplay

\enablemathcollection[ams] \usemathcollection[ams]

%D Reference formats

\definereferenceformat[eqref][left=(,right=)]

\protect \endinput 

