]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/InsetMathMacro.cpp
Reduce the number of accesses to coord cache when drawing a math row
[lyx.git] / src / mathed / InsetMathMacro.cpp
index 5743dd0e452b2a12311d51b8a512a9d285f4f1f5..d2c35c7e5bea913e1d857fc319871945de87d0db 100644 (file)
@@ -19,6 +19,7 @@
 #include "MathCompletionList.h"
 #include "MathExtern.h"
 #include "MathFactory.h"
+#include "MathRow.h"
 #include "MathStream.h"
 #include "MathSupport.h"
 
@@ -39,7 +40,7 @@
 #include "support/gettext.h"
 #include "support/lassert.h"
 #include "support/lstrings.h"
-#include "support/RefChanger.h"
+#include "support/Changer.h"
 #include "support/textutils.h"
 
 #include <ostream>
@@ -64,7 +65,7 @@ public:
                        asArray(def, def_);
        }
        ///
-       void setBuffer(Buffer & buffer)
+       void setBuffer(Buffer & buffer) override
        {
                Inset::setBuffer(buffer);
                def_.setBuffer(buffer);
@@ -74,9 +75,9 @@ public:
        ///
        InsetMathMacro const * owner() { return mathMacro_; }
        ///
-       marker_type marker(BufferView const *) const { return NO_MARKER; }
+       marker_type marker(BufferView const *) const override { return marker_type::NO_MARKER; }
        ///
-       InsetCode lyxCode() const { return ARGUMENT_PROXY_CODE; }
+       InsetCode lyxCode() const override { return ARGUMENT_PROXY_CODE; }
        /// The math data to use for display
        MathData const & displayCell(BufferView const * bv) const
        {
@@ -86,12 +87,12 @@ public:
                return use_def_arg ? def_ : mathMacro_->cell(idx_);
        }
        ///
-       bool addToMathRow(MathRow & mrow, MetricsInfo & mi) const
+       bool addToMathRow(MathRow & mrow, MetricsInfo & mi) const override
        {
                // macro arguments are in macros
                LATTEST(mathMacro_->nesting() > 0);
                /// The macro nesting can change display of insets. Change it locally.
-               Changer chg = make_change(mi.base.macro_nesting,
+               Changer chg = changeVar(mi.base.macro_nesting,
                                          mathMacro_->nesting() == 1 ? 0 : mathMacro_->nesting());
 
                MathRow::Element e_beg(mi, MathRow::BEGIN);
@@ -121,17 +122,17 @@ public:
                return has_contents;
        }
        ///
-       void beforeMetrics() const
+       void beforeMetrics() const override
        {
                mathMacro_->macro()->unlock();
        }
        ///
-       void afterMetrics() const
+       void afterMetrics() const override
        {
                mathMacro_->macro()->lock();
        }
        ///
-       void beforeDraw(PainterInfo const & pi) const
+       void beforeDraw(PainterInfo const & pi) const override
        {
                // if the macro is being edited, then the painter is in
                // monochrome mode.
@@ -139,42 +140,42 @@ public:
                        pi.pain.leaveMonochromeMode();
        }
        ///
-       void afterDraw(PainterInfo const & pi) const
+       void afterDraw(PainterInfo const & pi) const override
        {
                if (mathMacro_->editMetrics(pi.base.bv))
                        pi.pain.enterMonochromeMode(Color_mathmacroblend);
        }
        ///
-       void metrics(MetricsInfo &, Dimension &) const {
+       void metrics(MetricsInfo &, Dimension &) const override {
                // This should never be invoked, since InsetArgumentProxy insets are linearized
                LATTEST(false);
        }
        ///
-       void draw(PainterInfo &, int, int) const {
+       void draw(PainterInfo &, int, int) const override {
                // This should never be invoked, since InsetArgumentProxy insets are linearized
                LATTEST(false);
        }
        ///
-       int kerning(BufferView const * bv) const
+       int kerning(BufferView const * bv) const override
        {
                return displayCell(bv).kerning(bv);
        }
        // write(), normalize(), infoize() and infoize2() are not needed since
        // InsetMathMacro uses the definition and not the expanded cells.
        ///
-       void maple(MapleStream & ms) const { ms << mathMacro_->cell(idx_); }
+       void maple(MapleStream & ms) const override { ms << mathMacro_->cell(idx_); }
        ///
-       void maxima(MaximaStream & ms) const { ms << mathMacro_->cell(idx_); }
+       void maxima(MaximaStream & ms) const override { ms << mathMacro_->cell(idx_); }
        ///
-       void mathematica(MathematicaStream & ms) const { ms << mathMacro_->cell(idx_); }
+       void mathematica(MathematicaStream & ms) const override { ms << mathMacro_->cell(idx_); }
        ///
-       void mathmlize(MathStream & ms) const { ms << mathMacro_->cell(idx_); }
+       void mathmlize(MathMLStream & ms) const override { ms << mathMacro_->cell(idx_); }
        ///
-       void htmlize(HtmlStream & ms) const { ms << mathMacro_->cell(idx_); }
+       void htmlize(HtmlStream & ms) const override { ms << mathMacro_->cell(idx_); }
        ///
-       void octave(OctaveStream & os) const { os << mathMacro_->cell(idx_); }
+       void octave(OctaveStream & os) const override { os << mathMacro_->cell(idx_); }
        ///
-       MathClass mathClass() const
+       MathClass mathClass() const override
        {
                return MC_UNKNOWN;
                // This can be refined once the pointer issues are fixed. I did not
@@ -185,7 +186,7 @@ public:
 
 private:
        ///
-       Inset * clone() const
+       Inset * clone() const override
        {
                return new InsetArgumentProxy(*this);
        }
@@ -205,7 +206,7 @@ public:
                : name_(name), displayMode_(DISPLAY_INIT),
                  expanded_(buf), definition_(buf), attachedArgsNum_(0),
                  optionals_(0), nextFoldMode_(true), macroBackup_(buf),
-                 macro_(0), needsUpdate_(false), isUpdating_(false),
+                 macro_(nullptr), needsUpdate_(false), isUpdating_(false),
                  appetite_(9), nesting_(0), limits_(AUTO_LIMITS)
        {
        }
@@ -307,7 +308,11 @@ InsetMathMacro::InsetMathMacro(Buffer * buf, docstring const & name)
 InsetMathMacro::InsetMathMacro(InsetMathMacro const & that)
        : InsetMathNest(that), d(new Private(*that.d))
 {
-       setBuffer(*that.buffer_);
+       // FIXME This should not really be necessary, but when we are
+       // initializing the table of global macros, we create macros
+       // with no associated Buffer.
+       if (that.buffer_)
+               setBuffer(*that.buffer_);
        d->updateChildren(this);
 }
 
@@ -344,11 +349,11 @@ bool InsetMathMacro::addToMathRow(MathRow & mrow, MetricsInfo & mi) const
                return InsetMath::addToMathRow(mrow, mi);
 
        /// The macro nesting can change display of insets. Change it locally.
-       Changer chg = make_change(mi.base.macro_nesting, d->nesting_);
+       Changer chg = changeVar(mi.base.macro_nesting, d->nesting_);
 
        MathRow::Element e_beg(mi, MathRow::BEGIN);
        e_beg.inset = this;
-       e_beg.marker = (d->nesting_ == 1) ? marker(mi.base.bv) : NO_MARKER;
+       e_beg.marker = (d->nesting_ == 1) ? marker(mi.base.bv) : marker_type::NO_MARKER;
        mrow.push_back(e_beg);
 
        d->macro_->lock();
@@ -367,7 +372,7 @@ bool InsetMathMacro::addToMathRow(MathRow & mrow, MetricsInfo & mi) const
 
        MathRow::Element e_end(mi, MathRow::END);
        e_end.inset = this;
-       e_end.marker = (d->nesting_ == 1) ? marker(mi.base.bv) : NO_MARKER;
+       e_end.marker = (d->nesting_ == 1) ? marker(mi.base.bv) : marker_type::NO_MARKER;
        mrow.push_back(e_end);
 
        return has_contents;
@@ -393,14 +398,14 @@ bool InsetMathMacro::allowsLimitsChange() const
 }
 
 
-Limits InsetMathMacro::defaultLimits() const
+Limits InsetMathMacro::defaultLimits(bool display) 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();
+               ? in->defaultLimits(display) : in->limits();
        LATTEST(lim != AUTO_LIMITS);
        return lim;
 }
@@ -540,36 +545,36 @@ bool InsetMathMacro::editMetrics(BufferView const * bv) const
 }
 
 
-InsetMath::marker_type InsetMathMacro::marker(BufferView const * bv) const
+marker_type InsetMathMacro::marker(BufferView const * bv) const
 {
        if (nargs() == 0)
-               return NO_MARKER;
+               return marker_type::NO_MARKER;
 
        switch (d->displayMode_) {
        case DISPLAY_INIT:
        case DISPLAY_INTERACTIVE_INIT:
-               return NO_MARKER;
+               return marker_type::NO_MARKER;
        case DISPLAY_UNFOLDED:
-               return MARKER;
+               return marker_type::MARKER;
        case DISPLAY_NORMAL:
                switch (lyxrc.macro_edit_style) {
                case LyXRC::MACRO_EDIT_INLINE:
-                       return MARKER2;
+                       return marker_type::MARKER2;
                case LyXRC::MACRO_EDIT_INLINE_BOX:
-                       return d->editing_[bv] ? BOX_MARKER : MARKER2;
+                       return d->editing_[bv] ? marker_type::BOX_MARKER : marker_type::MARKER2;
                case LyXRC::MACRO_EDIT_LIST:
-                       return MARKER2;
+                       return marker_type::MARKER2;
                }
        }
        // please gcc 4.6
-       return NO_MARKER;
+       return marker_type::NO_MARKER;
 }
 
 
 void InsetMathMacro::metrics(MetricsInfo & mi, Dimension & dim) const
 {
        /// The macro nesting can change display of insets. Change it locally.
-       Changer chg = make_change(mi.base.macro_nesting, d->nesting_);
+       Changer chg = changeVar(mi.base.macro_nesting, d->nesting_);
 
        // set edit mode for which we will have calculated metrics. But only
        d->editing_[mi.base.bv] = editMode(mi.base.bv);
@@ -649,7 +654,7 @@ void InsetMathMacro::updateMacro(MacroContext const & mc)
                        d->needsUpdate_ = true;
                }
        } else {
-               d->macro_ = 0;
+               d->macro_ = nullptr;
        }
 }
 
