##
## This is file `putfile.',
## generated with the docstrip utility.
##
## The original source files were:
##
## coordsys.dtx  (with options: `putfile')
## 
## 
##  This is the coordsys and logsys packages
##  Copyright 2000 by Mogens Lemvig Hansen,
##                    mlhansen@uniserve.com
## 
## This program may be distributed and/or modified under the
## conditions of the LaTeX Project Public License, either version 1.2
## of this license or (at your option) any later version.
## The latest version of this license is in
##   http://www.latex-project.org/lppl.txt
## and version 1.2 or later is part of all distributions of LaTeX
## version 1999/12/01 or later.
## 
## This program consists of the files coordsys.dtx and coordsys.ins
## 
putfile := proc(fn::{string, list(string), set(string)},
                 P::PLOT(anything) )
   option `Copyright 2000 by Mogens Lemvig Hansen`;
   description "version 0.2";
   local L, fns, i, V, scl;
   L := [args[3..-1]];
   if hasoption(L, 'scale', scl, 'L') then
      scl := evalf(scl);
      if not type(scl, [numeric, numeric]) then
         ERROR("the scale option must be [numeric, numeric] but got",
               scl);
      fi;
   else
      scl := [1,1];
   fi;
   if L <> [] then
      ERROR("unknown options", op(L));
   fi;
   if type(fn, 'string') then
      fns := [fn];
   else
      fns := fn;
   fi;
   L := map( `putfile/grabcurves`, [op(P)] );
   if nops(fns) <> nops(L) then
      ERROR("the number of filenames must match the number of curves",
            sprintf("received %d names but %d curves",
                    nops(fns), nops(L)                )  );
   fi;
   V := select(type, [op(P)], 'VIEW'(anything, anything));
   if nops(V) <> 1 then
      V := ['DEFAULT', 'DEFAULT'];
   else
      V := [ op([1,1], V), op([1,2], V) ];
   fi;
   for i from 1 to nops(L) do
      L[i] := remove(`putfile/isbad`, L[i], V);
      L[i] := map(u->[scl[1]*u[1], scl[2]*u[2]], L[i]);
   od;
   for i from 1 to nops(L) do
      writedata(fns[i], L[i]);
   od;
end;
`putfile/grabcurves` := proc(x)
   option `Copyright 2000 by Mogens Lemvig Hansen`;
   description "version 0.2";
   local L;
   if op(0, x) = 'CURVES' then
      L := [ op(x) ];
      L := select(type, L, 'list');
      RETURN(op(L));
   else
      RETURN(NULL);
   fi;
end;
`putfile/isbad` := proc(pt::list, view::list)
   option `Copyright 2000 by Mogens Lemvig Hansen`;
   description "version 0.2";
   if not type(pt, [numeric, numeric]) then RETURN(true) fi;
   if type(view[1], numeric..numeric) and
      ( op([1,1], view) > pt[1] or pt[1] > op([1,2], view) ) then
      RETURN(true);
   fi;
   if type(view[2], numeric..numeric) and
      ( op([2,1], view) > pt[2] or pt[2] > op([2,2], view) ) then
      RETURN(true);
   fi;
   false;
end;
