%%%==============================================================================
%% Copyright 2025-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.2} {2025/10/14}
%%
%% The list of files that compose this work can be found in the README.md file at
%% https://ctan.org/pkg/xpeekahead
%%
%%%==============================================================================
\NeedsTeXFormat{LaTeX2e}[2022/06/01]

\ProvidesExplPackage
    {xpeekahead}
    {2025/10/14}
    {1.2}
    {A simplistic peek ahead set up}

%%%%%%%
%%%
%%% Just an attempt of having my packages info in a regular way
%%% Idea being: { <pck-name> / pkg info } for each and all.
%%%
%%%%%%%
\keys_define:nn { xpeekahead / pkg info}
  {
     name        .code:n = {xpeekahead} ,
     prefix      .code:n = {xpeekahead} ,
     date        .code:n = {2025/10/14},
     version     .code:n = {1.2} ,
     description .code:n = {A simplistic peek ahead set up}
  }
\cs_if_exist:NF \PkgInfo 
  {
    \NewDocumentCommand \PkgInfo {mm} { \keys_set:nn {#1 / pkg info}{#2} } 
    \NewDocumentCommand \PkgDescription {m} 
      { \noindent Package~ \textbf{\PkgInfo{#1}{name}}~Version:~\PkgInfo{#1}{version}~ -~ \PkgInfo{#1}{date}\par \emph{\PkgInfo{#1}{description}}~\par } 
  }  
%%%%%%%
%%% End of cut-n-paste
%%%%%%%

\msg_new:nnnn {xpeekahead} {deprecated}
  {
    (ID:#1)~#2 ~ is ~ deprecated.~ use~ '#3' ~ instead
  }
  {
    You~tried~to~use~a~deprecated~command:~#2. Use~'#3'~ instead.
    ~Error~Code~ ID:<#1>.
  }

\keys_define:nn { xpeekahead }
  {
    strict .usage:n    = load ,
    strict .code:n = 
      {
        \msg_redirect_module:nnn {xpeekahead} { warning } { error }
      } ,
  }
\ProcessKeyOptions [xpeekahead]

%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%

%%%
%%% Note that the TF endings
%%% are just for readability
%%% these commands aren't
%%% expl3 \prg_condicionals 
%%%


\cs_new:Npn \__xpeekahead_set:NNnnnn #1#2#3#4#5#6
  {
    #1 #3
      {
        #2 { #4 }
          { #5 }
          { #6 }        
      }
  }
%%%
%%% bending the expl3 rules a bit...
%%% This expects 6 parameters, 
%%% whereas the following commands will insert 
%%% 2 parameters *before* the user given 4...
%%%

\cs_new_protected:Npn \xpeekahead_set:Nnnn 
  {
    \__xpeekahead_set:NNnnnn \cs_set_protected:Npn \peek_regex:nTF
  }
\cs_new_eq:NN  \xpeekahead_set:NnTF \xpeekahead_set:Nnnn

\cs_new_protected:Npn \xpeekahead_gset:Nnnn 
  {
    \__xpeekahead_set:NNnnnn \cs_gset_protected:Npn \peek_regex:nTF
  }
\cs_new_eq:NN \xpeekahead_gset:NnTF \xpeekahead_gset:Nnnn

\cs_new_protected:Npn \xpeekahead_set:NNnn 
  {
    \__xpeekahead_set:NNnnnn \cs_set_protected:Npn \peek_regex:NTF
  }
\cs_new_eq:NN \xpeekahead_set:NNTF \xpeekahead_set:NNnn 

\cs_new_protected:Npn \xpeekahead_gset:NNnn 
  {
    \__xpeekahead_set:NNnnnn \cs_gset_protected:Npn \peek_regex:NTF
  }
\cs_new_eq:NN \xpeekahead_gset:NNTF \xpeekahead_gset:NNnn 


\cs_new:Npn \__xpeekahead_cmd_peek:Nnnn #1#2#3#4
  {
    #1 
      \__xpeekahead_cmd_peek_aux: 
      { #2 } { #3 } { #4 }     
    \xpeekahead_cmd_peek:N \__xpeekahead_cmd_peek_aux:
  }
%%%
%%% same (regarding expl3 rules)
%%% the following commands will insert 
%%% 1 parameter *before* the user given 3...
%%%

\cs_new_protected:Npn \xpeekahead_cmd_peek:nnn
  {
    \__xpeekahead_cmd_peek:Nnnn \xpeekahead_set:Nnnn
  }
\cs_new_eq:NN \xpeekahead_cmd_peek:nTF \xpeekahead_cmd_peek:nnn

\cs_new_protected:Npn \xpeekahead_cmd_peek:Nnn
  {
    \__xpeekahead_cmd_peek:Nnnn \xpeekahead_set:NNnn
  }
\cs_new_eq:NN \xpeekahead_cmd_peek:NTF \xpeekahead_cmd_peek:Nnn

  
  
\cs_new_protected:Npn \xpeekahead_cmd_peek:N #1
  {
    \peek_remove_spaces:n
      {
        \peek_analysis_map_inline:n
          {
            \exp_args:No \token_if_eq_meaning:NNF { ##1 } \para_end:
              {
                \peek_analysis_map_break:n
                  {
                    \exp_after:wN
                      #1
                    ##1
                  }
              }
            ##1
          }
     }
  }

\cs_set_eq:NN \__xpeekahead_tmpi: \relax
  
\cs_new_protected:Npn \__xpeekahead_env_end:n #1
  {
    \tl_if_eq:nVTF { #1 } \l__xpeekahead_peekenv_tl
      {
        % Environment context closes when calling {end~} 
        % therefore the peek function has to be saved first (\group_insert_after:N doesn't work here)
        \cs_gset_eq:NN \__xpeekahead_tmpi: \__xpeekahead_env_peek_cmd:
        \__xpeekahead_org_end:n {#1} %% context closed !!!
        \xpeekahead_cmd_peek:N  \__xpeekahead_tmpi: 
      }
      {
        \__xpeekahead_org_end:n {#1}
      }
  }

\cs_new_protected:Npn \__xpeekahead_set_end:n #1
  {
    %% WARNING: assuming end~ won't change thereafter.
    \cs_if_exist:NF \__xpeekahead_org_end:n
      { \cs_set_eq:Nc \__xpeekahead_org_end:n { end~ } }
    \cs_set_eq:cN { end~ } \__xpeekahead_env_end:n
    \tl_set:Nn \l__xpeekahead_peekenv_tl {#1}
  }

\cs_new_protected:Npn \xpeekahead_env_set:nN #1#2
  {
    \__xpeekahead_set_end:n {#1}
    \cs_set_eq:NN \__xpeekahead_env_peek_cmd: #2
  }

\cs_new_protected:Npn \__xpeekahead_env_set:Nnnnn #1#2#3#4#5
  {
%    \tl_show:n{#1 - #2 - #3 - #4 - #5}
    \__xpeekahead_set_end:n {#2}
    #1 
      \__xpeekahead_env_peek_cmd: 
      { #3 } { #4 } { #5 }
  }
%%%
%%% same (regarding expl3 rules)
%%% the following commands will insert 
%%% 1 parameter *before* the user given 4...
%%%

\cs_new_protected:Npn \xpeekahead_env_set:nnnn 
  {
    \__xpeekahead_env_set:Nnnnn \xpeekahead_set:Nnnn
  }
\cs_new_eq:NN \xpeekahead_env_set:nnTF \xpeekahead_env_set:nnnn 

\cs_new_protected:Npn \xpeekahead_env_set:nNnn 
  {
    \__xpeekahead_env_set:Nnnnn \xpeekahead_set:NNnn
  }
\cs_new_eq:NN \xpeekahead_env_set:nNTF \xpeekahead_env_set:nNnn 
  
%%%
%%%
%%% LaTeX2e alliases
%%%
%%%

\cs_new_eq:NN \xpeekSetCmd \xpeekahead_set:Nnnn
\cs_new_eq:NN \xpeekSetCmdGlobal \xpeekahead_gset:Nnnn

\cs_new_eq:NN \xpeekTok \xpeekahead_cmd_peek:nnn
\cs_new_eq:NN \xpeekTokCmd \xpeekahead_cmd_peek:N

\cs_new_eq:NN \xpeekEnv \xpeekahead_env_set:nnnn 
\cs_new_eq:NN \xpeekEnvCmd \xpeekahead_env_set:nN