@@ -684,7 +689,7 @@ void InsetMathMacro::updateRepresentation(Cursor * cur, MacroContext const & mc,
        UpdateLocker locker(*this);
 
        // known macro?
-       if (d->macro_ == 0)
+       if (d->macro_ == nullptr)
                return;
 
        // remember nesting level of this macro
@@ -837,7 +842,7 @@ void InsetMathMacro::setDisplayMode(InsetMathMacro::DisplayMode mode, int appeti
 
 InsetMathMacro::DisplayMode InsetMathMacro::computeDisplayMode() const
 {
-       if (d->nextFoldMode_ == true && d->macro_ && !d->macro_->locked())
+       if (d->nextFoldMode_ && d->macro_ && !d->macro_->locked())
                return DISPLAY_NORMAL;
        else
                return DISPLAY_UNFOLDED;
@@ -859,10 +864,10 @@ bool InsetMathMacro::validName() const
 
        // valid characters?
        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] != '*')
+               for (char_type c : n) {
+                       if (!(c >= 'a' && c <= 'z')
+                           && !(c >= 'A' && c <= 'Z')
+                           && c != '*')
                                return false;
                }
        }
@@ -1000,7 +1005,7 @@ Inset * InsetMathMacro::editXY(Cursor & cur, int x, int y)
 }
 
 
