%%
%% This is file `namedef.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% namedef.dtx  (with options: `package')
%% 
%% This is a generated file.
%% 
%% Copyright 2019--2020 Phelype Oleinik /phelype.oleinik@latex-project.org/
%% 
%% This work may be distributed and/or modified under the conditions of the
%% LaTeX Project Public License, either version 1.3c of this license or (at
%% your option) any later version. The latest version of this license is in
%% 
%%     http://www.latex-project.org/lppl.txt
%% 
%% and version 1.3c or later is part of all distributions of LaTeX version
%% 2005/12/01 or later.
%% 
\def\namedefDate{2020-06-20}
\def\namedefVersion{1.0}
\begingroup\expandafter\expandafter\expandafter\endgroup
\expandafter\ifx\csname RequirePackage\endcsname\relax
  \input expl3-generic.tex
\else
  \RequirePackage{expl3}[2018-05-15]
\fi
\ExplSyntaxOn
\cs_if_exist:NTF \ProvidesExplPackage
  {
    \cs_new_eq:NN \__namedef_end_package_hook: \prg_do_nothing:
    \ExplSyntaxOff
    \ProvidesExplPackage
  }
  {
    \cs_new_eq:NN \__namedef_end_package_hook: \ExplSyntaxOff
    \group_begin:
    \ExplSyntaxOff
    \cs_set_protected:Npn \__namedef_tmp:w #1#2#3#4
      {
        \group_end:
        \tl_gset:cx { ver @ #1 . sty } { #2 ~ v#3 ~ #4 }
        \cs_if_exist_use:NF \wlog { \iow_log:x }
          { Package: ~ #1 ~ #2 ~ v#3 ~ #4 }
      }
    \__namedef_tmp:w
  }
      {namedef} {\namedefDate} {\namedefVersion}
      {Named parameters in TeX definitions (PHO)}
\flag_new:n { __namedef_parm_count }
\tl_const:Nn \c__namedef_prefix_tl { namedef~parm~-> }
\tl_new:N \l__namedef_macro_tl
\quark_new:N \q__namedef_mark
\quark_new:N \q__namedef_stop
\scan_new:N \s__namedef
\cs_new:Npn \__namedef_skip_to_scan_mark:w  #1    \s__namedef {  }
\cs_new:Npn \__namedef_skip_to_scan_mark:nw #1 #2 \s__namedef {#1}
\cs_new_eq:NN \__namedef_tmp:w ?
\cs_new_protected:Npn \named { \__namedef_grab_prefix:nN { } }
\cs_new_protected:Npn \__namedef_grab_prefix:nN #1 #2
  {
    \__namedef_if_prefix:NTF #2
      { \__namedef_grab_prefix:nN }
      { \__namedef_detect_prefixes:Nn \__namedef_kill_outer:nN }
    { #1#2 }
  }
\prg_new_conditional:Npnn \__namedef_if_prefix:N #1 { TF }
  {
    \if_int_compare:w 0
        \if_meaning:w \tex_protected:D #1 1 \fi:
        \if_meaning:w \tex_global:D    #1 1 \fi:
        \if_meaning:w \tex_outer:D     #1 1 \fi:
        \if_meaning:w \tex_long:D      #1 1 \fi:
        \if_meaning:w \scan_stop:      #1 1 \fi:
        = 1 \exp_stop_f:
      \prg_return_true:
    \else:
      \prg_return_false:
    \fi:
  }
\cs_new_protected:Npn \__namedef_detect_prefixes:Nn #1 #2
  {
    \cs_set_nopar:Npn \__namedef_tmp:w { }
    \use:x
      {
        \exp_not:N #1
          {
            \exp_after:wN \exp_after:wN
              \exp_after:wN \__namedef_extract_prefixes:w
                \exp_after:wN \token_to_meaning:N
                  \cs:w __namedef_tmp:w \cs_end: \s__namedef
            \exp_not:n {#2}
          }
      }
  }
\use:x
  {
    \cs_new:Npn \exp_not:N \__namedef_extract_prefixes:w ##1
        \tl_to_str:n { macro: } ##2 \s__namedef
      {
        \exp_not:N \__namedef_extract_protected:n {##1}
        \exp_not:N \__namedef_extract_long:n {##1}
        \exp_not:N \__namedef_extract_outer:n {##1}
      }
  }
\cs_set_protected:Npn \__namedef_tmp:w #1 #2
  {
    \use:x
      {
        \cs_new:cpn { __namedef_extract_#1:n } ####1
          {
            \exp_not:c { __namedef_extract_#1_aux:w } ####1
              \token_to_str:N #2 \scan_stop: \token_to_str:N #2 \s__namedef
          }
        \cs_new:cpn { __namedef_extract_#1_aux:w } ####1
            \token_to_str:N #2 ####2 \token_to_str:N #2 ####3 \s__namedef
          {
            \exp_not:N \if_meaning:w \scan_stop: ####2
            \exp_not:N \else:
              \exp_not:c { tex_#1:D }
            \exp_not:N \fi:
          }
      }
  }
\__namedef_tmp:w { protected } { \protected }
\__namedef_tmp:w { long } { \long }
\__namedef_tmp:w { outer } { \outer }
\cs_new_protected:Npn \__namedef_kill_outer:nN #1
  {
    \cs_set:Npn \__namedef_tmp:w { \__namedef_start:nNp {#1} }
    \exp_after:wN \__namedef_tmp:w \exp_not:N
  }
\cs_new_protected:Npn \__namedef_start:nNp #1 #2 #3 #
  {
    \group_begin:
      \int_set:Nn \tex_escapechar:D { `\\ }
      \exp_after:wN \cs_set_eq:NN \exp_not:N #2 \scan_stop:
      \__namedef_replace_named:nNnn {#1} #2 {#3}
  }
\cs_new_protected:Npn \__namedef_replace_named:nNnn #1 #2 #3 #4
  {
      \tl_set:Nx \l__namedef_macro_tl { \token_to_str:N #2 }
      \__namedef_replace_parameter:Nn \__namedef_in_parameter:nN {#3}
      \__namedef_replace_parameter:Nn \__namedef_in_replacement:nN {#4}
      \__namedef_define:nnnN     {#1} #2
      \s__namedef
  }
\cs_new_protected:Npn \__namedef_define:nnnN #1 #2 #3 #4
  {
    \group_end:
    #3#4#2{#1}
  }
\cs_new_protected:Npn \__namedef_replace_parameter:Nn #1 #2
  {
    \cs_set_eq:NN \__namedef_handle_parameter:nN #1
    \__namedef_replace_parm:Nn \__namedef_parameter_output:nnw {#2}
  }
\cs_new_eq:NN \__namedef_handle_parameter:nN ?
\cs_new_protected:Npn \__namedef_parameter_output:nnw #1 #2
    #3 \__namedef_define:nnnN
  { #2 #3 \__namedef_define:nnnN {#1} }
\cs_new:Npn \__namedef_in_parameter:nN #1
  {
    \if_cs_exist:w \c__namedef_prefix_tl #1 \cs_end:
      \exp_after:wN \use_i:nn
    \else:
      \exp_after:wN \use_ii:nn
    \fi:
      { \msg_expandable_error:nnn { namedef } { repeated-parm } {#1} }
      {
        \exp_after:wN \use_none:n \cs:w \c__namedef_prefix_tl #1 \cs_end:
        \flag_raise:n { __namedef_parm_count }
      }
    \exp_args:Nf \__namedef_append_output:nnNwnn
      { \flag_height:n { __namedef_parm_count } }
      {#1}
  }
\cs_new:Npn \__namedef_in_replacement:nN #1 #2
  {
    \cs_if_exist:cTF { \c__namedef_prefix_tl #1 }
      {
        \exp_args:Nf \__namedef_append_output:nnNwnn
          { \use:c { \c__namedef_prefix_tl #1 } }
          { }
      }
      {
        \msg_expandable_error:nnn { namedef } { unknown-parm } {#1}
        \exp_args:Ne \__namedef_append_output:nnNwnn
          { #2 \__namedef_begin_name_token: #1 \__namedef_end_name_token: }
          { \cs_end: { } \use_none:nn }
      }
        #2
  }
\cs_new:Npn \__namedef_replace_parm:Nn #1 #2
  {
    \exp_after:wN #1
      \exp:w
      \__namedef_replace_loop:w #2
        \q__namedef_mark \q__namedef_stop { } { }
  }
\cs_new:Npn \__namedef_replace_loop:w #1 \q__namedef_stop
  {
    \tl_if_head_is_N_type:nTF {#1}
      { \__namedef_replace_normal:N }
      {
        \tl_if_head_is_group:nTF {#1}
          { \__namedef_replace_group:n }
          { \__namedef_replace_space:w }
      }
    #1 \q__namedef_stop
  }
\cs_new:Npn \__namedef_replace_end:wnn \q__namedef_stop #1 #2
  { \exp_end: {#1} {#2} }
\cs_new:Npn \__namedef_flush:nw #1
    #2 \q__namedef_stop #3 #4
  { \__namedef_replace_loop:w #2 \q__namedef_stop { #3 #1 } {#4} }
\cs_new:Npn \__namedef_append_output:nnNwnn #1 #2 #3
    #4 \q__namedef_stop #5 #6
  {
    \__namedef_replace_loop:w #4 \q__namedef_stop
      { #5 #3#1 }
      { #6 \cs_set:cpn { \c__namedef_prefix_tl #2 } {#1} }
  }
\cs_new:Npn \__namedef_abort_definition:w
    #1 \q__namedef_stop #2 #3
  {
    \__namedef_replace_loop:w #1 \q__namedef_stop
      {#2} { #3 \group_end: \__namedef_skip_to_scan_mark:w }
  }
\cs_new:Npn \__namedef_replace_normal:N #1
  {
    \token_if_eq_meaning:NNTF \q__namedef_mark #1
      { \__namedef_replace_end:wnn }
      {
        \token_if_parameter:NTF #1
          { \__namedef_grab_parm:Nw }
          { \__namedef_flush:nw }
            {#1}
      }
  }
\cs_new:Npn \__namedef_replace_group:n #1
  { \__namedef_replace_parm:Nn \__namedef_flush_group:nnw {#1} }
\cs_new:Npn \__namedef_flush_group:nnw #1 #2
  { \__namedef_flush:nw { {#1} } }
\exp_last_unbraced:NNo
\cs_new:Npn \__namedef_replace_space:w \c_space_tl
  { \__namedef_flush:nw { ~ } }
\cs_new:Npn \__namedef_grab_parm:Nw #1 #2 \q__namedef_stop
  {
    \tl_if_head_is_N_type:nTF {#2}
      { \__namedef_grab_parm_aux:NNw }
      { \__namedef_flush:nw }
        #1 #2 \q__namedef_stop
  }
\cs_new:Npn \__namedef_grab_parm_aux:NNw #1 #2
  {
    \exp_args:No \token_if_eq_charcode:NNTF
        { \__namedef_begin_name_token: } #2
      { \__namedef_grab_parm_scan:NNw }
      { \__namedef_grab_parm_noop:NNw }
        #1 #2
  }
\cs_new:Npn \__namedef_grab_parm_noop:NNw #1 #2
  {
    \token_if_eq_meaning:NNTF \q__namedef_mark #2
      { \__namedef_flush:nw { #1 } #2 }
      { \__namedef_flush:nw { #1 #2 } }
  }
\cs_new:Npn \__namedef_grab_parm_scan:NNw #1 #2 #3 \q__namedef_stop
  { \__namedef_grab_parm_loop:nw { } #3 \q__namedef_stop {#1} }
\cs_new:Npn \__namedef_grab_parm_loop:nw #1 #2 \q__namedef_stop
  {
    \tl_if_head_is_N_type:nTF {#2}
      { \__namedef_parm_get_normal:nN }
      {
        \tl_if_head_is_group:nTF {#2}
          { \__namedef_parm_get_group:nn }
          { \__namedef_parm_get_space:nw }
      }
    {#1} #2 \q__namedef_stop
  }
\cs_new:Npn \__namedef_parm_get_normal:nN #1 #2
  {
    \token_if_eq_meaning:NNTF \q__namedef_mark #2
      {
        \msg_expandable_error:nn { namedef } { missing-end }
        \__namedef_grab_parm_end:nw {#1} #2
      }
      {
        \exp_args:No \token_if_eq_charcode:NNTF
            { \__namedef_end_name_token: } #2
          { \__namedef_grab_parm_end:nw {#1} }
          { \__namedef_grab_parm_loop:nw {#1#2} }
      }
  }
\cs_new:Npn \__namedef_parm_get_group:nn #1 #2
  { \__namedef_grab_parm_loop:nw { #1{#2} } }
\cs_new:Npn \__namedef_parm_get_space:nw #1 ~
  { \__namedef_grab_parm_loop:nw { #1~ } }
\cs_new:Npn \__namedef_grab_parm_end:nw #1 #2 \q__namedef_stop #3
  { \__namedef_handle_parameter:nN {#1} #3 #2 \q__namedef_stop }
\cs_new:Npn \__namedef_begin_name_token: { [ }
\cs_new:Npn \__namedef_end_name_token: { ] }
\cs_new_protected:Npn \NamedDelim
  { \__namedef_named_delim_set:Nnn \cs_set:Npn  }
\cs_new_protected:Npn \globalNamedDelim
  { \__namedef_named_delim_set:Nnn \cs_gset:Npn }
\named \cs_new_protected:Npn \__namedef_named_delim_set:Nnn
    #[def] #[begin] #[end]
  {
    \tl_trim_spaces_apply:nN {#[begin]} \__namedef_check_delimiter:n
    \tl_trim_spaces_apply:nN {#[end]}   \__namedef_check_delimiter:n
    #[def] \__namedef_begin_name_token: {#[begin]}
    #[def] \__namedef_end_name_token: {#[end]}
    \s__namedef
  }
\named \cs_new_protected:Npn \__namedef_check_delimiter:n #[token]
  {
    \tl_if_empty:nT {#[token]}
      {
        \msg_error:nn { namedef } { blank-delim }
        \__namedef_skip_to_scan_mark:w
      }
    \tl_if_single_token:nF {#[token]}
      {
        \msg_error:nnn { namedef } { multi-token-delim } {#[token]}
        \__namedef_skip_to_scan_mark:w
      }
    \bool_lazy_or:nnT
        { \token_if_group_begin_p:N #[token] }
        { \token_if_group_end_p:N #[token] }
      {
        \msg_error:nnx { namedef } { group-delim }
          { \cs_to_str:N #[token] }
        \__namedef_skip_to_scan_mark:w
      }
    \token_if_parameter:NT #[token]
      {
        \msg_error:nnx { namedef } { param-delim }
          { \cs_to_str:N #[token] }
        \__namedef_skip_to_scan_mark:w
      }
    \token_if_cs:NT #[token]
      {
        \msg_error:nnx { namedef } { macro-delim }
          { \c_backslash_str \cs_to_str:N #[token] }
        \__namedef_skip_to_scan_mark:w
      }
  }
\msg_new:nnn { namedef } { repeated-parm }
  {
    Parameter~\iow_char:N\#[#1]~duplicated~in~
    definition~of~\l__namedef_macro_tl.
  }
\msg_new:nnn { namedef } { unknown-parm }
  {
    Unknown~parameter~\iow_char:N\#[#1]~in~
    definition~of~\l__namedef_macro_tl.
  }
\msg_new:nnn { namedef } { multi-token-delim }
  {
    Invalid~\iow_char:N\\named~parameter~delimiter~`#1'.~
    Delimiters~for~named~parameters~must~be~single~tokens.
  }
\msg_new:nnn { namedef } { macro-delim }
  {
    Invalid~\iow_char:N\\named~parameter~delimiter~`#1'.~
    Delimiters~for~named~parameters~can't~be~control~sequence~nor~
    active~characters.
  }
\msg_new:nnn { namedef } { group-delim }
  {
    Invalid~\iow_char:N\\named~parameter~delimiter~`\iow_char:N\\#1'.~
    Delimiters~for~named~parameters~can't~be~
      begin-/end-group~character~tokens.
  }
\msg_new:nnn { namedef } { blank-delim }
  {
    Invalid~\iow_char:N\\named~parameter~delimiter.~
    Delimiters~for~named~parameters~can't~be~empty~nor~space~tokens.
  }
\msg_new:nnn { namedef } { param-delim }
  {
    Invalid~\iow_char:N\\named~parameter~delimiter.~
    Delimiters~for~named~parameters~can't~be~parameter~tokens.
  }
\msg_new:nnn { namedef } { missing-end }
  {
    Missing~\__namedef_end_name_token:\iow_char:N\ inserted~in~
    definition~of~\l__namedef_macro_tl.
  }
\__namedef_end_package_hook:
%% 
%% This work is "maintained" (as per the LPPL maintenance status)
%% by Phelype Oleinik.
%% 
%% This work consists of the files namedef.dtx, and
%%                                 namedef.ins,
%% and the derived files           namedef.sty,
%%                                 namedef.pdf,
%%                                 README.md, and
%%                                 README.txt.
%%
%% End of file `namedef.sty'.
