changeset 28314:bc904ed5aad4

saveas.m: Change default format to .ofig (bug #58391). * NEWS: Announce compatibility change from PDF to OFIG format. * saveas.m: New default_fmt variable initialized with "ofig" rather than "pdf". Decode "fmt" and call savefig or print as appropriate. New code for "mfig" option to save an ofig file and create an m-file with commands to open it.
author Guillaume Flandin <guillaume.offline@gmail.com>
date Mon, 18 May 2020 09:00:50 -0700
parents d13ad9dc9348
children 7a1736f89c6b
files NEWS scripts/plot/util/saveas.m
diffstat 2 files changed, 45 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/NEWS	Fri May 15 23:03:24 2020 +0200
+++ b/NEWS	Mon May 18 09:00:50 2020 -0700
@@ -74,6 +74,9 @@
 previous versions of Octave, these functions returned a hg group of patch
 graphics objects for all graphics toolkits.
 
+- The function `saveas` now defaults to saving in Octave figure format
+(.ofig) rather than PDF (.pdf).
+
 ### Alphabetical list of new functions added in Octave 7
 
 * `endsWith`
--- a/scripts/plot/util/saveas.m	Fri May 15 23:03:24 2020 +0200
+++ b/scripts/plot/util/saveas.m	Mon May 18 09:00:50 2020 -0700
@@ -33,6 +33,14 @@
 ## are:
 ##
 ## @table @code
+##
+##   @item ofig
+##     Octave figure file format (default)
+##
+##   @item mfig
+##     Two files: Octave m-file @file{filename.m} containing code
+##     to open Octave figure file @file{filename.ofig}
+##
 ##   @item ps
 ##     PostScript
 ##
@@ -58,7 +66,7 @@
 ##
 ## If @var{fmt} is omitted it is extracted from the extension of
 ## @var{filename}.  The default format when there is no extension is
-## @qcode{"pdf"}.
+## @qcode{"ofig"}.
 ##
 ## @example
 ## @group
@@ -68,7 +76,7 @@
 ## @end group
 ## @end example
 ##
-## @seealso{print, hgsave, orient}
+## @seealso{print, savefig, hgsave, orient}
 ## @end deftypefn
 
 function saveas (h, filename, fmt)
@@ -90,32 +98,57 @@
     fig = ancestor (h, "figure");
   endif
 
+  default_fmt = "ofig";
+
   if (nargin == 2)
     ## Attempt to infer format from filename
     [~, ~, ext] = fileparts (filename);
-    if (! isempty (ext))
-      fmt = ext(2:end);
-    else
-      fmt = "pdf";
+    if (isempty (ext))
+      ext = ["." default_fmt];
+      filename = [filename ext];
     endif
+    fmt = ext(2:end);
   endif
 
   if (nargin == 3)
     if (! ischar (fmt))
       error ("saveas: FMT must be a string");
+    elseif (isempty (fmt))
+      fmt = default_fmt;
     endif
     [~, ~, ext] = fileparts (filename);
     if (isempty (ext))
-      filename = [filename "." fmt];
+      ext = ["." fmt];
+      filename = [filename ext];
     endif
   endif
 
-  prt_opt = ["-d" tolower(fmt)];
+  fmt = tolower (fmt);
+
+  if (any (strcmp (fmt, {"ofig", "fig"})))
+    savefig (fig, filename);
+  elseif (any (strcmp (fmt, {"m", "mfig"})))
+    [d, n] = fileparts (filename);
+    mfilename = fullfile (d, [n ".m"]);
+    figfilename = fullfile (d, [n ".ofig"]);
+
+    savefig (fig, figfilename);
 
-  print (fig, filename, prt_opt);
+    fid = fopen (mfilename, "wt");
+    if (fid < 0)
+      error ("saveas: could not open '%s' for writing", mfilename);
+    endif
+    fprintf (fid, ['h = openfig ("' figfilename '");' "\n"]);
+    fclose (fid);
+  else
+    prt_opt = ["-d" fmt];
+
+    print (fig, filename, prt_opt);
+  endif
 
 endfunction
 
+
 ## Test input validation
 %!error saveas ()
 %!error saveas (1)