%D \module
%D   [       file=core-sys, % moved from main-001
%D        version=1997.03.31,
%D          title=\CONTEXT\ Core Macros,
%D       subtitle=System, 
%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.

\writestatus{loading}{Context Core Macros (System)}

\unprotect 

\def\outputfilename{\@@svfile} 
\def\inputfilename {\@@svinputfile} 

\let\jobfilesuffix\c!tex

\def\splitjobfilename
  {\resetsystemmode{suffix-\jobfilesuffix}%
   \edef\ascii{\inputfilename}\convertcommand\ascii\to\ascii
   \splitstring\ascii\at.\to\jobfilename\and\jobfilesuffix
   \lowercasestring\jobfilesuffix\to\jobfilesuffix
   \doifnothing\jobfilename  {\let\jobfilename  \jobname}%
   \doifnothing\jobfilesuffix{\let\jobfilesuffix\c!tex}%
   \setsystemmode{suffix-\jobfilesuffix}}

\appendtoks \splitjobfilename \to \everyjob

% Some mechanisms (see x-res-01) use either \jobfilename or 
% \jobfilename.somesuffix, in which case we need to use the 
% full name if given or a default (like \jobfilename.xml); 
% this comes down to replacing the default tex suffix. 

\def\jobfullname{\jobfilename.\jobfilesuffix} 

\def\setjobfullname#1% #1 = default if not given 
  {\doifelsenothing\jobfilename
     {\let\jobfullname\empty}                          
     {\doif\jobfilesuffix\c!tex{\edef\jobfullname{\jobfilename.#1}}}}

% ... 

\def\dosetupsystem[#1]%
  {\getparameters[\??sv][#1]%
   \setuprandomize[\@@svwillekeur]%
   \beforesplitstring\@@svresolutie\at dpi\to\@@svresolutie
   \let\outputresolution\@@svresolutie
   \ifcase\@@svn
     %                        % 0 : unknown 
   \or 
     \setsystemmode\v!eerste  % 1 : first run 
   \or 
     %                        % 2 : successive run 
   \or 
     \setsystemmode\v!eerste  % 3 : first and only run 
   \or
     \setsystemmode\v!laatste % 4 : extra last run 
   \fi 
   \splitjobfilename}

\def\setupsystem
  {\dosingleargument\dosetupsystem}

%D The system modes set by the setup command can be used in 
%D situations like: 
%D
%D \starttypen
%D \startmode[*first]
%D   \executesystemcommand{cleanupxml text.xml clean-text.xml}
%D \stopmode 
%D 
%D \starttext 
%D   \typefile{clean-text.xml}
%D \stoptext
%D \stoptypen 

\def\setuprandomize[#1]%
  {\doifsomething{#1}
     {\bgroup
      \setrandomseed\minusone % signals thrd-ran to reseed
      % tex's time is in minutes 
      \processaction
        [#1]
        [  \v!klein=>\divide\normaltime 15, %  900, 
          \v!middel=>\divide\normaltime 30, % 1800, 
           \v!groot=>\divide\normaltime 60, % 3600, 
         \v!normaal=>,
         \s!default=>,
         \s!unknown=>\normaltime=#1]%    
      \nextrandom   
      \egroup}}

\setupsystem
  [\c!gebied=,
   \c!n=0, % 0:unknown 1: one run 2: first 3: successive 4: final run 
   \c!resolutie=600dpi,
   \c!willekeur=,
   \c!file=\jobname,
   \c!inputfile=\outputfilename, 
   \c!korps=\normalizedlocalbodyfontsize] % of iets anders

%D

\def\dostartglobaldefs#1#2%
  {\edef\!!stringa{\the\globaldefs}%
   \ifnum\globaldefs#10
     \globaldefs-\globaldefs
   \fi
   \advance\globaldefs #21
   \setevalue{@gd@\the\globaldefs}{\!!stringa}}

\def\dostopglobaldefs
  {\doifdefinedelse{@gd@\the\globaldefs}
     {\globaldefs\getvalue{@gd@\the\globaldefs}\relax}
     {\globaldefs\zerocount}}

\def\startlocal  {\dostartglobaldefs>-}
\def\stoplocal   {\dostopglobaldefs}
\def\startglobal {\dostartglobaldefs<+}
\def\stopglobal  {\dostopglobaldefs}

\def\complexstart[#1]{\bgroup\getvalue{\e!start#1}}
\def\complexstop [#1]{\getvalue{\e!stop #1}\egroup}

\let\simplestart\bgroup
\let\simplestop \egroup

\definecomplexorsimple\start
\definecomplexorsimple\stop

\def\dododefinestartstop[#1][#2]%
  {\getparameters
     [\??be#1]
     [\c!voor=,
      \c!na=,
      \c!commandos=,
      \c!letter=,
      #2]%
   \unexpanded\setvalue{#1}%
     {\groupedcommand
        {\getvalue{\??be#1\c!commandos}%
         \dostartattributes{\??be#1}\c!letter\c!kleur}
        {\dostopattributes}}%
   \setvalue{\e!start#1}%
     {\getvalue{\??be#1\c!voor}%
      \bgroup
      \getvalue{\??be#1\c!commandos}%
      \dostartattributes{\??be#1}\c!letter\c!kleur\empty}%
   \setvalue{\e!stop#1}%
     {\dostopattributes
      \egroup
      \getvalue{\??be#1\c!na}}}

\def\dodefinestartstop[#1][#2]%
  {\def\docommando##1{\dododefinestartstop[##1][#2]}%
   \processcommalist[#1]\docommando}

\def\definestartstop
  {\dodoubleargument\dodefinestartstop}

\def\dosetupstartstop[#1][#2]%
  {\def\docommando##1{\getparameters[\??be##1][#2]}%
   \processcommalist[#1]\docommando}

\def\setupstartstop
  {\dodoubleargument\dosetupstartstop}

% \docommando kan niet worden gebruikt omdat deze macro
%  soms lokaal wordt gebruikt

% te zijner tijd:
%
% \definevariable {pc}  % ProtectedCommand
%
% \def\executeprotected#1%
%   {\csname\??pc\string#1\endcsname}
%
% \def\defineprotected#1#2%
%   {\expandafter\def\csname\??pc\string#2\endcsname}
%
% \def\defineunprotected#1%
%   {\def#1}
%
% \def\doprotected%
%   {\ifx\next\define
%      \let\next=\defineprotected
%    \else
%      \let\next=\executeprotected
%    \fi
%    \next}
%
% \def\unexpanded%
%   {\futurelet\next\doprotected}
%
% \unexpanded\define\ziezo{ziezo}
%
% \unexpanded\ziezo

\def\complexdefinieer[#1]#2#3%
  {\ifx#2\undefined
   \else
     \showmessage\m!systems4{\string#2}%
   \fi
   \ifcase0#1\def#2{#3}%
   \or\def#2##1{#3}%
   \or\def#2##1##2{#3}%
   \or\def#2##1##2##3{#3}%
   \or\def#2##1##2##3##4{#3}%
   \or\def#2##1##2##3##4##5{#3}%
   \or\def#2##1##2##3##4##5##6{#3}%
   \or\def#2##1##2##3##4##5##6##7{#3}%
   \or\def#2##1##2##3##4##5##6##7##8{#3}%
   \or\def#2##1##2##3##4##5##6##7##8##9{#3}%
   \else\def#2{#3}%
   \fi}

\definecomplexorsimpleempty\definieer

\unexpanded\def\naam#1% brrr
  {\getvalue{#1}}

\def\gebruikcommandos#1%
  {\bgroup
   \def\docommando##1{\setbox0\hbox{\getvalue{\string##1}##1}}%
   \processcommalist[#1]\docommando
   \egroup}

\newif\ifforcefileexpansion % handy for document level overload 

% \def\convertexpanded#1#2#3% watch the double \v!ja expansion ! 
%   {\ExpandFirstAfter\processaction
%      [\ifforcefileexpansion\v!ja\else\getvalue{#1\c!expansie}\fi]
%      [       \v!ja=>{{\honorunexpanded
%                       \dontexpandencoding % new 
%                       \xdef\@@globalexpanded{#2}%
%                       \xdef\@@globalexpanded{\@@globalexpanded}}%
%                       \convertcommand\@@globalexpanded\to#3},
%        \v!commando=>{\convertcommand #2\to#3},
%         \s!default=>{\convertargument#2\to#3},
%         \s!unknown=>{\convertargument#2\to#3}]}

%D The next implementation is about 4 times as fast on an 
%D string of average length. Since this feature is used in 
%D XML processing, it made sense to support this faster 
%D alternative.

\def\installexpander#1{\setvalue{\s!do\c!expansie#1}}

\long\def\convertexpanded#1#2#3% hm, first we need to make sure 
  {\csname   % that we assign all exp a value   
     \s!do\c!expansie
     \ifforcefileexpansion 
       \v!ja 
     \else\@EA\ifx\csname\s!do\c!expansie\csname#1\c!expansie\endcsname\endcsname\relax
       \s!default
     \else
       \csname#1\c!expansie\endcsname
     \fi\fi
   \endcsname#2\to#3}

\installexpander\v!ja      {\convertmeaning }
\installexpander\v!ja      {\convertmeaning }
\installexpander\v!commando{\convertcommand }
\installexpander\s!default {\convertargument}
\installexpander\empty     {\convertargument}
\installexpander\v!nee     {\convertargument}

\def\convertmeaning#1\to % watch the double expansion ! 
  {\bgroup
     \honorunexpanded
     \dontexpandencoding % new 
     \xdef\@@globalexpanded{#1}%
     \xdef\@@globalexpanded{\@@globalexpanded}%
   \egroup
   \convertcommand\@@globalexpanded\to}

% \setvalue{statevalue\v!stop   }{0}
% \setvalue{statevalue\v!start  }{1}
% \setvalue{statevalue\v!normaal}{2}
% \setvalue{statevalue\v!leeg   }{3}
% \setvalue{statevalue\v!geen   }{4}
%
% \def\setcurrentstate#1%
%   {\chardef\currentstate=0\getvalue{statevalue\getvalue{#1\c!status}\relax}
%
% \ifcase\currentstate ...

\def\herhaal            {\dorepeat}
\def\herhaler           {\repeater}
\def\herhaalmetcommando {\dorepeatwithcommand}

\protect \endinput 
