annotate command-window.cpp @ 12:894be158b32d

define parser as a class and eliminate some global variables
author John W. Eaton <jwe@octave.org>
date Thu, 23 May 2019 17:57:20 -0400
parents b652a5528fb1
children 1e5a1e15fa56
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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 <cstdlib>
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
6 #include <cstring>
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
7
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
8 #include <QApplication>
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
9 #include <QKeyEvent>
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
10 #include <QTextDocument>
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
11 #include <QTextEdit>
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
12 #include <QTimer>
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
13
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
14 #include "command-window.h"
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
15 #include "interpreter.h"
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
16 #include "parser.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 #include <readline/readline.h>
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
19 #include <readline/history.h>
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
20
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
21 namespace calc
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
22 {
11
b652a5528fb1 handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents: 9
diff changeset
23 // 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
24 // 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
25 // 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
26 // 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
27 // contain the next available character.
b652a5528fb1 handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents: 9
diff changeset
28
9
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
29 static int available_char = 0;
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
30
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
31 // vvvvv readline vvvvv
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
32
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
33 static int getc (FILE *)
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
34 {
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
35 int tmp = available_char;
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
36 available_char = 0;
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
37 return tmp;
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
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
40 static void redisplay (void)
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 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
43 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
44 }
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
45
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
46 static void prep_term (int)
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
47 {
11
b652a5528fb1 handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents: 9
diff changeset
48 // Anything to do here?
9
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
49 }
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
50
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
51 static void deprep_term (void)
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
52 {
11
b652a5528fb1 handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents: 9
diff changeset
53 // Anything to do here?
9
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
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
56 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
57 {
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
58 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
59 {
b652a5528fb1 handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents: 9
diff changeset
60 if (! line)
b652a5528fb1 handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents: 9
diff changeset
61 command_window::the_command_window->eof ();
b652a5528fb1 handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents: 9
diff changeset
62 else
b652a5528fb1 handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents: 9
diff changeset
63 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
64 }
9
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
65 }
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 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
68 int /* max_length */)
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
69 {
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
70 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
71 {
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
72 std::ostringstream buf;
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 if (num_matches > 1)
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
75 buf << "\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 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
78 buf << matches[i] << "\n";
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
79
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
80 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
81
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
82 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
83 }
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
84 }
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
85
11
b652a5528fb1 handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents: 9
diff changeset
86 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
87 {
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
88 rl_initialize ();
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
89
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
90 rl_getc_function = getc;
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
91 rl_redisplay_function = redisplay;
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
92 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
93 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
94 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
95
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
96 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
97 }
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
98
11
b652a5528fb1 handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents: 9
diff changeset
99 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
100 {
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
101 rl_callback_handler_remove ();
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
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
104 // ^^^^^ readline ^^^^^
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
105
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
106 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
107
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
108 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
109 : 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
110 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
111 beg_mark (), prompt_mark ()
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 if (the_command_window)
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
114 {
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
115 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
116 exit (1);
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
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
119 the_command_window = this;
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
120
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
121 setWindowTitle ("Qt::TextEdit example");
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 setMinimumSize (QSize (600, 400));
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 setDocument (m_buffer);
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 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
128 this, SLOT (handle_result (double)));
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
129
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
130 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
131 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
132
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
133 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
134 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
135
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
136 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
137 this, SLOT (redisplay (void)));
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 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
140 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
141
11
b652a5528fb1 handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents: 9
diff changeset
142 connect (this, SIGNAL (eof_signal (void)),
b652a5528fb1 handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents: 9
diff changeset
143 this, SLOT (close (void)));
b652a5528fb1 handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents: 9
diff changeset
144
9
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
145 insert_at_end
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
146 ("Qt Example Calculator.\n"
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
147 "Available operations: + - * / ^ ()\n"
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
148 "Semicolon terminates statement.\n"
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
149 "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
150 "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
151
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
152 beg_mark = set_mark ();
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
153
11
b652a5528fb1 handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents: 9
diff changeset
154 readline_init ();
b652a5528fb1 handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents: 9
diff changeset
155
9
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
156 // 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
157 // 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
158 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
159 }
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
160
11
b652a5528fb1 handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents: 9
diff changeset
161 command_window::~command_window (void)
b652a5528fb1 handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents: 9
diff changeset
162 {
b652a5528fb1 handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents: 9
diff changeset
163 readline_fini ();
b652a5528fb1 handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents: 9
diff changeset
164 }
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 // 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
167
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
168 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
169 {
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
170 insert_at_end ("\n");
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
171
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
172 if (! line.empty ())
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
173 {
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
174 add_history (line.c_str ());
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
175 using_history ();
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
176
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
177 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
178 }
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
11
b652a5528fb1 handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents: 9
diff changeset
181 void command_window::eof (void)
b652a5528fb1 handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents: 9
diff changeset
182 {
b652a5528fb1 handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents: 9
diff changeset
183 emit eof_signal ();
b652a5528fb1 handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents: 9
diff changeset
184 }
b652a5528fb1 handle EOF on input; more misc refactoring
John W. Eaton <jwe@octave.org>
parents: 9
diff changeset
185
9
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
186 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
187 {
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
188 scroll_to_bottom ();
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 insert_at_cursor (text);
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
191 }
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
192
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
193 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
194 {
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
195 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
196
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
197 rl_abort (0, 0);
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
198 }
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 // 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
201 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
202 {
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
203 insert_result (value);
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
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
206 // Redisplay current command line.
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
207
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
208 void command_window::redisplay (void)
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 erase_line ();
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
211
12
894be158b32d define parser as a class and eliminate some global variables
John W. Eaton <jwe@octave.org>
parents: 11
diff changeset
212 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
213 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
214
9
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
215 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
216 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
217
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
218 insert_line (prompt, line);
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 scroll_to_bottom ();
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 QTextCursor cursor = textCursor ();
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 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
225
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
226 setTextCursor (cursor);
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
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
229 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
230 {
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
231 if (! event)
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
232 return;
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 if (event->type () == QEvent::KeyPress)
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 int key = event->key ();
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 switch (key)
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 case Qt::Key_Return:
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
241 key = 0x0A;
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
242 break;
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 case Qt::Key_Backspace:
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
245 key = 0x08;
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
246 break;
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 case Qt::Key_Tab:
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
249 key = 0x09;
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
250 break;
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 case Qt::Key_Escape:
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
253 key = 0x1b;
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
254 break;
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 case Qt::Key_Up:
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
257 case Qt::Key_Down:
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
258 case Qt::Key_Right:
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
259 case Qt::Key_Left:
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
260 key = do_arrow_key (key);
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
261 break;
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 default:
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 switch (event->modifiers ())
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
266 {
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
267 case Qt::ControlModifier:
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
268 if (key > 0x3f && key < 0x7b)
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
269 key &= 0x1f;
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
270 else
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
271 key = -1;
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
272 break;
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 default:
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
275 {
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
276 // 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
277 QString text = event->text ();
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
278 QByteArray latin_text = text.toLatin1 ();
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
279 key = latin_text[0];
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
280 }
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
281 break;
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
282 }
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 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
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
287 if (key >= 0)
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
288 emit input_char_available (key);
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 }
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
291
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
292 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
293 {
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
294 available_char = key;
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
295 rl_callback_read_char ();
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
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
298 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
299 {
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
300 int retval = 0;
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 available_char = 0x1b;
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
303 rl_callback_read_char ();
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
304
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
305 available_char = '[';
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
306 rl_callback_read_char ();
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 switch (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 case Qt::Key_Up:
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
311 retval = 'A';
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
312 break;
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
313
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
314 case Qt::Key_Down:
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
315 retval = 'B';
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
316 break;
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 case Qt::Key_Right:
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
319 retval = 'C';
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
320 break;
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
321
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
322 case Qt::Key_Left:
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
323 retval = 'D';
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
324 break;
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
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
327 return retval;
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
328 }
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
329
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
330 // 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
331 // current command.
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
332
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
333 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
334 {
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
335 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
336
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
337 if (entry)
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 erase_line ();
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
340
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
341 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
342
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
343 insert_line (prompt, entry->line);
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 else if (! up)
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
346 erase_line ();
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 scroll_to_bottom ();
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
349 }
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 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
352 {
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
353 QTextCursor cursor = textCursor ();
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 cursor.movePosition (QTextCursor::End);
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
356 cursor.select (QTextCursor::LineUnderCursor);
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
357 cursor.removeSelectedText ();
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
358
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
359 setTextCursor (cursor);
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
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
362 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
363 {
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
364 QTextCursor cursor = textCursor ();
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
365
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
366 cursor.insertText (QString::fromStdString (text));
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
367
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
368 setTextCursor (cursor);
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
369 }
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 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
372 const std::string& line)
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 beg_mark = set_mark ();
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 insert_at_cursor (prompt);
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 prompt_mark = set_mark ();
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 insert_at_cursor (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
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
383 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
384 {
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
385 return textCursor ().position ();
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
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
388 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
389 {
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
390 std::ostringstream buf;
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 buf << "ans = " << value << "\n";
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
393
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
394 insert_at_cursor (buf.str ());
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 beg_mark = set_mark ();
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 scroll_to_bottom ();
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
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
401 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
402 {
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
403 QTextCursor cursor = textCursor ();
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 cursor.movePosition (QTextCursor::End);
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
406
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
407 setTextCursor (cursor);
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
408 }
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
diff changeset
409 }