% \iffalse meta-comment
%
% Copyright 1989-1996 Johannes L. Braams and any individual authors
% listed elsewhere in this file.  All rights reserved.
% 
% For further copyright information see any other copyright notices in 
% this file.
% 
% This file is part of the Babel system release 3.6.
% --------------------------------------------------
%   This system is distributed in the hope that it will be useful,
%   but WITHOUT ANY WARRANTY; without even the implied warranty of
%   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
% 
%   For error reports concerning UNCHANGED versions of this file no
%   more than one year old, see bugs.txt.
% 
%   Please do not request updates from me directly.  Primary
%   distribution is through the CTAN archives.
% 
% 
% IMPORTANT COPYRIGHT NOTICE:
% 
% You are NOT ALLOWED to distribute this file alone.
% 
% You are allowed to distribute this file under the condition that it
% is distributed together with all the files listed in manifest.txt.
% 
% If you receive only some of these files from someone, complain!
% 
% Permission is granted to copy this file to another file with a
% clearly different name and to customize the declarations in that
% copy to serve the needs of your installation, provided that you
% comply with the conditions in the file legal.txt from the LaTeX2e
% distribution.
% 
% However, NO PERMISSION is granted to produce or to distribute a
% modified version of this file under its original name.
%  
% You are NOT ALLOWED to change this file.
% 
% 
% \fi
% \CheckSum{708}
%
% \iffalse
%<*dtx>
\ProvidesFile{bbplain.dtx}
       [1997/01/19 v1.0l Babel support for plain-based formats]
