Mercurial > jwe > qt-gui-with-push-parser
annotate command-window.cpp @ 17:2ddf3fe6fa33
initial attempt at using separate thread for interpreter
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Tue, 28 May 2019 18:27:18 -0400 |
parents | 79783f3e2017 |
children |
rev | line source |
---|---|
9
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
1 #include <iostream> |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
2 #include <sstream> |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
3 #include <string> |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
4 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
5 #include <QApplication> |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
6 #include <QKeyEvent> |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
7 #include <QTextDocument> |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
8 #include <QTextEdit> |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
9 #include <QTimer> |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
10 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
11 #include "command-window.h" |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
12 #include "interpreter.h" |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
13 #include "parser.h" |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
14 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
15 #include <readline/readline.h> |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
16 #include <readline/history.h> |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
17 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
18 namespace calc |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
19 { |
14
1e5a1e15fa56
clean up header files, more small readline changes
John W. Eaton <jwe@octave.org>
parents:
12
diff
changeset
|
20 // vvvvv readline vvvvv |
1e5a1e15fa56
clean up header files, more small readline changes
John W. Eaton <jwe@octave.org>
parents:
12
diff
changeset
|
21 |
11
b652a5528fb1
handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents:
9
diff
changeset
|
22 // We could eliminate this global variable and the global |
b652a5528fb1
handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents:
9
diff
changeset
|
23 // command_window::the_command_window variable if readline callbacks |
b652a5528fb1
handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents:
9
diff
changeset
|
24 // could be defined as C++ lambda functions. Then the lambdas could |
b652a5528fb1
handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents:
9
diff
changeset
|
25 // have direct access to the command_window object and it could |
b652a5528fb1
handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents:
9
diff
changeset
|
26 // contain the next available character. |
b652a5528fb1
handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents:
9
diff
changeset
|
27 |
9
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
28 static int available_char = 0; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
29 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
30 static int getc (FILE *) |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
31 { |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
32 int tmp = available_char; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
33 available_char = 0; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
34 return tmp; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
35 } |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
36 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
37 static void redisplay (void) |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
38 { |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
39 if (command_window::the_command_window) |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
40 emit command_window::the_command_window->redisplay_signal (); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
41 } |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
42 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
43 static void prep_term (int) |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
44 { |
11
b652a5528fb1
handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents:
9
diff
changeset
|
45 // Anything to do here? |
9
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
46 } |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
47 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
48 static void deprep_term (void) |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
49 { |
11
b652a5528fb1
handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents:
9
diff
changeset
|
50 // Anything to do here? |
9
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
51 } |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
52 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
53 static void accept_line (char *line) |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
54 { |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
55 if (command_window::the_command_window) |
11
b652a5528fb1
handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents:
9
diff
changeset
|
56 { |
b652a5528fb1
handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents:
9
diff
changeset
|
57 if (! line) |
b652a5528fb1
handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents:
9
diff
changeset
|
58 command_window::the_command_window->eof (); |
b652a5528fb1
handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents:
9
diff
changeset
|
59 else |
b652a5528fb1
handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents:
9
diff
changeset
|
60 command_window::the_command_window->accept_line (line); |
b652a5528fb1
handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents:
9
diff
changeset
|
61 } |
9
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
62 } |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
63 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
64 static void display_completion_matches (char **matches, int num_matches, |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
65 int /* max_length */) |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
66 { |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
67 if (command_window::the_command_window) |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
68 { |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
69 std::ostringstream buf; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
70 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
71 if (num_matches > 1) |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
72 buf << "\n"; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
73 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
74 for (int i = 1; i < num_matches; i++) |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
75 buf << matches[i] << "\n"; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
76 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
77 command_window::the_command_window->insert_at_end (buf.str ()); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
78 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
79 emit command_window::the_command_window->redisplay_signal (); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
80 } |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
81 } |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
82 |
11
b652a5528fb1
handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents:
9
diff
changeset
|
83 static void readline_init (void) |
9
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
84 { |
14
1e5a1e15fa56
clean up header files, more small readline changes
John W. Eaton <jwe@octave.org>
parents:
12
diff
changeset
|
85 // What we really want here is a readline object that we could |
1e5a1e15fa56
clean up header files, more small readline changes
John W. Eaton <jwe@octave.org>
parents:
12
diff
changeset
|
86 // create in the command_window constructor. |
1e5a1e15fa56
clean up header files, more small readline changes
John W. Eaton <jwe@octave.org>
parents:
12
diff
changeset
|
87 |
1e5a1e15fa56
clean up header files, more small readline changes
John W. Eaton <jwe@octave.org>
parents:
12
diff
changeset
|
88 // We might also want the option of shared history? |
1e5a1e15fa56
clean up header files, more small readline changes
John W. Eaton <jwe@octave.org>
parents:
12
diff
changeset
|
89 |
9
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
90 rl_initialize (); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
91 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
92 rl_getc_function = getc; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
93 rl_redisplay_function = redisplay; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
94 rl_prep_term_function = prep_term; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
95 rl_deprep_term_function = deprep_term; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
96 rl_completion_display_matches_hook = display_completion_matches; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
97 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
98 rl_callback_handler_install (">> ", accept_line); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
99 } |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
100 |
11
b652a5528fb1
handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents:
9
diff
changeset
|
101 static void readline_fini (void) |
9
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
102 { |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
103 rl_callback_handler_remove (); |
14
1e5a1e15fa56
clean up header files, more small readline changes
John W. Eaton <jwe@octave.org>
parents:
12
diff
changeset
|
104 |
1e5a1e15fa56
clean up header files, more small readline changes
John W. Eaton <jwe@octave.org>
parents:
12
diff
changeset
|
105 rl_getc_function = nullptr; |
1e5a1e15fa56
clean up header files, more small readline changes
John W. Eaton <jwe@octave.org>
parents:
12
diff
changeset
|
106 rl_redisplay_function = nullptr; |
1e5a1e15fa56
clean up header files, more small readline changes
John W. Eaton <jwe@octave.org>
parents:
12
diff
changeset
|
107 rl_prep_term_function = nullptr; |
1e5a1e15fa56
clean up header files, more small readline changes
John W. Eaton <jwe@octave.org>
parents:
12
diff
changeset
|
108 rl_deprep_term_function = nullptr; |
1e5a1e15fa56
clean up header files, more small readline changes
John W. Eaton <jwe@octave.org>
parents:
12
diff
changeset
|
109 rl_completion_display_matches_hook = nullptr; |
1e5a1e15fa56
clean up header files, more small readline changes
John W. Eaton <jwe@octave.org>
parents:
12
diff
changeset
|
110 |
1e5a1e15fa56
clean up header files, more small readline changes
John W. Eaton <jwe@octave.org>
parents:
12
diff
changeset
|
111 // Is there a function that undoes readline initialization? |
9
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
112 } |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
113 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
114 // ^^^^^ readline ^^^^^ |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
115 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
116 command_window *command_window::the_command_window = nullptr; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
117 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
118 command_window::command_window (QWidget *p) |
17
2ddf3fe6fa33
initial attempt at using separate thread for interpreter
John W. Eaton <jwe@octave.org>
parents:
15
diff
changeset
|
119 : QTextEdit (p), |
2ddf3fe6fa33
initial attempt at using separate thread for interpreter
John W. Eaton <jwe@octave.org>
parents:
15
diff
changeset
|
120 #if defined (CALC_USE_INTERPRETER_THREAD) |
2ddf3fe6fa33
initial attempt at using separate thread for interpreter
John W. Eaton <jwe@octave.org>
parents:
15
diff
changeset
|
121 m_interpreter_thread (), |
2ddf3fe6fa33
initial attempt at using separate thread for interpreter
John W. Eaton <jwe@octave.org>
parents:
15
diff
changeset
|
122 #endif |
2ddf3fe6fa33
initial attempt at using separate thread for interpreter
John W. Eaton <jwe@octave.org>
parents:
15
diff
changeset
|
123 m_buffer (new QTextDocument ()), |
9
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
124 m_interpreter (new calc::qt_interpreter ()), |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
125 beg_mark (), prompt_mark () |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
126 { |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
127 if (the_command_window) |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
128 { |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
129 std::cerr << "multiple command_windows are not possible!" << std::endl; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
130 exit (1); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
131 } |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
132 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
133 the_command_window = this; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
134 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
135 setWindowTitle ("Qt::TextEdit example"); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
136 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
137 setMinimumSize (QSize (600, 400)); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
138 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
139 setDocument (m_buffer); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
140 |
17
2ddf3fe6fa33
initial attempt at using separate thread for interpreter
John W. Eaton <jwe@octave.org>
parents:
15
diff
changeset
|
141 #if defined (CALC_USE_INTERPRETER_THREAD) |
2ddf3fe6fa33
initial attempt at using separate thread for interpreter
John W. Eaton <jwe@octave.org>
parents:
15
diff
changeset
|
142 m_interpreter->moveToThread (&m_interpreter_thread); |
2ddf3fe6fa33
initial attempt at using separate thread for interpreter
John W. Eaton <jwe@octave.org>
parents:
15
diff
changeset
|
143 |
2ddf3fe6fa33
initial attempt at using separate thread for interpreter
John W. Eaton <jwe@octave.org>
parents:
15
diff
changeset
|
144 connect (&m_interpreter_thread, &QThread::finished, |
2ddf3fe6fa33
initial attempt at using separate thread for interpreter
John W. Eaton <jwe@octave.org>
parents:
15
diff
changeset
|
145 m_interpreter, &QObject::deleteLater); |
2ddf3fe6fa33
initial attempt at using separate thread for interpreter
John W. Eaton <jwe@octave.org>
parents:
15
diff
changeset
|
146 #endif |
2ddf3fe6fa33
initial attempt at using separate thread for interpreter
John W. Eaton <jwe@octave.org>
parents:
15
diff
changeset
|
147 |
9
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
148 connect (m_interpreter, SIGNAL (result_ready (double)), |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
149 this, SLOT (handle_result (double))); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
150 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
151 connect (m_interpreter, SIGNAL (error_signal (const QString&)), |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
152 this, SLOT (handle_error (const QString&))); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
153 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
154 connect (this, SIGNAL (input_char_available (int)), |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
155 this, SLOT (handle_input_char (int))); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
156 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
157 connect (this, SIGNAL (redisplay_signal (void)), |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
158 this, SLOT (redisplay (void))); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
159 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
160 connect (this, SIGNAL (accept_line_signal (const QString&)), |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
161 m_interpreter, SLOT (accept_input_line (const QString&))); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
162 |
11
b652a5528fb1
handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents:
9
diff
changeset
|
163 connect (this, SIGNAL (eof_signal (void)), |
b652a5528fb1
handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents:
9
diff
changeset
|
164 this, SLOT (close (void))); |
b652a5528fb1
handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents:
9
diff
changeset
|
165 |
9
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
166 insert_at_end |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
167 ("Qt Example Calculator.\n" |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
168 "Available operations: + - * / ^ ()\n" |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
169 "Semicolon terminates statement.\n" |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
170 "Up Arrow key moves to previous line in the command history.\n" |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
171 "Down Arrow key moves to next line in the comand history.\n\n"); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
172 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
173 beg_mark = set_mark (); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
174 |
11
b652a5528fb1
handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents:
9
diff
changeset
|
175 readline_init (); |
b652a5528fb1
handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents:
9
diff
changeset
|
176 |
17
2ddf3fe6fa33
initial attempt at using separate thread for interpreter
John W. Eaton <jwe@octave.org>
parents:
15
diff
changeset
|
177 #if defined (CALC_USE_INTERPRETER_THREAD) |
2ddf3fe6fa33
initial attempt at using separate thread for interpreter
John W. Eaton <jwe@octave.org>
parents:
15
diff
changeset
|
178 m_interpreter_thread.start (); |
2ddf3fe6fa33
initial attempt at using separate thread for interpreter
John W. Eaton <jwe@octave.org>
parents:
15
diff
changeset
|
179 #endif |
2ddf3fe6fa33
initial attempt at using separate thread for interpreter
John W. Eaton <jwe@octave.org>
parents:
15
diff
changeset
|
180 |
9
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
181 // Defer initializing and executing the interpreter until after the main |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
182 // window and QApplication are running to prevent race conditions |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
183 QTimer::singleShot (0, m_interpreter, SLOT (execute (void))); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
184 } |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
185 |
11
b652a5528fb1
handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents:
9
diff
changeset
|
186 command_window::~command_window (void) |
b652a5528fb1
handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents:
9
diff
changeset
|
187 { |
b652a5528fb1
handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents:
9
diff
changeset
|
188 readline_fini (); |
17
2ddf3fe6fa33
initial attempt at using separate thread for interpreter
John W. Eaton <jwe@octave.org>
parents:
15
diff
changeset
|
189 |
2ddf3fe6fa33
initial attempt at using separate thread for interpreter
John W. Eaton <jwe@octave.org>
parents:
15
diff
changeset
|
190 #if defined (CALC_USE_INTERPRETER_THREAD) |
2ddf3fe6fa33
initial attempt at using separate thread for interpreter
John W. Eaton <jwe@octave.org>
parents:
15
diff
changeset
|
191 m_interpreter_thread.quit (); |
2ddf3fe6fa33
initial attempt at using separate thread for interpreter
John W. Eaton <jwe@octave.org>
parents:
15
diff
changeset
|
192 m_interpreter_thread.wait (); |
2ddf3fe6fa33
initial attempt at using separate thread for interpreter
John W. Eaton <jwe@octave.org>
parents:
15
diff
changeset
|
193 #endif |
11
b652a5528fb1
handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents:
9
diff
changeset
|
194 } |
b652a5528fb1
handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents:
9
diff
changeset
|
195 |
9
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
196 // Accept an input line, parse and possibly execute it. |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
197 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
198 void command_window::accept_line (const std::string& line) |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
199 { |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
200 insert_at_end ("\n"); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
201 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
202 if (! line.empty ()) |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
203 { |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
204 add_history (line.c_str ()); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
205 using_history (); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
206 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
207 emit accept_line_signal (QString::fromStdString (line)); |
17
2ddf3fe6fa33
initial attempt at using separate thread for interpreter
John W. Eaton <jwe@octave.org>
parents:
15
diff
changeset
|
208 |
2ddf3fe6fa33
initial attempt at using separate thread for interpreter
John W. Eaton <jwe@octave.org>
parents:
15
diff
changeset
|
209 // FIXME: how to wait until interpreter is finished and |
2ddf3fe6fa33
initial attempt at using separate thread for interpreter
John W. Eaton <jwe@octave.org>
parents:
15
diff
changeset
|
210 // results are printed before returning to readline callback |
2ddf3fe6fa33
initial attempt at using separate thread for interpreter
John W. Eaton <jwe@octave.org>
parents:
15
diff
changeset
|
211 // that will print the next prompt? |
9
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
212 } |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
213 } |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
214 |
11
b652a5528fb1
handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents:
9
diff
changeset
|
215 void command_window::eof (void) |
b652a5528fb1
handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents:
9
diff
changeset
|
216 { |
b652a5528fb1
handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents:
9
diff
changeset
|
217 emit eof_signal (); |
b652a5528fb1
handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents:
9
diff
changeset
|
218 } |
b652a5528fb1
handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents:
9
diff
changeset
|
219 |
9
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
220 void command_window::insert_at_end (const std::string& text) |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
221 { |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
222 scroll_to_bottom (); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
223 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
224 insert_at_cursor (text); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
225 } |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
226 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
227 void command_window::handle_error (const QString& msg) |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
228 { |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
229 insert_at_end ("parse error: " + msg.toStdString () + "\n"); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
230 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
231 rl_abort (0, 0); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
232 } |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
233 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
234 // FIXME: do we really need this extra function? |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
235 void command_window::handle_result (double value) |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
236 { |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
237 insert_result (value); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
238 } |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
239 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
240 // Redisplay current command line. |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
241 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
242 void command_window::redisplay (void) |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
243 { |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
244 erase_line (); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
245 |
12
894be158b32d
define parser as a class and eliminate some global variables
John W. Eaton <jwe@octave.org>
parents:
11
diff
changeset
|
246 interpreter& interp = m_interpreter->get_interpreter (); |
894be158b32d
define parser as a class and eliminate some global variables
John W. Eaton <jwe@octave.org>
parents:
11
diff
changeset
|
247 parser& parser = interp.get_parser (); |
894be158b32d
define parser as a class and eliminate some global variables
John W. Eaton <jwe@octave.org>
parents:
11
diff
changeset
|
248 |
9
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
249 std::string line = rl_line_buffer ? rl_line_buffer : ""; |
15
79783f3e2017
use rl_display_prompt instead of rl_prompt
John W. Eaton <jwe@octave.org>
parents:
14
diff
changeset
|
250 std::string prompt = parser.beg_of_stmt () ? rl_display_prompt : ""; |
9
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
251 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
252 insert_line (prompt, line); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
253 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
254 scroll_to_bottom (); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
255 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
256 QTextCursor cursor = textCursor (); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
257 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
258 cursor.setPosition (prompt_mark + rl_point, QTextCursor::MoveAnchor); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
259 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
260 setTextCursor (cursor); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
261 } |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
262 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
263 void command_window::keyPressEvent (QKeyEvent *event) |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
264 { |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
265 if (! event) |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
266 return; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
267 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
268 if (event->type () == QEvent::KeyPress) |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
269 { |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
270 int key = event->key (); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
271 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
272 switch (key) |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
273 { |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
274 case Qt::Key_Return: |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
275 key = 0x0A; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
276 break; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
277 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
278 case Qt::Key_Backspace: |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
279 key = 0x08; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
280 break; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
281 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
282 case Qt::Key_Tab: |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
283 key = 0x09; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
284 break; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
285 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
286 case Qt::Key_Escape: |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
287 key = 0x1b; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
288 break; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
289 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
290 case Qt::Key_Up: |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
291 case Qt::Key_Down: |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
292 case Qt::Key_Right: |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
293 case Qt::Key_Left: |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
294 key = do_arrow_key (key); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
295 break; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
296 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
297 default: |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
298 { |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
299 switch (event->modifiers ()) |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
300 { |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
301 case Qt::ControlModifier: |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
302 if (key > 0x3f && key < 0x7b) |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
303 key &= 0x1f; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
304 else |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
305 key = -1; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
306 break; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
307 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
308 default: |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
309 { |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
310 // Don't shoot me, this is just a demo... |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
311 QString text = event->text (); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
312 QByteArray latin_text = text.toLatin1 (); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
313 key = latin_text[0]; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
314 } |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
315 break; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
316 } |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
317 } |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
318 break; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
319 } |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
320 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
321 if (key >= 0) |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
322 emit input_char_available (key); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
323 } |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
324 } |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
325 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
326 void command_window::handle_input_char (int key) |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
327 { |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
328 available_char = key; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
329 rl_callback_read_char (); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
330 } |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
331 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
332 int command_window::do_arrow_key (int arrow_key) |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
333 { |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
334 int retval = 0; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
335 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
336 available_char = 0x1b; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
337 rl_callback_read_char (); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
338 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
339 available_char = '['; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
340 rl_callback_read_char (); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
341 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
342 switch (arrow_key) |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
343 { |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
344 case Qt::Key_Up: |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
345 retval = 'A'; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
346 break; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
347 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
348 case Qt::Key_Down: |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
349 retval = 'B'; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
350 break; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
351 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
352 case Qt::Key_Right: |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
353 retval = 'C'; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
354 break; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
355 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
356 case Qt::Key_Left: |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
357 retval = 'D'; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
358 break; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
359 } |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
360 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
361 return retval; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
362 } |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
363 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
364 // Retrieve a command from the history list and insert it on the |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
365 // current command. |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
366 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
367 void command_window::history (bool up) |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
368 { |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
369 HIST_ENTRY *entry = up ? previous_history () : next_history (); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
370 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
371 if (entry) |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
372 { |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
373 erase_line (); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
374 |
15
79783f3e2017
use rl_display_prompt instead of rl_prompt
John W. Eaton <jwe@octave.org>
parents:
14
diff
changeset
|
375 std::string prompt = rl_display_prompt; |
9
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
376 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
377 insert_line (prompt, entry->line); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
378 } |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
379 else if (! up) |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
380 erase_line (); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
381 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
382 scroll_to_bottom (); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
383 } |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
384 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
385 void command_window::erase_line (void) |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
386 { |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
387 QTextCursor cursor = textCursor (); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
388 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
389 cursor.movePosition (QTextCursor::End); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
390 cursor.select (QTextCursor::LineUnderCursor); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
391 cursor.removeSelectedText (); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
392 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
393 setTextCursor (cursor); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
394 } |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
395 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
396 void command_window::insert_at_cursor (const std::string& text) |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
397 { |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
398 QTextCursor cursor = textCursor (); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
399 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
400 cursor.insertText (QString::fromStdString (text)); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
401 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
402 setTextCursor (cursor); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
403 } |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
404 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
405 void command_window::insert_line (const std::string& prompt, |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
406 const std::string& line) |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
407 { |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
408 beg_mark = set_mark (); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
409 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
410 insert_at_cursor (prompt); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
411 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
412 prompt_mark = set_mark (); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
413 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
414 insert_at_cursor (line); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
415 } |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
416 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
417 int command_window::set_mark (void) |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
418 { |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
419 return textCursor ().position (); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
420 } |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
421 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
422 void command_window::insert_result (double value) |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
423 { |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
424 std::ostringstream buf; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
425 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
426 buf << "ans = " << value << "\n"; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
427 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
428 insert_at_cursor (buf.str ()); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
429 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
430 beg_mark = set_mark (); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
431 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
432 scroll_to_bottom (); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
433 } |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
434 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
435 void command_window::scroll_to_bottom (void) |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
436 { |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
437 QTextCursor cursor = textCursor (); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
438 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
439 cursor.movePosition (QTextCursor::End); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
440 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
441 setTextCursor (cursor); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
442 } |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
443 } |