37 #if defined (HAVE_CONFIG_H)
85 char *
line =
new char [line_len];
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';
130 edit_history_add_hist (
const std::string&
line)
134 std::string tmp =
line;
136 int len = tmp.length ();
138 if (
len > 0 && tmp[
len-1] ==
'\n')
139 tmp.resize (
len - 1);
162 ok = sscanf (tmp.c_str (),
"%d", &val) == 1;
174 bool insert_curr,
const char *warn_for)
178 int hist_count = hlist.
numel () - 1;
193 int hist_beg = hist_count;
194 int hist_end = hist_count;
196 bool reverse =
false;
200 int nargin = args.
length ();
204 if (! get_int_arg (args(0), hist_beg)
205 || ! get_int_arg (args(1), hist_end))
206 error (
"%s: arguments must be integers", warn_for);
209 hist_beg += (hist_count + 1);
213 hist_end += (hist_count + 1);
217 else if (nargin == 1)
219 if (! get_int_arg (args(0), hist_beg))
220 error (
"%s: argument must be an integer", warn_for);
223 hist_beg += (hist_count + 1);
230 if (hist_beg > hist_count || hist_end > hist_count)
231 error (
"%s: history specification out of range", warn_for);
233 if (hist_end < hist_beg)
235 std::swap (hist_end, hist_beg);
244 error (
"%s: couldn't open temporary file '%s'", warn_for,
249 for (
int i = hist_end; i >= hist_beg; i--)
250 file << hlist[i] <<
"\n";
254 for (
int i = hist_beg; i <= hist_end; i++)
255 file << hlist[i] <<
"\n";
264 : m_interpreter (interp), m_input_from_tmp_file (false),
265 m_timestamp_format_string (default_timestamp_format ())
273 sys::env::getenv (
"OCTAVE_HISTCONTROL"));
285 std::string timestamp = now.strftime (m_timestamp_format_string);
287 if (! timestamp.empty ())
303 "input_from_tmp_file");
311 "timestamp_format_string");
324 bool numbered_output = nargout == 0;
327 ([] (
const std::string& old_filename)
334 int nargin = args.
length ();
359 if (option ==
"-r" || option ==
"-w" || option ==
"-a"
365 = args(++i).xstring_value (
"history: filename must be a string for %s option",
377 else if (option ==
"-w")
381 else if (option ==
"-r")
388 else if (option ==
"-n")
400 else if (option ==
"-c")
405 else if (option ==
"-q")
406 numbered_output =
false;
407 else if (option ==
"--")
418 if (sscanf (option.c_str (),
"%d", &tmp) == 1)
428 if (option.length () > 0 && option[0] ==
'-')
429 error (
"history: unrecognized option '%s'", option.c_str ());
431 error (
"history: bad non-numeric arg '%s'", option.c_str ());
438 int len = hlist.numel ();
452 std::string name = mk_tmp_hist_file (args,
false,
"edit_history");
460 std::string cmd =
env.editor ();
461 cmd.append (R
"( ")" + name + '"');
475 if (status != EXIT_SUCCESS)
476 error (
"edit_history: text editor command failed");
485 while ((
line = edit_history_readline (file)) !=
nullptr)
495 edit_history_add_hist (
line);
502 int(*unlink_fptr)(
const std::string&) =
sys::unlink;
516 std::string name = mk_tmp_hist_file (args,
false,
"run_history");
521 int(*unlink_fptr)(
const std::string&) =
sys::unlink;
533 history_system::default_file ()
537 std::string env_file = sys::env::getenv (
"OCTAVE_HISTFILE");
539 if (! env_file.empty ())
547 std::string user_data_dir = sys::env::get_user_data_directory ();
552 file = sys::env::make_absolute (
"history", hist_dir);
560 history_system::default_size ()
564 std::string env_size = sys::env::getenv (
"OCTAVE_HISTSIZE");
566 if (! env_size.empty ())
570 if (sscanf (env_size.c_str (),
"%d", &val) == 1)
571 size = (val > 0 ? val : 0);
578 history_system::default_timestamp_format ()
582 + sys::env::get_user_name ()
584 + sys::env::get_host_name ()
634 DEFMETHOD (history, interp, args, nargout,
684 return nargout > 0 ?
ovl (
Cell (hlist)) :
ovl ();
751 DEFUN (history_control, args, nargout,
780 std::string tmp = old_history_control;
784 if (tmp != old_history_control)
790 DEFUN (history_size, args, nargout,
806 int tmp = old_history_size;
812 if (tmp != old_history_size)
818 DEFUN (history_file, args, nargout,
862 std::string tmp = old_history_file;
866 if (tmp != old_history_file)
872 DEFMETHOD (history_timestamp_format_string, interp, args, nargout,
907 DEFUN (history_save, args, nargout,
926 bool tmp = old_history_save;
930 if (tmp != old_history_save)
936 OCTAVE_END_NAMESPACE(
octave)
charNDArray max(char d, const charNDArray &m)
static bool ignoring_entries()
static void read(bool=true)
static void initialize(bool, const std::string &, int, const std::string &)
static void append(const std::string &="")
static std::string file()
static void set_file(const std::string &)
static std::string histcontrol()
static bool add(const std::string &)
static void set_size(int)
static void ignore_entries(bool=true)
static void process_histcontrol(const std::string &)
static void read_range(int=-1, int=-1, bool=true)
static string_vector list(int=-1, bool=false)
static void write(const std::string &="")
Provides threadsafe access to octave.
void append_history(const std::string &hist_entry)
void do_edit_history(const octave_value_list &args=octave_value_list())
bool input_from_tmp_file() 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)
std::string timestamp_format_string() const
string_vector do_history(const octave_value_list &args=octave_value_list(), int nargout=0)
environment & get_environment()
event_manager & get_event_manager()
octave_idx_type length() const
int int_value(bool req_int=false, bool frc_str_conv=false) const
std::string string_value(bool force=false) const
octave_idx_type numel() const
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
#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()
event_manager & __get_event_manager__()
int system(const std::string &cmd_str)
std::fstream fstream(const std::string &filename, const std::ios::openmode mode)
std::ofstream ofstream(const std::string &filename, const std::ios::openmode mode)
std::string tempnam(const std::string &dir, const std::string &pfx)
int unlink(const std::string &name)
octave_value set_internal_variable(bool &var, const octave_value_list &args, int nargout, const char *nm)
octave_value_list ovl(const OV_Args &... args)
Construct an octave_value_list with less typing.
void source_file(const std::string &file_name, const std::string &context="", bool verbose=false, bool require_file=true)
interrupt_handler ignore_interrupts()
interrupt_handler set_interrupt_handler(const volatile interrupt_handler &h, bool restart_syscalls)