[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Updated patch for multiple users of readline event hook
From: |
John W. Eaton |
Subject: |
Re: Updated patch for multiple users of readline event hook |
Date: |
Tue, 18 Sep 2007 14:50:23 -0400 |
On 18-Sep-2007, John Swensen wrote:
| OK...after looking at my implementation and how you suggest, yours is
| much better. I have started working on it, but I do have one more
| question though. I still need to create a "default" handler for both
| the startup hook and the event hook that then calls each of the
| registered functions. Would you prefer this to be in the cmd-edit.cc
| file also?
I was thinking of something more like the following change, which I've
checked in.
| It won't be a member of the class, since you can't set the
| readline function pointer to a class function (not even a static one).
Yes, I think you can set it to a static member function (see the
attached patch).
jwe
liboctave/ChangeLog:
2007-09-18 John W. Eaton <address@hidden>
* cmd-edit.h, cmd-edit.cc (command_editor::startup_hook_set,
command_editor::event_hook_set): New static data.
(default_command_editor::set_startup_hook,
gnu_readline::set_startup_hook,
default_command_editor::restore_startup_hook,
gnu_readline_restore_event_hook):
Rename from do_set_startup_hook and do_set_event_hook.
(gnu_readline::operate_and_get_next): Call
command_editor::add_startup_hook, not
command_editor::set_startup_hook.
(command_editor::startup_handler, command_editor::event_handler):
New functions.
(command_editor::add_startup_hook, command_editor::add_event_hook,
command_editor::remove_startup_hook,
command_editor::remove_event_hook): Rename from set_startup_hook
and restore_startup_hook. Handle hook sets here.
* cmd-edit.cc (gnu_history::do_goto_mark):
Call remove_startup_hook instead of restore_startup_hook.
src/ChangeLog:
2007-09-18 John W. Eaton <address@hidden>
* input.cc (input_event_hook, Finput_event_hook): Call
command_editor::add_event_hook and
command_editor::remove_event_hook intstead of
command_editor::set_event_hook and
command_editor::restore_event_hook.
Index: liboctave/cmd-edit.cc
===================================================================
RCS file: /cvs/octave/liboctave/cmd-edit.cc,v
retrieving revision 1.31
diff -u -u -r1.31 cmd-edit.cc
--- liboctave/cmd-edit.cc 30 Jun 2006 18:19:42 -0000 1.31
+++ liboctave/cmd-edit.cc 18 Sep 2007 18:47:23 -0000
@@ -49,6 +49,10 @@
command_editor *command_editor::instance = 0;
+std::set<command_editor::startup_hook_fcn> command_editor::startup_hook_set;
+
+std::set<command_editor::event_hook_fcn> command_editor::event_hook_set;
+
#if defined (USE_READLINE)
#include <cstdio>
@@ -63,7 +67,7 @@
typedef command_editor::startup_hook_fcn startup_hook_fcn;
- typedef command_editor::event_hook_fcn event_hook_hook_fcn;
+ typedef command_editor::event_hook_fcn event_hook_fcn;
typedef command_editor::completion_fcn completion_fcn;
@@ -118,11 +122,13 @@
void do_clear_undo_list (void);
- void do_set_startup_hook (startup_hook_fcn f);
+ void set_startup_hook (startup_hook_fcn f);
+
+ void restore_startup_hook (void);
- void do_restore_startup_hook (void);
+ void set_event_hook (event_hook_fcn f);
- void do_set_event_hook (event_hook_fcn f);
+ void restore_event_hook (void);
void do_restore_event_hook (void);
@@ -393,7 +399,7 @@
}
void
-gnu_readline::do_set_startup_hook (startup_hook_fcn f)
+gnu_readline::set_startup_hook (startup_hook_fcn f)
{
previous_startup_hook = ::octave_rl_get_startup_hook ();
@@ -401,13 +407,13 @@
}
void
-gnu_readline::do_restore_startup_hook (void)
+gnu_readline::restore_startup_hook (void)
{
::octave_rl_set_startup_hook (previous_startup_hook);
}
void
-gnu_readline::do_set_event_hook (event_hook_fcn f)
+gnu_readline::set_event_hook (event_hook_fcn f)
{
previous_event_hook = octave_rl_get_event_hook ();
@@ -415,7 +421,7 @@
}
void
-gnu_readline::do_restore_event_hook (void)
+gnu_readline::restore_event_hook (void)
{
::octave_rl_set_event_hook (previous_event_hook);
}
@@ -452,7 +458,7 @@
else
command_history::set_mark (x_where + 1);
- command_editor::set_startup_hook (command_history::goto_mark);
+ command_editor::add_startup_hook (command_history::goto_mark);
return 0;
}
@@ -615,6 +621,36 @@
#endif
}
+int
+command_editor::startup_handler (void)
+{
+ for (startup_hook_set_iterator p = startup_hook_set.begin ();
+ p != startup_hook_set.end (); p++)
+ {
+ startup_hook_fcn f = *p;
+
+ if (f)
+ f ();
+ }
+
+ return 0;
+}
+
+int
+command_editor::event_handler (void)
+{
+ for (event_hook_set_iterator p = event_hook_set.begin ();
+ p != event_hook_set.end (); p++)
+ {
+ event_hook_fcn f = *p;
+
+ if (f)
+ f ();
+ }
+
+ return 0;
+}
+
void
command_editor::set_name (const std::string& n)
{
@@ -806,31 +842,55 @@
}
void
-command_editor::set_startup_hook (startup_hook_fcn f)
+command_editor::add_startup_hook (startup_hook_fcn f)
{
if (instance_ok ())
- instance->do_set_startup_hook (f);
+ {
+ startup_hook_set.insert (f);
+
+ instance->set_startup_hook (startup_handler);
+ }
}
void
-command_editor::restore_startup_hook (void)
+command_editor::remove_startup_hook (startup_hook_fcn f)
{
if (instance_ok ())
- instance->do_restore_startup_hook ();
+ {
+ startup_hook_set_iterator p = startup_hook_set.find (f);
+
+ if (p != startup_hook_set.end ())
+ event_hook_set.erase (p);
+
+ if (startup_hook_set.empty ())
+ instance->restore_startup_hook ();
+ }
}
void
-command_editor::set_event_hook (event_hook_fcn f)
+command_editor::add_event_hook (event_hook_fcn f)
{
if (instance_ok ())
- instance->do_set_event_hook (f);
+ {
+ event_hook_set.insert (f);
+
+ instance->set_event_hook (event_handler);
+ }
}
void
-command_editor::restore_event_hook (void)
+command_editor::remove_event_hook (event_hook_fcn f)
{
if (instance_ok ())
- instance->do_restore_event_hook ();
+ {
+ event_hook_set_iterator p = event_hook_set.find (f);
+
+ if (p != event_hook_set.end ())
+ event_hook_set.erase (p);
+
+ if (event_hook_set.empty ())
+ instance->restore_event_hook ();
+ }
}
void
Index: liboctave/cmd-edit.h
===================================================================
RCS file: /cvs/octave/liboctave/cmd-edit.h,v
retrieving revision 1.17
diff -u -u -r1.17 cmd-edit.h
--- liboctave/cmd-edit.h 27 Oct 2006 01:45:55 -0000 1.17
+++ liboctave/cmd-edit.h 18 Sep 2007 18:47:23 -0000
@@ -26,6 +26,7 @@
#include <cstdio>
+#include <set>
#include <string>
#include "str-vec.h"
@@ -97,13 +98,13 @@
static void clear_undo_list (void);
- static void set_startup_hook (startup_hook_fcn f);
+ static void add_startup_hook (startup_hook_fcn f);
- static void restore_startup_hook (void);
+ static void remove_startup_hook (startup_hook_fcn f);
- static void set_event_hook (event_hook_fcn f);
+ static void add_event_hook (event_hook_fcn f);
- static void restore_event_hook (void);
+ static void remove_event_hook (event_hook_fcn f);
static void read_init_file (const std::string& file = std::string ());
@@ -127,6 +128,20 @@
static void make_command_editor (void);
+ static int startup_handler (void);
+
+ static int event_handler (void);
+
+ static std::set<startup_hook_fcn> startup_hook_set;
+
+ static std::set<event_hook_fcn> event_hook_set;
+
+ typedef std::set<startup_hook_fcn>::iterator startup_hook_set_iterator;
+ typedef std::set<startup_hook_fcn>::const_iterator
startup_hook_set_const_iterator;
+
+ typedef std::set<event_hook_fcn>::iterator event_hook_set_iterator;
+ typedef std::set<event_hook_fcn>::const_iterator
event_hook_set_const_iterator;
+
// The real thing.
static command_editor *instance;
@@ -191,13 +206,13 @@
virtual void do_clear_undo_list (void) { }
- virtual void do_set_startup_hook (startup_hook_fcn) { }
+ virtual void set_startup_hook (startup_hook_fcn) { }
- virtual void do_restore_startup_hook (void) { }
+ virtual void restore_startup_hook (void) { }
- virtual void do_set_event_hook (event_hook_fcn) { }
+ virtual void set_event_hook (startup_hook_fcn) { }
- virtual void do_restore_event_hook (void) { }
+ virtual void restore_event_hook (void) { }
virtual void do_read_init_file (const std::string&) { }
Index: liboctave/cmd-hist.cc
===================================================================
RCS file: /cvs/octave/liboctave/cmd-hist.cc,v
retrieving revision 1.21
diff -u -u -r1.21 cmd-hist.cc
--- liboctave/cmd-hist.cc 30 Jun 2006 18:19:42 -0000 1.21
+++ liboctave/cmd-hist.cc 18 Sep 2007 18:47:23 -0000
@@ -207,7 +207,7 @@
mark = 0;
// FIXME -- for operate_and_get_next.
- command_editor::restore_startup_hook ();
+ command_editor::remove_startup_hook (command_history::goto_mark);
return 0;
}
Index: src/input.cc
===================================================================
RCS file: /cvs/octave/src/input.cc,v
retrieving revision 1.181
diff -u -u -r1.181 input.cc
--- src/input.cc 14 May 2007 17:35:46 -0000 1.181
+++ src/input.cc 18 Sep 2007 18:47:28 -0000
@@ -1101,7 +1101,7 @@
hook_fcn = std::string ();
user_data = octave_value ();
- command_editor::set_event_hook (0);
+ command_editor::remove_event_hook (input_event_hook);
}
return 0;
@@ -1148,11 +1148,11 @@
return retval;
}
- command_editor::set_event_hook (input_event_hook);
+ command_editor::add_event_hook (input_event_hook);
}
if (nargin == 0)
- command_editor::set_event_hook (0);
+ command_editor::remove_event_hook (input_event_hook);
retval(1) = user_data;
retval(0) = hook_fcn;