commit c46f2ecbbba989277c78998bd095effd05613252
Author: Koji Yokota <[email protected]>
Date:   Wed Jun 11 21:40:37 2025 +0900

    Inform the user of the conflict between math command and math environment
---
 lib/math_conflicts           |  8 +++++++
 src/mathed/InsetMathGrid.cpp | 23 +++++++++++++++++++
 src/mathed/InsetMathGrid.h   |  2 ++
 src/mathed/MathFactory.cpp   | 53 ++++++++++++++++++++++++++++++++++++++++++++
 src/mathed/MathFactory.h     |  5 +++++
 5 files changed, 91 insertions(+)

diff --git a/lib/math_conflicts b/lib/math_conflicts
new file mode 100644
index 0000000000..94473f461e
--- /dev/null
+++ b/lib/math_conflicts
@@ -0,0 +1,8 @@
+#
+# List of incompatible commands and math environments
+#
+#                       incompatible math environments
+# command name          (incl. those with a trailing star)
+
+intertext               equation,eqnarray,multline,equation,simple,none
+shortintertext          equation,eqnarray,multline,equation,simple,none
diff --git a/src/mathed/InsetMathGrid.cpp b/src/mathed/InsetMathGrid.cpp
index 1786d1b9de..b4d216eae3 100644
--- a/src/mathed/InsetMathGrid.cpp
+++ b/src/mathed/InsetMathGrid.cpp
@@ -28,8 +28,10 @@
 #include "FuncRequest.h"
 #include "FuncStatus.h"
 #include "LaTeXFeatures.h"
+#include "MathFactory.h"
 #include "TexRow.h"
 
+#include "frontends/alert.h"
 #include "frontends/Clipboard.h"
 #include "frontends/Painter.h"
 
@@ -1296,6 +1298,7 @@ void InsetMathGrid::write(TeXMathStream & os,
                                   << "}{" << cellinfo_[idx].align
                                   << "}{";
                        }
+                       checkMathCommandConflict(idx);
                        os << cell(idx);
                        if (os.pendingBrace())
                                ModeSpecifier specifier(os, TEXT_MODE);
@@ -1323,6 +1326,26 @@ void InsetMathGrid::write(TeXMathStream & os,
        }
 }
 
+void InsetMathGrid::checkMathCommandConflict(idx_type idx) const
+{
+       LYXERR0("hulltype = " << hullName(getType()));
+       for (docstring const &command : mathedConflictCommands()) {
+               if (!cell(idx).empty() && cell(idx).front()->name() == command) 
{
+                       for (docstring const & hull : 
mathedConflictList().at(command)) {
+                               if (getType() == hullType(hull)) {
+                                       docstring const & warnTitle =
+                                               from_utf8(N_("Command 
conflict"));
+                                       docstring const & warnMessage =
+                                               bformat(_("TeX command '%1$s' 
cannot be used in math environment '%2$s'. Please change the environment or the 
command."),
+                                                       command, hull);
+                                       frontend::Alert::warning(warnTitle, 
warnMessage);
+                                       return;
+                               }
+                       }
+               }
+       }
+}
+
 
 int InsetMathGrid::colsep() const
 {
diff --git a/src/mathed/InsetMathGrid.h b/src/mathed/InsetMathGrid.h
index ae95eaf095..57f0cc1bed 100644
--- a/src/mathed/InsetMathGrid.h
+++ b/src/mathed/InsetMathGrid.h
@@ -287,6 +287,8 @@ private:
        char v_align_; // FIXME: add approp. type
        ///
        Inset * clone() const override;
+       ///
+       void checkMathCommandConflict(idx_type idx) const;
 };
 
 
diff --git a/src/mathed/MathFactory.cpp b/src/mathed/MathFactory.cpp
index f39bef3d96..e9f1aa2496 100644
--- a/src/mathed/MathFactory.cpp
+++ b/src/mathed/MathFactory.cpp
@@ -64,8 +64,11 @@
 #include "support/docstream.h"
 #include "support/FileName.h"
 #include "support/filetools.h" // LibFileSearch
+#include "support/gettext.h"
 #include "support/lstrings.h"
+#include "support/Package.h"
 
+#include "frontends/alert.h"
 #include "frontends/FontLoader.h"
 
 #include "Buffer.h"
@@ -88,6 +91,8 @@ namespace {
 
 MathWordList theMathWordList;
 MathVariantList theMathVariantList;
+MathConflictList theMathConflictList;
+std::vector<docstring> theMathConflictCommands;
 
 
 bool isMathFontAvailable(string & name)
@@ -425,6 +430,41 @@ void initVariantSymbols()
 }
 
 
+void initConflictList() {
+       FileName const filename = libFileSearch(string(), "math_conflicts");
+       LYXERR(Debug::MATHED, "read conflict list from " << filename);
+       if (filename.empty()) {
+               lyxerr << "Could not find conflict list file" << endl;
+               return;
+       }
+
+       ifstream fs(filename.toFilesystemEncoding().c_str());
+       // limit the size of strings we read to avoid memory problems
+       fs >> setw(65636);
+       string line;
+
+       while (getline(fs, line)) {
+
+               if (line.empty() || line[0] == '#')
+                       continue;
+
+               docstring command;
+               docstring mathHulls;
+               idocstringstream is(from_utf8(line));
+               is >> command >> mathHulls;
+               docstring hul;
+               vector<docstring> mathHullList;
+               idocstringstream is2(subst(mathHulls, ',', '\n'));
+               while (getline(is2, hul)) {
+                       mathHullList.push_back(hul);
+               }
+
+               theMathConflictList[command] = mathHullList;
+               theMathConflictCommands.push_back(command);
+       }
+}
+
+
 bool isSpecialChar(docstring const & name)
 {
        if (name.size() != 1)
@@ -451,6 +491,18 @@ MathVariantList const & mathedVariantList()
 }
 
 
+MathConflictList const & mathedConflictList()
+{
+       return theMathConflictList;
+}
+
+
+std::vector<docstring> const & mathedConflictCommands()
+{
+       return theMathConflictCommands;
+}
+
+
 void initMath()
 {
        static bool initialized = false;
@@ -459,6 +511,7 @@ void initMath()
                initParser();
                initSymbols();
                initVariantSymbols();
+               initConflictList();
        }
 }
 
diff --git a/src/mathed/MathFactory.h b/src/mathed/MathFactory.h
index 02c4be1c58..b92f5562ae 100644
--- a/src/mathed/MathFactory.h
+++ b/src/mathed/MathFactory.h
@@ -37,6 +37,11 @@ MathWordList const & mathedWordList();
 typedef std::map<docstring, UnicodeVariants> MathVariantList;
 MathVariantList const & mathedVariantList();
 
+typedef std::map<docstring, std::vector<docstring>> MathConflictList;
+MathConflictList const & mathedConflictList();
+
+std::vector<docstring> const & mathedConflictCommands();
+
 } // namespace lyx
 
 #endif
-- 
lyx-cvs mailing list
[email protected]
https://lists.lyx.org/mailman/listinfo/lyx-cvs

Reply via email to