commit d52b58fd55d7f54a79a2706f6b9ee78e02f8489c
Author: Koji Yokota <[email protected]>
Date:   Thu Jul 24 16:28:48 2025 +0900

    Add ImOffInMath tag in lib/languages for control of input method in math
---
 lib/languages                       | 10 +++++++++-
 src/Language.cpp                    |  5 +++++
 src/Language.h                      |  5 +++++
 src/frontends/qt/GuiInputMethod.cpp | 36 ++++++++++++++++++------------------
 4 files changed, 37 insertions(+), 19 deletions(-)

diff --git a/lib/languages b/lib/languages
index af12c41103..425724bf78 100644
--- a/lib/languages
+++ b/lib/languages
@@ -22,7 +22,8 @@
 #      FontEncoding       <font encoding|font encoding|...>
 #      InternalEncoding   <true|false>
 #      RTL                <true|false>
-#      WordWrap           <true|false>
+#      WordWrap           <true|false>
+#      ImOffInMath        <true|false>
 #      LangCode           <language_code>
 #      LangVariety        <language_variety>
 #      PreBabelPreamble
@@ -111,6 +112,8 @@
 # * WordWrap is only used for on-screen display: when is is true (the 
default), rows are broken
 #   at word boundary; otherwise, they can be ended at arbitrary position. This
 #   setting is useful for CJK languages.
+# * ImOffInMath shows if the input method should be turned off in the math 
mode.
+#   It is applicable to the languages that use the input method.
 # * LangCode is also used for spellchecking and thesaurus, where the
 #   dictionaries are named accordingly. Thus, check this when introducing/
 #   changing language codes (especially aspell, thesaurus).
@@ -554,6 +557,7 @@ Language chinese-simplified
        Encoding         euc-cn
        QuoteStyle       english
        WordWrap         false
+       ImOffInMath      true
        LangCode         zh_CN
        DateFormats      "yyyy年M月d日|yyyy-M-d|yy-M-d"
        Requires         CJK
@@ -568,6 +572,7 @@ Language chinese-traditional
        QuoteStyle       cjk
        Encoding         utf8-cjk
        WordWrap         false
+       ImOffInMath      true
        LangCode         zh_TW
        DateFormats      "yyyy年M月d日|yyyy年M月d日|yy年M月d日"
        Requires         CJK
@@ -980,6 +985,7 @@ Language japanese
        PolyglossiaName  japanese
        Encoding         jis-platex
        WordWrap         false
+       ImOffInMath      true
        LangCode         ja_JP
        Requires         japanese
        FontEncoding     ASCII
@@ -993,6 +999,7 @@ Language japanese-cjk
        Encoding        euc-jp
        DateFormats     "yyyy年M月d日|yyyy/MM/dd|yy/MM/dd"
        WordWrap        false
+       ImOffInMath      true
        LangCode        ja_JP
        Requires        CJK
        QuoteStyle      cjk
@@ -1037,6 +1044,7 @@ Language korean
        QuoteStyle      cjkangle
        DateFormats     "yyyy년 M월 d일|yyyy. M. d.|yy. M. d."
        WordWrap        false
+       ImOffInMath      true
        LangCode        ko_KR
        Requires        CJK
 End
diff --git a/src/Language.cpp b/src/Language.cpp
index 942a7ca4f6..f380b93772 100644
--- a/src/Language.cpp
+++ b/src/Language.cpp
@@ -140,6 +140,7 @@ bool Language::readLanguage(Lexer & lex)
                LA_FONTENC,
                LA_GUINAME,
                LA_HAS_GUI_SUPPORT,
+               LA_IM_OFF_IN_MATH,
                LA_INTERNAL_ENC,
                LA_LANG_CODE,
                LA_LANG_VARIETY,
@@ -169,6 +170,7 @@ bool Language::readLanguage(Lexer & lex)
                { "fontencoding",         LA_FONTENC },
                { "guiname",              LA_GUINAME },
                { "hasguisupport",        LA_HAS_GUI_SUPPORT },
+           { "imoffinmath",          LA_IM_OFF_IN_MATH },
                { "internalencoding",     LA_INTERNAL_ENC },
                { "langcode",             LA_LANG_CODE },
                { "langvariety",          LA_LANG_VARIETY },
@@ -257,6 +259,9 @@ bool Language::readLanguage(Lexer & lex)
                case LA_HAS_GUI_SUPPORT:
                        lex >> has_gui_support_;
                        break;
+               case LA_IM_OFF_IN_MATH:
+                       lex >> im_off_in_math_;
+                       break;
                case LA_INTERNAL_ENC:
                        lex >> internal_enc_;
                        break;
