66 */
77
88#include " unikey-im.h"
9+ #include " unikey-config.h"
10+ #include " unikeyinputcontext.h"
911#include " usrkeymap.h"
1012#include " vnconv.h"
13+ #include < cassert>
14+ #include < cstddef>
15+ #include < cstdint>
16+ #include < cstring>
17+ #include < fcitx-config/iniparser.h>
18+ #include < fcitx-utils/capabilityflags.h>
1119#include < fcitx-utils/charutils.h>
20+ #include < fcitx-utils/fs.h>
21+ #include < fcitx-utils/i18n.h>
22+ #include < fcitx-utils/key.h>
23+ #include < fcitx-utils/keysym.h>
24+ #include < fcitx-utils/log.h>
25+ #include < fcitx-utils/macros.h>
26+ #include < fcitx-utils/misc.h>
1227#include < fcitx-utils/standardpath.h>
28+ #include < fcitx-utils/textformatflags.h>
1329#include < fcitx-utils/utf8.h>
30+ #include < fcitx/action.h>
31+ #include < fcitx/addoninstance.h>
32+ #include < fcitx/event.h>
1433#include < fcitx/inputcontext.h>
1534#include < fcitx/inputcontextmanager.h>
35+ #include < fcitx/inputmethodentry.h>
1636#include < fcitx/inputpanel.h>
37+ #include < fcitx/instance.h>
1738#include < fcitx/menu.h>
1839#include < fcitx/statusarea.h>
40+ #include < fcitx/text.h>
41+ #include < fcitx/userinterface.h>
1942#include < fcitx/userinterfacemanager.h>
2043#include < fcntl.h>
44+ #include < iterator>
45+ #include < memory>
46+ #include < string>
47+ #include < string_view>
48+ #include < unordered_map>
49+ #include < unordered_set>
50+ #include < vector>
2151
22- namespace fcitx {
52+ # define FCITX_UNIKEY_DEBUG () FCITX_LOGC(:: fcitx::unikey, Debug)
2353
24- FCITX_DEFINE_LOG_CATEGORY (unikey, " unikey" );
25- #define FCITX_UNIKEY_DEBUG () FCITX_LOGC(unikey, Debug)
54+ namespace fcitx {
2655
2756namespace {
2857
58+ FCITX_DEFINE_LOG_CATEGORY (unikey, " unikey" );
59+
2960constexpr auto CONVERT_BUF_SIZE = 1024 ;
3061constexpr auto MAX_LENGTH_VNWORD = 7 ;
31- static const unsigned int Unikey_OC[] = {
32- CONV_CHARSET_XUTF8, CONV_CHARSET_TCVN3, CONV_CHARSET_VNIWIN,
33- CONV_CHARSET_VIQR, CONV_CHARSET_BKHCM2, CONV_CHARSET_UNI_CSTRING,
34- CONV_CHARSET_UNIREF, CONV_CHARSET_UNIREF_HEX};
35- static constexpr unsigned int NUM_OUTPUTCHARSET = FCITX_ARRAY_SIZE(Unikey_OC);
62+ const unsigned int Unikey_OC[] = {CONV_CHARSET_XUTF8, CONV_CHARSET_TCVN3,
63+ CONV_CHARSET_VNIWIN, CONV_CHARSET_VIQR ,
64+ CONV_CHARSET_BKHCM2, CONV_CHARSET_UNI_CSTRING,
65+ CONV_CHARSET_UNIREF, CONV_CHARSET_UNIREF_HEX};
66+ constexpr unsigned int NUM_OUTPUTCHARSET = FCITX_ARRAY_SIZE(Unikey_OC);
3667static_assert (NUM_OUTPUTCHARSET == UkConvI18NAnnotation::enumLength);
3768
38- static bool isWordBreakSym (unsigned char c) { return WordBreakSyms.count (c); }
69+ bool isWordBreakSym (unsigned char c) { return WordBreakSyms.count (c); }
3970
40- static bool isWordAutoCommit (unsigned char c) {
71+ bool isWordAutoCommit (unsigned char c) {
4172 static const std::unordered_set<unsigned char > WordAutoCommit = {
4273 ' 0' , ' 1' , ' 2' , ' 3' , ' 4' , ' 5' , ' 6' , ' 7' , ' 8' , ' 9' , ' b' , ' c' ,
4374 ' f' , ' g' , ' h' , ' j' , ' k' , ' l' , ' m' , ' n' , ' p' , ' q' , ' r' , ' s' ,
@@ -46,7 +77,7 @@ static bool isWordAutoCommit(unsigned char c) {
4677 return WordAutoCommit.count (c);
4778}
4879
49- static VnLexiName charToVnLexi (uint32_t ch) {
80+ VnLexiName charToVnLexi (uint32_t ch) {
5081 static const std::unordered_map<uint32_t , VnLexiName> map = []() {
5182 std::unordered_map<uint32_t , VnLexiName> result;
5283 for (int i = 0 ; i < vnl_lastChar; i++) {
@@ -61,7 +92,7 @@ static VnLexiName charToVnLexi(uint32_t ch) {
6192 return vnl_nonVnChar;
6293}
6394
64- static bool isVnChar (uint32_t ch) { return charToVnLexi (ch) != vnl_nonVnChar; }
95+ bool isVnChar (uint32_t ch) { return charToVnLexi (ch) != vnl_nonVnChar; }
6596
6697// code from x-unikey, for convert charset that not is XUtf-8
6798int latinToUtf (unsigned char *dst, const unsigned char *src, int inSize,
@@ -76,8 +107,9 @@ int latinToUtf(unsigned char *dst, const unsigned char *src, int inSize,
76107 ch = *src++;
77108 if (ch < 0x80 ) {
78109 outLeft -= 1 ;
79- if (outLeft >= 0 )
110+ if (outLeft >= 0 ) {
80111 *dst++ = ch;
112+ }
81113 } else {
82114 outLeft -= 2 ;
83115 if (outLeft >= 0 ) {
@@ -133,7 +165,8 @@ class UnikeyState final : public InputContextProperty {
133165 void updatePreedit ();
134166
135167 void eraseChars (int num_chars) {
136- int i, k;
168+ int i;
169+ int k;
137170 unsigned char c;
138171 k = num_chars;
139172
@@ -337,7 +370,7 @@ UnikeyEngine::UnikeyEngine(Instance *instance)
337370 for (UkInputMethod im : {UkTelex, UkVni, UkViqr, UkMsVi, UkUsrIM,
338371 UkSimpleTelex, UkSimpleTelex2}) {
339372 inputMethodSubAction_.emplace_back (std::make_unique<SimpleAction>());
340- auto action = inputMethodSubAction_.back ().get ();
373+ auto * action = inputMethodSubAction_.back ().get ();
341374 action->setShortText (UkInputMethodI18NAnnotation::toString (im));
342375 action->setCheckable (true );
343376 uiManager.registerAction (
@@ -364,7 +397,7 @@ UnikeyEngine::UnikeyEngine(Instance *instance)
364397 UkConv::VIQR, UkConv::BKHCM2, UkConv::UNI_CSTRING,
365398 UkConv::UNIREF, UkConv::UNIREF_HEX}) {
366399 charsetSubAction_.emplace_back (std::make_unique<SimpleAction>());
367- auto action = charsetSubAction_.back ().get ();
400+ auto * action = charsetSubAction_.back ().get ();
368401 action->setShortText (UkConvI18NAnnotation::toString (conv));
369402 action->setCheckable (true );
370403 connections_.emplace_back (action->connect <SimpleAction::Activated>(
@@ -416,7 +449,7 @@ UnikeyEngine::UnikeyEngine(Instance *instance)
416449
417450UnikeyEngine::~UnikeyEngine () {}
418451
419- void UnikeyEngine::activate (const InputMethodEntry &,
452+ void UnikeyEngine::activate (const InputMethodEntry & /* entry */ ,
420453 InputContextEvent &event) {
421454 auto &statusArea = event.inputContext ()->statusArea ();
422455 statusArea.addAction (StatusGroup::InputMethod, inputMethodAction_.get ());
@@ -441,9 +474,10 @@ void UnikeyEngine::deactivate(const InputMethodEntry &entry,
441474 reset (entry, event);
442475}
443476
444- void UnikeyEngine::keyEvent (const InputMethodEntry &, KeyEvent &keyEvent) {
445- auto ic = keyEvent.inputContext ();
446- auto state = ic->propertyFor (&factory_);
477+ void UnikeyEngine::keyEvent (const InputMethodEntry & /* entry*/ ,
478+ KeyEvent &keyEvent) {
479+ auto *ic = keyEvent.inputContext ();
480+ auto *state = ic->propertyFor (&factory_);
447481 state->rebuildFromSurroundingText ();
448482 state->keyEvent (keyEvent);
449483}
@@ -464,7 +498,8 @@ void UnikeyState::preedit(KeyEvent &keyEvent) {
464498 syncState (keyEvent.rawKey ().sym ());
465499 updatePreedit ();
466500 lastShiftPressed_ = FcitxKey_None;
467- return keyEvent.filterAndAccept ();
501+ keyEvent.filterAndAccept ();
502+ return ;
468503 }
469504 } else {
470505 // We pressed something else, reset the state.
@@ -479,50 +514,55 @@ void UnikeyState::preedit(KeyEvent &keyEvent) {
479514 (sym >= FcitxKey_KP_Home && sym <= FcitxKey_KP_Delete)) {
480515 handleIgnoredKey ();
481516 return ;
482- } else if (state.test (KeyState::Super)) {
517+ }
518+ if (state.test (KeyState::Super)) {
483519 return ;
484- } else if ((sym >= FcitxKey_Caps_Lock && sym <= FcitxKey_Hyper_R) ||
485- sym == FcitxKey_Shift_L || sym == FcitxKey_Shift_R) {
520+ }
521+ if ((sym >= FcitxKey_Caps_Lock && sym <= FcitxKey_Hyper_R) ||
522+ sym == FcitxKey_Shift_L || sym == FcitxKey_Shift_R) {
486523 return ;
487- } else if (sym == FcitxKey_BackSpace) {
524+ }
525+ if (sym == FcitxKey_BackSpace) {
488526 // capture BackSpace
489527 uic_.backspacePress ();
490528
491529 if (uic_.backspaces () == 0 || preeditStr_.empty ()) {
492530 commit ();
493531 return ;
532+ }
533+ if (static_cast <int >(preeditStr_.length ()) <= uic_.backspaces ()) {
534+ preeditStr_.clear ();
535+ autoCommit_ = true ;
494536 } else {
495- if (static_cast <int >(preeditStr_.length ()) <= uic_.backspaces ()) {
496- preeditStr_.clear ();
497- autoCommit_ = true ;
537+ eraseChars (uic_.backspaces ());
538+ }
539+
540+ // change tone position after press backspace
541+ if (uic_.bufChars () > 0 ) {
542+ if (engine_->config ().oc .value () == UkConv::XUTF8) {
543+ preeditStr_.append (reinterpret_cast <const char *>(uic_.buf ()),
544+ uic_.bufChars ());
498545 } else {
499- eraseChars (uic_. backspaces ()) ;
500- }
546+ unsigned char buf[CONVERT_BUF_SIZE] ;
547+ int bufSize = CONVERT_BUF_SIZE;
501548
502- // change tone position after press backspace
503- if (uic_.bufChars () > 0 ) {
504- if (engine_->config ().oc .value () == UkConv::XUTF8) {
505- preeditStr_.append (
506- reinterpret_cast <const char *>(uic_.buf ()),
507- uic_.bufChars ());
508- } else {
509- unsigned char buf[CONVERT_BUF_SIZE];
510- int bufSize = CONVERT_BUF_SIZE;
511-
512- latinToUtf (buf, uic_.buf (), uic_.bufChars (), &bufSize);
513- preeditStr_.append ((const char *)buf,
514- CONVERT_BUF_SIZE - bufSize);
515- }
516-
517- autoCommit_ = false ;
549+ latinToUtf (buf, uic_.buf (), uic_.bufChars (), &bufSize);
550+ preeditStr_.append ((const char *)buf,
551+ CONVERT_BUF_SIZE - bufSize);
518552 }
519- updatePreedit ();
553+
554+ autoCommit_ = false ;
520555 }
521- return keyEvent.filterAndAccept ();
522- } else if (sym >= FcitxKey_KP_Multiply && sym <= FcitxKey_KP_9) {
556+ updatePreedit ();
557+
558+ keyEvent.filterAndAccept ();
559+ return ;
560+ }
561+ if (sym >= FcitxKey_KP_Multiply && sym <= FcitxKey_KP_9) {
523562 handleIgnoredKey ();
524563 return ;
525- } else if (sym >= FcitxKey_space && sym <= FcitxKey_asciitilde) {
564+ }
565+ if (sym >= FcitxKey_space && sym <= FcitxKey_asciitilde) {
526566 // capture ascii printable char
527567 uic_.setCapsState (state.test (KeyState::Shift),
528568 state.test (KeyState::CapsLock));
@@ -545,23 +585,23 @@ void UnikeyState::preedit(KeyEvent &keyEvent) {
545585
546586 if ((*engine_->config ().im == UkTelex ||
547587 *engine_->config ().im == UkSimpleTelex2) &&
548- *engine_->config ().process_w_at_begin == false &&
588+ ! *engine_->config ().process_w_at_begin &&
549589 uic_.isAtWordBeginning () &&
550590 (sym == FcitxKey_w || sym == FcitxKey_W)) {
551591 uic_.putChar (sym);
552592 if (!*engine_->config ().macro ) {
553593 return ;
554- } else {
555- preeditStr_.append (sym == FcitxKey_w ? " w" : " W" );
556- updatePreedit ();
557- return keyEvent.filterAndAccept ();
558594 }
595+ preeditStr_.append (sym == FcitxKey_w ? " w" : " W" );
596+ updatePreedit ();
597+ keyEvent.filterAndAccept ();
598+ return ;
559599 }
560600
561601 autoCommit_ = false ;
562602
563603 // shift + space, shift + shift event
564- if (lastKeyWithShift_ == false && state.test (KeyState::Shift) &&
604+ if (! lastKeyWithShift_ && state.test (KeyState::Shift) &&
565605 sym == FcitxKey_space && !uic_.isAtWordBeginning ()) {
566606 uic_.restoreKeyStrokes ();
567607 } else {
@@ -576,21 +616,24 @@ void UnikeyState::preedit(KeyEvent &keyEvent) {
576616 if (!preeditStr_.empty ()) {
577617 if (preeditStr_.back () == sym && isWordBreakSym (sym)) {
578618 commit ();
579- return keyEvent.filterAndAccept ();
619+ keyEvent.filterAndAccept ();
620+ return ;
580621 }
581622 }
582623 // end commit string
583624
584625 updatePreedit ();
585- return keyEvent.filterAndAccept ();
626+ keyEvent.filterAndAccept ();
627+ return ;
586628 } // end capture printable char
587629
588630 // non process key
589631 handleIgnoredKey ();
590632}
591633
592- void UnikeyEngine::reset (const InputMethodEntry &, InputContextEvent &event) {
593- auto state = event.inputContext ()->propertyFor (&factory_);
634+ void UnikeyEngine::reset (const InputMethodEntry & /* entry*/ ,
635+ InputContextEvent &event) {
636+ auto *state = event.inputContext ()->propertyFor (&factory_);
594637 state->reset ();
595638 if (event.type () == EventType::InputContextReset) {
596639 if (event.inputContext ()->capabilityFlags ().test (
@@ -636,7 +679,8 @@ void UnikeyEngine::reloadKeymap() {
636679
637680void UnikeyEngine::save () {}
638681
639- std::string UnikeyEngine::subMode (const InputMethodEntry &, InputContext &) {
682+ std::string UnikeyEngine::subMode (const InputMethodEntry & /* entry*/ ,
683+ InputContext & /* inputContext*/ ) {
640684 return UkInputMethodI18NAnnotation::toString (*config_.im );
641685}
642686void UnikeyEngine::updateMacroAction (InputContext *ic) {
@@ -748,4 +792,4 @@ void UnikeyState::updatePreedit() {
748792
749793} // namespace fcitx
750794
751- FCITX_ADDON_FACTORY ( fcitx::UnikeyFactory)
795+ FCITX_ADDON_FACTORY_V2 (unikey, fcitx::UnikeyFactory)
0 commit comments