Skip to content

Commit

Permalink
Add speller/auto_clear
Browse files Browse the repository at this point in the history
This closes rime#60
  • Loading branch information
osfans committed Nov 3, 2015
1 parent 4734375 commit 8dde91a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
4 changes: 4 additions & 0 deletions include/rime/gear/speller.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@ class Speller : public Processor {
virtual ProcessResult ProcessKeyEvent(const KeyEvent& key_event);

protected:
enum AutoClearMethod { kClearNone, kClearAuto, kClearManual, kClearMaxLength };

bool AutoSelectAtMaxCodeLength(Context* ctx);
bool AutoSelectUniqueCandidate(Context* ctx);
bool AutoSelectPreviousMatch(Context* ctx, Segment* previous_segment);
bool FindEarlierMatch(Context* ctx, size_t start, size_t end);
bool AutoClear(Context* ctx);

string alphabet_;
string delimiters_;
Expand All @@ -37,6 +40,7 @@ class Speller : public Processor {
bool auto_select_ = false;
bool use_space_ = false;
boost::regex auto_select_pattern_;
AutoClearMethod auto_clear_ = kClearNone;
};

} // namespace rime
Expand Down
22 changes: 22 additions & 0 deletions src/gear/speller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ Speller::Speller(const Ticket& ticket) : Processor(ticket),
if (config->GetString("speller/auto_select_pattern", &pattern)) {
auto_select_pattern_ = pattern;
}
string auto_clear;
if (config->GetString("speller/auto_clear", &auto_clear)) {
if (auto_clear == "auto") auto_clear_ = kClearAuto;
else if (auto_clear == "manual") auto_clear_ = kClearManual;
else if (auto_clear == "max_length") auto_clear_ = kClearMaxLength;
}
}
if (initials_.empty()) {
initials_ = alphabet_;
Expand All @@ -99,6 +105,9 @@ ProcessResult Speller::ProcessKeyEvent(const KeyEvent& key_event) {
if (is_initial && AutoSelectAtMaxCodeLength(ctx)) {
DLOG(INFO) << "auto-select at max code length.";
}
else if ((auto_clear_ == kClearMaxLength || auto_clear_ == kClearManual) && AutoClear(ctx)) {
DLOG(INFO) << "auto-clear at max code when no candidate.";
}
// make a backup of previous conversion before modifying input
Segment previous_segment;
if (auto_select_ && ctx->HasMenu()) {
Expand All @@ -120,6 +129,9 @@ ProcessResult Speller::ProcessKeyEvent(const KeyEvent& key_event) {
if (AutoSelectUniqueCandidate(ctx)) {
DLOG(INFO) << "auto-select unique candidate.";
}
else if (auto_clear_ == kClearAuto && AutoClear(ctx)) {
DLOG(INFO) << "auto-clear when no candidate.";
}
return kAccepted;
}

Expand Down Expand Up @@ -198,6 +210,16 @@ bool Speller::AutoSelectPreviousMatch(Context* ctx,
return FindEarlierMatch(ctx, start ,end);
}

bool Speller::AutoClear(Context* ctx) {
if (!ctx->HasMenu() && auto_clear_ > kClearNone &&
(auto_clear_ != kClearMaxLength || max_code_length_ == 0 ||
ctx->input().length() >= max_code_length_)) {
ctx->Clear();
return true;
}
return false;
}

bool Speller::FindEarlierMatch(Context* ctx, size_t start, size_t end) {
if (end <= start + 1)
return false;
Expand Down

0 comments on commit 8dde91a

Please sign in to comment.