GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
cmd-edit.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1996-2024 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if ! defined (octave_cmd_edit_h)
27 #define octave_cmd_edit_h 1
28 
29 #include "octave-config.h"
30 
31 #include <cstdio>
32 
33 #include <set>
34 #include <string>
35 
36 #include "str-vec.h"
37 
39 
40 class
43 {
44 protected:
45 
47  : m_command_number (1), m_rows (24), m_cols (80), m_interrupted (false),
48  m_interrupt_event_loop (false), m_initial_input ()
49  { }
50 
51 public:
52 
53  typedef int (*startup_hook_fcn) ();
54 
55  typedef int (*pre_input_hook_fcn) ();
56 
57  typedef int (*event_hook_fcn) ();
58 
59  typedef std::string (*completion_fcn) (const std::string&, int);
60 
61  typedef char *(*completion_hook_fcn) ();
62 
63  typedef std::string (*quoting_fcn) (const std::string&, int, char);
64 
65  typedef std::string (*dequoting_fcn) (const std::string&, int);
66 
67  typedef int (*char_is_quoted_fcn) (const std::string&, int);
68 
69  typedef void (*user_accept_line_fcn) (const std::string&);
70 
71  OCTAVE_DISABLE_COPY_MOVE (command_editor)
72 
73  virtual ~command_editor () = default;
74 
75  static void set_name (const std::string& n);
76 
77  static std::string readline (const std::string& prompt);
78 
79  static std::string readline (const std::string& prompt, bool& eof);
80 
81  static void set_input_stream (FILE *f);
82 
83  static FILE * get_input_stream ();
84 
85  static void set_output_stream (FILE *f);
86 
87  static FILE * get_output_stream ();
88 
89  static void redisplay ();
90 
91  static int terminal_rows ();
92 
93  static int terminal_cols ();
94 
95  static void clear_screen (bool skip_redisplay = false);
96 
97  static void resize_terminal ();
98 
99  static void set_screen_size (int ht, int wd);
100 
101  static std::string decode_prompt_string (const std::string& s);
102 
103  static void restore_terminal_state ();
104 
105  static void blink_matching_paren (bool flag);
106 
107  static bool erase_empty_line (bool flag);
108 
109  static void set_basic_word_break_characters (const std::string& s);
110 
111  static void set_completer_word_break_characters (const std::string& s);
112 
113  static void set_basic_quote_characters (const std::string& s);
114 
115  static void set_filename_quote_characters (const std::string& s);
116 
117  static void set_completer_quote_characters (const std::string& s);
118 
119  static void set_completion_append_character (char c);
120 
121  static void set_completion_function (completion_fcn f);
122 
123  static void set_quoting_function (quoting_fcn f);
124 
125  static void set_dequoting_function (dequoting_fcn f);
126 
127  static void set_char_is_quoted_function (char_is_quoted_fcn f);
128 
129  static void set_user_accept_line_function (user_accept_line_fcn f);
130 
131  static completion_fcn get_completion_function ();
132 
133  static quoting_fcn get_quoting_function ();
134 
135  static dequoting_fcn get_dequoting_function ();
136 
137  static char_is_quoted_fcn get_char_is_quoted_function ();
138 
139  static user_accept_line_fcn get_user_accept_line_function ();
140 
141  static string_vector
142  generate_filename_completions (const std::string& text);
143 
144  static std::string get_line_buffer ();
145 
146  static std::string get_current_line ();
147 
148  static char get_prev_char (int);
149 
150  static void replace_line (const std::string& text, bool clear_undo = true);
151 
152  static void kill_full_line ();
153 
154  static void insert_text (const std::string& text);
155 
156  static void newline ();
157 
158  static void accept_line ();
159 
160  static bool undo ();
161 
162  static void clear_undo_list ();
163 
164  static void add_startup_hook (startup_hook_fcn f);
165 
166  static void remove_startup_hook (startup_hook_fcn f);
167 
168  static void add_pre_input_hook (pre_input_hook_fcn f);
169 
170  static void remove_pre_input_hook (pre_input_hook_fcn f);
171 
172  static void add_event_hook (event_hook_fcn f);
173 
174  static void remove_event_hook (event_hook_fcn f);
175 
176  static void run_event_hooks ();
177 
178  static void read_init_file (const std::string& file = "");
179 
180  static void re_read_init_file ();
181 
182  static bool filename_completion_desired (bool);
183 
184  static bool filename_quoting_desired (bool);
185 
186  static bool prefer_env_winsize (bool);
187 
188  static bool interrupt (bool = true);
189 
190  static void interrupt_event_loop (bool flag = true);
191 
192  static bool event_loop_interrupted ();
193 
194  static int current_command_number ();
195 
196  static void reset_current_command_number (int n);
197 
198  static void increment_current_command_number ();
199 
200  static void force_default_editor ();
201 
202  static void set_initial_input (const std::string& text);
203 
204  static int insert_initial_input ();
205 
206 private:
207 
208  static bool instance_ok ();
209 
210  static void make_command_editor ();
211 
212  static int startup_handler ();
213 
214  static int pre_input_handler ();
215 
216  static int event_handler ();
217 
218  static void cleanup_instance ()
219  {
220  delete s_instance;
221  s_instance = nullptr;
222  }
223 
224  static void handle_interrupt_signal ();
225 
226  //--------
227 
228  static command_editor *s_instance; // the real thing.
229 
230  static std::set<startup_hook_fcn> s_startup_hook_set;
231  static std::set<pre_input_hook_fcn> s_pre_input_hook_set;
232  static std::set<event_hook_fcn> s_event_hook_set;
233 
234 protected:
235 
236  // To use something other than the GNU readline library, derive a new
237  // class from command_editor, overload these functions as
238  // necessary, and make instance point to the new class.
239 
240  virtual void do_set_name (const std::string&) { }
241 
242  std::string do_readline (const std::string& prompt)
243  {
244  bool eof;
245 
246  return do_readline (prompt, eof);
247  }
248 
249  virtual std::string do_readline (const std::string&, bool&) = 0;
250 
251  virtual void do_set_input_stream (FILE *) = 0;
252 
253  virtual FILE * do_get_input_stream () = 0;
254 
255  virtual void do_set_output_stream (FILE *) = 0;
256 
257  virtual FILE * do_get_output_stream () = 0;
258 
259  virtual void do_redisplay () { }
260 
261  virtual int do_terminal_rows () { return m_rows; }
262 
263  virtual int do_terminal_cols () { return m_cols; }
264 
265  virtual void do_clear_screen (bool) { }
266 
267  virtual void do_resize_terminal () { }
268 
269  virtual void do_set_screen_size (int ht, int wd)
270  {
271  m_rows = ht;
272  m_cols = wd;
273  }
274 
275  virtual std::string do_decode_prompt_string (const std::string&);
276 
277  virtual std::string newline_chars () { return "\n"; }
278 
279  virtual void do_restore_terminal_state () { }
280 
281  virtual void do_blink_matching_paren (bool) { }
282 
283  virtual bool do_erase_empty_line (bool) { return false; }
284 
285  virtual void do_set_basic_word_break_characters (const std::string&) { }
286 
287  virtual void do_set_completer_word_break_characters (const std::string&) { }
288 
289  virtual void do_set_completer_word_break_hook (completion_hook_fcn) { }
290 
291  virtual void do_set_basic_quote_characters (const std::string&) { }
292 
293  virtual void do_set_filename_quote_characters (const std::string&) { }
294 
295  virtual void do_set_completer_quote_characters (const std::string&) { }
296 
297  virtual void do_set_completion_append_character (char) { }
298 
299  virtual void do_set_completion_function (completion_fcn) { }
300 
301  virtual void do_set_quoting_function (quoting_fcn) { }
302 
303  virtual void do_set_dequoting_function (dequoting_fcn) { }
304 
305  virtual void do_set_char_is_quoted_function (char_is_quoted_fcn) { }
306 
307  virtual void do_set_user_accept_line_function (user_accept_line_fcn) { }
308 
309  virtual completion_fcn do_get_completion_function () const
310  { return nullptr; }
311 
312  virtual quoting_fcn do_get_quoting_function () const
313  { return nullptr; }
314 
315  virtual dequoting_fcn do_get_dequoting_function () const
316  { return nullptr; }
317 
318  virtual char_is_quoted_fcn do_get_char_is_quoted_function () const
319  { return nullptr; }
320 
321  virtual user_accept_line_fcn do_get_user_accept_line_function () const
322  { return nullptr; }
323 
324  virtual string_vector
325  do_generate_filename_completions (const std::string& text) = 0;
326 
327  virtual std::string do_get_line_buffer () const = 0;
328 
329  virtual std::string do_get_current_line () const = 0;
330 
331  virtual char do_get_prev_char (int) const = 0;
332 
333  virtual void
334  do_replace_line (const std::string& text, bool clear_undo) = 0;
335 
336  virtual void do_kill_full_line () = 0;
337 
338  virtual void do_insert_text (const std::string& text) = 0;
339 
340  virtual void do_newline () = 0;
341 
342  virtual void do_accept_line () = 0;
343 
344  virtual bool do_undo () { return false; }
345 
346  virtual void do_clear_undo_list () { }
347 
348  virtual void set_startup_hook (startup_hook_fcn) { }
349 
350  virtual void restore_startup_hook () { }
351 
352  virtual void set_pre_input_hook (pre_input_hook_fcn) { }
353 
354  virtual void restore_pre_input_hook () { }
355 
356  virtual void set_event_hook (event_hook_fcn) { }
357 
358  virtual void restore_event_hook () { }
359 
360  virtual void do_read_init_file (const std::string&) { }
361 
362  virtual void do_re_read_init_file () { }
363 
364  virtual bool do_filename_completion_desired (bool) { return false; }
365 
366  virtual bool do_filename_quoting_desired (bool) { return false; }
367 
368  virtual bool do_prefer_env_winsize (bool) { return false; }
369 
370  virtual void do_interrupt (bool) { }
371 
372  virtual void do_handle_interrupt_signal () { }
373 
374  void do_interrupt_event_loop (bool arg) { m_interrupt_event_loop = arg; }
375 
377  { return m_interrupt_event_loop; }
378 
379  int do_insert_initial_input ();
380 
381  int read_octal (const std::string& s);
382 
383  void error (int);
384 
385  void error (const std::string&);
386 
387  // The current command number.
389 
390  int m_rows;
391  int m_cols;
392 
394 
396 
397  std::string m_initial_input;
398 };
399 
400 OCTAVE_END_NAMESPACE(octave)
401 
402 #endif
virtual void do_resize_terminal()
Definition: cmd-edit.h:267
virtual string_vector do_generate_filename_completions(const std::string &text)=0
virtual FILE * do_get_input_stream()=0
virtual std::string newline_chars()
Definition: cmd-edit.h:277
virtual user_accept_line_fcn do_get_user_accept_line_function() const
Definition: cmd-edit.h:321
virtual std::string do_get_current_line() const =0
virtual void do_set_screen_size(int ht, int wd)
Definition: cmd-edit.h:269
virtual std::string do_get_line_buffer() const =0
virtual bool do_erase_empty_line(bool)
Definition: cmd-edit.h:283
virtual void do_redisplay()
Definition: cmd-edit.h:259
virtual void do_set_basic_quote_characters(const std::string &)
Definition: cmd-edit.h:291
std::string do_readline(const std::string &prompt)
Definition: cmd-edit.h:242
int m_command_number
Definition: cmd-edit.h:388
virtual void restore_event_hook()
Definition: cmd-edit.h:358
virtual void do_kill_full_line()=0
virtual void do_set_user_accept_line_function(user_accept_line_fcn)
Definition: cmd-edit.h:307
virtual completion_fcn do_get_completion_function() const
Definition: cmd-edit.h:309
virtual FILE * do_get_output_stream()=0
virtual void do_set_name(const std::string &)
Definition: cmd-edit.h:240
virtual char do_get_prev_char(int) const =0
virtual void do_set_filename_quote_characters(const std::string &)
Definition: cmd-edit.h:293
virtual void do_set_completer_word_break_characters(const std::string &)
Definition: cmd-edit.h:287
bool m_interrupted
Definition: cmd-edit.h:393
virtual void restore_startup_hook()
Definition: cmd-edit.h:350
virtual void do_set_input_stream(FILE *)=0
std::string m_initial_input
Definition: cmd-edit.h:397
virtual void do_read_init_file(const std::string &)
Definition: cmd-edit.h:360
virtual void do_set_completion_append_character(char)
Definition: cmd-edit.h:297
virtual dequoting_fcn do_get_dequoting_function() const
Definition: cmd-edit.h:315
virtual int do_terminal_cols()
Definition: cmd-edit.h:263
virtual bool do_filename_completion_desired(bool)
Definition: cmd-edit.h:364
virtual char_is_quoted_fcn do_get_char_is_quoted_function() const
Definition: cmd-edit.h:318
bool m_interrupt_event_loop
Definition: cmd-edit.h:395
virtual void do_replace_line(const std::string &text, bool clear_undo)=0
virtual void do_re_read_init_file()
Definition: cmd-edit.h:362
virtual void do_set_output_stream(FILE *)=0
void do_interrupt_event_loop(bool arg)
Definition: cmd-edit.h:374
virtual void do_set_completer_word_break_hook(completion_hook_fcn)
Definition: cmd-edit.h:289
virtual void do_set_quoting_function(quoting_fcn)
Definition: cmd-edit.h:301
virtual quoting_fcn do_get_quoting_function() const
Definition: cmd-edit.h:312
virtual void do_set_char_is_quoted_function(char_is_quoted_fcn)
Definition: cmd-edit.h:305
virtual int do_terminal_rows()
Definition: cmd-edit.h:261
virtual void do_interrupt(bool)
Definition: cmd-edit.h:370
bool do_event_loop_interrupted() const
Definition: cmd-edit.h:376
virtual void do_clear_screen(bool)
Definition: cmd-edit.h:265
virtual void do_restore_terminal_state()
Definition: cmd-edit.h:279
virtual void do_set_basic_word_break_characters(const std::string &)
Definition: cmd-edit.h:285
virtual void do_set_completion_function(completion_fcn)
Definition: cmd-edit.h:299
virtual void do_blink_matching_paren(bool)
Definition: cmd-edit.h:281
virtual void restore_pre_input_hook()
Definition: cmd-edit.h:354
virtual void do_clear_undo_list()
Definition: cmd-edit.h:346
virtual void do_newline()=0
virtual void do_accept_line()=0
virtual void do_insert_text(const std::string &text)=0
virtual void set_pre_input_hook(pre_input_hook_fcn)
Definition: cmd-edit.h:352
virtual bool do_prefer_env_winsize(bool)
Definition: cmd-edit.h:368
virtual void do_set_completer_quote_characters(const std::string &)
Definition: cmd-edit.h:295
virtual bool do_filename_quoting_desired(bool)
Definition: cmd-edit.h:366
virtual void do_handle_interrupt_signal()
Definition: cmd-edit.h:372
virtual void do_set_dequoting_function(dequoting_fcn)
Definition: cmd-edit.h:303
virtual void set_event_hook(event_hook_fcn)
Definition: cmd-edit.h:356
virtual bool do_undo()
Definition: cmd-edit.h:344
virtual std::string do_readline(const std::string &, bool &)=0
virtual void set_startup_hook(startup_hook_fcn)
Definition: cmd-edit.h:348
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
void() error(const char *fmt,...)
Definition: error.cc:988
F77_RET_T const F77_DBLE const F77_DBLE * f
#define OCTAVE_API
Definition: main.cc:55
octave_idx_type n
Definition: mx-inlines.cc:761