%D \module
%D   [       file=file-job, % copied from main-001, later core-job
%D        version=1997.03.31,
%D          title=\CONTEXT\ Core Macros,
%D       subtitle=Job Handling,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%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 File Macros / Job Handling}

\unprotect

\registerctxluafile{file-job}{1.001}

% processing

\unexpanded\def\doifinputfileelse#name%
  {\ctxcommand{doifinputfileelse("#name")}}

\unexpanded\def\processfile#name%
  {\ctxcommand{processfile("#name")}}

% path control

\unexpanded\def\usepath[#path]%
  {\ctxcommand{usepath("#path")}}

\unexpanded\def\usesubpath[#path]%
  {\ctxcommand{usesubpath("#path")}}

\def\allinputpaths
  {\ctxcommand{allinputpaths()}}

% helper (not really needed nowadays)

\let\locatedfilepath\empty

\unexpanded\def\locatefilepath#name% no [] ?
  {\edef\locatedfilepath{\ctxcommand{locatefilepath("#name")}}}

% zip files are tree'd

\unexpanded\def\usezipfile
  {\dodoubleempty\syst_files_use_zip_file}

\def\syst_files_use_zip_file[#zipname][#subtree]%
  {\ctxcommand{usezipfile("#zipname","#subtree")}}

% exa stuff might become obsolete:

\unexpanded\def\loadexamodes
  {\dosingleempty\syst_files_load_exa_modes}

\def\syst_files_load_exa_modes[#name]%
  {\ctxcommand{loadexamodes("#name")}}

% runtime files (maybe also do this in lua?)

% \def\doloadsystemfile#1% only mkiv files
%   {\readfile{sys:///#1.\mksuffix}{\showmessage\m!system2{#1.\mksuffix}}\donothing}

\unexpanded\def\loadsystemfiles
  {\syst_files_load\f!newfilename  % new code, to be integrated at some point, plus fixes posted on the list
   \syst_files_load\f!locfilename  % new code, somewhat experimental, not distributed (outside the dev group)
   \syst_files_load\f!expfilename  % new code, very experimental, can be engine specific, mostly for me only
   \syst_files_load\f!sysfilename} % local settings, but probably not that good an idea to use

\def\syst_files_load#name% only mkiv files
  {\readsysfile{#name.\mksuffix}{\showmessage\m!system2{#name.\mksuffix}}\donothing}

% obsolete, but we keep it as reference of what happened
%
% \unexpanded\def\loadoptionfile
%   {\readjobfile{\jobname.\f!optionextension}
%      {\writestatus\m!system{\jobname.\f!optionextension\space loaded}%
%       \ctxcommand{copyfiletolog("\jobname.\f!optionextension")}}%
%      {\writestatus\m!system{no \jobname.\f!optionextension}}}

% document structure

\ifdefined\textlevel                         \else \newcount\textlevel                          \fi % might go away
\ifdefined\strc_pagenumbers_flush_final_page \else \let\strc_pagenumbers_flush_final_page\relax \fi % ugly

\unexpanded\def\dostarttext
  {\glet\dostarttext\relax
   \the\everystarttext
   \global\everystarttext\emptytoks}

\unexpanded\def\dostoptext
  {\glet\dostoptext\relax
   \strc_pagenumbers_flush_final_page
   \page % hm, bonus
   \the\everystoptext
   \global\everystoptext\emptytoks
   \the\everybye
   \global\everybye\emptytoks
   \the\everygoodbye
   \global\everygoodbye\emptytoks
   \doifsometokselse\everynotabene{\writeline\the\everynotabene\writeline}\donothing
   \normalend}           % tex's \end

\unexpanded\def\starttext    {\ctxcommand{starttext()}}
\unexpanded\def\stoptext     {\ctxcommand{stoptext()}}
\unexpanded\def\forcequitjob {\ctxcommand{forcequitjob()}}
\unexpanded\def\end          {\ctxcommand{forceendjob()}}
\unexpanded\def\autostarttext{\ctxcommand{autostarttext()}}
\unexpanded\def\autostoptext {\ctxcommand{autostoptext()}}

\unexpanded\def\finishjob{\stoptext} % nicer in luatex call commandline

\newtoks\everystartnotext
\newtoks\everystopnotext

\unexpanded\def\startnotext  {\the\everystartnotext}
\unexpanded\def\stopnotext   {\the\everystopnotext \normalend}

% protect structure

\unexpanded\def\processfilemany#name{\ctxcommand{processfilemany("#name")}}
\unexpanded\def\processfileonce#name{\ctxcommand{processfileonce("#name")}}
\unexpanded\def\processfilenone#name{\ctxcommand{processfilenone("#name")}}

\unexpanded\def\project    {\doifnextoptionalelse\useproject    \syst_structure_arg_project}
\unexpanded\def\product    {\doifnextoptionalelse\useproduct    \syst_structure_arg_product}
\unexpanded\def\component  {\doifnextoptionalelse\usecomponent  \syst_structure_arg_component}
\unexpanded\def\environment{\doifnextoptionalelse\useenvironment\syst_structure_arg_environment}

\def\syst_structure_arg_project    #name {\ctxcommand{useproject    ("#name")}}
\def\syst_structure_arg_product    #name {\ctxcommand{useproduct    ("#name")}}
\def\syst_structure_arg_component  #name {\ctxcommand{usecomponent  ("#name")}}
\def\syst_structure_arg_environment#name {\ctxcommand{useenvironment("#name")}}

\unexpanded\def\startproject    {\doifnextoptionalelse\syst_structure_start_opt_project    \syst_structure_start_arg_project    }
\unexpanded\def\startproduct    {\doifnextoptionalelse\syst_structure_start_opt_product    \syst_structure_start_arg_product    }
\unexpanded\def\startcomponent  {\doifnextoptionalelse\syst_structure_start_opt_component  \syst_structure_start_arg_component  }
\unexpanded\def\startenvironment{\doifnextoptionalelse\syst_structure_start_opt_environment\syst_structure_start_arg_environment}

\def\syst_structure_start_arg_project    #name {\ctxcommand{startproject    ("#name")}}
\def\syst_structure_start_arg_product    #name {\ctxcommand{startproduct    ("#name")}}
\def\syst_structure_start_arg_component  #name {\ctxcommand{startcomponent  ("#name")}}
\def\syst_structure_start_arg_environment#name {\ctxcommand{startenvironment("#name")}}

\unexpanded\def\useproject    [#name]{\ctxcommand{useproject    ("#name")}}
\unexpanded\def\useproduct    [#name]{\ctxcommand{useproduct    ("#name")}}
\unexpanded\def\usecomponent  [#name]{\ctxcommand{usecomponent  ("#name")}}
\unexpanded\def\useenvironment[#name]{\ctxcommand{useenvironment("#name")}}

\unexpanded\def\syst_structure_start_opt_project    [#name]{\ctxcommand{startproject    ("#name")}}
\unexpanded\def\syst_structure_start_opt_product    [#name]{\ctxcommand{startproduct    ("#name")}}
\unexpanded\def\syst_structure_start_opt_component  [#name]{\ctxcommand{startcomponent  ("#name")}}
\unexpanded\def\syst_structure_start_opt_environment[#name]{\ctxcommand{startenvironment("#name")}}

\newconditional\endofinput % hack, needed because \endinput happens one level down in the input when we write from lua

\unexpanded\def\signalendofinput#1% argument just for tracing
  {\global\settrue\endofinput}

\unexpanded\def\honorendofinput
  {\ifconditional\endofinput
     \global\setfalse\endofinput
     \endinput
   \fi}

\unexpanded\def\stopproject              {\ctxcommand{stopproject    ()}\honorendofinput}
\unexpanded\def\stopproduct              {\ctxcommand{stopproduct    ()}\honorendofinput}
\unexpanded\def\stopcomponent            {\ctxcommand{stopcomponent  ()}\honorendofinput}
\unexpanded\def\stopenvironment          {\ctxcommand{stopenvironment()}\honorendofinput}

\def\currentproject                      {\ctxcommand{currentproject    ()}}
\def\currentproduct                      {\ctxcommand{currentproduct    ()}}
\def\currentcomponent                    {\ctxcommand{currentcomponent  ()}}
\def\currentenvironment                  {\ctxcommand{currentenvironment()}}
\def\processedfile                       {\ctxcommand{processedfile()}}
\def\processedfiles                      {\ctxcommand{processedfiles()}}

\unexpanded\def\dostarttextfile     #name{\ctxcommand{dostarttextfile(name)}}
\unexpanded\def\dostoptextfile           {\ctxcommand{dostoptextfile()}}

\unexpanded\def\loadtexfile       [#name]{\ctxcommand{usetexfile("#name")}}
\unexpanded\def\loadluafile       [#name]{\ctxcommand{useluafile("#name")}}
\unexpanded\def\loadcldfile       [#name]{\ctxcommand{usecldfile("#name")}}
\unexpanded\def\loadanyfile       [#name]{\ctxcommand{useanyfile("#name")}}

\unexpanded\def\loadtexfileonce   [#name]{\ctxcommand{usetexfile("#name",true)}}
\unexpanded\def\loadluafileonce   [#name]{\ctxcommand{useluafile("#name",true)}}
\unexpanded\def\loadcldfileonce   [#name]{\ctxcommand{usecldfile("#name",true)}}
\unexpanded\def\loadanyfileonce   [#name]{\ctxcommand{useanyfile("#name",true)}}

%D Handy for modules that have a test/demo appended (true added).

\unexpanded\def\continueifinputfile#name{\ctxcommand{doifelsecontinuewithfile("#name",true)}\relax\endinput} % we cannot do \endinput via lua

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

% replaced by modes:
%
% \setvalue{\e!start\v!localenvironment}[#1]%
%   {\let\loadedlocalenvironments\empty
%    \def\docommand##1%
%      {\beforesplitstring##1\at.\to\someevironment
%       \addtocommalist\someevironment\loadedlocalenvironments}%
%    \processcommalist[#1]\docommand
%    \doifcommonelse{\currentproject,\currentproduct,\currentcomponent,\currentenvironment}\loadedlocalenvironments
%      {\letvalue{\e!stop\v!localenvironment}\relax}
%      {\grabuntil{\e!stop\v!localenvironment}\gobbleoneargument}}
%
% \setvalue{\v!localenvironment}{\doifnextoptionalelse\uselocalenvironment\redolocalenvironment}
%
% \def\redolocalenvironment#1 {\uselocalenvironment[#1]}
% \def\uselocalenvironment[#1]{\doexecutefileonce{#1}}

% weird place:

\neverypar\emptytoks

%D Wrappers (the signal is a synchronization with lua hack):

\newsystemmode\v!project
\newsystemmode\v!product
\newsystemmode\v!component
\newsystemmode\v!environment

\unexpanded\def\startprojectindeed
  {\starttext
   \pushsystemmode\v!project
   \setsystemmode\v!project}

\unexpanded\def\stopprojectindeed
  {\popsystemmode\v!project
   \stoptext
   \signalendofinput\v!project}

\unexpanded\def\startproductindeed
  {\starttext
   \pushsystemmode\v!product
   \setsystemmode\v!product}

\unexpanded\def\stopproductindeed
  {\popsystemmode\v!product
   \stoptext
   \signalendofinput\v!product}

\unexpanded\def\startcomponentindeed
  {\starttext
   \pushreferenceprefix\currentcomponent
   \pushsystemmode\v!component
   \setsystemmode\v!component}

\unexpanded\def\stopcomponentindeed
  {\popsystemmode\v!component
   \popreferenceprefix
   \stoptext
   \signalendofinput\v!component}

\unexpanded\def\startenvironmentindeed
  {\pushsystemmode\v!environment
   \setsystemmode\v!environment}

\unexpanded\def\stopenvironmentindeed
  {\popsystemmode\v!environment
   \signalendofinput\v!environment}

%D Relatively new (might move as it depends on setups):

\newtoks\everysetupdocument

\unexpanded\def\startdocument % todo: dostarttagged\t!document
  {\dosingleargument\syst_structure_document_start}

\unexpanded\def\syst_structure_document_start[#settings]%
  {\setvariables[\s!document][#settings]%
   \the\everysetupdocument\relax
   \starttext
   \documentvariable\c!before}

\unexpanded\def\stopdocument % todo: dostoptagged\t!document
  {\documentvariable\c!after
   \stoptext}

\def\documentvariable#name%
  {\getvariable\s!document{#name}}

\unexpanded\def\setupdocument[#settings]%
  {\setvariables[\s!document][#settings]%
   \the\everysetupdocument\relax}

% metadata:author  metadata:title  metadata:subject

\setvariables
  [document]
  [\c!before=\directsetup{\s!document:start},
   \c!after=\directsetup{\s!document:stop}]

%S The document: namespace will be used elsewhere too.

\protect \endinput
