changeset 16188:8366bc871d45

Allow negative numbers to be used in specifying edit_history commands. * libinterp/interpfcn/oct-hist.cc(mk_tmp_hist_file): Add total number of history commands to negative history numbers to wraparound to positive history number specification. * libinterp/interpfcn/oct-hist.cc(Fedit_history, Frun_history): Update docstrings to describe new behavior. * libinterp/interpfcn/oct-hist.cc(do_edit_history): Comment out first variable to stop unused warning during compilation.
author Rik <rik@octave.org>
date Mon, 04 Mar 2013 08:51:40 -0800
parents 490625211945
children 2825897a0c27
files libinterp/interpfcn/oct-hist.cc
diffstat 1 files changed, 95 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/interpfcn/oct-hist.cc
+++ b/libinterp/interpfcn/oct-hist.cc
@@ -404,8 +404,14 @@
       if (get_int_arg (args(0), hist_beg)
           && get_int_arg (args(1), hist_end))
         {
-          hist_beg--;
-          hist_end--;
+          if (hist_beg < 0)
+            hist_beg += (hist_count + 1);
+          else
+            hist_beg--;
+          if (hist_end < 0)
+            hist_end += (hist_count + 1);
+          else
+            hist_end--;
         }
       else
         usage_error = true;
@@ -414,23 +420,25 @@
     {
       if (get_int_arg (args(0), hist_beg))
         {
-          hist_beg--;
+          if (hist_beg < 0)
+            hist_beg += (hist_count + 1);
+          else
+            hist_beg--;
           hist_end = hist_beg;
         }
       else
         usage_error = true;
     }
 
-  if (hist_beg < 0 || hist_end < 0 || hist_beg > hist_count
-      || hist_end > hist_count)
+  if (usage_error)
     {
-      error ("%s: history specification out of range", warn_for);
+      usage ("%s [first] [last]", warn_for);
       return retval;
     }
 
-  if (usage_error)
+  if (hist_beg > hist_count || hist_end > hist_count)
     {
-      usage ("%s [first] [last]", warn_for);
+      error ("%s: history specification out of range", warn_for);
       return retval;
     }
 
@@ -484,9 +492,7 @@
   // Call up our favorite editor on the file of commands.
 
   std::string cmd = VEDITOR;
-  cmd.append (" \"");
-  cmd.append (name);
-  cmd.append ("\"");
+  cmd.append (" \"" + name + "\"");
 
   // Ignore interrupts while we are off editing commands.  Should we
   // maybe avoid using system()?
@@ -512,7 +518,7 @@
   std::fstream file (name.c_str (), std::ios::in);
 
   char *line;
-  int first = 1;
+  //int first = 1;
   while ((line = edit_history_readline (file)) != 0)
     {
       // Skip blank lines.
@@ -523,6 +529,9 @@
           continue;
         }
 
+      // FIXME: Don't delete this block of code until memory
+      //        leak in edit_history has been plugged and
+      //        it is clear that this code can be removed.
       // Command 'edit history' has already been removed in
       // mk_tmp_hist_file ()
       //if (first)
@@ -561,8 +570,7 @@
   if (name.empty ())
     return;
 
-  // Turn on command echo so the output from this will make better
-  // sense.
+  // Turn on command echo so the output from this will make better sense.
 
   unwind_protect frame;
 
