%D \module
%D   [      file=t-ltmsen,
%D        version=2005.07.xx,
%D          title=\CONTEXT\ User Module,
%D       subtitle=\LaTeX\ compatibility misc. environments,
%D         author=Brooks Moses,
%D           date=\currentdate,
%D      copyright={Brooks Moses; The LaTeX3 Project}]

%D This \ConTeXt\ module is part of the \LaTeX-compatibility
%D module set.  It provides a set of miscellaneous \LaTeX
%D environments.

\usemodule[ltbase]
\usemodule[ltenv]
\unprotect

%D \section{Miscellaneous Environments}
%D
%D Taken from \type{ltmiscen.dtx}, with modifications.  Note that
%D in \LaTeX, environment commands are \type{env} and \type{\endenv},
%D whereas in the compatibility module they are \type{\startenv} and
%D \type{\stopenv}.

%D The name of the current (\LaTeX) environment.  Initialized to
%D \type{document} to so that \type{\end{document}} works correctly.

\def\@currenvir{document}

%D \LaTeX\ comments (which may or may not apply to the \ConTeXt\
%D compatibility version) follow.
%D
%D The \type{\end{document}} hook is executed first. If necessary it can
%D contain a \type{\clearpage} to output dangling floats first. In this
%D position it can also contain something like \type{\end{foo}} so that
%D the whole document effectively starts and ends with some special
%D environment. However, this must be used with care, eg if two
%D applications would use this without knowledge of each other the
%D order of the environments will be wrong after all. 
%D \type{\AtEndDocument} is redefined
%D at this point so that and such commands that get into the hook do
%D not chase their tail\ldots

\def\stopdocument{%
   \let\AtEndDocument\@firstofone
   \@enddocumenthook
   \@checkend{document}%
   \clearpage
   \begingroup
     \if@filesw
       \immediate\closeout\@mainaux
       \let\@setckpt\@gobbletwo
       \let\@newl@bel\@testdef
       \@tempswafalse 
       \makeatletter \input\jobname.aux
     \fi
     \@dofilelist
%D
%D First we check for font size substitution bigger than
%D \type{\fontsubfuzz}. The \type{\relax} is necessary because this is a
%D macro, not a register.
%D
     \ifdim \font@submax >\fontsubfuzz\relax
%D
%D In case you wonder about the \type{\@gobbletwo} inside the message
%D below, this is a horrible hack to remove the tokens \type{\on@line.}
%D that are added by \type{\@font@warning} at the end.
%D
       \@font@warning{Size substitutions with differences\MessageBreak
                  up to \font@submax\space have occurred.\@gobbletwo}%
     \fi
%D
%D The macro \type{\@defaultsubs} is initially \type{\relax} but gets redefined
%D to produce a warning if there have been some default font substitutions.
%D
     \@defaultsubs
%D
%D The macro \type{\@refundefined} is initially \type{\relax} but gets redefined
%D to produce a warning if there are undefined refs.
%D
     \@refundefined
%D
%D If a label is defined more than once, \type{\@tempswa} will always be
%D true and thus produce a ``Label(s) may \ldots'' warning. But
%D since a rerun will not solve that problem (unless one uses a
%D package like \type{varioref} that generates labels on the fly),
%D we suppress this message.
%D
     \if@filesw
       \ifx \@multiplelabels \relax
         \if@tempswa
           \@latex@warning@no@line{Label(s) may have changed.
               Rerun to get cross-references right}%
         \fi
       \else
         \@multiplelabels 
       \fi
     \fi
   \endgroup
   \deadcycles\z@%
%D
%D [Here, after all this (which will probably need much work to actually fit
%D properly with \ConTeXt!), we call \ConTeXt's \type{\stoptext}, so that 
%D \ConTeXt\ can get its end-of-document functions in.]
%D
   \stoptext}

%D ### ToDo: convert all of this to something ConTeXt-friendly!

\def\@testdef #1#2#3{%
  \def\reserved@a{#3}\expandafter \ifx \csname #1@#2\endcsname
 \reserved@a  \else \@tempswatrue \fi}

