%D \module
%D   [       file=m-gnuplot,
%D        version=2006.01.04,
%D          title=\CONTEXT\ Extra Modules,
%D       subtitle=\GNUPLOT\ Inclusion,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright=\PRAGMA]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

% todo: reuse (only if really needed)

% \enablemode[demo]

%D example:
%D
%D \starttyping
%D texexec --pdf --mode=demo m-gnuplot
%D \stoptyping

\unprotect

\ifx\operatingsystem\undefined \def\operatingsystem{unix} \fi % texexec will set that

\newcounter\GNUPLOTnumber

\def\startGNUPLOTinclusions
  {\bgroup
   \obeylines
   \dostartGNUPLOTinclusions}

\def\dostartGNUPLOTinclusions#1\stopGNUPLOTinclusions
  {\gdef\GNUPLOTinclusions{#1}%
   \egroup}

\def\startGNUPLOTgraphic#1%
  {\bgroup
   \obeylines
   \dostartGNUPLOTgraphic{#1}}

\def\dostartGNUPLOTgraphic#1#2\stopGNUPLOTgraphic
  {\doglobal\increment\GNUPLOTnumber
   \letgvalue{gpg:n:#1}\GNUPLOTnumber
   \setgvalue{gpg:d:\GNUPLOTnumber}{#2}%
   \egroup}

\def\useGNUPLOTgraphic
  {\dodoubleempty\douseGNUPLOTgraphic}

\def\douseGNUPLOTgraphic[#1][#2]%
  {\iffirstargument
     \@EA\dodouseGNUPLOTgraphic % like \externalfigure[name][optional args]
   \else
     \@EA\redouseGNUPLOTgraphic % like \useMPgraphic{name}
   \fi[#1][#2]}

\def\redouseGNUPLOTgraphic[#1][#2]#3%
  {\dodouseGNUPLOTgraphic[#3][]}

\def\dodouseGNUPLOTgraphic[#1][#2]%
  {\bgroup
   \doifdefined{gpg:n:#1}
     {\edef\GNUPLOTnumber{\getvalue{gpg:n:#1}}%
      \bgroup
      % hm, gnuplot only seems to handle lf so we cannot use \mappartolineend
      % also, in order to prevent fuzzy runs (or no runs) we add a ; to each line
      \def\par{;\rawcharacter{10}}%
      \immediate\openout\scratchwrite=\bufferprefix gnuplot-\GNUPLOTnumber.gpd
      \immediate\write\scratchwrite{\GNUPLOTinclusions}%
      \immediate\write\scratchwrite{set output "\bufferprefix gnuplot-\GNUPLOTnumber.ps"}%
      \immediate\write\scratchwrite{\getvalue{gpg:d:\GNUPLOTnumber}}%
      \immediate\write\scratchwrite{end}%
      \immediate\closeout\scratchwrite
      \egroup
      \processGNUPLOTgraphic{\bufferprefix gnuplot-\GNUPLOTnumber}%
      \convertGNUPLOTgraphic{\bufferprefix gnuplot-\GNUPLOTnumber}%
      \externalfigure[\bufferprefix gnuplot-\GNUPLOTnumber.pdf][#2]}%
   \egroup}

% we assume pdf inclusion, eps wil be implemented when applicable

\def\processGNUPLOTgraphic#1%
  {\doifelse\operatingsystem{mswin}
     {\executesystemcommand{start pgnuplot #1.gpd}} % start is needed else gp fails
     {\executesystemcommand{gnuplot #1.gpd}}}

\def\convertGNUPLOTgraphic#1%
  {\executesystemcommand{texmfstart newpstopdf #1.ps}}

\protect \doifnotmode{demo}{\endinput}

\starttext

\startGNUPLOTinclusions
    set title "trigonometry"
    set terminal postscript
\stopGNUPLOTinclusions

\startGNUPLOTgraphic{sin}
    plot sin(x)
\stopGNUPLOTgraphic

\startGNUPLOTgraphic{cos}
    plot cos(x)
\stopGNUPLOTgraphic

\useGNUPLOTgraphic{sin}

\useGNUPLOTgraphic{cos}

\useGNUPLOTgraphic[cos]

\useGNUPLOTgraphic[sin][height=3cm]

\stoptext