@@ -598,19 +606,23 @@
 
 DEFUN (edit_history, args, ,
   "-*- texinfo -*-\n\
-@deftypefn {Command} {} edit_history [@var{first}] [@var{last}]\n\
-If invoked with no arguments, @code{edit_history} allows you to edit the\n\
-history list using the editor named by the variable @w{@env{EDITOR}}.  The\n\
-commands to be edited are first copied to a temporary file.  When you\n\
-exit the editor, Octave executes the commands that remain in the file.\n\
-It is often more convenient to use @code{edit_history} to define functions\n\
-rather than attempting to enter them directly on the command line.\n\
-By default, the block of commands is executed as soon as you exit the\n\
-editor.  To avoid executing any commands, simply delete all the lines\n\
-from the buffer before exiting the editor.\n\
+@deftypefn  {Command} {} edit_history\n\
+@deftypefnx {Command} {} edit_history @var{first}\n\
+@deftypefnx {Command} {} edit_history @var{first} @var{last}\n\
+Edit the history list using the editor named by the variable\n\
+@w{@env{EDITOR}}.\n\
 \n\
-The @code{edit_history} command takes two optional arguments specifying\n\
-the history numbers of first and last commands to edit.  For example,\n\
+The commands to be edited are first copied to a temporary file.  When you\n\
+exit the editor, Octave executes the commands that remain in the file.  It\n\
+is often more convenient to use @code{edit_history} to define functions\n\
+rather than attempting to enter them directly on the command line.  By\n\
+default, the block of commands is executed as soon as you exit the editor.  \n\
+To avoid executing any commands, simply delete all the lines from the buffer\n\
+before exiting the editor.\n\
+\n\
+When invoked with no arguments, edit the previously executed command.\n\
+Otherwise, @code{edit_history} accepts two optional arguments specifying\n\
+the history numbers of the first and last commands to edit.  For example,\n\
 the command\n\
 \n\
 @example\n\
@@ -626,10 +638,22 @@
 @end example\n\
 \n\
 @noindent\n\
-only extracts commands 13 through 169.  Specifying a larger number for\n\
-the first command than the last command reverses the list of commands\n\
-before placing them in the buffer to be edited.  If both arguments are\n\
-omitted, the previous command in the history list is used.\n\
+extracts only commands 13 through 169.  The command numbers\n\
+may also be negative where -1 refers to the most recently executed\n\
+command.\n\
+\n\
+Specifying a larger number for the first command than the last command\n\
+reverses the list of commands before placing them in the buffer to be\n\
+edited.\n\
+\n\
+The following are equivalent and run the most recently executed command.\n\
+\n\
+@example\n\
+@group\n\
+run_history\n\
+run_history -1\n\
+@end group\n\
+@end example\n\
 @seealso{run_history}\n\
 @end deftypefn")
 {
@@ -688,9 +712,47 @@
 
 DEFUN (run_history, args, ,
   "-*- texinfo -*-\n\
-@deftypefn {Command} {} run_history [@var{first}] [@var{last}]\n\
-Similar to @code{edit_history}, except that the editor is not invoked,\n\
-and the commands are simply executed as they appear in the history list.\n\
+@deftypefn  {Command} {} run_history\n\
+@deftypefnx {Command} {} run_history @var{first}\n\
+@deftypefnx {Command} {} run_history @var{first} @var{last}\n\
+Run commands from the history list.\n\
+\n\
+When invoked with no arguments, run the last executed command.\n\
+Otherwise, @code{run_history} accepts two arguments specifying\n\
+the history numbers of the first and last commands to run.\n\
+For example, the command\n\
+\n\
+@example\n\
+run_history 13\n\
+@end example\n\
+\n\
+@noindent\n\
+executes all commands from the 13th through the last in the history list.\n\
+The command\n\
+\n\
+@example\n\
+run_history 13 169\n\
+@end example\n\
+\n\
+@noindent\n\
+only executes commands 13 through 169.  The command numbers\n\
+may also be negative where -1 refers to the most recently executed\n\
+command.\n\
+\n\
+Specifying a larger number for the first command than the last command\n\
+reverses the list of commands before executing them.\n\
+For example:\n\
+\n\
+@example\n\
+@group\n\
+disp (1)\n\
+disp (2)\n\
+run_history -1 -2\n\
+ 2\n\
+ 1\n\
+@end group\n\
+@end example\n\
+\n\
 @seealso{edit_history}\n\
 @end deftypefn")
 {