%D \module
%D   [       file=typo-stc,
%D        version=20240619,
%D          title=\CONTEXT\ Typesetting Macros,
%D       subtitle=Stacking,
%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.

% todo: textstyle|color for inline \quotation etc

\writestatus{loading}{ConTeXt Typesetting Macros / Stacking}

\registerctxluafile{typo-stc}{autosuffix}

\unprotect

%D \starttyping
%D \definestacking[one]
%D \definestacking[two]
%D
%D \startbuffer
%D This is a test \stacking [one] {and a proof} of \stacking [two] {concept} indeed \stacking [one]
%D {\blackrule [width=4cm]} is it and the question is how \stacking [two] {\scale [s=2]
%D {fancy}} we can go
%D
%D \startstacking[one]
%D \startformula
%D     \sqrt{1+x}
%D \stopformula
%D \stopstacking
%D
%D and how useful \stacking[two]{\inframed{it}} is.
%D \stopbuffer
%D
%D \startstackingsteps[one,two,{one,two}]
%D     \getbuffer
%D \stopstackingsteps
%D
%D \startTEXpage[offset=1ts] \setupstacking[criterium={one}]     \getbuffer \stopTEXpage
%D \startTEXpage[offset=1ts] \setupstacking[criterium={two}]     \getbuffer \stopTEXpage
%D \startTEXpage[offset=1ts] \setupstacking[criterium={one,two}] \getbuffer \stopTEXpage
%D \stoptyping

\installcorenamespace {stacking}

\installcommandhandler \??stacking {stacking} \??stacking

\definesystemattribute[stacking][global]

\appendtoks
    \expandafter\setnewconstant\csname\??stacking:\currentstacking\endcsname
        \clf_newstacking{\currentstacking}{\stackingparameter\c!number}%
    \relax
    \ifcstok{\stackingparameter\c!define}\v!yes
        \protected\frozen\instance\edefcsname        \currentstacking\endcsname{\typo_stacking{\currentstacking}}%
        \protected\frozen\instance\edefcsname\e!start\currentstacking\endcsname{\typo_stacking_start{\currentstacking}}%
        \protected\frozen\instance\edefcsname\e!stop \currentstacking\endcsname{\typo_stacking_stop}%
    \fi
\to \everydefinestacking

\def\typo_stacking_set
  {\c_attr_stacking
     \ifcsname\??stacking:\currentstacking\endcsname
       \lastnamedcs
     \orelse\ifchknum\currentstacking\or
   % \orelse\ifchknumber\currentstacking\or
       \currentstacking
     \else
       \attributeunsetvalue
     \fi
     \relax}

\protected\def\resetstacking
  {\c_attr_stacking\attributeunsetvalue}

\protected\def\typo_stacking#1#2%
  {\begingroup
   \cdef\currentstacking{#1}%
   \typo_stacking_set
   #2%
   \endgroup}

\protected\def\typo_stacking_start#1%
  {\begingroup
   \cdef\currentstacking{#1}%
   \typo_stacking_set}

\protected\def\typo_stacking_stop
  {\endgroup}

\permanent\protected\def\stacking     [#1]{\typo_stacking{#1}}
\permanent\protected\def\startstacking[#1]{\typo_stacking_start{#1}}
\permanent\protected\def\stopstacking     {\typo_stacking_stop}

\appendtoks
    \clf_setstacking{\namedstackingparameter\empty\c!criterium}%
\to \everysetupstacking

% \definestacking[stack][define=yes]

\mutable\lettonothing\stackingsteplist

\def\typo_stacking_step#1%
  {\setupstacking[\c!criterium={#1}]%
   \getbuffer[stackingsteps]%
   \page}

\permanent\tolerant\protected\def\startstackingsteps[#1]%
  {\page
   \begingroup
   \edef\stackingsteplist{#1}%
   \begingroup
   \buff_grab_direct[stackingsteps][stackingsteps][startstackingsteps][stopstackingsteps]}

\permanent\protected\def\stopstackingsteps
  {%\endgroup
   \processcommacommand[\stackingsteplist]\typo_stacking_step
   \endgroup}

%D For Aditya Mahajan: ranges and such.
%D
%D \starttyping
%D \starttext
%D \startstackingsteps[1,{1:2,201},{1:3,301},{1,2,4},{1-5},{1-6},{1-7}]
%D     \startTEXpage
%D         \startstacking[1]   \framed{ONE}    \stopstacking
%D         \startstacking[2]   \framed{TWO}    \stopstacking
%D         \startstacking[201] \framed{MORE}   \stopstacking
%D         \startstacking[3]   \framed{THREE}  \stopstacking
%D         \startstacking[301] \framed{INDEED} \stopstacking
%D         \startstacking[4]   \framed{FOUR}   \stopstacking
%D         \startstacking[5]   \framed{\im {\sqrt {
%D              \stacking[6] { 1 + x ^ {
%D              \stacking[7] { 2 } } } } }}
%D         \stopstacking
%D     \stopTEXpage
%D \stopstackingsteps
%D \stoptext
%D \stoptyping

\lettonothing\currenthidestacking
\lettonothing\currentvidestacking

\def\hidestacking[#1]%
  {\lettonothing\currentvidestacking
   \cdef\currenthidestacking{#1}}

\def\videstacking[#1]%
  {\lettonothing\currenthidestacking
   \cdef\currentvidestacking{#1}}

% slow, we need a doifinrange

\def\typo_stacking_hide#1{\ifnum#1=\scratchcounter     \c_attr_stacking\zerocount\fi}
\def\typo_stacking_vide#1{\ifnum#1=\scratchcounter\else\c_attr_stacking\zerocount\fi}

\def\typo_stacking_unstack_hide
  {\clf_enablestacking
   \dowithrange\currenthidestacking\typo_stacking_hide}

\def\typo_stacking_unstack_vide
  {\clf_enablestacking
   \dowithrange\currentvidestacking\typo_stacking_vide}

\protected\def\typo_stacking_unstack
  {\ifcase\scratchcounter
     \c_attr_stacking\attributeunsetvalue % or -1
   \orunless\ifempty\currenthidestacking
     \typo_stacking_unstack_hide
   \orunless\ifempty\currentvidestacking
     \typo_stacking_unstack_vide
  %\else
     % nothing
   \fi}

\permanent\tolerant\protected\def\unstacking[#1]% empty resets
  {\groupedcommand
     {\scratchcounter\ifparameter#1\or#1\else\zerocount\fi\typo_stacking_unstack}
     \donothing}

%D For Mikael Sundqvist: just hiding
%D
%D \starttyping
%D \starttext
%D \startbuffer
%D \startformula
%D   4 \alignhere = 2 \unstacking[1]{+}                2 \breakhere
%D   4 \alignhere = 2 \unstacking[2]{+\unstacking{!}}  2 \breakhere
%D   4 \alignhere = 2 \unstacking[2]{+\unstacking{!}+} 2
%D \stopformula
%D \stopbuffer
%D
%D                    \getbuffer \blank[2*big]
%D \hidestacking[1]   \getbuffer \blank[2*big]
%D \hidestacking[2]   \getbuffer \blank[2*big]
%D \hidestacking[1:2] \getbuffer \page
%D
%D                    \getbuffer \blank[2*big]
%D \videstacking[1]   \getbuffer \blank[2*big]
%D \videstacking[2]   \getbuffer \blank[2*big]
%D \videstacking[1:2] \getbuffer \page
%D \stoptext
%D \stoptyping

\protect \endinput
