%D \module
%D   [       file=core-job, % copied from main-001,
%D        version=1997.03.31,
%D          title=\CONTEXT\ Core Macros,
%D       subtitle=Job Handling,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA / Hans Hagen \& Ton Otten}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

%D This module is still to be split and documented. 

\writestatus{loading}{Context Core Macros (Job Handling)}

%S InputFile     \input
%S InputFile     \omgeving    \environment
%S InputFile     \projekt     \project
%S InputFile     \produkt     \product
%S InputFile     \onderdeel   \component
%S
%S CheckStrings  \start  \stop
%S CheckStrings  \begin  \end
%S CheckStrings  \begin  \eind
%S
%S CheckChars    { }
%S CheckChars    [ ]
%S CheckChars    ( )
%S
%S CheckChar     $

\unprotect 

\def\currentproject     {}
\def\currentproduct     {}
\def\currentenvironment {}
\def\currentcomponent   {}

\def\loadedfiles        {}
\def\processedfiles     {}

\let\geenfilesmeer=\relax

\newcounter\textlevel
\newcounter\fileprocesslevel

\setvalue{\c!file::0}{\jobname}

\def\processedfile% is used in styles, don't change ! 
  {\getvalue{\c!file::\fileprocesslevel}}

%\def\processfile#1%
%  {\doglobal\increment\fileprocesslevel
%   \setxvalue{\c!file::\fileprocesslevel}{#1}%
%   \@EA\doglobal\@EA\addtocommalist\@EA{#1}\processedfiles
%   \readlocfile{#1}{}{}
%   \doglobal\decrement\fileprocesslevel}

\def\processlocalfile#1#2%
  {\doglobal\increment\fileprocesslevel
   \setxvalue{\c!file::\fileprocesslevel}{#2}%
   \@EA\doglobal\@EA\addtocommalist\@EA{#2}\processedfiles
   #1{#2}{}{}% #1=\readlocfile|\readsetfile{dir} #2=filename
   \doglobal\decrement\fileprocesslevel}

\def\processfile#1%
  {\ifx\allinputpaths\empty
     \def\next{\processlocalfile\readlocfile}%
   \else
     \let\filepath\empty
     \def\docommando##1%
       {\doiffileelse{\pathplusfile{##1}{#1}}
          {\donetrue\def\filepath{##1}}
          {\donefalse}%
        \ifdone\expandafter\quitcommalist\fi}%
     \doifparentfileelse{#1} % new 
       {\processcommacommand  [\allinputpaths]\docommando}
       {\processcommacommand[.,\allinputpaths]\docommando}%
     \ifx\filepath\empty
       \def\next{\processlocalfile\readlocfile}% fall back ../../..
     \else
       \def\next{\processlocalfile{\readsetfile\filepath}}% file found
     \fi
   \fi
   \next{#1}}

\let\allinputpaths\empty

\def\usepath[#1]%
  {\def\docommando##1%
     {\doifelse{##1}{\v!reset}
        {\let\allinputpaths\empty}
        {\addtocommalist{##1}\allinputpaths}}%
   \processcommalist[#1]\docommando}

\def\registreerfileinfo[#1#2]#3% geen \showmessage ? 
  {\writestatus{\m!systems}{#1#2 file #3 at line \the\inputlineno}%
   \immediatewriteutility{f #1 {#3}}}

\doifundefined{preloadfonts}    {\let\preloadfonts=\relax}
\doifundefined{preloadspecials} {\let\preloadspecials=\relax}

\def\loadsystemfiles%
  {\readsysfile{\f!newfilename}
     {\showmessage{\m!systems}{2}{\f!newfilename}}{}%
   \readsysfile{\f!oldfilename}
     {\showmessage{\m!systems}{2}{\f!oldfilename}}{}%
   \readsysfile{\f!filfilename}
     {\showmessage{\m!systems}{2}{\f!filfilename}}{}%
   \readsysfile{\f!sysfilename}
     {\showmessage{\m!systems}{2}{\f!sysfilename}}{}}

% test \@@svgebied

\def\loadallsystemfiles#1%
  {\ifx\@@svgebied\empty
     \readsysfile{#1}{\showmessage{\m!systems}{2}{#1}}{}%
   \else
     \def\doloadsystemfile##1%
       {\readsetfile{##1}{#1}{\showmessage{\m!systems}{2}{#1}}{}}%
     \processcommacommand[\@@svgebied]\doloadsystemfile
   \fi}

\def\loadsystemfiles%
  {\readsysfile{\f!newfilename}
     {\showmessage{\m!systems}{2}{\f!newfilename}}{}%
   \readsysfile{\f!oldfilename}
     {\showmessage{\m!systems}{2}{\f!oldfilename}}{}%
   \loadallsystemfiles\f!filfilename
   \loadallsystemfiles\f!sysfilename}

%D Loading of \type {cont-usr.tex} (edited by the user) 
%D and \type {cont-fmt.tex} (generated by texexec). 

\def\loaduserspecifications
  {\readsysfile{\f!usrfilename}
     {\showmessage{\m!systems}{2}{\f!usrfilename}}{}%
   \readjobfile{\f!fmtfilename}
     {\showmessage{\m!systems}{2}{\f!fmtfilename}}{}}

%D We don't want multiple jobfiles to interfere. 

\bgroup
\catcode`\%=\@@other
\xdef\texcommentsymbol{%}
\egroup

\def\loadoptionfile% 
  {\readjobfile{\jobname.\f!optionextension}
     {\showmessage{\m!systems}{2}{\jobname.\f!optionextension}}%
     {}}

% \newevery \everyjob \EveryJob
% \appendtoks ... \to \everyjob

\prependtoks \showcontextbanner   \to \everyjob

\appendtoks \loadsystemfiles      \to \everyjob
\appendtoks \preloadfonts         \to \everyjob
\appendtoks \settopskip           \to \everyjob
\appendtoks \preloadlanguages     \to \everyjob
\appendtoks \preloadspecials      \to \everyjob
\appendtoks \openspecialfile      \to \everyjob
%appendtoks \checkutilityfile     \to \everyjob % obsolete 
\appendtoks \openutilities        \to \everyjob
\appendtoks \loadoptionfile       \to \everyjob
\appendtoks \loadtwopassdata      \to \everyjob
\appendtoks \setupfootnotes       \to \everyjob % depends on bodyfont
\appendtoks \initializeMPgraphics \to \everyjob % after loading system files

\appendtoks \pagina[\v!laatste] \pagina           \to \everybye
\appendtoks \ifarrangingpages\poparrangedpages\fi \to \everybye
\appendtoks \registreerfileinfo[end]{\jobname}    \to \everybye

\appendtoks \savenofpages    \to \everybye
\appendtoks \savenofsubpages \to \everybye

\appendtoks \closeutilities    \to \everygoodbye
\appendtoks \stopcopyingblocks \to \everygoodbye
\appendtoks \closespecialfile  \to \everygoodbye

\appendtoks \checkreferences \to \everystarttext % nieuw 4-12-1999 

\def\doateverystarttext%
  {\the\everystarttext
   \global\let\doateverystarttext\relax}

\def\starttekst%
  {\doateverystarttext
   \ifnum\textlevel=0
    \registreerfileinfo[begin]{\jobname}%
    \expandafter\startcopyingblocks
   \fi
   \doglobal\increment\textlevel\relax}

\def\stoptekst%
  {\doglobal\decrement\textlevel\relax
   \ifnum\textlevel>0 \else
     \the\everystoptext
    %\the\everybye            % 
    %\the\everygoodbye        % == \end (new)
    %\expandafter\normalend   %
     \expandafter\end
   \fi}

\let\normalend=\end

\def\end%
  {\ifnum\textlevel>0 \else
     \the\everybye
     \the\everygoodbye
     \global\everygoodbye\emptytoks % rather unneeded
     \global\everybye\emptytoks     % but for sure 
     \expandafter\normalend
   \fi}

\def\doexecutefileonce#1%
  {\beforesplitstring#1\at.\to\currentfile
   \ExpandBothAfter\doifnotinset{\currentfile}{\loadedfiles}%
     {\ExpandFirstAfter\addtocommalist{\currentfile}\loadedfiles
      \doexecutefile{#1}}}

\def\doexecutefile#1%
  {\registreerfileinfo[begin]{#1}
   \processfile{#1}%
   \registreerfileinfo[end]{#1}}

\def\donotexecutefile#1%
  {}

\def\verwerkfile#1 %
  {\doexecutefile{#1}}

\def\omgeving #1 % at outermost level only
  {\def\startomgeving ##1 {}%
   \let\stopomgeving=\relax
   \startreadingfile
   \processfile{#1}% \readlocfile{#1}{}{}%
   \stopreadingfile}

\let\environment\omgeving % for the moment 

\newcounter\filelevel

\def\!!donextlevel#1#2#3#4#5#6\\%
  {\beforesplitstring#6\at.\to#1
   \ifnum\filelevel=0
     \starttekst
     \def\projekt   ##1 {#2{##1}}%
     \def\omgeving  ##1 {#3{##1}}%
     \def\produkt   ##1 {#4{##1}}%
     \def\onderdeel ##1 {#5{##1}}%
   \fi
   \increment\filelevel\relax
   \ExpandFirstAfter\addtocommalist{#1}\loadedfiles}

\def\doprevlevel%
  {\ifnum\filelevel=1
     \expandafter\stoptekst
   \else
     \decrement\filelevel\relax
     \expandafter\endinput
   \fi}

\def\startprojekt #1 %
  {\!!donextlevel\currentproject
     \donotexecutefile\doexecutefileonce
     \doexecutefileonce\doexecutefile#1\\}

\def\stopprojekt%
  {\doprevlevel}

\def\startprodukt #1 %
  {\doateverystarttext
   \!!donextlevel\currentproduct
     \doexecutefileonce\doexecutefileonce
     \donotexecutefile\doexecutefile#1\\}

\def\stopprodukt%
  {\doprevlevel}

\def\startonderdeel #1 %
  {\doateverystarttext
   \!!donextlevel\currentcomponent
     \doexecutefileonce\doexecutefileonce
     \donotexecutefile\doexecutefile#1\\}

\def\stoponderdeel%
  {\doprevlevel}

\def\startomgeving #1 %
  {\!!donextlevel\currentenvironment
     \donotexecutefile\doexecutefileonce
     \donotexecutefile\donotexecutefile#1\\}

\def\stopomgeving%
  {\doprevlevel}

\long\def\skipdeelomgeving#1\stopdeelomgeving%
  {}

\def\startdeelomgeving[#1]%
  {\def\partialenvironments{}%
   \def\docommando##1%
     {\beforesplitstring##1\at.\to\someevironment
      \ExpandFirstAfter\addtocommalist{\someevironment}\partialenvironments}%
   \processcommalist[#1]\docommando
   \ExpandBothAfter\doifcommonelse
       {\currentproject,\currentproduct,
        \currentcomponent,\currentenvironment}
       {\partialenvironments}
     {\let\stopdeelomgeving=\relax
      \let\next=\relax}
     {\let\next=\skipdeelomgeving}%
   \next}

\def\startproduct{\startprodukt}
\def\stopproduct {\stopprodukt}
\def\startproject{\startprojekt}
\def\stopproject {\stopprojekt}

\def\project{\projekt}
\def\product{\produkt}

\def\deelomgeving #1 %
  {\doexecutefileonce{#1}}

\expanded
  {\long\noexpand\def\csname\e!start\e!instellingen\endcsname##1 ##2\csname\e!stop\e!instellingen\endcsname%
     {\noexpand\long\noexpand\setvalue{\??su##1}{##2}}}

\long\def\startsetups#1 #2\stopsetups% for international purposes
  {\long\setvalue{\??su#1}{#2}}

\def\dodosetups#1%
  {\getvalue{\??su#1}}

\def\dosetups[#1]%
  {\iffirstargument
     \dodosetups{#1}%
   \else
     \expandafter\dodosetups
   \fi}

\def\setups%
  {\dosingleargument\dosetups}

% Het <pagina>-karakter (FormFeed), wordt omgezet in \par

\edef\oldlinefeed{\the\catcode`\^^L}

\catcode`\^^L=\oldlinefeed
\catcode`\^^L=\@@endofline

% NOT TOEVOEGEN: \the\everytrace

\neverypar=\emptytoks

\appendtoks \flushfootnotes                 \to \everypar    
\appendtoks \ifinner\else\checksidefloat\fi \to \everypar    
\appendtoks \checkindentation               \to \everypar    
\appendtoks \showparagraphnumber            \to \everypar    
\appendtoks \flushmargincontents            \to \everypar    
\appendtoks \flushcomments                  \to \everypar    
\appendtoks \synchronizefootnotes           \to \everypar    

\appendtoks \flushfootnotes                 \to \everydisplay
\appendtoks \adjustsidefloatdisplaylines    \to \everydisplay

% Default-instellingen (verborgen)

\resetutilities

\protect \endinput
