changeset 15814:1eee8d8c59fd

Return history as a cell string when called with an output argument (bug #37947) * oct-hist.cc (do_history): Return hlist variable. Don't output to octave_stdout unless requested. (Fhistory): Update docstring. Return hlist as a cell string if nargout > 0
author Jordi Gutiérrez Hermoso <jordigh@octave.org>
date Tue, 18 Dec 2012 14:29:15 -0500
parents 10d6ab9b1b4f
children f28b3dcbaa9a
files libinterp/interpfcn/oct-hist.cc
diffstat 1 files changed, 27 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/interpfcn/oct-hist.cc	Tue Dec 18 10:29:51 2012 -0800
+++ b/libinterp/interpfcn/oct-hist.cc	Tue Dec 18 14:29:15 2012 -0500
@@ -128,12 +128,13 @@
 // means read file, arg of -q means don't number lines.  Arg of N
 // means only display that many items.
 
-static void
-do_history (int argc, const string_vector& argv)
+static string_vector
+do_history (int argc, const string_vector& argv, bool output = true)
 {
   int numbered_output = 1;
 
   unwind_protect frame;
+  string_vector hlist;
 
   frame.add_fcn (command_history::set_file, command_history::file ());
 
@@ -167,7 +168,7 @@
           else
             panic_impossible ();
 
-          return;
+          return hlist;
         }
       else if (argv[i] == "-q")
         numbered_output = 0;
@@ -191,19 +192,22 @@
           else
             error ("history: bad non-numeric arg '%s'", argv[i].c_str ());
 
-          return;
+          return hlist;
         }
 
       if (limit < 0)
         limit = -limit;
     }
 
-  string_vector hlist = command_history::list (limit, numbered_output);
+  hlist = command_history::list (limit, numbered_output);
 
   int len = hlist.length ();
 
-  for (i = 0; i < len; i++)
-    octave_stdout << hlist[i] << "\n";
+  if (output)
+    for (i = 0; i < len; i++)
+      octave_stdout << hlist[i] << "\n";
+
+  return hlist;
 }
 
 // Read the edited history lines from STREAM and return them
@@ -582,9 +586,10 @@
   return retval;
 }
 
-DEFUN (history, args, ,
+DEFUN (history, args, nargout,
   "-*- texinfo -*-\n\
-@deftypefn {Command} {} history options\n\
+@deftypefn {Command} history options\n\
+@deftypefnx {Built-in Function} {@var{h} = } history (@var{opt1}, @var{opt2}, @dots{})\n\
 If invoked with no arguments, @code{history} displays a list of commands\n\
 that you have executed.  Valid options are:\n\
 \n\
@@ -609,6 +614,9 @@
 For example, to display the five most recent commands that you have\n\
 typed without displaying line numbers, use the command\n\
 @kbd{history -q 5}.\n\
+\n\
+If invoked with a single output argument, the history will be saved to that\n\
+argument as a cell string and will not be output to screen.\n\
 @end deftypefn")
 {
   octave_value_list retval;
@@ -620,7 +628,16 @@
   if (error_state)
     return retval;
 
-  do_history (argc, argv);
+  string_vector hlist;
+  if (nargout > 0)
+    {
+      argv.append (std::string ("-q"));
+      argc++;
+      hlist = do_history (argc, argv, false);
+      retval(0) = Cell (hlist);
+    }
+  else
+    do_history (argc, argv, true);
 
   return retval;
 }