Skip to content

Commit

Permalink
print.m: Optionally don't append file extension to file name (bug #64…
Browse files Browse the repository at this point in the history
…510).

* scripts/plot/util/private/__print_parse_opts__.m: Add flag
"-no-append-file-extension" to skip adding a file extension that would match
the selected file format.

* scripts/plot/util/print.m: Add documentation for new flag. Add test.
  • Loading branch information
mmuetzel committed Aug 27, 2023
1 parent 673d8e8 commit 0059398
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
21 changes: 21 additions & 0 deletions scripts/plot/util/print.m
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,10 @@
## respectively. Allowed values for @var{N} are 1, 2, or 4.
## @end table
##
## @item -no-append-file-extension
## With this option, @var{filename} is used verbatim. That means no file
## extension matching the file format is appended automatically.
##
## @seealso{saveas, getframe, savefig, hgsave, orient, figure}
## @end deftypefn

Expand Down Expand Up @@ -823,6 +827,23 @@

endfunction

## Print to file with and without file extension
%!test
%! hf = figure ("visible", "off");
%! unwind_protect
%! x = 0:0.1:1;
%! hax = axes (hf);
%! plot (hax, x, x);
%! tmp_name = tempname ();
%! print (hf, tmp_name, "-dpng");
%! assert (isfile ([tmp_name ".png"]));
%! unlink ([tmp_name ".png"]);
%! print (hf, tmp_name, "-dpng", "-no-append-file-extension");
%! assert (isfile (tmp_name));
%! unlink (tmp_name);
%! unwind_protect_cleanup
%! close (hf);
%! end_unwind_protect

%!error <a graphics handle>
%! hf = figure ("visible", "off");
Expand Down
16 changes: 10 additions & 6 deletions scripts/plot/util/private/__print_parse_opts__.m
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
arg_st.lpr_binary = __quote_path__ (__find_binary__ ("lpr"));
arg_st.polymerge = 1;
arg_st.name = "";
arg_st.append_file_extension = true;
arg_st.orientation = "";
arg_st.pstoedit_binary = __quote_path__ (__find_binary__ ("pstoedit"));
arg_st.preview = "";
Expand Down Expand Up @@ -148,6 +149,8 @@
elseif (any (strcmp (arg,
{"-interchange", "-metafile", "-pict", "-tiff"})))
arg_st.preview = arg(2:end);
elseif (strcmp (arg, "-no-append-file-extension"))
arg_st.append_file_extension = false;
elseif (strncmp (arg, "-debug", 6))
arg_st.debug = true;
arg_st.ghostscript.debug = true;
Expand Down Expand Up @@ -233,7 +236,7 @@
if (isempty (arg_st.devopt))
if (arg_st.rgb_output)
arg_st.devopt = "png";
elseif (dot == 0)
elseif (dot == 0 || ! arg_st.append_file_extension)
arg_st.devopt = "psc";
else
arg_st.devopt = lower (arg_st.name(dot+1:end));
Expand Down Expand Up @@ -355,7 +358,8 @@
default_suffix = suffixes{match};
endif

if (dot == 0 && ! isempty (arg_st.name) && ! isempty (default_suffix))
if (arg_st.append_file_extension && dot == 0 && ! isempty (arg_st.name) ...
&& ! isempty (default_suffix))
arg_st.name = [arg_st.name "." default_suffix];
endif

Expand Down Expand Up @@ -809,10 +813,10 @@
aliases.tiffn = "tiff24nc";

if (do_eps)
aliases.eps = "ps2write";
aliases.eps2 = "ps2write";
aliases.epsc = "ps2write";
aliases.epsc2 = "ps2write";
aliases.eps = "eps2write";
aliases.eps2 = "eps2write";
aliases.epsc = "eps2write";
aliases.epsc2 = "eps2write";
endif

endfunction

0 comments on commit 0059398

Please sign in to comment.