% -*- Mode: TeX -*-

\ifx\thisfileis\anUndefinedMacro\input maybeload \fi

\thisfileis{<eb.tex.texlocal>redefine} % abort loading if we're already loaded

% The LaTex macros in this file provide redefinition warnings.
% Also, the LaTex \newcommand facility is changed so that using
% \newcommand to redefine a function produces only a warning
% message, not an error.

% A control sequence equivalent to \relax is considered
% undefined, as in LaTex.

% However, allocation commands such as \newtoks are changed so
% that no redefinition takes place if their arguments are already
% defined.

\makeatletter

\edef\@backslash{\expandafter\@car\string\relax\@nil}

\def\@warnredef#1{\typeout{WARNING: The `#1' control sequence is being redefined.}}

\def\@warnnoredef#1{\typeout{WARNING: Aborting redefinition of the `#1' control sequence.}}

\def\@ifcsundefined#1#2#3{\ifx #1\anUndefinedMacro
   \def\@@temp{#2}\else\ifx #1\relax\def\@@temp{#2}\else
   \def\@@temp{#3}\fi\fi\@@temp}

% Change LaTex behavior.

\def\@notdefinable{\@warnredef{\@backslash\@tempa}}

% Define \redefcheck{\ctlseq} that just checks the sequence
% and ignores it.

\def\redefcheck#1{\@ifcsundefined#1{}{\@warnredef{\string#1}}}

% \redefabort{\oneargdefiner}{\ctlseq} puts out \oneargdefiner\ctlseq,
% except that if \ctlseq is already defined, it just gobbles both
% of them.

\def\redefabort#1#2{\@ifcsundefined#2{#1#2}{\@warnnoredef{\string#2}}}

% Define \newdef = \def with \redefcheck, and similarly for \gdef and \let.

\def\newdef#1{\redefcheck{#1}\def#1}
\def\newgdef#1{\redefcheck{#1}\gdef#1}
\def\newlet#1{\redefcheck{#1}\let#1}

% Add redefinition checking to \newif.
% \newif is declared as \outer, so we need to delay the definition
% of \latexnewif until we've scanned the definition of the new
% \newif.

\let\@tempa\newif
\def\newif#1{\redefcheck{#1}\latexnewif#1}
\let\latexnewif\@tempa

% Now for the allocation commands, which I'm setting up to skip the
% redefinition if their target control sequences are already defined.
% These are also declared \outer, but the above \@tempa trick won't work
% here; it's necessary to use another level of macros.

\def\latexnewbox{\latexouternewbox}
\let\latexouternewbox\newbox
\def\newbox#1{\redefabort\latexnewbox{#1}}

\def\latexnewcount{\latexouternewcount}
\let\latexouternewcount\newcount
\def\newcount#1{\redefabort\latexnewcount{#1}} 

\def\latexnewdimen{\latexouternewdimen}
\let\latexouternewdimen\newdimen
\def\newdimen#1{\redefabort\latexnewdimen{#1}} 

\def\latexnewtoks{\latexouternewtoks}
\let\latexouternewtoks\newtoks
\def\newtoks#1{\redefabort\latexnewtoks{#1}} 

\makeatother

