GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
octave-link.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2013-2018 John W. Eaton
4 Copyright (C) 2011-2018 Jacob Dawid
5 Copyright (C) 2011-2018 John P. Swensen
6 
7 This file is part of Octave.
8 
9 Octave is free software: you can redistribute it and/or modify it
10 under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13 
14 Octave is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with Octave; see the file COPYING. If not, see
21 <https://www.gnu.org/licenses/>.
22 
23 */
24 
25 #if defined (HAVE_CONFIG_H)
26 # include "config.h"
27 #endif
28 
29 #include "builtin-defun-decls.h"
30 #include "cmd-edit.h"
31 #include "defun.h"
32 #include "interpreter.h"
33 #include "interpreter-private.h"
34 #include "octave-link.h"
35 #include "oct-env.h"
36 #include "oct-mutex.h"
37 #include "ovl.h"
38 #include "pager.h"
39 #include "symscope.h"
40 
41 static int
43 {
48 
49  return 0;
50 }
51 
53 
55  : event_queue_mutex (new octave::mutex ()), gui_event_queue (),
56  debugging (false), link_enabled (true)
57 {
59 }
60 
62 {
63  delete event_queue_mutex;
64 }
65 
66 void
68 {
69  if (enabled ())
70  {
71  octave::symbol_table& symtab
72  = octave::__get_symbol_table__ ("octave_link::set_workspace");
73 
74  octave::symbol_scope scope = symtab.current_scope ();
75 
77  instance->debugging, scope, true);
78  }
79 }
80 
81 // OBJ should be an object of a class that is derived from the base
82 // class octave_link, or 0 to disconnect the link. It is the
83 // responsibility of the caller to delete obj.
84 
85 void
87 {
88  if (obj && instance)
89  error ("octave_link is already linked!");
90 
91  instance = obj;
92 }
93 
94 void
96 { }
97 
98 void
100 {
102 
103  gui_event_queue.run ();
104 
106 }
107 
108 void
110 {
112 
114 
116 }
117 
118 DEFUN (__octave_link_enabled__, , ,
119  doc: /* -*- texinfo -*-
120 @deftypefn {} {} __octave_link_enabled__ ()
121 Undocumented internal function.
122 @end deftypefn */)
123 {
124  return ovl (octave_link::enabled ());
125 }
126 
127 DEFUN (__octave_link_edit_file__, args, ,
128  doc: /* -*- texinfo -*-
129 @deftypefn {} {} __octave_link_edit_file__ (@var{file})
130 Undocumented internal function.
131 @end deftypefn */)
132 {
134 
135  if (args.length () == 1)
136  {
137  std::string file = args(0).xstring_value ("first argument must be filename");
138 
140 
142  }
143  else if (args.length () == 2)
144  {
145  std::string file = args(0).xstring_value ("first argument must be filename");
146 
148 
150  }
151 
152  return retval;
153 }
154 
155 DEFUN (__octave_link_message_dialog__, args, ,
156  doc: /* -*- texinfo -*-
157 @deftypefn {} {} __octave_link_message_dialog__ (@var{dlg}, @var{msg}, @var{title})
158 Undocumented internal function.
159 @end deftypefn */)
160 {
162 
163  if (args.length () == 3)
164  {
165  std::string dlg = args(0).xstring_value ("invalid arguments");
166  std::string msg = args(1).xstring_value ("invalid arguments");
167  std::string title = args(2).xstring_value ("invalid arguments");
168 
170 
171  retval = octave_link::message_dialog (dlg, msg, title);
172  }
173 
174  return retval;
175 }
176 
177 DEFUN (__octave_link_question_dialog__, args, ,
178  doc: /* -*- texinfo -*-
179 @deftypefn {} {} __octave_link_question_dialog__ (@var{msg}, @var{title}, @var{btn1}, @var{btn2}, @var{btn3}, @var{default})
180 Undocumented internal function.
181 @end deftypefn */)
182 {
184 
185  if (args.length () == 6)
186  {
187  std::string msg = args(0).xstring_value ("invalid arguments");
188  std::string title = args(1).xstring_value ("invalid arguments");
189  std::string btn1 = args(2).xstring_value ("invalid arguments");
190  std::string btn2 = args(3).xstring_value ("invalid arguments");
191  std::string btn3 = args(4).xstring_value ("invalid arguments");
192  std::string btndef = args(5).xstring_value ("invalid arguments");
193 
195 
196  retval = octave_link::question_dialog (msg, title, btn1, btn2, btn3,
197  btndef);
198  }
199 
200  return retval;
201 }
202 
203 DEFUN (__octave_link_file_dialog__, args, ,
204  doc: /* -*- texinfo -*-
205 @deftypefn {} {} __octave_link_file_dialog__ (@var{filterlist}, @var{title}, @var{filename}, @var{size} @var{multiselect}, @var{pathname})
206 Undocumented internal function.
207 @end deftypefn */)
208 {
209  if (args.length () != 6)
210  return ovl ();
211 
213 
214  const Array<std::string> flist = args(0).cellstr_value ();
215  std::string title = args(1).string_value ();
216  std::string filename = args(2).string_value ();
217  Matrix pos = args(3).matrix_value ();
218  std::string multi_on = args(4).string_value (); // on, off, create
219  std::string pathname = args(5).string_value ();
220 
221  octave_idx_type nel;
222  octave_link::filter_list filter_lst;
223 
224  for (octave_idx_type i = 0; i < flist.rows (); i++)
225  filter_lst.push_back (std::make_pair (flist(i,0),
226  (flist.columns () > 1
227  ? flist(i,1) : "")));
228 
230 
231  std::list<std::string> items_lst
232  = octave_link::file_dialog (filter_lst, title, filename, pathname,
233  multi_on);
234 
235  nel = items_lst.size ();
236 
237  // If 3, then retval is filename, directory, and selected index.
238  if (nel <= 3)
239  {
240  if (items_lst.front ().empty ())
241  retval = ovl (octave_value (0.), octave_value (0.), octave_value (0.));
242  else
243  {
244  int idx = 0;
245  for (auto& str : items_lst)
246  {
247  if (idx != 2)
248  retval(idx++) = str;
249  else
250  retval(idx++) = atoi (str.c_str ());
251  }
252  }
253  }
254  else
255  {
256  // Multiple files.
257  nel -= 2;
258  Cell items (dim_vector (1, nel));
259 
260  std::list<std::string>::iterator it = items_lst.begin ();
261 
262  for (int idx = 0; idx < nel; idx++, it++)
263  items.xelem (idx) = *it;
264 
265  retval = ovl (items, *it++, atoi (it->c_str ()));
266  }
267 
268  return retval;
269 }
270 
271 DEFUN (__octave_link_list_dialog__, args, ,
272  doc: /* -*- texinfo -*-
273 @deftypefn {} {} __octave_link_list_dialog__ (@var{list}, @var{mode}, @var{size}, @var{intial}, @var{name}, @var{prompt}, @var{ok_string}, @var{cancel_string})
274 Undocumented internal function.
275 @end deftypefn */)
276 {
277  if (args.length () != 8)
278  return ovl ();
279 
280  Cell list = args(0).cell_value ();
281  const Array<std::string> tlist = list.cellstr_value ();
282  octave_idx_type nel = tlist.numel ();
283  std::list<std::string> list_lst;
284  for (octave_idx_type i = 0; i < nel; i++)
285  list_lst.push_back (tlist(i));
286 
287  std::string mode = args(1).string_value ();
288 
289  Matrix size_matrix = args(2).matrix_value ();
290  int width = size_matrix(0);
291  int height = size_matrix(1);
292 
293  Matrix initial_matrix = args(3).matrix_value ();
294  nel = initial_matrix.numel ();
295  std::list<int> initial_lst;
296  for (octave_idx_type i = 0; i < nel; i++)
297  initial_lst.push_back (initial_matrix(i));
298 
299  std::string name = args(4).string_value ();
300  list = args(5).cell_value ();
301  const Array<std::string> plist = list.cellstr_value ();
302  nel = plist.numel ();
303  std::list<std::string> prompt_lst;
304  for (octave_idx_type i = 0; i < nel; i++)
305  prompt_lst.push_back (plist(i));
306  std::string ok_string = args(6).string_value ();
307  std::string cancel_string = args(7).string_value ();
308 
310 
311  std::pair<std::list<int>, int> result
312  = octave_link::list_dialog (list_lst, mode, width, height,
313  initial_lst, name, prompt_lst,
314  ok_string, cancel_string);
315 
316  std::list<int> items_lst = result.first;
317  nel = items_lst.size ();
318  Matrix items (dim_vector (1, nel));
319  octave_idx_type i = 0;
320  for (const auto& int_el : items_lst)
321  items.xelem(i++) = int_el;
322 
323  return ovl (items, result.second);
324 }
325 
326 DEFUN (__octave_link_input_dialog__, args, ,
327  doc: /* -*- texinfo -*-
328 @deftypefn {} {} __octave_link_input_dialog__ (@var{prompt}, @var{title}, @var{rowscols}, @var{defaults})
329 Undocumented internal function.
330 @end deftypefn */)
331 {
332  if (args.length () != 4)
333  return ovl ();
334 
335  Cell prompt = args(0).cell_value ();
337  octave_idx_type nel = tmp.numel ();
338  std::list<std::string> prompt_lst;
339  for (octave_idx_type i = 0; i < nel; i++)
340  prompt_lst.push_back (tmp(i));
341 
342  std::string title = args(1).string_value ();
343 
344  Matrix rc = args(2).matrix_value ();
345  nel = rc.rows ();
346  std::list<float> nr;
347  std::list<float> nc;
348  for (octave_idx_type i = 0; i < nel; i++)
349  {
350  nr.push_back (rc(i,0));
351  nc.push_back (rc(i,1));
352  }
353 
354  Cell defaults = args(3).cell_value ();
355  tmp = defaults.cellstr_value ();
356  nel = tmp.numel ();
357  std::list<std::string> defaults_lst;
358  for (octave_idx_type i = 0; i < nel; i++)
359  defaults_lst.push_back (tmp(i));
360 
362 
363  std::list<std::string> items_lst
364  = octave_link::input_dialog (prompt_lst, title, nr, nc,
365  defaults_lst);
366 
367  nel = items_lst.size ();
368  Cell items (dim_vector (nel, 1));
369  octave_idx_type i = 0;
370  for (const auto& str_el : items_lst)
371  items.xelem(i++) = str_el;
372 
373  return ovl (items);
374 }
375 
376 DEFUN (__octave_link_show_preferences__, , ,
377  doc: /* -*- texinfo -*-
378 @deftypefn {} {} __octave_link_show_preferences__ ()
379 Undocumented internal function.
380 @end deftypefn */)
381 {
383 }
384 
385 DEFMETHOD (openvar, interp, args, ,
386  doc: /* -*- texinfo -*-
387 @deftypefn {} {} openvar (@var{name})
388 Open the variable @var{name} in the graphical Variable Editor.
389 @end deftypefn */)
390 {
391  if (args.length () != 1)
392  print_usage ();
393 
394  if (! args(0).is_string ())
395  error ("openvar: NAME must be a string");
396 
397  std::string name = args(0).string_value ();
398 
399  if (! (Fisguirunning ())(0).is_true ())
400  warning ("openvar: GUI is not running, can't start Variable Editor");
401  else
402  {
403  octave::symbol_scope scope = interp.require_current_scope ("openvar");
404 
405  octave_value val = scope.varval (name);
406 
407  if (val.is_undefined ())
408  error ("openvar: '%s' is not a variable", name.c_str ());
409 
411  }
412 
413  return ovl ();
414 }
415 
416 /*
417 %!error openvar ()
418 %!error openvar ("a", "b")
419 %!error <NAME must be a string> openvar (1:10)
420 */
421 
422 DEFUN (__octave_link_show_doc__, args, ,
423  doc: /* -*- texinfo -*-
424 @deftypefn {} {} __octave_link_show_doc__ (@var{filename})
425 Undocumented internal function.
426 @end deftypefn */)
427 {
429 
430  if (args.length () >= 1)
431  file = args(0).string_value();
432 
433  return ovl (octave_link::show_doc (file));
434 }
435 
436 DEFUN (__octave_link_register_doc__, args, ,
437  doc: /* -*- texinfo -*-
438 @deftypefn {} {} __octave_link_register_doc__ (@var{filename})
439 Undocumented internal function.
440 @end deftypefn */)
441 {
443 
444  if (args.length () >= 1)
445  file = args(0).string_value();
446 
448 }
449 
450 DEFUN (__octave_link_unregister_doc__, args, ,
451  doc: /* -*- texinfo -*-
452 @deftypefn {} {} __octave_link_unregister_doc__ (@var{filename})
453 Undocumented internal function.
454 @end deftypefn */)
455 {
457 
458  if (args.length () >= 1)
459  file = args(0).string_value();
460 
462 }
octave_idx_type rows(void) const
Definition: Array.h:404
Definition: Cell.h:37
For example cd octave end example noindent changes the current working directory to file
Definition: dirfns.cc:124
#define DEFMETHOD(name, interp_name, args_name, nargout_name, doc)
Macro to define a builtin method.
Definition: defun.h:135
The value of lines which begin with a space character are not saved in the history list A value of all commands are saved on the history list
Definition: oct-hist.cc:734
OCTINTERP_API void print_usage(void)
Definition: defun.cc:54
identity matrix If supplied two scalar respectively For allows like xample val
Definition: data.cc:4986
Return the CPU time used by your Octave session The first output is the total time spent executing your process and is equal to the sum of second and third which are the number of CPU seconds spent executing in user mode and the number of CPU seconds spent executing in system mode
Definition: data.cc:6348
symbol_scope current_scope(void)
Definition: symtab.h:77
void unlock(void)
Definition: oct-mutex.h:92
void flush_stdout(void)
Definition: pager.cc:464
#define DEFUN(name, args_name, nargout_name, doc)
Macro to define a builtin function.
Definition: defun.h:53
void error(const char *fmt,...)
Definition: error.cc:578
std::string filename
Definition: urlwrite.cc:121
octave_idx_type columns(void) const
Definition: Array.h:413
bool at_top_level(void)
Definition: symtab.h:140
symbol_table & __get_symbol_table__(const std::string &who)
nd deftypefn *std::string name
Definition: sysdep.cc:647
std::string str
Definition: hash.cc:118
double tmp
Definition: data.cc:6252
is false
Definition: cellfun.cc:400
octave_value retval
Definition: data.cc:6246
bool is_true(const std::string &s)
Definition: dMatrix.h:36
octave_value varval(const std::string &name) const
Definition: symscope.h:727
With real return the complex result
Definition: data.cc:3260
T & xelem(octave_idx_type n)
Definition: Array.h:458
void warning(const char *fmt,...)
Definition: error.cc:801
return octave_value(v1.char_array_value() . concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string()) ? '\'' :'"'))
Array< std::string > cellstr_value(void) const
Definition: Cell.cc:126
OCTAVE_EXPORT octave_value_list isa nd deftypefn *return ovl(args(0).isinteger())
static void add_event_hook(event_hook_fcn f)
Definition: cmd-edit.cc:1554
OCTAVE_EXPORT octave_value_list Fisguirunning(const octave_value_list &args, int)
Definition: octave.cc:426
for i
Definition: data.cc:5264
octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:366
void lock(void)
Definition: oct-mutex.h:87
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:87
If this string is the system will ring the terminal sometimes it is useful to be able to print the original representation of the string
Definition: utils.cc:888