cmd-edit.h

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 1996-2012 John W. Eaton
00004 
00005 This file is part of Octave.
00006 
00007 Octave is free software; you can redistribute it and/or modify it
00008 under the terms of the GNU General Public License as published by the
00009 Free Software Foundation; either version 3 of the License, or (at your
00010 option) any later version.
00011 
00012 Octave is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00015 for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Octave; see the file COPYING.  If not, see
00019 <http://www.gnu.org/licenses/>.
00020 
00021 */
00022 
00023 #if !defined (octave_cmd_edit_h)
00024 #define octave_cmd_edit_h 1
00025 
00026 #include <cstdio>
00027 
00028 #include <set>
00029 #include <string>
00030 
00031 #include "str-vec.h"
00032 
00033 class
00034 OCTAVE_API
00035 command_editor
00036 {
00037 protected:
00038 
00039   command_editor (void)
00040     : command_number (0) { }
00041 
00042 public:
00043 
00044   typedef int (*startup_hook_fcn) (void);
00045 
00046   typedef int (*event_hook_fcn) (void);
00047 
00048   typedef std::string (*completion_fcn) (const std::string&, int);
00049 
00050   typedef std::string (*quoting_fcn) (const std::string&, int, char);
00051 
00052   typedef std::string (*dequoting_fcn) (const std::string&, int);
00053 
00054   typedef int (*char_is_quoted_fcn) (const std::string&, int);
00055 
00056   typedef void (*user_accept_line_fcn) (const std::string&);
00057 
00058   virtual ~command_editor (void) { }
00059 
00060   static void set_name (const std::string& n);
00061 
00062   static std::string readline (const std::string& prompt);
00063 
00064   static std::string readline (const std::string& prompt, bool& eof);
00065 
00066   static void set_input_stream (FILE *f);
00067 
00068   static FILE *get_input_stream (void);
00069 
00070   static void set_output_stream (FILE *f);
00071 
00072   static FILE *get_output_stream (void);
00073 
00074   static int terminal_rows (void);
00075 
00076   static int terminal_cols (void);
00077 
00078   static void clear_screen (void);
00079 
00080   static void resize_terminal (void);
00081 
00082   static std::string decode_prompt_string (const std::string& s);
00083 
00084   static void restore_terminal_state (void);
00085 
00086   static void blink_matching_paren (bool flag);
00087 
00088   static void set_basic_word_break_characters (const std::string& s);
00089 
00090   static void set_completer_word_break_characters (const std::string& s);
00091 
00092   static void set_basic_quote_characters (const std::string& s);
00093 
00094   static void set_filename_quote_characters (const std::string& s);
00095 
00096   static void set_completer_quote_characters (const std::string& s);
00097 
00098   static void set_completion_append_character (char c);
00099 
00100   static void set_completion_function (completion_fcn f);
00101 
00102   static void set_quoting_function (quoting_fcn f);
00103 
00104   static void set_dequoting_function (dequoting_fcn f);
00105 
00106   static void set_char_is_quoted_function (char_is_quoted_fcn f);
00107 
00108   static void set_user_accept_line_function (user_accept_line_fcn f);
00109 
00110   static completion_fcn get_completion_function (void);
00111 
00112   static quoting_fcn get_quoting_function (void);
00113 
00114   static dequoting_fcn get_dequoting_function (void);
00115 
00116   static char_is_quoted_fcn get_char_is_quoted_function (void);
00117 
00118   static user_accept_line_fcn get_user_accept_line_function (void);
00119 
00120   static string_vector generate_filename_completions (const std::string& text);
00121 
00122   static std::string get_line_buffer (void);
00123 
00124   static void insert_text (const std::string& text);
00125 
00126   static void newline (void);
00127 
00128   static void accept_line (void);
00129 
00130   static void clear_undo_list (void);
00131 
00132   static void add_startup_hook (startup_hook_fcn f);
00133 
00134   static void remove_startup_hook (startup_hook_fcn f);
00135 
00136   static void add_event_hook (event_hook_fcn f);
00137 
00138   static void remove_event_hook (event_hook_fcn f);
00139 
00140   static void run_event_hooks (void);
00141 
00142   static void read_init_file (const std::string& file = std::string ());
00143 
00144   static void re_read_init_file (void);
00145 
00146   static bool filename_completion_desired (bool);
00147 
00148   static bool filename_quoting_desired (bool);
00149 
00150   static int current_command_number (void);
00151 
00152   static void reset_current_command_number (int n);
00153 
00154   static void increment_current_command_number (void);
00155 
00156   static void force_default_editor (void);
00157 
00158 private:
00159 
00160   // No copying!
00161 
00162   command_editor (const command_editor&);
00163 
00164   command_editor& operator = (const command_editor&);
00165 
00166   static bool instance_ok (void);
00167 
00168   static void make_command_editor (void);
00169 
00170   static int startup_handler (void);
00171 
00172   static int event_handler (void);
00173 
00174   static std::set<startup_hook_fcn> startup_hook_set;
00175 
00176   static std::set<event_hook_fcn> event_hook_set;
00177 
00178   typedef std::set<startup_hook_fcn>::iterator startup_hook_set_iterator;
00179   typedef std::set<startup_hook_fcn>::const_iterator startup_hook_set_const_iterator;
00180 
00181   typedef std::set<event_hook_fcn>::iterator event_hook_set_iterator;
00182   typedef std::set<event_hook_fcn>::const_iterator event_hook_set_const_iterator;
00183 
00184   // The real thing.
00185   static command_editor *instance;
00186 
00187   static void cleanup_instance (void) { delete instance; instance = 0; }
00188 
00189 protected:
00190 
00191   // To use something other than the GNU readline library, derive a new
00192   // class from command_editor, overload these functions as
00193   // necessary, and make instance point to the new class.
00194 
00195   virtual void do_set_name (const std::string&) { }
00196 
00197   std::string do_readline (const std::string& prompt)
00198     {
00199       bool eof;
00200 
00201       return do_readline (prompt, eof);
00202     }
00203 
00204   virtual std::string do_readline (const std::string&, bool&) = 0;
00205 
00206   virtual void do_set_input_stream (FILE *) = 0;
00207 
00208   virtual FILE *do_get_input_stream (void) = 0;
00209 
00210   virtual void do_set_output_stream (FILE *) = 0;
00211 
00212   virtual FILE *do_get_output_stream (void) = 0;
00213 
00214   virtual int do_terminal_rows (void) { return 24; }
00215 
00216   virtual int do_terminal_cols (void) { return 80; }
00217 
00218   virtual void do_clear_screen (void) { }
00219 
00220   virtual void do_resize_terminal (void) { }
00221 
00222   virtual std::string do_decode_prompt_string (const std::string&);
00223 
00224   virtual std::string newline_chars (void) { return "\n"; }
00225 
00226   virtual void do_restore_terminal_state (void) { }
00227 
00228   virtual void do_blink_matching_paren (bool) { }
00229 
00230   virtual void do_set_basic_word_break_characters (const std::string&) { }
00231 
00232   virtual void do_set_completer_word_break_characters (const std::string&) { }
00233 
00234   virtual void do_set_basic_quote_characters (const std::string&) { }
00235 
00236   virtual void do_set_filename_quote_characters (const std::string&) { }
00237 
00238   virtual void do_set_completer_quote_characters (const std::string&) { }
00239 
00240   virtual void do_set_completion_append_character (char) { }
00241 
00242   virtual void do_set_completion_function (completion_fcn) { }
00243 
00244   virtual void do_set_quoting_function (quoting_fcn) { }
00245 
00246   virtual void do_set_dequoting_function (dequoting_fcn) { }
00247 
00248   virtual void do_set_char_is_quoted_function (char_is_quoted_fcn) { }
00249 
00250   virtual void do_set_user_accept_line_function (user_accept_line_fcn) { }
00251 
00252   virtual completion_fcn do_get_completion_function (void) const { return 0; }
00253 
00254   virtual quoting_fcn do_get_quoting_function (void) const { return 0; }
00255 
00256   virtual dequoting_fcn do_get_dequoting_function (void) const { return 0; }
00257 
00258   virtual char_is_quoted_fcn do_get_char_is_quoted_function (void) const { return 0; }
00259 
00260   virtual user_accept_line_fcn do_get_user_accept_line_function (void) const { return 0; }
00261 
00262   virtual string_vector do_generate_filename_completions (const std::string& text) = 0;
00263 
00264   virtual std::string do_get_line_buffer (void) const = 0;
00265 
00266   virtual void do_insert_text (const std::string&) = 0;
00267 
00268   virtual void do_newline (void) = 0;
00269 
00270   virtual void do_accept_line (void) = 0;
00271 
00272   virtual void do_clear_undo_list (void) { }
00273 
00274   virtual void set_startup_hook (startup_hook_fcn) { }
00275 
00276   virtual void restore_startup_hook (void) { }
00277 
00278   virtual void set_event_hook (startup_hook_fcn) { }
00279 
00280   virtual void restore_event_hook (void) { }
00281 
00282   virtual void do_read_init_file (const std::string&) { }
00283 
00284   virtual void do_re_read_init_file (void) { }
00285 
00286   virtual bool do_filename_completion_desired (bool) { return false; }
00287 
00288   virtual bool do_filename_quoting_desired (bool) { return false; }
00289 
00290   int read_octal (const std::string& s);
00291 
00292   void error (int);
00293 
00294   void error (const std::string&);
00295 
00296   // The current command number.
00297   int command_number;
00298 };
00299 
00300 #endif
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines