comparison liboctave/cmd-edit.cc @ 6913:f779c83d6ccf

[project @ 2007-09-18 18:58:12 by jwe]
author jwe
date Tue, 18 Sep 2007 18:58:13 +0000
parents 44f24cf66b95
children b84be419beab
comparison
equal deleted inserted replaced
6912:66c7da4ee7a1 6913:f779c83d6ccf
47 #include "oct-env.h" 47 #include "oct-env.h"
48 #include "oct-time.h" 48 #include "oct-time.h"
49 49
50 command_editor *command_editor::instance = 0; 50 command_editor *command_editor::instance = 0;
51 51
52 std::set<command_editor::startup_hook_fcn> command_editor::startup_hook_set;
53
54 std::set<command_editor::event_hook_fcn> command_editor::event_hook_set;
55
52 #if defined (USE_READLINE) 56 #if defined (USE_READLINE)
53 57
54 #include <cstdio> 58 #include <cstdio>
55 #include <cstdlib> 59 #include <cstdlib>
56 60
61 { 65 {
62 public: 66 public:
63 67
64 typedef command_editor::startup_hook_fcn startup_hook_fcn; 68 typedef command_editor::startup_hook_fcn startup_hook_fcn;
65 69
66 typedef command_editor::event_hook_fcn event_hook_hook_fcn; 70 typedef command_editor::event_hook_fcn event_hook_fcn;
67 71
68 typedef command_editor::completion_fcn completion_fcn; 72 typedef command_editor::completion_fcn completion_fcn;
69 73
70 gnu_readline (void); 74 gnu_readline (void);
71 75
116 120
117 void do_newline (void); 121 void do_newline (void);
118 122
119 void do_clear_undo_list (void); 123 void do_clear_undo_list (void);
120 124
121 void do_set_startup_hook (startup_hook_fcn f); 125 void set_startup_hook (startup_hook_fcn f);
122 126
123 void do_restore_startup_hook (void); 127 void restore_startup_hook (void);
124 128
125 void do_set_event_hook (event_hook_fcn f); 129 void set_event_hook (event_hook_fcn f);
130
131 void restore_event_hook (void);
126 132
127 void do_restore_event_hook (void); 133 void do_restore_event_hook (void);
128 134
129 void do_read_init_file (const std::string& file); 135 void do_read_init_file (const std::string& file);
130 136
391 { 397 {
392 ::octave_rl_clear_undo_list (); 398 ::octave_rl_clear_undo_list ();
393 } 399 }
394 400
395 void 401 void
396 gnu_readline::do_set_startup_hook (startup_hook_fcn f) 402 gnu_readline::set_startup_hook (startup_hook_fcn f)
397 { 403 {
398 previous_startup_hook = ::octave_rl_get_startup_hook (); 404 previous_startup_hook = ::octave_rl_get_startup_hook ();
399 405
400 ::octave_rl_set_startup_hook (f); 406 ::octave_rl_set_startup_hook (f);
401 } 407 }
402 408
403 void 409 void
404 gnu_readline::do_restore_startup_hook (void) 410 gnu_readline::restore_startup_hook (void)
405 { 411 {
406 ::octave_rl_set_startup_hook (previous_startup_hook); 412 ::octave_rl_set_startup_hook (previous_startup_hook);
407 } 413 }
408 414
409 void 415 void
410 gnu_readline::do_set_event_hook (event_hook_fcn f) 416 gnu_readline::set_event_hook (event_hook_fcn f)
411 { 417 {
412 previous_event_hook = octave_rl_get_event_hook (); 418 previous_event_hook = octave_rl_get_event_hook ();
413 419
414 ::octave_rl_set_event_hook (f); 420 ::octave_rl_set_event_hook (f);
415 } 421 }
416 422
417 void 423 void
418 gnu_readline::do_restore_event_hook (void) 424 gnu_readline::restore_event_hook (void)
419 { 425 {
420 ::octave_rl_set_event_hook (previous_event_hook); 426 ::octave_rl_set_event_hook (previous_event_hook);
421 } 427 }
422 428
423 void 429 void
450 || (x_where >= x_length - 1)) 456 || (x_where >= x_length - 1))
451 command_history::set_mark (x_where); 457 command_history::set_mark (x_where);
452 else 458 else
453 command_history::set_mark (x_where + 1); 459 command_history::set_mark (x_where + 1);
454 460
455 command_editor::set_startup_hook (command_history::goto_mark); 461 command_editor::add_startup_hook (command_history::goto_mark);
456 462
457 return 0; 463 return 0;
458 } 464 }
459 465
460 int 466 int
613 #else 619 #else
614 instance = new default_command_editor (); 620 instance = new default_command_editor ();
615 #endif 621 #endif
616 } 622 }
617 623
624 int
625 command_editor::startup_handler (void)
626 {
627 for (startup_hook_set_iterator p = startup_hook_set.begin ();
628 p != startup_hook_set.end (); p++)
629 {
630 startup_hook_fcn f = *p;
631
632 if (f)
633 f ();
634 }
635
636 return 0;
637 }
638
639 int
640 command_editor::event_handler (void)
641 {
642 for (event_hook_set_iterator p = event_hook_set.begin ();
643 p != event_hook_set.end (); p++)
644 {
645 event_hook_fcn f = *p;
646
647 if (f)
648 f ();
649 }
650
651 return 0;
652 }
653
618 void 654 void
619 command_editor::set_name (const std::string& n) 655 command_editor::set_name (const std::string& n)
620 { 656 {
621 if (instance_ok ()) 657 if (instance_ok ())
622 instance->do_set_name (n); 658 instance->do_set_name (n);
804 if (instance_ok ()) 840 if (instance_ok ())
805 instance->do_clear_undo_list (); 841 instance->do_clear_undo_list ();
806 } 842 }
807 843
808 void 844 void
809 command_editor::set_startup_hook (startup_hook_fcn f) 845 command_editor::add_startup_hook (startup_hook_fcn f)
810 { 846 {
811 if (instance_ok ()) 847 if (instance_ok ())
812 instance->do_set_startup_hook (f); 848 {
813 } 849 startup_hook_set.insert (f);
814 850
815 void 851 instance->set_startup_hook (startup_handler);
816 command_editor::restore_startup_hook (void) 852 }
817 { 853 }
818 if (instance_ok ()) 854
819 instance->do_restore_startup_hook (); 855 void
820 } 856 command_editor::remove_startup_hook (startup_hook_fcn f)
821 857 {
822 void 858 if (instance_ok ())
823 command_editor::set_event_hook (event_hook_fcn f) 859 {
824 { 860 startup_hook_set_iterator p = startup_hook_set.find (f);
825 if (instance_ok ()) 861
826 instance->do_set_event_hook (f); 862 if (p != startup_hook_set.end ())
827 } 863 event_hook_set.erase (p);
828 864
829 void 865 if (startup_hook_set.empty ())
830 command_editor::restore_event_hook (void) 866 instance->restore_startup_hook ();
831 { 867 }
832 if (instance_ok ()) 868 }
833 instance->do_restore_event_hook (); 869
870 void
871 command_editor::add_event_hook (event_hook_fcn f)
872 {
873 if (instance_ok ())
874 {
875 event_hook_set.insert (f);
876
877 instance->set_event_hook (event_handler);
878 }
879 }
880
881 void
882 command_editor::remove_event_hook (event_hook_fcn f)
883 {
884 if (instance_ok ())
885 {
886 event_hook_set_iterator p = event_hook_set.find (f);
887
888 if (p != event_hook_set.end ())
889 event_hook_set.erase (p);
890
891 if (event_hook_set.empty ())
892 instance->restore_event_hook ();
893 }
834 } 894 }
835 895
836 void 896 void
837 command_editor::read_init_file (const std::string& file_arg) 897 command_editor::read_init_file (const std::string& file_arg)
838 { 898 {