summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/third/gnuplot/t-gnuplot.tex
blob: f5fcff508155becec720f5ddbfd6e70397bd4dfc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
%D \module
%D   [       file=t-gnuplot,
%D        version=2011.01.10,
%D          title=\CONTEXT\ Extra Modules,
%D       subtitle=\GNUPLOT\ Inclusion,
%D         author={Hans Hagen, Taco Hoekwater, Mojca Miklavec, (Aditya Mahajan)},
%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.

%D This module is used for creating gnuplot graphs on-the-fly and including them
%D into documents.
%D
%D Known Bugs:
%D
%D \startitemize
%D \item spurious space \& page
%D \stopitemize
%D 
%D TODO (Optimisations):
%D
%D \startitemize
%D \item optimize the number of gnuplot runs (if possible, gnuplot should be run only once)
%D \item optimize the number of times for loading/converting an already used graphic
%D \item pstopdf is a bit slow
%D \stopitemize
%D
%D TODO (Handle things that may go wrong):
%D - gnuplot executable doesn't exist
%D - context terminal isn't available or some other failure in gnuplot script (no file created)
%D - write18 disabled (you may call gnuplot later - create a script)
%D
%D TODO (Missing functionality):
%D
%D - a lot ...

\writestatus{loading}{Gnuplot module}

\startmodule[gnuplot]

\unprotect

\def\c!terminal {terminal}
\def\c!options  {options}
\def\c!pointset {pointset}
\def\c!purge    {purge}

%D MPextensions
%D
%D XXX
%D if possible, they should be specific to \type{\startGNUPLOTgraphic},
%D so unvisible to \type{MPcode}
%D (probably something like \type+\appendtoks ... to\everyGNUPLOTgraphic+)
\startMPextensions
	% load metapost macros (only once)
	input mp-gnuplot.mp ;
	% number of points defined with \setupGNUPLOTterminal[pointset=...]
	%gp_num_points_with_tex := \gp:num:pointswithtex;
	gp_num_points_with_tex := 3;
	% main color should be set equal to the current text color
	gp_color_foreground := \MPcolor{currentcolor};
	gp_color_lt[-2] := gp_color_foreground;
	% TODO: is there any chance to make this local to gnuplot?
	% linejoin & linecap
	linejoin := \@@GNUPLOT@term@context@linejoin;
%	linecap  := \gp:term:context:linecap;
	% dashes or solid? (true/false)
	gp_use_dashed := \@@GNUPLOT@term@context@is@dashed;
	% dashlength scale
%	gp_scale_dashlength := \gp:term:context:dashlength;
	% linewidth scale
	gp_scale_linewidth := \@@GNUPLOT@term@context@linewidth;
	
	gp_scale_text := \@@GNUPLOT@term@context@textscale;

	% linejoin := \gp:term:context:linejoin;
	% linecap  := \gp:term:context:linecap;
	% % dashes or solid? (true/false)
	% gp_use_dashed := \gp:term:context:dashed;
	% % dashlength scale
	% gp_scale_dashlength := \gp:term:context:dashlength;
	% % linewidth scale
	% gp_scale_linewidth := \gp:term:context:linewidth;
	
	gp_points_with := gp_points_with_\@@GNUPLOT@term@context@points;
	
\stopMPextensions

%D (hopefully) temporary solution for handling the color in expressions like
%D    \type+draw \sometxt{...} withcolor red+
\chardef\TeXtextcolormode\zerocount

%D We have to make sure that runtime MP graphics are enabled,
%D otherwise sizes of labels created by \type+\sometxt{}+ would be wrong
%D if user didn't enable that option in \type{cont-usr.tex}.
%D (Taco says it might be a bug, but let's not worry too much about it.
%D  If it will be resolved, we may delete this.)
\runMPgraphicstrue

%D We need a \type{\strut} in front of labels for better vertical centering.
%D This might still fail for Zapfino and alike where \type{\strut} might be smaller
%D than the actual font height.
%D
%D TODO (optional improvement): instead of placing \type{\strut} in front,
%D create a \type{\hbox} and adjust it's dimensions to \type{\strut}'s height and depth.

%D Aditya, thanks a lot for requesting it!
%D Hans, thanks a lot for implementing this!
\definetextext[gp]{\strut}
%D TODO (feature request):
%D \starttyping
%D    \sometxt[gp][ss,20pt]{abc}
%D \stoptyping
%D should become equivalent to
%D \starttyping
%D    \sometxt{\switchtobodyfont[ss,20pt]\strut abc}
%D \stoptyping
%D
%D Or, even more drastic perhaps, I would love to implement
%D \starttyping
%D    \sometxt[gp][iwona,bold,c]{abc}
%D \stoptyping
%D meaning: iwona, bold typeface, size \quote{c}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%D The following patch has been written by Aditya and seems to work OK,
%D however it would be more clean to have this functionality in the core.
%D Redefining low-level macros might be a bit dangerous, esp. since
%D they tend to change over time.
%D
%D At the moment the hack only works in MKII; for MKIV it needs to be fixed
%D
%D TODO: remind Hans to implement it in core ;)
%D       and remove this patch from the module
%D
\ifx\directlua\undefined
\long\def\redofiltersometxt[#1]%
{\doifnextcharelse[{\reredofiltersometxt[#1]}{\redodofiltersometxt[#1]}}

\long\def\redodofiltersometxt[#1]#2%
  {\increment\txtcounter
   \TeXtext[#1]\txtcounter{#2}%
   \filtersometxt}

\long\def\reredofiltersometxt[#1][#2]#3%
  {\increment\txtcounter
   \TeXtext[#1]\txtcounter{\switchtobodyfont[#2]\strut#3}%
   \filtersometxt}
\fi

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%D This was needed for the old method of
%D \type{put_text("some text")} using textext: not used/supported any more
%\ifx\directlua\undefined
%	\forceMPTEXcheck{put_text}
%\fi
%\runMPTEXgraphicstrue

\newcounter\GNUPLOTnumber

%D \macros
%D   {startGNUPLOTinclusions, resetGNUPLOTinclusions}
%D
%D For those who want to have two or more graphs with similar options,
%D these options may be included inside \type{\startMPinclusions ... \stopMPinclusions}
%D and will be place on the top of the created \GNUPLOT\ script.
%D
%D \starttyping
%D \startGNUPLOTinclusions
%D    set xlabel '$x$'
%D    set ylabel '$y$'
%D    set format y "%.1f"
%D \stopGNUPLOTinclusions
%D
%D \startGNUPLOTscript[sin]
%D    plot sin(x)
%D \stopGNUPLOTscript
%D \startGNUPLOTscript[cos]
%D    plot cos(x)
%D \stopGNUPLOTscript
%D \stoptyping
\long\def\startGNUPLOTinclusions
  {\def\stopGNUPLOTinclusions{\ifx\savebuffer\undefined \else \savebuffer[gnuplot-inclusions]\fi}%
   \dostartbuffer[gnuplot-inclusions][startGNUPLOTinclusions][stopGNUPLOTinclusions]}

% \def\resetGNUPLOTinclusions{\let\GNUPLOTinclusions\empty}

% creates an empty file (there must be a cleaner way to do it)
\def\resetGNUPLOTinclusions
  {\immediate\openout\scratchwrite=\jobname-gnuplot-inclusions.tmp
   \immediate\closeout\scratchwrite}

%\startbuffer[gnuplot-inclusions]\stopbuffer\ifx\savebuffer\undefined \else \savebuffer[gnuplot-inclusions]\fi}

\resetGNUPLOTinclusions

%D On the other hand, one can probably achieve the same effect
%D when drawing two plots inside the same script, like that:
%D
%D \starttyping
%D \startGNUPLOTscript[sin and cos]
%D    set xlabel '$x$'
%D    set ylabel '$y$'
%D    set format y "%.1f"
%D    plot sin(x)
%D    plot cos(x)
%D \stopGNUPLOTscript
%D \stoptyping
%D
%D and then recall the graphics using \type{\useGNUPLOTgraphic[sin and cos][1]}.

%D \macros
%D   {startGNUPLOTscript}
%D
%D \starttyping
%D \startGNUPLOTscript{some name}
%D   plot sin(x)
%D \stopGNUPLOTscript
%D \stoptyping

\def\startGNUPLOTscript
  {\bgroup\dosingleempty\dostartGNUPLOTscript}

% \def\redostartGNUPLOTscript#1
%   {\obeylines
%    \catcode`\%=\@@letter
%    \catcode`\|=\@@letter
%    \catcode`\$=\@@letter
%    \dodostartGNUPLOTscript{#1}%
% %\dostartGNUPLOTscript[#2]
% }
% 
% \def\dostartGNUPLOTscript[#1]%
%   {\iffirstargument
%      \obeylines
%      \catcode`\%=\@@letter
%      \catcode`\|=\@@letter
%      \catcode`\$=\@@letter
%      \dodostartGNUPLOTscript{#1}%
%    \else
%      \redostartGNUPLOTscript
%    \fi
% }

\def\dostartGNUPLOTscript[#1]%
  {%\iffirstargument
     \dodostartGNUPLOTscript{#1}%
   %\else
   %  \dodostartGNUPLOTscript
   %\fi
}

% gps:n:{name} = gnuplotscript : number : {name} - number of script with name {name}
\long\def\dodostartGNUPLOTscript#1%#2\stopGNUPLOTscript
  {\doglobal\increment\GNUPLOTnumber
   \letgvalue{gps:n:#1}\GNUPLOTnumber
   % in case of LuaTeX we need to write the buffer into file explicitely
   \def\stopGNUPLOTscript{\egroup \ifx\savebuffer\undefined \else \savebuffer[gnuplot-\GNUPLOTnumber]\fi}%
   \dostartbuffer[gnuplot-\GNUPLOTnumber][startGNUPLOTscript][stopGNUPLOTscript]%
   }

% When are the graphics processed/read?
%
% - \gps:n:{name} (gnuplot script:name:{name}) holds the number of gnuplot script;
%   - that number was defined if we created the plot using \startGNUPLOTscript{name}
%   - and most probably undefined if we only issued \processGNUPLOTfile[name][filename]
%     => if, at the time of issuing \processGNUPLOTfile[name][filename], \gps:n:{name} is not defined,
%        it should be defined at that time
% - \gpe:{name}:{terminal} (gnuplot graphic executed:{name}:{terminal})
%   is defined if we executed the command for that specific name and for that terminal
%   (once it will probably be 0 for a failed run and 1 for a successful one)

% \useGNUPLOTgraphic[name] has three different ways of working:
% - it can be called after \processGNUPLOTfile[name][filename]
%   which defined MP graphics that are now used
% - it can be called for the first time under the current terminal
%   in that case it compiles the graphic and includes it
% - it can be called for the second, third, ... time
%   in which case it only includes files without compiling it

%D \macros
%D   {useGNUPLOTgraphic}

% 5 ways of calling it:
% - \useGNUPLOTgraphic{name}
% - \useGNUPLOTgraphic[name]
% - \useGNUPLOTgraphic[name][1,2,5]
% - \useGNUPLOTgraphic[name][width=.8\textwidth]
% - \useGNUPLOTgraphic[name][1,2,5][width=.8\textwidth]

% this code takes care of reading arguments
\def\useGNUPLOTgraphic
	{\dotripleempty\douseGNUPLOTgraphic}

\def\douseGNUPLOTgraphic[#1][#2][#3]%
	{\doifelse{#3}{}{%
		% < 3 arguments
		\doifelse{#2}{}%
			% 1 argument
			% as in \useGNUPLOTgraphic[name]
			{\dodouseGNUPLOTgraphic[#1][][]}%
			% % as in \useGNUPLOTgraphic{name}
			% {\redouseGNUPLOTgraphic[][][]}%
			% 2 arguments
			{\doifassignmentelse{#2}%
				% as in \useGNUPLOTgraphic[name][width=.8\textwidth]
				{\dodouseGNUPLOTgraphic[#1][][#2]}%
				% as in \useGNUPLOTgraphic[name][1,2,5]
				{\dodouseGNUPLOTgraphic[#1][#2][]}%
			}%
		}%
		% as in \useGNUPLOTgraphic[name][1,2,5][width=.8\textwidth]
		{\dodouseGNUPLOTgraphic[#1][#2][#3]}%
	}

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

% and this code actually does something with it

%D \doifundefined      {string}    {...}
%D \doifdefined        {string}    {...}
%D \doifundefinedelse  {string}    {then ...} {else ...}
%D \doifdefinedelse    {string}    {then ...} {else ...}

% \doifGNUPLOTscriptdefined{name}{...}

%  if gnuplot script with {name} and current terminal has already been processed
% \doifGNUPLOTscriptprocessed{name}{...}

% \letGNUPLOTscriptprocessed[optional terminal]{name} signals that the gnuplot script named {name}
% has already been processed with the current terminal
% TODO: currently it is always defined to be one as soon as one tries to process it,
%       even if en error is produced; in future it would be helpful if it would be set to zero
%       if it was unsuccessfully executed;
% that is needed, since scripts are processed only when one first asks for including the graphic
\def\letGNUPLOTscriptprocessed
	{\dosingleempty\doletGNUPLOTscriptprocessed}
%\def\doletGNUPLOTscriptprocessed[#1]#2%
%{\letgvalue{gpe:#1:\@@GNUPLOTterminal}\plusone}
\def\doletGNUPLOTscriptprocessed[#1]#2%
	{\iffirstargument
	 	% terminal has been specified
	 	\letgvalue{gpe:#2:#1}\plusone
	 \else
	 	% no terminal specified - use the current one
	 	\letgvalue{gpe:#2:\@@GNUPLOTterminal}\plusone
	 \fi}

% Although that should preferably not happen, one might come to an idea of defining
% a gnuplot script with the same name as already defined.
% In that case the old script cannot be referenced any more, but we can still try
% to do out best to make it work anyway. We have to do two things:
% - claim that script with that name hasn't been defined yet, so that processing will happen again
%   (TODO: do it in a more elegant way for all known terminals)
% - undefine any metapost graphics (TODO: I have no idea yet how it can be done)
%
% \resetGNUPLOTscriptprocessed{name}
\def\resetGNUPLOTscriptprocessed#1%
	{\bgroup
		% claim that the script with {name} (#1) hasn't been processed with {terminal} (##1) yet
		\def\undefineGNUPLOTscriptforterminal##1{\letbeundefined{gpe:#1:##1}}%
		% TODO: the list of available terminals should be generated automatically
		\processcommalist[context,postscript,ps,eps,pdf,metapost,mp,png]\undefineGNUPLOTscriptforterminal
		\resetGNUPLOTgraphics{#1}%
	\egroup}

% \resetGNUPLOTgraphics{name} undefines any gnuplot graphic defined with \startGNUPLOTgraphic[name][number]
\def\resetGNUPLOTgraphics#1%
	{\doloop
		{\doifMPgraphicelse{gpg:#1:\recurselevel}%
		% TODO: be aware - @@MPG is low-level ConTeXt variable which might change without notice,
		% it would be better to call this \undefineMPgraphic{gpg:#1:\recurselevel} or something similar
		{\letbeundefined{@@MPGgpg:#1:\recurselevel}}%
		{\exitloop}}%
	}%

% TODO: this can probably be done in a better way
\def\doifGNUPLOTscriptprocessed#1#2%
	{\doifdefined      {gpe:#1:\@@GNUPLOTterminal}{#2}}
\def\doifGNUPLOTscriptprocessedelse#1#2#3%
	{\doifdefinedelse  {gpe:#1:\@@GNUPLOTterminal}{#2}{#3}}
\def\doifGNUPLOTscriptnotprocessed#1#2%
	{\doifundefined    {gpe:#1:\@@GNUPLOTterminal}{#2}}
\def\doifGNUPLOTscriptnotprocessedelse#1#2#3%
	{\doifundefinedelse{gpe:#1:\@@GNUPLOTterminal}{#2}{#3}}


% private
% \writeandprocessGNUPLOTscript{name}
\def\writeandprocessGNUPLOTscript#1%
	% TODO: if gps:n:#1 (holding the script content) is not defined,
	% error or warning should be issued
	%
	% only process the script if it has been defined and not processed before for the current terminal
	{\doifdefined{gps:n:#1}{\doifGNUPLOTscriptnotprocessed{#1}{%
		% \gpe:{name}:{terminal} is defined
		%\setgvalue{gpe:#1:\@@GNUPLOTterminal}{#2}
		\letGNUPLOTscriptprocessed{#1}%
		% call to gnuplot and processing/converting the graphics is only needed in the first ConTeXt run
		\doifmode{*\v!first}{% (perhaps also: if files haven't changed)
			\bgroup
			% catcodes trickery & alike
			%\the\everyGNUPLOTscript
			% open file for writing
			\immediate\openout\scratchwrite=\GNUPLOTfile.plt
			% TODO: terminal-specific options (default or provided by the user)
			\immediate\write\scratchwrite{\letterhash\space Do not modify this file - all changes will be overwritten}%
			\immediate\write\scratchwrite{\letterhash\space Change \jobname.tex instead.}%
			\immediate\write\scratchwrite{set terminal \@@GNUPLOToutput\space\@@GNUPLOToptions}%
			% add common inclusions in scripts for multiple similar plots
			%\doifnotempty
			%	{\GNUPLOTinclusions}{\immediate\write\scratchwrite{\GNUPLOTinclusions}}%
			% include common gnuplot 'inclusions'
			\immediate\write\scratchwrite{load '\jobname-gnuplot-inclusions.tmp'}%
			% output file
			\immediate\write\scratchwrite{set output "\@@GNUPLOTresult"}%
			% write main contents of the script, like "plot sin(x)"
			%\immediate\write\scratchwrite{\getvalue{gps:d:\GNUPLOTnumber}}%
			\immediate\write\scratchwrite{load '\GNUPLOTfile.tmp'}%
			\immediate\closeout\scratchwrite
			\egroup
			% TODO:
			% - check the state of write18 and warn the user if it's disabled,
			%   otherwise just everyone will start complaining that the module doens't work
			% - check if execution was successful; possible pitfalls:
			%   - gnuplot doesn't exist as a binary
			%   - gnuplot doesn't support context terminal
			%     or some other error in script which results in empty output file
			%
			% run gnuplot & execute the script that has just been written
			% \executesystemcommand{mtxrun --ifchanged=\GNUPLOTfile.plt\space
			% 	--direct \@@GNUPLOTprogram\space\GNUPLOTfile.plt}%
			\executesystemcommand{mtxrun --direct \@@GNUPLOTprogram\space\GNUPLOTfile.plt}%
			\writestatus{aaa}{mtxrun --direct \@@GNUPLOTprogram\space\GNUPLOTfile.plt}%
			\convertGNUPLOTgraphic
		}%
		% for ConTeXt terminal only - read the result
		\doif{\@@GNUPLOTterminal}{context}{\processGNUPLOTfile[#1][\@@GNUPLOTresult]}%
	}}}

% \dodouseGNUPLOTgraphic[name][numbers][options]
\def\dodouseGNUPLOTgraphic[#1][#2][#3]%
	{\bgroup
	\doifdefinedelse{gps:n:#1}
		{\edef\GNUPLOTnumber{\getvalue{gps:n:#1}}%
		 \edef\GNUPLOTfile  {\bufferprefix gnuplot-\GNUPLOTnumber}%
		\writeandprocessGNUPLOTscript{#1}%
		% "ctxtools --purge" should delete the gnuplot script and other intermediate files
		% (but it seems that they are deleted automatically already; preferred or not?)
%		\registertempfile{\GNUPLOTfile.plt}
%		\registertempfile{\@@GNUPLOTresult}
%		\registertempfile{\@@GNUPLOTfinalresult}
		\doifelse{\@@GNUPLOTterminal}{context}%
		% for ConTeXt terminal only
		{\doifelse{#2}{}%
			% if no explicit figure number was specified, include all the figures
			{\doloop
				{\doifMPgraphicelse{gpg:#1:\recurselevel}%
				{\scale[#3]{\reuseMPgraphic{gpg:#1:\recurselevel}}}%
				{\exitloop}}}%
			% if numbers were specified, include the figures specified in the list only
			{\begingroup
				\def\useGNUPLOTgraphicN##1{\doifMPgraphicelse
					{gpg:#1:##1}%
					{\scale[#3]{\reuseMPgraphic{gpg:#1:##1}}}%
					% if graphic doesn't exist: draw a dummy frame instead and warn the user
					{\scale[#3]{\framed[frame=on,width=5in,height=3in,align={middle,lohi}]{GNUPLOT graphic #1, Nr. ##1 doesn't exist}}}}%
				\processcommalist[#2]\useGNUPLOTgraphicN
				\endgroup
			}%
		}%
		% for all the other terminals
		% TODO: add more safety switches (if pages don't exist for example)
		{\doifelse{#2}{}%
			% if no explicite figure number was specified, include all the pages
			% TODO: properly handle METAPOST & PNG (only works for (E)PS & PDF) !!!
			{\getfiguredimensions[\@@GNUPLOTfinalresult]%
				\dorecurse{\noffigurepages}{\externalfigure[\@@GNUPLOTfinalresult][page=\recurselevel,#3]}}%
			% if numbers were specified, include the pages specified in the list only
			{\begingroup
				\def\useGNUPLOTgraphicN##1{\externalfigure[\@@GNUPLOTfinalresult][page=##1,#3]}%
				\processcommalist[#2]\useGNUPLOTgraphicN
				\endgroup
			}%
		}%
	}%
	% else part - in case that graphic gets included with \processGNUPLOTfile[name][file]
	{\doifelse{\@@GNUPLOTterminal}{context}
		% TODO: this part has been literally copied from above; create a subroutine instead
		{\doifelse{#2}{}%
			% if no explicit figure number was specified, include all the figures
			{\doloop
				{\doifMPgraphicelse{gpg:#1:\recurselevel}%
				{\scale[#3]{\reuseMPgraphic{gpg:#1:\recurselevel}}}%
				{\exitloop}}}%
			% if numbers were specified, include the figures specified in the list only
			{\begingroup
				\def\useGNUPLOTgraphicN##1{\doifMPgraphicelse
					{gpg:#1:##1}%
					{\scale[#3]{\reuseMPgraphic{gpg:#1:##1}}}%
					% if graphic doesn't exist: draw a dummy frame instead and warn the user
					{\scale[#3]{\framed[frame=on,width=5in,height=3in,align={middle,lohi}]{GNUPLOT graphic #1, Nr. ##1 doesn't exist}}}}%
				\processcommalist[#2]\useGNUPLOTgraphicN
				\endgroup
			}%
		}%
		{\message{The gnuplot graphic #1 has not been defined.}}}%
	\egroup}

%D \macros
%D   {setupGNUPLOT}

\def\setupGNUPLOT
%  {\dodoubleempty\getparameters[@@GNUPLOT]}
  {\dosingleargument\dosetupGNUPLOT}

\def\dosetupGNUPLOT[#1]%
  {\getparameters[@@GNUPLOT][#1]%
   % define all the necessary points according to the option "pointset"
   %
   % XXX: no idea why this is needed, but otherwise it complains that @@GNUPLOTpointset is undefined
   \edef\currentGNUPLOTpointset{\@@GNUPLOTpointset}%
   \ifx\directlua\undefined
   \startTeXtexts
    \doloop{\doifelseconversionnumber{\@@GNUPLOTpointset}{\recurselevel}%
       % +500 is a hack (hopefully the plot doesn't contain more than 500 labels)
       % otherwise the points would be overwritten by labels with another \TeXtext:
       % it might need a fix in ConTeXt core
       {\TeXtext{\numexpr\recurselevel+500\relax}{{\convertnumber{\currentGNUPLOTpointset}{\recurselevel}}}}%
       {\exitloop}}%
   \stopTeXtexts
   \fi
   % \gp:num:pointswithtex is passed to metapost, so that it knows
   % how many points are defined and chooses the proper point
   % form a set of the defined ones
   %
   % for safety reasons define \gp:num:pointswithtex to be equal to 1 (it can only increase later), otherwise:
   % - (something mod 0) won't work
   % - if conversion is not defined, the number will retain its old value (not desirable)
   \edef\gp:num:pointswithtex{1}%
   \doloop{\doifelseconversionnumber{\@@GNUPLOTpointset}{\recurselevel}%
       {\edef\gp:num:pointswithtex{\recurselevel}}%
       {\exitloop}}%
   % TODO:
   % - issue a warning if user wants to use points with TeX,
   %   but pointset= is undefined (if the first point doesn't exist)
   % - no idea what happens if conversion is infinite,
   %   so try to stop at some reasonable value (100?)
   % - current implementation redefines the points even if only terminal type
   %   has been set to some other value (which is a stupid approach, but I
   %   wanted to have a working version first and will consider efficiency later)
   % - it may be that the old points remain defined if conversion changes
   %   (perhaps they should be undefined again?)
  }

% Hans has written this piece of code, but:
% - "start" and "/MIN" caused problems
% - pgnuplot is not much more "portable" than gnuplot
%   best thing to do is to create a "gnuplot.bat" somewhere in PATH
%
%\def\processGNUPLOTscript
%  {\doifelse\operatingsystem{mswin}
%     {\executesystemcommand{start /MIN pgnuplot \GNUPLOTfile.plt}} % start is needed else gp fails
%     {\executesystemcommand{gnuplot \GNUPLOTfile.plt}}}
%\def\processGNUPLOTscript
%  {\executesystemcommand{gnuplot \GNUPLOTfile.plt}}

% TODO: check if write18 is enabled; if not, issue a command and warn the user that running the module might be pointless or that he/she has to run gnuplot on the produced files manually
\def\convertGNUPLOTgraphic
  {\doifsomething\@@GNUPLOThandle{\writestatus{conversion}{\@@GNUPLOThandle}\executesystemcommand{\@@GNUPLOThandle}}}

%D \macros
%D   {processGNUPLOTfile}

%D It's needed to input a file resulting from a gnuplot run (with ConTeXt terminal).
%D It reads the file and "saves" the metapost graphics defined in that file,
%D so that they can be used with \usegnuplotgraphic (low level: \reuseMPgraphic) later
%D
% \processGNUPLOTfile[NAME][filename]
\def\processGNUPLOTfile
  {\dodoubleargument\doprocessGNUPLOTfile}

\def\doprocessGNUPLOTfile[#1][#2]%
  {% we first define two commands: \startGNUPLOTgraphic & \stopGNUPLOTgraphic to read the files in;
   % files that gnuplot creates in non-standalone mode look approximately like this:
   %
   % \startGNUPLOTgraphic[1]
   % ... metapost commands to draw the graph ...
   % \stopGNUPLOTgraphic
   % \startGNUPLOTgraphic[2]
   % ... metapost commands to draw the graph ...
   % \stopGNUPLOTgraphic
   % ...
   %
   % while reading the file in, metapost graphics named "gpg:{name}:{number}" are defined
   %
   % (we might need some additional arguments later on, but for now the figure number should suffice)
   \def\startGNUPLOTgraphic
      {%\obeyMPlines % <- no longer a problem
       \def\obeyedline{}% <- thanks to this
       \dosingleargument\dostartGNUPLOTgraphic}%
   \long\def\dostartGNUPLOTgraphic[##1]##2\stopGNUPLOTgraphic
      {\startreusableMPgraphic{gpg:#1:##1}##2\stopreusableMPgraphic}%
   %
   % if gps:n:{name} (which is a number) is not defined,
   % then we first have to increase the \GNUPLOTnumber counter and associate this name with the new number

   % , then we should now also define
   % gpe:{number}:context which should signal later on that the graphic
%   \doifdefined{gps:n:#1}
   % input the file
   % TODO: error / warning if the files doesn't exist
   \readlocfile{#2}{}{}% the third argument should be: if file not found
   % this file is known
   % TODO \letgvalue{gpf:#1}\plusone
  }

%D \macros
%D   {startGNUPLOTpage}
%D
%D Used for standalone \GNUPLOT\ figures & written out by gnuplot in standalone mode.
%D
%D Instead of having to \type{\useGNUPLOTgraphic}, a figure is inserted directly using
%D \type{\startMPpage ... }\type{\stopMPpage}.
%D
%D A high-level command is provided if some more advance features will
%D be needed in the future and to be able to ensure backward compatibility.

%D We need to preserve line breaks, otherwise metapost runs into troubles for longer input.
\def\startGNUPLOTpage
	{\begingroup\obeyMPlines\dostartGNUPLOTpage}

\long\def\dostartGNUPLOTpage#1\stopGNUPLOTpage
	{\endgroup\startMPpage#1\stopMPpage}

% TODO: use a separate instance for MKIV
%
% \defineMPinstance[gnuplot]
% \startTEXpage\startMPcode{gnuplot}
% \stopMPcode\stopTEXpage

% TODO: currently, the following definitions are used,
%       but the ones below would be preferred

\def\defineGNUPLOThandle#1#2#3#4#5#6% name; output; suffix; conversion-method; gnuplot's result; final result
  {\setvalue{@@GNUPLOT-#1}{{#2}{#3}{#4}{#5}{#6}}}

\def\@@GNUPLOToutput{\filterfromvalue{@@GNUPLOT-\@@GNUPLOTterminal}51}
\def\@@GNUPLOTsuffix{\filterfromvalue{@@GNUPLOT-\@@GNUPLOTterminal}52}
\def\@@GNUPLOThandle{\filterfromvalue{@@GNUPLOT-\@@GNUPLOTterminal}53}
\def\@@GNUPLOTresult{\filterfromvalue{@@GNUPLOT-\@@GNUPLOTterminal}54}
\def\@@GNUPLOTfinalresult{\filterfromvalue{@@GNUPLOT-\@@GNUPLOTterminal}55}

%                    name       "set term" suffix conversion (system command)                         gnuplot's result          final result
% (suffix is probably not needed any more since full names were introduced)
\defineGNUPLOThandle
	{postscript}{postscript color}{ps}{mtxrun pstopdf \GNUPLOTfile-ps.ps}
	{\GNUPLOTfile-ps.ps}{\GNUPLOTfile-ps.pdf}
\defineGNUPLOThandle
	{ps}{postscript color}{ps}{mtxrun pstopdf -dAutoRotatePages=/PageByPage \GNUPLOTfile-ps.ps}
	{\GNUPLOTfile-ps.ps}{\GNUPLOTfile-ps.pdf}
\defineGNUPLOThandle
	{eps}{postscript color eps}{ps}{mtxrun pstopdf -dEPSCrop \GNUPLOTfile-eps.eps}
	{\GNUPLOTfile-eps.eps}{\GNUPLOTfile-eps.pdf}
\defineGNUPLOThandle
	{pdf}{pdf}{pdf}{}
	{\GNUPLOTfile-pdf.pdf}{\GNUPLOTfile-pdf.pdf}
\defineGNUPLOThandle
	{metapost}{mp}{mp}{mtxrun mptopdf \GNUPLOTfile-mp.mp}
	{\GNUPLOTfile-mp.mp}{\GNUPLOTfile-mp-0.pdf}
\defineGNUPLOThandle
	{mp}{mp}{mp}{mtxrun mptopdf \GNUPLOTfile-mp.mp}
	{\GNUPLOTfile-mp.mp}{\GNUPLOTfile-mp-0.pdf}
\defineGNUPLOThandle
	{png}{png}{png}{}
	{\GNUPLOTfile-png.png}{\GNUPLOTfile-png.png}
\defineGNUPLOThandle
	{context}{context textscale \@@GNUPLOT@term@context@textscale}{tex}{}
	{\GNUPLOTfile-ctx.tex}{\GNUPLOTfile-ctx.tex}
% \doifnotempty{\@@GNUPLOT@term@context@textscale}{textscale \@@GNUPLOT@term@context@textscale}

\def\defineGNUPLOTterminal
	{\dodoubleargument\dodefineGNUPLOTterminal}

\def\dodefineGNUPLOTterminal[#1][#2]%
	{\doifassignmentelse{#2}%
	 	% define a proper terminal
	 	{}% TODO
	 	% only define a synonym for that terminal
	 	{}% TODO
	}

%D {\sl terminal}: argument to be passed to gnuplot in the form of "set term {\sl terminal}"
%D {\sl defaultoptions}: options to be passed to gnuplot after terminal name:
%D      set term {\sl teminal} {\sl defaultoptions}
%D      not to be touched by users
%D {\sl suffix}: filename suffix
%D {\sl result}: the file to which gnuplot should output the result
%D      set output {\sl result}
%D {\sl convertwith}: command for conversion from gnuplot-generated file to a file that can be read by \CONTEXT; may be empty
%D {\sl finalresult}: file resulting after the conversion to be read by \CONTEXT

%D \POSTSCRIPT\ terminal
\defineGNUPLOTterminal
	[postscript]
	[terminal=postscript,
	 defaultoptions=color, % TODO: only if \setupcolors[state=start]
	 suffix=ps,
	 result=\GNUPLOTfile-ps.ps,
	 % TODO: -dAutoRotatePages=/PageByPage or remove that option from the default ones
	 convertwith={mtxrun pstopdf \GNUPLOTfile-ps.ps},
	 finalresult=\GNUPLOTfile-ps.pdf,
	]
%D Define \type{ps} as a synonym for \type{postscript} terminal
\defineGNUPLOTterminal
	[ps]
	[postscript]

%D \EPS\ terminal:
%D - same terminal as for \POSTSCRIPT, but slightly different handling
\defineGNUPLOTterminal
	[eps]
	[terminal=postscript,
	 defaultoptions=eps color,
	 suffix=eps,
	 result=\GNUPLOTfile-eps.eps,
	 % TODO: -dEPSCrop
	 convertwith={mtxrun pstopdf -dEPSCrop \GNUPLOTfile-eps.eps},
	 finalresult=\GNUPLOTfile-eps.pdf,
	]

%D \PDF\ terminal
\defineGNUPLOTterminal
	[pdf]
	[terminal=pdf,
	 defaultoptions=,
	 suffix=pdf,
	 result=\GNUPLOTfile-pdf.pdf,
	 convertwith=,
	 finalresult=\GNUPLOTfile-pdf.pdf,
	]

%D \METAPOST\ terminal:
%D - deprecated: use the \CONTEXT\ terminal instead,
%D   which was built starting from the \METAPOST\ one,
%D   but improved in many aspects
\defineGNUPLOTterminal
	[mp]
	[terminal=mp,
	 defaultoptions=,
	 suffix=mp,
	 result=\GNUPLOTfile-mp.mp,
	 convertwith={mtxrun mptopdf \GNUPLOTfile-mp.mp},
	 % TODO: support for multiple graphics
	 % one would need a switch in mptopdf, so that a single pdf would be created instead of multiple ones
	 finalresult=\GNUPLOTfile-mp-0.pdf,
	]

\defineGNUPLOTterminal
	[metapost]
	[mp]

%D png terminal:
%D - bitmap
%D - no conversion needed
%D - new version pretty advanced
\defineGNUPLOTterminal
	[png]
	[terminal=png,
	 defaultoptions=,
	 suffix=png,
	 result=\GNUPLOTfile-png.png,
	 convertwith=,
	 finalresult=\GNUPLOTfile-png.png,
	]

%D \CONTEXT\ terminal (native)
\defineGNUPLOTterminal
	[context]
	[terminal=context,
	 defaultoptions=,
	 suffix=tex,
	 result=\GNUPLOTfile-ctx.tex,
	 convertwith=,
	 finalresult=\GNUPLOTfile-ctx.tex,
	]

\def\setupGNUPLOTterminal
	{\dodoubleargument\dosetupGNUPLOTterminal}

\def\dosetupGNUPLOTterminal[#1][#2]%
	{% TODO
		\getparameters[@@GNUPLOT@term@#1@][#2]
		% TODO: width & height
		% linejoin, linecap - I have to improve this !!!
	%	\edef\gp:term:context:linejoin{\@@GNUPLOT@term@context@linejoin}
	%	\edef\gp:term:context:linecap{\@@GNUPLOT@term@context@linecap}
		% dashed or solid lines?
		\doifsamestringelse{\@@GNUPLOT@term@context@dashed}{yes}% yes or no
			{\edef\@@GNUPLOT@term@context@is@dashed{true}}%
			{\edef\@@GNUPLOT@term@context@is@dashed{false}}%
	%		{\edef\gp:term:context:dashed{true}}%
	%		{\edef\gp:term:context:dashed{false}}%
	%	% dashlength scale
	%	\edef\gp:term:context:dashlength{\@@GNUPLOT@term@context@dashlength}
	%	% linewidth scale
	%	\edef\gp:term:context:linewidth{\@@GNUPLOT@term@context@linewidth}
	%	\doifsamestringelse{\@@GNUPLOT@term@context@points}{metapost}% tex or metapost
	}

\setupGNUPLOTterminal
	[context]
	[width=default,    % *default* (5in) | <dimension>
	 height=default,   % *default* (3in) | <dimension>
	 linejoin=rounded, % mitered | *rounded* | beveled
	 linecap=butt,     % *butt* | rounded (in gnuplot: round) | squared
	 dashed=yes,       % *yes* | no
	 dashlength=1,     % scaling factor for dash lengths
	 linewidth=1,      % scaling factor for line widths (1.0 means 0.4bp)
	 textscale=1,      % scaling factor for text labels
	 points=metapost]  % *metapost* | tex (Should points be drawn with MetaPost or TeX?)

% TODO: better scaling
\defineconversion
	[gnuplot:pointset]
	[{\scale[scale=800]{\mathematics{+}}},
	 {\scale[scale=800]{\mathematics{\times}}},
	 \mathematics{\ast},
	 {\scale[scale=700]{\mathematics{\square}}},
	 {\scale[scale=700]{\mathematics{\blacksquare}}},
	 \mathematics{\circ},
	 \mathematics{\bullet},
	 {\scale[scale=900]{\mathematics{\triangleup}}},
	 {\scale[scale=900]{\mathematics{\blacktriangle}}},
	 {\scale[scale=900]{\mathematics{\triangledown}}},
	 {\scale[scale=900]{\mathematics{\blacktriangledown}}},
	 {\scale[scale=800]{\mathematics{\lozenge}}},
	 {\scale[scale=800]{\mathematics{\blacklozenge}}}%,
%  {\rotate[rotation=45]{\mathematics{\square}}},
%  {\rotate[rotation=45]{\mathematics{\blacksquare}}},
	]

%\defineGNUPLOTcolor[red]    [r=1]
%\defineGNUPLOTcolor[green]  [g=1]
%\defineGNUPLOTcolor[blue]   [b=1]
%\defineGNUPLOTcolor[magenta][r=1,b=1]
%\defineGNUPLOTcolor[cyan]   [g=1,b=1]
%\defineGNUPLOTcolor[yellow] [r=1,g=1]
%\defineGNUPLOTcolor[black]  [s=0]
%\defineGNUPLOTcolor[orange] [r=1,g=.3,b=0]
%\defineGNUPLOTcolor[gray50] [s=.5]

%\defineGNUPLOTcolors
%	[default] % from PostScript
%	[red,green,blue,magenta,cyan,yellow,black,orange,gray50]

% TODO: testset is here only temporary & for testing
\setupGNUPLOT[program=gnuplot,\c!terminal=context,\c!purge=\v!yes,\c!options=,\c!pointset=gnuplot:pointset] % colors=postscript

%D Some additional typescripts which enable us using font "Arial" and "Helvetica"

%D Pretend the font to be serif as well, so that no "ss" switch is needed
\starttypescript [serif] [helvetica] [name]
  \definefontsynonym [Serif]            [Helvetica]
  \definefontsynonym [SerifBold]        [Helvetica-Bold]
  \definefontsynonym [SerifItalic]      [Helvetica-Oblique]
  \definefontsynonym [SerifSlanted]     [Helvetica-Oblique]
  \definefontsynonym [SerifBoldItalic]  [Helvetica-BoldOblique]
  \definefontsynonym [SerifBoldSlanted] [Helvetica-BoldOblique]
  \definefontsynonym [SerifCaps]        [Helvetica]
\stoptypescript

\beginOLDTEX

  \starttypescript [gnuplot] [texnansi,ec]
    \definetypeface [Helvetica] [rm] [serif]  [helvetica] [default] [encoding=\typescripttwo]
    \definetypeface [Helvetica] [ss] [sans]   [helvetica] [default] [encoding=\typescripttwo]
    \definetypeface [Arial]     [rm] [serif]  [helvetica] [default] [encoding=\typescripttwo]
    \definetypeface [Arial]     [ss] [sans]   [helvetica] [default] [encoding=\typescripttwo]
  \stoptypescript
  \usetypescript[gnuplot][ec]

\endOLDTEX
\beginNEWTEX

  \starttypescript [gnuplot]
    \definetypeface [Helvetica] [rm] [serif]  [helvetica] [default]
    \definetypeface [Helvetica] [ss] [sans]   [helvetica] [default]
    \definetypeface [Arial]     [rm] [serif]  [helvetica] [default]
    \definetypeface [Arial]     [ss] [sans]   [helvetica] [default]
  \stoptypescript
  \usetypescript[gnuplot]

\endNEWTEX

\stopmodule

\protect \doifnotmode{demo}{\endinput}


\starttext

\startGNUPLOTscript[exp]
set key bottom
set format x "%.1f"
set format y "%.1f"
set style fill solid 0.25 noborder
plot [0:3] 2/sqrt(pi)*exp(-x**2) t '$\frac{2}{\sqrt{\pi}}e^{-x^2}$' with filledcurves x1 lt 3, erf(x) lc 3 lw 2
\stopGNUPLOTscript

\useGNUPLOTgraphic[exp][width=.7\textwidth]


\startGNUPLOTinclusions
    set title "trigonometry"
\stopGNUPLOTinclusions

\startGNUPLOTscript[sin]
    plot sin(x)
\stopGNUPLOTscript

\startGNUPLOTscript[cos]
    plot cos(x)
\stopGNUPLOTscript

\useGNUPLOTgraphic[sin] \endgraf
\useGNUPLOTgraphic[cos] \endgraf
\useGNUPLOTgraphic[cos] \endgraf

\setupGNUPLOT[terminal=ps]  \useGNUPLOTgraphic[sin][object=no,height=2cm] \blank
%setupGNUPLOT[terminal=pdf] \useGNUPLOTgraphic[sin][object=no,height=2cm] \blank
%setupGNUPLOT[terminal=png] \useGNUPLOTgraphic[sin][object=no,height=2cm] \blank
\setupGNUPLOT[terminal=mp]  \useGNUPLOTgraphic[sin][object=no,height=2cm,options=color] \blank

\stoptext