% $Date: 2007-04-22 12:22:27 +0200 (Sun, 22 Apr 2007) $
% $Revision: 39 $
\def \filedate {2007/04/22}
\def \fileversion {v1.0}
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{xoptarg}%
  [\filedate \space \fileversion \space Expandable optional-argument macros (JN)]

\ifx \eTeXversion \@undefined
  \PackageWarning{xoptarg}{%
    This package works best with e-TeX\MessageBreak
    for its \protect\protected \space feature%
  }%
  \let \XOA@protected = \relax
\else
  \let \XOA@protected = \protected
\fi

\RequirePackage{newcommand}

%% \newcommand*{\xnewcommand}{%
%%   \@ifstar {\XOA@xnewcommand[\def]}%
%%            {\@testopt{\XOA@xnewcommand}{\long \def}}%
%% }

\newcommand*{\xnewcommand}{\@star@or@long \XOA@xnewcommand}

\newcommand*{\XOA@xnewcommand}{}
\newcommand*{\XOA@@xnewcommand}{}
\newcommand*{\XOA@@@xnewcommand}{}
\newcommand*{\XOA@@@@xnewcommand}{}
\newcommand*{\XOA@@@@ynewcommand}{}
\newcommand*{\XOA@@@ynewcommand}{}

% [<prefix-def>] {<command>}
%% \long \def \XOA@xnewcommand [#1]#2{%
%%   \@testopt {\XOA@@xnewcommand{#1}{#2}}{0}%
%% }

% {<command>}
\long \def \XOA@xnewcommand #1{\@testopt {\XOA@@xnewcommand{#1}}{0}}

% {<prefix-def>} {<command>} [<arg-num>]
%% \long \def \XOA@@xnewcommand #1#2[#3]{%
%%   \ifnum #3>0
%%     \expandafter \@firstoftwo
%%   \else
%%     \expandafter \@secondoftwo
%%   \fi
%%   {\XOA@@@xnewcommand{#1}{#2}{#3}}% Possibly  with    optional
%%   {\XOA@@@ynewcommand{#1}{#2}}%     Certainly without optional
%% }

% {<command>} [<arg-num>]
\long \def \XOA@@xnewcommand #1[#2]{%
  \ifnum #2>0
    \expandafter \@firstoftwo
  \else
    \expandafter \@secondoftwo
  \fi
  {\XOA@@@xnewcommand{#1}{#2}}% Possibly  with    optional
  {\XOA@@@ynewcommand{#1}}%     Certainly without optional
}


% {<prefix-def>} {<command>} {<arg-num>}
%% \long \def \XOA@@@xnewcommand #1#2#3{%
%%   \@ifnextchar [%]
%%     {\XOA@@@@xnewcommand{#1}{#2}{#3}}% With    optional
%%     {\XOA@@@@ynewcommand{#1}{#2}{#3}}% Without optional
%% }

% {<command>} {<arg-num>}
\long \def \XOA@@@xnewcommand #1#2{%
  \@ifnextchar [%]
    {\XOA@@@@xnewcommand{#1}{#2}}% With    optional
    {\XOA@@@@ynewcommand{#1}{#2}}% Without optional
}

% {<prefix-def>} {<command>} {<arg-num>} [<default>] {<definition>}
%% \long \def \XOA@@@@xnewcommand #1#2#3[#4]#5{%
%%   \ifnum #3<2
%%     \PackageWarning{xoptarg}{%
%%       Without at least one mandatory argument {...}\MessageBreak
%%       I cannot make the macro \protect#2\space expandible.\MessageBreak
%%       I will make it robust instead%
%%     }%
%%     \def \@tempa ##1{%
%%       \in@{\protected}{#1}%
%%       \unless \ifin@
%%         \XOA@protected
%%       \fi
%%       #1#2{\@testopt {##1}{#4}}%
%%       \def \@tempa {#1##1}%
%%       \def \@tempb ####1#3####2\@nil {####1#3}%
%%       \expandafter \@tempa \@tempb [####1]%
%%         ####2####3####4####5####6####7####8####9\@nil
%%     }%
%%     \count@ = \escapechar
%%     \escapechar = -1
%%     \expandafter \@tempa \csname \string #2 \endcsname {#5}%
%%     \escapechar = \count@
%%   \else
%%     \def \@tempa ##1{%
%%       #1#2####1####{%
%%         \XOA@ifempty{####1}%
%%           {##1[{#4}]}%
%%           {%
%%             \XOA@ifbeginswithspace{####1}%
%%               {\XOA@removespace \foo ####1}%
%%               {\XOA@ifbracket   {##1[}{##1[{#4}]}####1}%]
%%           }%
%%       }%
%%       \def \@tempa {#1##1}%
%%       \def \@tempb ####1#3####2\@nil {####1#3}%
%%       \expandafter \@tempa \@tempb [####1]%
%%         ####2####3####4####5####6####7####8####9\@nil
%%     }%
%%     \count@ = \escapechar
%%     \escapechar = -1
%%     \expandafter \@tempa \csname \string #2 \endcsname {#5}%
%%     \escapechar = \count@
%%   \fi
%% }

% {<command>} {<arg-num>} [<default>] {<definition>}
\long \def \XOA@@@@xnewcommand #1#2[#3]#4{%
  \ifnum #2<2
    \PackageWarning{xoptarg}{%
      Without at least one mandatory argument {...}\MessageBreak
      I cannot make the macro \protect#1\space expandible.\MessageBreak
      I will make it robust instead%
    }%
    \def \@tempa ##1{%
      \expandafter \in@ \expandafter \protected \expandafter {\l@ngrel@x}%
      \unless \ifin@
        \XOA@protected
      \fi
      \l@ngrel@x \def #1{\@testopt {##1}{#3}}%
      \def \@tempa {\l@ngrel@x \def ##1}%
      \def \@tempb ####1#2####2\@nil {####1#2}%
      \expandafter \@tempa \@tempb [####1]%
        ####2####3####4####5####6####7####8####9\@nil
    }%
    \count@ = \escapechar
    \escapechar = -1
    \expandafter \@tempa \csname \string #1 \endcsname {#4}%
    \escapechar = \count@
  \else
    \def \@tempa ##1{%
      \l@ngrel@x \def #1####1####{%
        \XOA@ifempty{####1}%
          {##1[{#3}]}%
          {%
            \XOA@ifbeginswithspace{####1}%
              {\XOA@removespace \foo ####1}%
              {\XOA@ifbracket   {##1[}{##1[{#3}]}####1}%]
          }%
      }%
      \def \@tempa {\l@ngrel@x \def ##1}%
      \def \@tempb ####1#2####2\@nil {####1#2}%
      \expandafter \@tempa \@tempb [####1]%
        ####2####3####4####5####6####7####8####9\@nil
    }%
    \count@ = \escapechar
    \escapechar = -1
    \expandafter \@tempa \csname \string #1 \endcsname {#4}%
    \escapechar = \count@
  \fi
}

% {<prefix-def>} {<command>} {<arg-num>} {<definition>}
%% \long \def \XOA@@@@ynewcommand #1#2#3#4{%
%%   \def \@tempa {#1#2}%
%%   \def \@tempb ##1#3##2\@nil {##1#3}%
%%   \expandafter \@tempa \@tempb ##1##2##3##4##5##6##7##8##9\@nil {#4}%
%% }

% {<command>} {<arg-num>} {<definition>}
\long \def \XOA@@@@ynewcommand #1#2#3{%
  \def \@tempa {\l@ngrel@x \def #1}%
  \def \@tempb ##1#2##2\@nil {##1#2}%
  \expandafter \@tempa \@tempb ##1##2##3##4##5##6##7##8##9\@nil {#3}%
}

% {<prefix-def>} {<command>} {<definition>}
%% \long \def \XOA@@@ynewcommand #1#2#3{#1#2{#3}}

% {<command>} {<definition>}
\long \def \XOA@@@ynewcommand #1#2{\l@ngrel@x \def #1{#2}}

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

\newcommand*{\XOA@ifbeginswithspace}[1]{%
  \XOA@@ifbeginswithspace #1 \@nil
}

\newcommand*{\XOA@@ifbeginswithspace}{}
\def \XOA@@ifbeginswithspace #1 #2\@nil {%
  \XOA@ifempty{#1}%
    {\@firstoftwo}%
    {\@secondoftwo}%
}

\newcommand*{\XOA@removespace}{}
\def \XOA@removespace #1 {#1}

\newcommand*{\XOA@ifbracket}[3]{%
  \ifx [#3%
    \expandafter \@firstoftwo
  \else
    \expandafter \@secondoftwo
  \fi
  {#1}{#2#3}%
}

\newcommand*{\XOA@ifempty}[1]{%
  \ifx \relax #1\relax
    \expandafter \@firstoftwo
  \else
    \expandafter \@secondoftwo
  \fi
}

\empty
