37#if defined (HAVE_CONFIG_H)
85 char *line =
new char [line_len];
88 while (stream.get (c))
90 if (lindex + 2 >= line_len)
92 char *tmp_line =
new char [line_len += 128];
93 strcpy (tmp_line, line);
100 line[lindex++] =
'\n';
101 line[lindex++] =
'\0';
114 if (lindex + 2 >= line_len)
116 char *tmp_line =
new char [lindex+3];
117 strcpy (tmp_line, line);
124 line[lindex++] =
'\n';
125 line[lindex++] =
'\0';
134 std::string tmp = line;
136 int len = tmp.length ();
138 if (
len > 0 && tmp[
len-1] ==
'\n')
139 tmp.resize (
len - 1);
143 if (command_history::add (tmp))
163 ok = sscanf (tmp.c_str (),
"%d", &val) == 1;
175 bool insert_curr,
const char *warn_for)
179 int hist_count = hlist.
numel () - 1;
187 command_history::remove (hist_count);
194 int hist_beg = hist_count;
195 int hist_end = hist_count;
197 bool reverse =
false;
201 int nargin = args.
length ();
207 error (
"%s: arguments must be integers", warn_for);
210 hist_beg += (hist_count + 1);
214 hist_end += (hist_count + 1);
218 else if (nargin == 1)
221 error (
"%s: argument must be an integer", warn_for);
224 hist_beg += (hist_count + 1);
231 if (hist_beg > hist_count || hist_end > hist_count)
232 error (
"%s: history specification out of range", warn_for);
234 if (hist_end < hist_beg)
236 std::swap (hist_end, hist_beg);
245 error (
"%s: couldn't open temporary file '%s'", warn_for,
250 for (
int i = hist_end; i >= hist_beg; i--)
251 file << hlist[i] <<
"\n";
255 for (
int i = hist_beg; i <= hist_end; i++)
256 file << hlist[i] <<
"\n";
265 : m_interpreter (interp), m_input_from_tmp_file (false),
266 m_timestamp_format_string (default_timestamp_format ())
273 sys::env::getenv (
"OCTAVE_HISTCONTROL"));
286 if (! timestamp.empty ())
288 if (command_history::add (timestamp))
302 "input_from_tmp_file");
310 "timestamp_format_string");
322 bool numbered_output = nargout == 0;
324 unwind_action restore_history_filename
325 ([] (
const std::string& old_filename)
327 command_history::set_file (old_filename);
328 }, command_history::file ());
332 int nargin = args.
length ();
357 if (option ==
"-r" || option ==
"-w" || option ==
"-a"
363 = args(++i).xstring_value (
"history: filename must be a string for %s option",
366 command_history::set_file (fname);
373 command_history::append ();
375 else if (option ==
"-w")
377 command_history::write ();
379 else if (option ==
"-r")
382 command_history::read ();
386 else if (option ==
"-n")
389 command_history::read_range ();
398 else if (option ==
"-c")
400 command_history::clear ();
403 else if (option ==
"-q")
404 numbered_output =
false;
405 else if (option ==
"--")
416 if (sscanf (option.c_str (),
"%d", &tmp) == 1)
426 if (option.length () > 0 && option[0] ==
'-')
427 error (
"history: unrecognized option '%s'", option.c_str ());
429 error (
"history: bad non-numeric arg '%s'", option.c_str ());
434 hlist = command_history::list (limit, numbered_output);
436 int len = hlist.numel ();
457 std::string cmd = env.
editor ();
458 cmd.append (R
"( ")" + name + '"');
472 if (status != EXIT_SUCCESS)
473 error (
"edit_history: text editor command failed");
500 unwind_action unlink_action (unlink_fptr,
name);
518 unwind_action unlink_action (unlink_fptr,
name);
532 std::string env_file = sys::env::getenv (
"OCTAVE_HISTFILE");
534 if (! env_file.empty ())
542 std::string user_data_dir = sys::env::get_user_data_directory ();
558 std::string env_size = sys::env::getenv (
"OCTAVE_HISTSIZE");
560 if (! env_size.empty ())
564 if (sscanf (env_size.c_str (),
"%d", &val) == 1)
565 size = (val > 0 ? val : 0);
575 + sys::env::get_user_name ()
577 + sys::env::get_host_name ()
618 if (args.length () > 2)
628DEFMETHOD (history, interp, args, nargout,
678 return nargout > 0 ?
ovl (
Cell (hlist)) :
ovl ();
737 if (args.length () > 2)
745DEFUN (history_control, args, nargout,
772 std::string old_history_control = command_history::histcontrol ();
774 std::string tmp = old_history_control;
778 if (tmp != old_history_control)
779 command_history::process_histcontrol (tmp);
784DEFUN (history_size, args, nargout,
798 int old_history_size = command_history::size ();
800 int tmp = old_history_size;
806 if (tmp != old_history_size)
807 command_history::set_size (tmp);
812DEFUN (history_file, args, nargout,
854 std::string old_history_file = command_history::file ();
856 std::string tmp = old_history_file;
860 if (tmp != old_history_file)
861 command_history::set_file (tmp);
866DEFMETHOD (history_timestamp_format_string, interp, args, nargout,
901DEFUN (history_save, args, nargout,
918 bool old_history_save = ! command_history::ignoring_entries ();
920 bool tmp = old_history_save;
924 if (tmp != old_history_save)
925 command_history::ignore_entries (! tmp);
charNDArray max(char d, const charNDArray &m)
octave_value editor(const octave_value_list &args, int nargout)
Provides threadsafe access to octave.
void append_history(const std::string &hist_entry)
OCTINTERP_API void set_history(void)
void do_edit_history(const octave_value_list &args=octave_value_list())
bool input_from_tmp_file(void) const
history_system(interpreter &interp)
void do_run_history(const octave_value_list &args=octave_value_list())
octave_value timestamp_format_string(const octave_value_list &args, int nargout)
void initialize(bool read_history_file=false)
bool m_input_from_tmp_file
static std::string default_timestamp_format(void)
std::string timestamp_format_string(void) const
interpreter & m_interpreter
std::string m_timestamp_format_string
static int default_size(void)
static std::string default_file(void)
string_vector do_history(const octave_value_list &args=octave_value_list(), int nargout=0)
void write_timestamp(void)
event_manager & get_event_manager(void)
environment & get_environment(void)
octave_idx_type length(void) const
int int_value(bool req_int=false, bool frc_str_conv=false) const
bool isnumeric(void) const
bool is_string(void) const
std::string string_value(bool force=false) const
octave_idx_type numel(void) const
OCTINTERP_API void print_usage(void)
#define DEFMETHOD(name, interp_name, args_name, nargout_name, doc)
Macro to define a builtin method.
#define DEFUN(name, args_name, nargout_name, doc)
Macro to define a builtin function.
void error(const char *fmt,...)
#define panic_impossible()
void err_wrong_type_arg(const char *name, const char *s)
std::string dir_sep_str(void)
std::fstream fstream(const std::string &filename, const std::ios::openmode mode)
std::string tempnam(const std::string &dir, const std::string &pfx)
std::ofstream ofstream(const std::string &filename, const std::ios::openmode mode)
int unlink(const std::string &name)
int system(const std::string &cmd_str)
event_manager & __get_event_manager__(const std::string &who)
static void edit_history_add_hist(const std::string &line)
static std::string mk_tmp_hist_file(const octave_value_list &args, bool insert_curr, const char *warn_for)
static OCTAVE_NAMESPACE_BEGIN char * edit_history_readline(std::fstream &stream)
static bool get_int_arg(const octave_value &arg, int &val)
void source_file(const std::string &file_name, const std::string &context, bool verbose, bool require_file)
static void initialize(void)
octave_value_list ovl(const OV_Args &... args)
Construct an octave_value_list with less typing.
interrupt_handler set_interrupt_handler(const volatile interrupt_handler &h, bool restart_syscalls)
interrupt_handler ignore_interrupts(void)
static string_vector make_absolute(const string_vector &sv)
octave_value set_internal_variable(bool &var, const octave_value_list &args, int nargout, const char *nm)