]> 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 34b5160a5f84dc90ff13109ef26643457ca5a754..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,
@@ -1068,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";
@@ -1106,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);
 }