%%%==============================================================================
%% 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.1} {2025/10/08}
%%
%% 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/08}
    {1.1}
    {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/08},
     version     .code:n = {1.1} ,
     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]

\cs_new_protected:Npn \xpeekahead_set:NnTF #1#2#3#4
  {
    \cs_set_protected:Npn #1
      {
        \peek_regex:nTF { #2 }
          { #3 }
          { #4 }        
      }
  }
  
\cs_new_protected:Npn \xpeekahead_gset:NnTF #1#2#3#4
  {
    \cs_gset_protected:Npn #1
      {
        \peek_regex:nTF { #2 }
          { #3 }
          { #4 }        
      }
  }


\cs_new_protected:Npn \xpeekahead_set:NNTF #1#2#3#4
  {
    \cs_set_protected:Npn #1
      {
        \peek_regex:NTF {#2} 
          { #3 }
          { #4 }        
      }
  }

  
\cs_new_protected:Npn \xpeekahead_gset:NNTF #1#2#3#4
  {
    \cs_gset_protected:Npn #1
      {
        \peek_regex:NTF {#2}
          { #3 }
          { #4 }        
      }
  }


\cs_new_protected:Nn \xpeekahead_cmd_peek:nTF
  {
    \xpeekahead_set:NnTF 
      \__xpeekahead_cmd_peek_aux: 
      { #1 } { #2 } { #3 }     
    \xpeekahead_cmd_peek:N \__xpeekahead_cmd_peek_aux:
  }


\cs_new_protected:Nn \xpeekahead_cmd_peek:NTF
  {
    \xpeekahead_set:NNTF 
      \__xpeekahead_cmd_peek_aux: 
       { #1 }  { #2 } { #3 }     
    \xpeekahead_cmd_peek:N \__xpeekahead_cmd_peek_aux:
  }
  
  
\cs_new_protected:Nn \xpeekahead_cmd_peek:N
  {
    \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:nnTF #1#2#3#4
  {
    \__xpeekahead_set_end:n {#1}
    \xpeekahead_set:NnTF 
      \__xpeekahead_env_peek_cmd: 
      { #2 } { #3 } { #4 }
  }


\cs_new_protected:Npn \xpeekahead_env_set:nNTF #1#2#3#4
  {
    \__xpeekahead_set_end:n {#1}
    \xpeekahead_set:NNTF 
      \__xpeekahead_env_peek_cmd: 
      { #2 } { #3 } { #4 }
  }



  
%%%
%%%
%%% LaTeX2e alliases
%%%
%%%

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

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

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



%%%
%%% Argh!!
%%%

\cs_new_protected:Npn \xpeekahead_set:Nnnn #1#2#3#4
  {
    \msg_warning:nnnnn {xpeekahead}{deprecated}
      {dep~01}{\xpeekahead_set:Nnnn}{\xpeekahead_set:NnTF}
    \xpeekahead_set:NnTF  #1 {#2}{#3}{#4}
  }
\cs_new_protected:Npn \xpeekahead_gset:Nnnn #1#2#3#4
  {
    \msg_warning:nnnnn {xpeekahead}{deprecated}
      {dep~02}{\xpeekahead_gset:Nnnn}{\xpeekahead_gset:NnTF}
    \xpeekahead_gset:NnTF #1 {#2}{#3}{#4}
  }
\cs_new_protected:Npn \xpeekahead_set:NNnn #1#2#3#4
  {
    \msg_warning:nnnnn {xpeekahead}{deprecated}
      {dep~02}{\xpeekahead_set:NNnn}{\xpeekahead_set:NNTF}
    \xpeekahead_set:NNTF #1 {#2}{#3}{#4}
  }
\cs_new_protected:Npn \xpeekahead_gset:NNnn #1#2#3#4
  {
    \msg_warning:nnnnn {xpeekahead}{deprecated}
      {dep~02}{\xpeekahead_gset:NNnn}{\xpeekahead_gset:NNTF}
    \xpeekahead_gset:NNTF #1 {#2}{#3}{#4}
  }
\cs_new_protected:Npn \xpeekahead_cmd_peek:nnn #1#2#3
  {
    \msg_warning:nnnnn {xpeekahead}{deprecated}
      {dep~03}{\xpeekahead_cmd_peek:nnn}{\xpeekahead_cmd_peek:nTF}
    \xpeekahead_cmd_peek:nTF  {#1} {#2} {#3}
  }
\cs_new_protected:Npn \xpeekahead_cmd_peek:Nnn #1#2#3
  {
    \msg_warning:nnnnn {xpeekahead}{deprecated}
      {dep~03}{\xpeekahead_cmd_peek:Nnn}{\xpeekahead_cmd_peek:NTF}
    \xpeekahead_cmd_peek:NTF  {#1} {#2} {#3}
  }
\cs_new_protected:Npn \xpeekahead_env_set:nnnn #1#2#3#4
  {
    \msg_warning:nnnnn {xpeekahead}{deprecated}
      {dep~04}{\xpeekahead_env_set:nnnn}{\xpeekahead_env_set:nnTF}
    \xpeekahead_env_set:nnTF  {#1} {#2} {#3} {#4}
  }
\cs_new_protected:Npn \xpeekahead_env_set:nNnn #1#2#3#4
  {
    \msg_warning:nnnnn {xpeekahead}{deprecated}
      {dep~04}{\xpeekahead_env_set:nNnn}{\xpeekahead_env_set:nNTF}
    \xpeekahead_env_set:nNTF  {#1} {#2} {#3} {#4}
  }


