%D \module
%D   [      file=t-amsl,
%D        version=2003.04.11,
%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

\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}\doeqalign}
\def\doeqalign#1{%
  \startformula
  \let\\\cr
  \eqalign{#1\cr}
  \stopformula}
\let\stopalign\relax

\def\startgather{\grabuntil{stopgather}\dogather}
\def\dogather#1{%
    \startformula
    \def\\{\cr&}%
    \eqalign{&#1\cr}
    \stopformula}
\let\stopgather\relax

%D Blocks used in formulas

%D LaTeX "array"

\unprotect

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

\newcount\amp@count

\arraycolsep1ex

\let\@arraycr\crcr

\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\scanarraypreamble{%
  \let\array!l=l
  \let\array!r=r
  \let\array!c=c
  \afterassignment\doscanarraypreamble\let\next=%
}
\def\arrayaddleftaligned{%
  \appendtoks&$##$\hss\to\arraypreamble
}
\def\arrayaddrightaligned{%
  \appendtoks&\hss$##$\to\arraypreamble
}
\def\arrayaddcentered{%
  \appendtoks&\hss$##$\hss\to\arraypreamble
}
\def\doscanarraypreamble{%
  \ifx\next\array!l
    \def\!!next{%
      \arrayaddleftaligned
      \advance\amp@count1
      \afterassignment\doscanarraypreamble\let\next=%
    }
  \else
    \ifx\next\array!r
      \def\!!next{%
        \arrayaddrightaligned
        \advance\amp@count1
        \afterassignment\doscanarraypreamble\let\next=%
      }
    \else
      \ifx\next\array!c
        \def\!!next{%
          \arrayaddcentered
          \appendtoks&\hss$####$\hss\to\arraypreamble
          \advance\amp@count1
          \afterassignment\doscanarraypreamble\let\next=%
        }
      \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
            \afterassignment\doscanarraypreamble\let\next=%
          }
        \fi
      \fi
    \fi
  \fi
  \!!next
}

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

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

\protect

\def\startcases{%
  \left\{\startarray{ll}
}
\def\stopcases{%
  \stoparray\right.
}






%D Font Stuff
%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}}

\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=)]

