os << "\\" << name();
bool first = true;
- 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<docstring> const & defaults = macroBackup_.defaults();
+ // Optional arguments:
+ // First find last non-empty optional argument
+ idx_type emptyOptFrom = 0;
+ idx_type i = 0;
+ for (; i < cells_.size() && i < optionals_; ++i) {
+ if (!cell(i).empty())
+ emptyOptFrom = i + 1;
+ }
- // 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) << "]";
- }
+ // 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 << " ";
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_ << ']';
-
- // 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)) << "]";
- }
- }
+ }
+ os << "{\\" << name().c_str() << '}';
+ if (numargs_ > 0)
+ os << '[' << numargs_ << ']';
+
+ // 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()) << "}";
if (!cell(displayIdx()).empty())
os << "\n{" << cell(displayIdx()) << '}';
}
-}
int MathMacroTemplate::plaintext(Buffer const & buf, odocstream & os,