45 #include <sys/types.h>
82 if (! env_file.empty ())
99 if (! env_size.empty ())
103 if (sscanf (env_size.c_str (),
"%d", &val) == 1)
104 size = val > 0 ? val : 0;
114 std::string (
"# Octave " OCTAVE_VERSION ", %a %b %d %H:%M:%S %Y %Z <")
135 bool numbered_output = nargout == 0;
143 int nargin = args.
length ();
169 if (option ==
"-r" || option ==
"-w" || option ==
"-a"
174 if (args(i+1).is_string ())
178 error (
"history: expecting file name for %s option",
190 else if (option ==
"-w")
194 else if (option ==
"-r")
201 else if (option ==
"-n")
213 else if (option ==
"-c")
218 else if (option ==
"-q")
219 numbered_output =
false;
220 else if (option ==
"--")
231 if (sscanf (option.c_str (),
"%d", &tmp) == 1)
241 if (option.length () > 0 && option[0] ==
'-')
242 error (
"history: unrecognized option '%s'", option.c_str ());
244 error (
"history: bad non-numeric arg '%s'", option.c_str ());
253 int len = hlist.
length ();
274 char *
line =
new char [line_len];
277 while (stream.get (c))
279 if (lindex + 2 >= line_len)
281 char *tmp_line =
new char [line_len += 128];
282 strcpy (tmp_line, line);
289 line[lindex++] =
'\n';
290 line[lindex++] =
'\0';
303 if (lindex + 2 >= line_len)
305 char *tmp_line =
new char [lindex+3];
306 strcpy (tmp_line, line);
313 line[lindex++] =
'\n';
314 line[lindex++] =
'\0';
323 std::string tmp = line;
325 int len = tmp.length ();
327 if (len > 0 && tmp[len-1] ==
'\n')
328 tmp.resize (len - 1);
345 ok = sscanf (tmp.c_str (),
"%d", &val) == 1;
357 bool insert_curr,
const char *warn_for)
363 int hist_count = hlist.
length () - 1;
378 int hist_beg = hist_count;
379 int hist_end = hist_count;
381 bool reverse =
false;
385 int nargin = args.
length ();
387 bool usage_error =
false;
394 hist_beg += (hist_count + 1);
398 hist_end += (hist_count + 1);
405 else if (nargin == 1)
410 hist_beg += (hist_count + 1);
421 usage (
"%s [first] [last]", warn_for);
425 if (hist_beg > hist_count || hist_end > hist_count)
427 error (
"%s: history specification out of range", warn_for);
431 if (hist_end < hist_beg)
433 std::swap (hist_end, hist_beg);
439 std::fstream file (name.c_str (), std::ios::out);
443 error (
"%s: couldn't open temporary file '%s'", warn_for,
450 for (
int i = hist_end; i >= hist_beg; i--)
451 file << hlist[i] <<
"\n";
455 for (
int i = hist_beg; i <= hist_end; i++)
456 file << hlist[i] <<
"\n";
467 gnulib::unlink (file);
481 cmd.append (
" \"" + name +
"\"");
489 int status = system (cmd.c_str ());
495 if (status != EXIT_SUCCESS)
497 error (
"edit_history: text editor command failed");
504 std::fstream file (name.c_str (), std::ios::in);
580 if (! timestamp.empty ())
585 DEFUN (edit_history, args, ,
587 @deftypefn {Command} {} edit_history\n\
588 @deftypefnx {Command} {} edit_history @var{cmd_number}\n\
589 @deftypefnx {Command} {} edit_history @var{first} @var{last}\n\
590 Edit the history list using the editor named by the variable\n\
593 The commands to be edited are first copied to a temporary file. When you\n\
594 exit the editor, Octave executes the commands that remain in the file. It\n\
595 is often more convenient to use @code{edit_history} to define functions\n\
596 rather than attempting to enter them directly on the command line.\n\
597 The block of commands is executed as soon as you exit the editor.\n\
598 To avoid executing any commands, simply delete all the lines from the buffer\n\
599 before leaving the editor.\n\
601 When invoked with no arguments, edit the previously executed command;\n\
602 With one argument, edit the specified command @var{cmd_number};\n\
603 With two arguments, edit the list of commands between @var{first} and\n\
604 @var{last}. Command number specifiers may also be negative where -1\n\
605 refers to the most recently executed command.\n\
606 The following are equivalent and edit the most recently executed command.\n\
615 When using ranges, specifying a larger number for the first command than the\n\
616 last command reverses the list of commands before they are placed in the\n\
617 buffer to be edited.\n\
618 @seealso{run_history}\n\
628 DEFUN (history, args, nargout,
630 @deftypefn {Command} {} history\n\
631 @deftypefnx {Command} {} history @var{opt1} @dots{}\n\
632 @deftypefnx {Built-in Function} {@var{h} =} history ()\n\
633 @deftypefnx {Built-in Function} {@var{h} =} history (@var{opt1}, @dots{})\n\
634 If invoked with no arguments, @code{history} displays a list of commands\n\
635 that you have executed. Valid options are:\n\
640 Display only the most recent @var{n} lines of history.\n\
643 Clear the history list.\n\
646 Don't number the displayed lines of history. This is useful for cutting\n\
647 and pasting commands using the X Window System.\n\
649 @item -r @var{file}\n\
650 Read the file @var{file}, appending its contents to the current\n\
651 history list. If the name is omitted, use the default history file\n\
652 (normally @file{~/.octave_hist}).\n\
654 @item -w @var{file}\n\
655 Write the current history to the file @var{file}. If the name is\n\
656 omitted, use the default history file (normally @file{~/.octave_hist}).\n\
659 For example, to display the five most recent commands that you have\n\
660 typed without displaying line numbers, use the command\n\
661 @kbd{history -q 5}.\n\
663 If invoked with a single output argument, the history will be saved to that\n\
664 argument as a cell string and will not be output to screen.\n\
672 retval =
Cell (hlist);
677 DEFUN (run_history, args, ,
679 @deftypefn {Command} {} run_history\n\
680 @deftypefnx {Command} {} run_history @var{cmd_number}\n\
681 @deftypefnx {Command} {} run_history @var{first} @var{last}\n\
682 Run commands from the history list.\n\
684 When invoked with no arguments, run the previously executed command;\n\
685 With one argument, run the specified command @var{cmd_number};\n\
686 With two arguments, run the list of commands between @var{first} and\n\
687 @var{last}. Command number specifiers may also be negative where -1\n\
688 refers to the most recently executed command.\n\
689 For example, the command\n\
700 executes the most recent command again.\n\
704 run_history 13 169\n\
708 executes commands 13 through 169.\n\
710 Specifying a larger number for the first command than the last command\n\
711 reverses the list of commands before executing them.\n\
725 @seealso{edit_history}\n\
735 DEFUN (history_control, args, nargout,
737 @deftypefn {Built-in Function} {@var{val} =} history_control ()\n\
738 @deftypefnx {Built-in Function} {@var{old_val} =} history_control (@var{new_val})\n\
739 Query or set the internal variable that specifies how commands are saved\n\
740 to the history list. The default value is an empty character string,\n\
741 but may be overridden by the environment variable\n\
742 @w{@env{OCTAVE_HISTCONTROL}}.\n\
744 The value of @code{history_control} is a colon-separated list of values\n\
745 controlling how commands are saved on the history list. If the list\n\
746 of values includes @code{ignorespace}, lines which begin with a space\n\
747 character are not saved in the history list. A value of @code{ignoredups}\n\
748 causes lines matching the previous history entry to not be saved.\n\
749 A value of @code{ignoreboth} is shorthand for @code{ignorespace} and\n\
750 @code{ignoredups}. A value of @code{erasedups} causes all previous lines\n\
751 matching the current line to be removed from the history list before that\n\
752 line is saved. Any value not in the above list is ignored. If\n\
753 @code{history_control} is the empty string, all commands are saved on\n\
754 the history list, subject to the value of @code{history_save}.\n\
755 @seealso{history_file, history_size, history_timestamp_format_string, history_save}\n\
762 std::string tmp = old_history_control;
766 if (tmp != old_history_control)
772 DEFUN (history_size, args, nargout,
774 @deftypefn {Built-in Function} {@var{val} =} history_size ()\n\
775 @deftypefnx {Built-in Function} {@var{old_val} =} history_size (@var{new_val})\n\
776 Query or set the internal variable that specifies how many entries\n\
777 to store in the history file. The default value is @code{1000},\n\
778 but may be overridden by the environment variable @w{@env{OCTAVE_HISTSIZE}}.\n\
779 @seealso{history_file, history_timestamp_format_string, history_save}\n\
786 int tmp = old_history_size;
792 if (tmp != old_history_size)
798 DEFUN (history_file, args, nargout,
800 @deftypefn {Built-in Function} {@var{val} =} history_file ()\n\
801 @deftypefnx {Built-in Function} {@var{old_val} =} history_file (@var{new_val})\n\
802 Query or set the internal variable that specifies the name of the\n\
803 file used to store command history. The default value is\n\
804 @file{~/.octave_hist}, but may be overridden by the environment\n\
805 variable @w{@env{OCTAVE_HISTFILE}}.\n\
806 @seealso{history_size, history_save, history_timestamp_format_string}\n\
813 std::string tmp = old_history_file;
817 if (tmp != old_history_file)
823 DEFUN (history_timestamp_format_string, args, nargout,
825 @deftypefn {Built-in Function} {@var{val} =} history_timestamp_format_string ()\n\
826 @deftypefnx {Built-in Function} {@var{old_val} =} history_timestamp_format_string (@var{new_val})\n\
827 @deftypefnx {Built-in Function} {} history_timestamp_format_string (@var{new_val}, \"local\")\n\
828 Query or set the internal variable that specifies the format string\n\
829 for the comment line that is written to the history file when Octave\n\
830 exits. The format string is passed to @code{strftime}. The default\n\
834 \"# Octave VERSION, %a %b %d %H:%M:%S %Y %Z <USER@@HOST>\"\n\
837 When called from inside a function with the @qcode{\"local\"} option, the\n\
838 variable is changed locally for the function and any subroutines it calls. \n\
839 The original variable value is restored when exiting the function.\n\
840 @seealso{strftime, history_file, history_size, history_save}\n\
846 DEFUN (history_save, args, nargout,
848 @deftypefn {Built-in Function} {@var{val} =} history_save ()\n\
849 @deftypefnx {Built-in Function} {@var{old_val} =} history_save (@var{new_val})\n\
850 @deftypefnx {Built-in Function} {} history_save (@var{new_val}, \"local\")\n\
851 Query or set the internal variable that controls whether commands entered\n\
852 on the command line are saved in the history file.\n\
854 When called from inside a function with the @qcode{\"local\"} option, the\n\
855 variable is changed locally for the function and any subroutines it calls. \n\
856 The original variable value is restored when exiting the function.\n\
857 @seealso{history_control, history_file, history_size, history_timestamp_format_string}\n\
864 bool tmp = old_history_save;
868 if (tmp != old_history_save)