Mercurial > hg > octave-nkf
annotate scripts/plot/gnuplot_drawnow.m @ 7680:a0ec02774303
Use popen2 for communication with gnuplot
author | David Bateman <dbateman@free.fr> |
---|---|
date | Wed, 02 Apr 2008 13:29:19 -0400 |
parents | 246f905cb984 |
children | 1f429086565c |
rev | line source |
---|---|
7408 | 1 ## Copyright (C) 2005, 2006, 2007 John W. Eaton |
2 ## | |
3 ## This file is part of Octave. | |
4 ## | |
5 ## Octave is free software; you can redistribute it and/or modify it | |
6 ## under the terms of the GNU General Public License as published by | |
7 ## the Free Software Foundation; either version 3 of the License, or (at | |
8 ## your option) any later version. | |
9 ## | |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 ## General Public License for more details. | |
14 ## | |
15 ## You should have received a copy of the GNU General Public License | |
16 ## along with Octave; see the file COPYING. If not, see | |
17 ## <http://www.gnu.org/licenses/>. | |
18 | |
19 ## -*- texinfo -*- | |
20 ## @deftypefn {Function File} {} drawnow () | |
21 ## Update and display the current graphics. | |
22 ## | |
23 ## Octave automatically calls drawnow just before printing a prompt, | |
24 ## when @code{sleep} or @code{pause} is called, or while waiting for | |
25 ## command-line input. | |
26 ## @end deftypefn | |
27 | |
28 ## Author: jwe | |
29 | |
30 function gnuplot_drawnow (h, term, file, mono, debug_file) | |
31 | |
32 if (nargin < 4) | |
33 mono = false; | |
34 endif | |
35 | |
36 if (nargin >= 3 && nargin <= 5) | |
37 f = __get__ (h); | |
38 plot_stream = []; | |
39 fid = []; | |
40 unwind_protect | |
7680
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7408
diff
changeset
|
41 [plot_stream, enhanced] = open_gnuplot_stream (1, [], term, file); |
7408 | 42 __go_draw_figure__ (f, plot_stream, enhanced, mono); |
43 if (nargin == 5) | |
44 fid = fopen (debug_file, "wb"); | |
45 enhanced = init_plot_stream (fid, [], term, file); | |
46 __go_draw_figure__ (f, fid, enhanced, mono); | |
47 endif | |
48 unwind_protect_cleanup | |
49 if (! isempty (plot_stream)) | |
50 pclose (plot_stream); | |
51 endif | |
52 if (! isempty (fid)) | |
53 fclose (fid); | |
54 endif | |
55 end_unwind_protect | |
56 elseif (nargin == 1) | |
57 f = __get__ (h); | |
58 plot_stream = f.__plot_stream__; | |
59 if (isempty (plot_stream)) | |
7680
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7408
diff
changeset
|
60 [plot_stream, enhanced] = open_gnuplot_stream (2, h); |
7408 | 61 set (h, "__enhanced__", enhanced); |
62 else | |
63 enhanced = f.__enhanced__; | |
64 endif | |
7680
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7408
diff
changeset
|
65 __go_draw_figure__ (f, plot_stream (1), enhanced, mono); |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7408
diff
changeset
|
66 fflush (plot_stream (1)); |
7408 | 67 else |
68 print_usage (); | |
69 endif | |
70 | |
71 endfunction | |
72 | |
7680
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7408
diff
changeset
|
73 function [plot_stream, enhanced] = open_gnuplot_stream (npipes, h, varargin) |
7408 | 74 |
75 cmd = gnuplot_binary (); | |
76 | |
7680
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7408
diff
changeset
|
77 if (npipes > 1) |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7408
diff
changeset
|
78 [plot_stream(1), plot_stream(2), pid] = popen2 (cmd); |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7408
diff
changeset
|
79 if (pid < 0) |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7408
diff
changeset
|
80 error ("drawnow: failed to open connection to gnuplot"); |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7408
diff
changeset
|
81 endif |
7408 | 82 else |
7680
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7408
diff
changeset
|
83 plot_stream = popen (cmd, "w"); |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7408
diff
changeset
|
84 if (plot_stream < 0) |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7408
diff
changeset
|
85 error ("drawnow: failed to open connection to gnuplot"); |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7408
diff
changeset
|
86 endif |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7408
diff
changeset
|
87 endif |
7408 | 88 |
7680
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7408
diff
changeset
|
89 if (! isempty (h)) |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7408
diff
changeset
|
90 set (h, "__plot_stream__", plot_stream); |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7408
diff
changeset
|
91 endif |
7408 | 92 |
7680
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7408
diff
changeset
|
93 enhanced = init_plot_stream (plot_stream (1), h, varargin{:}); |
7408 | 94 |
95 endfunction | |
96 | |
97 function enhanced = init_plot_stream (plot_stream, h, term, file) | |
98 | |
99 if (nargin == 4) | |
100 enhanced = enhanced_term (term); | |
101 if (! isempty (term)) | |
102 if (enhanced) | |
103 fprintf (plot_stream, "set terminal %s enhanced;\n", term); | |
104 else | |
105 fprintf (plot_stream, "set terminal %s;\n", term); | |
106 endif | |
107 endif | |
108 if (! isempty (file)) | |
109 fprintf (plot_stream, "set output \"%s\";\n", file); | |
110 endif | |
111 else | |
112 | |
113 ## Guess the terminal type. | |
114 term = getenv ("GNUTERM"); | |
115 if (isempty (term)) | |
116 if (! isempty (getenv ("DISPLAY"))) | |
117 term = "x11"; | |
118 elseif (! isunix ()) | |
119 term = "windows"; | |
120 else | |
121 ## This should really be checking for os x before setting | |
122 ## the terminal type to aqua, but nobody will notice because | |
123 ## every other unix will be using x11 and windows will be | |
124 ## using windows. Those diehards still running octave from | |
125 ## a linux console know how to set the GNUTERM variable. | |
126 term = "aqua"; | |
127 endif | |
128 endif | |
129 | |
130 enhanced = enhanced_term (term); | |
131 if (enhanced) | |
132 enh_str = "enhanced"; | |
133 else | |
134 enh_str = ""; | |
135 endif | |
136 | |
137 ## If no 'h' (why not?) then open the terminal as Figure 0. | |
138 if (isempty (h)) | |
139 h = 0; | |
140 endif | |
141 | |
142 if (strcmp (term, "x11")) | |
143 fprintf (plot_stream, "set terminal x11 %s title \"Figure %d\"\n", | |
144 enh_str, h); | |
145 elseif (strcmp (term, "aqua")) | |
146 ## Aqua doesn't understand the 'title' option despite what the | |
147 ## gnuplot 4.2 documentation says. | |
148 fprintf (plot_stream, "set terminal aqua %d %s\n", h, enh_str); | |
149 elseif (strcmp (term, "wxt")) | |
150 fprintf (plot_stream, "set terminal wxt %s title \"Figure %d\"\n", | |
151 enh_str, h); | |
152 | |
153 elseif (enhanced) | |
154 fprintf (plot_stream, "set terminal %s %s\n", term, enh_str); | |
155 endif | |
156 ## gnuplot will pick up the GNUTERM environment variable itself | |
157 ## so no need to set the terminal type if not also setting the | |
158 ## figure title or enhanced mode. | |
159 | |
160 endif | |
161 | |
162 endfunction | |
163 | |
164 function have_enhanced = enhanced_term (term) | |
165 persistent enhanced_terminals; | |
166 | |
167 if (isempty (enhanced_terminals)) | |
168 ## Don't include pstex, pslatex or epslatex here as the TeX commands | |
169 ## should not be interpreted in that case. | |
170 if (compare_versions (__gnuplot_version__ (), "4.0", ">")) | |
171 enhanced_terminals = {"aqua", "dumb", "png", "jpeg", "gif", "pm", ... | |
172 "windows", "wxt", "svg", "postscript", "x11", "pdf"}; | |
173 else | |
174 enhanced_terminals = {"x11", "postscript"}; | |
175 endif | |
176 endif | |
177 | |
178 term = tolower (term); | |
179 | |
180 have_enhanced = false; | |
181 for i = 1 : length (enhanced_terminals) | |
182 t = enhanced_terminals{i}; | |
183 if (strncmp (term, t, min (length (term), length (t)))) | |
184 have_enhanced = true; | |
185 break; | |
186 endif | |
187 endfor | |
188 endfunction |