diff command-window.cpp @ 11:b652a5528fb1

handle EOF on input; more misc refactoring
author John W. Eaton <jwe@octave.org>
date Thu, 23 May 2019 13:42:57 -0400
parents 822a2fe5bb51
children 894be158b32d
line wrap: on
line diff
--- a/command-window.cpp	Thu May 23 12:58:58 2019 -0400
+++ b/command-window.cpp	Thu May 23 13:42:57 2019 -0400
@@ -20,6 +20,12 @@
 
 namespace calc
 {
+  // We could eliminate this global variable and the global
+  // command_window::the_command_window variable if readline callbacks
+  // could be defined as C++ lambda functions.  Then the lambdas could
+  // have direct access to the command_window object and it could
+  // contain the next available character.
+
   static int available_char = 0;
 
   // vvvvv readline vvvvv
@@ -39,16 +45,23 @@
 
   static void prep_term (int)
   {
+    // Anything to do here?
   }
 
   static void deprep_term (void)
   {
+    // Anything to do here?
   }
 
   static void accept_line (char *line)
   {
     if (command_window::the_command_window)
-      command_window::the_command_window->accept_line (line ? line : "");
+      {
+        if (! line)
+          command_window::the_command_window->eof ();
+        else
+          command_window::the_command_window->accept_line (line);
+      }
   }
 
   static void display_completion_matches (char **matches, int num_matches,
@@ -70,7 +83,7 @@
       }
   }
 
-  void readline_init (void)
+  static void readline_init (void)
   {
     rl_initialize ();
 
@@ -83,7 +96,7 @@
     rl_callback_handler_install (">> ", accept_line);
   }
 
-  void readline_fini (void)
+  static void readline_fini (void)
   {
     rl_callback_handler_remove ();
   }
@@ -93,8 +106,7 @@
   command_window *command_window::the_command_window = nullptr;
 
   command_window::command_window (QWidget *p)
-    : QTextEdit (p),
-      m_buffer (new QTextDocument ()),
+    : QTextEdit (p), m_buffer (new QTextDocument ()),
       m_interpreter (new calc::qt_interpreter ()),
       beg_mark (), prompt_mark ()
   {
@@ -127,6 +139,9 @@
     connect (this, SIGNAL (accept_line_signal (const QString&)),
              m_interpreter, SLOT (accept_input_line (const QString&)));
 
+    connect (this, SIGNAL (eof_signal (void)),
+             this, SLOT (close (void)));
+
     insert_at_end
       ("Qt Example Calculator.\n"
        "Available operations: + - * / ^ ()\n"
@@ -136,11 +151,18 @@
 
     beg_mark = set_mark ();
 
+    readline_init ();
+
     // Defer initializing and executing the interpreter until after the main
     // window and QApplication are running to prevent race conditions
     QTimer::singleShot (0, m_interpreter, SLOT (execute (void)));
   }
 
+  command_window::~command_window (void)
+  {
+    readline_fini ();
+  }
+
   // Accept an input line, parse and possibly execute it.
 
   void command_window::accept_line (const std::string& line)
@@ -156,6 +178,11 @@
       }
   }
 
+  void command_window::eof (void)
+  {
+    emit eof_signal ();
+  }
+
   void command_window::insert_at_end (const std::string& text)
   {
     scroll_to_bottom ();