1
|
1 /* |
|
2 |
1827
|
3 Copyright (C) 1996 John W. Eaton |
1
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1
|
20 |
|
21 */ |
|
22 |
1297
|
23 #if defined (__GNUG__) |
|
24 #pragma implementation |
|
25 #endif |
|
26 |
240
|
27 #ifdef HAVE_CONFIG_H |
1192
|
28 #include <config.h> |
1
|
29 #endif |
|
30 |
1343
|
31 #include <cstring> |
|
32 |
1728
|
33 #include <string> |
|
34 |
1350
|
35 #include <fstream.h> |
|
36 #include <iostream.h> |
|
37 #include <strstream.h> |
|
38 |
|
39 #ifdef HAVE_UNISTD_H |
2442
|
40 #ifdef HAVE_SYS_TYPES_H |
524
|
41 #include <sys/types.h> |
2442
|
42 #endif |
524
|
43 #include <unistd.h> |
|
44 #endif |
1
|
45 |
524
|
46 #include "SLStack.h" |
|
47 #include "procstream.h" |
|
48 |
1802
|
49 #include "file-ops.h" |
1755
|
50 #include "str-vec.h" |
|
51 |
1352
|
52 #include "defun.h" |
|
53 #include "error.h" |
|
54 #include "gripes.h" |
|
55 #include "help.h" |
|
56 #include "load-save.h" |
1742
|
57 #include "mappers.h" |
|
58 #include "oct-obj.h" |
|
59 #include "pt-cmd.h" |
|
60 #include "pt-exp.h" |
|
61 #include "pt-plot.h" |
2124
|
62 #include "pt-walk.h" |
2209
|
63 #include "sighandlers.h" |
1750
|
64 #include "sysdep.h" |
1
|
65 #include "utils.h" |
2371
|
66 #include "variables.h" |
1
|
67 |
2176
|
68 // If TRUE, a replot command is issued automatically each time a plot |
|
69 // changes in some way. |
|
70 static bool Vautomatic_replot; |
|
71 |
|
72 // The name of the shell command to execute to start gnuplot. |
|
73 static string Vgnuplot_binary; |
|
74 |
2289
|
75 // TRUE if gnuplot appears to support multiple plot windows with X11. |
|
76 static bool Vgnuplot_has_frames; |
|
77 |
2176
|
78 // TRUE if gnuplot appears to support multiplot. |
|
79 static bool Vgnuplot_has_multiplot; |
|
80 |
1358
|
81 // The number of lines we've plotted so far. |
735
|
82 static int plot_line_count = 0; |
1
|
83 |
|
84 // Is this a parametric plot? Makes a difference for 3D plotting. |
1827
|
85 static bool parametric_plot = false; |
1
|
86 |
943
|
87 // The gnuplot terminal type. |
|
88 static char *gnuplot_terminal_type = 0; |
|
89 |
478
|
90 // Should the graph window be cleared before plotting the next line? |
1827
|
91 static bool clear_before_plotting = true; |
478
|
92 |
524
|
93 // List of files to delete when we exit or crash. |
1750
|
94 static SLStack <string> tmp_files; |
524
|
95 |
|
96 // Pipe to gnuplot. |
1449
|
97 static oprocstream *plot_stream = 0; |
524
|
98 |
2211
|
99 // ID of the plotter process. |
|
100 static pid_t plot_stream_pid = 0; |
|
101 |
866
|
102 // Use shortest possible abbreviations to minimize trouble caused by |
|
103 // gnuplot's fixed-length command line buffer. |
|
104 |
2523
|
105 #ifndef GPLOT_CMD_PLOT |
|
106 #define GPLOT_CMD_PLOT "pl" |
866
|
107 #endif |
|
108 |
2523
|
109 #ifndef GPLOT_CMD_REPLOT |
|
110 #define GPLOT_CMD_REPLOT "rep" |
|
111 #endif |
|
112 |
|
113 #ifndef GPLOT_CMD_SPLOT |
|
114 #define GPLOT_CMD_SPLOT "sp" |
866
|
115 #endif |
|
116 |
2523
|
117 #ifndef GPLOT_CMD_USING |
|
118 #define GPLOT_CMD_USING "u" |
866
|
119 #endif |
|
120 |
2523
|
121 #ifndef GPLOT_CMD_WITH |
|
122 #define GPLOT_CMD_WITH "w" |
866
|
123 #endif |
|
124 |
2523
|
125 #ifndef GPLOT_CMD_TITLE |
|
126 #define GPLOT_CMD_TITLE "t" |
866
|
127 #endif |
|
128 |
2523
|
129 #ifndef GPLOT_CMD_END |
|
130 #define GPLOT_CMD_END ";\n" |
866
|
131 #endif |
|
132 |
597
|
133 static void |
2209
|
134 plot_stream_death_handler (pid_t pid, int) |
|
135 { |
|
136 close_plot_stream (); |
|
137 |
|
138 warning ("connection to external plotter (pid = %d) lost --", pid); |
|
139 warning ("please try your plot command(s) again"); |
|
140 } |
|
141 |
|
142 static void |
597
|
143 open_plot_stream (void) |
|
144 { |
1827
|
145 static bool initialized = false; |
597
|
146 |
1449
|
147 if (plot_stream && ! *plot_stream) |
|
148 { |
|
149 delete plot_stream; |
|
150 plot_stream = 0; |
|
151 } |
|
152 |
|
153 if (! plot_stream) |
597
|
154 { |
1827
|
155 initialized = false; |
943
|
156 |
597
|
157 plot_line_count = 0; |
|
158 |
2176
|
159 string plot_prog = Vgnuplot_binary; |
1755
|
160 |
2209
|
161 if (plot_prog.empty ()) |
|
162 plot_prog = "gnuplot"; |
|
163 |
|
164 // XXX FIXME XXX -- I'm not sure this is the right thing to do, |
|
165 // but without it, C-c at the octave prompt will kill gnuplot... |
|
166 |
2211
|
167 #if defined (HAVE_POSIX_SIGNALS) |
|
168 sigset_t set, oset; |
|
169 sigemptyset (&set); |
|
170 sigaddset (&set, SIGINT); |
|
171 sigprocmask (SIG_BLOCK, &set, &oset); |
|
172 #endif |
2209
|
173 |
|
174 plot_stream = new oprocstream (plot_prog.c_str ()); |
|
175 |
|
176 if (plot_stream) |
597
|
177 { |
2209
|
178 if (! *plot_stream) |
1449
|
179 { |
|
180 delete plot_stream; |
|
181 plot_stream = 0; |
2209
|
182 |
|
183 error ("plot: unable to open pipe to `%s'", plot_prog.c_str ()); |
1449
|
184 } |
2209
|
185 else |
597
|
186 { |
2211
|
187 plot_stream_pid = plot_stream->pid (); |
|
188 octave_child_list::insert (plot_stream_pid, |
|
189 plot_stream_death_handler); |
597
|
190 } |
|
191 } |
|
192 else |
2209
|
193 error ("plot: unable to open pipe to `%s'", plot_prog.c_str ()); |
2211
|
194 |
|
195 #if defined (HAVE_POSIX_SIGNALS) |
|
196 sigprocmask (SIG_SETMASK, &oset, 0); |
|
197 #endif |
597
|
198 } |
|
199 |
1449
|
200 if (! error_state && plot_stream && *plot_stream && ! initialized) |
597
|
201 { |
1827
|
202 initialized = true; |
1449
|
203 *plot_stream << "set data style lines\n"; |
943
|
204 |
|
205 if (gnuplot_terminal_type) |
2523
|
206 *plot_stream << "set term " << gnuplot_terminal_type << GPLOT_CMD_END; |
597
|
207 } |
|
208 } |
|
209 |
|
210 static int |
|
211 send_to_plot_stream (const char *cmd) |
|
212 { |
1449
|
213 if (! (plot_stream && *plot_stream)) |
597
|
214 { |
|
215 open_plot_stream (); |
|
216 |
|
217 if (error_state) |
|
218 return -1; |
|
219 } |
|
220 |
2523
|
221 int replot_len = strlen (GPLOT_CMD_REPLOT); |
|
222 int splot_len = strlen (GPLOT_CMD_SPLOT); |
|
223 int plot_len = strlen (GPLOT_CMD_PLOT); |
930
|
224 |
2523
|
225 bool is_replot = (strncmp (cmd, GPLOT_CMD_REPLOT, replot_len) == 0); |
|
226 bool is_splot = (strncmp (cmd, GPLOT_CMD_SPLOT, splot_len) == 0); |
|
227 bool is_plot = (strncmp (cmd, GPLOT_CMD_PLOT, plot_len) == 0); |
661
|
228 |
|
229 if (plot_line_count == 0 && is_replot) |
597
|
230 error ("replot: no previous plot"); |
|
231 else |
|
232 { |
1449
|
233 *plot_stream << cmd; |
930
|
234 |
661
|
235 if (! (is_replot || is_splot || is_plot) |
|
236 && plot_line_count > 0 |
2176
|
237 && Vautomatic_replot) |
2523
|
238 *plot_stream << GPLOT_CMD_REPLOT << GPLOT_CMD_END; |
930
|
239 |
1449
|
240 plot_stream->flush (); |
597
|
241 } |
|
242 |
|
243 return 0; |
|
244 } |
|
245 |
581
|
246 // Plotting, eh? |
1
|
247 |
|
248 tree_plot_command::~tree_plot_command (void) |
|
249 { |
|
250 delete range; |
|
251 delete plot_list; |
|
252 } |
|
253 |
578
|
254 void |
|
255 tree_plot_command::eval (void) |
1
|
256 { |
143
|
257 if (error_state) |
578
|
258 return; |
143
|
259 |
597
|
260 open_plot_stream (); |
|
261 |
1
|
262 ostrstream plot_buf; |
|
263 |
|
264 switch (ndim) |
|
265 { |
476
|
266 case 1: |
478
|
267 if (plot_line_count == 0) |
|
268 { |
|
269 if (plot_list) |
2523
|
270 plot_buf << GPLOT_CMD_PLOT; |
478
|
271 else |
|
272 { |
|
273 ::error ("replot: must have something to plot"); |
578
|
274 return; |
478
|
275 } |
|
276 } |
|
277 else |
2523
|
278 plot_buf << GPLOT_CMD_REPLOT; |
476
|
279 break; |
777
|
280 |
1
|
281 case 2: |
478
|
282 if (clear_before_plotting || plot_line_count == 0) |
|
283 { |
|
284 plot_line_count = 0; |
2523
|
285 plot_buf << GPLOT_CMD_PLOT; |
478
|
286 } |
|
287 else |
2523
|
288 plot_buf << GPLOT_CMD_REPLOT; |
1
|
289 break; |
777
|
290 |
1
|
291 case 3: |
930
|
292 if (clear_before_plotting || plot_line_count == 0) |
|
293 { |
|
294 plot_line_count = 0; |
2523
|
295 plot_buf << GPLOT_CMD_SPLOT; |
930
|
296 } |
|
297 else |
2523
|
298 plot_buf << GPLOT_CMD_REPLOT; |
1
|
299 break; |
777
|
300 |
1
|
301 default: |
771
|
302 gripe_2_or_3_dim_plot (); |
|
303 return; |
1
|
304 } |
|
305 |
524
|
306 if (range) |
478
|
307 { |
|
308 if (plot_line_count == 0) |
|
309 range->print (ndim, plot_buf); |
|
310 else |
|
311 warning ("can't specify new plot ranges with `replot' or while\ |
|
312 hold is on"); |
|
313 } |
1
|
314 |
191
|
315 if (error_state) |
578
|
316 return; |
191
|
317 |
591
|
318 if (plot_list) |
1
|
319 { |
591
|
320 int status = plot_list->print (ndim, plot_buf); |
578
|
321 |
771
|
322 if (error_state || status < 0) |
578
|
323 return; |
1
|
324 } |
|
325 |
2523
|
326 plot_buf << GPLOT_CMD_END << ends; |
1
|
327 |
1358
|
328 // Just testing... |
|
329 // char *message = plot_buf.str (); |
|
330 // cout << "[*]" << message << "[*]\n"; |
1
|
331 |
|
332 if (parametric_plot && ndim == 2) |
|
333 { |
|
334 warning ("can't make 2D parametric plot -- setting noparametric..."); |
|
335 send_to_plot_stream ("set noparametric\n"); |
|
336 char *message = plot_buf.str (); |
|
337 send_to_plot_stream (message); |
|
338 delete [] message; |
|
339 send_to_plot_stream ("set parametric\n"); |
|
340 } |
|
341 else |
|
342 { |
|
343 char *message = plot_buf.str (); |
|
344 send_to_plot_stream (message); |
|
345 delete [] message; |
|
346 } |
|
347 } |
|
348 |
591
|
349 void |
2124
|
350 tree_plot_command::accept (tree_walker& tw) |
1
|
351 { |
2124
|
352 tw.visit_plot_command (*this); |
1
|
353 } |
|
354 |
578
|
355 plot_limits::~plot_limits (void) |
1
|
356 { |
|
357 delete x_range; |
|
358 delete y_range; |
|
359 delete z_range; |
|
360 } |
|
361 |
|
362 void |
578
|
363 plot_limits::print (int ndim, ostrstream& plot_buf) |
1
|
364 { |
|
365 if (ndim == 2 || ndim == 3) |
|
366 { |
524
|
367 if (x_range) |
1
|
368 x_range->print (plot_buf); |
|
369 else |
|
370 return; |
|
371 |
524
|
372 if (y_range) |
1
|
373 y_range->print (plot_buf); |
|
374 else |
|
375 return; |
|
376 } |
|
377 |
524
|
378 if (ndim == 3 && z_range) |
1
|
379 z_range->print (plot_buf); |
|
380 } |
|
381 |
591
|
382 void |
2124
|
383 plot_limits::accept (tree_walker& tw) |
591
|
384 { |
2124
|
385 tw.visit_plot_limits (*this); |
591
|
386 } |
|
387 |
578
|
388 plot_range::~plot_range (void) |
1
|
389 { |
|
390 delete lower; |
|
391 delete upper; |
|
392 } |
|
393 |
|
394 void |
578
|
395 plot_range::print (ostrstream& plot_buf) |
1
|
396 { |
|
397 plot_buf << " ["; |
|
398 |
524
|
399 if (lower) |
1
|
400 { |
2086
|
401 octave_value lower_val = lower->eval (false); |
191
|
402 if (error_state) |
|
403 { |
240
|
404 ::error ("evaluating lower bound of plot range"); |
191
|
405 return; |
|
406 } |
|
407 else |
|
408 { |
629
|
409 double lo = lower_val.double_value (); |
191
|
410 plot_buf << lo; |
|
411 } |
1
|
412 } |
|
413 |
|
414 plot_buf << ":"; |
|
415 |
524
|
416 if (upper) |
1
|
417 { |
2086
|
418 octave_value upper_val = upper->eval (false); |
191
|
419 if (error_state) |
|
420 { |
240
|
421 ::error ("evaluating upper bound of plot range"); |
191
|
422 return; |
|
423 } |
|
424 else |
|
425 { |
629
|
426 double hi = upper_val.double_value (); |
191
|
427 plot_buf << hi; |
|
428 } |
1
|
429 } |
|
430 |
|
431 plot_buf << "]"; |
|
432 } |
|
433 |
591
|
434 void |
2124
|
435 plot_range::accept (tree_walker& tw) |
591
|
436 { |
2124
|
437 tw.visit_plot_range (*this); |
591
|
438 } |
|
439 |
578
|
440 subplot_using::~subplot_using (void) |
1
|
441 { |
|
442 delete scanf_fmt; |
|
443 } |
|
444 |
|
445 int |
872
|
446 subplot_using::eval (int ndim, int n_max) |
1
|
447 { |
2124
|
448 if ((ndim == 2 && qual_count > 4) |
|
449 || (ndim == 3 && qual_count > 3)) |
1
|
450 return -1; |
|
451 |
2124
|
452 if (qual_count > 0) |
|
453 val.resize (qual_count); |
872
|
454 |
2124
|
455 for (int i = 0; i < qual_count; i++) |
1
|
456 { |
524
|
457 if (x[i]) |
1
|
458 { |
2086
|
459 octave_value tmp = x[i]->eval (false); |
191
|
460 if (error_state) |
|
461 { |
240
|
462 ::error ("evaluating plot using command"); |
191
|
463 return -1; |
|
464 } |
|
465 |
872
|
466 double val_tmp; |
1
|
467 if (tmp.is_defined ()) |
|
468 { |
872
|
469 val_tmp = tmp.double_value (); |
1
|
470 |
872
|
471 if (error_state) |
|
472 return -1; |
1
|
473 |
1086
|
474 if (xisnan (val_tmp)) |
|
475 { |
|
476 ::error ("NaN is invalid as a column specifier"); |
|
477 return -1; |
|
478 } |
|
479 |
872
|
480 int n = NINT (val_tmp); |
1086
|
481 |
322
|
482 if (n < 1 || n_max > 0 && n > n_max) |
1
|
483 { |
240
|
484 ::error ("using: column %d out of range", n); |
1
|
485 return -1; |
|
486 } |
|
487 else |
2305
|
488 val (i) = n; |
1
|
489 } |
|
490 else |
|
491 return -1; |
|
492 } |
|
493 else |
|
494 return -1; |
|
495 } |
|
496 |
524
|
497 if (scanf_fmt) |
1
|
498 warning ("ignoring scanf format in plot command"); |
|
499 |
872
|
500 return 0; |
|
501 } |
|
502 |
|
503 ColumnVector |
|
504 subplot_using::values (int ndim, int n_max) |
|
505 { |
|
506 int status = eval (ndim, n_max); |
|
507 |
1203
|
508 if (status < 0) |
872
|
509 return -1; |
|
510 |
|
511 return val; |
|
512 } |
|
513 |
|
514 int |
|
515 subplot_using::print (int ndim, int n_max, ostrstream& plot_buf) |
|
516 { |
|
517 int status = eval (ndim, n_max); |
|
518 |
1203
|
519 if (status < 0) |
872
|
520 return -1; |
|
521 |
2124
|
522 for (int i = 0; i < qual_count; i++) |
872
|
523 { |
|
524 if (i == 0) |
2523
|
525 plot_buf << " " << GPLOT_CMD_USING << " "; |
872
|
526 else |
|
527 plot_buf << ":"; |
|
528 |
2305
|
529 plot_buf << val (i); |
872
|
530 } |
|
531 |
1
|
532 return 0; |
|
533 } |
|
534 |
591
|
535 void |
2124
|
536 subplot_using::accept (tree_walker& tw) |
1
|
537 { |
2124
|
538 tw.visit_subplot_using (*this); |
1
|
539 } |
|
540 |
578
|
541 subplot_style::~subplot_style (void) |
1622
|
542 { |
2124
|
543 delete sp_linetype; |
|
544 delete sp_pointtype; |
1
|
545 } |
|
546 |
|
547 int |
578
|
548 subplot_style::print (ostrstream& plot_buf) |
1
|
549 { |
2124
|
550 if (! sp_style.empty ()) |
1
|
551 { |
2523
|
552 plot_buf << " " << GPLOT_CMD_WITH << " " << sp_style; |
1
|
553 |
2124
|
554 if (sp_linetype) |
1
|
555 { |
2124
|
556 octave_value tmp = sp_linetype->eval (false); |
191
|
557 if (! error_state && tmp.is_defined ()) |
1
|
558 { |
629
|
559 double val = tmp.double_value (); |
1086
|
560 if (xisnan (val)) |
|
561 { |
|
562 ::error ("NaN is invalid a plotting line style"); |
|
563 return -1; |
|
564 } |
|
565 else |
|
566 plot_buf << " " << NINT (val); |
1
|
567 } |
|
568 else |
191
|
569 { |
240
|
570 ::error ("evaluating plot style command"); |
191
|
571 return -1; |
|
572 } |
1
|
573 } |
|
574 |
2124
|
575 if (sp_pointtype) |
1
|
576 { |
2124
|
577 octave_value tmp = sp_pointtype->eval (false); |
191
|
578 if (! error_state && tmp.is_defined ()) |
1
|
579 { |
629
|
580 double val = tmp.double_value (); |
1086
|
581 if (xisnan (val)) |
|
582 { |
|
583 ::error ("NaN is invalid a plotting point style"); |
|
584 return -1; |
|
585 } |
|
586 else |
|
587 plot_buf << " " << NINT (val); |
1
|
588 } |
|
589 else |
191
|
590 { |
240
|
591 ::error ("evaluating plot style command"); |
191
|
592 return -1; |
|
593 } |
1
|
594 } |
|
595 } |
|
596 else |
|
597 return -1; |
|
598 |
|
599 return 0; |
|
600 } |
|
601 |
872
|
602 int |
|
603 subplot_style::errorbars (void) |
|
604 { |
2542
|
605 return (almost_match ("boxerrorbars", sp_style, 5, 0) |
|
606 || almost_match ("boxxyerrorbars", sp_style, 4, 0) |
|
607 || almost_match ("errorbars", sp_style, 1, 0) |
|
608 || almost_match ("xerrorbars", sp_style, 2, 0) |
|
609 || almost_match ("xyerrorbars", sp_style, 2, 0) |
|
610 || almost_match ("yerrorbars", sp_style, 1, 0)); |
872
|
611 } |
|
612 |
591
|
613 void |
2124
|
614 subplot_style::accept (tree_walker& tw) |
591
|
615 { |
2124
|
616 tw.visit_subplot_style (*this); |
591
|
617 } |
|
618 |
878
|
619 subplot::~subplot (void) |
|
620 { |
2124
|
621 delete sp_plot_data; |
|
622 delete sp_using_clause; |
|
623 delete sp_title_clause; |
|
624 delete sp_style_clause; |
878
|
625 } |
|
626 |
2086
|
627 octave_value |
|
628 subplot::extract_plot_data (int ndim, octave_value& data) |
872
|
629 { |
2086
|
630 octave_value retval; |
872
|
631 |
2124
|
632 if (sp_using_clause) |
872
|
633 { |
2124
|
634 ColumnVector val = sp_using_clause->values (ndim); |
872
|
635 |
2086
|
636 octave_value_list args; |
872
|
637 args(1) = val; |
2086
|
638 args(0) = octave_value::magic_colon_t; |
872
|
639 |
2371
|
640 retval = data.index (args); |
872
|
641 |
|
642 if (error_state) |
2086
|
643 return octave_value (); |
872
|
644 } |
|
645 else |
|
646 { |
|
647 retval = data; |
|
648 } |
|
649 |
2124
|
650 if (ndim == 2 && sp_style_clause && sp_style_clause->errorbars ()) |
872
|
651 { |
|
652 int nc = retval.columns (); |
|
653 |
2542
|
654 if (nc < 3 || nc > 6) |
872
|
655 { |
2542
|
656 error ("plots with errorbars require 3 to 6 columns of data"); |
872
|
657 error ("but %d were provided", nc); |
2086
|
658 return octave_value (); |
872
|
659 } |
|
660 } |
|
661 |
|
662 return retval; |
|
663 } |
|
664 |
591
|
665 int |
872
|
666 subplot::handle_plot_data (int ndim, ostrstream& plot_buf) |
591
|
667 { |
2124
|
668 if (sp_plot_data) |
591
|
669 { |
2124
|
670 octave_value data = sp_plot_data->eval (false); |
872
|
671 |
591
|
672 if (! error_state && data.is_defined ()) |
|
673 { |
1750
|
674 string file; |
|
675 |
610
|
676 if (data.is_string ()) |
591
|
677 { |
1358
|
678 // Should really try to look at data file to determine |
|
679 // n_max. Can't do much about other arbitrary gnuplot |
|
680 // commands though... |
872
|
681 |
|
682 int n_max = 0; |
|
683 |
1750
|
684 file = oct_tilde_expand (data.string_value ()); |
1728
|
685 |
1750
|
686 ifstream ftmp (file.c_str ()); |
|
687 |
591
|
688 if (ftmp) |
|
689 { |
|
690 plot_buf << " \"" << file << '"'; |
|
691 } |
|
692 else |
|
693 { |
1750
|
694 file = ""; |
591
|
695 |
1358
|
696 // Opening as a file failed. Let's try passing it |
|
697 // along as a plot command. |
|
698 |
591
|
699 plot_buf << " " << data.string_value (); |
872
|
700 } |
|
701 |
2124
|
702 if (sp_using_clause) |
872
|
703 { |
2124
|
704 int status = sp_using_clause->print (ndim, n_max, plot_buf); |
1750
|
705 |
872
|
706 if (status < 0) |
|
707 return -1; |
591
|
708 } |
|
709 } |
872
|
710 else |
591
|
711 { |
1358
|
712 // Eliminate the need for printing a using clause to |
|
713 // plot_buf. |
872
|
714 |
2086
|
715 octave_value tmp_data = extract_plot_data (ndim, data); |
872
|
716 |
|
717 if (tmp_data.is_defined ()) |
|
718 { |
|
719 switch (ndim) |
|
720 { |
|
721 case 2: |
|
722 file = save_in_tmp_file (tmp_data, ndim); |
|
723 break; |
777
|
724 |
872
|
725 case 3: |
|
726 file = save_in_tmp_file (tmp_data, ndim, |
|
727 parametric_plot); |
|
728 break; |
777
|
729 |
872
|
730 default: |
|
731 gripe_2_or_3_dim_plot (); |
|
732 break; |
|
733 } |
591
|
734 |
1750
|
735 if (file.length () > 0) |
872
|
736 { |
|
737 mark_for_deletion (file); |
|
738 plot_buf << " \"" << file << '"'; |
|
739 } |
|
740 } |
591
|
741 } |
|
742 } |
|
743 else |
|
744 return -1; |
|
745 } |
|
746 else |
|
747 return -1; |
|
748 |
872
|
749 return 0; |
|
750 } |
591
|
751 |
872
|
752 int |
|
753 subplot::print (int ndim, ostrstream& plot_buf) |
|
754 { |
|
755 int status = handle_plot_data (ndim, plot_buf); |
|
756 |
|
757 if (status < 0) |
|
758 return -1; |
591
|
759 |
2124
|
760 if (sp_title_clause) |
591
|
761 { |
2124
|
762 octave_value tmp = sp_title_clause->eval (false); |
610
|
763 if (! error_state && tmp.is_string ()) |
2523
|
764 plot_buf << " " << GPLOT_CMD_TITLE << " " |
866
|
765 << '"' << tmp.string_value () << '"'; |
591
|
766 else |
|
767 { |
|
768 warning ("line title must be a string"); |
2523
|
769 plot_buf << " " << GPLOT_CMD_TITLE << " " |
866
|
770 << '"' << "line " << plot_line_count << '"'; |
591
|
771 } |
|
772 } |
|
773 else |
2523
|
774 plot_buf << " " << GPLOT_CMD_TITLE << " " |
866
|
775 << '"' << "line " << plot_line_count << '"'; |
591
|
776 |
2124
|
777 if (sp_style_clause) |
591
|
778 { |
2124
|
779 int status = sp_style_clause->print (plot_buf); |
591
|
780 if (status < 0) |
|
781 return -1; |
|
782 } |
|
783 |
|
784 return 0; |
|
785 } |
|
786 |
|
787 void |
2124
|
788 subplot::accept (tree_walker& tw) |
591
|
789 { |
2124
|
790 tw.visit_subplot (*this); |
591
|
791 } |
|
792 |
1622
|
793 subplot_list::~subplot_list (void) |
|
794 { |
|
795 while (! empty ()) |
|
796 { |
|
797 subplot *t = remove_front (); |
|
798 delete t; |
|
799 } |
|
800 } |
|
801 |
883
|
802 int |
591
|
803 subplot_list::print (int ndim, ostrstream& plot_buf) |
|
804 { |
|
805 int status = 0; |
|
806 |
|
807 for (Pix p = first (); p != 0; next (p)) |
|
808 { |
|
809 subplot *elt = this->operator () (p); |
|
810 |
|
811 plot_line_count++; |
|
812 |
|
813 if (p != first ()) |
|
814 plot_buf << ",\\\n "; |
|
815 |
|
816 status = elt->print (ndim, plot_buf); |
|
817 |
|
818 if (status < 0) |
|
819 break; |
|
820 } |
|
821 |
|
822 return status; |
|
823 } |
|
824 |
|
825 void |
2124
|
826 subplot_list::accept (tree_walker& tw) |
591
|
827 { |
2124
|
828 tw.visit_subplot_list (*this); |
591
|
829 } |
|
830 |
1755
|
831 string |
2086
|
832 save_in_tmp_file (octave_value& t, int ndim, bool parametric) |
524
|
833 { |
1802
|
834 string name = oct_tempnam (); |
1755
|
835 |
|
836 if (! name.empty ()) |
524
|
837 { |
1755
|
838 ofstream file (name.c_str ()); |
|
839 |
524
|
840 if (file) |
|
841 { |
|
842 switch (ndim) |
|
843 { |
|
844 case 2: |
1755
|
845 save_ascii_data (file, t, name, 1); |
524
|
846 break; |
777
|
847 |
524
|
848 case 3: |
607
|
849 save_three_d (file, t, parametric); |
524
|
850 break; |
777
|
851 |
524
|
852 default: |
771
|
853 gripe_2_or_3_dim_plot (); |
524
|
854 break; |
|
855 } |
|
856 } |
|
857 else |
|
858 { |
1755
|
859 error ("couldn't open temporary output file `%s'", name.c_str ()); |
|
860 name.resize (0); |
524
|
861 } |
|
862 } |
1755
|
863 |
524
|
864 return name; |
|
865 } |
|
866 |
|
867 void |
1750
|
868 mark_for_deletion (const string& file) |
524
|
869 { |
1750
|
870 tmp_files.push (file); |
524
|
871 } |
|
872 |
|
873 void |
|
874 cleanup_tmp_files (void) |
|
875 { |
|
876 while (! tmp_files.empty ()) |
|
877 { |
1750
|
878 string filename = tmp_files.pop (); |
|
879 unlink (filename.c_str ()); |
524
|
880 } |
|
881 } |
|
882 |
|
883 void |
|
884 close_plot_stream (void) |
|
885 { |
2211
|
886 octave_child_list::remove (plot_stream_pid); |
|
887 |
1449
|
888 if (plot_stream) |
|
889 { |
|
890 delete plot_stream; |
|
891 plot_stream = 0; |
|
892 } |
524
|
893 |
|
894 plot_line_count = 0; |
|
895 } |
|
896 |
1328
|
897 void |
1755
|
898 do_external_plotter_cd (const string& newdir) |
1328
|
899 { |
1449
|
900 if (plot_stream && *plot_stream) |
1328
|
901 { |
|
902 ostrstream plot_buf; |
2523
|
903 plot_buf << "cd \"" << newdir << "\"" GPLOT_CMD_END << ends; |
1328
|
904 char *message = plot_buf.str (); |
|
905 send_to_plot_stream (message); |
|
906 delete [] message; |
|
907 } |
|
908 } |
|
909 |
1957
|
910 DEFUN (clearplot, , , |
883
|
911 "clearplot (): clear the plot window") |
|
912 { |
2086
|
913 octave_value_list retval; |
883
|
914 send_to_plot_stream ("clear\n"); |
930
|
915 |
1358
|
916 // XXX FIXME XXX -- instead of just clearing these things, it would |
|
917 // be nice if we could reset things to a user-specified default |
|
918 // state. |
930
|
919 |
|
920 send_to_plot_stream ("set title\n"); |
|
921 send_to_plot_stream ("set xlabel\n"); |
|
922 send_to_plot_stream ("set ylabel\n"); |
940
|
923 send_to_plot_stream ("set nogrid\n"); |
1000
|
924 send_to_plot_stream ("set nolabel\n"); |
940
|
925 |
1358
|
926 // This makes a simple `replot' not work after a `clearplot' command |
|
927 // has been issued. |
940
|
928 |
|
929 plot_line_count = 0; |
930
|
930 |
883
|
931 return retval; |
|
932 } |
|
933 |
884
|
934 DEFALIAS (clg, clearplot); |
|
935 |
1957
|
936 DEFUN (closeplot, , , |
524
|
937 "closeplot (): close the stream to plotter") |
|
938 { |
2086
|
939 octave_value_list retval; |
524
|
940 close_plot_stream (); |
|
941 return retval; |
|
942 } |
|
943 |
1957
|
944 DEFUN_TEXT (hold, args, , |
524
|
945 "hold [on|off]\n\ |
|
946 \n\ |
|
947 determine whether the plot window is cleared before the next line is\n\ |
|
948 drawn. With no argument, toggle the current state.") |
|
949 { |
2086
|
950 octave_value_list retval; |
524
|
951 |
1755
|
952 int argc = args.length () + 1; |
|
953 |
1968
|
954 string_vector argv = args.make_argv ("hold"); |
1755
|
955 |
|
956 if (error_state) |
|
957 return retval; |
524
|
958 |
|
959 switch (argc) |
|
960 { |
|
961 case 1: |
|
962 clear_before_plotting = ! clear_before_plotting; |
|
963 break; |
777
|
964 |
524
|
965 case 2: |
1755
|
966 if (argv[1] == "on") |
1827
|
967 clear_before_plotting = false; |
1755
|
968 else if (argv[1] == "off") |
1827
|
969 clear_before_plotting = true; |
524
|
970 else |
|
971 print_usage ("hold"); |
|
972 break; |
777
|
973 |
524
|
974 default: |
|
975 print_usage ("hold"); |
|
976 break; |
|
977 } |
|
978 |
|
979 return retval; |
|
980 } |
|
981 |
1957
|
982 DEFUN (ishold, , , |
735
|
983 "ishold\n\ |
|
984 \n\ |
|
985 Return 1 if hold is on, otherwise return 0.") |
|
986 { |
|
987 return (double) (! clear_before_plotting); |
|
988 } |
|
989 |
1957
|
990 DEFUN (purge_tmp_files, , , |
524
|
991 "delete temporary data files used for plotting") |
|
992 { |
2086
|
993 octave_value_list retval; |
524
|
994 cleanup_tmp_files (); |
|
995 return retval; |
|
996 } |
|
997 |
2520
|
998 DEFUN_TEXT (gset, args, , |
|
999 "gset [options]\n\ |
524
|
1000 \n\ |
2520
|
1001 set plotting options for gnuplot") |
524
|
1002 { |
2086
|
1003 octave_value_list retval; |
524
|
1004 |
1755
|
1005 int argc = args.length () + 1; |
|
1006 |
1968
|
1007 string_vector argv = args.make_argv ("set"); |
1755
|
1008 |
|
1009 if (error_state) |
|
1010 return retval; |
524
|
1011 |
|
1012 ostrstream plot_buf; |
|
1013 |
|
1014 if (argc > 1) |
|
1015 { |
|
1016 if (almost_match ("parametric", argv[1], 3)) |
1827
|
1017 parametric_plot = true; |
524
|
1018 else if (almost_match ("noparametric", argv[1], 5)) |
1827
|
1019 parametric_plot = false; |
943
|
1020 else if (almost_match ("term", argv[1], 1)) |
|
1021 { |
|
1022 delete [] gnuplot_terminal_type; |
|
1023 ostrstream buf; |
|
1024 for (int i = 2; i < argc; i++) |
|
1025 buf << argv[i] << " "; |
2523
|
1026 buf << GPLOT_CMD_END << ends; |
943
|
1027 gnuplot_terminal_type = buf.str (); |
|
1028 } |
524
|
1029 } |
|
1030 |
|
1031 for (int i = 0; i < argc; i++) |
|
1032 plot_buf << argv[i] << " "; |
|
1033 |
2523
|
1034 plot_buf << GPLOT_CMD_END << ends; |
524
|
1035 |
|
1036 char *plot_command = plot_buf.str (); |
|
1037 send_to_plot_stream (plot_command); |
|
1038 |
|
1039 delete [] plot_command; |
|
1040 |
|
1041 return retval; |
|
1042 } |
|
1043 |
2520
|
1044 DEFUN_TEXT (set, args, nargout, |
|
1045 "This command is has been replaced by `gset'.") |
|
1046 { |
2521
|
1047 warning ("set is obsolete -- use gset instead"); |
2520
|
1048 return Fgset (args, nargout); |
|
1049 } |
|
1050 |
|
1051 DEFUN_TEXT (gshow, args, , |
|
1052 "gshow [options]\n\ |
524
|
1053 \n\ |
|
1054 show plotting options") |
|
1055 { |
2086
|
1056 octave_value_list retval; |
524
|
1057 |
1755
|
1058 int argc = args.length () + 1; |
|
1059 |
1968
|
1060 string_vector argv = args.make_argv ("show"); |
1755
|
1061 |
|
1062 if (error_state) |
|
1063 return retval; |
524
|
1064 |
|
1065 ostrstream plot_buf; |
|
1066 |
|
1067 for (int i = 0; i < argc; i++) |
|
1068 plot_buf << argv[i] << " "; |
|
1069 |
2523
|
1070 plot_buf << GPLOT_CMD_END << ends; |
524
|
1071 |
|
1072 char *plot_command = plot_buf.str (); |
|
1073 send_to_plot_stream (plot_command); |
|
1074 |
|
1075 delete [] plot_command; |
|
1076 |
|
1077 return retval; |
|
1078 } |
|
1079 |
2520
|
1080 DEFUN_TEXT (show, args, nargout, |
|
1081 "This command is has been replaced by `gshow'.") |
|
1082 { |
2521
|
1083 warning ("show is obsolete -- use gshow instead"); |
2520
|
1084 return Fgshow (args, nargout); |
|
1085 } |
|
1086 |
2176
|
1087 static int |
|
1088 automatic_replot (void) |
|
1089 { |
|
1090 Vautomatic_replot = check_preference ("automatic_replot"); |
|
1091 |
|
1092 return 0; |
|
1093 } |
|
1094 |
|
1095 int |
|
1096 gnuplot_binary (void) |
|
1097 { |
|
1098 int status = 0; |
|
1099 |
|
1100 string s = builtin_string_variable ("gnuplot_binary"); |
|
1101 |
|
1102 if (s.empty ()) |
|
1103 { |
|
1104 gripe_invalid_value_specified ("gnuplot_binary"); |
|
1105 status = -1; |
|
1106 } |
|
1107 else |
|
1108 Vgnuplot_binary = s; |
|
1109 |
|
1110 return status; |
|
1111 } |
|
1112 |
|
1113 static int |
2289
|
1114 gnuplot_has_frames (void) |
|
1115 { |
|
1116 Vgnuplot_has_frames = check_preference ("gnuplot_has_frames"); |
|
1117 |
|
1118 return 0; |
|
1119 } |
|
1120 |
|
1121 static int |
2176
|
1122 gnuplot_has_multiplot (void) |
|
1123 { |
|
1124 Vgnuplot_has_multiplot = check_preference ("gnuplot_has_multiplot"); |
|
1125 |
|
1126 return 0; |
|
1127 } |
|
1128 |
|
1129 void |
|
1130 symbols_of_pt_plot (void) |
|
1131 { |
|
1132 DEFVAR (automatic_replot, 0.0, 0, automatic_replot, |
|
1133 "if true, auto-insert a replot command when a plot changes"); |
|
1134 |
|
1135 DEFVAR (gnuplot_binary, "gnuplot", 0, gnuplot_binary, |
|
1136 "path to gnuplot binary"); |
|
1137 |
2289
|
1138 #ifdef GNUPLOT_HAS_FRAMES |
|
1139 double with_frames = 1.0; |
|
1140 #else |
|
1141 double with_frames = 0.0; |
|
1142 #endif |
|
1143 |
|
1144 DEFVAR (gnuplot_has_frames, with_frames, 0, gnuplot_has_frames, |
|
1145 "true if gnuplot supports multiple plot windows on X11, false otherwise"); |
|
1146 |
2176
|
1147 #ifdef GNUPLOT_HAS_MULTIPLOT |
|
1148 double with_multiplot = 1.0; |
|
1149 #else |
|
1150 double with_multiplot = 0.0; |
|
1151 #endif |
|
1152 |
|
1153 DEFVAR (gnuplot_has_multiplot, with_multiplot, 0, gnuplot_has_multiplot, |
|
1154 "true if gnuplot supports multiplot, false otherwise"); |
|
1155 } |
|
1156 |
1
|
1157 /* |
|
1158 ;;; Local Variables: *** |
|
1159 ;;; mode: C++ *** |
|
1160 ;;; End: *** |
|
1161 */ |