%%%%%%%%%
% hymltex3.tex is  based on Mike's masthyph.tex firstly modified Oct. 91 
% by Bernard GAULLE to define dynamically languages and load hyphenation files 
% with MLTeX. This file is adapted for MlTeX V3.
% Updates --bg:
%  2/28/92 language number setting was wrong.
%  3/20/92 allow hyphenation exceptions file (in language.dat) loading.
%  7/03/92 change c@ to @c@ to be supported by kbconfig.tex          
%  8/24/92 introduce \l@<language> being the language number
% 12/10/92 Missing \hyphenation macro reintroduced.
% 03/03/93 add dialect feature via ``='' sign.
% 05/04/93 \fhyph, \ehyph & \ghyph revisited to avoid loops when
%          \openin can't access a full PATH.
% 08/19/93 apply \c mods from MJF -- July 14, 92 
% 08/20/93 insert space after \input X for NFSS2 (H. Soulard) 
% 01/13/94 add \j def for non french language (noted by R. Lemieux).
% 03/22/94 \LANGUAGESinFORMAT now contains all languages names (proposed
%             by Michel Lavaud).
%          \input hyphen replaced by \input ushyphen (sugg. of M.Lavaud).
% 05/05/94 \patterns@loaded replace \LANGUAGESinFORMAT as in Babel.
% 08/10/94 \input replaced by \@@input.
% 12/13/94 Allow nothing else than =name in language.dat
% 12/18/94 Let language counter progress even = sign after the first language.
% 04/04/95 Language counter didn't advance correctly due to misplaced \test.
% 96/09/10 Allow exception file to be blank in language.dat.
% 96/10/29 Messages numbered.
% 96/12/30 Taking in account new frhyph.tex.
% 97/03/26 \hyphenation and \showhyphens doesn't run "Improper \hyphenation 
%          will be flushed" if any accent macro is used in argument. This is
%          not the place for this here. Rewrite the code and put it in the
%          appropriate language part of packages (eg french.sty). Thus macros
%          \hyphenation and \accenthyphcodes are removed.
% 98/03/19 As people can have a language.dat file without french, i've
%          added a test and an error message, as suggested by Thomas Esser.
% 98/04/22 As pointed by T.Esser when reading language.dat file either .tex
%          file loading or error message might occur. Macro \@l@ngu@ge now
%          revisited.
% 98/04/22 ushyph.tex file defaultly loaded (instead of ushyphen).
% 98/07/15 \defaultlanguage was \undefined if no language.dat file.
%          \beginlanguage can be set (in .ini file) to the language we 
%          really target for our general usage. If no such language defined
%          (in language.dat) the error message is issued and format creation
%          is stopped immediately.
%          It was reported by T. Esser that =xxx% in language.dat generated
%          a "paragraph ended before \@language ended" message. Now corrected.
% 98/08/07 When exception file was empty \input was done erroneously. 
% 99/01/20 No-blank empty exception file name now checked.
%%
%%      checksum        = "36773 121 679 5984"
%%

\language=0
\let\@tempc\defaultlanguage% Save it if any.

% ======== Load Patterns as described in language.dat ======

{\newcount\l@nguage\newcount\initl@\newcount\workl@%
       % Compatibility with TeX 2: \language count
       \ifx\language\let\language\l@nguage\fi%
\initl@=\language\l@nguage=\initl@\workl@=\l@nguage
\xdef\patterns@loaded{}% will contain all languages names
\def\@newlanguage[#1]#2{\language=#1%
        \workl@=#1\advance\workl@ by -\initl@%
      \message{-38- The #2 language is used as language number \the\workl@}%
      \expandafter\xdef\csname l@#2\endcsname{\the\workl@}%
      \xdef\patterns@loaded{\patterns@loaded\space #2}%
      \expandafter\xdef\csname#2\endcsname{\language=\the\workl@\relax}%2/28/92
\ifx\undefined\defaultlanguage\gdef\defaultlanguage{\csname#2\endcsname}\fi}%
% test if #1 equal ``='' that means same language hyphenation but a dialect.
\edef\test{=}%
\def\@language#1#2 #3 #4/#5{\def\@tempa{=}\def\@tempb{#1}%
    \ifx\@tempa\@tempb%
                     \ifnum\l@nguage > \initl@ \advance\l@nguage by -1\fi%
                     \@newlanguage[\the\l@nguage]{#2}%
                     \ifnum\l@nguage \test \initl@ \advance\l@nguage by -1\fi%
    \else\edef\test{<}\@l@ngu@ge#1#2 #3 #4/{#5}% 
    \fi}%
\def\@l@ngu@ge#1 #2 #3/#4{\begingroup\@newlanguage[#4]{#1}%
  \def\@tempa{#2}% Get patterns file name and
  \ifx\@tempa\empty\else\expandafter\@@input#2 \relax%% load that file if any
    \edef\@tempa{#3}% Get exceptions file name.
     \ifx\@tempa\empty\else% Check if empty, 
      \edef\@tempb{\space\space}% else if blanks, two or
       \ifx\@tempa\@tempb\else\edef\@tempb{\space}% just one, otherwise
        \ifx\@tempa\@tempb\else\expandafter\@@input#3 \relax\fi%load that file.
       \fi%
     \fi% 
 \fi%
                          \endgroup}%
\newif\if@more\@moretrue
\openin1 = language.dat
\ifeof1\message{-39- Error: file language.dat not found, trying to load %
                 US-english hyphenation file}\language=0\input ushyph 
                 \@newlanguage[0]{english}% and let it be english hyphenation
\else\advance\l@nguage by -1
\loop  \endlinechar=-1  \read1 to \@lineD  \endlinechar`\^^M
  \ifx\@lineD\empty  \else \advance\l@nguage by 1%
    \edef\@lineD{\@lineD\space\space/{\the\l@nguage}}% 
    \expandafter\@language\@lineD
  \fi
  \ifeof1 \@morefalse \fi
  \if@more\repeat
\fi\closein1

}\defaultlanguage% go to the default language (first defined in language,dat) 

% ======= definitions for fast e/f/g hyphenation =====
\def\ehyph{\language=\l@english \lccode`\'=0 \nonfrenchspacing}%
\def\fhyph{\language=\l@french  \lccode`\'=`\'  \frenchspacing}%
\def\ghyph{\language=\l@german}%

% ======= check if default language is alive =========
\ifx\beginlanguage\undefined\else\expandafter% is begining language available?
  \ifx\csname l@\beginlanguage\endcsname\relax% if not error.
   \message{-39- Error: \beginlanguage\space 
                        language is undefined in language.dat file}% 
  \expandafter\stop% then stop immediately
  \fi%
\fi%

\endinput
