Skip to content

Commit a5eeb46

Browse files
authored
Merge pull request #306 from jgray-19/plot_improvements
Add a flag to force the plots to close on exit
2 parents db3a1fd + d1e47e8 commit a5eeb46

File tree

1 file changed

+27
-10
lines changed

1 file changed

+27
-10
lines changed

src/madl_plot.mad

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -265,23 +265,39 @@ MAD_SET_TERM = " \
265265

266266
]]
267267

268+
local kill_sids = {}
269+
local function kill_plots()
270+
pause("Closing plots, ")
271+
for sid, term in pairs(kill_sids) do
272+
gplot:set_sid(sid)
273+
gplot:printf("set term %s close", term)
274+
end
275+
end
276+
277+
local kill_atexit = \ atexit(kill_plots)
268278
-- dump header and set terminal to script file
269279
local function setup_cmd (self)
270-
local output, scrdump in self
271-
local file, fname = get_tmpfile(self.sid)
272-
gplot:set_sid(self.sid)
280+
local output, default, scrdump, font, fontsize, sid, term, close_plots in self
281+
local file, fname = get_tmpfile(sid)
282+
gplot:set_sid(sid)
273283
file:write(init_cmd % self)
274284

275-
self.font = self.font or ""
276-
self.fontsize = self.fontsize or ""
285+
self.font = font or ""
286+
self.fontsize = fontsize or ""
277287

278288
if is_nil(output) then
279289
file:write("@MAD_INC_WID\n")
280-
file:write("MAD_TERM = \"set term ${term} MAD_WID size ${wsizex},\z
281-
${wsizey} enhanced font '${font},${fontsize}'\"\n" % self)
282-
elseif is_number(output) then
283-
file:write("MAD_TERM = \"set term ${term} ${output} size ${wsizex},\z
284-
${wsizey} enhanced font '${font},${fontsize}'\"\n" % self)
290+
output = "MAD_WID" -- So as not to mutate the object
291+
end
292+
if is_number(output) or output == "MAD_WID" then
293+
file:write(
294+
("MAD_TERM = \"set term ${term} " .. output .. " size ${wsizex},\z
295+
${wsizey} enhanced font '${font},${fontsize}'\"\n") % self
296+
)
297+
if close_plots then
298+
runonce(kill_atexit)
299+
kill_sids[sid] = term
300+
end
285301
elseif is_string(output) then
286302
local ext = output:sub(-3, -1)
287303
if ext == "png" then
@@ -1019,6 +1035,7 @@ local plot = command "plot" {
10191035

10201036
-- output
10211037
output = nil, -- ("filename") -> pdf | number -> wid
1038+
close_plots = true, -- boolean, close all plots on exit of MAD-NG or not
10221039
scrdump = nil, -- ("filename")
10231040

10241041
-- title

0 commit comments

Comments
 (0)