%--------------------------------------------
%
% Package pgfplots
%
% Copyright 2007-2011 by Christian Feuersänger.
%
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
% 
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
% 
% You should have received a copy of the GNU General Public License
% along with this program.  If not, see <http://www.gnu.org/licenses/>.
%
%--------------------------------------------
%
% This source file contains helper utilities for verbatim character
% processing.


%
% USAGE: 
% \beginpgfplotsverbatim[result/.code={\message{I read #1}}]
% <verbatim code>
% \endpgfplotsverbatim
%
% The verbatim code MUST start at the line after the ']' (or after
% \beginpgfplotsverbatim if there is no ']')
\def\beginpgfplotsverbatim{\pgfutil@ifnextchar[{\beginpgfplotsverbatim@}{\beginpgfplotsverbatim@[]}}
\def\beginpgfplotsverbatim@[#1]{%
	\begingroup
	\pgfqkeys{/pgfplots/verb}{#1}%
	\pgfplots@verbatim@prepare@collect
	\beginpgfplotsverbatim@gobbleuntileol
}

\begingroup
\catcode`\|=0
\catcode`\\=12
|gdef|endpgfplotsverbatim{\endpgfplotsverbatim}%
|endgroup

\expandafter\long\expandafter\def\expandafter\pgfplots@verbatim@collect@until\expandafter#\expandafter1\endpgfplotsverbatim{%
	\begingroup
	\pgfplots@verbatim@newline@handler
	\xdef\pgfplots@glob@TMPa{#1}%
	\endgroup
	\pgfkeysgetvalue{/pgfplots/verb/result/.@cmd}\pgfplots@loc@TMPb
	\global\let\pgfplots@glob@TMPb=\pgfplots@loc@TMPb
	\endgroup% restore old catcodes etc.
	\expandafter\pgfplots@glob@TMPb\expandafter{\pgfplots@glob@TMPa}\pgfeov%
}%

\begingroup
\long\def\pgfplots@loc@TMPa{
}%^^M}%
\catcode`\^^M=13 %
\gdef\beginpgfplotsverbatim@gobbleuntileol^^M{\pgfplots@verbatim@collect@until}%
\gdef\pgfplots@verbatim@newline@toHATHATJ{\def^^M{^^J}}%
\xdef\pgfplots@verbatim@newline@IDENTITY{\noexpand\long\noexpand\def\noexpand^^M{\pgfplots@loc@TMPa}}%
\endgroup

\pgfkeys{
	/pgfplots/verb/result/.code={%
		\pgfplots@error{Please use \string\beginpgfplotsverbatim[result/.code=....] to configure what to do with the content of \string\beginpgfplotsverbatim...\endpgfplotsverbatim\space (or use \string\pgfplotsset{verb/.cd,result/.code=....} in your preamble)}%
	},
	/pgfplots/verb/newline handler/.is choice,
	/pgfplots/verb/newline handler/to outfile/.code=
		{\let\pgfplots@verbatim@newline@handler=\pgfplots@verbatim@newline@toHATHATJ},
	/pgfplots/verb/newline handler/identity/.code=
		{\let\pgfplots@verbatim@newline@handler=\pgfplots@verbatim@newline@IDENTITY},
	/pgfplots/verb/newline handler/identity,
}

\def\pgfplots@verbatim@prepare@collect{%
	\def\do##1{\catcode`##1=12 }\dospecials
	\catcode`\^^M=13
	\catcode`\^^J=12
	\catcode`\^^I=12
}%

\def\pgfplots@verbatim@restore@catcodes{%
	\catcode`\^^M=5
	\catcode`\^^J=10
	\catcode`\^^I=10
	% this is important to get \scantokens to work: otherwise, it will
	% eat up the ^^M chars:
	\endlinechar=`\^^M
	\newlinechar=\endlinechar
}%

\endinput
