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); |
2553
|
172 #else |
|
173 volatile octave_interrupt_handler *old_interrupt_handler |
|
174 = octave_ignore_interrupts (); |
2211
|
175 #endif |
2209
|
176 |
|
177 plot_stream = new oprocstream (plot_prog.c_str ()); |
|
178 |
|
179 if (plot_stream) |
597
|
180 { |
2209
|
181 if (! *plot_stream) |
1449
|
182 { |
|
183 delete plot_stream; |
|
184 plot_stream = 0; |
2209
|
185 |
|
186 error ("plot: unable to open pipe to `%s'", plot_prog.c_str ()); |
1449
|
187 } |
2209
|
188 else |
597
|
189 { |
2211
|
190 plot_stream_pid = plot_stream->pid (); |
|
191 octave_child_list::insert (plot_stream_pid, |
|
192 plot_stream_death_handler); |
597
|
193 } |
|
194 } |
|
195 else |
2209
|
196 error ("plot: unable to open pipe to `%s'", plot_prog.c_str ()); |
2211
|
197 |
|
198 #if defined (HAVE_POSIX_SIGNALS) |
|
199 sigprocmask (SIG_SETMASK, &oset, 0); |
2553
|
200 #else |
|
201 octave_set_interrupt_handler (old_interrupt_handler); |
2211
|
202 #endif |
597
|
203 } |
|
204 |
1449
|
205 if (! error_state && plot_stream && *plot_stream && ! initialized) |
597
|
206 { |
1827
|
207 initialized = true; |
1449
|
208 *plot_stream << "set data style lines\n"; |
943
|
209 |
|
210 if (gnuplot_terminal_type) |
2523
|
211 *plot_stream << "set term " << gnuplot_terminal_type << GPLOT_CMD_END; |
597
|
212 } |
|
213 } |
|
214 |
|
215 static int |
|
216 send_to_plot_stream (const char *cmd) |
|
217 { |
1449
|
218 if (! (plot_stream && *plot_stream)) |
597
|
219 { |
|
220 open_plot_stream (); |
|
221 |
|
222 if (error_state) |
|
223 return -1; |
|
224 } |
|
225 |
2523
|
226 int replot_len = strlen (GPLOT_CMD_REPLOT); |
|
227 int splot_len = strlen (GPLOT_CMD_SPLOT); |
|
228 int plot_len = strlen (GPLOT_CMD_PLOT); |
930
|
229 |
2523
|
230 bool is_replot = (strncmp (cmd, GPLOT_CMD_REPLOT, replot_len) == 0); |
|
231 bool is_splot = (strncmp (cmd, GPLOT_CMD_SPLOT, splot_len) == 0); |
|
232 bool is_plot = (strncmp (cmd, GPLOT_CMD_PLOT, plot_len) == 0); |
661
|
233 |
|
234 if (plot_line_count == 0 && is_replot) |
597
|
235 error ("replot: no previous plot"); |
|
236 else |
|
237 { |
1449
|
238 *plot_stream << cmd; |
930
|
239 |
661
|
240 if (! (is_replot || is_splot || is_plot) |
|
241 && plot_line_count > 0 |
2176
|
242 && Vautomatic_replot) |
2523
|
243 *plot_stream << GPLOT_CMD_REPLOT << GPLOT_CMD_END; |
930
|
244 |
1449
|
245 plot_stream->flush (); |
597
|
246 } |
|
247 |
|
248 return 0; |
|
249 } |
|
250 |
581
|
251 // Plotting, eh? |
1
|
252 |
|
253 tree_plot_command::~tree_plot_command (void) |
|
254 { |
|
255 delete range; |
|
256 delete plot_list; |
|
257 } |
|
258 |
578
|
259 void |
|
260 tree_plot_command::eval (void) |
1
|
261 { |
143
|
262 if (error_state) |
578
|
263 return; |
143
|
264 |
597
|
265 open_plot_stream (); |
|
266 |
1
|
267 ostrstream plot_buf; |
|
268 |
|
269 switch (ndim) |
|
270 { |
476
|
271 case 1: |
478
|
272 if (plot_line_count == 0) |
|
273 { |
|
274 if (plot_list) |
2523
|
275 plot_buf << GPLOT_CMD_PLOT; |
478
|
276 else |
|
277 { |
|
278 ::error ("replot: must have something to plot"); |
578
|
279 return; |
478
|
280 } |
|
281 } |
|
282 else |
2523
|
283 plot_buf << GPLOT_CMD_REPLOT; |
476
|
284 break; |
777
|
285 |
1
|
286 case 2: |
478
|
287 if (clear_before_plotting || plot_line_count == 0) |
|
288 { |
|
289 plot_line_count = 0; |
2523
|
290 plot_buf << GPLOT_CMD_PLOT; |
478
|
291 } |
|
292 else |
2523
|
293 plot_buf << GPLOT_CMD_REPLOT; |
1
|
294 break; |
777
|
295 |
1
|
296 case 3: |
930
|
297 if (clear_before_plotting || plot_line_count == 0) |
|
298 { |
|
299 plot_line_count = 0; |
2523
|
300 plot_buf << GPLOT_CMD_SPLOT; |
930
|
301 } |
|
302 else |
2523
|
303 plot_buf << GPLOT_CMD_REPLOT; |
1
|
304 break; |
777
|
305 |
1
|
306 default: |
771
|
307 gripe_2_or_3_dim_plot (); |
|
308 return; |
1
|
309 } |
|
310 |
524
|
311 if (range) |
478
|
312 { |
|
313 if (plot_line_count == 0) |
|
314 range->print (ndim, plot_buf); |
|
315 else |
|
316 warning ("can't specify new plot ranges with `replot' or while\ |
|
317 hold is on"); |
|
318 } |
1
|
319 |
191
|
320 if (error_state) |
578
|
321 return; |
191
|
322 |
591
|
323 if (plot_list) |
1
|
324 { |
591
|
325 int status = plot_list->print (ndim, plot_buf); |
578
|
326 |
771
|
327 if (error_state || status < 0) |
578
|
328 return; |
1
|
329 } |
|
330 |
2523
|
331 plot_buf << GPLOT_CMD_END << ends; |
1
|
332 |
1358
|
333 // Just testing... |
|
334 // char *message = plot_buf.str (); |
|
335 // cout << "[*]" << message << "[*]\n"; |
1
|
336 |
|
337 if (parametric_plot && ndim == 2) |
|
338 { |
|
339 warning ("can't make 2D parametric plot -- setting noparametric..."); |
|
340 send_to_plot_stream ("set noparametric\n"); |
|
341 char *message = plot_buf.str (); |
|
342 send_to_plot_stream (message); |
|
343 delete [] message; |
|
344 send_to_plot_stream ("set parametric\n"); |
|
345 } |
|
346 else |
|
347 { |
|
348 char *message = plot_buf.str (); |
|
349 send_to_plot_stream (message); |
|
350 delete [] message; |
|
351 } |
|
352 } |
|
353 |
591
|
354 void |
2124
|
355 tree_plot_command::accept (tree_walker& tw) |
1
|
356 { |
2124
|
357 tw.visit_plot_command (*this); |
1
|
358 } |
|
359 |
578
|
360 plot_limits::~plot_limits (void) |
1
|
361 { |
|
362 delete x_range; |
|
363 delete y_range; |
|
364 delete z_range; |
|
365 } |
|
366 |
|
367 void |
578
|
368 plot_limits::print (int ndim, ostrstream& plot_buf) |
1
|
369 { |
|
370 if (ndim == 2 || ndim == 3) |
|
371 { |
524
|
372 if (x_range) |
1
|
373 x_range->print (plot_buf); |
|
374 else |
|
375 return; |
|
376 |
524
|
377 if (y_range) |
1
|
378 y_range->print (plot_buf); |
|
379 else |
|
380 return; |
|
381 } |
|
382 |
524
|
383 if (ndim == 3 && z_range) |
1
|
384 z_range->print (plot_buf); |
|
385 } |
|
386 |
591
|
387 void |
2124
|
388 plot_limits::accept (tree_walker& tw) |
591
|
389 { |
2124
|
390 tw.visit_plot_limits (*this); |
591
|
391 } |
|
392 |
578
|
393 plot_range::~plot_range (void) |
1
|
394 { |
|
395 delete lower; |
|
396 delete upper; |
|
397 } |
|
398 |
|
399 void |
578
|
400 plot_range::print (ostrstream& plot_buf) |
1
|
401 { |
|
402 plot_buf << " ["; |
|
403 |
524
|
404 if (lower) |
1
|
405 { |
2086
|
406 octave_value lower_val = lower->eval (false); |
191
|
407 if (error_state) |
|
408 { |
240
|
409 ::error ("evaluating lower bound of plot range"); |
191
|
410 return; |
|
411 } |
|
412 else |
|
413 { |
629
|
414 double lo = lower_val.double_value (); |
191
|
415 plot_buf << lo; |
|
416 } |
1
|
417 } |
|
418 |
|
419 plot_buf << ":"; |
|
420 |
524
|
421 if (upper) |
1
|
422 { |
2086
|
423 octave_value upper_val = upper->eval (false); |
191
|
424 if (error_state) |
|
425 { |
240
|
426 ::error ("evaluating upper bound of plot range"); |
191
|
427 return; |
|
428 } |
|
429 else |
|
430 { |
629
|
431 double hi = upper_val.double_value (); |
191
|
432 plot_buf << hi; |
|
433 } |
1
|
434 } |
|
435 |
|
436 plot_buf << "]"; |
|
437 } |
|
438 |
591
|
439 void |
2124
|
440 plot_range::accept (tree_walker& tw) |
591
|
441 { |
2124
|
442 tw.visit_plot_range (*this); |
591
|
443 } |
|
444 |
578
|
445 subplot_using::~subplot_using (void) |
1
|
446 { |
|
447 delete scanf_fmt; |
|
448 } |
|
449 |
|
450 int |
872
|
451 subplot_using::eval (int ndim, int n_max) |
1
|
452 { |
2124
|
453 if ((ndim == 2 && qual_count > 4) |
|
454 || (ndim == 3 && qual_count > 3)) |
1
|
455 return -1; |
|
456 |
2124
|
457 if (qual_count > 0) |
|
458 val.resize (qual_count); |
872
|
459 |
2124
|
460 for (int i = 0; i < qual_count; i++) |
1
|
461 { |
524
|
462 if (x[i]) |
1
|
463 { |
2086
|
464 octave_value tmp = x[i]->eval (false); |
191
|
465 if (error_state) |
|
466 { |
240
|
467 ::error ("evaluating plot using command"); |
191
|
468 return -1; |
|
469 } |
|
470 |
872
|
471 double val_tmp; |
1
|
472 if (tmp.is_defined ()) |
|
473 { |
872
|
474 val_tmp = tmp.double_value (); |
1
|
475 |
872
|
476 if (error_state) |
|
477 return -1; |
1
|
478 |
1086
|
479 if (xisnan (val_tmp)) |
|
480 { |
|
481 ::error ("NaN is invalid as a column specifier"); |
|
482 return -1; |
|
483 } |
|
484 |
872
|
485 int n = NINT (val_tmp); |
1086
|
486 |
322
|
487 if (n < 1 || n_max > 0 && n > n_max) |
1
|
488 { |
240
|
489 ::error ("using: column %d out of range", n); |
1
|
490 return -1; |
|
491 } |
|
492 else |
2305
|
493 val (i) = n; |
1
|
494 } |
|
495 else |
|
496 return -1; |
|
497 } |
|
498 else |
|
499 return -1; |
|
500 } |
|
501 |
524
|
502 if (scanf_fmt) |
1
|
503 warning ("ignoring scanf format in plot command"); |
|
504 |
872
|
505 return 0; |
|
506 } |
|
507 |
|
508 ColumnVector |
|
509 subplot_using::values (int ndim, int n_max) |
|
510 { |
|
511 int status = eval (ndim, n_max); |
|
512 |
1203
|
513 if (status < 0) |
872
|
514 return -1; |
|
515 |
|
516 return val; |
|
517 } |
|
518 |
|
519 int |
|
520 subplot_using::print (int ndim, int n_max, ostrstream& plot_buf) |
|
521 { |
|
522 int status = eval (ndim, n_max); |
|
523 |
1203
|
524 if (status < 0) |
872
|
525 return -1; |
|
526 |
2124
|
527 for (int i = 0; i < qual_count; i++) |
872
|
528 { |
|
529 if (i == 0) |
2523
|
530 plot_buf << " " << GPLOT_CMD_USING << " "; |
872
|
531 else |
|
532 plot_buf << ":"; |
|
533 |
2305
|
534 plot_buf << val (i); |
872
|
535 } |
|
536 |
1
|
537 return 0; |
|
538 } |
|
539 |
591
|
540 void |
2124
|
541 subplot_using::accept (tree_walker& tw) |
1
|
542 { |
2124
|
543 tw.visit_subplot_using (*this); |
1
|
544 } |
|
545 |
578
|
546 subplot_style::~subplot_style (void) |
1622
|
547 { |
2124
|
548 delete sp_linetype; |
|
549 delete sp_pointtype; |
1
|
550 } |
|
551 |
|
552 int |
578
|
553 subplot_style::print (ostrstream& plot_buf) |
1
|
554 { |
2124
|
555 if (! sp_style.empty ()) |
1
|
556 { |
2523
|
557 plot_buf << " " << GPLOT_CMD_WITH << " " << sp_style; |
1
|
558 |
2124
|
559 if (sp_linetype) |
1
|
560 { |
2124
|
561 octave_value tmp = sp_linetype->eval (false); |
191
|
562 if (! error_state && tmp.is_defined ()) |
1
|
563 { |
629
|
564 double val = tmp.double_value (); |
1086
|
565 if (xisnan (val)) |
|
566 { |
|
567 ::error ("NaN is invalid a plotting line style"); |
|
568 return -1; |
|
569 } |
|
570 else |
|
571 plot_buf << " " << NINT (val); |
1
|
572 } |
|
573 else |
191
|
574 { |
240
|
575 ::error ("evaluating plot style command"); |
191
|
576 return -1; |
|
577 } |
1
|
578 } |
|
579 |
2124
|
580 if (sp_pointtype) |
1
|
581 { |
2124
|
582 octave_value tmp = sp_pointtype->eval (false); |
191
|
583 if (! error_state && tmp.is_defined ()) |
1
|
584 { |
629
|
585 double val = tmp.double_value (); |
1086
|
586 if (xisnan (val)) |
|
587 { |
|
588 ::error ("NaN is invalid a plotting point style"); |
|
589 return -1; |
|
590 } |
|
591 else |
|
592 plot_buf << " " << NINT (val); |
1
|
593 } |
|
594 else |
191
|
595 { |
240
|
596 ::error ("evaluating plot style command"); |
191
|
597 return -1; |
|
598 } |
1
|
599 } |
|
600 } |
|
601 else |
|
602 return -1; |
|
603 |
|
604 return 0; |
|
605 } |
|
606 |
2543
|
607 bool |
|
608 subplot_style::columns_ok (int nc) |
872
|
609 { |
2543
|
610 bool retval = true; |
|
611 |
|
612 if ((almost_match ("boxes", sp_style, 5, 0) && nc != 3) |
|
613 || (almost_match ("boxerrorbars", sp_style, 5, 0) |
|
614 && (! (nc == 3 || nc == 4 || nc == 5))) |
|
615 || ((almost_match ("boxxyerrorbars", sp_style, 4, 0) |
|
616 || almost_match ("xyerrorbars", sp_style, 2, 0)) |
|
617 && (! (nc == 4 || nc == 6 || nc == 7))) |
|
618 || ((almost_match ("candlesticks", sp_style, 1, 0) |
|
619 || almost_match ("financebars", sp_style, 2, 0)) |
|
620 && nc != 5) |
|
621 || ((almost_match ("errorbars", sp_style, 1, 0) |
|
622 || almost_match ("xerrorbars", sp_style, 1, 0) |
|
623 || almost_match ("yerrorbars", sp_style, 1, 0)) |
|
624 && (! (nc == 3 || nc == 4)))) |
|
625 { |
|
626 error |
|
627 ("invalid number of data columns = %d specified for plot style `%s'", |
|
628 nc, sp_style.c_str ()); |
|
629 |
|
630 retval = false; |
|
631 } |
|
632 |
|
633 return retval; |
872
|
634 } |
|
635 |
591
|
636 void |
2124
|
637 subplot_style::accept (tree_walker& tw) |
591
|
638 { |
2124
|
639 tw.visit_subplot_style (*this); |
591
|
640 } |
|
641 |
878
|
642 subplot::~subplot (void) |
|
643 { |
2124
|
644 delete sp_plot_data; |
|
645 delete sp_using_clause; |
|
646 delete sp_title_clause; |
|
647 delete sp_style_clause; |
878
|
648 } |
|
649 |
2086
|
650 octave_value |
|
651 subplot::extract_plot_data (int ndim, octave_value& data) |
872
|
652 { |
2086
|
653 octave_value retval; |
872
|
654 |
2124
|
655 if (sp_using_clause) |
872
|
656 { |
2124
|
657 ColumnVector val = sp_using_clause->values (ndim); |
872
|
658 |
2086
|
659 octave_value_list args; |
872
|
660 args(1) = val; |
2086
|
661 args(0) = octave_value::magic_colon_t; |
872
|
662 |
2371
|
663 retval = data.index (args); |
872
|
664 |
|
665 if (error_state) |
2086
|
666 return octave_value (); |
872
|
667 } |
|
668 else |
|
669 { |
|
670 retval = data; |
|
671 } |
|
672 |
2543
|
673 int nc = retval.columns (); |
872
|
674 |
2543
|
675 if (ndim == 2 && sp_style_clause && ! sp_style_clause->columns_ok (nc)) |
|
676 return octave_value (); |
872
|
677 |
|
678 return retval; |
|
679 } |
|
680 |
591
|
681 int |
872
|
682 subplot::handle_plot_data (int ndim, ostrstream& plot_buf) |
591
|
683 { |
2124
|
684 if (sp_plot_data) |
591
|
685 { |
2124
|
686 octave_value data = sp_plot_data->eval (false); |
872
|
687 |
591
|
688 if (! error_state && data.is_defined ()) |
|
689 { |
1750
|
690 string file; |
|
691 |
610
|
692 if (data.is_string ()) |
591
|
693 { |
1358
|
694 // Should really try to look at data file to determine |
|
695 // n_max. Can't do much about other arbitrary gnuplot |
|
696 // commands though... |
872
|
697 |
|
698 int n_max = 0; |
|
699 |
1750
|
700 file = oct_tilde_expand (data.string_value ()); |
1728
|
701 |
1750
|
702 ifstream ftmp (file.c_str ()); |
|
703 |
591
|
704 if (ftmp) |
|
705 { |
|
706 plot_buf << " \"" << file << '"'; |
|
707 } |
|
708 else |
|
709 { |
1750
|
710 file = ""; |
591
|
711 |
1358
|
712 // Opening as a file failed. Let's try passing it |
|
713 // along as a plot command. |
|
714 |
591
|
715 plot_buf << " " << data.string_value (); |
872
|
716 } |
|
717 |
2124
|
718 if (sp_using_clause) |
872
|
719 { |
2124
|
720 int status = sp_using_clause->print (ndim, n_max, plot_buf); |
1750
|
721 |
872
|
722 if (status < 0) |
|
723 return -1; |
591
|
724 } |
|
725 } |
872
|
726 else |
591
|
727 { |
1358
|
728 // Eliminate the need for printing a using clause to |
|
729 // plot_buf. |
872
|
730 |
2086
|
731 octave_value tmp_data = extract_plot_data (ndim, data); |
872
|
732 |
|
733 if (tmp_data.is_defined ()) |
|
734 { |
|
735 switch (ndim) |
|
736 { |
|
737 case 2: |
|
738 file = save_in_tmp_file (tmp_data, ndim); |
|
739 break; |
777
|
740 |
872
|
741 case 3: |
|
742 file = save_in_tmp_file (tmp_data, ndim, |
|
743 parametric_plot); |
|
744 break; |
777
|
745 |
872
|
746 default: |
|
747 gripe_2_or_3_dim_plot (); |
|
748 break; |
|
749 } |
591
|
750 |
1750
|
751 if (file.length () > 0) |
872
|
752 { |
|
753 mark_for_deletion (file); |
|
754 plot_buf << " \"" << file << '"'; |
|
755 } |
|
756 } |
591
|
757 } |
|
758 } |
|
759 else |
|
760 return -1; |
|
761 } |
|
762 else |
|
763 return -1; |
|
764 |
872
|
765 return 0; |
|
766 } |
591
|
767 |
872
|
768 int |
|
769 subplot::print (int ndim, ostrstream& plot_buf) |
|
770 { |
|
771 int status = handle_plot_data (ndim, plot_buf); |
|
772 |
|
773 if (status < 0) |
|
774 return -1; |
591
|
775 |
2124
|
776 if (sp_title_clause) |
591
|
777 { |
2124
|
778 octave_value tmp = sp_title_clause->eval (false); |
610
|
779 if (! error_state && tmp.is_string ()) |
2523
|
780 plot_buf << " " << GPLOT_CMD_TITLE << " " |
866
|
781 << '"' << tmp.string_value () << '"'; |
591
|
782 else |
|
783 { |
|
784 warning ("line title must be a string"); |
2523
|
785 plot_buf << " " << GPLOT_CMD_TITLE << " " |
866
|
786 << '"' << "line " << plot_line_count << '"'; |
591
|
787 } |
|
788 } |
|
789 else |
2523
|
790 plot_buf << " " << GPLOT_CMD_TITLE << " " |
866
|
791 << '"' << "line " << plot_line_count << '"'; |
591
|
792 |
2124
|
793 if (sp_style_clause) |
591
|
794 { |
2124
|
795 int status = sp_style_clause->print (plot_buf); |
591
|
796 if (status < 0) |
|
797 return -1; |
|
798 } |
|
799 |
|
800 return 0; |
|
801 } |
|
802 |
|
803 void |
2124
|
804 subplot::accept (tree_walker& tw) |
591
|
805 { |
2124
|
806 tw.visit_subplot (*this); |
591
|
807 } |
|
808 |
1622
|
809 subplot_list::~subplot_list (void) |
|
810 { |
|
811 while (! empty ()) |
|
812 { |
|
813 subplot *t = remove_front (); |
|
814 delete t; |
|
815 } |
|
816 } |
|
817 |
883
|
818 int |
591
|
819 subplot_list::print (int ndim, ostrstream& plot_buf) |
|
820 { |
|
821 int status = 0; |
|
822 |
|
823 for (Pix p = first (); p != 0; next (p)) |
|
824 { |
|
825 subplot *elt = this->operator () (p); |
|
826 |
|
827 plot_line_count++; |
|
828 |
|
829 if (p != first ()) |
|
830 plot_buf << ",\\\n "; |
|
831 |
|
832 status = elt->print (ndim, plot_buf); |
|
833 |
|
834 if (status < 0) |
|
835 break; |
|
836 } |
|
837 |
|
838 return status; |
|
839 } |
|
840 |
|
841 void |
2124
|
842 subplot_list::accept (tree_walker& tw) |
591
|
843 { |
2124
|
844 tw.visit_subplot_list (*this); |
591
|
845 } |
|
846 |
1755
|
847 string |
2086
|
848 save_in_tmp_file (octave_value& t, int ndim, bool parametric) |
524
|
849 { |
1802
|
850 string name = oct_tempnam (); |
1755
|
851 |
|
852 if (! name.empty ()) |
524
|
853 { |
1755
|
854 ofstream file (name.c_str ()); |
|
855 |
524
|
856 if (file) |
|
857 { |
|
858 switch (ndim) |
|
859 { |
|
860 case 2: |
1755
|
861 save_ascii_data (file, t, name, 1); |
524
|
862 break; |
777
|
863 |
524
|
864 case 3: |
607
|
865 save_three_d (file, t, parametric); |
524
|
866 break; |
777
|
867 |
524
|
868 default: |
771
|
869 gripe_2_or_3_dim_plot (); |
524
|
870 break; |
|
871 } |
|
872 } |
|
873 else |
|
874 { |
1755
|
875 error ("couldn't open temporary output file `%s'", name.c_str ()); |
|
876 name.resize (0); |
524
|
877 } |
|
878 } |
1755
|
879 |
524
|
880 return name; |
|
881 } |
|
882 |
|
883 void |
1750
|
884 mark_for_deletion (const string& file) |
524
|
885 { |
1750
|
886 tmp_files.push (file); |
524
|
887 } |
|
888 |
|
889 void |
|
890 cleanup_tmp_files (void) |
|
891 { |
|
892 while (! tmp_files.empty ()) |
|
893 { |
1750
|
894 string filename = tmp_files.pop (); |
|
895 unlink (filename.c_str ()); |
524
|
896 } |
|
897 } |
|
898 |
|
899 void |
|
900 close_plot_stream (void) |
|
901 { |
2211
|
902 octave_child_list::remove (plot_stream_pid); |
|
903 |
1449
|
904 if (plot_stream) |
|
905 { |
|
906 delete plot_stream; |
|
907 plot_stream = 0; |
|
908 } |
524
|
909 |
|
910 plot_line_count = 0; |
|
911 } |
|
912 |
1328
|
913 void |
1755
|
914 do_external_plotter_cd (const string& newdir) |
1328
|
915 { |
1449
|
916 if (plot_stream && *plot_stream) |
1328
|
917 { |
|
918 ostrstream plot_buf; |
2523
|
919 plot_buf << "cd \"" << newdir << "\"" GPLOT_CMD_END << ends; |
1328
|
920 char *message = plot_buf.str (); |
|
921 send_to_plot_stream (message); |
|
922 delete [] message; |
|
923 } |
|
924 } |
|
925 |
1957
|
926 DEFUN (clearplot, , , |
883
|
927 "clearplot (): clear the plot window") |
|
928 { |
2086
|
929 octave_value_list retval; |
883
|
930 send_to_plot_stream ("clear\n"); |
930
|
931 |
1358
|
932 // XXX FIXME XXX -- instead of just clearing these things, it would |
|
933 // be nice if we could reset things to a user-specified default |
|
934 // state. |
930
|
935 |
|
936 send_to_plot_stream ("set title\n"); |
|
937 send_to_plot_stream ("set xlabel\n"); |
|
938 send_to_plot_stream ("set ylabel\n"); |
940
|
939 send_to_plot_stream ("set nogrid\n"); |
1000
|
940 send_to_plot_stream ("set nolabel\n"); |
940
|
941 |
1358
|
942 // This makes a simple `replot' not work after a `clearplot' command |
|
943 // has been issued. |
940
|
944 |
|
945 plot_line_count = 0; |
930
|
946 |
883
|
947 return retval; |
|
948 } |
|
949 |
884
|
950 DEFALIAS (clg, clearplot); |
|
951 |
1957
|
952 DEFUN (closeplot, , , |
524
|
953 "closeplot (): close the stream to plotter") |
|
954 { |
2086
|
955 octave_value_list retval; |
524
|
956 close_plot_stream (); |
|
957 return retval; |
|
958 } |
|
959 |
1957
|
960 DEFUN_TEXT (hold, args, , |
524
|
961 "hold [on|off]\n\ |
|
962 \n\ |
|
963 determine whether the plot window is cleared before the next line is\n\ |
|
964 drawn. With no argument, toggle the current state.") |
|
965 { |
2086
|
966 octave_value_list retval; |
524
|
967 |
1755
|
968 int argc = args.length () + 1; |
|
969 |
1968
|
970 string_vector argv = args.make_argv ("hold"); |
1755
|
971 |
|
972 if (error_state) |
|
973 return retval; |
524
|
974 |
|
975 switch (argc) |
|
976 { |
|
977 case 1: |
|
978 clear_before_plotting = ! clear_before_plotting; |
|
979 break; |
777
|
980 |
524
|
981 case 2: |
1755
|
982 if (argv[1] == "on") |
1827
|
983 clear_before_plotting = false; |
1755
|
984 else if (argv[1] == "off") |
1827
|
985 clear_before_plotting = true; |
524
|
986 else |
|
987 print_usage ("hold"); |
|
988 break; |
777
|
989 |
524
|
990 default: |
|
991 print_usage ("hold"); |
|
992 break; |
|
993 } |
|
994 |
|
995 return retval; |
|
996 } |
|
997 |
1957
|
998 DEFUN (ishold, , , |
735
|
999 "ishold\n\ |
|
1000 \n\ |
|
1001 Return 1 if hold is on, otherwise return 0.") |
|
1002 { |
|
1003 return (double) (! clear_before_plotting); |
|
1004 } |
|
1005 |
1957
|
1006 DEFUN (purge_tmp_files, , , |
524
|
1007 "delete temporary data files used for plotting") |
|
1008 { |
2086
|
1009 octave_value_list retval; |
524
|
1010 cleanup_tmp_files (); |
|
1011 return retval; |
|
1012 } |
|
1013 |
2520
|
1014 DEFUN_TEXT (gset, args, , |
|
1015 "gset [options]\n\ |
524
|
1016 \n\ |
2520
|
1017 set plotting options for gnuplot") |
524
|
1018 { |
2086
|
1019 octave_value_list retval; |
524
|
1020 |
1755
|
1021 int argc = args.length () + 1; |
|
1022 |
1968
|
1023 string_vector argv = args.make_argv ("set"); |
1755
|
1024 |
|
1025 if (error_state) |
|
1026 return retval; |
524
|
1027 |
|
1028 ostrstream plot_buf; |
|
1029 |
|
1030 if (argc > 1) |
|
1031 { |
|
1032 if (almost_match ("parametric", argv[1], 3)) |
1827
|
1033 parametric_plot = true; |
524
|
1034 else if (almost_match ("noparametric", argv[1], 5)) |
1827
|
1035 parametric_plot = false; |
943
|
1036 else if (almost_match ("term", argv[1], 1)) |
|
1037 { |
|
1038 delete [] gnuplot_terminal_type; |
|
1039 ostrstream buf; |
|
1040 for (int i = 2; i < argc; i++) |
|
1041 buf << argv[i] << " "; |
2523
|
1042 buf << GPLOT_CMD_END << ends; |
943
|
1043 gnuplot_terminal_type = buf.str (); |
|
1044 } |
524
|
1045 } |
|
1046 |
|
1047 for (int i = 0; i < argc; i++) |
|
1048 plot_buf << argv[i] << " "; |
|
1049 |
2523
|
1050 plot_buf << GPLOT_CMD_END << ends; |
524
|
1051 |
|
1052 char *plot_command = plot_buf.str (); |
|
1053 send_to_plot_stream (plot_command); |
|
1054 |
|
1055 delete [] plot_command; |
|
1056 |
|
1057 return retval; |
|
1058 } |
|
1059 |
2520
|
1060 DEFUN_TEXT (set, args, nargout, |
|
1061 "This command is has been replaced by `gset'.") |
|
1062 { |
2521
|
1063 warning ("set is obsolete -- use gset instead"); |
2520
|
1064 return Fgset (args, nargout); |
|
1065 } |
|
1066 |
|
1067 DEFUN_TEXT (gshow, args, , |
|
1068 "gshow [options]\n\ |
524
|
1069 \n\ |
|
1070 show plotting options") |
|
1071 { |
2086
|
1072 octave_value_list retval; |
524
|
1073 |
1755
|
1074 int argc = args.length () + 1; |
|
1075 |
1968
|
1076 string_vector argv = args.make_argv ("show"); |
1755
|
1077 |
|
1078 if (error_state) |
|
1079 return retval; |
524
|
1080 |
|
1081 ostrstream plot_buf; |
|
1082 |
|
1083 for (int i = 0; i < argc; i++) |
|
1084 plot_buf << argv[i] << " "; |
|
1085 |
2523
|
1086 plot_buf << GPLOT_CMD_END << ends; |
524
|
1087 |
|
1088 char *plot_command = plot_buf.str (); |
|
1089 send_to_plot_stream (plot_command); |
|
1090 |
|
1091 delete [] plot_command; |
|
1092 |
|
1093 return retval; |
|
1094 } |
|
1095 |
2520
|
1096 DEFUN_TEXT (show, args, nargout, |
|
1097 "This command is has been replaced by `gshow'.") |
|
1098 { |
2521
|
1099 warning ("show is obsolete -- use gshow instead"); |
2520
|
1100 return Fgshow (args, nargout); |
|
1101 } |
|
1102 |
2176
|
1103 static int |
|
1104 automatic_replot (void) |
|
1105 { |
|
1106 Vautomatic_replot = check_preference ("automatic_replot"); |
|
1107 |
|
1108 return 0; |
|
1109 } |
|
1110 |
|
1111 int |
|
1112 gnuplot_binary (void) |
|
1113 { |
|
1114 int status = 0; |
|
1115 |
|
1116 string s = builtin_string_variable ("gnuplot_binary"); |
|
1117 |
|
1118 if (s.empty ()) |
|
1119 { |
|
1120 gripe_invalid_value_specified ("gnuplot_binary"); |
|
1121 status = -1; |
|
1122 } |
|
1123 else |
|
1124 Vgnuplot_binary = s; |
|
1125 |
|
1126 return status; |
|
1127 } |
|
1128 |
|
1129 static int |
2289
|
1130 gnuplot_has_frames (void) |
|
1131 { |
|
1132 Vgnuplot_has_frames = check_preference ("gnuplot_has_frames"); |
|
1133 |
|
1134 return 0; |
|
1135 } |
|
1136 |
|
1137 static int |
2176
|
1138 gnuplot_has_multiplot (void) |
|
1139 { |
|
1140 Vgnuplot_has_multiplot = check_preference ("gnuplot_has_multiplot"); |
|
1141 |
|
1142 return 0; |
|
1143 } |
|
1144 |
|
1145 void |
|
1146 symbols_of_pt_plot (void) |
|
1147 { |
|
1148 DEFVAR (automatic_replot, 0.0, 0, automatic_replot, |
|
1149 "if true, auto-insert a replot command when a plot changes"); |
|
1150 |
|
1151 DEFVAR (gnuplot_binary, "gnuplot", 0, gnuplot_binary, |
|
1152 "path to gnuplot binary"); |
|
1153 |
2289
|
1154 #ifdef GNUPLOT_HAS_FRAMES |
|
1155 double with_frames = 1.0; |
|
1156 #else |
|
1157 double with_frames = 0.0; |
|
1158 #endif |
|
1159 |
|
1160 DEFVAR (gnuplot_has_frames, with_frames, 0, gnuplot_has_frames, |
|
1161 "true if gnuplot supports multiple plot windows on X11, false otherwise"); |
|
1162 |
2176
|
1163 #ifdef GNUPLOT_HAS_MULTIPLOT |
|
1164 double with_multiplot = 1.0; |
|
1165 #else |
|
1166 double with_multiplot = 0.0; |
|
1167 #endif |
|
1168 |
|
1169 DEFVAR (gnuplot_has_multiplot, with_multiplot, 0, gnuplot_has_multiplot, |
|
1170 "true if gnuplot supports multiplot, false otherwise"); |
|
1171 } |
|
1172 |
1
|
1173 /* |
|
1174 ;;; Local Variables: *** |
|
1175 ;;; mode: C++ *** |
|
1176 ;;; End: *** |
|
1177 */ |