-
Notifications
You must be signed in to change notification settings - Fork 570
/
context.cc
271 lines (241 loc) · 6.4 KB
/
context.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// vim: set sts=2 sw=2 et:
// encoding: utf-8
//
// Copyleft 2011 RIME Developers
// License: GPLv3
//
// 2011-05-08 GONG Chen <chen.sst@gmail.com>
//
#include <rime/candidate.h>
#include <rime/context.h>
#include <rime/composition.h>
#include <rime/menu.h>
#include <rime/segmentation.h>
namespace rime {
Context::Context() : input_(), caret_pos_(0), composition_(new Composition) {
}
Context::~Context() {
}
bool Context::Commit() {
if (!IsComposing())
return false;
// notify the engine and interesting components
commit_notifier_(this);
// record commit history
commit_history_.Push(*composition_, input_);
// start over
Clear();
return true;
}
const std::string Context::GetCommitText() const {
return composition_->GetCommitText();
}
const std::string Context::GetScriptText() const {
return composition_->GetScriptText();
}
void Context::GetPreedit(Preedit *preedit) const {
if (!prompt_.empty()) {
preedit->text = prompt_;
preedit->caret_pos = prompt_.length();
preedit->sel_start = 0;
preedit->sel_end = prompt_.length();
return;
}
composition_->GetPreedit(preedit);
preedit->caret_pos = preedit->text.length();
if (IsComposing()) {
// TODO: use schema settings
const std::string caret("\xe2\x80\xba");
preedit->text.append(caret);
if (caret_pos_ < input_.length()) {
preedit->text.append(input_.substr(caret_pos_));
}
}
}
bool Context::IsComposing() const {
return !input_.empty();
}
bool Context::HasMenu() const {
return !composition_->empty() && composition_->back().menu;
}
bool Context::PushInput(char ch) {
if (caret_pos_ >= input_.length()) {
input_.push_back(ch);
caret_pos_ = input_.length();
}
else {
input_.insert(caret_pos_, 1, ch);
++caret_pos_;
}
update_notifier_(this);
return true;
}
bool Context::PopInput() {
if (caret_pos_ == 0)
return false;
--caret_pos_;
input_.erase(caret_pos_, 1);
update_notifier_(this);
return true;
}
bool Context::DeleteInput() {
if (caret_pos_ >= input_.length())
return false;
input_.erase(caret_pos_, 1);
update_notifier_(this);
return true;
}
void Context::Clear() {
prompt_.clear();
input_.clear();
caret_pos_ = 0;
composition_->clear();
update_notifier_(this);
}
bool Context::Select(size_t index) {
if (composition_->empty())
return false;
Segment &seg(composition_->back());
shared_ptr<Candidate> cand(seg.GetCandidateAt(index));
if (cand) {
seg.selected_index = index;
seg.status = Segment::kSelected;
EZDBGONLYLOGGERPRINT("Selected: '%s', index = %d.",
cand->text().c_str(), index);
select_notifier_(this);
return true;
}
return false;
}
bool Context::DeleteCurrentSelection() {
if (composition_->empty())
return false;
Segment &seg(composition_->back());
shared_ptr<Candidate> cand(seg.GetSelectedCandidate());
if (cand) {
EZDBGONLYLOGGERPRINT("Deleting: '%s', selected_index = %d.",
cand->text().c_str(), seg.selected_index);
delete_notifier_(this);
return true; // CAVEAT: this doesn't mean anything is deleted for sure
}
return false;
}
bool Context::ConfirmCurrentSelection() {
if (composition_->empty())
return false;
Segment &seg(composition_->back());
shared_ptr<Candidate> cand(seg.GetSelectedCandidate());
if (cand) {
seg.status = Segment::kSelected;
EZDBGONLYLOGGERPRINT("Confirmed: '%s', selected_index = %d.",
cand->text().c_str(), seg.selected_index);
select_notifier_(this);
return true;
}
return false;
}
bool Context::ConfirmPreviousSelection() {
EZDBGONLYLOGGERFUNCTRACKER;
for (Composition::reverse_iterator it = composition_->rbegin();
it != composition_->rend(); ++it) {
if (it->status > Segment::kSelected) return false;
if (it->status == Segment::kSelected) {
it->status = Segment::kConfirmed;
return true;
}
}
return false;
}
bool Context::ReopenPreviousSegment() {
EZDBGONLYLOGGERFUNCTRACKER;
if (composition_->Trim()) {
if (!composition_->empty() &&
composition_->back().status >= Segment::kSelected) {
composition_->back().status = Segment::kVoid;
}
update_notifier_(this);
return true;
}
return false;
}
bool Context::ClearPreviousSegment() {
EZDBGONLYLOGGERFUNCTRACKER;
if (composition_->empty())
return false;
size_t where = composition_->back().start;
if (where >= input_.length())
return false;
set_input(input_.substr(0, where));
return true;
}
bool Context::ReopenPreviousSelection() {
EZDBGONLYLOGGERFUNCTRACKER;
for (Composition::reverse_iterator it = composition_->rbegin();
it != composition_->rend(); ++it) {
if (it->status > Segment::kSelected) return false;
if (it->status == Segment::kSelected) {
it->status = Segment::kVoid;
while (it != composition_->rbegin())
composition_->pop_back();
update_notifier_(this);
return true;
}
}
return false;
}
bool Context::ClearNonConfirmedComposition() {
bool reverted = false;
while (!composition_->empty() &&
composition_->back().status < Segment::kSelected) {
composition_->pop_back();
reverted = true;
}
if (reverted) {
composition_->Forward();
EZDBGONLYLOGGERVAR(composition_->GetDebugText());
}
return reverted;
}
bool Context::RefreshNonConfirmedComposition() {
EZDBGONLYLOGGERFUNCTRACKER;
if (ClearNonConfirmedComposition()) {
update_notifier_(this);
return true;
}
return false;
}
void Context::set_caret_pos(size_t caret_pos) {
if (caret_pos > input_.length())
caret_pos_ = input_.length();
else
caret_pos_ = caret_pos;
update_notifier_(this);
}
void Context::set_composition(Composition *comp) {
if (composition_.get() != comp)
composition_.reset(comp);
// TODO: notification
}
void Context::set_input(const std::string &value) {
input_ = value;
caret_pos_ = input_.length();
update_notifier_(this);
}
Composition* Context::composition() {
return composition_.get();
}
const Composition* Context::composition() const {
return composition_.get();
}
void Context::set_option(const std::string &name, bool value) {
options_[name] = value;
option_update_notifier_(this, name);
}
bool Context::get_option(const std::string &name) const {
std::map<std::string, bool>::const_iterator it = options_.find(name);
if (it != options_.end())
return it->second;
else
return false;
}
} // namespace rime