%D \module
%D   [       file=core-dat,
%D        version=2021.04.17, % replaces core-two from 1997.03.31,
%D          title=\CONTEXT\ Core Macros,
%D       subtitle=Multipass Datasets,
%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 / Multipass Datasets}

%D \starttyping
%D \definedataset[test-1]
%D \definedataset[test-2][delay=yes]
%D
%D \startlines
%D set 1: \setdataset[test-1][whatever=this-or-that-1]
%D set 2: \setdataset[test-2][whatever=this-or-that-2]
%D set 3: \setdataset[test-2][whatever=this-or-that-3]
%D \stoplines
%D
%D \startlines
%D get 1: \datasetvariable{test-1}{1}{whatever} / \datasetvariable{test-1}{1}{realpage}
%D get 2: \datasetvariable{test-2}{1}{whatever} / \datasetvariable{test-2}{1}{realpage}
%D get 3: \datasetvariable{test-2}{2}{whatever} / \datasetvariable{test-2}{2}{realpage}
%D \stoplines
%D \stoptyping

%D \starttyping
%D \definedataset[nofpages]
%D
%D \dorecurse{16}{whatever\page}
%D
%D \setdataset [nofpages] [pagetotal=\lastpagenumber]
%D
%D \testfeatureonce{100}{test \datasetvariable[todo-datasets]{nofpages}{1}{pagetotal} }
%D \stoptyping

\unprotect

\registerctxluafile{core-dat}{autosuffix}

\installcorenamespace{dataset}

\installcommandhandler \??dataset {dataset} \??dataset

\permanent\tolerant\protected\def\setdataset[#1]#*[#S#2]#*[#S#3]%
  {\begingroup
   \cdef\currentdataset{#1}%
   \ifparameters\or\or
     \clf_setdataset
        name  {\currentdataset}%
        delay {\datasetparameter\c!delay}%
        data  {#2}%
     \relax
   \or
     \cdef\currentdataset{#1}%
     \clf_setdataset
        name  {\currentdataset}%
        tag   {#2}%
        delay {\datasetparameter\c!delay}%
        data  {#3}%
     \relax
   \fi
   \endgroup}

\permanent\tolerant\def\datasetvariable[#1]{\clf_datasetvariable{#1}}
\permanent\tolerant\def\datasetsize    [#1]{\clf_datasetsize    {#1}}

% \startsetups[dataset:demo]
%     \datasetvariable{autores}{\recurselevel}{whatever}
% \stopsetups
% \dorecurse{\datasetsize{demo}}{
%   \setups[dataset:demo]
% }

% \startsetups[dataset:demo]
%     \datasetvariable{autores}{\datasetindex}{whatever}
% \stopsetups
% \processdataset[demo][dataset:demo]

% \startsetups[dataset:demo]
%     \datasetentry{whatever}
% \stopsetups
% \processdataset[demo][dataset:demo]

\mutable\let\datasetindex\!!zerocount
\mutable\let\datasetentry\gobbleoneargument

\permanent\tolerant\protected\def\processdataset[#1]#*[#2]#*[#3]%
  {\ifparameters\or
   \or\system_process_dataset  {}{#1}{#2}%
   \or\system_process_dataset{#1}{#2}{#3}%
   \fi}

\def\system_process_dataset#1#2#3%
  {\pushmacro\datasetindex
   \pushmacro\datasetentry
   \def\datasetentry{\clf_datasetvariable{#1}{#2}{\datasetindex}}%
   \dorecurse{\datasetsize[#1]{#2}}{\let\datasetindex\recurselevel\directsetup{#3}}%
   \popmacro\datasetentry
   \popmacro\datasetindex}

\protect

