comparison qt-interpreter.cpp @ 5:54edd85237ab

use signal to send input to qt interpreter object
author John W. Eaton <jwe@octave.org>
date Wed, 22 May 2019 18:07:37 -0400
parents 0e154787183d
children 1b575145197e
comparison
equal deleted inserted replaced
4:0e154787183d 5:54edd85237ab
1 #include <sstream> 1 #include <sstream>
2
3 #include <QString>
2 4
3 #include "gui-main.h" 5 #include "gui-main.h"
4 #include "interpreter.h" 6 #include "interpreter.h"
5 #include "qt-interpreter.h" 7 #include "qt-interpreter.h"
6 8
7 #include <readline/readline.h> 9 #include <readline/readline.h>
8 #include <readline/history.h> 10 #include <readline/history.h>
9 11
10 namespace calc 12 namespace calc
11 { 13 {
12 static int getc (FILE *)
13 {
14 int tmp = gui::available_char;
15 gui::available_char = 0;
16 return tmp;
17 }
18
19 static void redisplay (void)
20 {
21 if (gui::calc_interaction_window)
22 gui::calc_interaction_window->redisplay ();
23 }
24
25 static void prep_term (int)
26 {
27 }
28
29 static void deprep_term (void)
30 {
31 }
32
33 static void accept_line (char *line)
34 {
35 if (gui::calc_interaction_window)
36 gui::calc_interaction_window->accept (line ? line : "");
37 }
38
39 static void display_completion_matches (char **matches, int num_matches,
40 int /* max_length */)
41 {
42 if (gui::calc_interaction_window)
43 {
44 std::ostringstream buf;
45
46 if (num_matches > 1)
47 buf << "\n";
48
49 for (int i = 1; i < num_matches; i++)
50 buf << matches[i] << "\n";
51
52 gui::calc_interaction_window->insert_at_end (buf.str ());
53
54 gui::calc_interaction_window->redisplay ();
55 }
56 }
57
58 static void readline_init (void)
59 {
60 rl_initialize ();
61
62 rl_getc_function = getc;
63 rl_redisplay_function = redisplay;
64 rl_prep_term_function = prep_term;
65 rl_deprep_term_function = deprep_term;
66 rl_completion_display_matches_hook = display_completion_matches;
67
68 rl_callback_handler_install (">> ", accept_line);
69 }
70
71 static void readline_fini (void)
72 {
73 rl_callback_handler_remove ();
74 }
75
76 qt_interpreter::qt_interpreter (void) 14 qt_interpreter::qt_interpreter (void)
77 { 15 {
78 interpreter::init (); 16 interpreter::init ();
79 } 17 }
80 18
81 qt_interpreter::~qt_interpreter (void) 19 qt_interpreter::~qt_interpreter (void)
82 { 20 {
83 interpreter::fini (); 21 interpreter::fini ();
84
85 readline_fini ();
86 } 22 }
87 23
88 void qt_interpreter::execute (void) 24 void qt_interpreter::execute (void)
89 { 25 {
90 readline_init (); 26 }
27
28 void qt_interpreter::accept_input_line (const QString& line)
29 {
30 interpreter::parse_and_execute (line.toStdString ());
91 } 31 }
92 } 32 }