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

\startmessages  dutch  library: layouts
      9: momenteel maximaal -- niveaus in opsommingen
\stopmessages

\startmessages  english  library: layouts
      9: currently no more than -- levels in itimezations
\stopmessages

\startmessages  german  library: layouts
      9: z.Z. nicht mehr als -- Niveaus in Posten
\stopmessages

\startmessages  czech  library: layouts
      9: aktualne ne vice nez -- urovne/urovni vyctu
\stopmessages

\unprotect 

% - meerdere niveaus (moet niet moeilijk zijn)
% - instellingen in macro
% - \defineitemize
% - [0] voor start op 0

\newif\ifsubsom         \subsomfalse
\newif\ifsymsom         \symsomfalse
\newif\ifkopsom         \kopsomfalse
\newif\ifsomintro       \somintrofalse
\newif\ifsomautointro   \somautointrofalse
\newif\ifoptimizeitems  \optimizeitemstrue
\newif\ifpackeditems    \packeditemsfalse
\newif\iffirstlist      \firstlistfalse
\newif\ifparagraphitems \paragraphitemsfalse

\newcounter \itemcolumndepth

\definetwopasslist{\s!list}

\newcounter\noflists
\newcounter\itemlevel

\def\dolistreference%
  {\immediatewriteutilitycommand%
     {\twopassentry%
        {\s!list}%
        {\currentlist}%
        {\currentlist:\noflistelements}}}