-void InsetMathMacro::removeArgument(Inset::pos_type pos) {
+void InsetMathMacro::removeArgument(pos_type pos) {
        if (d->displayMode_ == DISPLAY_NORMAL) {
                LASSERT(size_t(pos) < cells_.size(), return);
                cells_.erase(cells_.begin() + pos);
@@ -1015,7 +1020,7 @@ void InsetMathMacro::removeArgument(Inset::pos_type pos) {
 }
 
 
-void InsetMathMacro::insertArgument(Inset::pos_type pos) {
+void InsetMathMacro::insertArgument(pos_type pos) {
        if (d->displayMode_ == DISPLAY_NORMAL) {
                LASSERT(size_t(pos) <= cells_.size(), return);
                cells_.insert(cells_.begin() + pos, MathData());
@@ -1128,7 +1133,7 @@ bool InsetMathMacro::folded() const
 }
 
 
-void InsetMathMacro::write(WriteStream & os) const
+void InsetMathMacro::write(TeXMathStream & os) const
 {
        mode_type mode = currentMode();
        MathEnsurer ensurer(os, mode == MATH_MODE, true, mode == TEXT_MODE);
@@ -1172,10 +1177,10 @@ void InsetMathMacro::write(WriteStream & os) const
                // contains macros with optionals.
                bool braced = false;
                size_type last = cell(i).size() - 1;
-               if (cell(i).size() && cell(i)[last]->asUnknownInset()) {
+               if (!cell(i).empty() && 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()) {
+               } else if (!cell(i).empty() && cell(i)[0]->asScriptInset()) {
                        braced = cell(i)[0]->asScriptInset()->nuc().empty();
                } else {
                        for (size_type j = 0; j < cell(i).size(); ++j) {
@@ -1209,8 +1214,11 @@ void InsetMathMacro::write(WriteStream & os) const
        }
 
        // add space if there was no argument
-       if (first)
+       // or add braces if we have optionals but none are present and [ follows
+       if (first) {
                os.pendingSpace(true);
+               os.useBraces(d->optionals_ > 0);
+       }
 
        // write \(no)limits modifiers if relevant
        writeLimits(os);
@@ -1235,7 +1243,7 @@ void InsetMathMacro::mathematica(MathematicaStream & os) const
 }
 
 
-void InsetMathMacro::mathmlize(MathStream & ms) const
+void InsetMathMacro::mathmlize(MathMLStream & ms) const
 {
        // macro_ is 0 if this is an unknown macro
        LATTEST(d->macro_ || d->displayMode_ != DISPLAY_NORMAL);