28 #if defined (HAVE_CONFIG_H)
52 #include "builtin-defun-decls.h"
100 quoting_filename (
const std::string&
text,
int,
char quote)
113 find_indexed_expression (
const std::string&
text)
117 int pos =
line.length () -
text.length ();
123 while (pos >= 0 && (
line[pos] ==
')' ||
line[pos] ==
'}'))
125 if (
line[pos] ==
')')
130 while (curly_count + paren_count > 0 && --pos >= 0)
132 if (
line[pos] ==
')')
134 else if (
line[pos] ==
'(')
136 else if (
line[pos] ==
'}')
138 else if (
line[pos] ==
'{')
142 while (--pos >= 0 &&
line[pos] ==
' ')
146 while (pos >= 0 && (isalnum (
line[pos]) ||
line[pos] ==
'_'))
150 return (
line.substr (pos, last + 1 - pos));
152 return std::string ();
156 generate_struct_completions (
const std::string&
text,
157 std::string& prefix, std::string& hint)
161 size_t pos =
text.rfind (
'.');
164 if (pos != std::string::npos)
166 if (pos ==
text.length ())
169 hint =
text.substr (pos+1);
171 prefix =
text.substr (0, pos);
176 prefix = find_indexed_expression (
text);
179 std::string base_name = prefix;
181 pos = base_name.find_first_of (
"{(. ");
183 if (pos != std::string::npos)
184 base_name = base_name.substr (0, pos);
189 if (interp.is_variable (base_name))
193 error_system& es = interp.get_error_system ();
195 unwind_protect frame;
198 es.discard_warning_messages ());
200 es.discard_warning_messages (
true);
205 = interp.eval_string (prefix,
true, parse_status);
215 interp.recover_from_exception ();
230 looks_like_struct (
const std::string&
text,
char prev_char)
233 && (
text !=
"." || prev_char ==
')' || prev_char ==
'}')
235 &&
text.find (
"..") == std::string::npos
236 &&
text.rfind (
'.') != std::string::npos);
244 generate_possible_completions (
const std::string&
text, std::string& prefix,
245 std::string& hint,
bool& deemed_struct)
252 deemed_struct = looks_like_struct (
text, prev_char);
255 names = generate_struct_completions (
text, prefix, hint);
267 is_completing_dirfns (
void)
269 static std::string dirfns_commands[] = {
"cd",
"isfile",
"isfolder",
"ls"};
270 static const size_t dirfns_commands_length = 4;
276 for (
size_t i = 0; i < dirfns_commands_length; i++)
278 int index =
line.find (dirfns_commands[i] +
' ');
291 generate_completion (
const std::string&
text,
int state)
295 static std::string prefix;
296 static std::string hint;
298 static size_t hint_len = 0;
300 static int list_index = 0;
301 static int name_list_len = 0;
302 static int name_list_total_len = 0;
306 static int matches = 0;
319 bool deemed_struct =
false;
321 if (is_completing_dirfns ())
324 name_list = generate_possible_completions (
text, prefix, hint,
327 name_list_len = name_list.
numel ();
336 name_list.
append (file_name_list);
340 name_list_total_len = name_list.
numel ();
342 hint_len = hint.length ();
346 for (
int i = 0; i < name_list_len; i++)
347 if (hint == name_list[i].substr (0, hint_len))
351 if (name_list_total_len > 0 && matches > 0)
353 while (list_index < name_list_total_len)
355 std::string
name = name_list[list_index];
359 if (hint ==
name.substr (0, hint_len))
363 if (list_index <= name_list_len && ! prefix.empty ())
364 retval = (prefix ==
"." ?
"" : prefix) +
'.' +
name;
371 if (matches == 1 && looks_like_struct (
retval, prev_char))
380 input_system& input_sys
384 (input_sys.completion_append_char ());
395 static int internal_input_event_hook_fcn (
void)
399 input_system& input_sys
402 input_sys.run_input_event_hooks ();
412 : m_interpreter (interp), m_PS1 (R
"(octave:\#> )"), m_PS2 ("> "),
413 m_completion_append_char (
' '), m_gud_mode (
false),
414 m_mfile_encoding (
"system"), m_auto_repeat_debug_command (
true),
415 m_last_debugging_command (
"\n"), m_input_event_hook_functions (),
416 m_initialized (
false)
439 static const char *s =
"\t\n !\"\'*+-/:;<=>(){}[\\]^`~";
477 "completion_append_char");
499 "__mfile_encoding__");
522 if (codec ==
reinterpret_cast<void *
> (-1))
526 error (
"__mfile_encoding__: conversion from encoding '%s' "
527 "not supported", encoding.c_str ());
529 error (
"__mfile_encoding__: error %d opening encoding '%s'.",
530 errno, encoding.c_str ());
539 feval (
"__event_manager_gui_preference__",
550 "auto_repeat_debug_command");
555 std::string prompt_string = prompt +
"(yes or no) ";
563 if (input_buf ==
"yes")
565 else if (input_buf ==
"no")
568 message (
nullptr,
"Please answer yes or no.");
612 int read_as_string = 0;
617 std::string prompt = args(0).xstring_value (
"input: unrecognized argument");
629 if (input_buf.empty ())
630 error (
"input: reading user-input failed!");
632 size_t len = input_buf.length ();
636 if (input_buf[
len - 1] !=
'\n')
642 if (input_buf.length () == 1 && input_buf[0] ==
'\n')
649 int parse_status = 0;
656 if (! tw.in_debug_repl () &&
retval.empty ())
704 if (! eof &&
retval.empty ())
727 if (! tw.in_debug_repl ())
730 evmgr.pre_input_event ();
732 evmgr.set_workspace ();
735 bool history_skip_auto_repeated_debugging_command =
false;
747 retval = input_sys.interactive_input (prompt, eof);
752 &&
retval.find_first_not_of (
" \t\n\r") != std::string::npos)
758 if (tw.in_debug_repl ())
759 input_sys.last_debugging_command (
retval);
761 input_sys.last_debugging_command (
"\n");
763 else if (tw.in_debug_repl () && input_sys.auto_repeat_debug_command ())
765 retval = input_sys.last_debugging_command ();
766 history_skip_auto_repeated_debugging_command =
true;
771 if (! history_skip_auto_repeated_debugging_command)
774 evmgr.append_history (
retval);
789 evmgr.post_input_event ();
795 terminal_reader :
public base_reader
799 terminal_reader (interpreter& interp)
800 : base_reader (interp), m_eof (
false), m_input_queue ()
803 std::string get_input (
const std::string& prompt,
bool& eof);
805 std::string input_source (
void)
const {
return s_in_src; }
807 bool input_from_terminal (
void)
const {
return true; }
812 std::queue<std::string> m_input_queue;
814 static const std::string s_in_src;
818 file_reader :
public base_reader
822 file_reader (interpreter& interp, FILE *f_arg)
823 : base_reader (interp), m_file (f_arg) { }
825 std::string get_input (
const std::string& prompt,
bool& eof);
827 std::string input_source (
void)
const {
return s_in_src; }
829 bool input_from_file (
void)
const {
return true; }
835 static const std::string s_in_src;
839 eval_string_reader :
public base_reader
843 eval_string_reader (interpreter& interp,
const std::string& str)
844 : base_reader (interp), m_eval_string (str)
847 std::string get_input (
const std::string& prompt,
bool& eof);
849 std::string input_source (
void)
const {
return s_in_src; }
851 bool input_from_eval_string (
void)
const {
return true; }
855 std::string m_eval_string;
857 static const std::string s_in_src;
861 : m_rep (
new terminal_reader (interp))
865 : m_rep (
new file_reader (interp, file))
869 : m_rep (
new eval_string_reader (interp, str))
874 const std::string terminal_reader::s_in_src (
"terminal");
884 terminal_reader::get_input (
const std::string& prompt,
bool& eof)
890 if (m_input_queue.empty ())
892 std::string
input = octave_gets (prompt, m_eof);
916 size_t end =
input.find (
'\n', beg);
918 if (end == std::string::npos)
920 m_input_queue.push (
input.substr (beg));
925 m_input_queue.push (
input.substr (beg, end-beg+1));
931 std::string
retval = m_input_queue.front ();
932 m_input_queue.pop ();
934 if (m_input_queue.empty ())
940 const std::string file_reader::s_in_src (
"file");
943 file_reader::get_input (
const std::string& ,
bool& eof)
951 input_system& input_sys = m_interpreter.get_input_system ();
953 std::string mfile_encoding = input_sys.mfile_encoding ();
955 std::string encoding;
956 if (mfile_encoding.compare (
"system") == 0)
960 std::transform (encoding.begin (), encoding.end (), encoding.begin (),
964 encoding = mfile_encoding;
966 if (encoding.compare (
"utf-8") == 0)
969 if (src_str.compare (0, 3,
"\xef\xbb\xbf") == 0)
970 src_str.erase (0, 3);
976 "Invalid UTF-8 byte sequences have been replaced.");
981 const char *src = src_str.c_str ();
982 size_t srclen = src_str.length ();
991 error (
"file_reader::get_input: "
992 "converting from codepage '%s' to UTF-8: %s",
993 encoding.c_str (), std::strerror (errno));
995 unwind_protect frame;
996 frame.add_fcn (::
free,
static_cast<void *
> (utf8_str));
998 src_str = std::string (
reinterpret_cast<char *
> (utf8_str),
length);
1004 const std::string eval_string_reader::s_in_src (
"eval_string");
1007 eval_string_reader::get_input (
const std::string& ,
bool& eof)
1067 int nargin = args.length ();
1069 if (nargin < 1 || nargin > 2)
1091 int nargin = args.length ();
1101 prompt = args(0).xstring_value (
"yes_or_no: PROMPT must be a string");
1124 int nargin = args.length ();
1134 = args(0).xstring_value (
"keyboard: PROMPT must be a string");
1144 DEFUN (completion_matches, args, nargout,
1155 if (args.length () != 1)
1160 std::string hint = args(0).string_value ();
1170 std::string cmd = octave::generate_completion (hint, k);
1191 if (! list.empty ())
1202 int len = list.numel ();
1204 for (
int i = 0; i <
len; i++)
1228 DEFUN (readline_read_init_file, args, ,
1241 int nargin = args.length ();
1250 std::string file = args(0).string_value ();
1258 DEFUN (readline_re_read_init_file, args, ,
1268 if (args.length () != 0)
1276 DEFMETHOD (add_input_event_hook, interp, args, ,
1296 int nargin = args.length ();
1298 if (nargin < 1 || nargin > 2)
1304 user_data = args(1);
1312 return ovl (hook_fcn.id ());
1315 DEFMETHOD (remove_input_event_hook, interp, args, ,
1325 int nargin = args.length ();
1327 if (nargin < 1 || nargin > 2)
1330 std::string hook_fcn_id = args(0).xstring_value (
"remove_input_event_hook: argument not valid as a hook function name or id");
1332 bool warn = (nargin < 2);
1337 warning (
"remove_input_event_hook: %s not found in list",
1338 hook_fcn_id.c_str ());
1384 return input_sys.
PS1 (args, nargout);
1408 return input_sys.
PS2 (args, nargout);
1411 DEFMETHOD (completion_append_char, interp, args, nargout,
1431 DEFMETHOD (__request_drawnow__, , args, ,
1438 int nargin = args.length ();
1451 DEFMETHOD (__gud_mode__, interp, args, nargout,
1459 return input_sys.
gud_mode (args, nargout);
1462 DEFMETHOD (__mfile_encoding__, interp, args, nargout,
1473 DEFMETHOD (auto_repeat_debug_command, interp, args, nargout,
1499 octave_yes_or_no (
const std::string& prompt)
1508 remove_input_event_hook_functions (
void)
1521 get_input_from_stdin (
void)
charNDArray max(char d, const charNDArray &m)
map_type::iterator iterator
void run(const octave_value_list &initial_args=octave_value_list())
iterator find(const std::string &id)
void insert(const std::string &id, const hook_function &f)
std::string id(void) const
interpreter & m_interpreter
std::string octave_gets(const std::string &prompt, bool &eof)
static const std::string s_in_src
static void set_basic_quote_characters(const std::string &s)
static void re_read_init_file(void)
static void set_completion_function(completion_fcn f)
static std::string get_line_buffer(void)
static void set_basic_word_break_characters(const std::string &s)
static void set_name(const std::string &n)
static void set_completer_quote_characters(const std::string &s)
static string_vector generate_filename_completions(const std::string &text)
static void set_input_stream(FILE *f)
static void read_init_file(const std::string &file="")
static void set_completer_word_break_characters(const std::string &s)
static void set_completion_append_character(char c)
static FILE * get_input_stream(void)
static void force_default_editor(void)
static void add_event_hook(event_hook_fcn f)
static std::string readline(const std::string &prompt)
static void set_quoting_function(quoting_fcn f)
static void set_filename_quote_characters(const std::string &s)
static char get_prev_char(int)
static bool add(const std::string &)
void set_discard_warning_messages(bool flag)
void exit_debugger_event(void)
output_system & get_output_system(void)
input_system & get_input_system(void)
octave_value_list eval_string(const std::string &eval_str, bool silent, int &parse_status, int nargout)
load_path & get_load_path(void)
void handle_exception(const execution_exception &e)
bool interactive(void) const
event_manager & get_event_manager(void)
tree_evaluator & get_evaluator(void)
void keyboard(const std::string &prompt="keyboard> ")
octave_idx_type length(void) const
bool is_classdef_object(void) const
bool is_defined(void) const
bool isstruct(void) const
string_vector map_keys(void) const
string_vector & append(const std::string &s)
string_vector & sort(bool make_uniq=false)
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 warning(const char *fmt,...)
void warning_with_id(const char *id, const char *fmt,...)
void error(const char *fmt,...)
void message(const char *name, const char *fmt,...)
OCTAVE_EXPORT octave_value_list Fdrawnow(octave::interpreter &interp, const octave_value_list &args, int)
ColumnVector transform(const Matrix &m, double x, double y, double z)
iconv_t octave_iconv_open_wrapper(const char *tocode, const char *fromcode)
int octave_iconv_close_wrapper(iconv_t cd)
std::string octave_fgets(FILE *f)
const char * octave_locale_charset_wrapper(void)
OCTAVE_API unsigned int u8_validate(const std::string &who, std::string &in_string, const u8_fallback_type type=U8_REPLACEMENT_CHAR)
std::string dir_sep_chars(void)
string_vector make_name_list(void)
static uint32_t state[624]
int pipe_handler_error_count
interpreter & __get_interpreter__(const std::string &who)
input_system & __get_input_system__(const std::string &who)
octave_value_list feval(const char *name, const octave_value_list &args, int nargout)
Evaluate an Octave function (built-in or interpreted) and return the list of result values.
static int input(yyscan_t yyscanner)
octave_value::octave_value(const Array< char > &chm, char type) return retval
octave_value_list ovl(const OV_Args &... args)
Construct an octave_value_list with less typing.
static OCTAVE_NORETURN void eval_error(const char *msg, const dim_vector &x, const dim_vector &y)
uint8_t * octave_u8_conv_from_encoding(const char *fromcode, const char *src, size_t srclen, size_t *lengthp)
octave_value set_internal_variable(bool &var, const octave_value_list &args, int nargout, const char *nm)