%D \module
%D   [       file=typo-dif,
%D        version=2025.04.12,
%D          title=\CONTEXT\ Typesetting Macros,
%D       subtitle=Compare,
%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 Typesetting Macros / Compare Text}

% Currently only a color can be set but I might add support for underline
% and simple background.

%D Here is an example:
%D
%D \starttyping
%D \definecomparingtext[first] [color=blue]
%D \definecomparingtext[second][color=red]
%D \definecomparingtext[third] [color=magenta]
%D
%D \setuplayout[tight]
%D
%D \setuplayout
%D   [grid=yes,
%D    lines=40]
%D
%D \definecolumnset
%D   [comparethem]
%D   [n=2,
%D    align={lesswidows,lessclubs,lessbroken,lessorphans}]
%D
%D \definesubcolumnset[comparethem][L][1,3]
%D \definesubcolumnset[comparethem][R][2,4]
%D
%D \starttext
%D
%D \startcolumnset[comparethem]
%D
%D \dorecurse{10} {
%D
%D     \comparetext[first,second]
%D
%D     \startsubcolumnset[L]
%D         \startcomparingtext[first]%
%D             the {\it color} of spring and the odor of summer
%D         \stopcomparingtext
%D     \stopsubcolumnset
%D
%D     \startsubcolumnset[R]
%D         \startcomparingtext[second]%
%D             the {\green colour} of spring and the odour of summer
%D         \stopcomparingtext
%D     \stopsubcolumnset
%D
%D     \flushsubcolumnsets
%D
%D     \comparetext[first,third]
%D
%D     \startsubcolumnset[L]
%D         \startcomparingtext[first]%
%D             a country that is a pseudo two party democracy
%D         \stopcomparingtext
%D     \stopsubcolumnset
%D
%D     \startsubcolumnset[R]
%D         \startcomparingtext[third]%
%D             quickly becomes a country of destructive idiocracy
%D         \stopcomparingtext
%D     \stopsubcolumnset
%D
%D     \flushsubcolumnsets
%D
%D }
%D
%D \stopcolumnset
%D
%D \stoptext
%D \stoptyping

\unprotect

\registerctxluafile{typo-dif}{autosuffix}

\definesystemattribute[comparetext][public]

\installcorenamespace{comparingtext}
\installcorenamespace{comparingchannel}

\installcommandhandler \??comparingtext {comparingtext} \??comparingtext

\newinteger\c_comparetext

\appendtoks
    % if parent
    \advanceby\c_comparetext\plusone
    \permanent\immutable\expandafter\integerdef\csname\??comparingchannel\currentcomparingtext\endcsname\c_comparetext
    \clf_setupcomparingtext
       {\currentcomparingtext}%
       \c_comparetext
       {\comparingtextparameter\c!color}%
\to \everydefinecomparingtext

\permanent\protected\def\startcomparingtext
  {\begingroup
   \setcomparingtext}

\permanent\protected\def\stopcomparingtext
  {\endgroup}

\permanent\protected\def\setcomparingtext[#1]%
  {\cdef\currentcomparingtext{#1}%
   \ifx\currentcomparingtext\s!reset
     \resetcomparingtext
   \orelse\ifcsname\??comparingchannel\currentcomparingtext\endcsname
     \clf_setcomparingtext\lastnamedcs
   \else
     \resetcomparingtext
   \fi}

\permanent\protected\def\resetcomparingtext
  {\ifcase\c_attr_comparetext\else
     \clf_resetcomparingtext
   \fi}

\permanent\protected\def\comparetext[#1]%
  {\clf_setcomparetext
     {#1}%
     {\comparingtextparameter\c!method}%
     {\comparingtextparameter\c!color}}

\appendtoks
    \resetcomparingtext
\to \everyresettypesetting

\protect
