%D \module
%D   [       file=cont-run,
%D        version=2014.12.26,
%D          title=\CONTEXT\ Core Macros,
%D       subtitle=Runner,
%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.

\writestatus{loading}{ConTeXt Core Macros / Runner}

\registerctxluafile{cont-run}{autosuffix}

% \enabletrackers[system.synctex.visualize]
% \enabletrackers[system.synctex.xml]
% \enabledirectives[system.synctex.details]
% \setupsynctex[state=start]  % works in most cases (old libs)
% \setupsynctex[state=repeat] % in case the lib is new

\unprotect

% \synctexsetfilename   % defined at lua end
% \synctexresetfilename % idem
% \synctexblockfilename % idem

\permanent\protected\lettonothing\synctexpause
\permanent\protected\lettonothing\synctexresume
\permanent\protected\lettonothing\synctexpushline
\permanent\protected\lettonothing\synctexpopline

\appendtoks\synctexpause \to\everybeforeoutput
\appendtoks\synctexresume\to\everyafteroutput

\lettonothing \p_state

\permanent\protected\def\setupsynctex[#1]%
  {\begingroup
   \getdummyparameters[\c!state=\v!stop,\c!method=\v!max,#1]%
   \edef\p_state{\dummyparameter\c!state}%
   \clf_setupsynctex
      state  {\p_state}%
      method {\dummyparameter\c!method}%
   \relax
   \ifx\p_state\v!repeat
     \let\p_state\v!start
   \fi
   \ifx\p_state\v!start
     % when public implementor, use conditional
     \enforced\glet\synctexpause   \clf_synctexpause
     \enforced\glet\synctexresume  \clf_synctexresume
     \enforced\glet\synctexpushline\clf_synctexpushline
     \enforced\glet\synctexpopline \clf_synctexpopline
   \fi
   \endgroup}

\permanent\protected\def\blocksynctexfile[#1]%
  {\processcommacommand{#1}\synctexblockfilename}

\permanent\protected\lettonothing\continuewhenlmtxmode

\permanent\protected\def\continuewhenmkivmode
  {\starttext
   \startTEXpage[\c!offset=2\exheight,\c!foregroundstyle=\tttf]%
      This file needs \LuaTeX\ and \ConTeXt\ \MKIV.%
   \stopTEXpage
   \stoptext
   \endinput}

%D Control (this might become nicer):

\newtoks\t_syst_every_export_images
\newtoks\t_syst_every_export_content

\permanent\protected\def\exportimages {\expand\t_syst_every_export_images}
\permanent\protected\def\exportcontent{\expand\t_syst_every_export_content}

\appendtoks
    \setupbackend[\c!export=\v!yes]%
    \enabledirectives[export.images]%
    \enablemode[exportimages]%
    \collectexported
    \usemodule[export-todo]%
\to \t_syst_every_export_images

\appendtoks
    \setupbackend[\c!export=\v!yes]%
    \usemodule[export-todo]%
\to \t_syst_every_export_content

% \appendtoks
%     \ifcstok{\backendparameter\c!export}\v!yes
%       \doifmode {exportimages} {%
%          \enabledirectives[export.images]%
%          \collectexported
%       }%
%     \fi
% \to \everysetupbackend

\protect \endinput
