5790
|
1 /* |
|
2 |
|
3 Copyright (C) 2004 John W. Eaton and Teemu Ikonen |
|
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 Octavee; see the file COPYING. If not, write to the Free |
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 %option prefix="gpt" |
|
25 %option noyywrap |
|
26 |
|
27 %{ |
|
28 // PKG_ADD: mark_as_rawcommand ("__gnuplot_plot__"); |
|
29 // PKG_ADD: mark_as_rawcommand ("__gnuplot_set__"); |
|
30 // PKG_ADD: mark_as_rawcommand ("__gnuplot_splot__"); |
|
31 // PKG_ADD: mark_as_rawcommand ("__gnuplot_replot__"); |
|
32 |
|
33 // PKG_ADD: mark_as_command ("__gnuplot_show__"); |
|
34 |
|
35 // PKG_ADD: atexit ("closeplot"); |
|
36 |
|
37 #ifdef HAVE_CONFIG_H |
|
38 #include <config.h> |
|
39 #endif |
|
40 |
|
41 #include <map> |
|
42 #include <string> |
|
43 #include <fstream> |
|
44 #include <iostream> |
|
45 #include <sstream> |
|
46 |
|
47 #ifdef HAVE_UNISTD_H |
|
48 #ifdef HAVE_SYS_TYPES_H |
|
49 #include <sys/types.h> |
|
50 #endif |
|
51 #include <unistd.h> |
|
52 #endif |
|
53 |
|
54 #include "file-ops.h" |
|
55 #include "oct-env.h" |
|
56 |
|
57 #include "defun-dld.h" |
|
58 #include "file-io.h" |
|
59 #include "gripes.h" |
|
60 #include "load-save.h" |
|
61 #include "parse.h" |
|
62 #include "procstream.h" |
|
63 #include "sighandlers.h" |
|
64 #include "utils.h" |
|
65 #include "variables.h" |
|
66 |
|
67 enum _toktype |
|
68 { |
|
69 START_PAREN = 1, |
|
70 END_PAREN, |
|
71 START_BRACE, |
|
72 END_BRACE, |
|
73 START_BRACKET, |
|
74 END_BRACKET, |
|
75 COLON, |
|
76 SEMICOLON, |
|
77 COMMA, |
|
78 QUOTE, |
|
79 IDENT, |
|
80 NUMBER, |
|
81 BINOP, |
|
82 UNOP, |
|
83 STRING, |
|
84 OTHER, |
|
85 TITLE, |
|
86 USING, |
|
87 WITH, |
|
88 AXES, |
|
89 CLEAR |
|
90 }; |
|
91 |
|
92 typedef bool (*pred) (const int); |
|
93 |
|
94 class |
|
95 gpt_parse_error |
|
96 { |
|
97 public: |
|
98 gpt_parse_error (void) : msg () { } |
|
99 gpt_parse_error (std::string errmsg) : msg (errmsg) { } |
|
100 |
|
101 std::string msg; |
|
102 }; |
|
103 |
|
104 static bool gpt_quote_is_transpose; |
|
105 static bool gpt_allow_plotkw; |
|
106 static int gpt_parens; |
|
107 static int gpt_brackets; |
|
108 static int gpt_braces; |
|
109 |
|
110 static bool can_be_plotkw (void); |
|
111 |
|
112 static int is_plot_keyword (const std::string& s); |
|
113 |
|
114 static int handle_string (char delim); |
|
115 static std::string strbuf; |
|
116 |
|
117 %} |
|
118 |
|
119 D [0-9] |
|
120 S [ \t] |
|
121 IDENT ([_a-zA-Z@][_a-zA-Z0-9]*) |
|
122 EXPON ([DdEe][+-]?{D}+) |
|
123 NUMBER (({D}+\.?{D}*{EXPON}?)|(\.{D}+{EXPON}?)|(0[xX][0-9a-fA-F]+)) |
|
124 NOT ((\~)|(\!)) |
|
125 /* NOT is not strictly a binary operator, but is close enough for us. */ |
|
126 BINOP (({NOT})|(\.?([\*/\\^+-]|\*\*)=?)|([<=~!>&|]=)|([=&|<>]{1,2})|(<<=)|(>>=)|(\.)) |
|
127 /* single quote (') transpose operator is handled separately. */ |
|
128 UNOP ((\+\+)|(\-\-)|(\.')) |
|
129 |
|
130 %% |
|
131 |
|
132 "(" { |
|
133 gpt_quote_is_transpose = false; |
|
134 gpt_parens++; |
|
135 return START_PAREN; |
|
136 } |
|
137 |
|
138 ")" { |
|
139 gpt_quote_is_transpose = true; |
|
140 gpt_parens--; |
|
141 return END_PAREN; |
|
142 } |
|
143 |
|
144 "{" { |
|
145 gpt_quote_is_transpose = false; |
|
146 gpt_braces++; |
|
147 return START_BRACE; |
|
148 } |
|
149 |
|
150 "}" { |
|
151 gpt_quote_is_transpose = true; |
|
152 gpt_braces--; |
|
153 return END_BRACE; |
|
154 } |
|
155 |
|
156 "[" { |
|
157 gpt_quote_is_transpose = false; |
|
158 gpt_brackets++; |
|
159 return START_BRACKET; |
|
160 } |
|
161 |
|
162 "]" { |
|
163 gpt_quote_is_transpose = true; |
|
164 gpt_brackets--; |
|
165 return END_BRACKET; |
|
166 } |
|
167 |
|
168 ":" { |
|
169 gpt_quote_is_transpose = false; |
|
170 return COLON; |
|
171 } |
|
172 |
|
173 ";" { |
|
174 gpt_quote_is_transpose = false; |
|
175 return SEMICOLON; |
|
176 } |
|
177 |
|
178 "," { |
|
179 gpt_quote_is_transpose = false; |
|
180 return COMMA; |
|
181 } |
|
182 |
|
183 "'" { |
|
184 if (gpt_quote_is_transpose) |
|
185 { |
|
186 gpt_allow_plotkw = true; |
|
187 return QUOTE; |
|
188 } |
|
189 else |
|
190 { |
|
191 gpt_quote_is_transpose = true; |
|
192 gpt_allow_plotkw = true; |
|
193 return handle_string ('\''); |
|
194 } |
|
195 } |
|
196 |
|
197 "\"" { |
|
198 return handle_string ('"'); |
|
199 } |
|
200 |
|
201 {IDENT} { |
|
202 int itok = 0; |
|
203 if (can_be_plotkw () && (itok = is_plot_keyword (yytext))) |
|
204 { |
|
205 gpt_quote_is_transpose = false; |
|
206 gpt_allow_plotkw = true; |
|
207 return itok; |
|
208 } |
|
209 else if (std::string (yytext) == "function") |
|
210 throw gpt_parse_error ("function keyword not allowed in plot commands"); |
|
211 else |
|
212 { |
|
213 gpt_quote_is_transpose = true; |
|
214 gpt_allow_plotkw = true; |
|
215 return IDENT; |
|
216 } |
|
217 } |
|
218 |
|
219 {D}+/\.[\*/\\^'] | /* ' */ |
|
220 {NUMBER} { |
|
221 gpt_quote_is_transpose = true; |
|
222 gpt_allow_plotkw = true; |
|
223 return NUMBER; |
|
224 } |
|
225 |
|
226 {BINOP} { |
|
227 gpt_quote_is_transpose = false; |
|
228 gpt_allow_plotkw = false; |
|
229 return BINOP; |
|
230 } |
|
231 |
|
232 {UNOP} { |
|
233 gpt_quote_is_transpose = false; |
|
234 gpt_allow_plotkw = true; |
|
235 return UNOP; |
|
236 } |
|
237 |
|
238 {S} { /* Ignore spaces and tabs outside of character strings. */ } |
|
239 |
|
240 . { |
|
241 gpt_quote_is_transpose = false; |
|
242 gpt_allow_plotkw = true; |
|
243 warning ("unknown token = \"%s\" in plot command", yytext); |
|
244 return OTHER; |
|
245 } |
|
246 |
|
247 %% |
|
248 |
|
249 // ------------------------------------------------------------ |
|
250 // Interface to external gnuplot process(es), including gnuplot |
|
251 // command parser. |
|
252 // ------------------------------------------------------------ |
|
253 |
|
254 // The name of the shell command to execute to start gnuplot. |
|
255 static std::string Vgnuplot_binary = GNUPLOT_BINARY; |
|
256 |
|
257 // Append -title "Figure NN" to the gnuplot command? |
|
258 static bool Vgnuplot_use_title_option = octave_env::have_x11_display (); |
|
259 |
|
260 // Gnuplot command strings that we use. |
|
261 static std::string Vgnuplot_command_plot = "pl"; |
|
262 static std::string Vgnuplot_command_replot = "rep"; |
|
263 static std::string Vgnuplot_command_splot = "sp"; |
|
264 static std::string Vgnuplot_command_using = "u"; |
|
265 static std::string Vgnuplot_command_with = "w"; |
|
266 static std::string Vgnuplot_command_axes = "ax"; |
|
267 static std::string Vgnuplot_command_title = "t"; |
|
268 static std::string Vgnuplot_command_end = "\n"; |
|
269 |
|
270 // If TRUE, a replot command is issued automatically each time a plot |
|
271 // changes in some way. |
|
272 static bool Vautomatic_replot = true; |
|
273 |
|
274 // Check if the parser state is such that a plot keyword can occur. |
|
275 |
|
276 static bool |
|
277 can_be_plotkw (void) |
|
278 { |
|
279 return (gpt_allow_plotkw |
|
280 && (gpt_braces == 0) |
|
281 && (gpt_brackets == 0) |
|
282 && (gpt_parens == 0)); |
|
283 } |
|
284 |
|
285 // Check to see if a character string matches any one of the plot |
|
286 // option keywords. Don't match abbreviations for clear, since that's |
|
287 // not a gnuplot keyword (users will probably only expect to be able |
|
288 // to abbreviate actual gnuplot keywords). |
|
289 |
|
290 static int |
|
291 is_plot_keyword (const std::string& s) |
|
292 { |
|
293 if (almost_match ("title", s, 1)) |
|
294 return TITLE; |
|
295 else if (almost_match ("using", s, 1)) |
|
296 return USING; |
|
297 else if (almost_match ("with", s, 1)) |
|
298 return WITH; |
|
299 else if (almost_match ("axes", s, 2) || almost_match ("axis", s, 2)) |
|
300 return AXES; |
|
301 else if ("clear" == s) |
|
302 return CLEAR; |
|
303 else |
|
304 return 0; |
|
305 } |
|
306 |
|
307 // This is used to handle character strings. Kludge alert. |
|
308 |
|
309 static int |
|
310 handle_string (char delim) |
|
311 { |
|
312 int c; |
|
313 bool escape_pending = false; |
|
314 |
|
315 strbuf = std::string (1, delim); |
|
316 |
|
317 while ((c = yyinput ()) != EOF) |
|
318 { |
|
319 if (c == '\\') |
|
320 { |
|
321 if (escape_pending) |
|
322 { |
|
323 strbuf += static_cast<char> (c); |
|
324 escape_pending = false; |
|
325 } |
|
326 else |
|
327 { |
|
328 strbuf += static_cast<char> (c); |
|
329 escape_pending = true; |
|
330 } |
|
331 continue; |
|
332 } |
|
333 else if (c == '\n') |
|
334 { |
|
335 error ("unterminated string constant"); |
|
336 break; |
|
337 } |
|
338 else if (c == delim) |
|
339 { |
|
340 if (escape_pending) |
|
341 strbuf += static_cast<char> (c); |
|
342 else |
|
343 { |
|
344 c = yyinput (); |
|
345 |
|
346 if (c == delim) |
|
347 { |
|
348 strbuf += static_cast<char> (c); |
|
349 strbuf += static_cast<char> (c); |
|
350 } |
|
351 else |
|
352 { |
|
353 yyunput (c, yytext); |
|
354 strbuf += static_cast<char> (delim); |
|
355 return STRING; |
|
356 } |
|
357 } |
|
358 } |
|
359 else |
|
360 strbuf += static_cast<char> (c); |
|
361 |
|
362 escape_pending = false; |
|
363 } |
|
364 |
|
365 throw gpt_parse_error ("unterminated string"); |
|
366 |
|
367 return 0; |
|
368 } |
|
369 |
|
370 // (Probably not necessesary, but current Matlab style plot functions |
|
371 // break without this (they emit too short gnuplot commands)) |
|
372 |
|
373 static std::string |
|
374 plot_style_token (const std::string& s) |
|
375 { |
|
376 std::string retval; |
|
377 |
|
378 // FIXME -- specify minimum match length for these. |
|
379 static const char *plot_styles[] = |
|
380 { |
|
381 "boxes", |
|
382 "boxerrorbars", |
|
383 "boxxyerrorbars", |
|
384 "candlesticks", |
|
385 "dots", |
|
386 "errorbars", |
|
387 "financebars", |
|
388 "fsteps", |
|
389 "histeps", |
|
390 "impulses", |
|
391 "lines", |
|
392 "linespoints", |
|
393 "points", |
|
394 "steps", |
|
395 "vector", |
|
396 "xerrorbars", |
|
397 "xyerrorbars", |
|
398 "yerrorbars", |
|
399 0, |
|
400 }; |
|
401 |
|
402 const char * const *tmp = plot_styles; |
|
403 while (*tmp) |
|
404 { |
|
405 if (almost_match (*tmp, s.c_str ())) |
|
406 { |
|
407 retval = *tmp; |
|
408 break; |
|
409 } |
|
410 |
|
411 tmp++; |
|
412 } |
|
413 |
|
414 return retval; |
|
415 } |
|
416 |
|
417 // Some predicates on tokens |
|
418 |
|
419 // Return true for ":". |
|
420 |
|
421 static bool |
|
422 colonp (const int tok) |
|
423 { |
|
424 return (tok == COLON); |
|
425 } |
|
426 |
|
427 // Return TRUE for "]". |
|
428 |
|
429 static bool |
|
430 endbracketp (const int tok) |
|
431 { |
|
432 return (tok == END_BRACKET); |
|
433 } |
|
434 |
|
435 // Return TRUE for plot token, comma or end of input. |
|
436 |
|
437 static bool |
|
438 plottok_or_end_p (const int tok) |
|
439 { |
|
440 return (tok == TITLE |
|
441 || tok == USING |
|
442 || tok == WITH |
|
443 || tok == AXES |
|
444 || tok == CLEAR |
|
445 || tok == COMMA |
|
446 || tok == 0); |
|
447 } |
|
448 |
|
449 // Equivalent to (colonp (tok) || plottok_or_end_p (tok)). |
|
450 |
|
451 static bool |
|
452 colon_plottok_or_end_p (const int tok) |
|
453 { |
|
454 return (tok == COLON || plottok_or_end_p (tok)); |
|
455 } |
|
456 |
|
457 // read until test is true and delimiters are balanced, or end of input. |
|
458 // Return the last token in lasttok |
|
459 |
|
460 static std::string |
|
461 read_until (pred test, int& lasttok) throw (gpt_parse_error) |
|
462 { |
|
463 int tok; |
|
464 |
|
465 // We have to maintain balanced delimiters per subexpression too. |
|
466 int brackets = 0; |
|
467 int parens = 0; |
|
468 int braces = 0; |
|
469 std::string s; |
|
470 |
|
471 tok = gptlex (); |
|
472 |
|
473 while (tok && ! (test (tok) |
|
474 && brackets == 0 |
|
475 && parens == 0 |
|
476 && braces == 0)) |
|
477 { |
|
478 switch (tok) |
|
479 { |
|
480 case START_BRACKET: |
|
481 brackets++; |
|
482 break; |
|
483 |
|
484 case END_BRACKET: |
|
485 brackets--; |
|
486 break; |
|
487 |
|
488 case START_PAREN: |
|
489 parens++; |
|
490 break; |
|
491 |
|
492 case END_PAREN: |
|
493 parens--; |
|
494 break; |
|
495 |
|
496 case START_BRACE: |
|
497 braces++; |
|
498 break; |
|
499 |
|
500 case END_BRACE: |
|
501 braces--; |
|
502 break; |
|
503 |
|
504 default: |
|
505 break; |
|
506 } |
|
507 |
|
508 s += (tok == STRING ? strbuf : std::string (yytext)) + " "; |
|
509 |
|
510 tok = gptlex (); |
|
511 } |
|
512 |
|
513 // Throw error only if we've reached the end token and the test |
|
514 // doesn't accept it. |
|
515 |
|
516 if (! test (tok) && ! tok) |
|
517 throw gpt_parse_error ("unexpected end of input"); |
|
518 |
|
519 lasttok = tok; |
|
520 |
|
521 return s; |
|
522 } |
|
523 |
|
524 // Eval the two expressions giving limits of range and print it. |
|
525 |
|
526 static std::string |
|
527 printrange (std::string starts, std::string ends) |
|
528 { |
|
529 octave_value startv, endv; |
|
530 int status; |
|
531 std::string s; |
|
532 std::ostringstream range_buf; |
|
533 |
|
534 range_buf << "["; |
|
535 |
|
536 if (! starts.empty ()) |
|
537 { |
|
538 startv = eval_string (starts, true, status); |
|
539 if (! startv.is_real_scalar ()) |
|
540 throw gpt_parse_error (); |
|
541 startv.print_raw (range_buf); |
|
542 } |
|
543 |
|
544 range_buf << ":"; |
|
545 |
|
546 if (! ends.empty ()) |
|
547 { |
|
548 endv = eval_string (ends, true, status); |
|
549 if (! endv.is_real_scalar ()) |
|
550 throw gpt_parse_error (); |
|
551 endv.print_raw (range_buf); |
|
552 } |
|
553 |
|
554 range_buf << "]"; |
|
555 |
|
556 s = range_buf.str (); |
|
557 |
|
558 return s; |
|
559 } |
|
560 |
|
561 // Handle plot parameters. |
|
562 |
|
563 // Parse, evaluate and print colon separated expressions in the using |
|
564 // plot parameter. The use of trailing format string is not supported. |
|
565 |
|
566 static std::string |
|
567 handle_using (int& lasttok) |
|
568 { |
|
569 int tok; |
|
570 std::string expr_str; |
|
571 std::string retstr = Vgnuplot_command_using + " "; |
|
572 bool out = false; |
|
573 |
|
574 octave_value tmp_data; |
|
575 int status; |
|
576 while (! out) |
|
577 { |
|
578 expr_str = read_until (colon_plottok_or_end_p, tok); |
|
579 |
|
580 tmp_data = eval_string (expr_str, true, status); |
|
581 if (status != 0 || ! tmp_data.is_real_scalar ()) |
|
582 throw gpt_parse_error (); |
|
583 |
|
584 std::ostringstream tmp_buf; |
|
585 tmp_data.print_raw (tmp_buf); |
|
586 retstr += tmp_buf.str (); |
|
587 |
|
588 if (tok == COLON) |
|
589 retstr += ":"; |
|
590 else |
|
591 out = true; |
|
592 } |
|
593 |
|
594 lasttok = tok; |
|
595 |
|
596 return retstr; |
|
597 } |
|
598 |
|
599 // Presently just passes the linewidth, pointtype etc. tokens as they are. |
|
600 |
|
601 static std::string |
|
602 handle_style (int& lasttok) |
|
603 { |
|
604 std::string retstr = Vgnuplot_command_with + " "; |
|
605 std::string style; |
|
606 |
|
607 lasttok = gptlex (); |
|
608 |
|
609 if (lasttok != IDENT) |
|
610 throw gpt_parse_error ("expected plot style token"); |
|
611 |
|
612 style = std::string (yytext); |
|
613 style = plot_style_token (style); |
|
614 |
|
615 if (! style.empty ()) |
|
616 retstr += style; |
|
617 else |
|
618 retstr += std::string (yytext); |
|
619 |
|
620 // FIXME -- should evaluate the remaining tokens, but this |
|
621 // needs changes in the parser. |
|
622 retstr += " " + read_until (plottok_or_end_p, lasttok); |
|
623 |
|
624 return retstr; |
|
625 } |
|
626 |
|
627 // Axes has only one qualifier keyword, which is not evaluated. |
|
628 |
|
629 static std::string |
|
630 handle_axes (int& lasttok) |
|
631 { |
|
632 return Vgnuplot_command_axes + " " + read_until (plottok_or_end_p, lasttok); |
|
633 } |
|
634 |
|
635 static std::string |
|
636 save_in_tmp_file (const octave_value& t, int ndim = 2, bool parametric = false) |
|
637 { |
|
638 std::string name = file_ops::tempnam ("", "oct-"); |
|
639 |
|
640 if (! name.empty ()) |
|
641 { |
|
642 std::ofstream file (name.c_str ()); |
|
643 |
|
644 if (file) |
|
645 { |
|
646 switch (ndim) |
|
647 { |
|
648 case 2: |
|
649 save_ascii_data_for_plotting (file, t, name); |
|
650 break; |
|
651 |
|
652 case 3: |
|
653 save_three_d (file, t, parametric); |
|
654 break; |
|
655 |
|
656 default: |
|
657 gripe_2_or_3_dim_plot (); |
|
658 break; |
|
659 } |
|
660 } |
|
661 else |
|
662 { |
|
663 error ("couldn't open temporary output file `%s'", name.c_str ()); |
|
664 name.resize (0); |
|
665 } |
|
666 } |
|
667 |
|
668 return name; |
|
669 } |
|
670 |
|
671 static int |
|
672 get_current_figure (void) |
|
673 { |
|
674 int retval = 1; |
|
675 |
|
676 octave_value cf = get_global_value ("__current_figure__", true); |
|
677 |
|
678 if (cf.is_defined ()) |
|
679 retval = cf.int_value (); |
|
680 else |
|
681 set_global_value ("__current_figure__", retval); |
|
682 |
|
683 return retval; |
|
684 } |
|
685 |
|
686 class |
|
687 gnuplot |
|
688 { |
|
689 protected: |
|
690 |
|
691 gnuplot (void) |
|
692 : plot_line_count (0), parametric_plot (false), |
|
693 use_title_option (Vgnuplot_use_title_option), |
|
694 gnuplot_exe (Vgnuplot_binary), |
|
695 gnuplot_terminal_type (), plot_stream () { } |
|
696 |
|
697 public: |
|
698 |
|
699 ~gnuplot (void) { } |
|
700 |
|
701 static bool have_instance (void); |
|
702 |
|
703 static bool ensure_instance (void); |
|
704 |
|
705 static bool ensure_plot_stream (void); |
|
706 |
|
707 static void open (void); |
|
708 |
|
709 static void close (void); |
|
710 |
|
711 static void close_all (void); |
|
712 |
|
713 static bool plot_stream_event_handler (pid_t pid, int status); |
|
714 |
|
715 static void send (const std::string& cmd) |
|
716 { |
|
717 if (ensure_plot_stream ()) |
|
718 instance->do_send (cmd); |
|
719 } |
|
720 |
|
721 static void send_raw (const std::string& cmd) |
|
722 { |
|
723 if (ensure_plot_stream ()) |
|
724 instance->do_send_raw (cmd); |
|
725 } |
|
726 |
|
727 static void clear (void) |
|
728 { |
|
729 if (ensure_plot_stream ()) |
|
730 instance->do_clear (); |
|
731 } |
|
732 |
|
733 static void set (const string_vector& argv) |
|
734 { |
|
735 if (ensure_plot_stream ()) |
|
736 instance->do_set (argv); |
|
737 } |
|
738 |
|
739 static void show (const string_vector& argv) |
|
740 { |
|
741 if (ensure_plot_stream ()) |
|
742 instance->do_show (argv); |
|
743 } |
|
744 |
|
745 static void set_gnuplot_exe (const std::string& exe) |
|
746 { |
|
747 if (ensure_instance ()) |
|
748 instance->do_set_gnuplot_exe (exe); |
|
749 } |
|
750 |
|
751 static void set_gnuplot_use_title_option (bool opt) |
|
752 { |
|
753 if (ensure_instance ()) |
|
754 instance->do_set_gnuplot_use_title_option (opt); |
|
755 } |
|
756 |
|
757 // FIXME -- should only remove tmp files associated with |
|
758 // gnuplot? |
|
759 static void cleanup_tmp_files (void) { ::cleanup_tmp_files (); } |
|
760 |
|
761 static void plot (const string_vector& argv) |
|
762 { |
|
763 if (ensure_plot_stream ()) |
|
764 instance->do_plot (argv); |
|
765 } |
|
766 |
|
767 private: |
|
768 |
|
769 static gnuplot *instance; |
|
770 |
|
771 static std::map<int, gnuplot *> instance_map; |
|
772 |
|
773 // The number of lines we've plotted so far. |
|
774 int plot_line_count; |
|
775 |
|
776 // Is this a parametric plot? Makes a difference for 3D plotting. |
|
777 bool parametric_plot; |
|
778 |
|
779 // Should we append '-title "TITLE"' to the gnuplot command? |
|
780 bool use_title_option; |
|
781 |
|
782 // The executable program to run. |
|
783 std::string gnuplot_exe; |
|
784 |
|
785 // The gnuplot terminal type. |
|
786 std::string gnuplot_terminal_type; |
|
787 |
|
788 // Pipe to gnuplot. |
|
789 oprocstream *plot_stream; |
|
790 |
|
791 pid_t pid (void) const; |
|
792 |
|
793 static gnuplot *lookup_by_pid (pid_t pid); |
|
794 |
|
795 void do_open (void); |
|
796 |
|
797 void do_close (void); |
|
798 |
|
799 void delete_plot_stream (void); |
|
800 |
|
801 void reset_plot_stream (void); |
|
802 |
|
803 void do_send (const std::string& cmd); |
|
804 |
|
805 void do_send_raw (const std::string& cmd); |
|
806 |
|
807 void do_clear (void); |
|
808 |
|
809 void do_set (const string_vector& argv); |
|
810 |
|
811 void do_show (const string_vector& argv); |
|
812 |
|
813 void do_set_gnuplot_exe (const std::string& exe) { gnuplot_exe = exe; } |
|
814 |
|
815 void do_set_gnuplot_use_title_option (bool opt) { use_title_option = opt; } |
|
816 |
|
817 void do_plot (const string_vector& argv); |
|
818 |
|
819 std::string |
|
820 makeplot (std::string caller, std::string args) throw (gpt_parse_error); |
|
821 |
|
822 std::string handle_title (int& lasttok); |
|
823 }; |
|
824 |
|
825 gnuplot *gnuplot::instance = 0; |
|
826 |
|
827 std::map<int, gnuplot *> gnuplot::instance_map; |
|
828 |
|
829 bool |
|
830 gnuplot::have_instance (void) |
|
831 { |
|
832 int current_figure = get_current_figure (); |
|
833 |
|
834 if (instance_map.find (current_figure) != instance_map.end ()) |
|
835 { |
|
836 instance = instance_map[current_figure]; |
|
837 return true; |
|
838 } |
|
839 else |
|
840 return false; |
|
841 } |
|
842 |
|
843 bool |
|
844 gnuplot::ensure_instance (void) |
|
845 { |
|
846 if (! have_instance ()) |
|
847 { |
|
848 instance = new gnuplot (); |
|
849 |
|
850 if (! instance) |
|
851 { |
|
852 ::error ("unable to create gnuplot object!"); |
|
853 |
|
854 return false; |
|
855 } |
|
856 else |
|
857 instance_map[get_current_figure ()] = instance; |
|
858 } |
|
859 |
|
860 return true; |
|
861 } |
|
862 |
|
863 bool |
|
864 gnuplot::ensure_plot_stream (void) |
|
865 { |
|
866 if (ensure_instance ()) |
|
867 { |
|
868 instance->do_open (); |
|
869 |
|
870 if (error_state) |
|
871 return false; |
|
872 } |
|
873 |
|
874 return true; |
|
875 } |
|
876 |
|
877 void |
|
878 gnuplot::close (void) |
|
879 { |
|
880 if (have_instance ()) |
|
881 { |
|
882 instance->do_close (); |
|
883 |
|
884 instance_map.erase (get_current_figure ()); |
|
885 } |
|
886 } |
|
887 |
|
888 void |
|
889 gnuplot::close_all (void) |
|
890 { |
|
891 for (std::map<int, gnuplot *>::const_iterator p = instance_map.begin (); |
|
892 p != instance_map.end (); |
|
893 p++) |
|
894 { |
|
895 gnuplot *elt = p->second; |
|
896 |
|
897 elt->do_close (); |
|
898 } |
|
899 } |
|
900 |
|
901 pid_t |
|
902 gnuplot::pid (void) const |
|
903 { |
|
904 return plot_stream ? plot_stream->pid () : -1; |
|
905 } |
|
906 |
|
907 gnuplot * |
|
908 gnuplot::lookup_by_pid (pid_t pid) |
|
909 { |
|
910 gnuplot *retval = 0; |
|
911 |
|
912 for (std::map<int, gnuplot *>::const_iterator p = instance_map.begin (); |
|
913 p != instance_map.end (); |
|
914 p++) |
|
915 { |
|
916 gnuplot *elt = p->second; |
|
917 |
|
918 if (elt && elt->pid () == pid) |
|
919 { |
|
920 retval = elt; |
|
921 break; |
|
922 } |
|
923 } |
|
924 |
|
925 return retval; |
|
926 } |
|
927 |
|
928 void |
|
929 gnuplot::do_open (void) |
|
930 { |
|
931 static bool initialized = false; |
|
932 |
|
933 if (plot_stream && ! *plot_stream) |
|
934 do_close (); |
|
935 |
|
936 if (! plot_stream) |
|
937 { |
|
938 initialized = false; |
|
939 |
|
940 plot_line_count = 0; |
|
941 |
|
942 std::string cmd; |
|
943 |
|
944 if (gnuplot_exe.empty ()) |
|
945 cmd = "gnuplot"; |
|
946 else |
|
947 cmd = "\"" + gnuplot_exe + "\""; |
|
948 |
|
949 // FIXME -- I'm not sure this is the right thing to do, |
|
950 // but without it, C-c at the octave prompt will kill gnuplot... |
|
951 |
|
952 #if defined (HAVE_POSIX_SIGNALS) |
|
953 sigset_t nset, oset; |
|
954 sigemptyset (&nset); |
|
955 sigaddset (&nset, SIGINT); |
|
956 sigprocmask (SIG_BLOCK, &nset, &oset); |
|
957 #else |
|
958 volatile octave_interrupt_handler old_interrupt_handler |
|
959 = octave_ignore_interrupts (); |
|
960 #endif |
|
961 |
|
962 if (use_title_option) |
|
963 { |
|
964 std::ostringstream buf; |
|
965 |
|
966 buf << cmd << " -title \"Figure " << get_current_figure () << "\""; |
|
967 |
|
968 cmd = buf.str (); |
|
969 } |
|
970 |
|
971 plot_stream = new oprocstream (cmd.c_str ()); |
|
972 |
|
973 if (plot_stream && *plot_stream) |
|
974 octave_child_list::insert (plot_stream->pid (), |
|
975 plot_stream_event_handler); |
|
976 else |
|
977 { |
|
978 delete_plot_stream (); |
|
979 |
|
980 error ("plot: unable to open pipe to `%s'", cmd.c_str ()); |
|
981 } |
|
982 |
|
983 #if defined (HAVE_POSIX_SIGNALS) |
|
984 sigprocmask (SIG_SETMASK, &oset, 0); |
|
985 #else |
|
986 octave_set_interrupt_handler (old_interrupt_handler); |
|
987 #endif |
|
988 } |
|
989 |
|
990 if (! error_state && plot_stream && *plot_stream && ! initialized) |
|
991 { |
|
992 initialized = true; |
|
993 |
|
994 do_send_raw ("set style data lines\n"); |
|
995 |
|
996 if (! gnuplot_terminal_type.empty ()) |
|
997 do_send_raw ("set term " + gnuplot_terminal_type |
|
998 + Vgnuplot_command_end + "\n"); |
|
999 } |
|
1000 } |
|
1001 |
|
1002 void |
|
1003 gnuplot::delete_plot_stream (void) |
|
1004 { |
|
1005 do_send_raw ("\nquit\n"); |
|
1006 |
|
1007 delete plot_stream; |
|
1008 plot_stream = 0; |
|
1009 } |
|
1010 |
|
1011 void |
|
1012 gnuplot::reset_plot_stream (void) |
|
1013 { |
|
1014 delete_plot_stream (); |
|
1015 |
|
1016 plot_line_count = 0; |
|
1017 parametric_plot = false; |
|
1018 } |
|
1019 |
|
1020 void |
|
1021 gnuplot::do_close (void) |
|
1022 { |
|
1023 if (plot_stream) |
|
1024 { |
|
1025 octave_child_list::remove (plot_stream->pid ()); |
|
1026 |
|
1027 delete_plot_stream (); |
|
1028 } |
|
1029 |
|
1030 plot_line_count = 0; |
|
1031 } |
|
1032 |
|
1033 bool |
|
1034 gnuplot::plot_stream_event_handler (pid_t pid, int status) |
|
1035 { |
|
1036 bool retval = false; |
|
1037 |
|
1038 if (pid > 0) |
|
1039 { |
|
1040 if (WIFEXITED (status) || WIFSIGNALLED (status)) |
|
1041 { |
|
1042 gnuplot *plotter = gnuplot::lookup_by_pid (pid); |
|
1043 |
|
1044 // We should only print a warning or request removal of the |
|
1045 // process from the child list if the process died |
|
1046 // unexpectedly. If do_close is responsible for |
|
1047 // gnuplot's death, then plotter will be 0 here and we don't |
|
1048 // need to do anything. |
|
1049 |
|
1050 if (plotter) |
|
1051 { |
|
1052 plotter->reset_plot_stream (); |
|
1053 |
|
1054 warning ("connection to external plotter (pid = %d) lost --", pid); |
|
1055 // Request removal of this PID from the list of child |
|
1056 // processes. |
|
1057 |
|
1058 retval = true; |
|
1059 } |
|
1060 } |
|
1061 } |
|
1062 |
|
1063 return retval; |
|
1064 } |
|
1065 |
|
1066 void |
|
1067 gnuplot::do_send (const std::string& cmd) |
|
1068 { |
|
1069 int replot_len = Vgnuplot_command_replot.length (); |
|
1070 |
|
1071 bool is_replot = (Vgnuplot_command_replot == cmd.substr (0, replot_len)); |
|
1072 |
|
1073 if (! (plot_line_count == 0 && is_replot)) |
|
1074 do_send_raw (cmd); |
|
1075 } |
|
1076 |
|
1077 void |
|
1078 gnuplot::do_send_raw (const std::string& cmd) |
|
1079 { |
|
1080 if (plot_stream && *plot_stream) |
|
1081 { |
|
1082 *plot_stream << cmd; |
|
1083 |
|
1084 plot_stream->flush (); |
|
1085 } |
|
1086 } |
|
1087 |
|
1088 void |
|
1089 gnuplot::do_clear (void) |
|
1090 { |
|
1091 do_send_raw ("clear\n"); |
|
1092 |
|
1093 // FIXME -- instead of just clearing these things, it would |
|
1094 // be nice if we could reset things to a user-specified default |
|
1095 // state. |
|
1096 |
|
1097 do_send_raw ("set title\n"); |
|
1098 do_send_raw ("set xlabel\n"); |
|
1099 do_send_raw ("set ylabel\n"); |
|
1100 do_send_raw ("set nogrid\n"); |
|
1101 do_send_raw ("set nolabel\n"); |
|
1102 |
|
1103 // This makes a simple `replot' not work after a `clearplot' command |
|
1104 // has been issued. |
|
1105 |
|
1106 plot_line_count = 0; |
|
1107 } |
|
1108 |
|
1109 void |
|
1110 gnuplot::do_set (const string_vector& argv) |
|
1111 { |
|
1112 int argc = argv.length (); |
|
1113 |
|
1114 std::ostringstream plot_buf; |
|
1115 |
|
1116 if (argc > 1) |
|
1117 { |
|
1118 if (almost_match ("parametric", argv[1], 3)) |
|
1119 parametric_plot = true; |
|
1120 else if (almost_match ("noparametric", argv[1], 5)) |
|
1121 parametric_plot = false; |
|
1122 else if (almost_match ("term", argv[1], 1)) |
|
1123 { |
|
1124 gnuplot_terminal_type = ""; |
|
1125 std::ostringstream buf; |
|
1126 int i; |
|
1127 for (i = 2; i < argc-1; i++) |
|
1128 buf << argv[i] << " "; |
|
1129 if (i < argc) |
|
1130 buf << argv[i]; |
|
1131 buf << Vgnuplot_command_end; |
|
1132 gnuplot_terminal_type = buf.str (); |
|
1133 } |
|
1134 } |
|
1135 |
|
1136 int i; |
|
1137 for (i = 0; i < argc-1; i++) |
|
1138 plot_buf << argv[i] << " "; |
|
1139 |
|
1140 if (i < argc) |
|
1141 plot_buf << argv[i]; |
|
1142 |
|
1143 plot_buf << Vgnuplot_command_end; |
|
1144 |
|
1145 do_send_raw (plot_buf.str ()); |
|
1146 } |
|
1147 |
|
1148 void |
|
1149 gnuplot::do_show (const string_vector& argv) |
|
1150 { |
|
1151 int argc = argv.length (); |
|
1152 |
|
1153 std::ostringstream plot_buf; |
|
1154 |
|
1155 int i; |
|
1156 for (i = 0; i < argc-1; i++) |
|
1157 plot_buf << argv[i] << " "; |
|
1158 if (i < argc) |
|
1159 plot_buf << argv[i]; |
|
1160 |
|
1161 plot_buf << Vgnuplot_command_end; |
|
1162 |
|
1163 do_send (plot_buf.str ()); |
|
1164 } |
|
1165 |
|
1166 void |
|
1167 gnuplot::do_plot (const string_vector& argv) |
|
1168 { |
|
1169 std::string s; |
|
1170 |
|
1171 for (int i = 1; i < argv.length (); i++) |
|
1172 s += argv[i] + " "; |
|
1173 |
|
1174 try |
|
1175 { |
|
1176 std::string cmd = makeplot (argv[0], s); |
|
1177 |
|
1178 do_send (cmd); |
|
1179 } |
|
1180 catch (gpt_parse_error& e) |
|
1181 { |
|
1182 if (e.msg.empty ()) |
|
1183 error ("could not parse plot command"); |
|
1184 else |
|
1185 error (e.msg.c_str ()); |
|
1186 } |
|
1187 } |
|
1188 |
|
1189 // Parse and evaluate parameter string and pass it to gnuplot pipe. |
|
1190 |
|
1191 std::string |
|
1192 gnuplot::makeplot (std::string caller, std::string args) |
|
1193 throw (gpt_parse_error) |
|
1194 { |
|
1195 std::string retval; |
|
1196 |
|
1197 YY_BUFFER_STATE bstate; |
|
1198 |
|
1199 bstate = yy_scan_string (args.c_str ()); |
|
1200 yy_switch_to_buffer (bstate); |
|
1201 std::string outstr; |
|
1202 int ndim = 2; |
|
1203 |
|
1204 if (caller == "replot") |
|
1205 { |
|
1206 ndim = 1; |
|
1207 outstr += Vgnuplot_command_replot + " "; |
|
1208 } |
|
1209 else if (caller == "plot") |
|
1210 { |
|
1211 ndim = 2; |
|
1212 plot_line_count = 0; |
|
1213 outstr += Vgnuplot_command_plot + " "; |
|
1214 } |
|
1215 else if (caller == "splot") |
|
1216 { |
|
1217 ndim = 3; |
|
1218 plot_line_count = 0; |
|
1219 outstr += Vgnuplot_command_splot + " "; |
|
1220 } |
|
1221 else |
|
1222 throw gpt_parse_error ("unknown plot command"); |
|
1223 |
|
1224 gpt_quote_is_transpose = false; |
|
1225 gpt_allow_plotkw = false; |
|
1226 gpt_parens = 0; |
|
1227 gpt_braces = 0; |
|
1228 gpt_brackets = 0; |
|
1229 |
|
1230 int tok; |
|
1231 tok = gptlex (); |
|
1232 if (plottok_or_end_p (tok) && caller != "replot") |
|
1233 throw gpt_parse_error ("must have something to plot"); |
|
1234 |
|
1235 while (tok) |
|
1236 { |
|
1237 bool title_set = false; |
|
1238 bool using_set = false; |
|
1239 bool style_set = false; |
|
1240 bool axes_set = false; |
|
1241 |
|
1242 if (tok == START_BRACKET) |
|
1243 { |
|
1244 if (caller == "replot") |
|
1245 throw gpt_parse_error ("can't specify new plot ranges with `replot' or while hold is on"); |
|
1246 |
|
1247 std::string xrange_start_str = read_until (colonp, tok); |
|
1248 std::string xrange_end_str = read_until (endbracketp, tok); |
|
1249 outstr += printrange (xrange_start_str, xrange_end_str) + " "; |
|
1250 tok = gptlex (); |
|
1251 if (tok == START_BRACKET) |
|
1252 { |
|
1253 std::string yrange_start_str = read_until (colonp, tok); |
|
1254 std::string yrange_end_str = read_until (endbracketp, tok); |
|
1255 outstr += printrange (yrange_start_str, yrange_end_str) + " "; |
|
1256 tok = gptlex (); |
|
1257 if (tok == START_BRACKET && caller == "gsplot") |
|
1258 { |
|
1259 std::string zrange_start_str = read_until (colonp, tok); |
|
1260 std::string zrange_end_str = read_until (endbracketp, tok); |
|
1261 outstr += printrange (zrange_start_str, zrange_end_str) + " "; |
|
1262 tok = gptlex (); |
|
1263 } |
|
1264 } |
|
1265 } |
|
1266 |
|
1267 if (plottok_or_end_p (tok)) |
|
1268 return std::string (); |
|
1269 else |
|
1270 { |
|
1271 std::string file; |
|
1272 plot_line_count++; |
|
1273 |
|
1274 std::string plot_expr_str; |
|
1275 plot_expr_str += std::string (yytext) + " "; |
|
1276 plot_expr_str += read_until (plottok_or_end_p, tok); |
|
1277 |
|
1278 int status = 0; |
|
1279 octave_value tmp_data = eval_string (plot_expr_str, |
|
1280 true, status); |
|
1281 |
|
1282 if (status != 0 || ! tmp_data.is_defined ()) |
|
1283 throw gpt_parse_error (); |
|
1284 |
|
1285 std::ostringstream tmp_buf; |
|
1286 tmp_data.print_raw (tmp_buf); |
|
1287 |
|
1288 if (tmp_data.is_string ()) |
|
1289 { |
|
1290 file = file_ops::tilde_expand (tmp_data.string_value ()); |
|
1291 // FIXME -- perhaps should check if the file exists? |
|
1292 outstr += file + " "; |
|
1293 } |
|
1294 else |
|
1295 { |
|
1296 switch (ndim) |
|
1297 { |
|
1298 case 2: |
|
1299 file = save_in_tmp_file (tmp_data, ndim); |
|
1300 break; |
|
1301 |
|
1302 case 3: |
|
1303 file = save_in_tmp_file (tmp_data, ndim, |
|
1304 parametric_plot); |
|
1305 break; |
|
1306 |
|
1307 default: |
|
1308 gripe_2_or_3_dim_plot (); |
|
1309 break; |
|
1310 } |
|
1311 |
|
1312 if (file.length () > 0) |
|
1313 { |
|
1314 mark_for_deletion (file); |
|
1315 outstr += "'" + file + "' "; |
|
1316 } |
|
1317 } |
|
1318 } |
|
1319 |
|
1320 std::string title_str; |
|
1321 std::string using_str; |
|
1322 std::string style_str; |
|
1323 std::string axes_str; |
|
1324 |
|
1325 bool out = false; |
|
1326 while (tok && ! out) |
|
1327 { |
|
1328 switch (tok) |
|
1329 { |
|
1330 case COMMA: |
|
1331 out = true; |
|
1332 break; |
|
1333 |
|
1334 case TITLE: |
|
1335 if (! title_set) |
|
1336 title_str += handle_title (tok) + " "; |
|
1337 else |
|
1338 throw gpt_parse_error ("only one title option may be specified"); |
|
1339 title_set = true; |
|
1340 break; |
|
1341 |
|
1342 case USING: |
|
1343 if (! using_set) |
|
1344 using_str += handle_using (tok) + " "; |
|
1345 else |
|
1346 throw gpt_parse_error ("only one using option may be specified"); |
|
1347 using_set = true; |
|
1348 break; |
|
1349 |
|
1350 case WITH: |
|
1351 if (! style_set) |
|
1352 style_str += handle_style (tok) + " "; |
|
1353 else |
|
1354 throw gpt_parse_error ("only one style option may be specified"); |
|
1355 style_set = true; |
|
1356 break; |
|
1357 |
|
1358 case AXES: |
|
1359 if (! axes_set) |
|
1360 axes_str += handle_axes (tok) + " "; |
|
1361 else |
|
1362 throw gpt_parse_error ("only one axes option may be specified"); |
|
1363 axes_set = true; |
|
1364 break; |
|
1365 |
|
1366 default: |
|
1367 tok = 0; |
|
1368 break; |
|
1369 } |
|
1370 } |
|
1371 |
|
1372 if (! title_set) |
|
1373 { |
|
1374 std::ostringstream tmp_buf; |
|
1375 tmp_buf << Vgnuplot_command_title << " \"line " |
|
1376 << plot_line_count << "\" "; |
|
1377 title_str = tmp_buf.str (); |
|
1378 title_set = true; |
|
1379 } |
|
1380 |
|
1381 // Plot parameters have to be output in this order. |
|
1382 if (using_set) |
|
1383 outstr += using_str; |
|
1384 |
|
1385 if (axes_set) |
|
1386 outstr += axes_str; |
|
1387 |
|
1388 if (title_set) |
|
1389 outstr += title_str; |
|
1390 |
|
1391 if (style_set) |
|
1392 outstr += style_str; |
|
1393 |
|
1394 if (out) |
|
1395 { |
|
1396 // Saw comma on while loop. |
|
1397 outstr += ", "; |
|
1398 gpt_quote_is_transpose = false; |
|
1399 gpt_allow_plotkw = false; |
|
1400 gpt_parens = 0; |
|
1401 gpt_braces = 0; |
|
1402 gpt_brackets = 0; |
|
1403 tok = gptlex (); |
|
1404 } |
|
1405 } |
|
1406 |
|
1407 outstr += Vgnuplot_command_end; |
|
1408 |
|
1409 return outstr; |
|
1410 } |
|
1411 |
|
1412 // Title has one string expression which is evaluated and printed to the |
|
1413 // gnuplot command string. |
|
1414 |
|
1415 std::string |
|
1416 gnuplot::handle_title (int& lasttok) |
|
1417 { |
|
1418 int tok; |
|
1419 std::string retstr = Vgnuplot_command_title + " "; |
|
1420 std::string title_expr_str; |
|
1421 |
|
1422 title_expr_str += read_until (plottok_or_end_p, tok); |
|
1423 |
|
1424 int status; |
|
1425 octave_value tmp_data = eval_string (title_expr_str, true, status); |
|
1426 |
|
1427 if (status != 0 || ! tmp_data.is_defined ()) |
|
1428 throw gpt_parse_error (); |
|
1429 |
|
1430 std::ostringstream tmp_buf; |
|
1431 if (tmp_data.is_string ()) |
|
1432 { |
|
1433 tmp_buf << '"'; |
|
1434 tmp_data.print_raw (tmp_buf); |
|
1435 tmp_buf << '"'; |
|
1436 } |
|
1437 else |
|
1438 { |
|
1439 warning ("line title must be a string"); |
|
1440 tmp_buf << '"' << "line " << plot_line_count << '"'; |
|
1441 } |
|
1442 |
|
1443 retstr += tmp_buf.str (); |
|
1444 |
|
1445 lasttok = tok; |
|
1446 |
|
1447 return retstr; |
|
1448 } |
|
1449 |
|
1450 // The static instance of this class is here so that |
|
1451 // gnuplot::close_all will be called when the .oct file is unloaded. |
|
1452 |
|
1453 class |
|
1454 gnuplot_X |
|
1455 { |
|
1456 public: |
|
1457 gnuplot_X (void) { } |
|
1458 ~gnuplot_X (void) { gnuplot::close_all (); } |
|
1459 }; |
|
1460 |
|
1461 static gnuplot_X X; |
|
1462 |
|
1463 // ----------------------- |
|
1464 // User-callable functions |
|
1465 // ----------------------- |
|
1466 |
|
1467 DEFUN_DLD (automatic_replot, args, , |
|
1468 "-*- texinfo -*-\n\ |
|
1469 @deftypefn {Loadable Function} {@var{val} =} automatic_replot ()\n\ |
|
1470 @deftypefnx {Loadable Function} {@var{old_val} =} automatic_replot (@var{new_val})\n\ |
|
1471 Query or set the current automatic replot state. Although it is fairly\n\ |
|
1472 inefficient, especially for large plots, automatic replotting is enabled\n\ |
|
1473 by default for compatibility with Matlab.\n\ |
|
1474 \n\ |
|
1475 When setting the state, @var{new_val}\n\ |
|
1476 @end deftypefn") |
|
1477 { |
|
1478 return SET_INTERNAL_VARIABLE (automatic_replot); |
|
1479 } |
|
1480 |
|
1481 DEFUN_DLD (gnuplot_binary, args, , |
|
1482 "-*- texinfo -*-\n\ |
|
1483 @deftypefn {Loadable Function} gnuplot_binary\n\ |
|
1484 The name of the program invoked by the plot command. The default value\n\ |
|
1485 is @code{\"gnuplot\"}. @xref{Installation}.\n\ |
|
1486 @end deftypefn") |
|
1487 { |
|
1488 octave_value retval = SET_INTERNAL_VARIABLE (gnuplot_binary); |
|
1489 |
|
1490 if (! error_state && args.length () == 1) |
|
1491 gnuplot::set_gnuplot_exe (Vgnuplot_binary); |
|
1492 |
|
1493 return retval; |
|
1494 } |
|
1495 |
|
1496 DEFUN_DLD (gnuplot_command_plot, args, , |
|
1497 "-*- texinfo -*-\n\ |
|
1498 @deftypefn {Loadable Function} {@var{val} = } gnuplot_command_plot ()\n\ |
|
1499 @deftypefnx {Loadable Function} {@var{old_val} = } gnuplot_command_plot (@var{new_val})\n\ |
|
1500 @end deftypefn") |
|
1501 { |
|
1502 return SET_INTERNAL_VARIABLE (gnuplot_command_plot); |
|
1503 } |
|
1504 |
|
1505 DEFUN_DLD (gnuplot_command_replot, args, , |
|
1506 "-*- texinfo -*-\n\ |
|
1507 @deftypefn {Loadable Function} {@var{val} =} gnuplot_command_replot ()\n\ |
|
1508 @deftypefnx {Loadable Function} {@var{old_val} =} gnuplot_command_replot (@var{new_val})\n\ |
|
1509 @end deftypefn") |
|
1510 { |
|
1511 return SET_INTERNAL_VARIABLE (gnuplot_command_replot); |
|
1512 } |
|
1513 |
|
1514 DEFUN_DLD (gnuplot_command_splot, args, , |
|
1515 "-*- texinfo -*-\n\ |
|
1516 @deftypefn {Loadable Function} {@var{val} =} gnuplot_command_splot ()\n\ |
|
1517 @deftypefnx {Loadable Function} {@var{old_val} =} gnuplot_command_splot (@var{new_val})\n\ |
|
1518 @end deftypefn") |
|
1519 { |
|
1520 return SET_INTERNAL_VARIABLE (gnuplot_command_splot); |
|
1521 } |
|
1522 |
|
1523 DEFUN_DLD (gnuplot_command_using, args, , |
|
1524 "-*- texinfo -*-\n\ |
|
1525 @deftypefn {Loadable Function} {@var{val} =} gnuplot_command_using ()\n\ |
|
1526 @deftypefnx {Loadable Function} {@var{old_val} =} gnuplot_command_using (@var{new_val})\n\ |
|
1527 @end deftypefn") |
|
1528 { |
|
1529 return SET_INTERNAL_VARIABLE (gnuplot_command_using); |
|
1530 } |
|
1531 |
|
1532 DEFUN_DLD (gnuplot_command_with, args, , |
|
1533 "-*- texinfo -*-\n\ |
|
1534 @deftypefn {Loadable Function} {@var{val} =} gnuplot_command_with ()\n\ |
|
1535 @deftypefnx {Loadable Function} {@var{old_val} =} gnuplot_command_with (@var{new_val})\n\ |
|
1536 @end deftypefn") |
|
1537 { |
|
1538 return SET_INTERNAL_VARIABLE (gnuplot_command_with); |
|
1539 } |
|
1540 |
|
1541 DEFUN_DLD (gnuplot_command_axes, args, , |
|
1542 "-*- texinfo -*-\n\ |
|
1543 @deftypefn {Loadable Function} {@var{val} =} gnuplot_command_axes ()\n\ |
|
1544 @deftypefnx {Loadable Function} {@var{old_val} =} gnuplot_command_axes (@var{new_val})\n\ |
|
1545 @end deftypefn") |
|
1546 { |
|
1547 return SET_INTERNAL_VARIABLE (gnuplot_command_axes); |
|
1548 } |
|
1549 |
|
1550 DEFUN_DLD (gnuplot_command_title, args, , |
|
1551 "-*- texinfo -*-\n\ |
|
1552 @deftypefn {Loadable Function} {@var{val} =} gnuplot_command_title ()\n\ |
|
1553 @deftypefnx {Loadable Function} {@var{old_val} =} gnuplot_command_title (@var{new_val})\n\ |
|
1554 @end deftypefn") |
|
1555 { |
|
1556 return SET_INTERNAL_VARIABLE (gnuplot_command_title); |
|
1557 } |
|
1558 |
|
1559 DEFUN_DLD (gnuplot_command_end, args, , |
|
1560 "-*- texinfo -*-\n\ |
|
1561 @deftypefn {Loadable Function} {@var{val} =} gnuplot_command_end ()\n\ |
|
1562 @deftypefnx {Loadable Function} {@var{old_val} =} gnuplot_command_end (@var{new_val})\n\ |
|
1563 @end deftypefn") |
|
1564 { |
|
1565 return SET_INTERNAL_VARIABLE (gnuplot_command_end); |
|
1566 } |
|
1567 |
|
1568 DEFUN_DLD (gnuplot_use_title_option, args, , |
|
1569 "-*- texinfo -*-\n\ |
|
1570 @deftypefn {Loadable Function} {@var{val} =} gnuplot_use_title_option ()\n\ |
|
1571 @deftypefnx {Loadable Function} {@var{old_val} =} gnuplot_use_title_option (@var{new_val})\n\ |
|
1572 If enabled, append @samp{-title \"Figure NN\"} to the gnuplot command.\n\ |
|
1573 By default, this feature is enabled if the @code{DISPLAY} environment\n\ |
|
1574 variable is set when Octave starts.\n\ |
|
1575 @end deftypefn") |
|
1576 { |
|
1577 return SET_INTERNAL_VARIABLE (gnuplot_use_title_option); |
|
1578 } |
|
1579 |
|
1580 DEFUN_DLD (clearplot, , , |
|
1581 "-*- texinfo -*-\n\ |
|
1582 @deftypefn {Loadable Function} {} clearplot\n\ |
|
1583 @deftypefnx {Loadable Function} {} clg\n\ |
|
1584 Clear the plot window and any titles or axis labels. The name\n\ |
|
1585 @code{clg} is aliased to @code{clearplot} for compatibility with\n\ |
|
1586 @sc{Matlab}.\n\ |
|
1587 @end deftypefn") |
|
1588 { |
|
1589 gnuplot::clear (); |
|
1590 |
|
1591 octave_value_list args; |
|
1592 |
|
1593 args(0) = "off"; |
|
1594 |
|
1595 feval ("hold", args); |
|
1596 |
|
1597 return octave_value_list (); |
|
1598 } |
|
1599 |
|
1600 DEFUN_DLD (closeplot, , , |
|
1601 "-*- texinfo -*-\n\ |
|
1602 @deftypefn {Loadable Function} {} closeplot\n\ |
|
1603 Close stream to the @code{gnuplot} subprocess. If you are using X11,\n\ |
|
1604 this will close the plot window.\n\ |
|
1605 @end deftypefn") |
|
1606 { |
|
1607 gnuplot::close (); |
|
1608 |
|
1609 return octave_value_list (); |
|
1610 } |
|
1611 |
|
1612 DEFUN_DLD (purge_tmp_files, , , |
|
1613 "-*- texinfo -*-\n\ |
|
1614 @deftypefn {Loadable Function} {} purge_tmp_files\n\ |
|
1615 Delete the temporary files created by the plotting commands.\n\ |
|
1616 \n\ |
|
1617 Octave creates temporary data files for @code{gnuplot} and then sends\n\ |
|
1618 commands to @code{gnuplot} through a pipe. Octave will delete the\n\ |
|
1619 temporary files on exit, but if you are doing a lot of plotting you may\n\ |
|
1620 want to clean up in the middle of a session.\n\ |
|
1621 \n\ |
|
1622 A future version of Octave will eliminate the need to use temporary\n\ |
|
1623 files to hold the plot data.\n\ |
|
1624 @end deftypefn") |
|
1625 { |
|
1626 gnuplot::cleanup_tmp_files (); |
|
1627 |
|
1628 return octave_value_list (); |
|
1629 } |
|
1630 |
|
1631 DEFUN_DLD (__gnuplot_raw__, args, , |
|
1632 "-*- texinfo -*-\n\ |
|
1633 @deftypefn {Loadable Function} {} __gnuplot_raw__ (@var{string})\n\ |
|
1634 Send @var{string} directly to gnuplot subprocess.\n\ |
|
1635 @end deftypefn") |
|
1636 { |
|
1637 if (args.length () == 1 && args(0).is_string ()) |
|
1638 { |
|
1639 std::string cmd = args(0).string_value (); |
|
1640 |
|
1641 gnuplot::send_raw (cmd); |
|
1642 } |
|
1643 else |
|
1644 print_usage ("__gnuplot_raw__"); |
|
1645 |
|
1646 return octave_value_list (); |
|
1647 } |
|
1648 |
|
1649 DEFUN_DLD (__gnuplot_set__, args, , |
|
1650 "-*- texinfo -*-\n\ |
|
1651 @deffn {{Loadable Function} __gnuplot_set__ options\n\ |
|
1652 Set plotting options for gnuplot\n\ |
|
1653 @end deffn") |
|
1654 { |
|
1655 string_vector argv = args.make_argv ("set"); |
|
1656 |
|
1657 if (! error_state) |
|
1658 gnuplot::set (argv); |
|
1659 |
|
1660 return octave_value_list (); |
|
1661 } |
|
1662 |
|
1663 DEFUN_DLD (__gnuplot_show__, args, , |
|
1664 "-*- texinfo -*-\n\ |
|
1665 @deffn {{Loadable Function} __gnuplot_show__ options\n\ |
|
1666 Show plotting options.\n\ |
|
1667 @end deffn") |
|
1668 { |
|
1669 string_vector argv = args.make_argv ("show"); |
|
1670 |
|
1671 if (! error_state) |
|
1672 gnuplot::show (argv); |
|
1673 |
|
1674 return octave_value_list (); |
|
1675 } |
|
1676 |
|
1677 DEFUN_DLD (__gnuplot_plot__, args, , |
|
1678 "Plot with gnuplot.\n") |
|
1679 { |
|
1680 string_vector argv = args.make_argv ("plot"); |
|
1681 |
|
1682 if (! error_state) |
|
1683 gnuplot::plot (argv); |
|
1684 |
|
1685 return octave_value_list (); |
|
1686 } |
|
1687 |
|
1688 DEFUN_DLD (__gnuplot_splot__, args, , |
|
1689 "Plot with gnuplot.\n") |
|
1690 { |
|
1691 string_vector argv = args.make_argv ("splot"); |
|
1692 |
|
1693 if (! error_state) |
|
1694 gnuplot::plot (argv); |
|
1695 |
|
1696 return octave_value_list (); |
|
1697 } |
|
1698 |
|
1699 DEFUN_DLD (__gnuplot_replot__, args, , |
|
1700 "Plot with gnuplot.\n") |
|
1701 { |
|
1702 string_vector argv = args.make_argv ("replot"); |
|
1703 |
|
1704 if (! error_state) |
|
1705 gnuplot::plot (argv); |
|
1706 |
|
1707 return octave_value_list (); |
|
1708 } |
|
1709 |
|
1710 /* |
|
1711 ;;; Local Variables: *** |
|
1712 ;;; mode: C++ *** |
|
1713 ;;; End: *** |
|
1714 */ |