GNU Octave  8.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-2023 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) (void);
54 
55  typedef int (*pre_input_hook_fcn) (void);
56 
57  typedef int (*event_hook_fcn) (void);
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  // No copying!
72 
73  command_editor (const command_editor&) = delete;
74 
75  command_editor& operator = (const command_editor&) = delete;
76 
77  virtual ~command_editor (void) = default;
78 
79  static void set_name (const std::string& n);
80 
81  static std::string readline (const std::string& prompt);
82 
83  static std::string readline (const std::string& prompt, bool& eof);
84 
85  static void set_input_stream (FILE *f);
86 
87  static FILE * get_input_stream (void);
88 
89  static void set_output_stream (FILE *f);
90 
91  static FILE * get_output_stream (void);
92 
93  static void redisplay (void);
94 
95  static int terminal_rows (void);
96 
97  static int terminal_cols (void);
98 
99  static void clear_screen (bool skip_redisplay = false);
100 
101  static void resize_terminal (void);
102 
103  static void set_screen_size (int ht, int wd);
104 
105  static std::string decode_prompt_string (const std::string& s);
106 
107  static void restore_terminal_state (void);
108 
109  static void blink_matching_paren (bool flag);
110 
111  static bool erase_empty_line (bool flag);
112 
113  static void set_basic_word_break_characters (const std::string& s);
114 
115  static void set_completer_word_break_characters (const std::string& s);
116 
117  static void set_basic_quote_characters (const std::string& s);
118 
119  static void set_filename_quote_characters (const std::string& s);
120 
121  static void set_completer_quote_characters (const std::string& s);
122 
123  static void set_completion_append_character (char c);
124 
125  static void set_completion_function (completion_fcn f);
126 
127  static void set_quoting_function (quoting_fcn f);
128 
129  static void set_dequoting_function (dequoting_fcn f);
130 
131  static void set_char_is_quoted_function (char_is_quoted_fcn f);
132 
133  static void set_user_accept_line_function (user_accept_line_fcn f);
134 
135  static completion_fcn get_completion_function (void);
136 
137  static quoting_fcn get_quoting_function (void);
138 
139  static dequoting_fcn get_dequoting_function (void);
140 
141  static char_is_quoted_fcn get_char_is_quoted_function (void);
142 
143  static user_accept_line_fcn get_user_accept_line_function (void);
144 
145  static string_vector
146  generate_filename_completions (const std::string& text);
147 
148  static std::string get_line_buffer (void);
149 
150  static std::string get_current_line (void);
151 
152  static char get_prev_char (int);
153 
154  static void replace_line (const std::string& text, bool clear_undo = true);
155 
156  static void kill_full_line (void);
157 
158  static void insert_text (const std::string& text);
159 
160  static void newline (void);
161 
162  static void accept_line (void);
163 
164  static bool undo (void);
165 
166  static void clear_undo_list (void);
167 
168  static void add_startup_hook (startup_hook_fcn f);
169 
170  static void remove_startup_hook (startup_hook_fcn f);
171 
172  static void add_pre_input_hook (pre_input_hook_fcn f);
173 
174  static void remove_pre_input_hook (pre_input_hook_fcn f);
175 
176  static void add_event_hook (event_hook_fcn f);
177 
178  static void remove_event_hook (event_hook_fcn f);
179 
180  static void run_event_hooks (void);
181 
182  static void read_init_file (const std::string& file = "");
183 
184  static void re_read_init_file (void);
185 
186  static bool filename_completion_desired (bool);
187 
188  static bool filename_quoting_desired (bool);
189 
190  static bool prefer_env_winsize (bool);
191 
192  static bool interrupt (bool = true);
193 
194  static void interrupt_event_loop (bool flag = true);
195 
196  static bool event_loop_interrupted (void);
197 
198  static int current_command_number (void);
199 
200  static void reset_current_command_number (int n);
201 
202  static void increment_current_command_number (void);
203 
204  static void force_default_editor (void);
205 
206  static void set_initial_input (const std::string& text);
207 
208  static int insert_initial_input (void);
209 
210 private:
211 
212  static bool instance_ok (void);
213 
214  static void make_command_editor (void);
215 
216  static int startup_handler (void);
217 
218  static int pre_input_handler (void);
219 
220  static int event_handler (void);
221 
222  static void cleanup_instance (void)
223  {
224  delete s_instance;
225  s_instance = nullptr;
226  }
227 
228  static void handle_interrupt_signal (void);
229 
230  //--------
231 
232  static command_editor *s_instance; // the real thing.
233 
234  static std::set<startup_hook_fcn> m_startup_hook_set;
235  static std::set<pre_input_hook_fcn> m_pre_input_hook_set;
236  static std::set<event_hook_fcn> m_event_hook_set;
237 
238 protected:
239 
240  // To use something other than the GNU readline library, derive a new
241  // class from command_editor, overload these functions as
242  // necessary, and make instance point to the new class.
243 
244  virtual void do_set_name (const std::string&) { }
245 
246  std::string do_readline (const std::string& prompt)
247  {
248  bool eof;
249 
250  return do_readline (prompt, eof);
251  }
252 
253  virtual std::string do_readline (const std::string&, bool&) = 0;
254 
255  virtual void do_set_input_stream (FILE *) = 0;
256 
257  virtual FILE * do_get_input_stream (void) = 0;
258 
259  virtual void do_set_output_stream (FILE *) = 0;
260 
261  virtual FILE * do_get_output_stream (void) = 0;
262 
263  virtual void do_redisplay (void) { }
264 
265  virtual int do_terminal_rows (void) { return m_rows; }
266 
267  virtual int do_terminal_cols (void) { return m_cols; }
268 
269  virtual void do_clear_screen (bool) { }
270 
271  virtual void do_resize_terminal (void) { }
272 
273  virtual void do_set_screen_size (int ht, int wd)
274  {
275  m_rows = ht;
276  m_cols = wd;
277  }
278 
279  virtual std::string do_decode_prompt_string (const std::string&);
280 
281  virtual std::string newline_chars (void) { return "\n"; }
282 
283  virtual void do_restore_terminal_state (void) { }
284 
285  virtual void do_blink_matching_paren (bool) { }
286 
287  virtual bool do_erase_empty_line (bool) { return false; }
288 
289  virtual void do_set_basic_word_break_characters (const std::string&) { }
290 
291  virtual void do_set_completer_word_break_characters (const std::string&) { }
292 
293  virtual void do_set_completer_word_break_hook (completion_hook_fcn) { }
294 
295  virtual void do_set_basic_quote_characters (const std::string&) { }
296 
297  virtual void do_set_filename_quote_characters (const std::string&) { }
298 
299  virtual void do_set_completer_quote_characters (const std::string&) { }
300 
301  virtual void do_set_completion_append_character (char) { }
302 
303  virtual void do_set_completion_function (completion_fcn) { }
304 
305  virtual void do_set_quoting_function (quoting_fcn) { }
306 
307  virtual void do_set_dequoting_function (dequoting_fcn) { }
308 
309  virtual void do_set_char_is_quoted_function (char_is_quoted_fcn) { }
310 
311  virtual void do_set_user_accept_line_function (user_accept_line_fcn) { }
312 
313  virtual completion_fcn do_get_completion_function (void) const
314  { return nullptr; }
315 
316  virtual quoting_fcn do_get_quoting_function (void) const
317  { return nullptr; }
318 
319  virtual dequoting_fcn do_get_dequoting_function (void) const
320  { return nullptr; }
321 
322  virtual char_is_quoted_fcn do_get_char_is_quoted_function (void) const
323  { return nullptr; }
324 
325  virtual user_accept_line_fcn do_get_user_accept_line_function (void) const
326  { return nullptr; }
327 
328  virtual string_vector
329  do_generate_filename_completions (const std::string& text) = 0;
330 
331  virtual std::string do_get_line_buffer (void) const = 0;
332 
333  virtual std::string do_get_current_line (void) const = 0;
334 
335  virtual char do_get_prev_char (int) const = 0;
336 
337  virtual void
338  do_replace_line (const std::string& text, bool clear_undo) = 0;
339 
340  virtual void do_kill_full_line (void) = 0;
341 
342  virtual void do_insert_text (const std::string& text) = 0;
343 
344  virtual void do_newline (void) = 0;
345 
346  virtual void do_accept_line (void) = 0;
347 
348  virtual bool do_undo (void) { return false; }
349 
350  virtual void do_clear_undo_list (void) { }
351 
352  virtual void set_startup_hook (startup_hook_fcn) { }
353 
354  virtual void restore_startup_hook (void) { }
355 
356  virtual void set_pre_input_hook (pre_input_hook_fcn) { }
357 
358  virtual void restore_pre_input_hook (void) { }
359 
360  virtual void set_event_hook (event_hook_fcn) { }
361 
362  virtual void restore_event_hook (void) { }
363 
364  virtual void do_read_init_file (const std::string&) { }
365 
366  virtual void do_re_read_init_file (void) { }
367 
368  virtual bool do_filename_completion_desired (bool) { return false; }
369 
370  virtual bool do_filename_quoting_desired (bool) { return false; }
371 
372  virtual bool do_prefer_env_winsize (bool) { return false; }
373 
374  virtual void do_interrupt (bool) { }
375 
376  virtual void do_handle_interrupt_signal (void) { }
377 
378  void do_interrupt_event_loop (bool arg) { m_interrupt_event_loop = arg; }
379 
380  bool do_event_loop_interrupted (void) const
381  { return m_interrupt_event_loop; }
382 
383  int do_insert_initial_input (void);
384 
385  int read_octal (const std::string& s);
386 
387  void error (int);
388 
389  void error (const std::string&);
390 
391  // The current command number.
393 
394  int m_rows;
395  int m_cols;
396 
398 
400 
401  std::string m_initial_input;
402 };
403 
405 
406 #endif
OCTAVE_END_NAMESPACE(octave)
virtual string_vector do_generate_filename_completions(const std::string &text)=0
static void cleanup_instance(void)
Definition: cmd-edit.h:222
virtual void restore_event_hook(void)
Definition: cmd-edit.h:362
static std::set< pre_input_hook_fcn > m_pre_input_hook_set
Definition: cmd-edit.h:235
static command_editor * s_instance
Definition: cmd-edit.h:232
virtual void do_set_screen_size(int ht, int wd)
Definition: cmd-edit.h:273
virtual void do_resize_terminal(void)
Definition: cmd-edit.h:271
virtual bool do_erase_empty_line(bool)
Definition: cmd-edit.h:287
virtual void do_set_basic_quote_characters(const std::string &)
Definition: cmd-edit.h:295
std::string do_readline(const std::string &prompt)
Definition: cmd-edit.h:246
int m_command_number
Definition: cmd-edit.h:392
virtual void do_set_user_accept_line_function(user_accept_line_fcn)
Definition: cmd-edit.h:311
virtual void do_set_name(const std::string &)
Definition: cmd-edit.h:244
virtual char do_get_prev_char(int) const =0
virtual void do_set_filename_quote_characters(const std::string &)
Definition: cmd-edit.h:297
virtual void do_set_completer_word_break_characters(const std::string &)
Definition: cmd-edit.h:291
bool m_interrupted
Definition: cmd-edit.h:397
virtual void do_set_input_stream(FILE *)=0
virtual std::string do_get_current_line(void) const =0
std::string m_initial_input
Definition: cmd-edit.h:401
command_editor(const command_editor &)=delete
virtual quoting_fcn do_get_quoting_function(void) const
Definition: cmd-edit.h:316
virtual void do_read_init_file(const std::string &)
Definition: cmd-edit.h:364
virtual void do_set_completion_append_character(char)
Definition: cmd-edit.h:301
virtual bool do_filename_completion_desired(bool)
Definition: cmd-edit.h:368
static std::set< event_hook_fcn > m_event_hook_set
Definition: cmd-edit.h:236
bool m_interrupt_event_loop
Definition: cmd-edit.h:399
virtual void do_replace_line(const std::string &text, bool clear_undo)=0
virtual void do_set_output_stream(FILE *)=0
virtual bool do_undo(void)
Definition: cmd-edit.h:348
void do_interrupt_event_loop(bool arg)
Definition: cmd-edit.h:378
virtual void do_set_completer_word_break_hook(completion_hook_fcn)
Definition: cmd-edit.h:293
virtual void do_set_quoting_function(quoting_fcn)
Definition: cmd-edit.h:305
virtual void do_restore_terminal_state(void)
Definition: cmd-edit.h:283
command_editor(void)
Definition: cmd-edit.h:46
virtual user_accept_line_fcn do_get_user_accept_line_function(void) const
Definition: cmd-edit.h:325
virtual void do_set_char_is_quoted_function(char_is_quoted_fcn)
Definition: cmd-edit.h:309
virtual void restore_pre_input_hook(void)
Definition: cmd-edit.h:358
virtual std::string do_get_line_buffer(void) const =0
virtual char_is_quoted_fcn do_get_char_is_quoted_function(void) const
Definition: cmd-edit.h:322
virtual FILE * do_get_output_stream(void)=0
virtual void do_interrupt(bool)
Definition: cmd-edit.h:374
static std::set< startup_hook_fcn > m_startup_hook_set
Definition: cmd-edit.h:234
virtual int do_terminal_rows(void)
Definition: cmd-edit.h:265
virtual void do_clear_screen(bool)
Definition: cmd-edit.h:269
virtual void do_accept_line(void)=0
virtual void do_set_basic_word_break_characters(const std::string &)
Definition: cmd-edit.h:289
virtual void do_set_completion_function(completion_fcn)
Definition: cmd-edit.h:303
virtual void do_blink_matching_paren(bool)
Definition: cmd-edit.h:285
virtual void do_clear_undo_list(void)
Definition: cmd-edit.h:350
bool do_event_loop_interrupted(void) const
Definition: cmd-edit.h:380
virtual std::string newline_chars(void)
Definition: cmd-edit.h:281
virtual void do_re_read_init_file(void)
Definition: cmd-edit.h:366
virtual ~command_editor(void)=default
virtual completion_fcn do_get_completion_function(void) const
Definition: cmd-edit.h:313
virtual dequoting_fcn do_get_dequoting_function(void) const
Definition: cmd-edit.h:319
virtual void do_insert_text(const std::string &text)=0
virtual void do_newline(void)=0
virtual void do_kill_full_line(void)=0
virtual void do_handle_interrupt_signal(void)
Definition: cmd-edit.h:376
virtual void do_redisplay(void)
Definition: cmd-edit.h:263
virtual void set_pre_input_hook(pre_input_hook_fcn)
Definition: cmd-edit.h:356
virtual bool do_prefer_env_winsize(bool)
Definition: cmd-edit.h:372
virtual void do_set_completer_quote_characters(const std::string &)
Definition: cmd-edit.h:299
virtual int do_terminal_cols(void)
Definition: cmd-edit.h:267
virtual void restore_startup_hook(void)
Definition: cmd-edit.h:354
virtual bool do_filename_quoting_desired(bool)
Definition: cmd-edit.h:370
virtual void do_set_dequoting_function(dequoting_fcn)
Definition: cmd-edit.h:307
virtual FILE * do_get_input_stream(void)=0
virtual void set_event_hook(event_hook_fcn)
Definition: cmd-edit.h:360
virtual std::string do_readline(const std::string &, bool &)=0
virtual void set_startup_hook(startup_hook_fcn)
Definition: cmd-edit.h:352
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
void error(const char *fmt,...)
Definition: error.cc:979
F77_RET_T const F77_DBLE const F77_DBLE * f
#define OCTAVE_API
Definition: main.in.cc:55
octave_idx_type n
Definition: mx-inlines.cc:753