%%%==============================================================================
%% 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.0} {2026/02/18}
%%
%% 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 {tokgroupmark}
  {
     name        = {tokgroupmark} ,
     prefix      = {groupmark} ,
     date        = {2026/02/18},
     version     = {1.0} ,
     description = {tokgroupmark - restoring tokens after closing multiple groups}
  }
%%%%%%%
%%% End of cut-n-paste
%%%%%%%

\msg_new:nnnn { tokgroupmark } {defined}
  {
    (#1)~#2~already~defined.
  }
  {
    #2~ is ~ already~ defined, ~and~ will~ not~ be~ redefined.
    ~Error~Code~ ID:<#1>.
  }

\cs_new_protected:Npn \groupmark_new:n #1
  {
    \cs_if_exist:cTF {#1 _groupmark:}
      {
        \msg_error:nnnn { tokgroupmark } {defined}
          {new01} {#1}
      }
      {
        \int_new:c {l__xstacks_ #1 _int}
    
        \cs_new_protected:cpe {#1 _groupmark:}
          {
            \exp_not:N \int_set_eq:NN  \exp_not:c {l__xstacks_ #1 _int} \exp_not:N \currentgrouplevel
            \exp_not:N \int_incr:N \exp_not:c {l__xstacks_ #1 _int}
          }
        
        \cs_new_protected:cpe {#1 _aftergroup:N} ##1
          {
            \exp_not:N \if_int_compare:w \exp_not:c {l__xstacks_ #1 _int} < \exp_not:N \currentgrouplevel
                \exp_not:N \group_insert_after:N \exp_not:c {#1 _aftergroup:N}
                \exp_not:N \group_insert_after:N ##1 
            \exp_not:N \else:
                \exp_not:N \group_insert_after:N ##1 
            \exp_not:N \fi:
          }
      }
  }

