diff --git a/src/mathed/MathMacro.cpp b/src/mathed/MathMacro.cpp index 8741dfc705..37dc8aa030 100644 --- a/src/mathed/MathMacro.cpp +++ b/src/mathed/MathMacro.cpp @@ -552,39 +552,28 @@ void MathMacro::write(WriteStream & os) const os << "\\" << name(); bool first = true; + + // Optional arguments: + // First find last non-empty optional argument + idx_type emptyOptFrom = 0; idx_type i = 0; - - // Use macroBackup_ instead of macro_ here, because - // this is outside the metrics/draw calls, hence the macro_ - // variable can point to a MacroData which was freed already. - vector const & defaults = macroBackup_.defaults(); - - // Optional argument - if (os.latex()) { - // Print first optional in LaTeX semantics - if (i < optionals_) { - // the first real optional, the others are non-optional in latex - if (!cell(i).empty()) { - first = false; - os << "[" << cell(0) << "]"; - } - - ++i; - } - } else { - // In lyx mode print all in any case - for (; i < cells_.size() && i < optionals_; ++i) { - first = false; - os << "[" << cell(i) << "]"; - } + for (; i < cells_.size() && i < optionals_; ++i) { + if (!cell(i).empty()) + emptyOptFrom = i + 1; } - + + // print out optionals + for (i=0; i < cells_.size() && i < emptyOptFrom; ++i) { + first = false; + os << "[" << cell(i) << "]"; + } + + // skip the tailing empty optionals + i = optionals_; + // Print remaining macros - // (also the further optional parameters in LaTeX mode!) for (; i < cells_.size(); ++i) { - if (cell(i).empty() && i < optionals_) - os << "{" << defaults[i] << "}"; - else if (cell(i).size() == 1 + if (cell(i).size() == 1 && cell(i)[0].nucleus()->asCharInset()) { if (first) os << " "; diff --git a/src/mathed/MathMacroTemplate.cpp b/src/mathed/MathMacroTemplate.cpp index c892d34673..231633b947 100644 --- a/src/mathed/MathMacroTemplate.cpp +++ b/src/mathed/MathMacroTemplate.cpp @@ -970,40 +970,26 @@ void MathMacroTemplate::write(WriteStream & os) const void MathMacroTemplate::write(WriteStream & os, bool overwriteRedefinition) const { - if (type_ == MacroTypeDef) { - os << "\\def\\" << name().c_str(); - for (int i = 1; i <= numargs_; ++i) - os << '#' << i; - } else { - // newcommand or renewcommand - if (redefinition_ && !overwriteRedefinition) + // newcommand or renewcommand + if (os.latex() && optionals_ > 1) + os << "\\newlyxcommand"; + else { + if (redefinition_) os << "\\renewcommand"; else os << "\\newcommand"; - os << "{\\" << name().c_str() << '}'; - if (numargs_ > 0) - os << '[' << numargs_ << ']'; + } + os << "{\\" << name().c_str() << '}'; + if (numargs_ > 0) + os << '[' << numargs_ << ']'; - // optional values - if (os.latex()) { - // in latex only one optional possible, simulate the others - if (optionals_ >= 1) { - docstring optValue = asString(cell(optIdx(0))); - if (optValue.find(']') != docstring::npos) - os << "[{" << cell(optIdx(0)) << "}]"; - else - os << "[" << cell(optIdx(0)) << "]"; - } - } else { - // in lyx we handle all optionals as real optionals - for (int i = 0; i < optionals_; ++i) { - docstring optValue = asString(cell(optIdx(i))); - if (optValue.find(']') != docstring::npos) - os << "[{" << cell(optIdx(i)) << "}]"; - else - os << "[" << cell(optIdx(i)) << "]"; - } - } + // optional values + for (int i = 0; i < optionals_; ++i) { + docstring optValue = asString(cell(optIdx(i))); + if (optValue.find(']') != docstring::npos) + os << "[{" << cell(optIdx(i)) << "}]"; + else + os << "[" << cell(optIdx(i)) << "]"; } os << "{" << cell(defIdx()) << "}"; @@ -1016,7 +1002,6 @@ void MathMacroTemplate::write(WriteStream & os, bool overwriteRedefinition) cons if (!cell(displayIdx()).empty()) os << "\n{" << cell(displayIdx()) << '}'; } -} int MathMacroTemplate::plaintext(Buffer const & buf, odocstream & os,