%</dtx>
%
% Babel package for LaTeX version 2e
% Copyright (C) 1989 - 1997
%           by Johannes Braams
%
% Please report errors to: J.L. Braams
%                          JLBraams@cistron.nl
%
%    This file is part of the babel system, it provides the source
%    code for the extra definitions needed to be able to use babel
%    with a plain-based format.
%<*filedriver>
\documentclass{ltxdoc}
\font\manual=logo10 % font used for the METAFONT logo, etc.
\newcommand*\MF{{\manual META}\-{\manual FONT}}
\newcommand*{\babel}{\textsf{babel}}
\newcommand*{\langvar}{$\langle \it lang \rangle$}
\newcommand*{\note}[1]{}
\newcommand*{\Lopt}[1]{\textsf{#1}}
\newcommand*{\file}[1]{\texttt{#1}}
\begin{document}
 \DocInput{bbplain.dtx}
\end{document}
%</filedriver>
% \fi
%
% \GetFileInfo{bbplain.dtx}
% \StopEventually{}
%
%  \section{Support for formats based on \textsc{plain}\TeX}
%
%    The following code duplicates or emulates parts of \LaTeXe\ that
%    are needed for \babel.
%
% \changes{bbplain-1.0f}{1996/07/09}{Consistently use \cs{@undefined}
%    instead of \cs{undefined}} 
% \changes{bbplain-1.0f}{1996/07/09}{added \cs{@empty}}
% \changes{bbplain-1.0h}{1996/10/07}{Only load the necessary parts
%    into the format, let this file be read agian by babel.def} 
%    \begin{macrocode}
%<*code>
\ifx\adddialect\@undefined
%    \end{macrocode}
%    When |\adddialect| is still undefined we are making a
%    format. In that case only the first part of this file is needed. 
%    \begin{macrocode}
  \def\@empty{}
%    \end{macrocode}
%    We need to define |\loadlocalcfg| for plain users as the
%    \LaTeX\ definition uses |\InputIfFileExists|.
% \changes{bbplain-1.0c}{1995/07/26}{Added definition of
%    \cs{loadlocalcfg}}
% \changes{bbplain-1.0d}{1996/01/08}{Also reset category codes after
%    loading the configuration file as \cs{AtEndOfPackage} is
%    undefined in this case} 
%    \begin{macrocode}
  \def\loadlocalcfg#1{%
    \openin0#1.cfg
    \ifeof0
      \closein0
    \else
      \closein0
      {\immediate\write16{*************************************}%
       \immediate\write16{* Local config file #1.cfg used}%
       \immediate\write16{*}%
       }
      \input #1.cfg\relax
    \fi
%    \end{macrocode}
%    We have to execute \cs{@endofldf} in this case
%    \begin{macrocode}
    \@endofldf
    }
%    \end{macrocode}
%
% \changes{bbplain-1.0c}{1995/11/08}{Moved the \cs{dump} code here from
%    \file{babel.dtx}}
%
% \changes{bbplain-0.1}{1991/10/31}{Added redefinition of \cs{dump} to
%    add a message to \cs{everyjob}}
%    We want to add a message to the message \LaTeX$\:$2.09 puts in
%    the |\everyjob| register. This could be done by the following
%    code: 
%    \begin{verbatim}
%    \let\orgeveryjob\everyjob
%    \def\everyjob#1{%
%      \orgeveryjob{#1}%
%      \orgeveryjob\expandafter{\the\orgeveryjob\immediate\write16{%
%          hyphenation patterns for \the\loaded@patterns loaded.}}%
%      \let\everyjob\orgeveryjob\let\orgeveryjob\@undefined}
%    \end{verbatim}
%    The code above redefines the control sequence \cs{everyjob}
%    in order to be able to add something to the current contents of
%    the register. This is necessary because the processing of
%    hyphenation patterns happens long before \LaTeX\ fills the
%    register.\\
%    There are some problems with this approach though.
%  \begin{itemize}
%    \item When someone wants to use several hyphenation patterns with
%    \SliTeX\ the above scheme won't work. The reason is that \SliTeX\
%    overwrites the contents of the |\everyjob| register with its own
%    message.
%    \item Plain \TeX\ does not use the |\everyjob| register so the
%    message would not be displayed.
%  \end{itemize}
%    To circumvent this a `dirty trick' can be used. As this code is
%    only processed when creating a new format file there is one
%    command that is sure to be used, |\dump|. Therefore the orginal
%    |\dump| is saved in |\org@dump| and a new definition is supplied.
%    \begin{macrocode}
  \let\orig@dump=\dump
  \def\dump{%
%    \end{macrocode}
%    To make sure that \LaTeX$\:$2.09 executes the
%    |\@begindocumenthook| we would want to alter |\begin{document}|,
%    but as this done too often already, we add the new code at the
%    front of |\@preamblecmds|. But we can only do that after it has
%    been defined, so we add this peice of code to |\dump|.
% \changes{bbplain-1.0c}{1995/11/19}{Add execution of
%    \cs{@begindocumenthook} to \cs{@preamblecmds}} 
% \changes{bbplain-1.0i}{1997/01/02}{\cs{document} is not a
%    \LaTeX2.09-only command; AMS\TeX defines it too; now use
%    \cs{@ztryfc} to detect \LaTeX2.09} 
% \changes{bbplain-1.0j}{1997/01/07}{Add the definition of
%    \cs{@begindocumenthook} to the \LaTeX2.09 format} 
%    \begin{macrocode}
    \ifx\@ztryfc\@undefined
    \else
      \toks0=\expandafter{\@preamblecmds}
      \edef\@preamblecmds{\noexpand\@begindocumenthook\the\toks0}
      \def\@begindocumenthook{}
    \fi
%    \end{macrocode}
%    This new definition starts by adding an instruction to write a
%    message on the terminal and in the transcript file to inform the
%    user of the preloaded hyphenation patterns.
% \changes{bbplain-1.0f}{1996/07/09}{Use \cs{toks8} instead of
%    \cs{patterns@loaded}} 
%    \begin{macrocode}
    \everyjob\expandafter{\the\everyjob%
      \immediate\write16{\the\toks8 loaded.}}%
%    \end{macrocode}
%    Then everything is restored to the old situation and the format
%    is dumped.
%    \begin{macrocode}
    \let\dump\orig@dump\let\orig@dump\@undefined\dump}
  \expandafter\endinput
\fi
%    \end{macrocode}
%    The rest of this file is not processed by ini\TeX\ but during the
%    normal document run.
%    A number of \LaTeX\ macro's that are needed later on.
%    \begin{macrocode}
\long\def\@firstofone#1{#1}
\long\def\@firstoftwo#1#2{#1}
\def\@ifstar#1{\@ifnextchar *{\@firstoftwo{#1}}}
\def\@star@or@long#1{%
  \@ifstar
  {\let\l@ngrel@x\relax#1}%
  {\let\l@ngrel@x\long#1}}
\let\l@ngrel@x\relax
\def\@car#1#2\@nil{#1}
\def\@cdr#1#2\@nil{#2}
\let\@typeset@protect\relax
\long\def\@gobble#1{}
\edef\@backslashchar{\expandafter\@gobble\string\\}
\def\strip@prefix#1>{}
\def\g@addto@macro#1#2{{%
    \toks@\expandafter{#1#2}%
    \xdef#1{\the\toks@}}}
\def\@namedef#1{\expandafter\def\csname #1\endcsname}
%    \end{macrocode}
%    \LaTeXe\ has the command |\@onlypreamble| which adds commands to
%    a list of commands that are no longer needed after
%    |\begin{document}|.
%    \begin{macrocode}
\ifx\@preamblecmds\@undefined
  \def\@preamblecmds{}
\fi
\def\@onlypreamble#1{%
  \expandafter\gdef\expandafter\@preamblecmds\expandafter{%
    \@preamblecmds\do#1}}
\@onlypreamble\@onlypreamble
%    \end{macrocode}
%    Mimmick \LaTeX's |\AtBeginDocument|; for this to work the user
%    needs to add |\begindocument| to his file.
%    \begin{macrocode}
\def\begindocument{%
  \@begindocumenthook
  \global\let\@begindocumenthook\@undefined
  \def\do##1{\global\let ##1\@undefined}%
  \@preamblecmds
  \global\let\do\noexpand
  }
%    \end{macrocode}
%    
% \changes{bbplain-1.0j}{1997/01/07}{\cs{@begindocumenthook} might
%    already be defined} 
% \changes{bbplain-1.0k}{1997/01/14}{missing \cs{@undefined} added}
%    \begin{macrocode}
\ifx\@begindocumenthook\@undefined
  \def\@begindocumenthook{}
\fi
\@onlypreamble\@begindocumenthook
\def\AtBeginDocument{\g@addto@macro\@begindocumenthook}
%    \end{macrocode}
%    We also have to mimick \LaTeX's |\AtEndOfPackage|. Our
%    replacement macro is much simpler; it stores its argument in
%    |\@endofldf|.
%    \begin{macrocode}
\def\AtEndOfPackage#1{\g@addto@macro\@endofldf{#1}}
\@onlypreamble\AtEndOfPackage
\def\@endofldf{}
\@onlypreamble\@endofldf
%    \end{macrocode}
%    \LaTeX\ needs to be able to switch off writing to its auxiliary
%    files; plain doesn't have them by default.
%    \begin{macrocode}
\let\if@filesw\iffalse
%    \end{macrocode}
%    Mimick \LaTeX's commands to define control sequences.
% \changes{bbplain-1.0e}{1996/02/22}{Provide a more complete emulation
%    of \cs{DeclareRobustCommand} and \cs{newcommand}} 
%    \begin{macrocode}
\def\newcommand{\@star@or@long\new@command}
\def\new@command#1{%
  \@testopt{\@newcommand#1}0}
\def\@newcommand#1[#2]{%
  \@ifnextchar [{\@xargdef#1[#2]}%
                {\@argdef#1[#2]}}
\long\def\@argdef#1[#2]#3{%
  \@yargdef#1\@ne{#2}{#3}}
\long\def\@xargdef#1[#2][#3]#4{%
  \expandafter\def\expandafter#1\expandafter{%
    \expandafter\@protected@testopt\expandafter #1%
    \csname\string#1\expandafter\endcsname{#3}}%
  \expandafter\@yargdef \csname\string#1\endcsname
  \tw@{#2}{#4}}
\long\def\@yargdef#1#2#3{%
  \@tempcnta#3\relax
  \advance \@tempcnta \@ne
  \let\@hash@\relax
  \edef\reserved@a{\ifx#2\tw@ [\@hash@1]\fi}%
  \@tempcntb #2%
  \@whilenum\@tempcntb <\@tempcnta
  \do{%
    \edef\reserved@a{\reserved@a\@hash@\the\@tempcntb}%
    \advance\@tempcntb \@ne}%
  \let\@hash@##%
  \l@ngrel@x\expandafter\def\expandafter#1\reserved@a}
\let\providecommand\newcommand
%    \end{macrocode}
%    
%    \begin{macrocode}

\def\DeclareRobustCommand{\@star@or@long\declare@robustcommand}
\def\declare@robustcommand#1{%
   \edef\reserved@a{\string#1}%
   \def\reserved@b{#1}%
   \edef\reserved@b{\expandafter\strip@prefix\meaning\reserved@b}%
   \edef#1{%
      \ifx\reserved@a\reserved@b
         \noexpand\x@protect
         \noexpand#1%
      \fi
      \noexpand\protect
      \expandafter\noexpand\csname
         \expandafter\@gobble\string#1 \endcsname
   }%
   \expandafter\new@command\csname
      \expandafter\@gobble\string#1 \endcsname
}
\def\x@protect#1{%
   \ifx\protect\@typeset@protect\else
      \@x@protect#1%
   \fi
}
\def\@x@protect#1\fi#2#3{%
   \fi\protect#1%
}
%    \end{macrocode}
%
%    \LaTeX\ has a macro to check whether a certain package was loaded
%    with specific options. The command has two extra arguments which
%    are code to be executed in either the true or false case.
%    This is used to detect whether the document needs one of the
%    accents to be activated (\Lopt{activegrave} and
%    \Lopt{activeacute}). For plain \TeX\ we assume that the user
%    wants them to be active by default. Therefore the only thing we
%    do is execute the third argument (the code for the true case).
% 
%    \begin{macrocode}
\def\@ifpackagewith#1#2#3#4{%
  #3}
%    \end{macrocode}
%
% \changes{bbplain-1.0e}{1996/01/15}{Added the \cs{newcommand} code}
%    For the following code we need to make sure that the commands
%    |\newcommand| and |\providecommand| exist with some sensible
%    definition. They are not fully equivalent to their \LaTeXe\
%    versions; just enough to make things work in plain~\TeX
%    environments.
%
% \changes{bbplain-1.0k}{1997/01/14}{\cs{newcount} is an \cs{outer}
%    command, can't use it inside an \cs{if} construct} 
% \changes{bbplain-1.0l}{1997/01/19}{Mixed up the definition of \cs{@tempcntb}}
%    \begin{macrocode}
\ifx\@tempcnta\@undefined
  \csname newcount\endcsname\@tempcnta\relax
\fi
\ifx\@tempcntb\@undefined
  \csname newcount\endcsname\@tempcntb\relax
\fi
%    \end{macrocode}
%    To prevent wasting two counters in \LaTeX$\:$2.09 (because
%    counters with the same name are allocated later by it) we reset
%    the counter that holds the next free counter (|\count10|).
%    \begin{macrocode}
\ifx\bye\@undefined
 \advance\count10 by -2\relax
\fi
\ifx\@ifnextchar\@undefined
  \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}
\fi
\def\@testopt#1#2{%
  \@ifnextchar[{#1}{#1[#2]}}
\def\@protected@testopt#1{%%
  \ifx\protect\@typeset@protect
    \expandafter\@testopt
  \else
    \@x@protect#1%
  \fi}
\long\def\@whilenum#1\do #2{\ifnum #1\relax #2\relax\@iwhilenum{#1\relax
     #2\relax}\fi}
\long\def\@iwhilenum#1{\ifnum #1\expandafter\@iwhilenum
         \else\expandafter\@gobble\fi{#1}}
%    \end{macrocode}
%    Code from \file{ltoutenc.dtx}, adapted for use in the plain \TeX\
%    environment. 
%    \begin{macrocode}
\def\DeclareTextCommand{%
   \@dec@text@cmd\providecommand
}
\def\ProvideTextCommand{%
   \@dec@text@cmd\providecommand
}
\def\DeclareTextSymbol#1#2#3{%
   \@dec@text@cmd\chardef#1{#2}#3\relax
}
\def\@dec@text@cmd#1#2#3{%
   \expandafter\def\expandafter#2%
      \expandafter{%
         \csname#3-cmd\expandafter\endcsname
         \expandafter#2%
         \csname#3\string#2\endcsname
      }%
%   \let\@ifdefinable\@rc@ifdefinable
   \expandafter#1\csname#3\string#2\endcsname
}
\def\@current@cmd#1{%
  \ifx\protect\@typeset@protect\else
      \noexpand#1\expandafter\@gobble
  \fi
}
\def\@changed@cmd#1#2{%
   \ifx\protect\@typeset@protect
      \expandafter\ifx\csname\cf@encoding\string#1\endcsname\relax
         \expandafter\ifx\csname ?\string#1\endcsname\relax
            \expandafter\def\csname ?\string#1\endcsname{%
               \@changed@x@err{#1}%
            }%
         \fi
         \global\expandafter\let
           \csname\cf@encoding \string#1\expandafter\endcsname
           \csname ?\string#1\endcsname
      \fi
      \csname\cf@encoding\string#1%
        \expandafter\endcsname
   \else
      \noexpand#1%
   \fi
}
\def\@changed@x@err#1{%
    \errhelp{Your command will be ignored, type <return> to proceed}%
    \errmessage{Command \protect#1 undefined in encoding \cf@encoding}}
\def\DeclareTextCommandDefault#1{%
   \DeclareTextCommand#1?%
}
\def\ProvideTextCommandDefault#1{%
   \ProvideTextCommand#1?%
}
\expandafter\let\csname OT1-cmd\endcsname\@current@cmd
\expandafter\let\csname?-cmd\endcsname\@changed@cmd
\def\DeclareTextAccent#1#2#3{%
  \DeclareTextCommand#1{#2}[1]{\accent#3 ##1}
}
\def\DeclareTextCompositeCommand#1#2#3#4{%
   \expandafter\let\expandafter\reserved@a\csname#2\string#1\endcsname
   \edef\reserved@b{\string##1}%
   \edef\reserved@c{%
     \expandafter\@strip@args\meaning\reserved@a:-\@strip@args}%
   \ifx\reserved@b\reserved@c
      \expandafter\expandafter\expandafter\ifx
         \expandafter\@car\reserved@a\relax\relax\@nil
         \@text@composite
      \else
         \edef\reserved@b##1{%
            \def\expandafter\noexpand
               \csname#2\string#1\endcsname####1{%
               \noexpand\@text@composite
                  \expandafter\noexpand\csname#2\string#1\endcsname
                  ####1\noexpand\@empty\noexpand\@text@composite
                  {##1}%
            }%
         }%
         \expandafter\reserved@b\expandafter{\reserved@a{##1}}%
      \fi
      \expandafter\def\csname\expandafter\string\csname
         #2\endcsname\string#1-\string#3\endcsname{#4}
   \else
     \errhelp{Your command will be ignored, type <return> to proceed}%
     \errmessage{\string\DeclareTextCompositeCommand\space used on
         inappropriate command \protect#1}
   \fi
}
\def\@text@composite#1#2#3\@text@composite{%
   \expandafter\@text@composite@x
      \csname\string#1-\string#2\endcsname
}
\def\@text@composite@x#1#2{%
   \ifx#1\relax
      #2%
   \else
      #1%
   \fi
}
%
\def\@strip@args#1:#2-#3\@strip@args{#2}
\def\DeclareTextComposite#1#2#3#4{%
   \def\reserved@a{\DeclareTextCompositeCommand#1{#2}{#3}}%
   \bgroup
      \lccode`\@=#4%
      \lowercase{%
   \egroup
      \reserved@a @%
   }%
}
%
\def\UseTextSymbol#1#2{%
%   \let\@curr@enc\cf@encoding
%   \@use@text@encoding{#1}%
   #2%
%   \@use@text@encoding\@curr@enc
}
\def\UseTextAccent#1#2#3{%
%   \let\@curr@enc\cf@encoding
%   \@use@text@encoding{#1}%
%   #2{\@use@text@encoding\@curr@enc\selectfont#3}%
%   \@use@text@encoding\@curr@enc
}
\def\@use@text@encoding#1{%
%   \edef\f@encoding{#1}%
%   \xdef\font@name{%
%      \csname\curr@fontshape/\f@size\endcsname
%   }%
%   \pickup@font
%   \font@name
%   \@@enc@update
}
\def\DeclareTextSymbolDefault#1#2{%
   \DeclareTextCommandDefault#1{\UseTextSymbol{#2}#1}%
}
\def\DeclareTextAccentDefault#1#2{%
   \DeclareTextCommandDefault#1{\UseTextAccent{#2}#1}%
}
\def\cf@encoding{OT1}
%    \end{macrocode}
%    Currently we only use the \LaTeXe\ method for accents for those
%    that are known to be made active in \emph{some} language
%    definition file.
%    \begin{macrocode}
\DeclareTextAccent{\"}{OT1}{127}
\DeclareTextAccent{\'}{OT1}{19}
\DeclareTextAccent{\^}{OT1}{94}
\DeclareTextAccent{\`}{OT1}{18}
\DeclareTextAccent{\~}{OT1}{126}
%    \end{macrocode}
%    The following two control sequences are used in \file{babel.def}
%    but are not defined for \textsc{plain} \TeX.
% \changes{bbplain-1.0f}{1996/07/10}{Added \cs{textquotedblright} and
%    \cs{textquoteright}} 
% \changes{bbplain-1.0g}{1996/08/15}{Added \cs{ss} and \cs{i}}
%    \begin{macrocode}
\DeclareTextSymbol{\textquotedblright}{OT1}{`\"}
\DeclareTextSymbol{\textquoteright}{OT1}{`\'}
\DeclareTextSymbol{\i}{OT1}{16}
\DeclareTextSymbol{\ss}{OT1}{25}
%    \end{macrocode}
%    For a couple of languages we need the \LaTeX-control sequence
%    |\scriptsize| to be available. Because plain \TeX\ doesn't have
%    such a sofisticated font mechanism as \LaTeX\ has, we just |\let|
%    it to |\sevenrm|.
% \changes{bbplain-1.0f}{1996/07/10}{Added definition of
%    \cs{scriptsize}} 
%    \begin{macrocode}
\ifx\scriptsize\@undefined
  \let\scriptsize\sevenrm
\fi
%</code>
%    \end{macrocode}
%
\endinput
%%
%% \CharacterTable
%%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%%   Digits        \0\1\2\3\4\5\6\7\8\9
%%   Exclamation   \!     Double quote  \"     Hash (number) \#
%%   Dollar        \$     Percent       \%     Ampersand     \&
%%   Acute accent  \'     Left paren    \(     Right paren   \)
%%   Asterisk      \*     Plus          \+     Comma         \,
%%   Minus         \-     Point         \.     Solidus       \/
%%   Colon         \:     Semicolon     \;     Less than     \<
%%   Equals        \=     Greater than  \>     Question mark \?
%%   Commercial at \@     Left bracket  \[     Backslash     \\
%%   Right bracket \]     Circumflex    \^     Underscore    \_
%%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%%   Right brace   \}     Tilde         \~}