diff --git a/src/Language.h b/src/Language.h
index 56e324a359..91577abb9b 100644
--- a/src/Language.h
+++ b/src/Language.h
@@ -38,6 +38,7 @@ public:
        ///
        Language() : rightToLeft_(false), encoding_(0), internal_enc_(false),
                                 has_gui_support_(false), word_wrap_(true),
+                    im_off_in_math_(false),
                                 use_babel_provide_(0){}
        /// LyX language name
        std::string const lang() const { return lang_; }
@@ -73,6 +74,8 @@ public:
        bool rightToLeft() const { return rightToLeft_; }
        /// shall text be wrapped at word boundary ?
        bool wordWrap() const { return word_wrap_; }
+       ///
+       bool inputMethodOffInMath() const { return im_off_in_math_; }
        /**
         * Translate a string from the layout files that appears in the output.
         * It takes the translations from lib/layouttranslations instead of
@@ -166,6 +169,8 @@ private:
        ///
        bool word_wrap_;
        ///
+       bool im_off_in_math_;
+       ///
        TranslationMap layoutTranslations_;
        ///
        int use_babel_provide_;
diff --git a/src/frontends/qt/GuiInputMethod.cpp 
b/src/frontends/qt/GuiInputMethod.cpp
index 71a38b0680..81320080f8 100644
--- a/src/frontends/qt/GuiInputMethod.cpp
+++ b/src/frontends/qt/GuiInputMethod.cpp
@@ -58,7 +58,7 @@ struct GuiInputMethod::Private
        QInputMethod * sys_im_ = guiApp->inputMethod();
        QLocale locale_;
        /// if language should not use preedit in the math mode
-       bool is_cjk_locale = false;
+       bool im_off_in_math_ = false;
 
        ColorCache & color_cache_ = guiApp->colorCache();
        QColor font_color_;
@@ -118,18 +118,17 @@ GuiInputMethod::~GuiInputMethod()
 
 
 void GuiInputMethod::toggleInputMethodAcceptance(){
-#if defined(Q_OS_WIN) || defined (Q_CYGWIN_WIN) || defined (Q_OS_LINUX)
-       // Since QInputMethod::locale() doesn't work on these systems, use 
language
-       // at the cursor point to infer the current input language as a second 
best
-       std::string lang = d->cur_->getFont().language()->lang();
-       if (lang == "chinese-simplified" || lang == "chinese-traditional" ||
-               lang == "korean" || lang == "japanese")
-               d->is_cjk_locale = true;
-       else
-               d->is_cjk_locale = false;
-#endif
+       if (guiApp->platformName() != "cocoa") {
+               // Since QInputMethod::locale() doesn't work on these systems, 
use language
+               // at the cursor point to infer the current input language as a 
second best
+               if (d->cur_->getFont().language()->inputMethodOffInMath())
+                       d->im_off_in_math_ = true;
+               else
+                       d->im_off_in_math_ = false;
+       }
+
        // note that d->cur_->inset() is a cache so it lags from actual key 
moves
-       if ((d->is_cjk_locale &&
+       if ((d->im_off_in_math_ &&
                d->cur_->inset().currentMode() == Inset::MATH_MODE) ||
                 guiApp->isInCommandMode())
                d->im_state_.enabled_ = false;
@@ -142,12 +141,13 @@ void GuiInputMethod::toggleInputMethodAcceptance(){
 
 void GuiInputMethod::onLocaleChanged()
 {
-       QLocale lang = d->sys_im_->locale().language();
-       if (lang == QLocale::Chinese || lang == QLocale::Japanese ||
-               lang == QLocale::Korean)
-               d->is_cjk_locale = true;
+       const std::string im_locale_code = 
fromqstr(d->sys_im_->locale().name());
+       const Language * lang = languages.getFromCode(im_locale_code);
+
+       if (lang->inputMethodOffInMath())
+               d->im_off_in_math_ = true;
        else
-               d->is_cjk_locale = false;
+               d->im_off_in_math_ = false;
 }
 
 
@@ -445,7 +445,7 @@ void GuiInputMethod::setPreeditStyle(
                        char_format.setFontUnderline(true);
                        start = focus_style->start;
                        length = (size_type)focus_style->length;
-               } else if (!d->is_cjk_locale && !d->preedit_str_.empty() &&
+               } else if (!d->im_off_in_math_ && !d->preedit_str_.empty() &&
                           d->style_.segments_.empty()) {
                        // dead keys on MacOS give no char format having 
preedit strings
                        length = d->preedit_str_.length();
-- 
lyx-cvs mailing list
[email protected]
https://lists.lyx.org/mailman/listinfo/lyx-cvs

Reply via email to