%D \module
%D   [       file=core-syn,
%D        version=1997.03.31,
%D          title=\CONTEXT\ Core Macros,
%D       subtitle=Synonyms and Sorts,
%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 / Synonyms and Sorts}

\unprotect

% \checkdefined kan hierheen

%  Formaat tex-utility-input-file <jobname.tui>:
%
%    synonym entry {tag} {pure} {text} {synonym}
%
%  Deze file wordt met het programma TeXUtil omgezet in een
%  in te lezen TeXFile met de commando's:
%
%    \synonymentry {tag} {pure} {text} {synonym}

\newif\ifsynonymmeaning

\def\dostelsynoniemenin[#1][#2]%
  {\getparameters[\??sm#1][#2]}

\def\stelsynoniemenin
  {\dodoubleargument\dostelsynoniemenin}

\def\doresetsynonym#1%
  {\letvalue{#1\s!entry}\gobblethreearguments}

\def\dohandlesynonymentry#1#2#3#4%
  {\bgroup
   \global\utilitydonetrue
   \syndef
     {\doattributes{\??sm#1}\c!tekstletter\c!tekstkleur{#3}}
     \ConvertToConstant\doifelse{#4}{}{\unknown}{#4}\par
   \egroup}

\def\synonymentry#1%
  {\executeifdefined{#1\s!entry}\gobblethreearguments}

\def\dosetsynonym#1%
  {\doifdefinedelse{\??sm#1\c!commando}
     {\setvalue{#1\s!entry}{\getvalue{\??sm#1\c!commando}}} % 3 argumenten
     {\setvalue{#1\s!entry}{\dohandlesynonymentry{#1}}}}

\def\doplaatslijstmetsynoniemen#1#2%
  {\witruimte
   \begingroup
   \def\currentsynonym{#1}%
%   \def\synplaats{\getvalue{\??sm#1\c!plaats}}%
%   \def\synbreedte{\getvalue{\??sm#1\c!breedte}}%
%   \def\synmonster{\getvalue{\??sm#1\c!monster}}%
%
   \doordefinieren  % nog eens een class van maken, net als framed
     [syndef]
     [\c!plaats=\getvalue{\??sm#1\c!plaats},
      \c!breedte=\getvalue{\??sm#1\c!breedte},
      \c!afstand=\getvalue{\??sm#1\c!afstand},
      \c!monster=\getvalue{\??sm#1\c!monster},
      \c!hang=\getvalue{\??sm#1\c!hang},
      \c!uitlijnen=\getvalue{\??sm#1\c!uitlijnen},
      \c!voor=\getvalue{\??sm#1\c!voor},
      \c!tussen=\getvalue{\??sm#1\c!tussen},
      \c!na=\getvalue{\??sm#1\c!na},
      \c!springvolgendein=\getvalue{\??sm#1\c!springvolgendein},
      \c!kopletter=,
      \c!letter=]%
%
   \setupwhitespace[\v!geen]%
   \doutilities{#1}\jobname{#2}\relax\par
   \endgroup
   \ifutilitydone\else\geenwitruimte\fi}

\def\dovolledigelijstmetsynoniemen#1#2% expansion needed to avoid v! (due to french active !)
  {\expanded
     {\plaatsvolledig
        {\noexpand\systemsuppliedchapter}%
        {#1}%
        {\noexpand\headtext{#2}}%
        {\noexpand\doplaatslijstmetsynoniemen{#1}{#2}}}}

\def\processsynonym#1#2#3%
  {\begingroup  % anders in mathmode lege \hbox, zie eenheden
   \ifsynonymmeaning
     \synonymmeaningfalse
     \doattributes{\??sm#1}\c!synoniemletter\c!synoniemkleur{#3}%
   \else
     \explicithmode
     \doattributes{\??sm#1}\c!tekstletter\c!tekstkleur{#2}%
   \fi
   \endgroup}

%\def\getsynonymmeaning#1#2#3%
%  {\doifdefinedelse{#2#3}
%     {{\synonymmeaningtrue\getvalue{#2#3}}}
%     {{\showmessage\m!systems{18}{#1,#3}}}}

\def\getsynonymmeaning#1#2#3%
  {\bgroup
   \doifundefined{#2#3}
     {\setgvalue{#2#3}{{\tt[#3]}}%
      \showmessage\m!systems{18}{#1,#3}}%
   \synonymmeaningtrue
   \getvalue{#2#3}%
   \egroup}

\def\dowritesynonym#1#2#3#4%
  {\begingroup  % anders in mathmode lege \hbox
   \convertexpanded{\??sm#1}{#3}\asciisynonym
   \convertexpanded{\??sm#1}{#4}\asciimeaning
   \immediatewriteutility{s e {#1} {#2} {\asciisynonym} {\asciimeaning}}%
   \endgroup}

\def\preexecutesynonym#1#2#3#4%
  {\ifdoinpututilities \else
     \dowritesynonym{#1}{#2}{#3}{#4}%
     \unexpanded\setgvalue{#2}{\processsynonym{#1}{#3}{#4}}%
   \fi}

\def\executesynonym#1#2#3#4%
  {\preexecutesynonym{#1}{#2}{#3}{#4}%
   \processsynonym{#1}{#3}{#4}}

\def\expandsynonym#1#2#3#4%
  {{\synonymmeaningtrue
    \processsynonym{#1}{#3}{#4}}}

\def\dodoloadsynonym#1#2#3#4%
  {\setgvalue{#2}{\executesynonym{#1}{#2}{#3}{#4}}}

\def\doloadsynonym#1%
  {\setvalue{#1\s!entry}##1##2##3%
     {\doifelsenothing{##1}
        {\dodoloadsynonym{#1}{##2}{##2}{##3}}
        {\dodoloadsynonym{#1}{##1}{##2}{##3}}%
      \global\utilitydonetrue}}

\def\dolaadsynoniemen#1#2%
  {\bgroup
   \let\dosetsynonym\doloadsynonym
   \showmessage\m!systems{19}{#2}%
   \doutilities{#1}\jobname\empty\relax\relax
   \egroup
   \setvalue{\s!check#1}##1{}}

\def\dodocomplexsynonym[#1][#2]#3#4%
  {\doifsomething{#2}
     {\getvalue{\s!check#1}{#2}%
      \doifelsevalue{\??sm#1\c!conversie}\v!ja
        {\unexpanded\setgvalue{#2}{\expandsynonym{#1}{#2}{#3}{#4}}}
        {\doifelsevalue{\??sm#1\c!status}\v!start
           {\doifelsevalue{\??sm#1\c!criterium}\v!alles
              {\preexecutesynonym{#1}{#2}{#3}{#4}}
              {\unexpanded\setgvalue{#2}{\executesynonym{#1}{#2}{#3}{#4}}}}
           {\unexpanded\setgvalue{#2}{\processsynonym{#1}{#3}{#4}}}}}}

\def\docomplexsynonym[#1][#2][#3]#4#5%
  {\ifthirdargument
     \dodocomplexsynonym[#2][#1#3]{#4}{#5}%
   \else
     \dodocomplexsynonym[#2][#1#4]{#4}{#5}%
   \fi}

\def\dodefinieersynoniemen[#1][#2][#3][#4]%
  {\setvalue{\e!stel#2\e!in}%
     {\dodoubleargument\getparameters[\??sm#1]}%
   \iffourthargument
     \unexpanded\def#4##1%
       {\getsynonymmeaning{#1}{\??sm:#1:}{##1}}%
     \ifthirdargument
       \unexpanded\def#3##1%
         {\getvalue{\??sm:#1:##1}}%
     \fi
     \setvalue{#1}%
       {\dotripleempty\docomplexsynonym[\??sm:#1:][#1]}%
   \else
     \ifthirdargument
       \unexpanded\def#3##1%
         {\getsynonymmeaning{#1}{}{##1}}%
     \fi
     \setvalue{#1}%
       {\dotripleempty\docomplexsynonym[][#1]}%
   \fi
   \dostelsynoniemenin
     [#1]%
     [\c!synoniemletter=,
      \c!tekstletter=,
      \c!status=\v!start,
      \c!criterium=,
      \c!plaats=\v!links,
      \c!breedte=5em,
      \c!afstand=0pt,
      \c!monster=,
      \c!hang=,
      \c!uitlijnen=,
      \c!voor=,
      \c!tussen=,
      \c!na=,
      \c!springvolgendein=\v!nee,
      \c!expansie=]%
   \presetheadtext % changes the \if...argument
     [#2=\Word{#2}]%
   \setvalue{\s!set#1}%
     {\dosetsynonym{#1}}%
   \setvalue{\s!reset#1}%
     {\doresetsynonym{#1}}%
   \setvalue{\s!check#1}##1%
     {\checkdefined{synoniemen}{#1}{##1}}%
   \addutilityreset{#1}%
   \setvalue{\e!laad#2}%
     {\dolaadsynoniemen{#1}{#2}}%
   \setvalue{\e!plaats\e!lijstmet#2}%
     {\doplaatslijstmetsynoniemen{#1}{#2}}%
   \setvalue{\e!volledige\e!lijstmet#2}%
     {\dovolledigelijstmetsynoniemen{#1}{#2}}}

\def\definieersynoniemen
  {\doquadrupleempty\dodefinieersynoniemen}

%  Formaat tex-utility-input-file <jobname.tui>:
%
%    synonym entry {tag} {pure} {text} {}
%
%  Deze file wordt met het programma TeXUtil omgezet in een
%  in te lezen TeXFile met de commando's:
%
%    \synonymentry {tag} {pure} {text} {}

\def\dostelsorterenin[#1][#2]%
  {\getparameters[\??so#1][#2]}

\def\stelsorterenin
  {\dodoubleargument\dostelsorterenin}

\def\doresetsorteren#1%
  {\letvalue{#1\s!entry}\gobblethreearguments}

\def\dosetsorteren#1%
  {\setvalue{#1\s!entry}##1##2##3%
     {\def\dowritesort####1####2####3{}%
      \global\utilitydonetrue
      \bgroup
      \doifdefinedelse{\??so#1\c!commando}
        {\getvalue{\??so#1\c!commando}{##2}}  % 1 argument
        {\getvalue{\??so#1\c!voor}%
         \doattributes{\??so#1}\c!letter\c!kleur{##2}%
         \getvalue{\??so#1\c!na}}%
      \egroup}}

\def\doplaatslijstmetsorteren#1% NOG EEN RUWE VERSIE MAKEN
  {\witruimte                  % ZONDER WITRUIMTE ETC ETC
   \begingroup
   \setupwhitespace[\v!geen]%
   \doutilities{#1}\jobname{#1}\relax\par
   \endgroup
   \ifutilitydone\else\geenwitruimte\fi}

\def\dovolledigelijstmetsorteren#1#2%
  {\plaatsvolledig
     {\systemsuppliedchapter}{#1}{\headtext{#2}}
     {\doplaatslijstmetsorteren{#1}}}

\def\processsort#1#2#3%
  {\explicithmode
   \bgroup
   \doattributes{\??so#1}\c!letter\c!kleur{#2}%
   \egroup}

\def\dowritesort#1#2#3%
  {\bgroup
   \convertexpanded{\??so#1}{#3}\asciisynonym
   \immediatewriteutility{s e {#1} {#2} {\asciisynonym} {}}%
   \egroup}

\def\synonymentry#1%
  {\executeifdefined{#1\s!entry}\gobblethreearguments}

\def\preexecutesort#1#2#3%
  {\ifdoinpututilities \else
     \dowritesort{#1}{#2}{#3}%
     \unexpanded\setgvalue{#2}{\processsort{#1}{#3}{#2}}%
   \fi}

% \def\executesort#1#2#3%
%   {\preexecutesort{#1}{#2}{#3}%
%    \processsort{#1}{#3}{#2}}
%
% Better because it catches nested logo's:

\def\executesort#1#2#3%
  {\bgroup
   \def\executesort##1##2##3{##3}% Trick needed for nested logo's.
   \preexecutesort{#1}{#2}{#3}%
   \processsort{#1}{#3}{#2}%
   \egroup}

\def\doloadsort#1%
  {\setvalue{#1\s!entry}##1##2##3%
     {\setgvalue{##1}{##2}%
      \global\utilitydonetrue}}

\def\dolaadsorteren#1#2%
  {\bgroup
   \let\dosetsorteren\doloadsort
   \showmessage\m!systems{20}{#2}%
   \doutilities{#1}\jobname\empty\relax\relax
   \egroup
   \setvalue{\s!check#1}##1{}}

\def\dodocomplexsort[#1][#2]#3%
  {\doifsomething{#2}
     {\getvalue{\s!check#1}{#2}%
      \doifelsevalue{\??so#1\c!status}\v!start
        {\doifelsevalue{\??so#1\c!criterium}\v!alles
           {\preexecutesort{#1}{#2}{#3}}
           {\unexpanded\setgvalue{#2}{\executesort{#1}{#2}{#3}}}}
        {\unexpanded\setgvalue{#2}{\processsort{#1}{#3}{#2}}}}}

\def\docomplexsort[#1][#2][#3]#4%
  {\ifthirdargument
     \dodocomplexsort[#2][#1#3]{#4}%
   \else
     \dowritesort{#2}{#4}{#4}%
   \fi}

% if #3=\relax or \v!geen, then no command but still protected

\def\dodefinieersorteren[#1][#2][#3]%
  {\getparameters[\??so#1]
     [%\c!commando=, % we test for defined !
      \c!status=\v!start,
      \c!criterium=,
      \c!letter=,
      \c!voor=,
      \c!na=\endgraf,
      \c!expansie=]%
   \presetheadtext[#2=\Word{#2}]%
   \setvalue{\e!stel#2\e!in}[##1]% vervalt tzt, soort oo-mode
     {\getparameters[\??so#1][##1]}%
   \ifthirdargument
     \ConvertConstantAfter\doifnot{#3}\v!geen
       {\ifx#3\relax \else
          \def#3##1{\getvalue{\??so:#1:##1}}
        \fi}%
     \setvalue{#1}%
       {\dotripleempty\docomplexsort[\??so:#1:][#1]}%
   \else
     \setvalue{#1}%
       {\dotripleempty\docomplexsort[][#1]}%
   \fi
   \setvalue{\s!set#1}%
     {\dosetsorteren{#1}}%
   \setvalue{\s!reset#1}%
     {\doresetsorteren{#1}}%
   \addutilityreset{#1}%
   \setvalue{\e!laad#2}%
     {\dolaadsorteren{#1}{#2}}%
   \setvalue{\s!check#1}##1%
     {\checkdefined{sorteren}{#1}{##1}}%
   \setvalue{\e!plaats\e!lijstmet#2}%
     {\doplaatslijstmetsorteren{#1}}%
   \setvalue{\e!volledige\e!lijstmet#2}%
     {\dovolledigelijstmetsorteren{#1}{#2}}}

\def\definieersorteren%
  {\dotripleempty\dodefinieersorteren}

\definieersynoniemen
  [\v!afkorting]
  [\v!afkortingen]
  [\voluit]

\stelsynoniemenin
  [\v!afkorting]
  [\c!tekstletter=\v!kapitaal,
   \c!synoniemletter=,
   \c!tekstkleur=,
   \c!synoniemkleur=,
   \c!plaats=\v!links,
   \c!breedte=5em,
   \c!status=\v!start]

\definieersorteren
  [\v!logo]
  [\v!logos]

\definieersynoniemen
  [\v!eenheid]
  [\v!eenheden]
  [\betekenis]

\stelsynoniemenin
  [\v!eenheid]
  [\c!tekstletter=\dimension]

\protect \endinput