\long\def\@writefile#1#2{%
  \@ifundefined{tf@#1}\relax
    {\@temptokena{#2}%
     \immediate\write\csname tf@#1\endcsname{\the\@temptokena}%
    }%
}

%D [We have a name clash with \type{\stop}.]

\def\latexstop{\clearpage\deadcycles\z@\let\par\@@par\@@end}

%D [We don't actually want these errors, so this is commented out.]

%\everypar{\@nodocument} %% To get an error if text appears before the
%\nullfont               %% \begin{document}



%D We do need a default value for \type{\@currenvline} on top-level since
%D the document environment cancels the brace group. This means that
%D a missmatch with \type{\begindocument} will not produce
%D a line number. Thus the outer default must be \type{\@empty} or we
%D will end up with two spaces.

\let\@currenvline\@empty

%D \subsection{Center, Flushright, Flushleft}

\def\@centercr{\ifhmode \unskip\else \@nolnerr\fi
       \par\@ifstar{\nobreak\@xcentercr}\@xcentercr}

\def\@xcentercr{\addvspace{-\parskip}\@ifnextchar
    [\@icentercr\ignorespaces}

\def\@icentercr[#1]{\vskip #1\ignorespaces}

%D We use \type{\relax} to prevent \type{\item} scanning too far.

\def\startcenter{\trivlist \latexcentering\item\relax}

\def\stiocenter{\endtrivlist}

%D [There is a name conflict with \ConTeXt's \type{\centering}, so
%D as usual we replace this with \type{\latexcentering}.  Similarly
%D with \type{\raggedright} and \type{\flushright}.]

\def\latexcentering{%
  \let\\\@centercr
  \rightskip\@flushglue\leftskip\@flushglue
  \parindent\z@\parfillskip\z@skip}

\newskip\@rightskip \@rightskip \z@skip

%D We use \type{\relax} to prevent \type{\item} scanning too far.

\def\startflushleft{\trivlist \latexraggedright\item\relax}

\def\stopflushleft{\endtrivlist}

\def\latexraggedright{%
  \let\\\@centercr\@rightskip\@flushglue \rightskip\@rightskip
  \leftskip\z@skip
  \parindent\z@}

\def\startflushright{\trivlist \latexraggedleft\item\relax}

\def\endflushright{\endtrivlist}

\def\latexraggedleft{%
  \let\\\@centercr
  \rightskip\z@skip\leftskip\@flushglue
  \parindent\z@\parfillskip\z@skip}


%D \subsection{Verbatim}

{\catcode`\ =\active%
\gdef\@vobeyspaces{\catcode`\ \active\let \@xobeysp}}

\begingroup \catcode `|=0 \catcode `[= 1
\catcode`]=2 \catcode `\{=12 \catcode `\}=12
\catcode`\\=12 |gdef|@xverbatim#1\end{verbatim}[#1|end[verbatim]]
|gdef|@sxverbatim#1\end{verbatim*}[#1|end[verbatim*]]
|endgroup

\def\@verbatim{\trivlist \item\relax 
  \if@minipage\else\vskip\parskip\fi
  \leftskip\@totalleftmargin\rightskip\z@skip
  \parindent\z@\parfillskip\@flushglue\parskip\z@skip
  \@@par
  \@tempswafalse 
  \def\par{%
    \if@tempswa
      \leavevmode \null \@@par\penalty\interlinepenalty
    \else
      \@tempswatrue
      \ifhmode\@@par\penalty\interlinepenalty\fi
    \fi}%
%D
%D To allow customization we hide the font used in a separate macro.
%D
  \let\do\@makeother \dospecials
  \obeylines \verbatim@font \@noligs
  \hyphenchar\font\m@ne
%D
%D To avoid a breakpoint after the labels box, we remove the penalty
%D put there by the list macros: another use of \type{\unpenalty}!
%D
  \everypar \expandafter{\the\everypar \unpenalty}%
}

\def\startverbatim{\@verbatim \frenchspacing\@vobeyspaces \@xverbatim}
\def\stopverbatim{\if@newlist \leavevmode\fi\endtrivlist}

%D This has been changed to a \ConTeXt-specific font command.
%D
%D ### ToDo: do this right!

%\def\verbatim@font{\normalfont\ttfamily}
\def\verbatim@font{\tt}

\@namedef{startverbatim*}{\@verbatim\@sxverbatim}
\expandafter\let\csname stopverbatim*\endcsname =\stopverbatim

\def\@makeother#1{\catcode`#112\relax}

\let\verb@balance@group\@empty

\def\verb@egroup{\global\let\verb@balance@group\@empty\egroup}

\begingroup
  \obeylines%
  \gdef\verb@eol@error{\obeylines%
    \def^^M{\verb@egroup\@latex@error{%
            \noexpand\verb ended by end of line}\@ehc}}%
\endgroup

\def\verb{\relax\ifmmode\hbox\else\leavevmode\null\fi
  \bgroup
    \verb@eol@error \let\do\@makeother \dospecials
    \verbatim@font\@noligs
    \@ifstar\@sverb\@verb}

\def\@sverb#1{%
  \catcode`#1\active
  \lccode`\~`#1%
  \gdef\verb@balance@group{\verb@egroup
     \@latex@error{\noexpand\verb illegal in command argument}\@ehc}%
  \aftergroup\verb@balance@group
  \lowercase{\let~\verb@egroup}}%

\def\@verb{\@vobeyspaces \frenchspacing \@sverb}

\def\verbatim@nolig@list{\do\`\do\<\do\>\do\,\do\'\do\-}

\def\do@noligs#1{%
  \catcode`#1\active
  \begingroup
     \lccode`\~`#1\relax
     \lowercase{\endgroup\def~{\leavevmode\kern\z@\char`#1}}}
     
%D To stay compatible with packages that use \type{\@noligs} we keep it.

\def\@noligs{\let\do\do@noligs \verbatim@nolig@list}


\protect
