%%
%% This is file `cprotect.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% cprotect.dtx  (with options: `package')
%% ----------------------------------------------------------------
%% cprotect --- verbatim in macro arguments
%% E-mail: blflatex@gmail.com
%% Released under the LaTeX Project Public License v1.3c or later
%% See http://www.latex-project.org/lppl.txt
%% ----------------------------------------------------------------
%% 
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{cprotect}[2011/01/27 v1.0e (Bruno Le Floch)]
\RequirePackage{ifthen}
\RequirePackage{suffix}
\def\CPT@qend{\CPT@qend}
\def\CPT@option@head#1=#2\CPT@qend{#1}
\def\CPT@option@tail#1=#2\CPT@qend{#2}
\DeclareOption*{%
  \ifthenelse{%
    \equal{gobbling-escape}{%
      \expandafter\CPT@option@head\CurrentOption=\CPT@qend}%
  }{%
    \edef\CPT@gobbling@escape{%
      \expandafter\CPT@option@tail\CurrentOption\CPT@qend
    }%
  }{%
    \ifthenelse{%
      \equal{gobbling-letter}{%
        \expandafter\CPT@option@head\CurrentOption=\CPT@qend}%
    }{%
      \edef\CPT@gobbling@letter{%
        \expandafter\CPT@option@tail\CurrentOption\CPT@qend
      }%
    }{%
      \PackageError{cprotect}{Unknown option \CurrentOption}{}%
    }%
  }%
}
\def\CPT@gobbling@escape{E}
\def\CPT@gobbling@letter{L}
\ProcessOptions\relax
\newwrite\CPT@WriteOut
\newcounter{CPT@WriteCount}
\edef\CPT@filename{\jobname.cpt}
\newcommand{\CPT@Write}[1]{%
  \immediate\openout\CPT@WriteOut=\CPT@filename%
  \newlinechar`\^^M%
  \immediate\write\CPT@WriteOut{#1}%
  \immediate\closeout\CPT@WriteOut%
  \expandafter\xdef\csname CPT@\CPT@filename\endcsname{%
    \noexpand\scantokens{#1}%
  }%
  %\expandafter\gdef\csname \string\CPT@\CPT@filename\expandafter\endcsname\expandafter{%
  %  \expandafter\protect\csname CPT@\CPT@filename\endcsname}%
  %\expandafter\show\csname \string\CPT@\CPT@filename\endcsname%
  %\expandafter\show\csname CPT@\CPT@filename\endcsname%
}
\newcommand{\makeallother}{%
  \count0=0\relax
  \loop
  \catcode\count0=12\relax
  \advance\count0 by 1\relax
  \ifnum\count0<256
  \repeat
}
{
  \catcode`\/=0
  /catcode`/\=12
  /catcode`/^=12
  /xdef/CPT@escape@hat@hat@L{\^^/CPT@gobbling@letter/space}
  /xdef/CPT@escape@hat@hat@E{\^^/CPT@gobbling@escape/space}
  /xdef/CPT@hat@hat@E@hat@hat@L{%
    ^^/CPT@gobbling@escape^^/CPT@gobbling@letter/space}
}
\expandafter\scantokens\expandafter{%
  \expandafter\catcode\expandafter`\CPT@escape@hat@hat@E=0}
\expandafter\scantokens\expandafter{%
  \expandafter\catcode\expandafter`\CPT@hat@hat@E@hat@hat@L=11}
\expandafter\scantokens\expandafter{%
  \expandafter\def\CPT@hat@hat@E@hat@hat@L{}}
