%D \module
%D   [      file=t-lterr,
%D        version=2005.07.xx,
%D          title=\CONTEXT\ User Module,
%D       subtitle=\LaTeX\ compatibility error handling,
%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 the \LaTeX\ error-handling routines.

\usemodule[ltbase]
\usemodule[ltcmds]
\unprotect

%D \section{\LaTeX\ Files}
%D
%D From \type{ltfiles.dtx}.
%D
%D ### If we need more of this, we may want to move it to its own file.

% Also used in ltclass, along with ltdefns and lterror.

\newwrite\@unused



%D \section{\LaTeX\ Error Commands}
%D
%D From \type{lterror.dtx}.  This may need to be edited later to bring
%D it into proper conformance with \ConTeXt, but a straight copy should
%D work for now.
%D
%D ### Check on this!
%D
%D ### Also, a number of the specific error messages will be unnecessary
%D due to only being called in parts of \LaTeX\ that are being handled
%D by \ConTeXt\ commands in this version.

%D \subsection{General commands}
%D
%D This command prints a new-line inside a message, followed by a 
%D continuation line begun with \type{\@msg@continuation}.  Normally it is 
%D defined to be \type{\relax}, but inside messages, it is let to 
%D \type{\@message@break}.

\let\MessageBreak\relax

%D This takes two arguments: a continuation and a message, and sends 
%D the result to the log file.

\DeclareRobustCommand{\GenericInfo}[2]{%
   \begingroup
      \def\MessageBreak{^^J#1}%
      \set@display@protect
      \immediate\write\m@ne{#2\on@line.}%
   \endgroup
}

%D This takes two arguments: a continuation and a message, and sends 
%D the result to the screen.

\DeclareRobustCommand{\GenericWarning}[2]{%
   \begingroup
      \def\MessageBreak{^^J#1}%
      \set@display@protect
      \immediate\write\@unused{^^J#2\on@line.^^J}%
   \endgroup
}

%D This macro takes four arguments: a continuation,
%D an error message, where to go for further information, and the help
%D information.  It displays the error message, and sets the error help
%D (the result of typing \type{h} to the prompt), and does a horrible hack
%D to turn the last context line (which by default is the only context
%D line) into just three dots.  This could be made more efficient.
%D
%D [Note that the ``if'' option to take care of older \TeX\ versions has
%D been removed as unneeded.]

\bgroup
\lccode`\@=`\ %
\lccode`\~=`\ %
\lccode`\}=`\ %
\lccode`\{=`\ %
\lccode`\T=`\T%
\lccode`\H=`\H%
\catcode`\ =11\relax%
\lowercase{%
\egroup%
\DeclareRobustCommand{\GenericError}[4]{%
\begingroup%
\immediate\write\@unused{}%
\def\MessageBreak{^^J}%
\set@display@protect%
\edef%
%    %<-------------------do not delete this space!------------------->%
\@err@                                                                 %
{{#4}}%
\errhelp
%    %<-------------------do not delete this space!------------------->%
\@err@                                                                 %
\let
%    %<-------------------do not delete this space!------------------->%
\@err@                                                                 %
\@empty
\def\MessageBreak{^^J#1}%
\def~{\errmessage{%
#2.^^J^^J%
#3^^J%
Type  H <return>  for immediate help%
%    %<-------------------do not delete this space!------------------->%
\@err@                                                                 %
}}%
~%
\endgroup}%
}

%D These commands are intended for use by package and class writers, to
%D give information to authors.
%D
%D [Additional commented snipped; see original file for details.]

\gdef\PackageError#1#2#3{%
   \GenericError{%
      (#1)\@spaces\@spaces\@spaces\@spaces
   }{%
      Package #1 Error: #2%
   }{%
      See the #1 package documentation for explanation.%
   }{#3}%
}
\def\PackageWarning#1#2{%
   \GenericWarning{%
      (#1)\@spaces\@spaces\@spaces\@spaces
   }{%
      Package #1 Warning: #2%
   }%
}
\def\PackageWarningNoLine#1#2{%
   \PackageWarning{#1}{#2\@gobble}%
}
\def\PackageInfo#1#2{%
   \GenericInfo{%
      (#1) \@spaces\@spaces\@spaces
   }{%
      Package #1 Info: #2%
   }%
}

\gdef\ClassError#1#2#3{%
   \GenericError{%
      (#1) \space\@spaces\@spaces\@spaces
   }{%
      Class #1 Error: #2%
   }{%
      See the #1 class documentation for explanation.%
   }{#3}%
}
\def\ClassWarning#1#2{%
   \GenericWarning{%
      (#1) \space\@spaces\@spaces\@spaces
   }{%
      Class #1 Warning: #2%
   }%
}
\def\ClassWarningNoLine#1#2{%
   \ClassWarning{#1}{#2\@gobble}%
}
\def\ClassInfo#1#2{%
   \GenericInfo{%
      (#1) \space\space\@spaces\@spaces
   }{%
      Class #1 Info: #2%
   }%
}

%D \type{\@latex@error} and other internal commands:

\gdef\@latex@error#1#2{%
   \GenericError{%
      \space\space\space\@spaces\@spaces\@spaces
   }{%
      LaTeX Error: #1%
   }{%
      See the LaTeX manual or LaTeX Companion for explanation.%
   }{#2}%
}
\def\@latex@warning#1{%
   \GenericWarning{%
      \space\space\space\@spaces\@spaces\@spaces
   }{%
      LaTeX Warning: #1%
   }%
}

\def\@latex@warning@no@line#1{%
   \@latex@warning{#1\@gobble}}

\def\@latex@info#1{%
   \GenericInfo{%
      \@spaces\@spaces\@spaces
   }{%
      LaTeX Info: #1%
   }%
}

\def\@latex@info@no@line#1{%
  \@latex@info{#1\@gobble}}

%D \type{\errorcontextlines} is a \LaTeX\ counter, so that it may be be
%D manipulated with \type{\setcounter} (once it is defined :-)

\let\c@errorcontextlines\errorcontextlines
\c@errorcontextlines=-1

%D The message `on input line~$n$', if possible.

\ifnum\inputlineno=\m@ne
  \let\on@line\@empty
\else
  \def\on@line{ on input line \the\inputlineno}
\fi

%D Older \LaTeX{} messages.  For the moment, these
%D \type{\let} to the new message commands.  They may be changed later,
%D once only obsolete packages and classes contain them.

\let\@warning\@latex@warning
\let\@@warning\@latex@warning@no@line
\global\let\@latexerr\@latex@error

%D Four spaces.

\def\@spaces{\space\space\space\space}

%D \subsection{Specific errors}
%D
%D The more common error help messages.

\gdef\@eha{%
  Your command was ignored.\MessageBreak
  Type \space I <command> <return> \space to replace it %
  with another command,\MessageBreak
  or \space <return> \space to continue without it.}
\gdef\@ehb{%
  You've lost some text. \space \@ehc}
\gdef\@ehc{%
  Try typing \space <return> %
  \space to proceed.\MessageBreak
  If that doesn't work, type \space X <return> \space to quit.}
\gdef\@ehd{%
  You're in trouble here.  \space\@ehc}

%D Here are most of the error message-generating commands of \LaTeX.

%D Error message generated in \type{\@ifdefinable} from calls
%D to one of the commands \type{\newcommand}, \type{\newlength} or \type{\newtheorem}
%D specifying an already-defined command name or one that begins
%D \type{\end...}.

\gdef\@notdefinable{%
  \@latex@error{%
    Command \@backslashchar\reserved@a\space
    already defined.\MessageBreak
    Or name \@backslashchar\@qend... illegal,
    see p.192 of the manual}\@eha}

%D Generated by \type{\newline} and \type{\\} when called in vertical mode.

\gdef\@nolnerr{%
   \@latex@error{There's no line here to end}\@eha}

%D Generated by \type{\setcounter}, \type{\addtocounter} or
%D \type{\newcounter} if applied to an undefined counter.
%D
\gdef\@nocounterr#1{%
   \@latex@error{No counter '#1' defined}\@eha}

%D Obsolete version for \LaTeX\ 2.09 compatibility.

\gdef\@nocnterr{\@nocounterr?}

%D Called when trying to print the value of a counter
%D numbered by letters that's greater than 26.

\gdef\@ctrerr{%
   \@latex@error{Counter too large}\@ehb}

%D Error produced if paragraphs are typeset in the preamble.

\gdef\@nodocument{%
   \@latex@error{Missing \latexprotect\begin{document}}\@ehd}

%D Called by \type{\end} that doesn't match its \type{\begin}.

\gdef\@badend#1{%
   \@latex@error{\latexprotect\begin{\@currenvir}\@currenvline
                      \space ended by \latexprotect\end{#1}}\@eha}

%D Called by \type{\[}, \type{\]}, \type{\(} or \type{\)} when used in wrong mode.
%D
%D ### This should not be needed in \ConTeXt, as \ConTeXt\ is handling the math modes.

\gdef\@badmath{%
   \@latex@error{Bad math environment delimiter}\@eha}

%D Called by a list environment nested more than six levels
%D deep, or an enumerate or itemize nested more than four levels.

\gdef\@toodeep{%
   \@latex@error{Too deeply nested}\@ehd}
   
%D Called by \type{\endtabbing} when not enough \type{\poptabs} have
%D occurred, or by \type{\poptabs} when too many have occurred.

\gdef\@badpoptabs{%
   \@latex@error{\latexprotect\pushtabs\space and \latexprotect\poptabs
       \space don't match}\@ehd}

%D Called by \type{\>}, \type{\+} , \type{\-} or \type{\<} when stepping to an undefined tab.

\gdef\@badtab{%
  \@latex@error{Undefined tab position}\@ehd}

%D This error is special: it appears in places where we normally have
%D to \type{\latexprotect} expansions. However, to prevent a protection of
%D the error message itself (which would result in the message
%D getting printed not issued on the terminal) we need to locally
%D reset \type{\latexprotect} to \type{\relax}.

\gdef\@preamerr#1{%
  \begingroup
    \let\latexprotect\relax
    \@latex@error{\ifcase #1 Illegal character\or
     Missing @-exp\or Missing p-arg\fi\space
     in array arg}\@ehd
  \endgroup}

%D Occurs in \type{\line} and \type{\vector} command when a bad slope
%D argument is encountered.

\gdef\@badlinearg{%
   \@latex@error{%
        Bad \latexprotect\line\space or \latexprotect\vector 
        \space argument}\@ehb}

%D Occurs in a float environment or a \type{\marginpar} when
%D encountered in inner vertical mode.

\gdef\@parmoderr{%
   \@latex@error{Not in outer par mode}\@ehb}

%D Occurs in float environment or \type{\marginpar} when there
%D are no more free boxes for storing floats.

\gdef\@fltovf{%
   \@latex@error{Too many unprocessed floats}\@ehb}

%D Occurs in output routine.  This is bad news.

\gdef\@latexbug{%
   \@latex@error{This may be a LaTeX bug}{Call for help}}

%D \type{\addvspace} or \type{\addpenalty} was called when not in
%  vmode. Probably caused by a missing \type{\item}.

\gdef\@noitemerr{%
   \@latex@error{Something's wrong--perhaps a missing %
       \latexprotect\item}\@ehc}

%D A command that can be used only in the preamble
%D appears after the command |\begin{document}|.

\gdef\@notprerr{%
   \@latex@error{Can be used only in preamble}\@eha}

%D Issued by commands that don't work correctly within math (like
%D \type{\item}). There is no real error recovery happening, e.g., the
%D user might get additional errors afterwards.

\gdef\@inmatherr#1{%
   \relax
   \ifmmode
   \@latex@error{Command \latexprotect#1 invalid in math mode}\@ehc
   \fi}



\protect