Debian Codegraph @

modules/punctuation/punctuation.cpp

raw
1 /*
2 * SPDX-FileCopyrightText: 2017-2017 CSSlayer <[email protected]>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-or-later
5 *
6 */
7 #include "punctuation.h"
8 #include "notifications_public.h"
9 #include <algorithm>
10 #include <cstdint>
11 #include <exception>
12 #include <fcitx-config/iniparser.h>
13 #include <fcitx-config/rawconfig.h>
14 #include <fcitx-utils/capabilityflags.h>
15 #include <fcitx-utils/charutils.h>
16 #include <fcitx-utils/fs.h>
17 #include <fcitx-utils/i18n.h>
18 #include <fcitx-utils/key.h>
19 #include <fcitx-utils/keysym.h>
20 #include <fcitx-utils/log.h>
21 #include <fcitx-utils/macros.h>
22 #include <fcitx-utils/misc.h>
23 #include <fcitx-utils/standardpaths.h>
24 #include <fcitx-utils/stringutils.h>
25 #include <fcitx-utils/utf8.h>
26 #include <fcitx/addoninstance.h>
27 #include <fcitx/event.h>
28 #include <fcitx/inputcontext.h>
29 #include <fcitx/inputcontextmanager.h>
30 #include <fcitx/inputmethodentry.h>
31 #include <fcitx/inputmethodmanager.h>
32 #include <fcitx/instance.h>
33 #include <fcitx/statusarea.h>
34 #include <fcitx/userinterfacemanager.h>
35 #include <fcntl.h>
36 #include <filesystem>
37 #include <fstream>
38 #include <ios>
39 #include <istream>
40 #include <iterator>
41 #include <string>
42 #include <string_view>
43 #include <unordered_map>
44 #include <utility>
45 #include <vector>
46
47 using namespace fcitx;
48
49 namespace {
50 const std::string emptyString;
51 const std::pair<std::string, std::string> emptyStringPair;
52
53 bool dontConvertWhenEn(uint32_t c) { return c == '.' || c == ','; }
54
55 std::string langByPath(const std::string &path) {
56 constexpr std::string_view prefix = "punctuationmap/";
57 if (path.starts_with(prefix)) {
58 return path.substr(prefix.size());
59 }
60 return "";
61 }
62 } // namespace
63
64 class PunctuationState : public InputContextProperty {
65 public:
66 std::unordered_map<uint32_t, std::string> lastPuncStack_;
67 char lastIsEngOrDigit_ = 0;
68 uint32_t notConverted_ = 0;
69 bool mayRebuildStateFromSurroundingText_ = false;
70
71 std::unordered_map<uint32_t, std::string> lastPuncStackBackup_;
72 uint32_t notConvertedBackup_ = 0;
73 };
74
75 void PunctuationProfile::loadSystem(std::istream &in) {
76 load(in);
77 punctuationMapConfig_.syncDefaultValueToCurrent();
78 }
79
80 void PunctuationProfile::resetDefaultValue() {
81 punctuationMapConfig_ = PunctuationMapConfig();
82 punctuationMapConfig_.syncDefaultValueToCurrent();
83 }
84
85 void PunctuationProfile::addEntry(uint32_t key, const std::string &value,
86 const std::string &value2) {
87 decltype(puncMap_)::mapped_type::value_type p;
88 p.first = value;
89 p.second = value2;
90 puncMap_[key].push_back(p);
91
92 std::string punc = utf8::UCS4ToUTF8(key);
93 auto *configValue = punctuationMapConfig_.entries.mutableValue();
94 configValue->emplace_back();
95 PunctuationMapEntryConfig &entryConfig = configValue->back();
96 entryConfig.key.setValue(punc);
97 entryConfig.mapResult1.setValue(value);
98 entryConfig.mapResult2.setValue(value2);
99 }
100
101 void PunctuationProfile::load(std::istream &in) {
102 puncMap_.clear();
103 auto *configValue = punctuationMapConfig_.entries.mutableValue();
104 configValue->clear();
105
106 std::string strBuf;
107 while (std::getline(in, strBuf)) {
108 std::string_view trimmed = stringutils::trimView(strBuf);
109 if (trimmed.empty()) {
110 continue;
111 }
112 auto tokens = stringutils::split(trimmed, FCITX_WHITESPACE);
113 if (tokens.size() != 2 && tokens.size() != 3) {
114 continue;
115 }
116
117 if (!std::any_of(
118 tokens.begin(), tokens.end(),
119 [](const std::string &s) { return utf8::validate(s); })) {
120 continue;
121 }
122 // we don't make # as comment here, # would be consider as a valid char
123 if (utf8::lengthValidated(tokens[0]) != 1) {
124 continue;
125 }
126 auto c = utf8::getChar(tokens[0]);
127 addEntry(c, tokens[1], tokens.size() > 2 ? tokens[2] : "");
128 }
129 }
130
131 void PunctuationProfile::set(const RawConfig &config) {
132 PunctuationMapConfig newConfig;
133 newConfig.load(config);
134
135 puncMap_.clear();
136 auto *configValue = punctuationMapConfig_.entries.mutableValue();
137 configValue->clear();
138
139 const auto &entries = *newConfig.entries;
140 // remove duplicate entry
141 for (const auto &entry : entries) {
142 if (entry.key->empty() || entry.mapResult1->empty()) {
143 continue;
144 }
145 if (utf8::lengthValidated(*entry.key) != 1) {
146 continue;
147 }
148 auto c = utf8::getChar(*entry.key);
149
150 addEntry(c, *entry.mapResult1, *entry.mapResult2);
151 }
152 }
153
154 void PunctuationProfile::save(std::string_view name) const {
155 StandardPaths::global().safeSave(
156 StandardPathsType::PkgData,
157 stringutils::concat("punctuation/", profilePrefix, name),
158 [this](int fd) {
159 for (const auto &entry : *punctuationMapConfig_.entries) {
160 fs::safeWrite(fd, entry.key->data(), entry.key->size());
161 fs::safeWrite(fd, " ", sizeof(char));
162 fs::safeWrite(fd, entry.mapResult1->data(),
163 entry.mapResult1->size());
164 if (!entry.mapResult2->empty()) {
165 fs::safeWrite(fd, " ", sizeof(char));
166 fs::safeWrite(fd, entry.mapResult2->data(),
167 entry.mapResult2->size());
168 }
169 fs::safeWrite(fd, "\n", sizeof(char));
170 }
171 return true;
172 });
173 }
174
175 const std::pair<std::string, std::string> &
176 PunctuationProfile::getPunctuation(uint32_t unicode) const {
177 auto iter = puncMap_.find(unicode);
178 if (iter == puncMap_.end()) {
179 return emptyStringPair;
180 }
181 return iter->second[0];
182 }
183
184 std::vector<std::string>
185 PunctuationProfile::getPunctuations(uint32_t unicode) const {
186 auto iter = puncMap_.find(unicode);
187 if (iter == puncMap_.end()) {
188 return {};
189 }
190 // Return only first if the result size is 1.
191 // This allows single paired symbol to work.
192 if (iter->second.size() == 1) {
193 return {iter->second[0].first};
194 }
195 std::vector<std::string> result;
196 for (const auto &punc : iter->second) {
197 result.push_back(punc.first);
198 if (!punc.second.empty()) {
199 result.push_back(punc.second);
200 }
201 }
202 return result;
203 }
204
205 Punctuation::Punctuation(Instance *instance)
206 : instance_(instance),
207 factory_([](InputContext &) { return new PunctuationState; }) {
208 reloadConfig();
209 if (!instance_) {
210 return;
211 }
212 instance_->inputContextManager().registerProperty("punctuationState",
213 &factory_);
214 instance_->userInterfaceManager().registerAction("punctuation",
215 &toggleAction_);
216
217 commitConn_ = instance_->connect<Instance::CommitFilter>(
218 [this](InputContext *ic, const std::string &sentence) {
219 auto *state = ic->propertyFor(&factory_);
220 // Though sentence is utf8, we only check ascii.
221 if (!sentence.empty() && (charutils::isupper(sentence.back()) ||
222 charutils::islower(sentence.back()) ||
223 charutils::isdigit(sentence.back()))) {
224 state->lastIsEngOrDigit_ = sentence.back();
225 } else {
226 state->lastIsEngOrDigit_ = '\0';
227 }
228 });
229 auto processKeyEvent = [this](const KeyEventBase &keyEvent) {
230 auto *state = keyEvent.inputContext()->propertyFor(&factory_);
231 if (keyEvent.isRelease()) {
232 return;
233 }
234 if (!keyEvent.accepted()) {
235 if (keyEvent.key().isUAZ() || keyEvent.key().isLAZ() ||
236 keyEvent.key().isDigit() ||
237 (keyEvent.key().sym() >= FcitxKey_KP_0 &&
238 keyEvent.key().sym() <= FcitxKey_KP_9 &&
239 !keyEvent.key().hasModifier())) {
240 state->lastIsEngOrDigit_ =
241 Key::keySymToUnicode(keyEvent.key().sym());
242 } else {
243 state->lastIsEngOrDigit_ = '\0';
244 }
245 }
246 };
247 keyEventConn_ =
248 instance_->connect<Instance::KeyEventResult>(processKeyEvent);
249 eventWatchers_.emplace_back(instance_->watchEvent(
250 EventType::InputContextForwardKey, EventWatcherPhase::PostInputMethod,
251 [processKeyEvent](Event &event) {
252 auto &keyEvent = static_cast<ForwardKeyEvent &>(event);
253 processKeyEvent(keyEvent);
254 }));
255 eventWatchers_.emplace_back(instance_->watchEvent(
256 EventType::InputContextKeyEvent, EventWatcherPhase::PostInputMethod,
257 [this](Event &event) {
258 auto &keyEvent = static_cast<KeyEvent &>(event);
259 if (keyEvent.isRelease()) {
260 return;
261 }
262 if (inWhiteList(keyEvent.inputContext()) &&
263 keyEvent.key().checkKeyList(config_.hotkey.value())) {
264 setEnabled(!enabled(), keyEvent.inputContext());
265 if (notifications()) {
266 notifications()->call<INotifications::showTip>(
267 "fcitx-punc-toggle", _("Punctuation"),
268 enabled() ? "fcitx-punc-active" : "fcitx-punc-inactive",
269 _("Punctuation"),
270 enabled() ? _("Full width punctuation is enabled.")
271 : _("Full width punctuation is disabled."),
272 -1);
273 }
274 keyEvent.filterAndAccept();
275 }
276 }));
277 eventWatchers_.emplace_back(instance_->watchEvent(
278 EventType::InputContextFocusIn, EventWatcherPhase::PostInputMethod,
279 [this](Event &event) {
280 auto &icEvent = static_cast<InputContextEvent &>(event);
281 auto *ic = icEvent.inputContext();
282 auto *state = ic->propertyFor(&factory_);
283 if (ic->capabilityFlags().test(CapabilityFlag::SurroundingText)) {
284 state->mayRebuildStateFromSurroundingText_ = true;
285 }
286 }));
287 eventWatchers_.emplace_back(instance_->watchEvent(
288 EventType::InputContextReset, EventWatcherPhase::PostInputMethod,
289 [this](Event &event) {
290 auto &icEvent = static_cast<InputContextEvent &>(event);
291 auto *ic = icEvent.inputContext();
292 auto *state = ic->propertyFor(&factory_);
293 state->lastIsEngOrDigit_ = 0;
294 // Backup the state.
295 state->notConvertedBackup_ = state->notConverted_;
296 state->notConverted_ = 0;
297 // Backup the state.
298 state->lastPuncStackBackup_ = state->lastPuncStack_;
299 state->lastPuncStack_.clear();
300 if (ic->capabilityFlags().test(CapabilityFlag::SurroundingText)) {
301 state->mayRebuildStateFromSurroundingText_ = true;
302 }
303 }));
304 eventWatchers_.emplace_back(instance_->watchEvent(
305 EventType::InputContextSurroundingTextUpdated,
306 EventWatcherPhase::PostInputMethod, [this](Event &event) {
307 auto &icEvent = static_cast<InputContextEvent &>(event);
308 auto *ic = icEvent.inputContext();
309 auto *state = ic->propertyFor(&factory_);
310 // Enable to rebuild punctuation state from surrounding text.
311 if (state->mayRebuildStateFromSurroundingText_) {
312 state->mayRebuildStateFromSurroundingText_ = false;
313 } else {
314 state->notConvertedBackup_ = 0;
315 state->lastPuncStackBackup_.clear();
316 return;
317 }
318 if (!ic->capabilityFlags().test(CapabilityFlag::SurroundingText) ||
319 !ic->surroundingText().isValid()) {
320 return;
321 }
322 // We need text before the cursor.
323 const auto &text = ic->surroundingText().text();
324 auto cursor = ic->surroundingText().cursor();
325 auto length = utf8::lengthValidated(text);
326 if (length == utf8::INVALID_LENGTH) {
327 return;
328 }
329 if (cursor <= 0 && cursor > length) {
330 return;
331 }
332 uint32_t lastCharBeforeCursor;
333 auto start = utf8::nextNChar(text.begin(), cursor - 1);
334 auto end =
335 utf8::getNextChar(start, text.end(), &lastCharBeforeCursor);
336 if (lastCharBeforeCursor == utf8::INVALID_CHAR ||
337 lastCharBeforeCursor == utf8::NOT_ENOUGH_SPACE) {
338 return;
339 }
340 // Need to make sure we have ascii.
341 if (std::distance(start, end) == 1 &&
342 (charutils::isupper(lastCharBeforeCursor) ||
343 charutils::islower(lastCharBeforeCursor) ||
344 charutils::isdigit(lastCharBeforeCursor))) {
345 state->lastIsEngOrDigit_ = lastCharBeforeCursor;
346 }
347 // Restore the not converted state if we still after the same chr.
348 if (lastCharBeforeCursor == state->notConvertedBackup_ &&
349 state->notConverted_ == 0) {
350 state->notConverted_ = state->notConvertedBackup_;
351 }
352 state->notConvertedBackup_ = 0;
353 // Scan through the surrounding text
354 if (!state->lastPuncStackBackup_.empty() &&
355 state->lastPuncStack_.empty()) {
356 auto range = utf8::MakeUTF8StringViewRange(
357 MakeIterRange(text.begin(), end));
358 for (std::string_view chr : range) {
359 auto puncIter = std::find_if(
360 state->lastPuncStackBackup_.begin(),
361 state->lastPuncStackBackup_.end(),
362 [chr](auto &p) { return p.second == chr; });
363 if (puncIter != state->lastPuncStackBackup_.end()) {
364 state->lastPuncStack_.insert(*puncIter);
365 }
366 }
367 }
368 state->lastPuncStackBackup_.clear();
369 }));
370 }
371
372 Punctuation::~Punctuation() {}
373
374 void Punctuation::reloadConfig() {
375 readAsIni(config_, "conf/punctuation.conf");
376 loadProfiles();
377 toggleAction_.setHotkey(config_.hotkey.value());
378 }
379
380 void Punctuation::loadProfiles() {
381 auto filter = [](const std::filesystem::path &path) {
382 return path.filename().string().starts_with(
383 PunctuationProfile::profilePrefix);
384 };
385 auto systemFiles = StandardPaths::global().locate(
386 StandardPathsType::PkgData, "punctuation", filter,
387 StandardPathsMode::System);
388 auto allFiles = StandardPaths::global().locate(StandardPathsType::PkgData,
389 "punctuation", filter);
390
391 // Remove non-exist profiles.
392 auto iter = profiles_.begin();
393 while (iter != profiles_.end()) {
394 if (!allFiles.contains(stringutils::concat(
395 PunctuationProfile::profilePrefix, iter->first))) {
396 iter = profiles_.erase(iter);
397 } else {
398 iter++;
399 }
400 }
401
402 for (const auto &file : allFiles) {
403 if (file.first.string().size() <=
404 PunctuationProfile::profilePrefix.size()) {
405 continue;
406 }
407 auto lang = file.first.string().substr(
408 PunctuationProfile::profilePrefix.size());
409
410 auto iter = systemFiles.find(file.first);
411 const bool hasSystemFile = iter != systemFiles.end();
412 bool hasUserFile = true;
413 if (hasSystemFile && iter->second == file.second) {
414 hasUserFile = false;
415 }
416 try {
417 if (hasSystemFile) {
418 std::ifstream in(iter->second, std::ios::in | std::ios::binary);
419 profiles_[lang].loadSystem(in);
420 } else {
421 profiles_[lang].resetDefaultValue();
422 }
423 if (hasUserFile) {
424 std::ifstream in(file.second, std::ios::in | std::ios::binary);
425 profiles_[lang].load(in);
426 }
427 } catch (const std::exception &e) {
428 FCITX_WARN() << "Error when load profile " << file.first << ": "
429 << e.what();
430 }
431 }
432 }
433
434 bool Punctuation::inWhiteList(fcitx::InputContext *inputContext) const {
435 return toggleAction_.isParent(&inputContext->statusArea());
436 }
437
438 const std::string &Punctuation::pushPunctuation(const std::string &language,
439 InputContext *ic,
440 uint32_t unicode) {
441 if (!enabled()) {
442 return emptyString;
443 }
444 auto *state = ic->propertyFor(&factory_);
445 if (state->lastIsEngOrDigit_ && *config_.halfWidthPuncAfterLatinOrNumber &&
446 dontConvertWhenEn(unicode)) {
447 state->notConverted_ = unicode;
448 return emptyString;
449 }
450 auto iter = profiles_.find(language);
451 if (iter == profiles_.end()) {
452 return emptyString;
453 }
454 const auto &result = getPunctuation(language, unicode);
455 state->notConverted_ = 0;
456 if (result.second.empty()) {
457 return result.first;
458 }
459
460 auto puncIter = state->lastPuncStack_.find(unicode);
461 if (puncIter != state->lastPuncStack_.end()) {
462 state->lastPuncStack_.erase(puncIter);
463 return result.second;
464 }
465 state->lastPuncStack_.emplace(unicode, result.first);
466 return result.first;
467 }
468
469 std::pair<std::string, std::string>
470 Punctuation::pushPunctuationV2(const std::string &language, InputContext *ic,
471 uint32_t unicode) {
472 if (!enabled()) {
473 return {emptyString, emptyString};
474 }
475 auto *state = ic->propertyFor(&factory_);
476 if (state->lastIsEngOrDigit_ && *config_.halfWidthPuncAfterLatinOrNumber &&
477 dontConvertWhenEn(unicode)) {
478 state->notConverted_ = unicode;
479 return {emptyString, emptyString};
480 }
481 auto iter = profiles_.find(language);
482 if (iter == profiles_.end()) {
483 return {emptyString, emptyString};
484 }
485 const auto &result = getPunctuation(language, unicode);
486 state->notConverted_ = 0;
487 if (result.second.empty()) {
488 return {result.first, emptyString};
489 }
490
491 if (*config_.typePairedPunctuationTogether) {
492 return {result.first, result.second};
493 }
494
495 auto puncIter = state->lastPuncStack_.find(unicode);
496 if (puncIter != state->lastPuncStack_.end()) {
497 state->lastPuncStack_.erase(puncIter);
498 return {result.second, emptyString};
499 }
500 state->lastPuncStack_.emplace(unicode, result.first);
501 return {result.first, emptyString};
502 }
503
504 const std::string &Punctuation::cancelLast(const std::string &language,
505 InputContext *ic) {
506 if (!enabled()) {
507 return emptyString;
508 }
509 auto *state = ic->propertyFor(&factory_);
510 if (dontConvertWhenEn(state->notConverted_)) {
511 const auto &result = getPunctuation(language, state->notConverted_);
512 state->notConverted_ = 0;
513 return result.first;
514 }
515 return emptyString;
516 }
517
518 std::vector<std::string>
519 Punctuation::getPunctuationCandidates(const std::string &language,
520 uint32_t unicode) {
521 if (!enabled()) {
522 return {};
523 }
524 auto iter = profiles_.find(language);
525 if (iter == profiles_.end()) {
526 return {};
527 }
528 return getPunctuations(language, unicode);
529 }
530
531 const std::pair<std::string, std::string> &
532 Punctuation::getPunctuation(const std::string &language, uint32_t unicode) {
533 if (!*config_.enabled) {
534 return emptyStringPair;
535 }
536
537 auto iter = profiles_.find(language);
538 if (iter == profiles_.end()) {
539 return emptyStringPair;
540 }
541
542 return iter->second.getPunctuation(unicode);
543 }
544
545 std::vector<std::string>
546 Punctuation::getPunctuations(const std::string &language, uint32_t unicode) {
547 if (!*config_.enabled) {
548 return {};
549 }
550
551 auto iter = profiles_.find(language);
552 if (iter == profiles_.end()) {
553 return {};
554 }
555
556 return iter->second.getPunctuations(unicode);
557 }
558
559 const fcitx::Configuration *
560 Punctuation::getSubConfig(const std::string &path) const {
561 auto lang = langByPath(path);
562 if (lang.empty()) {
563 return nullptr;
564 }
565 if (auto iter = profiles_.find(lang); iter != profiles_.end()) {
566 return &iter->second.config();
567 }
568 return nullptr;
569 }
570
571 void Punctuation::setSubConfig(const std::string &path,
572 const fcitx::RawConfig &config) {
573 std::string lang = langByPath(path);
574 auto iter = profiles_.find(lang);
575 if (iter == profiles_.end()) {
576 return;
577 }
578 iter->second.set(config);
579 iter->second.save(lang);
580 }
581
582 FCITX_ADDON_FACTORY_V2(punctuation, PunctuationFactory);