]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/MathMacro.cpp
... and RELEASE-NOTES
[lyx.git] / src / mathed / MathMacro.cpp
index e772bc1b04eec024a6dceee9e0f2a2f950971af5..06a55bf7036d391756f80417f045d6e57e52d092 100644 (file)
@@ -22,7 +22,6 @@
 #include "MathSupport.h"
 
 #include "Buffer.h"
-#include "BufferList.h"
 #include "BufferView.h"
 #include "CoordCache.h"
 #include "Cursor.h"
@@ -31,6 +30,7 @@
 #include "LaTeXFeatures.h"
 #include "LyX.h"
 #include "LyXRC.h"
+#include "MetricsInfo.h"
 
 #include "frontends/Painter.h"
 
@@ -64,6 +64,8 @@ public:
        ///
        void setOwner(MathMacro * mathMacro) { mathMacro_ = mathMacro; }
        ///
+       MathMacro const * owner() { return mathMacro_; }
+       ///
        InsetCode lyxCode() const { return ARGUMENT_PROXY_CODE; }
        ///
        void metrics(MetricsInfo & mi, Dimension & dim) const {
@@ -152,6 +154,9 @@ public:
        /// This needs to be called every time a copy of the owner is created
        /// (bug 9418).
        void updateChildren(MathMacro * owner);
+       /// Recursively update the pointers of all expanded macros
+       /// appearing in the arguments of the current macro
+       void updateNestedChildren(MathMacro * owner, InsetMathNest * ni);
        /// name of macro
        docstring name_;
        /// current display mode
@@ -191,6 +196,42 @@ void MathMacro::Private::updateChildren(MathMacro * owner)
                ArgumentProxy * p = dynamic_cast<ArgumentProxy *>(expanded_[i].nucleus());
                if (p)
                        p->setOwner(owner);
+
+               InsetMathNest * ni = expanded_[i].nucleus()->asNestInset();
+               if (ni)
+                       updateNestedChildren(owner, ni);
+       }
+
+       if (macro_) {
+               // The macro_ pointer is updated when MathData::metrics() is
+               // called. However, when instant preview is on or the macro is
+               // not on screen, MathData::metrics() is not called and we may
+               // have a dangling pointer. As a safety measure, when a macro
+               // is copied, always let macro_ point to the backup copy of the
+               // MacroData structure. This backup is updated every time the
+               // macro is changed, so it will not become stale.
+               macro_ = &macroBackup_;
+       }
+}
+
+
+void MathMacro::Private::updateNestedChildren(MathMacro * owner, InsetMathNest * ni)
+{
+       for (size_t i = 0; i < ni->nargs(); ++i) {
+               MathData & ar = ni->cell(i);
+               for (size_t j = 0; j < ar.size(); ++j) {
+                       ArgumentProxy * ap = dynamic_cast
+                               <ArgumentProxy *>(ar[j].nucleus());
+                       if (ap) {
+                               MathMacro::Private * md = ap->owner()->d;
+                               if (md->macro_)
+                                       md->macro_ = &md->macroBackup_;
+                               ap->setOwner(owner);
+                       }
+                       InsetMathNest * imn = ar[j].nucleus()->asNestInset();
+                       if (imn)
+                               updateNestedChildren(owner, imn);
+               }
        }
 }
 
@@ -203,18 +244,8 @@ MathMacro::MathMacro(Buffer * buf, docstring const & name)
 MathMacro::MathMacro(MathMacro const & that)
        : InsetMathNest(that), d(new Private(*that.d))
 {
+       setBuffer(*that.buffer_);
        d->updateChildren(this);
-       if (d->macro_ && lyxrc.preview == LyXRC::PREVIEW_ON) {
-               // We need to update d->macro_ by ourselves because in this case
-               // MathData::metrics() is not called when selecting a math inset
-               DocIterator const & pos = d->macroBackup_.pos();
-               Buffer const * buf = pos.buffer();
-               if (buf && !theBufferList().isLoaded(buf))
-                       buf = 0;
-               d->macro_ = buf ? buf->getMacro(name(), pos) : 0;
-               if (!d->macro_)
-                       d->macro_ = &d->macroBackup_;
-       }
 }
 
 
@@ -225,17 +256,6 @@ MathMacro & MathMacro::operator=(MathMacro const & that)
        InsetMathNest::operator=(that);
        *d = *that.d;
        d->updateChildren(this);
