]> git.lyx.org Git - features.git/blobdiff - src/mathed/InsetMathMacro.cpp
Avoid spurious requirements
[features.git] / src / mathed / InsetMathMacro.cpp
index 3a8ec4de0acd8ddda52d837a8db8c58de56f747e..5743dd0e452b2a12311d51b8a512a9d285f4f1f5 100644 (file)
@@ -15,6 +15,7 @@
 #include "InsetMathMacro.h"
 
 #include "InsetMathChar.h"
+#include "InsetMathScript.h"
 #include "MathCompletionList.h"
 #include "MathExtern.h"
 #include "MathFactory.h"
@@ -63,6 +64,12 @@ public:
                        asArray(def, def_);
        }
        ///
+       void setBuffer(Buffer & buffer)
+       {
+               Inset::setBuffer(buffer);
+               def_.setBuffer(buffer);
+       }
+       ///
        void setOwner(InsetMathMacro * mathMacro) { mathMacro_ = mathMacro; }
        ///
        InsetMathMacro const * owner() { return mathMacro_; }
@@ -135,7 +142,7 @@ public:
        void afterDraw(PainterInfo const & pi) const
        {
                if (mathMacro_->editMetrics(pi.base.bv))
-                       pi.pain.enterMonochromeMode(Color_mathbg, Color_mathmacroblend);
+                       pi.pain.enterMonochromeMode(Color_mathmacroblend);
        }
        ///
        void metrics(MetricsInfo &, Dimension &) const {
@@ -199,7 +206,7 @@ public:
                  expanded_(buf), definition_(buf), attachedArgsNum_(0),
                  optionals_(0), nextFoldMode_(true), macroBackup_(buf),
                  macro_(0), needsUpdate_(false), isUpdating_(false),
-                 appetite_(9), nesting_(0)
+                 appetite_(9), nesting_(0), limits_(AUTO_LIMITS)
        {
        }
        /// Update the pointers to our owner of all expanded macros.
@@ -232,7 +239,7 @@ public:
        ///
        mutable std::map<BufferView const *, bool> editing_;
        ///
-       std::string requires_;
+       std::string required_;
        /// update macro representation
        bool needsUpdate_;
        ///
@@ -241,6 +248,8 @@ public:
        size_t appetite_;
        /// Level of nesting in macros (including this one)
        int nesting_;
+       ///
+       Limits limits_;
 };
 
 
@@ -364,6 +373,51 @@ bool InsetMathMacro::addToMathRow(MathRow & mrow, MetricsInfo & mi) const
        return has_contents;
 }
 
