%
% laemu.sty -- helper TeX macros and LaTeX emulation for plain TeX + LaTeX
% by pts@fazekas.hu at Fri Jan 24 11:14:34 CET 2003
%
% To use this package from LaTeX, type
%
%	\usepackage{laemu}
%
% To use this package from either LaTeX, plain TeX or AMSTeX, type
%
%	\input laemu.sty
%
% As an alternative, the following solution will open laemu.sty only once:
%
%	\expandafter\ifx\csname usepackage\endcsname\relax%
%	  \def\usepackage#1{\input#1.sty\relax}\fi%
%	\usepackage{laemu}
%	% this ensures that laemu.sty won't be loaded twice
%
% Tho use further packages from any TeX formats, type
%
%	\usepackage{...PACKAGENAME...}
%

% vvv Don't load laemu.sty more than once
\expandafter\ifx\csname ifLaTeX\endcsname\relax\else\expandafter\endinput\fi

\newif\ifWasLetter% false
\ifnum\the\catcode`\@=\the\catcode`\a\relax\WasLettertrue\fi%
\catcode`\@=11% early \makeatletter

%** Are we running a real LaTeX?
\newif\ifLaTeX% default to false
%** Sets \ifLaTeX to (#1 == 'L' && #2 == 'a')
\def\@@setlatex#1#2#3\end{%
  \if#1L\if#2a\LaTeXtrue\else\LaTeXfalse\fi\fi%
}%
% Dat: \fmtname is "plain", "LaTeX2e" or "AmS-TeX"
\expandafter\@@setlatex\fmtname\end%

\ifLaTeX%
  %\PackageError{laemu}{alma}\@ehc
  %** Calls #1{...}, where ... is the comma-separated option list for the
  %** current package.
  %** Arranges that these options won't be found at \endinput
  %** Example: \LaemuProcessOptions\errmessage
  %** @param #1 a \cs
  \def\LaemuProcessOptions#1{%
    \edef\reserved@a{\noexpand#1{\@ptionlist{\@currname.\@currext}}}%
    \reserved@a
    \AtEndOfPackage{\def\@unprocessedoptions{}}%
  }%
\else%
  \expandafter\let\csname opt@laemu.sty\endcsname\@empty% loaded ourselves
  \long\def\@gobble#1{}%
  \def\@ehc{No help here.}%
  \newlinechar=`\^^J%
  \def\on@line{ in line \the\inputlineno}% so it can be \@gobble-d
  \def\NeedsTeXFormat#1{}% ignore
  \def\ProvidesPackage#1#2[#3]{\makeatletter}% so `\input MYPKG.sty' works
  % Dat: use \MessageBreak to separate error/warning/info lines
  \def\PackageWarning#1#2{% Imp: robust
    \def\MessageBreak{^^Jw: (#1) }%
    % vvv Dat: line number refers to place of the \PackageWarning invocation
    \immediate\write16{^^JPackage #1 Warning: #2\on@line.^^J}%
  }%
  \def\PackageInfo#1#2{% Imp: robust
    \def\MessageBreak{^^Ji: (#1) }%
    % vvv Dat: line number refers to place of the \PackageWarning invocation
    \immediate\write-1{Package #1 Info: #2\on@line.}%
  }%
  \def\PackageError#1#2#3{\begingroup% Imp: robust
    % Dat: real LaTeX \GenericError doesn't print stupid macro expansions
    % after the error message. We cannot emulate it here, possibly because it
    % this \PackageError should have been put into the format file (??)
    \immediate\write16{}% an empty line
    \def\MessageBreak{^^J!: (#1) }%
    % vvv Dat: line number refers to place of the \PackageWarning invocation
    \edef\@err@{{Package #1 Error: #2\MessageBreak#3}}%
    \errhelp\@err@%
    %\let\@err@\empty%
    \errmessage{Package #1 Error: #2^^J!, See the package #1 docs for explanation}%
    %~%
  \endgroup}%
  %
  %** `\RequirePackage{package}' loads package.sty unless already loaded
  %** `\RequirePackage[option]{package}'
  %** `\RequirePackage[option1,option2,option3]{package}'
  \def\RequirePackage{%
    % Imp: allow optional arguments
    % Imp: show a warning of loading with different arguments
    \@ifnextchar[%]
      {\@@RequirePackage@lowa}%
      {\@@RequirePackage@lowa[]}%
  }%
  \def\@@RequirePackage@lowa[#1]#2{%
    % Imp: add/strip ".sty" from end of #1
    \@@RequirePackage@lowb{#2.sty}{#1}%
  }%
  %** {current.sty}{lower.sty}....%
  \def\@@laemu@packagestack{.}% Imp: save \catcode64
  %** \def#1{...}, where ... is the current package filename
  \def\@@laemu@packagetop#1{%
    %\message{T(\@@laemu@packagestack)}%
    \expandafter\@@laemu@packagetop@low\@@laemu@packagestack#1%
  }%
  \def\@@laemu@packagetop@low#1#2.#3{%
    % Dat: #2 is invalid now (braces may be stripped), don't use it
    \def#3{#1}%
  }%
  \def\@@laemu@packagepop{% Imp: error on underflow
    %\message{P(\@@laemu@packagestack)}%
    \global\edef\@@laemu@packagestack{\expandafter\@gobble\@@laemu@packagestack}%
    %\expandafter\@@laemu@packagepop@low\@@laemu@packagestack% doesn't work well
  }%
  %\def\@@laemu@packagepop@low#1#2.{% SUXX: removes brace from {y} in {x}{y}.
  %  \message{Q(#1,#2)}%
  %  \global\edef\@@laemu@packagestack{#2.}%
  %  \message{R(\@@laemu@packagestack)}%
  %}%
  \def\@@laemu@packagepush#1{%
    \global\edef\@@laemu@packagestack{{#1}\@@laemu@packagestack}%
    %\message{E(\@@laemu@packagestack)}%
  }%
  %** @param #1 filename with `.sty'
  %** @param #2 options
  \def\@@RequirePackage@lowb#1#2{%
    \expandafter\ifx\csname opt@#1\endcsname\relax%
      \expandafter\def\csname opt@#1\endcsname{#2}%
      %\ifcat \noexpand @\noexpand a%
      \@@laemu@packagepush{#1}%
      \ifnum\the\catcode`\@=\the\catcode`\a\relax% Dat: \ifcat is wrong, because we need the _current_ value (at the time of execution)
        \input#1\relax%
      \else%
        \makeatletter%
        \input#1\relax%
        \makeatother%
      \fi%
      \@@laemu@packagepop%
    \else% package already loaded -- verify options
      \def\reserved@a{#2}%
      % Imp: allow \usepackage[a,b]{c} and \usepackage[b,a]{c}
      % Dat: LaTeX \def's comma-separated-values to \csname opt@graphic.p.sty\endcsname
      \expandafter\ifx\csname opt@#1\endcsname\reserved@a\else%
        \PackageError{laemu}{Option clash for package file `#1'}{%
          Old options: \csname opt@#1\endcsname\MessageBreak%
          New options: \reserved@a
        }%
      \fi%
      \let\reserved@a\@empty
    \fi%
  }%
  \let\usepackage\RequirePackage%
  % vvv see full comment above
  \def\LaemuProcessOptions#1{%
    \@@laemu@packagetop\reserved@a%
    \edef\reserved@a{\noexpand#1{\csname opt@\reserved@a\endcsname}}%
    \reserved@a
  }%
  %
  \def\makeatletter{\catcode`\@11\relax}%
  \def\makeatother{\catcode`\@12\relax}%
  \def\@empty{}% not needed for LaTeX, needed for plain TeX
  \long\def\@secondoftwo#1#2{#2}%
  \def\@namedef#1{\expandafter\def\csname #1\endcsname}%
  \def\@nameuse#1{\csname #1\endcsname}%
  \expandafter\ifx\csname @ne\endcsname\relax \chardef\@ne=1\fi%
  %
  \long\def\@ifnextchar#1#2#3{%
    \let\reserved@d=#1%
    \def\reserved@a{#2}%
    \def\reserved@b{#3}%
    \futurelet\@let@token\@ifnch}
  \def\@ifnch{%
    \ifx\@let@token\@sptoken
      \let\reserved@c\@xifnch
    \else
      \ifx\@let@token\reserved@d
        \let\reserved@c\reserved@a
      \else
        \let\reserved@c\reserved@b
      \fi
    \fi  
    \reserved@c}
  \def\:{\let\@sptoken= } \:  % this makes \@sptoken a space token
  \def\:{\@xifnch} \expandafter\def\: {\futurelet\@let@token\@ifnch}%
  \expandafter\ifx\csname @inputcheck\endcsname\relax%
    \newread\@inputcheck%
  \fi%
\fi%

%** \input's a file if it exists
%** @param #1 filename
\def\LaemuInputQuiet#1{%
  \openin\@inputcheck#1 %
  \ifeof\@inputcheck\else%
    \closein\@inputcheck%
    \input#1 %
  \fi
}%

%\PackageWarning{laemu}{alma\MessageBreak korte}
%\PackageInfo{laemu}{ialma\MessageBreak korte}
%\PackageError{laemu}{ialma\MessageBreak korte}\@ehc

\ifWasLetter\else\makeatother\fi%
