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 @env{EDITOR}.\n\
592 The commands to be edited are first copied to a temporary file. When you\n\
593 exit the editor, Octave executes the commands that remain in the file. It\n\
594 is often more convenient to use @code{edit_history} to define functions\n\
595 rather than attempting to enter them directly on the command line.\n\
596 The block of commands is executed as soon as you exit the editor.\n\
597 To avoid executing any commands, simply delete all the lines from the buffer\n\
598 before leaving the editor.\n\
600 When invoked with no arguments, edit the previously executed command;\n\
601 With one argument, edit the specified command @var{cmd_number};\n\
602 With two arguments, edit the list of commands between @var{first} and\n\
603 @var{last}. Command number specifiers may also be negative where -1\n\
604 refers to the most recently executed command.\n\
605 The following are equivalent and edit the most recently executed command.\n\
614 When using ranges, specifying a larger number for the first command than the\n\
615 last command reverses the list of commands before they are placed in the\n\
616 buffer to be edited.\n\
617 @seealso{run_history, history}\n\
627 DEFUN (history, args, nargout,
629 @deftypefn {Command} {} history\n\
630 @deftypefnx {Command} {} history @var{opt1} @dots{}\n\
631 @deftypefnx {Built-in Function} {@var{h} =} history ()\n\
632 @deftypefnx {Built-in Function} {@var{h} =} history (@var{opt1}, @dots{})\n\
633 If invoked with no arguments, @code{history} displays a list of commands\n\
634 that you have executed.\n\
636 Valid options are:\n\
641 Display only the most recent @var{n} lines of history.\n\
644 Clear the history list.\n\
647 Don't number the displayed lines of history. This is useful for cutting\n\
648 and pasting commands using the X Window System.\n\
650 @item -r @var{file}\n\
651 Read the file @var{file}, appending its contents to the current\n\
652 history list. If the name is omitted, use the default history file\n\
653 (normally @file{~/.octave_hist}).\n\
655 @item -w @var{file}\n\
656 Write the current history to the file @var{file}. If the name is\n\
657 omitted, use the default history file (normally @file{~/.octave_hist}).\n\
660 For example, to display the five most recent commands that you have\n\
661 typed without displaying line numbers, use the command\n\
662 @kbd{history -q 5}.\n\
664 If invoked with a single output argument, the history will be saved to that\n\
665 argument as a cell string and will not be output to screen.\n\
666 @seealso{edit_history, run_history}\n\
674 retval =
Cell (hlist);
679 DEFUN (run_history, args, ,
681 @deftypefn {Command} {} run_history\n\
682 @deftypefnx {Command} {} run_history @var{cmd_number}\n\
683 @deftypefnx {Command} {} run_history @var{first} @var{last}\n\
684 Run commands from the history list.\n\
686 When invoked with no arguments, run the previously executed command;\n\
688 With one argument, run the specified command @var{cmd_number};\n\
690 With two arguments, run the list of commands between @var{first} and\n\
691 @var{last}. Command number specifiers may also be negative where -1\n\
692 refers to the most recently executed command. For example, the command\n\
703 executes the most recent command again.\n\
707 run_history 13 169\n\
711 executes commands 13 through 169.\n\
713 Specifying a larger number for the first command than the last command\n\
714 reverses the list of commands before executing them.\n\
728 @seealso{edit_history, history}\n\
738 DEFUN (history_control, args, nargout,
740 @deftypefn {Built-in Function} {@var{val} =} history_control ()\n\
741 @deftypefnx {Built-in Function} {@var{old_val} =} history_control (@var{new_val})\n\
742 Query or set the internal variable that specifies how commands are saved\n\
743 to the history list.\n\
745 The default value is an empty character string, but may be overridden by the\n\
746 environment variable @w{@env{OCTAVE_HISTCONTROL}}.\n\
748 The value of @code{history_control} is a colon-separated list of values\n\
749 controlling how commands are saved on the history list. If the list\n\
750 of values includes @code{ignorespace}, lines which begin with a space\n\
751 character are not saved in the history list. A value of @code{ignoredups}\n\
752 causes lines matching the previous history entry to not be saved.\n\
753 A value of @code{ignoreboth} is shorthand for @code{ignorespace} and\n\
754 @code{ignoredups}. A value of @code{erasedups} causes all previous lines\n\
755 matching the current line to be removed from the history list before that\n\
756 line is saved. Any value not in the above list is ignored. If\n\
757 @code{history_control} is the empty string, all commands are saved on\n\
758 the history list, subject to the value of @code{history_save}.\n\
759 @seealso{history_file, history_size, history_timestamp_format_string, history_save}\n\
766 std::string tmp = old_history_control;
770 if (tmp != old_history_control)
776 DEFUN (history_size, args, nargout,
778 @deftypefn {Built-in Function} {@var{val} =} history_size ()\n\
779 @deftypefnx {Built-in Function} {@var{old_val} =} history_size (@var{new_val})\n\
780 Query or set the internal variable that specifies how many entries\n\
781 to store in the history file.\n\
783 The default value is @code{1000}, but may be overridden by the environment\n\
784 variable @w{@env{OCTAVE_HISTSIZE}}.\n\
785 @seealso{history_file, history_timestamp_format_string, history_save}\n\
792 int tmp = old_history_size;
798 if (tmp != old_history_size)
804 DEFUN (history_file, args, nargout,
806 @deftypefn {Built-in Function} {@var{val} =} history_file ()\n\
807 @deftypefnx {Built-in Function} {@var{old_val} =} history_file (@var{new_val})\n\
808 Query or set the internal variable that specifies the name of the\n\
809 file used to store command history.\n\
811 The default value is @file{~/.octave_hist}, but may be overridden by the\n\
812 environment variable @w{@env{OCTAVE_HISTFILE}}.\n\
813 @seealso{history_size, history_save, history_timestamp_format_string}\n\
820 std::string tmp = old_history_file;
824 if (tmp != old_history_file)
830 DEFUN (history_timestamp_format_string, args, nargout,
832 @deftypefn {Built-in Function} {@var{val} =} history_timestamp_format_string ()\n\
833 @deftypefnx {Built-in Function} {@var{old_val} =} history_timestamp_format_string (@var{new_val})\n\
834 @deftypefnx {Built-in Function} {} history_timestamp_format_string (@var{new_val}, \"local\")\n\
835 Query or set the internal variable that specifies the format string\n\
836 for the comment line that is written to the history file when Octave\n\
839 The format string is passed to @code{strftime}. The default value is\n\
842 \"# Octave VERSION, %a %b %d %H:%M:%S %Y %Z <USER@@HOST>\"\n\
845 When called from inside a function with the @qcode{\"local\"} option, the\n\
846 variable is changed locally for the function and any subroutines it calls.\n\
847 The original variable value is restored when exiting the function.\n\
848 @seealso{strftime, history_file, history_size, history_save}\n\
854 DEFUN (history_save, args, nargout,
856 @deftypefn {Built-in Function} {@var{val} =} history_save ()\n\
857 @deftypefnx {Built-in Function} {@var{old_val} =} history_save (@var{new_val})\n\
858 @deftypefnx {Built-in Function} {} history_save (@var{new_val}, \"local\")\n\
859 Query or set the internal variable that controls whether commands entered\n\
860 on the command line are saved in the history file.\n\
862 When called from inside a function with the @qcode{\"local\"} option, the\n\
863 variable is changed locally for the function and any subroutines it calls.\n\
864 The original variable value is restored when exiting the function.\n\
865 @seealso{history_control, history_file, history_size, history_timestamp_format_string}\n\
872 bool tmp = old_history_save;
876 if (tmp != old_history_save)
static bool ignoring_entries(void)
octave_interrupt_handler octave_set_interrupt_handler(const volatile octave_interrupt_handler &h, bool restart_syscalls)
void gripe_wrong_type_arg(const char *name, const char *s, bool is_error)
void source_file(const std::string &file_name, const std::string &context, bool verbose, bool require_file, const std::string &warn_for)
std::string strftime(const std::string &fmt) const
octave_idx_type length(void) const
bool is_numeric_type(void) const
static std::string Vhistory_timestamp_format_string
int int_value(bool req_int=false, bool frc_str_conv=false) const
static string_vector do_history(const octave_value_list &args, int nargout)
static void unlink_cleanup(const char *file)
static std::string file(void)
#define DEFUN(name, args_name, nargout_name, doc)
static void set_history(const string_vector &hist)
void error(const char *fmt,...)
#define SET_INTERNAL_VARIABLE(NM)
static std::string default_history_timestamp_format(void)
static void append(const std::string &=std::string())
static void read(bool=true)
octave_value set_internal_variable(bool &var, const octave_value_list &args, int nargout, const char *nm)
static void set_file(const std::string &)
void usage(const char *fmt,...)
static string_vector list(int=-1, bool=false)
static void do_run_history(const octave_value_list &args)
static void edit_history_add_hist(const std::string &line)
static bool add(const std::string &)
void initialize_history(bool read_history_file)
static void clear_history(void)
static std::string concat(const std::string &, const std::string &)
void add_fcn(void(*fcn)(void))
void octave_history_write_timestamp(void)
std::string string_value(bool force=false) const
static std::string mk_tmp_hist_file(const octave_value_list &args, bool insert_curr, const char *warn_for)
bool is_string(void) const
static std::string getenv(const std::string &name)
#define panic_impossible()
size_t size(T const (&)[z])
octave_interrupt_handler octave_ignore_interrupts(void)
charNDArray max(char d, const charNDArray &m)
octave_idx_type length(void) const
Number of elements in the array.
static void write(const std::string &=std::string())
bool input_from_tmp_history_file
static bool get_int_arg(const octave_value &arg, int &val)
static std::string get_user_name(void)
static std::string histcontrol(void)
static void initialize(bool, const std::string &, int, const std::string &)
static int default_history_size(void)
static std::string default_history_file(void)
static char * edit_history_readline(std::fstream &stream)
static void set_size(int)
std::string octave_tempnam(const std::string &dir, const std::string &pfx)
static void append_history(const std::string &hist_entry)
static std::string get_host_name(void)
static void do_edit_history(const octave_value_list &args)
static void process_histcontrol(const std::string &)
static void ignore_entries(bool=true)
static void read_range(int=-1, int=-1, bool=true)
static bool read_history_file
static std::string get_home_directory(void)