\newcommand\ReadVerbatimUntil[2][]{%
  \def\CPT@commandatend{#2}%
  \begingroup #1%
  \makeallother%
  \CPT@setup}
\WithSuffix\newcommand\ReadVerbatimUntil*[2][]{%
  \def\CPT@commandatend{#2}%
  \begingroup #1%
  \makeallother%
  \CPT@starsetup}
\newcommand{\CPT@def}[2]{\expandafter\def\expandafter#1%
  \expandafter##\expandafter1#2}
\newcommand{\CPT@setup}[1]{%
  \def\CPT@delimiter{#1}%
  \CPT@def\CPT@readPreText\CPT@delimiter{%
       \def\CPT@preText{##1}\CPT@readPostText}%
  \CPT@def\CPT@readPostText\CPT@delimiter{%
       \def\CPT@postText{##1}\CPT@readBegin}%
  \CPT@def\CPT@readBegin\CPT@delimiter{%
       \def\CPT@begin{##1}\CPT@readEnd}%
  \CPT@def\CPT@readEnd\CPT@delimiter{%
       \def\CPT@end{##1}\CPT@readContent}%
  \CPT@readPreText%
}
\newcommand{\CPT@starsetup}[1]{\CPT@setup#1#1#1}
\newcounter{CPT@numB}
\newcommand{\CPT@store}[1]{\edef\CPT@storage{\CPT@storage#1}}
\newcommand{\CPT@readContent}{%
  \CPT@def\CPT@gobbleOneB\CPT@begin##2{%
    \ifx\CPT@qend##2\CPT@store{##1}\addtocounter{CPT@numB}{-1}%
    \else\CPT@store{##1\CPT@begin}\stepcounter{CPT@numB}%
    \expandafter\CPT@gobbleOneB\expandafter##2\fi}%
  %
  \CPT@def\CPT@gobbleUntilE\CPT@end{%
    \edef\CPT@tempi{##1\CPT@begin}%
    \expandafter\CPT@gobbleOneB\CPT@tempi\CPT@qend%
    \ifthenelse{\value{CPT@numB}<0}{%
      \CPT@store{\CPT@postText}%
      \CPT@Write{\CPT@storage}\endgroup%
      \CPT@commandatend%
    }{%
      \CPT@store{\CPT@end}\CPT@gobbleUntilE%
    }%
  }%
  \setcounter{CPT@numB}{0}%
  \def\CPT@storage{\CPT@preText}%
  \CPT@gobbleUntilE%
}
\outer\long\def\cprotect{\icprotect}
\newtoks\CPT@commandatend@toks
\newcommand{\icprotect}[2][om]{%
  \def\CPT@argsig{#1}%
  \def\CPT@cs{#2}%
  \CPT@commandatend@toks{#2}%
  \def\CPT@commandatend{\CPT@read@args}% used by RVU.
  \CPT@commandatend%
}
\def\CPT@argsig@pop{%
  \edef\CPT@argsig{\expandafter\@gobble\CPT@argsig}%
}
 %\newcommand\CPT@read@args{\CPT@read@m}
\newcommand\CPT@read@args{%
  \ifx\CPT@argsig\empty
  \expandafter\the\expandafter\CPT@commandatend@toks
  \else
  \expandafter\expandafter\expandafter\CPT@read@one
  \expandafter\CPT@argsig\expandafter\CPT@qend%
  \fi
}
\def\CPT@read@one#1#2\CPT@qend{%
  \def\CPT@argsig{#2}%
  \def\CPT@tempii{\csname CPT@read@#1\endcsname}% To make the \afterassignment simpler.
  \afterassignment\CPT@tempii\let\CPT@next=%
}
\newcommand\CPT@read@m{%
  \ifx\CPT@next\bgroup%
  \expandafter\CPT@read@mbeg%
  \else%
  \expandafter\CPT@read@mone%
  \fi%
}
\def\CPT@read@mone{\CPT@cs\CPT@next}
\begingroup
  \catcode`\{=12 \catcode`\}=12
  \catcode`\(=1 \catcode`\)=2
  \gdef\CPT@other@bgroup({)
  \gdef\CPT@other@egroup(})
\endgroup
\def\CPT@read@mbeg{%
  \stepcounter{CPT@WriteCount}%
  \edef\CPT@filename{\jobname-\arabic{CPT@WriteCount}.cpt}%
  \expandafter\expandafter\expandafter\CPT@commandatend@toks
  \expandafter\expandafter\expandafter{%
    \expandafter\the
    \expandafter\CPT@commandatend@toks
    % Input a file:
    \expandafter{%
      \expandafter\protect
      \expandafter\input
      \CPT@filename
      \relax
    }%
    % % Using \scantokens: requires '%' active.
    % \expandafter{%
    %   \csname \string\CPT@\CPT@filename\endcsname
    %   \expandafter\protect
    %   \expandafter\csname  CPT@\CPT@filename\endcsname
    % }%
  }%
  %\showthe\CPT@commandatend@toks%
  \begingroup%
  \makeallother%
  \def\CPT@preText{}%
  \let\CPT@postText\CPT@hat@hat@E@hat@hat@L%
  \let\CPT@begin\CPT@other@bgroup%
  \let\CPT@end\CPT@other@egroup%
  \CPT@readContent%
}%
\def\CPT@read@o{\CPT@read@d[]}
\def\CPT@read@d#1#2{%
  \if\noexpand\CPT@next#1%
  \expandafter\CPT@read@d@beg%
  \else%
  \expandafter\CPT@read@d@none%
  \fi%
  {#1}{#2}%
}
\def\CPT@read@d@none#1#2{%
  \CPT@read@args\CPT@next%
}
\def\CPT@read@d@beg#1#2{%
  \stepcounter{CPT@WriteCount}%
  \edef\CPT@filename{\jobname-\arabic{CPT@WriteCount}.cpt}%
  \expandafter\expandafter\expandafter\CPT@commandatend@toks%
  \expandafter\expandafter\expandafter{%
    \expandafter\the%
    \expandafter\CPT@commandatend@toks%
    \expandafter #1%
      \expandafter\protect%
      \expandafter\input%
      \CPT@filename%
      \relax%
    #2%
  }%
  \begingroup%
  \makeallother%
  \def\CPT@preText{}%
  \let\CPT@postText\CPT@hat@hat@E@hat@hat@L%
  \def\CPT@begin{#1}%
  \def\CPT@end{#2}%
  \CPT@readContent%
}%
\newcommand{\cMakeRobust}[1]{%
  \def\CPT@cs@name{\expandafter\@gobble\string#1}%
  \expandafter\let\csname CPT@old@\CPT@cs@name\endcsname #1%
  \expandafter\outer\expandafter\def\csname\CPT@cs@name\endcsname{%
    \expandafter\icprotect\csname CPT@old@\CPT@cs@name\endcsname}%
}
\newcommand{\CPTbegin}[1]{%
  \stepcounter{CPT@WriteCount}%
  \edef\CPT@filename{\jobname-\arabic{CPT@WriteCount}.cpt}%
  \edef\CPT@commandatend{%
    \noexpand\begin\noexpand{\noexpand#1\noexpand}%
      \noexpand\expandafter\noexpand\protect%
      \noexpand\expandafter\noexpand\input \CPT@filename\relax%
    \noexpand\end\noexpand{\noexpand#1\noexpand}%
  }%
  \begingroup%
  \CPT@env@setup{#1}%
  \makeallother%
  \CPT@readContent%
}
\newcommand{\CPT@otherify}[1]{%
  \expandafter\expandafter\expandafter\@gobble
  \expandafter\string\csname #1\endcsname
}
\newcommand{\CPT@env@setup}[1]{%
  \def\CPT@temp{#1}%
  \edef\CPT@temp{\CPT@otherify{#1}}%
  \edef\CPT@temp{\expandafter\strip@prefix\meaning\CPT@temp}%
  \expandafter\CPT@env@setup@\expandafter{\CPT@temp}%
}
\def\CPT@env@setup@#1{%
  \let\CPT@preText\CPT@hat@hat@E@hat@hat@L%
  \let\CPT@postText\CPT@hat@hat@E@hat@hat@L%
  \edef\CPT@begin{\string\begin\string{#1\string}}%
  \edef\CPT@end{\string\end\string{#1\string}}%
}
\def\cprotEnv\begin{\CPTbegin}
%% 
%% Copyright (C) 2010-2011 by Bruno Le Floch <blflatex@gmail.com>
%% 
%% This work may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License (LPPL), either
%% version 1.3c of this license or (at your option) any later
%% version.  The latest version of this license is in the file:
%% 
%% http://www.latex-project.org/lppl.txt
%% 
%% This work is "maintained" (as per LPPL maintenance status) by
%% Bruno Le Floch.
%% 
%% This work consists of the file  cprotect.dtx
%% and the derived files           cprotect.ins,
%%                                 cprotect.pdf and
%%                                 cprotect.sty.
%% 
%%
%% End of file `cprotect.sty'.