-       if (d->macro_ && lyxrc.preview == LyXRC::PREVIEW_ON) {
-               // We need to update d->macro_ by ourselves because in this case
-               // MathData::metrics() is not called when selecting a math inset
-               DocIterator const & pos = d->macroBackup_.pos();
-               Buffer const * buf = pos.buffer();
-               if (buf && !theBufferList().isLoaded(buf))
-                       buf = 0;
-               d->macro_ = buf ? buf->getMacro(name(), pos) : 0;
-               if (!d->macro_)
-                       d->macro_ = &d->macroBackup_;
-       }
        return *this;
 }
 
@@ -413,7 +433,7 @@ void MathMacro::metrics(MetricsInfo & mi, Dimension & dim) const
                if (lyxrc.macro_edit_style == LyXRC::MACRO_EDIT_INLINE_BOX
                    && d->editing_[mi.base.bv]) {
                        FontInfo font = mi.base.font;
-                       augmentFont(font, from_ascii("lyxtex"));
+                       augmentFont(font, "lyxtex");
                        Dimension namedim;
                        mathed_string_dim(font, name(), namedim);
 #if 0
@@ -532,10 +552,10 @@ void MathMacro::draw(PainterInfo & pi, int x, int y) const
        int expy = y;
 
        if (d->displayMode_ == DISPLAY_INIT || d->displayMode_ == DISPLAY_INTERACTIVE_INIT) {
-               FontSetChanger dummy(pi.base, "lyxtex");
+               Changer dummy = pi.base.changeFontSet("lyxtex");
                pi.pain.text(x, y, from_ascii("\\") + name(), pi.base.font);
        } else if (d->displayMode_ == DISPLAY_UNFOLDED) {
-               FontSetChanger dummy(pi.base, "lyxtex");
+               Changer dummy = pi.base.changeFontSet("lyxtex");
                pi.pain.text(x, y, from_ascii("\\"), pi.base.font);
                x += mathed_string_width(pi.base.font, from_ascii("\\")) + 1;
                cell(0).draw(pi, x, y);
@@ -594,6 +614,9 @@ void MathMacro::draw(PainterInfo & pi, int x, int y) const
                drawMarkers2(pi, expx, expy);
        } else {
                bool drawBox = lyxrc.macro_edit_style == LyXRC::MACRO_EDIT_INLINE_BOX;
+               bool upshape = currentMode() == TEXT_MODE;
+               Changer dummy = pi.base.font.changeShape(upshape ? UP_SHAPE
+                                                       : pi.base.font.shape());
 
                // warm up cells
                for (size_t i = 0; i < nargs(); ++i)
@@ -602,7 +625,7 @@ void MathMacro::draw(PainterInfo & pi, int x, int y) const
                if (drawBox && d->editing_[pi.base.bv]) {
                        // draw header and rectangle around
                        FontInfo font = pi.base.font;
-                       augmentFont(font, from_ascii("lyxtex"));
+                       augmentFont(font, "lyxtex");
                        font.setSize(FONT_SIZE_TINY);
                        font.setColor(Color_mathmacrolabel);
                        Dimension namedim;
@@ -728,6 +751,17 @@ size_t MathMacro::appetite() const
 }
 
 
+InsetMath::mode_type MathMacro::currentMode() const
+{
+       // User defined macros are always assumed to be mathmode macros.
+       // Only the global macros defined in lib/symbols may be textmode.
+
+       MacroData const * data = MacroTable::globalMacros().get(name());
+       bool textmode = data && data->symbol() && data->symbol()->extra == "textmode";
+       return textmode ? TEXT_MODE : MATH_MODE;
+}
+
+
 void MathMacro::validate(LaTeXFeatures & features) const
 {
        // Immediately after a document is loaded, in some cases the MacroData
@@ -905,7 +939,13 @@ bool MathMacro::folded() const
 
 void MathMacro::write(WriteStream & os) const
 {
-       MathEnsurer ensurer(os, d->macro_ != 0, true);
+       MacroData const * data = MacroTable::globalMacros().get(name());
+       bool textmode_macro = data && data->symbol()
+                                  && data->symbol()->extra == "textmode";
+       bool needs_mathmode = data && (!data->symbol()
+                                      || data->symbol()->extra != "textmode");
+
+       MathEnsurer ensurer(os, needs_mathmode, true, textmode_macro);
 
        // non-normal mode
        if (d->displayMode_ != DISPLAY_NORMAL) {
@@ -989,7 +1029,7 @@ void MathMacro::mathmlize(MathStream & os) const
                docstring const xmlname = d->macro_->xmlname();
                if (!xmlname.empty()) {
                        char const * type = d->macro_->MathMLtype();
-                       os << '<' << type << "> " << xmlname << " /<"
+                       os << '<' << type << "> " << xmlname << " </"
                           << type << '>';
                        return;
                }