+
+/// Whether the inset allows \(no)limits
+bool InsetMathMacro::allowsLimitsChange() const
+{
+       // similar to the code in mathClass(), except that we search for
+       // the right-side class.
+       MathClass mc = MC_UNKNOWN;
+       if (MacroData const * m = macroBackup()) {
+               // If it is a global macro and is defined explicitly
+               if (m->symbol())
+                       mc = string_to_class(m->symbol()->extra);
+       }
+       // Otherwise guess from the expanded macro
+       if (mc == MC_UNKNOWN)
+               mc = d->expanded_.lastMathClass();
+
+       return mc == MC_OP;
+}
+
+
+Limits InsetMathMacro::defaultLimits() const
+{
+       if (d->expanded_.empty())
+               return NO_LIMITS;
+       // Guess from the expanded macro
+       InsetMath const * in = d->expanded_.back().nucleus();
+       Limits const lim = in->limits() == AUTO_LIMITS
+               ? in->defaultLimits() : in->limits();
+       LATTEST(lim != AUTO_LIMITS);
+       return lim;
+}
+
+
+Limits InsetMathMacro::limits() const
+{
+       return d->limits_;
+}
+
+
+void InsetMathMacro::limits(Limits lim)
+{
+       d->limits_ = lim;
+}
+
+
 void InsetMathMacro::beforeMetrics() const
 {
        d->macro_->lock();
@@ -379,7 +433,7 @@ void InsetMathMacro::afterMetrics() const
 void InsetMathMacro::beforeDraw(PainterInfo const & pi) const
 {
        if (d->editing_[pi.base.bv])
-               pi.pain.enterMonochromeMode(Color_mathbg, Color_mathmacroblend);
+               pi.pain.enterMonochromeMode(Color_mathmacroblend);
 }
 
 
@@ -637,7 +691,7 @@ void InsetMathMacro::updateRepresentation(Cursor * cur, MacroContext const & mc,
        d->nesting_ = nesting;
 
        // update requires
-       d->requires_ = d->macro_->requires();
+       d->required_ = d->macro_->required();
 
        if (!d->needsUpdate_
                // non-normal mode? We are done!
@@ -660,17 +714,21 @@ void InsetMathMacro::updateRepresentation(Cursor * cur, MacroContext const & mc,
                values[i].insert(0, MathAtom(proxy));
        }
        // expanding macro with the values
-       // Only update the argument macros if anything was expanded, otherwise
-       // we would get an endless loop (bug 9140). UpdateLocker does not work
-       // in this case, since MacroData::expand() creates new InsetMathMacro
-       // objects, so this would be a different recursion path than the one
-       // protected by UpdateLocker.
-       if (d->macro_->expand(values, d->expanded_)) {
+       // Only update the argument macros if anything was expanded or the LyX
+       // representation part does not contain the macro itself, otherwise we
+       // would get an endless loop (bugs 9140 and 11595). UpdateLocker does
+       // not work in this case, since MacroData::expand() creates new
+       // InsetMathMacro objects, so this would be a different recursion path
+       // than the one protected by UpdateLocker.
+       docstring const & display = d->macro_->display();
+       docstring const latexname = from_ascii("\\") + macroName();
+       bool const ret = d->macro_->expand(values, d->expanded_);
+       d->expanded_.setBuffer(buffer());
+       if (ret && !support::contains(display, latexname)) {
                if (utype == OutputUpdate && !d->expanded_.empty())
                        d->expanded_.updateMacros(cur, mc, utype, nesting);
        }
        // get definition for list edit mode
-       docstring const & display = d->macro_->display();
        asArray(display.empty() ? d->macro_->definition() : display,
                d->definition_, Parse::QUIET | Parse::MACRODEF);
 }
@@ -733,7 +791,7 @@ void InsetMathMacro::draw(PainterInfo & pi, int x, int y) const
                        pi.pain.text(x, y, from_ascii(":"), labelFont);
                        x += strw2;
 
-                       // draw paramter
+                       // draw parameter
                        cell(i).draw(pi, x, y);
 
                        // next line
@@ -800,11 +858,13 @@ bool InsetMathMacro::validName() const
                return false;*/
 
        // valid characters?
-       for (size_t i = 0; i<n.size(); ++i) {
-               if (!(n[i] >= 'a' && n[i] <= 'z')
-                   && !(n[i] >= 'A' && n[i] <= 'Z')
-                   && n[i] != '*')
-                       return false;
+       if (n.size() > 1) {
+               for (size_t i = 0; i<n.size(); ++i) {
+                       if (!(n[i] >= 'a' && n[i] <= 'z')
+                           && !(n[i] >= 'A' && n[i] <= 'Z')
+                           && n[i] != '*')
+                               return false;
+               }
        }
 
        return true;
@@ -893,13 +953,13 @@ void InsetMathMacro::validate(LaTeXFeatures & features) const
        // instant preview is on for math, in which case we will be missing
        // the corresponding requirements.
        // In this case, we get the required info from the global macro table.
-       if (!d->requires_.empty())
-               features.require(d->requires_);
+       if (!d->required_.empty())
+               features.require(d->required_);
        else if (!d->macro_) {
                // Update requires for known global macros.
                MacroData const * data = MacroTable::globalMacros().get(name());
-               if (data && !data->requires().empty())
-                       features.require(data->requires());
+               if (data && !data->required().empty())
+                       features.require(data->required());
        }
 
        if (name() == "binom")
@@ -907,7 +967,16 @@ void InsetMathMacro::validate(LaTeXFeatures & features) const
 
        // validate the cells and the definition
        if (displayMode() == DISPLAY_NORMAL) {
-               d->definition_.validate(features);
+               // Don't update requirements if the macro comes from
+               // the symbols file and has not been redefined.
+               MathWordList const & words = mathedWordList();
+               MathWordList::const_iterator it = words.find(name());
+               MacroNameSet macros;
+               buffer().listMacroNames(macros);
+               if (it == words.end() || it->second.inset != "macro"
+                   || macros.find(name()) != macros.end()) {
+                       d->definition_.validate(features);
+               }
                InsetMathNest::validate(features);
        }
 }
@@ -1076,15 +1145,6 @@ void InsetMathMacro::write(WriteStream & os) const
        // we should be ok to continue even if this fails.
        LATTEST(d->macro_);
 
-       // We may already be in the argument of a macro
-       bool const inside_macro = os.insideMacro();
-       os.insideMacro(true);
-
-       // Enclose in braces to avoid latex errors with xargs if we have
-       // optional arguments and are in the optional argument of a macro
-       if (d->optionals_ && inside_macro)
-               os << '{';
-
        // Always protect macros in a fragile environment
        if (os.fragile())
                os << "\\protect";
@@ -1104,7 +1164,32 @@ void InsetMathMacro::write(WriteStream & os) const
        // print out optionals
        for (i=0; i < cells_.size() && i < emptyOptFrom; ++i) {
                first = false;
-               os << "[" << cell(i) << "]";
+               // For correctly parsing it when a document is reloaded, we
+               // need to enclose an optional argument in braces if it starts
+               // with a script inset with empty nucleus or ends with a
+               // delimiter-size-modifier macro (see #10497 and #11346).
+               // We also need to do that when the optional argument
+               // contains macros with optionals.
+               bool braced = false;
+               size_type last = cell(i).size() - 1;
+               if (cell(i).size() && cell(i)[last]->asUnknownInset()) {
+                       latexkeys const * l = in_word_set(cell(i)[last]->name());
+                       braced = (l && l->inset == "big");
+               } else if (cell(i).size() && cell(i)[0]->asScriptInset()) {
+                       braced = cell(i)[0]->asScriptInset()->nuc().empty();
+               } else {
+                       for (size_type j = 0; j < cell(i).size(); ++j) {
+                               InsetMathMacro const * ma = cell(i)[j]->asMacro();
+                               if (ma && ma->optionals()) {
+                                       braced = true;
+                                       break;
+                               }
+                       }
+               }
+               if (braced)
+                       os << "[{" << cell(i) << "}]";
+               else
+                       os << "[" << cell(i) << "]";
        }
 
        // skip the tailing empty optionals
@@ -1123,13 +1208,12 @@ void InsetMathMacro::write(WriteStream & os) const
                first = false;
        }
 
-       // Close the opened brace or add space if there was no argument
-       if (d->optionals_ && inside_macro)
-               os << '}';
-       else if (first)
+       // add space if there was no argument
+       if (first)
                os.pendingSpace(true);
 
-       os.insideMacro(inside_macro);
+       // write \(no)limits modifiers if relevant
+       writeLimits(os);
 }
 
 
@@ -1151,16 +1235,17 @@ void InsetMathMacro::mathematica(MathematicaStream & os) const
 }
 
 