\def\setnextitemlevel#1%
  {\doifundefined{\??op#1\c!breedte}
     {\edef\itemreferences{\itemreferences,#1}%
      \copyparameters
        [\??op#1][\??oo]
        [\c!breedte,\c!factor,\c!afstand,
         \c!letter,\c!marletter,\c!symletter,\c!kopletter,
         \c!kleur,\c!markleur,\c!symkleur,\c!kopkleur,
         \c!kopvoor,\c!kopna,\c!voor,\c!tussen,\c!na,
         \c!afsluiter,\c!plaatsafsluiter,\c!inspringen, 
         \c!n,\c!binnen,\c!symbool,\c!marge]%
      \makecounter{\s!itemcount#1}%
      \setvalue{\??op\c!symbool\s!global#1}{#1}}}

\def\maxitemlevel{0}
\def\itemreferences{0}

\def\dostelopsommingenin[#1]%  % still undocumented
  {\getparameters[\??oo][\c!niveaus=4,#1]%
   \ifnum\@@ooniveaus>\maxitemlevel
     \edef\maxitemlevel{\@@ooniveaus}%
    %\herhaal[\maxitemlevel*\setnextitemlevel{\herhaler}]%
     \dorecurse{\maxitemlevel}{\setnextitemlevel\recurselevel}%
   \fi}

\def\stelopsommingenin%
  {\dosingleargument\dostelopsommingenin}

\def\doitemreference#1,#2,#3\\%
  {\ifnum\itemlevel>#1
     \ifnum#1>0
       \tempsymbool
     \fi
     \getvalue{\??op\c!symbool#2}%
     \doitemreference#2,#3\\%
   \fi}

\def\itemreference%
  {\expandafter\doitemreference\itemreferences,,\\}

\def\itemuse#1%
  {\getvalue{\??op\itemlevel#1}}%

\def\packitems%
  {\ifnum\itemlevel=0 \else\packeditemstrue\fi}

\def\dostelopsomminginvariable[#1][#2]%  niveau instellingen
  {\doifelsenothing{#1}
     {\getparameters[\??op\itemlevel][#2]}%
     {\getparameters[\??op#1][#2]}}

\def\dododostelopsomminginconstant[#1][#2#3#4]% * permits [2]
  {\processaction
     [#2#3#4]
     [   \v!opelkaar*=>\packitems,
        \v!vanelkaar*=>\packeditemsfalse,
            \v!intro*=>\somintrotrue,
        \v!autointro*=>\somautointrotrue,
             \v!ruim*=>{\doassign[\??op#1][\c!factor=1]},
        #2#3*\v!ruim*=>{\doassign[\??op#1][\c!factor=#2#3]},
          #2*\v!ruim*=>{\doassign[\??op#1][\c!factor=#2]},
            \v!marge*=>{\doassign[\??op#1][\c!breedte=-2em]},               % signal
          \v!inmarge*=>{\doassign[\??op#1][\c!breedte=-2em]},               % signal
          \v!opmarge*=>\doifnot{#1}{1}{\doassign[\??op#1][\c!breedte=0em]}, % signal
         \v!kolommen*=>\packitems,
              \v!los*=>\optimizeitemsfalse,
           \v!alinea*=>{\paragraphitemstrue
                        \packitems},
      \v!aansluitend*=>{\getparameters[\??op#1]
                          [\c!kopvoor=,\c!kopna=,
                           \c!voor=,\c!tussen=,\c!na=]%
                        \packitems},
        \v!aanelkaar*=>{\doassign[\??op#1][\c!factor=-1]},
   #2#3*\v!aanelkaar*=>{\doassign[\??op#1][\c!factor=-#2#3]},
     #2*\v!aanelkaar*=>{\doassign[\??op#1][\c!factor=-#2]},
        \v!afsluiter*=>{\doassign[\??op#1][\c!plaatsafsluiter=\v!ja]},
        \v!standaard*=>{\getparameters[\??op#1]
                          [\c!breedte=1.5em,
                           \c!factor=0,
                           \c!afstand=.5em,
                           \c!kopvoor=,
                           \c!kopna=\blanko,
                           \c!voor=\blanko,
                           \c!tussen=\blanko,
                           \c!na=\blanko,
                           \c!binnen=]}]}

\def\dostelopsomminginconstant[#1][#2]%
  {\def\dodostelopsomminginconstant##1%
     {\dododostelopsomminginconstant[#1][##1*]}%
   \processcommalist[#2]\dodostelopsomminginconstant}

\def\dodododostelopsommingin[#1][#2]%
  {\ConvertToConstant\doifinstringelse{=}{#2}
     {\dostelopsomminginvariable[#1][#2]}
     {\setvalue{\??op#1}{\dostelopsomminginconstant[#1][#2]}}}%

\def\dododostelopsommingin[#1][#2]%
  {\ConvertToConstant\doifnot{#2}{}
     {\doifelse{#1}{\v!elk}
        {\herhaal[\maxitemlevel*{\ExpandFirstAfter\dodododostelopsommingin[\herhaler][#2]}]}
%        {\dorecurse{\maxitemlevel}{\ExpandFirstAfter\dodododostelopsommingin[\recurselevel][#2]}}
        {\ExpandFirstAfter\dodododostelopsommingin[#1][#2]}}}

\def\dodostelopsommingin[#1][#2]%
  {\ConvertToConstant\doifelse{#2}{}
     {\ifnum\itemlevel=0\relax
        \dododostelopsommingin[\v!elk][#1]%
      \else
        \dododostelopsommingin[\itemlevel][#1]%
      \fi}
     {\doifelsenothing{#1}
        {\dododostelopsommingin[\itemlevel][#2]}
        {\dododostelopsommingin[#1][#2]}}}

\def\dostelopsommingin[#1][#2][#3]%
  {\dodostelopsommingin[#1][#2]%
   \ConvertToConstant\doifnot{#3}{}  % anders wordt #2 overruled
     {\dodostelopsommingin[#1][#3]}}

\def\stelopsommingin%
  {\dotripleempty\dostelopsommingin}

\def\doadvanceitem%
  {\ifsubsom\else\ifsymsom\else
     \pluscounter{\s!itemcount\itemlevel}%
   \fi\fi}

\def\setitemlevel#1%
  {\ifnum\itemlevel>0\relax
     \firstlisttrue
     \doifnotinset{\v!verder}{#1}
       {\resetcounter{\s!itemcount\itemlevel}}%
     \def\tempnumber%
       {\countervalue{\s!itemcount\itemlevel}}%
     \doifelsevalue{\??op\itemlevel\c!plaatsafsluiter}{\v!ja}
       {\def\tempsymbool{\getvalue{\??op\itemlevel\c!afsluiter}}}
       {\def\tempsymbool{}}%
   \fi}

% PAS OP: ook 'opelkaar' en zo worden getest, nog eens afvangen!

\def\setitemmark#1% % en pas op: resets \docommando
  {\doifsymboldefinedelse{#1}
     {\setxvalue{\??op\c!symbool\s!global\itemlevel}{#1}%
      \setgvalue{\??op\c!symbool\s!local\itemlevel}{?}%
      \def\listitem{\symbol[#1]}%
      \let\docommando\gobbleoneargument}
     {\doifconversiondefinedelse{#1}
        {\setxvalue{\??op\c!symbool\s!global\itemlevel}{#1}%
         \setgvalue{\??op\c!symbool\s!local\itemlevel}%
           {\convertnumber{#1}{\countervalue{\s!itemcount\itemlevel}}}%
         \def\listitem%
           {\getvalue{\??op\c!symbool\s!local\itemlevel}\tempsymbool}%
         \let\docommando\gobbleoneargument}
       {\let\listitem\empty}}}

%\def\calculatelistwidth#1#2%
%  {#2=\getvalue{\??op#1\c!afstand}\relax
%   \multiply#2 by \getvalue{\??op#1\c!factor}\relax
%   \advance#2 by \getvalue{\??op#1\c!breedte}\relax}

\def\calculatelistwidth#1#2%
  {#2=\getvalue{\??op#1\c!afstand}\relax
   \ifnum\getvalue{\??op#1\c!factor}>0
     \ifdim#2=\!!zeropoint #2=.5em\fi
   \fi
   \multiply#2 by \getvalue{\??op#1\c!factor}\relax
   \advance#2 by \getvalue{\??op#1\c!breedte}\relax}

\def\dodostartopsomming[#1][#2]%
  {\ifhmode
     \par
   \fi
   \ifnum\itemlevel=\maxitemlevel\relax
     \showmessage{\m!layouts}{9}{\maxitemlevel}%
     \def\itemincrement{0}%
   \else
     \def\itemincrement{1}%
   \fi
   \doglobal\increment(\itemlevel,\itemincrement)%
   \begingroup
   \ifnum\itemlevel=1 % NIEUW
     \doadaptleftskip{\getvalue{\??op1\c!marge}}%
%     \dosetraggedcommand{\@@oouitlijnen}\raggedcommand
   \fi
   \dosetraggedcommand{\getvalue{\??op\itemlevel\c!uitlijnen}}\raggedcommand
   \doifvaluesomething{\??op\itemlevel\c!inspringen}
     {\expanded{\stelinspringenin[\getvalue{\??op\itemlevel\c!inspringen}]}}%
   \doifinset{\v!kolommen}{#1}%
     {\ifbinnenkolommen\else\ifnum\itemcolumndepth=0
        \global\let\itemcolumndepth\itemlevel
        \getvalue{\??op\itemlevel\c!voor}%
        \processfirstactioninset
          [#1]
          [  \v!een=>\!!counta=1\relax,
            \v!twee=>\!!counta=2\relax,
            \v!drie=>\!!counta=3\relax,
            \v!vier=>\!!counta=4\relax,
            \v!vijf=>\!!counta=5\relax,
         \s!unknown=>\@EA\!!counta\getvalue{\??op\itemlevel\c!n}]%
        \startkolommen
          [\c!n=\!!counta, % netter \??op\itemlevel\c!n 
           \c!hoogte=,
           \c!lijn=\v!uit,
           \c!balanceren=\v!ja,
           \c!uitlijnen=\v!nee]%
      \fi\fi}%
   \doifinsetelse{\v!intro}{#1}
     {\somintrotrue}
     {\somintrofalse}%
   \doglobal\increment\noflists
   \let\currentlist=\noflists
   \newcounter\noflistelements
   \kopsomfalse
   \subsomfalse
   \symsomfalse
   \let\marsymbol=\relax
   \global\let\somdestination=\empty
   \def\symsymbol{}%
   \def\som%
     {\dosom}%
   \def\but[##1]%
     {\gdef\somdestination{##1}%
      \dosom}%
   \def\nop%
     {\sym{\strut}\strut}%
   \definecomplexorsimple\its
   \setvalue{\v!mar}##1%
     {\def\marsymbol%
        {\llap
           {\doattributes{\??op\itemlevel}\c!marletter\c!markleur{##1}%
            \hskip\leftskip\hskip\linkermargeafstand}}%
      \dosom}%
   \setvalue{\v!kop}%
     {\kopsomtrue\dokop}%
   \setvalue{\v!sub}%
     {\subsomtrue\dosom}%
   \setvalue{\v!sym}##1%
     {\def\symsymbol%
        {\doattributes{\??op\itemlevel}\c!symletter\c!symkleur{##1}}%
      \symsomtrue
      \dosom}%
%   \setvalue{\v!its}%
%     {\getvalue{\v!sym}%
%        {\calculatelistwidth{\itemlevel}{\dimen0}%
%         \hbox to \dimen0
%           {\dorecurse{\getvalue{\??op\itemlevel\c!items}}
%              {\listitem\hss}%
%            \unskip
%            \hskip\getvalue{\??op\itemlevel\c!afstand}}}}%
   \setvalue{\v!its}%
     {\getvalue{\v!ran}%
        {\dorecurse{\getvalue{\??op\itemlevel\c!items}}{\listitem\hss}%
         \unskip}}%
   \setvalue{\v!ran}##1%
     {\getvalue{\v!sym}%
        {\calculatelistwidth{\itemlevel}{\dimen0}%
         \hbox to \dimen0
           {##1\hskip\getvalue{\??op\itemlevel\c!afstand}}}}%
   \setitemlevel{#1}%
   \getvalue{\??op\itemlevel}%
   \doifelsenothing{#1} % iffirstargument
     {\edef\@@opsymbool{\getvalue{\??op\itemlevel\c!symbool}}%
      \setgvalue{\??op\c!symbool\s!global\itemlevel}{}%
      \setgvalue{\??op\v!verder\itemlevel}{}%
      \setitemmark{\@@opsymbool}%
      \dostelopsomminginvariable[\itemlevel][#2]}
     {\dostelopsomminginconstant[\itemlevel][#1]%
      \dostelopsomminginvariable[\itemlevel][#2]%
      \doifinsetelse{\v!verder}{#1}% \noexpand, else problems in non-etex with chinese
       %{\edef\@@opsymbool{\getvalue{\??op\c!symbool\s!global\itemlevel}}%
        {\edef\@@opsymbool{\noexpand\getvalue{\??op\c!symbool\s!global\itemlevel}}%
         \getvalue{\??op\v!verder\itemlevel}}
       %{\edef\@@opsymbool{\getvalue{\??op\itemlevel\c!symbool}}%
        {\edef\@@opsymbool{\noexpand\getvalue{\??op\itemlevel\c!symbool}}%
         \setgvalue{\??op\v!verder\itemlevel}%
           {\dostelopsomminginconstant[\itemlevel][#1]%
            \dostelopsomminginvariable[\itemlevel][#2]}}%
      \def\docommando{\setitemmark}% \setitemmark resets \docommando
      \processcommalist[#1,\@@opsymbool]\docommando}%
   \ifsomautointro\ifnum\prevgraf<3
     \somintrotrue
   \fi\fi
   \ifparagraphitems 
     \ifnum\itemlevel>1 \doassign[\??op\itemlevel][\c!tussen=]\fi
   \else\ifpackeditems 
     \doassign[\??op\itemlevel][\c!tussen=]%
   \fi\fi
   \calculatelistwidth{\itemlevel}{\dimen0}%
   \ifdim\dimen0>\!!zeropoint\relax
     \advance\leftskip by \dimen0\relax
   \fi}

\def\dostartopsomming[#1][#2]%
  {\ifsecondargument
     \dodostartopsomming[#1][#2]%
   \else
     \doifassignmentelse{#1}
       {\dodostartopsomming[][#1]}
       {\dodostartopsomming[#1][]}%
   \fi}

\def\startopsomming%
  {\bgroup
   \dodoubleempty\dostartopsomming}

\def\stopopsomming%
  {\par
   \ifnum\itemcolumndepth=0 \dolistreference \fi % beware !
   \iffirstlist \else \endgroup \fi % toegevoegd, eerte \som opent groep
   \ifnum\itemcolumndepth=\itemlevel\relax
     \stopkolommen
     \doglobal\newcounter\itemcolumndepth
     \getvalue{\??op\itemlevel\c!na}%
   \else
     \ifnum\itemlevel=1
       \dosomebreak\allowbreak           % toegevoegd
       \getvalue{\??op1\c!na}%
       \doif{\@@oospringvolgendein}{\v!nee}{\noindentation}%
     \fi
   \fi
   \endgroup
   \doglobal\decrement(\itemlevel,\itemincrement)%
   \egroup}

\def\sombreak%
  {\flushfootnotes\penalty-5\relax}   % -10

\def\somnobreak%
 {\flushfootnotes\penalty+5\ifbinnenkolommen\else00\fi\relax} % +5

\def\dolistitem% evt aantal items opslaan per niveau, scheelt zoeken 
  {\par
   \ignorespaces
   \increment\noflistelements
   \ifnum\itemcolumndepth=0\relax\ifoptimizeitems
     \ifnum\noflistelements=1                 % tgv bv kolommen/nesting
       \findtwopassdata{\s!list}{\noflists:}% % wordt soms de volgorde
     \fi                                      % verstoord, vandaar \find
     \iftwopassdatafound
       \ifnum\twopassdata=3
         \ifnum\noflistelements>1
           \dosomebreak\somnobreak
         \fi
       \else\ifnum\twopassdata>3
         \ifnum\noflistelements=2
           \ifsomintro
             \dosomebreak\nobreak
           \else
             \dosomebreak\somnobreak
           \fi
         \else\ifnum\twopassdata=\noflistelements\relax
           \dosomebreak\somnobreak
         \else\ifnum\noflistelements>2
           \dosomebreak\sombreak
         \else
           \ifsomintro\else\dosomebreak\sombreak\fi
         \fi\fi\fi
       \fi\fi
     \fi
   \fi\fi
   \noindent
   \ifkopsom
     \setbox8=\hbox
       {\doattributes{\??op\itemlevel}\c!kopletter\c!kopkleur{\listitem}}%
   \else\ifsymsom
     \setbox8=\hbox{\symsymbol}%
   \else
     \setbox8=\hbox
       {\doattributes{\??op\itemlevel}\c!letter\c!kleur{\listitem}}%
   \fi\fi
   \doifsomething{\somdestination}
    %{\setbox8=\hbox{\naar{\copy8}[\somdestination]}}% 
     {\setbox8=\hbox{\naar{\box8}[\somdestination]}}% 
   \global\let\somdestination=\empty
   \dimen2=\getvalue{\??op\itemlevel\c!breedte}\relax
   \ifdim\dimen2<\!!zeropoint\relax
     \llap{\ifsubsom\llap{+}\fi\box8\hskip\linkermargeafstand}%
   \else
     \ifdim\dimen2=\!!zeropoint\relax
       \calculatelistwidth{1}{\dimen0}%
     \else
       \calculatelistwidth{\itemlevel}{\dimen0}%
     \fi
     \llap{\hbox to \dimen0{\ifsubsom\llap{+}\fi\box8\hfill}}%
   \fi
   \setevalue{\??op\c!symbool\itemlevel}%
    %{\getvalue{\??op\c!symbool\s!local\itemlevel}}% problems with \uchar 
     {\noexpand\getvalue{\??op\c!symbool\s!local\itemlevel}}%
   \kopsomfalse
   \subsomfalse
   \symsomfalse
   \EveryPar{\ignorespaces}%
   \ignorespaces}

\def\complexdosom[#1]%
  {\par
   \ignorespaces
   \doadvanceitem
   \ifnum\itemcolumndepth=0\relax\ifnum\noflistelements>0\relax
     \dosomebreak\nobreak
   \fi\fi
   \iffirstlist
     \firstlistfalse
     \begingroup
     \ifcase\itemlevel
     \or % 1
       \ifnum\itemcolumndepth=0\relax
         \ifsomintro\dosomebreak\nobreak\fi
         \getvalue{\??op1\c!voor}%
         \ifsomintro\dosomebreak\nobreak\fi
       \fi
     \else % 2 en hoger
       \ifparagraphitems \else
         \let\previtemlevel=\itemlevel
         \decrement\previtemlevel
         \getvalue{\??op\previtemlevel\c!tussen}%  = itemlevel-1
       \fi
     \fi
   \else
     \itemuse\c!tussen
   \fi
   \ignorespaces
   \dolistitem
   \ifpackeditems
     \stelwitruimtein[\v!geen]%
   \fi
   \itemuse\c!binnen
   \marsymbol
   \let\marsymbol=\relax
   \doifsomething{#1}
     {\doifnot{\itemreference}{?}
        {\bgroup
         \protectconversion
         \rawreference{\s!lst}{#1}{\itemreference}%
         \egroup}}%
   \strut % added 11-08-99 
   \ignorespaces}

\def\complexsom[#1]#2\par%
  {\startopsomming[#1]
   \complexdosom[]\ignorespaces\begstrut#2\unskip\endstrut\par
   \stopopsomming}

\definecomplexorsimpleempty\som
\definecomplexorsimpleempty\dosom

% \def\complexdokop[#1]#2\par%
%   {\ifpackeditems\else\itemuse\c!kopvoor\fi
%    \dosomebreak{\pagina[\v!voorkeur]}%  geen \goodbreak ! \allowbreak testen
%    \complexdosom[#1]{\doattributes{\??op\itemlevel}\c!kopletter\c!kopkleur
%      {\ignorespaces#2}}\par
%    \ifpackeditems\else\itemuse\c!kopna\fi
%    \dosomebreak\nobreak
%    \noindentation}

\def\complexdokop[#1]#2\par% % beter in \complexdosom hangen met een if  
  {\iffirstlist\else\dosomebreak\allowbreak\fi
   \ifpackeditems\else\itemuse\c!kopvoor\fi
   \iffirstlist\ifsomintro\else\ifcase\itemlevel % incr in \complexdosom
     %\dosomebreak{\pagina[\v!voorkeur]}%  geen \goodbreak ! \allowbreak testen
      \dosomebreak\allowbreak
   \fi\fi\fi
   \complexdosom[#1]{\doattributes{\??op\itemlevel}\c!kopletter\c!kopkleur
     {\ignorespaces#2}}\par
   \dosomebreak\nobreak
   \ifpackeditems\else\itemuse\c!kopna\fi
   \dosomebreak\nobreak
   \noindentation}

\def\complexkop[#1]#2\par#3\par%
  {\startopsomming[#1]%
   \complexdokop[]\ignorespaces#2\par#3\par
   \stopopsomming}

\definecomplexorsimpleempty\kop
\definecomplexorsimpleempty\dokop

\def\sym#1%
  {\noindent
   \setbox0=\hbox{#1}%
   \ifdim\wd0<1em\relax
     \setbox0=\hbox to 1.5em{#1\hfil}%
   \else
     \setbox0=\hbox spread 1em{#1\hfil}%
   \fi
   \hangindent=\wd0\relax
   \box0
   \ignorespaces}

\stelopsommingenin  % undocumented
  [\c!niveaus=6,
   \c!marge=\!!zeropoint,
   \c!springvolgendein=\v!ja,
   \c!breedte=1.5em,
   \c!factor=0,
   \c!afstand=.5em,
   \c!uitlijnen=\v!normaal,
   \c!inspringen=, % untouched if empty 
   \c!kleur=,
   \c!letter=, % kan tzt weg
   \c!marletter=\c!type,  % \c! ???
   \c!symletter=,
   \c!kopletter=,
   \c!markleur=,
   \c!symkleur=,
   \c!kopkleur=,
   \c!kopvoor=,
   \c!kopna=\blanko,
   \c!voor=\blanko,
   \c!tussen=\blanko,
   \c!na=\blanko,
   \c!afsluiter=.,
   \c!plaatsafsluiter=\v!ja,
   \c!binnen=,
   \c!n=2,
   \c!items=4,
   \c!symbool=\itemlevel]

\protect \endinput 
