changeset 30992:4ef25c610433

format: Add 'default' format option (bug #62430) * pr-output.cc (set_format_style): Add format option 'default' to set the same state as if format was called without arguments and overrides other format options if multiple options are provided. * pr-output.cc (format): Update docstring with changes about new 'default' option, function calling form, and errors when both inputs and outputs are specified. * NEWS.8.md: Note change under Matlab Compatability.
author Nicholas R. Jankowski <jankowski.nicholas@gmail.com>
date Mon, 09 May 2022 13:58:46 -0400
parents 2e39b8ff1860
children 1212ed22f962
files etc/NEWS.8.md libinterp/corefcn/pr-output.cc
diffstat 2 files changed, 34 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/etc/NEWS.8.md	Mon May 09 12:58:05 2022 -0400
+++ b/etc/NEWS.8.md	Mon May 09 13:58:46 2022 -0400
@@ -33,6 +33,9 @@
 - `print` now accepts option `-image` to specify the "opengl" renderer
   and `-vector` to specify the "painters" renderer.
 
+- `format` now accepts the option "default", which is equivalent to
+  calling `format` without any options to reset the default state.
+
 ### Alphabetical list of new functions added in Octave 8
 
 
--- a/libinterp/corefcn/pr-output.cc	Mon May 09 12:58:05 2022 -0400
+++ b/libinterp/corefcn/pr-output.cc	Mon May 09 13:58:46 2022 -0400
@@ -3614,7 +3614,15 @@
       std::string arg = argv[idx++];
       std::transform (arg.begin (), arg.end (), arg.begin (), tolower);
 
-      if (arg == "short")
+      if (arg == "default")
+        {
+          format = "short";
+          init_format_state ();
+          set_output_prec (5);
+          Vcompact_format = false;
+          uppercase_format = false;
+        }
+      else if (arg == "short")
         {
           format = arg;
           init_format_state ();
@@ -3806,6 +3814,7 @@
        doc: /* -*- texinfo -*-
 @deftypefn  {} {} format
 @deftypefnx {} {} format options
+@deftypefnx {} {} format (@var{options})
 @deftypefnx {} {[@var{format}, @var{formatspacing}, @var{uppercase}] =} format
 Reset or specify the format of the output produced by @code{disp} and Octave's
 normal echoing mechanism.
@@ -3817,13 +3826,16 @@
 
 By default, Octave displays 5 significant digits in a human readable form
 (option @samp{short}, option @samp{lowercase}, and option @samp{loose} format
-for matrices).  If @code{format} is invoked without any options, this default
-format is restored.
+for matrices).  If @code{format} is invoked without any options, or the option
+@samp{default} is specified, then this default format is restored.
 
 Valid format options for floating point numbers are listed in the following
 table.
 
 @table @code
+@item default
+Restore the default format state described above.
+
 @item short
 Fixed point format with 5 significant figures (default).
 
@@ -3969,10 +3981,12 @@
 @end table
 
 If @code{format} is called with multiple competing options, the rightmost one
-is used.  In case of an error the format remains unchanged.
+is used, except for @samp{default} which will override all other options. In
+case of an error the format remains unchanged.
 
 If called with one to three output arguments, and no inputs, return the current
-format, format spacing, and uppercase preference.
+format, format spacing, and uppercase preference.  Specifying both outputs and
+inputs will produce an error.
 
 @seealso{fixed_point_format, output_precision, split_long_rows,
 print_empty_dimensions, rats}
@@ -4020,7 +4034,7 @@
 %!   assert (str, "3.1415927E+00\n");
 %!   new_fmt = format ();
 %!   assert (new_fmt, "longe");
-%!   ## Test resetting format
+%!   ## Test resetting format (method #1)
 %!   format compact;
 %!   [~, new_spacing] = format ();
 %!   assert (new_spacing, "compact");
@@ -4029,6 +4043,17 @@
 %!   assert (new_fmt, "short");
 %!   assert (new_spacing, "loose");
 %!   assert (new_case, "lowercase");
+%!   ## Test resetting format (method #2)
+%!   format compact uppercase long e;
+%!   [new_fmt, new_spacing, new_case] = format ();
+%!   assert (new_fmt, "longe");
+%!   assert (new_spacing, "compact");
+%!   assert (new_case, "uppercase");
+%!   format ("default");
+%!   [new_fmt, new_spacing, new_case] = format ();
+%!   assert (new_fmt, "short");
+%!   assert (new_spacing, "loose");
+%!   assert (new_case, "lowercase");
 %! unwind_protect_cleanup
 %!   format (old_fmt);
 %!   format (old_spacing);