-void InsetMathMacro::mathmlize(MathStream & os) const
+void InsetMathMacro::mathmlize(MathStream & ms) const
 {
        // macro_ is 0 if this is an unknown macro
        LATTEST(d->macro_ || d->displayMode_ != DISPLAY_NORMAL);
        if (d->macro_) {
-               docstring const xmlname = d->macro_->xmlname();
+               docstring const xmlname = (ms.xmlMode()) ? d->macro_->xmlname() : d->macro_->htmlname();
                if (!xmlname.empty()) {
                        char const * type = d->macro_->MathMLtype();
-                       os << '<' << type << "> " << xmlname << " </"
-                          << type << '>';
+                       ms << "<" << from_ascii(ms.namespacedTag(type)) << ">"
+                          << xmlname
+                          << "</" << from_ascii(ms.namespacedTag(type)) << ">";
                        return;
                }
        }
@@ -1168,7 +1253,7 @@ void InsetMathMacro::mathmlize(MathStream & os) const
                // this means that we do not recognize the macro
                throw MathExportException();
        }
-       os << d->expanded_;
+       ms << d->expanded_;
 }
 
 
@@ -1177,7 +1262,7 @@ void InsetMathMacro::htmlize(HtmlStream & os) const
        // macro_ is 0 if this is an unknown macro
        LATTEST(d->macro_ || d->displayMode_ != DISPLAY_NORMAL);
        if (d->macro_) {
-               docstring const xmlname = d->macro_->xmlname();
+               docstring const xmlname = d->macro_->htmlname();
                if (!xmlname.empty()) {
                        os << ' ' << xmlname << ' ';
                        return;
@@ -1215,7 +1300,6 @@ bool InsetMathMacro::completionSupported(Cursor const & cur) const
                return InsetMathNest::completionSupported(cur);
 
        return lyxrc.completion_popup_math
-               && displayMode() == DISPLAY_UNFOLDED
                && cur.bv().cursor().pos() == int(name().size());
 }
 
@@ -1226,7 +1310,6 @@ bool InsetMathMacro::inlineCompletionSupported(Cursor const & cur) const
                return InsetMathNest::inlineCompletionSupported(cur);
 
        return lyxrc.completion_inline_math
-               && displayMode() == DISPLAY_UNFOLDED
                && cur.bv().cursor().pos() == int(name().size());
 }