]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/MathMacro.cpp
Reintroduce broken support for default macro arguments
[lyx.git] / src / mathed / MathMacro.cpp
index a5b1803f6d3f50aa743ae1756a28cd5bd9897cfa..f6791bbf7638869e66b98ba5bc6e97a96dd684ba 100644 (file)
@@ -81,7 +81,13 @@ public:
                mrow.push_back(e_beg);
 
                mathMacro_->macro()->unlock();
-               bool has_contents = mathMacro_->cell(idx_).addToMathRow(mrow, mi);
+               bool has_contents;
+               // handle default macro arguments
+               if (!mathMacro_->editMetrics(mi.base.bv)
+                       && mathMacro_->cell(idx_).empty())
+                       has_contents = def_.addToMathRow(mrow, mi);
+               else
+                       has_contents = mathMacro_->cell(idx_).addToMathRow(mrow, mi);
                mathMacro_->macro()->lock();
 
                // if there was no contents, and the contents is editable,
@@ -456,8 +462,10 @@ void MathMacro::metrics(MetricsInfo & mi, Dimension & dim) const
 
        // calculate new metrics according to display mode
        if (d->displayMode_ == DISPLAY_INIT || d->displayMode_ == DISPLAY_INTERACTIVE_INIT) {
+               Changer dummy = mi.base.changeFontSet("lyxtex");
                mathed_string_dim(mi.base.font, from_ascii("\\") + name(), dim);
        } else if (d->displayMode_ == DISPLAY_UNFOLDED) {
+               Changer dummy = mi.base.changeFontSet("lyxtex");
                cell(0).metrics(mi, dim);
                Dimension bsdim;
                mathed_string_dim(mi.base.font, from_ascii("\\"), bsdim);
@@ -508,6 +516,10 @@ void MathMacro::metrics(MetricsInfo & mi, Dimension & dim) const
        } else {
                LBUFERR(d->macro_);
 
+               Changer dummy = (currentMode() == TEXT_MODE)
+                       ? mi.base.font.changeShape(UP_SHAPE)
+                       : Changer();
+
                // calculate metrics, hoping that all cells are seen
                d->macro_->lock();
                d->expanded_.metrics(mi, dim);
@@ -714,9 +726,9 @@ void MathMacro::draw(PainterInfo & pi, int x, int y) const
                ++pi.base.macro_nesting;
 
                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());
+               Changer dummy = (currentMode() == TEXT_MODE)
+                       ? pi.base.font.changeShape(UP_SHAPE)
+                       : Changer();
 
                // warm up cells
                for (size_t i = 0; i < nargs(); ++i)
@@ -1062,6 +1074,15 @@ void MathMacro::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";
@@ -1100,9 +1121,13 @@ void MathMacro::write(WriteStream & os) const
                first = false;
        }
 
-       // add space if there was no argument
-       if (first)
+       // Close the opened brace or add space if there was no argument
+       if (d->optionals_ && inside_macro)
+               os << '}';
+       else if (first)
                os.pendingSpace(true);
+
+       os.insideMacro(inside_macro);
 }