% Copyright 2006 by Till Tantau
% Taken from pgf/doc/generic/pgf/macros/pgfmanual-en-macros.tex
% This file may be distributed and/or modified
% 1. under the LaTeX Project Public License and/or
% 2. under the GNU Free Documentation License.

\RequirePackage{tikz}
\RequirePackage{fancyvrb}

\newcommand\pgfmanualentryheadline[1]{\itemsep=0pt\parskip=0pt\item\strut{#1}\par\topsep=0pt}
\newcommand{\macrooption}[3]{%
  \pgfmanualentryheadline{\texttt{\color{red}#1}\hfill(\textbf{#2}, intial value: \emph{#3})}%
}
\newcommand{\macro}[1]{\texttt{\textbackslash #1}}

% taken form pgf manual
\colorlet{graphicbackground}{orange!20}
\colorlet{codebackground}{blue!20}
% \definecolor{graphicbackground}{rgb}{0.96,0.96,0.8}
% \definecolor{codebackground}{rgb}{0.8,0.8,1}

% options fo codeexample env (inspired by pgf manual)
\pgfqkeys{/codeexample}{%
	width/.code=	{\setlength\codeexamplewidth{#1}},
	graphic/.code=	{\colorlet{graphicbackground}{#1}},
	code/.code=	{\colorlet{codebackground}{#1}},
	execute code/.is if=code@execute,
	code only/.code=	{\code@executefalse},
	tabsize/.initial=0,% FIXME : this here is merely used for indentation. It is just a TAB REPLACEMENT.
        gobble/.initial=0,
        font/.store in=\code@font,
	every codeexample/.style={width=6cm+7pt},
}

\newdimen\codeexamplewidth
\newif\ifcode@execute
\newbox\codeexamplebox

% begin of the env, find out if there is optional parameters
\def\codeexample{%
  % by default, the code is executed, the font is footnotesize
  \code@executetrue%
  \pgfqkeys{/codeexample}{every codeexample,font=\footnotesize}%
  % are there optional parameters
  \catcode`\^^M=\active%
  \@ifnextchar[{\catcode`\^^M=5\codeexample@}{\catcode`\^^M=5\codeexample@@}%
}
% if there are optional parameters parse them, execute them and then continue
\def\codeexample@[#1]{%
  \pgfqkeys{/codeexample}{#1}%
  \codeexample@@%
}
% after, let's save the content of the env
\def\codeexample@@{%
  % execute the every codeexample content
  \parindent=0pt%
  \topsep=0pt%
  \VerbatimEnvironment%
  \pgfkeysgetvalue{/codeexample/gobble}{\code@gobble}%
  % begins the saving of the env content
  \begin{VerbatimOut}[gobble=\code@gobble]{\jobname.tmp}%
}
% finish parsing the content and print everything
\def\endcodeexample{%
  \end{VerbatimOut}%
  % centering the result
  \center%
  \ifcode@execute% 
    % if we want execution, execute and then typeset
    \typeset@result{\input{\jobname.tmp}}%
  \else%
    \def\code@mid{\par}%
    \def\code@width{\linewidth-6pt}%
    \def\code@end{}%
  \fi%
  \code@mid%  
  % typeset the code
  \colorbox{codebackground}{%
    \begin{minipage}[c]{\code@width}%
      \VerbatimInput[fontsize=\code@font,commentchar=\%]{\jobname.tmp}%  
    \end{minipage}%
  }%
  \code@end%
  \par%
  \medskip%
  \endcenter%
}
% typeset the result of the parsed code
\newcommand{\typeset@result}[1]{%
  % put it in a box to get its width
  \setbox\codeexamplebox=\hbox{%
    \colorbox{graphicbackground}{\color{black}#1}%
  }%
  \ifdim\wd\codeexamplebox>\codeexamplewidth%
    % if the code is larger than codeexamplewidth, typeset centered result and then code
    \def\code@start{\par\begin{center}}%%MODIF
    \def\code@flushstart{}\def\code@flushend{}%
    \def\code@mid{\end{center}\parskip2pt\par\noindent}%%MODIF
    \def\code@width{\linewidth-6pt}%
    \def\code@end{}%
  \else%
    % otherwise typeset both on the same line
    \def\code@start{%
      \linewidth=\textwidth%
      \parshape \@ne 0pt \linewidth%
      \leavevmode%
      \hbox\bgroup}%
    \def\code@flushstart{\hfill}%
    \def\code@flushend{\hbox{}}%
    \def\code@mid{\hskip6pt}%
    \def\code@width{\linewidth-12pt-\codeexamplewidth}%
    \def\code@end{\egroup}%
  \fi%
  \code@start%
  \noindent%
  \begin{minipage}[c]{\codeexamplewidth}%\raggedright%
    \hrule width0pt%
    \footnotesize%
%    \vskip-1em%
    \code@flushstart\box\codeexamplebox\code@flushend%
%    \vskip-1ex%
    \leavevmode%
  \end{minipage}%      
}









% \ifx\scantokens\@undefined
%   \PackageError{pgfmanual-macros}{You need to use extended latex
%     (elatex) or (pdfelatex) to process this document}{}
% \fi

% \begingroup
% \catcode`|=0
% \catcode`[= 1
% \catcode`]=2
% \catcode`\{=12
% \catcode `\}=12
% \catcode`\\=12 |gdef|find@example#1\end{codeexample}[|endofcodeexample[#1]]
% |endgroup

% \begingroup
% \catcode`\^=7
% \catcode`\^^M=13
% \catcode`\ =13%
% \gdef\returntospace{\catcode`\ =13\def {\space}\catcode`\^^M=13\def^^M{}}%
% \endgroup

% \begingroup
% \catcode`\%=13
% \catcode`\^^M=13
% \gdef\commenthandler{\catcode`\%=13\def%{\@gobble@till@return}}
% \gdef\@gobble@till@return#1^^M{}
% \gdef\@gobble@till@return@ignore#1^^M{\ignorespaces}
% \gdef\typesetcomment{\catcode`\%=13\def%{\@typeset@till@return}}
% \gdef\@typeset@till@return#1^^M{{\def%{\char`\%}\textsl{\char`\%#1}}\par}
% \endgroup

% \define@key{codeexample}{width}{\setlength\codeexamplewidth{#1}}
% \define@key{codeexample}{graphic}{\colorlet{graphicbackground}{#1}}
% \define@key{codeexample}{code}{\colorlet{codebackground}{#1}}
% \define@key{codeexample}{execute code}{\csname code@execute#1\endcsname}
% \define@key{codeexample}{code only}[]{\code@executefalse}
% \define@key{codeexample}{pre}{\def\code@pre{#1}}
% \define@key{codeexample}{post}{\def\code@post{#1}}
% \define@key{codeexample}{vbox}[]{\def\code@pre{\vbox\bgroup\setlength{\hsize}{\linewidth-6pt}}\def\code@post{\egroup}}
% \define@key{codeexample}{ignorespaces}[]{\let\@gobble@till@return=\@gobble@till@return@ignore}
% \define@key{codeexample}{leave comments}[]{\def\code@catcode@hook{\catcode`\%=12}\let\commenthandler=\relax\let\typesetcomment=\relax}

% \def\code@pre{}
% \def\code@post{}
% \def\code@catcode@hook{}

% \newdimen\codeexamplewidth
% \newif\ifcode@execute
% \newbox\codeexamplebox
% \def\codeexample[#1]{%
%   \begingroup%
%   \code@executetrue
%   \setlength\codeexamplewidth{4cm+7pt}
%   \setkeys{codeexample}{#1}%
%   \parindent0pt
%   \begingroup%
%   \par%
%   \medskip%
%   \let\do\@makeother%
%   \dospecials%
%   \obeylines%
%   \@vobeyspaces%
%   \catcode`\%=13%
%   \catcode`\^^M=13%
%   \code@catcode@hook%
%   \relax%
%   \find@example}
% \def\endofcodeexample#1{%
%   \endgroup%
%   \ifcode@execute%
%     \setbox\codeexamplebox=\hbox{%
%       {%
%         {%
%           \returntospace%
%           \commenthandler%
%           \xdef\code@temp{#1}% removes returns and comments
%         }%
%         \colorbox{graphicbackground}{\color{black}\ignorespaces%
%           \code@pre\expandafter\scantokens\expandafter{\code@temp\ignorespaces}\code@post\ignorespaces}%
%       }%
%     }%
%     \ifdim\wd\codeexamplebox>\codeexamplewidth%
%       \def\code@start{\par}%
%       \def\code@flushstart{}\def\code@flushend{}%
%       \def\code@mid{\parskip2pt\par\noindent}%
%       \def\code@width{\linewidth-6pt}%
%       \def\code@end{}%
%     \else%
%       \def\code@start{%
%         \linewidth=\textwidth%
%         \parshape \@ne 0pt \linewidth
%         \leavevmode%
%         \hbox\bgroup}%
%       \def\code@flushstart{\hfill}%
%       \def\code@flushend{\hbox{}}%
%       \def\code@mid{\hskip6pt}%
%       \def\code@width{\linewidth-12pt-\codeexamplewidth}%
%       \def\code@end{\egroup}%
%     \fi%
%     \code@start%
%     \noindent%
%     \begin{center}%
%     \begin{minipage}[t]{\codeexamplewidth}\raggedright
%       \hrule width0pt%
%       \footnotesize\vskip-1em%
%       \code@flushstart\box\codeexamplebox\code@flushend%
%       \vskip-1ex
%       \leavevmode%
%     \end{minipage}%
%     \end{center}%
%   \else%
%     \def\code@mid{\par}
%     \def\code@width{\linewidth-6pt}
%     \def\code@end{}
%   \fi%
%   \code@mid%  
%   \colorbox{codebackground}{%
%     \begin{minipage}[t]{\code@width}%
%       {%
%         \let\do\@makeother
%         \dospecials
%         \frenchspacing\@vobeyspaces
%         \normalfont\ttfamily\footnotesize
%         \typesetcomment%
%         \@tempswafalse
%         \def\par{%
%           \if@tempswa
%           \leavevmode \null \@@par\penalty\interlinepenalty
%           \else
%           \@tempswatrue
%           \ifhmode\@@par\penalty\interlinepenalty\fi
%           \fi}%
%         \obeylines
%         \everypar \expandafter{\the\everypar \unpenalty}%
%         #1}
%     \end{minipage}}%
%   \code@end%
%   \par%
%   \medskip
%   \end{codeexample}
% }

% \def\endcodeexample{\endgroup}


%%% Local Variables: 
%%% mode: latex
%%% ispell-dictionary: "english"
%%% End: 
