Mercurial > jwe > qt-gui-with-push-parser
annotate command-window.cpp @ 14:1e5a1e15fa56
clean up header files, more small readline changes
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 23 May 2019 18:41:04 -0400 |
parents | 894be158b32d |
children | 79783f3e2017 |
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) |
11
b652a5528fb1
handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents:
9
diff
changeset
|
119 : QTextEdit (p), m_buffer (new QTextDocument ()), |
9
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
120 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
|
121 beg_mark (), prompt_mark () |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
122 { |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
123 if (the_command_window) |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
124 { |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
125 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
|
126 exit (1); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
127 } |
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 the_command_window = this; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
130 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
131 setWindowTitle ("Qt::TextEdit example"); |
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 setMinimumSize (QSize (600, 400)); |
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 setDocument (m_buffer); |
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 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
|
138 this, SLOT (handle_result (double))); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
139 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
140 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
|
141 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
|
142 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
143 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
|
144 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
|
145 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
146 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
|
147 this, SLOT (redisplay (void))); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
148 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
149 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
|
150 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
|
151 |
11
b652a5528fb1
handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents:
9
diff
changeset
|
152 connect (this, SIGNAL (eof_signal (void)), |
b652a5528fb1
handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents:
9
diff
changeset
|
153 this, SLOT (close (void))); |
b652a5528fb1
handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents:
9
diff
changeset
|
154 |
9
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
155 insert_at_end |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
156 ("Qt Example Calculator.\n" |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
157 "Available operations: + - * / ^ ()\n" |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
158 "Semicolon terminates statement.\n" |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
159 "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
|
160 "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
|
161 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
162 beg_mark = set_mark (); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
163 |
11
b652a5528fb1
handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents:
9
diff
changeset
|
164 readline_init (); |
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 // 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
|
167 // 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
|
168 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
|
169 } |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
170 |
11
b652a5528fb1
handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents:
9
diff
changeset
|
171 command_window::~command_window (void) |
b652a5528fb1
handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents:
9
diff
changeset
|
172 { |
b652a5528fb1
handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents:
9
diff
changeset
|
173 readline_fini (); |
b652a5528fb1
handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents:
9
diff
changeset
|
174 } |
b652a5528fb1
handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents:
9
diff
changeset
|
175 |
9
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
176 // 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
|
177 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
178 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
|
179 { |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
180 insert_at_end ("\n"); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
181 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
182 if (! line.empty ()) |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
183 { |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
184 add_history (line.c_str ()); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
185 using_history (); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
186 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
187 emit accept_line_signal (QString::fromStdString (line)); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
188 } |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
189 } |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
190 |
11
b652a5528fb1
handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents:
9
diff
changeset
|
191 void command_window::eof (void) |
b652a5528fb1
handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents:
9
diff
changeset
|
192 { |
b652a5528fb1
handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents:
9
diff
changeset
|
193 emit eof_signal (); |
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 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
|
197 { |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
198 scroll_to_bottom (); |
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_cursor (text); |
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 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
203 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
|
204 { |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
205 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
|
206 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
207 rl_abort (0, 0); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
208 } |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
209 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
210 // 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
|
211 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
|
212 { |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
213 insert_result (value); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
214 } |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
215 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
216 // Redisplay current command line. |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
217 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
218 void command_window::redisplay (void) |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
219 { |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
220 erase_line (); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
221 |
12
894be158b32d
define parser as a class and eliminate some global variables
John W. Eaton <jwe@octave.org>
parents:
11
diff
changeset
|
222 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
|
223 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
|
224 |
9
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
225 std::string line = rl_line_buffer ? rl_line_buffer : ""; |
12
894be158b32d
define parser as a class and eliminate some global variables
John W. Eaton <jwe@octave.org>
parents:
11
diff
changeset
|
226 std::string prompt = (rl_prompt && parser.beg_of_stmt ()) ? rl_prompt : ""; |
9
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
227 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
228 insert_line (prompt, line); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
229 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
230 scroll_to_bottom (); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
231 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
232 QTextCursor cursor = textCursor (); |
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 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
|
235 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
236 setTextCursor (cursor); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
237 } |
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 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
|
240 { |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
241 if (! event) |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
242 return; |
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 if (event->type () == QEvent::KeyPress) |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
245 { |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
246 int key = event->key (); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
247 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
248 switch (key) |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
249 { |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
250 case Qt::Key_Return: |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
251 key = 0x0A; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
252 break; |
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 case Qt::Key_Backspace: |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
255 key = 0x08; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
256 break; |
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 case Qt::Key_Tab: |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
259 key = 0x09; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
260 break; |
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 case Qt::Key_Escape: |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
263 key = 0x1b; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
264 break; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
265 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
266 case Qt::Key_Up: |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
267 case Qt::Key_Down: |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
268 case Qt::Key_Right: |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
269 case Qt::Key_Left: |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
270 key = do_arrow_key (key); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
271 break; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
272 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
273 default: |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
274 { |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
275 switch (event->modifiers ()) |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
276 { |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
277 case Qt::ControlModifier: |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
278 if (key > 0x3f && key < 0x7b) |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
279 key &= 0x1f; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
280 else |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
281 key = -1; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
282 break; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
283 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
284 default: |
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 // 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
|
287 QString text = event->text (); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
288 QByteArray latin_text = text.toLatin1 (); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
289 key = latin_text[0]; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
290 } |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
291 break; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
292 } |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
293 } |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
294 break; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
295 } |
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 if (key >= 0) |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
298 emit input_char_available (key); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
299 } |
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 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
302 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
|
303 { |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
304 available_char = key; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
305 rl_callback_read_char (); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
306 } |
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 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
|
309 { |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
310 int retval = 0; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
311 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
312 available_char = 0x1b; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
313 rl_callback_read_char (); |
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 available_char = '['; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
316 rl_callback_read_char (); |
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 switch (arrow_key) |
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 case Qt::Key_Up: |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
321 retval = 'A'; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
322 break; |
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 case Qt::Key_Down: |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
325 retval = 'B'; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
326 break; |
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 case Qt::Key_Right: |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
329 retval = 'C'; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
330 break; |
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 case Qt::Key_Left: |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
333 retval = 'D'; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
334 break; |
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 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
337 return retval; |
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 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
340 // 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
|
341 // current command. |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
342 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
343 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
|
344 { |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
345 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
|
346 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
347 if (entry) |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
348 { |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
349 erase_line (); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
350 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
351 std::string prompt = rl_prompt ? rl_prompt : ""; |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
352 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
353 insert_line (prompt, entry->line); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
354 } |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
355 else if (! up) |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
356 erase_line (); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
357 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
358 scroll_to_bottom (); |
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 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
|
362 { |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
363 QTextCursor cursor = textCursor (); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
364 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
365 cursor.movePosition (QTextCursor::End); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
366 cursor.select (QTextCursor::LineUnderCursor); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
367 cursor.removeSelectedText (); |
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 setTextCursor (cursor); |
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 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
372 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
|
373 { |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
374 QTextCursor cursor = textCursor (); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
375 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
376 cursor.insertText (QString::fromStdString (text)); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
377 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
378 setTextCursor (cursor); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
379 } |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
380 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
381 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
|
382 const std::string& line) |
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 beg_mark = set_mark (); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
385 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
386 insert_at_cursor (prompt); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
387 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
388 prompt_mark = set_mark (); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
389 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
390 insert_at_cursor (line); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
391 } |
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 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
|
394 { |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
395 return textCursor ().position (); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
396 } |
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 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
|
399 { |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
400 std::ostringstream buf; |
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 buf << "ans = " << value << "\n"; |
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 insert_at_cursor (buf.str ()); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
405 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
406 beg_mark = set_mark (); |
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 scroll_to_bottom (); |
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 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
411 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
|
412 { |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
413 QTextCursor cursor = textCursor (); |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
414 |
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
415 cursor.movePosition (QTextCursor::End); |
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 setTextCursor (cursor); |
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 } |