]> git.lyx.org Git - features.git/blobdiff - src/mathed/InsetMathScript.cpp
Revert part of 503c7c16: InsetMathNest:edit resets anchor.
[features.git] / src / mathed / InsetMathScript.cpp
index cbef2fafb10bf58369e3ef80d4eaf9734474ea8c..73e1ef9d91976a6eb793810fe09356de3226aca3 100644 (file)
@@ -253,6 +253,23 @@ int InsetMathScript::nker(BufferView const * bv) const
 }
 
 
+Limits InsetMathScript::limits() const
+{
+       if (nuc().empty())
+               return AUTO_LIMITS;
+       else
+               // only the limits status of the last element counts
+               return nuc().back()->limits();
+}
+
+
+void InsetMathScript::limits(Limits lim)
+{
+       if (!nuc().empty())
+               nuc().back()->limits(lim);
+}
+
+
 MathClass InsetMathScript::mathClass() const
 {
        // FIXME: this is a hack, since the class will not be correct if
@@ -272,7 +289,7 @@ MathClass InsetMathScript::mathClass() const
 void InsetMathScript::metrics(MetricsInfo & mi, Dimension & dim) const
 {
        // we store this, because it is much easier
-       has_limits_ = hasLimits(mi.base.font);
+       has_limits_ = hasLimits(mi.base.font.style());
 
        // Compute metrics of the available cells
        Dimension dim0;
@@ -368,15 +385,14 @@ void InsetMathScript::drawT(TextPainter & pain, int x, int y) const
 }
 
 
-bool InsetMathScript::hasLimits(FontInfo const & font) const
+bool InsetMathScript::hasLimits(MathStyle const & style) const
 {
-       if (font.style() != DISPLAY_STYLE)
-               return false;
        if (nuc().empty())
                return false;
 
        Limits const lim = nuc().back()->limits() == AUTO_LIMITS
-               ? nuc().back()->defaultLimits() : nuc().back()->limits();
+               ? nuc().back()->defaultLimits(style == DISPLAY_STYLE)
+               : nuc().back()->limits();
        LASSERT(lim != AUTO_LIMITS, return false);
        return lim == LIMITS;
 }
@@ -388,7 +404,7 @@ void InsetMathScript::removeScript(bool up)
                if (up == cell_1_is_up_)
                        cells_.pop_back();
        } else if (nargs() == 3) {
-               if (up == true) {
+               if (up) {
                        swap(cells_[1], cells_[2]);
                        cell_1_is_up_ = false;
                } else {
@@ -421,7 +437,7 @@ bool InsetMathScript::hasDown() const
 }
 
 
-Inset::idx_type InsetMathScript::idxOfScript(bool up) const
+idx_type InsetMathScript::idxOfScript(bool up) const
 {
        if (nargs() == 1)
                return 0;
@@ -489,13 +505,16 @@ bool InsetMathScript::idxUpDown(Cursor & cur, bool up) const
 }
 
 
-void InsetMathScript::write(WriteStream & os) const
+void InsetMathScript::write(TeXMathStream & os) const
 {
        MathEnsurer ensurer(os);
 
-       if (!nuc().empty())
+       if (!nuc().empty()) {
                os << nuc();
-       else if (os.firstitem())
+               // Avoid double superscript errors (bug 1633)
+               if (os.latex() && hasUp() && nuc().back()->getChar() == '\'')
+                       os << "{}";
+       } else if (os.firstitem())
                LYXERR(Debug::MATHED, "suppressing {} when writing");
        else
                os << "{}";
@@ -510,8 +529,13 @@ void InsetMathScript::write(WriteStream & os) const
                    (up().size() == 1 && up().back()->asBraceInset() &&
                     up().back()->asBraceInset()->cell(0).empty())))
                        os << "^ {}";
-               else
+               else {
                        os << "^{" << up() << '}';
+                       // Avoid double superscript errors by writing an
+                       // empty group {} when a prime immediately follows
+                       if (os.latex())
+                               os.useBraces(true);
+               }
        }
 
        if (lock_ && !os.latex())
@@ -578,33 +602,39 @@ void InsetMathScript::mathematica(MathematicaStream & os) const
 }
 
 
-void InsetMathScript::mathmlize(MathStream & ms) const
+void InsetMathScript::mathmlize(MathMLStream & ms) const
 {
        bool d = hasDown() && !down().empty();
        bool u = hasUp() && !up().empty();
-       // FIXME: the MathStream should be able to give us this information
-       bool l = has_limits_;
+       bool has_limits = hasLimits(ms.getFontMathStyle());
+
+       if (!d && !u)
+               return;
 
+       const char * tag = nullptr;
        if (u && d)
-               ms << MTag(l ? "munderover" : "msubsup");
+               tag = has_limits ? "munderover" : "msubsup";
        else if (u)
-               ms << MTag(l ? "mover" : "msup");
+               tag = has_limits ? "mover" : "msup";
        else if (d)
-               ms << MTag(l ? "munder" : "msub");
+               tag = has_limits ? "munder" : "msub";
+
+       ms << MTag(tag);
 
        if (!nuc().empty())
-               ms << MTag("mrow") << nuc() << ETag("mrow");
+               ms << nuc();
        else
-               ms << "<" << from_ascii(ms.namespacedTag("mrow")) << " />";
+               // TODO: is this empty <mrow> required?
+               ms << CTag("mrow");
 
-       if (u && d)
-               ms << MTag("mrow") << down() << ETag("mrow")
-                  << MTag("mrow") << up() << ETag("mrow")
-                  << ETag(l ? "munderover" : "msubsup");
-       else if (u)
-               ms << MTag("mrow") << up() << ETag("mrow") << ETag(l ? "mover" : "msup");
-       else if (d)
-               ms << MTag("mrow") << down() << ETag("mrow") << ETag(l ? "munder" : "msub");
+       // No need to wrap these in an <mrow>, as it's done by MathExtern.
+       // More details in https://www.lyx.org/trac/ticket/12221#comment:10.
+       if (d)
+               ms << down();
+       if (u)
+               ms << up();
+
+       ms << ETag(tag);
 }