%%%==============================================================================
%% Copyright 2026-present by Alceu Frigeri
%%
%% This work may be distributed and/or modified under the conditions of
%%
%% * The [LaTeX Project Public License](http://www.latex-project.org/lppl.txt),
%%   version 1.3c (or later), and/or
%% * The [GNU Affero General Public License](https://www.gnu.org/licenses/agpl-3.0.html),
%%   version 3 (or later)
%%
%% This work has the LPPL maintenance status *maintained*.
%%
%% The Current Maintainer of this work is Alceu Frigeri
%%
%% This is version {1.1} {2026/02/19}
%%
%% The list of files that compose this work can be found in the README.md file at
%% https://ctan.org/pkg/xstacks
%%
%%%==============================================================================
\NeedsTeXFormat{LaTeX2e}[2025/06/01]

%%%%%%%
%%%
%%% Just an attempt at having my package's info in a regular way
%%%   \pkginfograb_set:nn {<pkg-name>} { props} sets package info
%%%
%%%   \pkginfograbProvidesExplPackage {<pkg-name>} { props} sets package's info
%%%     and calls \ProvidesExplPackage
%%%
%%%%%%%
\RequirePackage{pkginfograb}
\pkginfograbProvidesExplPackage {xstacks}
  {
     name        = {xstacks} ,
     prefix      = {xstacks} ,
     date        = {2026/02/19},
     version     = {1.1} ,
     description = {xstacks - after group(s) and dedicated global stack(s) implementations}
  }
%%%%%%%
%%% End of cut-n-paste
%%%%%%%


%%%%%%%
%%%  'fixing' it
%%%%%%%

\RequirePackage{tokgroupmark}
\RequirePackage{tokglobalstack}

%%% NEWEST ONE...
\cs_set_eq:NN \xstacks_groupmark_new:n \groupmark_new:n


\cs_set_eq:NN \xstacks_cs_gset:n \globalstack_csnew:n
\cs_set_eq:NN \xstacks_cs_gset:N \globalstack_csnew:n

\cs_set_eq:NN \xstacks_gset:N \globalstack_new:N

\cs_set_eq:NN \xstacks_gpush:Nn \globalstack_gpush:Nn
\cs_set_eq:NN \xstacks_gput_right:Nn \globalstack_gput_right:Nn
\cs_set_eq:NN \xstacks_gput_left:Nn \globalstack_gput_left:Nn
\cs_set_eq:NN \xstacks_gpop:N \globalstack_gpop:N

%%%%%%%
%%%  'legacy'
%%%%%%%


%%%%%%%
%%%  'pure' aftergroup
%%%%%%%

\int_new:N \l__xstacks_level_int
\cs_new_protected:Npn \xstacks_groupmark:
  {
    \int_set_eq:NN \l__xstacks_level_int \currentgrouplevel
    \int_incr:N \l__xstacks_level_int
  }

\cs_new_protected:Npn \xstacks_aftergroup:N #1
  {
    \if_int_compare:w \l__xstacks_level_int < \currentgrouplevel
        \group_insert_after:N \xstacks_aftergroup:N 
        \group_insert_after:N #1 
    \else:
        \group_insert_after:N #1 
    \fi:
  }

%
% The difference between \int_compare:nNnTF and \if_int_compare:w is brutal...
%
%\cs_new_protected:Npn \xstacks_aftergroup:N #1
%  {
%    \int_compare:nNnTF \l__xstacks_level_int < \currentgrouplevel
%      { 
%        \group_insert_after:N \xstacks_aftergroup:N 
%        \group_insert_after:N #1 
%      }
%      { 
%        \group_insert_after:N #1 
%      }
%  }


%%%

\cs_new_protected:Npn \xstacks_groupmark:N #1
  {
    \int_set_eq:NN #1 \currentgrouplevel
    \int_incr:N #1
  }

\cs_new_protected:Npn \xstacks_aftergroup:NN #1#2
  {
    \if_int_compare:w #1 < \currentgrouplevel
        \group_insert_after:N \xstacks_aftergroup:NN 
        \group_insert_after:N #1 
        \group_insert_after:N #2 
    \else:
        \group_insert_after:N #2 
    \fi:
  }

%
% The difference between \int_compare:nNnTF and \if_int_compare:w is brutal...
%
%\cs_new_protected:Npn \xstacks_aftergroup:NN #1#2
%  {
%    \int_compare:nNnTF #1 < \currentgrouplevel
%      { 
%        \group_insert_after:N \xstacks_aftergroup:NN 
%        \group_insert_after:N #1 
%        \group_insert_after:N #2 
%      }
%      { 
%        \group_insert_after:N #2 
%      }
%  }

