%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}

\unprotect

\let \currentproject     \empty
\let \currentproduct     \empty
\let \currentenvironment \empty
\let \currentcomponent   \empty

\let \loadedfiles        \empty
\let \processedfiles     \empty

\let \nomorefiles        \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\dostarttextfile#1%
  {\doglobal\increment\fileprocesslevel
   \setxvalue{\c!file::\fileprocesslevel}{#1}%
   \@EA\doglobal\@EA\addtocommalist\@EA{#1}\processedfiles}

\def\dostoptextfile
  {\doglobal\decrement\fileprocesslevel}

\def\processlocalfile#1#2%
  {% #1=\readlocfile|\readsetfile{dir} #2=filename
   % #1{#2}\donothing\donothing
   #1{#2}\donothing{\readfile{#2}\donothing\donothing}}

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

\def\doifinputfileelse#1% rarely used
  {\ifx\allinputpaths\empty
     \@EA\secondoftwoarguments
   \else
     \let\filepath\empty
     \def\docommand##1%
       {\doiffileelse{\pathplusfile{##1}{#1}}{\donetrue\def\filepath{##1}}\donefalse
        \ifdone\expandafter\quitcommalist\fi}%
     \processcommacommand[.,\allinputpaths]\docommand
     \ifx\filepath\empty
       \@EAEAEA\secondoftwoarguments
     \else
       \@EAEAEA\firstoftwoarguments
     \fi
   \fi}

\let\allinputpaths\empty

% \def\usepath[#1]%
%   {\def\docommand##1%
%      {\doifelse{##1}\v!reset
%         {\let\allinputpaths\empty}
%         {\addtocommalist{##1}\allinputpaths}}%
%    \processcommalist[#1]\docommand}
%
% \def\usesubpath[#1]% new, see webschrift for usage
%   {\ifx\allinputpaths\empty
%      \edef\allinputpaths{#1}%
%    \else
%      \def\docommand##1%
%        {\def\dodocommand####1{\addtocommalist{##1/####1}\allinputpaths}%
%         \processcommalist[#1]\dodocommand}%
%      \processcommacommand[\allinputpaths]\docommand
%    \fi}
%
% the following alternatives permit ../{name} i.e. braces

\def\usepath[#1]%
  {\def\docommand##1%
     {\doifelse{##1}\v!reset
        {\let\allinputpaths\empty}
        {\sanitizefilename#1\to\ascii
         \convertcommand\ascii\to\ascii
         \addtocommalist\ascii\allinputpaths}}%
   \processcommalist[#1]\docommand}

\def\usesubpath[#1]% test for a real long time, permits ../{name} i.e. braces
  {\ifx\allinputpaths\empty
     \sanitizefilename#1\to\allinputpaths
   \else
     \def\docommand##1%
       {\def\dodocommand####1%
          {\sanitizefilename####1\to\ascii
           \convertcommand\ascii\to\ascii
           \addtocommalist{##1/\ascii}\allinputpaths}%
        \processcommalist[#1]\dodocommand}%
     \processcommacommand[\allinputpaths]\docommand
   \fi}

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

\ifx\preloadfonts   \undefined \let\preloadfonts   \relax \fi
\ifx\preloadspecials\undefined \let\preloadspecials\relax \fi

\def\loadallsystemfiles#1#2%
  {\ifx\@@svdirectory\empty
     \readsysfile{#1}{\showmessage\m!systems2{#1}}{#2}%
   \else% yet undocumented
     \def\doloadsystemfile##1%
       {\readsetfile{##1}{#1}{\showmessage\m!systems2{#1}}{#2}}%
     \processcommacommand[\@@svdirectory]\doloadsystemfile
   \fi}

\ifx\disableXML\undefined \let\disableXML\relax \fi

\def\loadsystemfiles
  {\reportprotectionstate
   \readsysfile\f!newfilename{\showmessage\m!systems2\f!newfilename}\donothing
   \readsysfile\f!oldfilename{\showmessage\m!systems2\f!oldfilename}\donothing
   \loadallsystemfiles\f!filfilename
     \donothing
   \loadallsystemfiles\f!sysfilename
     {\loadallsystemfiles{\f!sysfilename.rme}\donothing % new, fall back
      \doglobal\appendtoks % brrr better \setcatcodetable\ctxcatcodes % % test
        \bgroup\disableXML\loadallsystemfiles\f!errfilename\donothing\egroup
      \to\everygoodbye}}

%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!systems2\f!usrfilename}\donothing
   \readjobfile\f!fmtfilename{\showmessage\m!systems2\f!fmtfilename}\donothing}

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

\def\loadoptionfile
  {\readjobfile{\jobname.\f!optionextension}
     {\showmessage\m!systems2{\jobname.\f!optionextension}}%
     {\writestatus\m!systems {no \jobname.\f!optionextension}}}

% \newevery \everyjob \EveryJob
% \appendtoks ... \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 \checknotes           \to \everyjob % depends on bodyfont
\appendtoks \initializeMPgraphics \to \everyjob % after loading system files

\appendtoks \page[\v!last] \page                  \to \everybye
\appendtoks \ifarrangingpages\poparrangedpages\fi \to \everybye
\appendtoks \registerfileinfo[end]\jobname        \to \everybye

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

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

\prependtoks \resetutilities  \to \everystarttext % moved 28-02-2002
\prependtoks \loadtwopassdata \to \everystarttext % moved 28-02-2002
\appendtoks  \checkreferences \to \everystarttext % new   04-12-1999

% Most natural ...
%
% \def\doateverystarttext
%   {\the\everystarttext
%    \global\let\doateverystarttext\relax}
%
% ... most practical, since we can load env's in a
% something.run file (nested \starttext's; see for
% instance x-res-08, where we definitely want to
% open the file!).

\def\doateverystarttext
  {\the\everystarttext
   \global\everystarttext\emptytoks}

\def\starttext
  {\doateverystarttext
   \ifnum\textlevel=\zerocount
     \registerfileinfo[begin]\jobname
     \expandafter\startcopyingblocks
   \fi
   \doglobal\increment\textlevel\relax}

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

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

\let\end\finalend

\def\emergencyend
  {\writestatus\m!systems{invalid \@EA\string\csname\e!start\v!text\endcsname...\@EA\string\csname\e!stop\v!text\endcsname\space structure}%
   \stoptext}

\def\currentfile{\inputfilename}

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

\def\doexecutefile#1%
  {\registerfileinfo[begin]{#1}%
   \dostarttextfile{#1}%
   \processfile{#1}%
   \dostoptextfile
   \registerfileinfo[end]{#1}}

\def\donotexecutefile#1%
  {}

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

\def\useenvironment[#1]% maybe commalist
  {\environment #1 \relax}

\def\environment #1 % at outermost level only (load only once)
  {\pushmacro\startenvironment
   \pushmacro\stopenvironment
   \def\startenvironment ##1 {}%
   \let\stopenvironment\relax
   \startreadingfile
   \doexecutefileonce{#1}
   \stopreadingfile
   \popmacro\stopenvironment
   \popmacro\startenvironment}

\def\component #1 % at outermost level only
  {\dostarttextfile{#1}%
   \processfile{#1}%
   \dostoptextfile}

\newcounter\filelevel

% \def\donextlevel#1#2#3#4#5#6\\%
%   {\beforesplitstring#6\at.\to#1
%    \ifcase\filelevel\relax
%      \starttext
%      \def\project     ##1 {#2{##1}}%
%      \def\environment ##1 {#3{##1}}%
%      \def\product     ##1 {#4{##1}}%
%      \def\component   ##1 {#5{##1}}%
%    \fi
%    \increment\filelevel\relax
%    \fullexpandoneargafter\addtocommalist{#1}\loadedfiles}
%
% \def\doprevlevel
%   {\ifnum\filelevel=\plusone
%      \expandafter\stoptext
%    \else
%      \decrement\filelevel\relax
%      \expandafter\endinput
%    \fi}

\let\currentcomponent    \v!text
\let\currentcomponentpath\f!currentpath

\def\donextlevel#1#2#3#4#5#6#7\\%
  {\pushmacro\currentcomponent
   \pushmacro\currentcomponentpath
   \let\currentcomponent#1%
   \setsystemmode\currentcomponent
   \splitfilename{#1}%
   \ifx\splitoffpath\empty
     \let\currentcomponentpath\f!currentpath
   \else
     \let\currentcomponentpath\splitoffpath
   \fi
   \beforesplitstring#7\at.\to#2\relax % can become path + base
   \ifcase\filelevel\relax
     \starttext
     \def\project     ##1 {#3{##1}}%
     \def\environment ##1 {#4{##1}}%
     \def\product     ##1 {#5{##1}}%
     \def\component   ##1 {#6{##1}}%
   \fi
   \increment\filelevel\relax
   \fullexpandoneargafter\addtocommalist{#1}\loadedfiles}

\def\doprevlevel
  {\popmacro\currentcomponentpath
   \popmacro\currentcomponent
   \setsystemmode\currentcomponent
   \ifnum\filelevel=\plusone
     \expandafter\stoptext
   \else
     \decrement\filelevel\relax
     \expandafter\endinput
   \fi}

% \def\startproject #1 %
%   {\donextlevel\currentproject
%      \donotexecutefile\doexecutefileonce
%      \doexecutefileonce\doexecutefile#1\\}
%
% \def\startproduct #1 %
%   {\doateverystarttext
%    \donextlevel\currentproduct
%      \doexecutefileonce\doexecutefileonce
%      \donotexecutefile\doexecutefile#1\\}
%
% \def\startcomponent #1 %
%   {\doateverystarttext
%    \donextlevel\currentcomponent
%      \doexecutefileonce\doexecutefileonce
%      \donotexecutefile\doexecutefile#1\\}
%
% \def\startenvironment #1 %
%   {\donextlevel\currentenvironment
%      \donotexecutefile\doexecutefileonce
%      \donotexecutefile\donotexecutefile#1\\}

\def\startproject #1 %
  {\donextlevel\v!project\currentproject
     \donotexecutefile\doexecutefileonce
     \doexecutefileonce\doexecutefile#1\\}

\def\startproduct #1 %
  {\doateverystarttext
   \donextlevel\v!product\currentproduct
     \doexecutefileonce\doexecutefileonce
     \donotexecutefile\doexecutefile#1\\}

\def\startcomponent #1 %
  {\doateverystarttext
   \donextlevel\v!component\currentcomponent
     \doexecutefileonce\doexecutefileonce
     \donotexecutefile\doexecutefile#1\\}

\def\startenvironment #1 %
  {\donextlevel\v!environment\currentenvironment
     \donotexecutefile\doexecutefileonce
     \donotexecutefile\donotexecutefile#1\\}

% \startproject test
%     1: \startmode[*project] project \stopmode \endgraf
%     2: \startmode[*product] product \stopmode \endgraf
% \stopproject

\def\stopproject    {\doprevlevel}
\def\stopproduct    {\doprevlevel}
\def\stopcomponent  {\doprevlevel}
\def\stopenvironment{\doprevlevel}

% more or less replaced by modes

\setvalue{\e!start\v!localenvironment}[#1]%
  {\let\loadedlocalenvironments\empty
   \def\docommand##1%
     {\beforesplitstring##1\at.\to\someevironment
      \fullexpandoneargafter\addtocommalist\someevironment\loadedlocalenvironments}%
   \processcommalist[#1]\docommand
   \fullexpandtwoargsafter\doifcommonelse % no longer next needed
       {\currentproject,\currentproduct,%
        \currentcomponent,\currentenvironment}
       {\loadedlocalenvironments}
     {\letvalue{\e!stop\v!localenvironment}\relax}
     {\grabuntil{\e!stop\v!localenvironment}\gobbleoneargument}} % TH: fixed, was \relax

\setvalue{\v!localenvironment}#1 {\doexecutefileonce{#1}}

% NOT TOEVOEGEN: \the\everytrace

\neverypar=\emptytoks

% \appendtoks \flushnotes                     \to \everypar
% \appendtoks \synchronizesidefloats          \to \everypar
% \appendtoks \checkindentation               \to \everypar
% \appendtoks \showparagraphnumber            \to \everypar
% \appendtoks \flushmargincontents            \to \everypar
% \appendtoks \flushcommentanchors            \to \everypar
% \appendtoks \synchronizenotes               \to \everypar

% \appendtoks \flushnotes                     \to \everydisplay
% \appendtoks \adjustsidefloatdisplaylines    \to \everydisplay

% soon, when pdftex 1.22 is out in the field:

\chardef\systemcommandmode\zerocount % 0=unknown 1=disabled 2=enabled

\ifx\pdfshellescape\undefined \else
  \prependtoks
    \chardef\systemcommandmode \ifcase\pdfshellescape \plusone \else \plustwo \fi
  \to \everyjob
\fi

\appendtoks
  \ifcase\systemcommandmode
  \or
    \writestatus\m!systems{system commands are disabled}%
  \or
    \writestatus\m!systems{system commands are enabled}%
  \fi
\to \everyjob

\ifx\etexversion\undefined \else \ifnum\etexversion<202
  \prependtoks
     \writestatus\m!systems{eTeX version \number\etexversion\space -> too old (bugs)}%
     \writeline
  \to \everyjob
\fi \fi

\ifx\pdftexversion\undefined \else \ifnum\number\pdftexversion<120
  \prependtoks
     \writestatus\m!systems{pdfTeX version \number\pdftexversion\space -> please update}%
     \writeline
  \to \everyjob
\fi \fi

\prependtoks \showcontextbanner \to \everyjob

% Default-instellingen (verborgen)

\resetutilities

\protect \endinput
