%++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++%
%                                                                          %
% This is file 'ltxtools-environ.sty', version 0.0.1, December 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/12/12]}
\@ifpackageloaded{ltxtools}{}{\RequirePackage{ltxtools}[2011/12/12]}
\UseNormalCatcodes
\StyleFilePurpose{Collect and execute environment body (AM)}
\StyleFileRCSInfo
$Id: ltxtools-environ.sty,v 0.0.1 2011/12/12 09:00:00 Ahmed Musa Exp $
\ProvidesPackage{ltxtools-environ}[\StyleFileInfo]
\NeedsTeXFormat{LaTeX2e}[2011/06/27]

% \newenviron, \renewenviron {<name>}{<start>}<body>{<end>}.
% Collect environment body in \envbody:
\new@def*\AlwaysTrimEnvEntries{\global\LTS@alwaystrimtrue}
\new@def*\trimenvspaces{%
  \ifboolTF{LTS@alwaystrim}\cpttrimspaces\unexpanded
}
\new@def*\everyeoenv#1{\ltsfiltermergetsv\LTS@everyeoenv{#1}\nofilter}
\everyeoenv{\@ignoretrue}
\robust@def*\newenviron{\cpt@starorlong\LTS@newenviron}
\robust@def*\LTS@newenviron#1{%
  \edef\LTS@tempa{\trimenvspaces{#1}}%
  \cptexpandarg\cpt@testopt
    {\LTS@n@wenviron{\expandcsonce\LTS@tempa}}0%
}
\robust@def*\LTS@n@wenviron#1[#2]{%
  \cpt@ifbrack
    {\LTS@n@wenv@ron#1[#2]}
    {\LTS@n@wenv@r@n{#1}{[#2]}}%
}
\robust@def*\LTS@n@wenv@ron#1[#2][#3]{\LTS@n@wenv@r@n{#1}{[#2][{#3}]}}
\robust@def*\renewenviron{\cpt@starorlong\LTS@renewenviron}
\robust@def*\LTS@renewenviron#1{%
  \edef\LTS@tempa{\trimenvspaces{#1}}%
  \ifcsndefTF\LTS@tempa{}
    {\@latex@error{Environment #1 is undefined}\@ehc}%
  \letcsntocs\LTS@tempa\relax
  \letcsntocs{end\LTS@tempa}\relax
  \expandafter\LTS@newenviron\expandafter{\LTS@tempa}%
}
\robust@def\LTS@n@wenv@r@n#1#2#3#4{%
  \ifcsndefTF{#1}{}{\letcsntocsn{#1}{end#1}}%
  \aftercsname\new@command{#1}#2{%
    \edef\LTS@beforebody{\trimenvspaces{#3}}%
    \LTS@collectbody
  }%
  \l@ngrel@x\csn@edef{end#1}{%
    \begingroup
    \defpass\noexpand\x{\endgroup\trimenvspaces{#4}}%
  }%
}
\robust@def*\LTS@collectbody{%
  \begingroup
  \toks@{}%
  \everyeof{\end{EOF}\relax}%
  \LTS@coll@ctbody
}
\robust@def\LTS@coll@ctbody#1\end#2{%
  \toks1{%
    \cptexpanded{%
      \toks@{\the\toks@\trimenvspaces{#1}%
      \noexpand\end{\expandcsonce\LTS@tempa}}%
    }%
    \LTS@coll@ctbody
  }%
  \edef\LTS@tempa{\trimenvspaces{#2}}%
  \ifxTF\LTS@tempa\@currenvir{%
    \edef\LTS@tempb{\LTS@pushbegin#1\begin\end\cpt@relax}%
    \if@csemptyTF\LTS@tempb{%
      \cptexpandsecond\endgroup{%
        \edef\noexpand\envbody{\noexpand\unexpanded{%
          \the\toks@\ifblankTF{#1}{}{\trimenvspaces{#1}}%
        }}%
        \unexpanded{%
          \LTS@beforebody\relax\LTS@everyeoenv
          \ifboolTF{LTS@alwaystrim}\@ignoretrue\relax
        }%
        \noexpand\end{\LTS@tempa}%
      }%
    }{%
      \the\toks1
    }%
  }{%
    \oifstrcmpTF{\LTS@tempa}{document}{%
      \expandafter\endgroup\expandafter
        \@checkend\expandafter{\LTS@tempa}%
    }{%
      \oifstrcmpTF{\LTS@tempa}{EOF}{%
        \expandafter\endgroup\expandafter
          \@checkend\expandafter{\LTS@tempa}%
      }{%
        \the\toks1
      }%
    }%
  }%
}
\new@def\LTS@pushbegin#1\begin#2{%
  \expandafter\ifxTF\cpt@car#2x\car@nil\end{%
    \@gobble
  }{%
    \expandafter\swapbracefirst\expandafter
    {\romannumeral-`\q\cptoxdetok\LTS@tempa}
    {\expandafter\ifstrcmpTF\expandafter
    {\detokenize\expandafter{\romannumeral-`\q
      \trimenvspaces{#2}}}}{%
      x\cptgobbletorelax
    }{%
      \LTS@pushbegin
    }%
  }%
}
\XDeclareBooleanOption{alwaystrim}[true](LTS@){}{}
\XDeclareOption*{\LTS@warn{Unknown option '\CurrentOption' ignored}}
\XExecuteOptions{alwaystrim}
\XProcessOptions*\relax

\endinput