%D \module
%D   [       file=m-sync,
%D        version=2003.12.23,
%D          title=\CONTEXT\ Modules,
%D       subtitle=Editor Synchronization
%D         author={Hans Hagen \& Ton Otten},
%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.

% to do: proper hook into the special handler so that we can also
% support dvipdfmx

\ifx\pdfsavepos\undefined \endinput \fi

\unprotect

\ifx\editorsyncmode\undefined       \chardef\editorsyncmode=1 \fi
\ifx\edtsncout     \undefined \else \endinput                 \fi

\newwrite\edtsncout

\appendtoks
  \immediate\openout\edtsncout=\jobname.pdfsync
  \immediate\write\edtsncout{\jobname}
  \immediate\write\edtsncout{version unknown}
\to \everystarttext

\appendtoks
  \closeout\edtsncout
\to \everystoptext

\newcounter\editorsynchronization

\let\synchronizeeditortag\relax

\def\synchronizeeditor
  {\doglobal\increment\editorsynchronization
   \ifcase\editorsyncmode
     % disabled
   \or
     % compact mode
     \pdfsavepos
     \synchronizeeditortag
     \expanded
       {\write\edtsncout
          {s \purenumber\editorsynchronization\space
             \purenumber\inputlineno\space
             \noexpand\purenumber\realpageno\space
             \noexpand\purenumber\pdflastypos\space
             \noexpand\purenumber\pdflastxpos}}%
   \or
     % latex mode
      \pdfsavepos
      \synchronizeeditortag
      \expanded
        {\write\edtsncout
           {l \purenumber\editorsynchronization\space
              \purenumber\inputlineno\space}}%
      \expanded
        {\write\edtsncout
           {p \purenumber\editorsynchronization\space
              \noexpand\purenumber\pdflastypos\space
              \noexpand\purenumber\pdflastxpos}}%
   \fi}

\def\synchronizeeditortag
  {\iftracepositions
     \begingroup
     \incolortrue
     \llap{\infofont\backgroundline[black]{\white\editorsynchronization\space\the\inputlineno}\quad}%
     \endgroup
   \fi}

\appendtoksonce\synchronizeeditor\to\everypar

\protect \endinput

% \starttext
%   \dorecurse{100}{\input ward \par} \page
% \stoptext