%++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++%
%                                                                          %
% This is file 'storecmd.sty', version 0.0.1, October 2011.                %
%                                                                          %
% This package and accompanying files may be distributed and/or            %
% modified under the conditions of the LaTeX Project Public License,       %
% either version 1.3 of this license or any later version. The latest      %
% version of this license is in http://www.latex-project.org/lppl.txt      %
% and version 1.3 or later is part of all distributions of LaTeX           %
% version 2005/12/01 or later.                                             %
%                                                                          %
% The LPPL maintenance status of this software is 'author-maintained'.     %
%                                                                          %
% This software is provided 'as it is', without warranty of any kind,      %
% either expressed or implied, including, but not limited to, the          %
% implied warranties of merchantability and fitness for a particular       %
% purpose.                                                                 %
%                                                                          %
% Copyright (c) 2011 Ahmed Musa (amusa22@gmail.com).                       %
%                                                                          %
%++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++%

\@ifpackageloaded{catoptions}{}{\RequirePackage{catoptions}[2011/08/07]}
\UseNormalCatcodes
\StyleFilePurpose{Store command name at definition time}
\StyleFileRCSInfo
$Id: storecmd.sty,v 0.0.1 2011/10/15 09:00:00 Ahmed Musa Exp $
\ProvidesPackage{storecmd}[\StyleFileInfo]
\NeedsTeXFormat{LaTeX2e}[2005/12/01]
\SetStyleFileMessages[stcmd@]{info}{warn}{err}
\RequirePackage{ltxtools}[2011/09/10]
\robust@def*\stcmd@verbosewarn#1{\ifstcmd@verbose\stcmd@warn{#1}\fi}
\robust@def*\stcmd@verboseinfo#1{\ifstcmd@verbose\stcmd@info{#1}\fi}
\providecommand*\ifsentinelTF[1]{%
  \edef\reserved@a{\unexpanded{#1}}%
  \ifx\reserved@a\currsentinel\expandafter\@firstoftwo
    \else\expandafter\@secondoftwo\fi
}
\robust@def*\stcmd@merge{\LTS@testopt\stcmd@m@rge,}
\robust@def*\stcmd@m@rge[#1]#2#3{%
  \trim@@spaces{#3}\LTS@tempa
  \ifdefTF#2{}{\def#2{}}%
  \xifinsetTF{#1\@space\LTS@tempa#1}{#1\@space#2#1}{}{%
    \xdef#2{\csliststack{#1\@space}#2\expandcsonce\LTS@tempa}%
  }%
}
\newvariables{if}[stcmd@]{storefile,storecmd,cmdescap,global,long,
  protected,verbose}
\robust@def*\stcmd@typeintocontinue{%
  \message{^^JType x or X to quit, or <RETURN> to proceed^^J}%
  {\endlinechar-1\global\read-1 to\@gtempa}%
  \lowercase\expandafter{\expandafter\def\expandafter
    \@gtempa\expandafter{\@gtempa}}%
  \xifstrcmpTF\@gtempa{x}\LTS@emergencystop\relax
}

\docommalist{storerenew,overwrite,neversave,showsource}{%
  \XDeclareSwitchOption{#1}[true](stcmd@){%
    \ifdefswitchTF{stcmd@#1}{%
      \stcmd@verboseinfo{'#1' set true\on@line}%
    }{}%
  }%
}

\XDeclareSwitchOption{verbose}[true](stcmd@){%
  \ifdefswitchTF{stcmd@verbose}{%
    \stcmd@info{'verbose' set true\on@line}%
  }{}%
}
\XDeclareCommandOption{storefile}[cmdnamesfile](stcmd@){%
  \ifblankTF{#1}{%
    \stcmd@err{No storage file name}\@ehc
    \stcmd@storefilefalse
  }{%
    \if\stcmd@overwrite\else
      \IfFileExists\stcmd@storefile{%
        \message{^^J! File '\stcmd@storefile' already exists:}%
        \stcmd@typeintocontinue
      }{}%
    \fi
    \stcmd@storefiletrue
    \NewWrites[stcmd@]{outputcheck}%
    \immediate\openout\stcmd@outputcheck=\stcmd@storefile\@space
    \immediate\write\stcmd@outputcheck{\relax}%
    \LTS@AfterEndDocument{\immediate\closeout\stcmd@outputcheck}%
  }%
}
\XDeclareCommandOption{storecmd}[cmdnamesmacro](stcmd@){%
  \def\stcmd@tempa##1{%
    \message{^^J! Command '##1\stcmd@storecmd' already exists:}%
    \stcmd@typeintocontinue
  }%
  \ifblankTF{#1}{%
    \stcmd@err{No storage command name}\@ehc
    \stcmd@storecmdfalse
  }{%
    \stcmd@storecmdtrue
    \ifescapedTF{#1}{%
      \if\stcmd@overwrite\else
        \ifdefFT{#1}{}{\stcmd@tempa\expandcsonce}%
      \fi
      \stcmd@cmdescaptrue
      \gdef#1{}%
    }{%
      \if\stcmd@overwrite\else
        \ifcsndefFT{#1}{}{\stcmd@tempa\noexpandcsn}%
      \fi
      \stcmd@cmdescapfalse
      \csn@gdef{#1}{}%
    }%
  }%
}
\XDeclareOption*{\stcmd@warn{Unknown option '\CurrentOption' ignored}}
\XExecuteOptions{%
  verbose=true,neversave=false,storerenew=false,overwrite=false,
  showsource=false
}
\XProcessOptions*\relax
\robust@def*\longcmd{\stcmd@longtrue}
\robust@def*\globalcmd{\stcmd@globaltrue}
\robust@def*\protectedcmd{\stcmd@protectedtrue}
\robust@def*\stcmd@cmdprefixes@a{%
  \ifstcmd@global\global\fi
  \ifstcmd@long\long\fi
  \ifstcmd@protected\protected\fi
}
\robust@def*\stcmd@cmdprefixes@b{%
  \ifstcmd@global
    \stcmd@globalfalse
    \let\stcmd@global\global
  \else
    \let\stcmd@global\@empty
  \fi
  \ifstcmd@long
    \stcmd@longfalse
    \let\stcmd@long\long
  \else
    \let\stcmd@long\@empty
  \fi
  \ifstcmd@protected
    \stcmd@protectedfalse\protected
  \fi
  \stcmd@global\stcmd@long
}
\robust@def*\defsavecmd{\cpt@starorlong\stcmd@defsavecmd}
\robust@def*\newsavecmd{\cpt@starorlong\stcmd@newsavecmd}
\robust@def*\renewsavecmd{\cpt@starorlong\stcmd@renewsavecmd}
\robust@def*\stcmd@savecmd#1{%
  \if\stcmd@neversave\else
    \begingroup
    \escapechar=92
    \xdef\@gtempa{%
      \expandafter\@gobble\string#1%
      \if\stcmd@showsource
        \@space(\ifx\@currname\@empty
        \jobname.tex\else\@currname.\@currext\fi)%
      \fi
    }%
    \endgroup
    \ifstcmd@storefile
      \immediate\write\stcmd@outputcheck{\@gtempa}%
    \fi
    \ifboolFT{stcmd@storecmd}{}{%
      \cptexpanded{%
        \stcmd@merge
          \ifstcmd@cmdescap
            \expandcsonce\stcmd@storecmd
          \else
            \noexpandcsn\stcmd@storecmd
          \fi
        {\@gtempa}%
      }%
    }%
  \fi
}
\robust@def*\stcmd@defsavecmd#1{%
  \ifdefFT{#1}{}{%
    \if\stcmd@verbose
      \stcmd@info{Command '\string#1' is redefined\on@line}%
    \fi
  }%
  \stcmd@savecmd{#1}%
  \stcmd@cmdprefixes@b\l@ngrel@x\def#1%
}
\robust@def*\stcmd@newsavecmd#1{%
  \stcmd@savecmd{#1}%
  \LTS@testopt{\stcmd@n@wc@mmand#1}{0}%
}
\robust@def*\stcmd@renewsavecmd#1{%
  \ifdefTF{#1}{%
    \if\stcmd@storerenew
      \stcmd@savecmd{#1}%
    \fi
    \let\ifcsdefinable\rc@ifcsdefinable
    \stcmd@n@wcommand#1%
  }{%
    \stcmd@err{Command '\string#1' is undefined:
      \MessageBreak it can't be redefined}\@ehc
  }%
}
\robust@def*\stcmd@newcommand{\cpt@starorlong\stcmd@n@wcommand}
\robust@def*\stcmd@n@wcommand#1{\LTS@testopt{\stcmd@n@wc@mmand#1}0}
\robust@def*\stcmd@n@wc@mmand#1[#2]{%
  \ifnextisbrack{\stcmd@xargdef@a#1{#2}}{\stcmd@argdef@a#1{#2}}%
}
\robust@def\stcmd@argdef@a#1#2{%
  \LTS@testpnopt{\stcmd@argdef@b{#1}{#2}}{}%
}
\robust@def\stcmd@argdef@b#1#2(#3)#4{%
  \def\stcmd@tempd{#3}%
  \ifcsdefinable#1{\stcmd@yargdef#1\@ne{#2}{#4}}%
}
\robust@def\stcmd@xargdef@a#1#2[#3]{%
  \LTS@testpnopt{\stcmd@xargdef@b{#1}{#2}{#3}}{}%
}
\robust@def\stcmd@xargdef@b#1#2#3(#4)#5{%
  \def\stcmd@tempd{#4}%
  \ifcsdefinable#1{%
    \stcmd@cmdprefixes@a
    \expandafter\def\expandafter#1\expandafter{%
      \expandafter\@protected@testopt\expandafter#1%
      \csname\string#1\endcsname{#3}%
    }%
    \aftercsname\stcmd@yargdef{\string#1}\tw@{#2}{#5}%
  }%
}
\robust@def\stcmd@yargdef#1#2#3{%
  \ifnum#2=\tw@
    \def\stcmd@tempb##11{[####1]}%
  \else
    \let\stcmd@tempb\@gobble
  \fi
  \expandafter\stcmd@zargdef\expandafter{\number#3}#1%
}
\robust@def\stcmd@zargdef#1#2{%
  \begingroup
  \def\stcmd@tempa##1#1##2##{%
    \def\stcmd@tempc{\stcmd@cmdprefixes@b\l@ngrel@x}%
    \cptexpandsecond{\expandafter\endgroup\stcmd@tempc
      \expandafter\def\expandafter#2\stcmd@tempb##1#1}{\elt{#1}}%
  }%
  \@tempcnta\z@pt
  \loop
    \advance\@tempcnta\@ne
    \csn@def{d\number\@tempcnta}{}%
    \ifnum\@tempcnta<9\relax
  \repeat
  \ifx\stcmd@tempd\@empty\else
    \@tempcnta\z@pt
    \def\do##1##2{%
      \ifstrcmpTF{##1}\zargdef@nil{}{%
        \advance\@tempcnta\@ne
        \ifnum\@tempcnta>#1\relax
          \stcmd@err{Number of delimiters exceeds
            number of parameters}\@ehc
        \fi
        \ifinsetTF{,##1,}{,1,2,3,4,5,6,7,8,9,}{}{%
          \stcmd@err{Invalid delimiter syntax:
            \MsgBrk no digit [1-9] found before delimiter}\@ehc
        }%
        \csn@def{d##1}{##2}\do
      }%
    }%
    \expandafter\do\stcmd@tempd\zargdef@nil\zargdef@nil
  \fi
  \ifx\stcmd@tempb\@gobble\else
    \expandafter\ifx\csname d1\endcsname\@empty\else
      \stcmd@err{Argument one of command \string#2 is
        \MsgBrk already delimited by '[]'}\@ehc
    \fi
  \fi
  \def\elt##1{\expandcsnonce{d##1}}%
  \edef\stcmd@tempc{0####1\ifx\stcmd@tempb\@gobble\elt1\fi
    ####2\elt2####3\elt3####4\elt4####5\elt5####6\elt6%
    ####7\elt7####8\elt8####9\elt9%
  }%
  \expandafter\stcmd@tempa\stcmd@tempc###1%
}

\endinput