2 * \file MathMacroTemplate.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "MathMacroTemplate.h"
15 #include "DocIterator.h"
16 #include "InsetMathBrace.h"
17 #include "InsetMathChar.h"
18 #include "InsetMathSqrt.h"
19 #include "MathMacro.h"
20 #include "MathMacroArgument.h"
21 #include "MathStream.h"
22 #include "MathParser.h"
23 #include "MathSupport.h"
24 #include "MathMacroArgument.h"
27 #include "BufferView.h"
30 #include "support/debug.h"
31 #include "DispatchResult.h"
32 #include "FuncRequest.h"
33 #include "FuncStatus.h"
34 #include "support/gettext.h"
38 #include "frontends/FontMetrics.h"
39 #include "frontends/Painter.h"
41 #include "support/convert.h"
42 #include "support/docstream.h"
43 #include "support/lstrings.h"
45 #include "support/debug.h"
53 using support::bformat;
55 //////////////////////////////////////////////////////////////////////
57 class InsetLabelBox : public InsetMathNest {
60 InsetLabelBox(MathAtom const & atom, docstring label,
61 MathMacroTemplate const & parent, bool frame = false);
62 InsetLabelBox(docstring label, MathMacroTemplate const & parent,
65 void metrics(MetricsInfo & mi, Dimension & dim) const;
67 void draw(PainterInfo &, int x, int y) const;
71 MathMacroTemplate const & parent_;
73 Inset * clone() const;
75 docstring const label_;
81 InsetLabelBox::InsetLabelBox(MathAtom const & atom, docstring label,
82 MathMacroTemplate const & parent, bool frame)
83 : InsetMathNest(1), parent_(parent), label_(label), frame_(frame)
85 cell(0).insert(0, atom);
89 InsetLabelBox::InsetLabelBox(docstring label,
90 MathMacroTemplate const & parent, bool frame)
91 : InsetMathNest(1), parent_(parent), label_(label), frame_(frame)
96 Inset * InsetLabelBox::clone() const
98 return new InsetLabelBox(*this);
102 void InsetLabelBox::metrics(MetricsInfo & mi, Dimension & dim) const
105 cell(0).metrics(mi, dim);
114 // adjust to common height in main metrics phase
115 if (!parent_.premetrics()) {
116 dim.asc = max(dim.asc, parent_.commonLabelBoxAscent());
117 dim.des = max(dim.des, parent_.commonLabelBoxDescent());
121 if (parent_.editing(mi.base.bv) && label_.length() > 0) {
123 FontInfo font = sane_font;
124 font.setSize(FONT_SIZE_TINY);
125 font.setColor(Color_mathmacrolabel);
127 // make space for label and box
128 int lwid = mathed_string_width(font, label_);
131 math_font_max_dim(font, maxasc, maxdes);
133 dim.wid = max(dim.wid, lwid + 2);
135 // space for the label
136 if (!parent_.premetrics())
137 dim.des += maxasc + maxdes + 1;
140 setDimCache(mi, dim);
144 void InsetLabelBox::draw(PainterInfo & pi, int x, int y) const
146 Dimension const dim = dimension(*pi.base.bv);
147 Dimension const cdim = cell(0).dimension(*pi.base.bv);
150 cell(0).draw(pi, x + (dim.wid - cdim.wid) / 2, y);
153 if (parent_.editing(pi.base.bv) && label_.length() > 0) {
155 FontInfo font = sane_font;
156 font.setSize(FONT_SIZE_TINY);
157 font.setColor(Color_mathmacrolabel);
159 // make space for label and box
160 int lwid = mathed_string_width(font, label_);
163 math_font_max_dim(font, maxasc, maxdes);
166 pi.pain.text(x + (dim.wid - lwid) / 2, y + dim.des - maxdes, label_, font);
168 pi.pain.text(x, y + dim.des - maxdes, label_, font);
172 int boxHeight = parent_.commonLabelBoxAscent() + parent_.commonLabelBoxDescent();
174 pi.pain.rectangle(x + 1, y - dim.ascent() + 1,
175 dim.wid - 2, boxHeight - 2,
181 //////////////////////////////////////////////////////////////////////
183 class DisplayLabelBox : public InsetLabelBox {
186 DisplayLabelBox(MathAtom const & atom, docstring label,
187 MathMacroTemplate const & parent);
190 void metrics(MetricsInfo & mi, Dimension & dim) const;
192 void draw(PainterInfo &, int x, int y) const;
196 Inset * clone() const;
200 DisplayLabelBox::DisplayLabelBox(MathAtom const & atom,
202 MathMacroTemplate const & parent)
203 : InsetLabelBox(atom, label, parent, true)
209 Inset * DisplayLabelBox::clone() const
211 return new DisplayLabelBox(*this);
215 void DisplayLabelBox::metrics(MetricsInfo & mi, Dimension & dim) const
217 InsetLabelBox::metrics(mi, dim);
218 if (!parent_.editing(mi.base.bv)
219 && parent_.cell(parent_.displayIdx()).empty()) {
223 setDimCache(mi, dim);
228 void DisplayLabelBox::draw(PainterInfo & pi, int x, int y) const
230 if (parent_.editing(pi.base.bv)
231 || !parent_.cell(parent_.displayIdx()).empty()) {
232 InsetLabelBox::draw(pi, x, y);
234 bool enabled = pi.pain.isDrawingEnabled();
235 pi.pain.setDrawingEnabled(false);
236 InsetLabelBox::draw(pi, x, y);
237 pi.pain.setDrawingEnabled(enabled);
242 //////////////////////////////////////////////////////////////////////
244 class InsetMathWrapper : public InsetMath {
247 InsetMathWrapper(MathData const * value) : value_(value) {}
249 void metrics(MetricsInfo & mi, Dimension & dim) const;
251 void draw(PainterInfo &, int x, int y) const;
255 Inset * clone() const;
257 MathData const * value_;
261 Inset * InsetMathWrapper::clone() const
263 return new InsetMathWrapper(*this);
267 void InsetMathWrapper::metrics(MetricsInfo & mi, Dimension & dim) const
269 value_->metrics(mi, dim);
270 //metricsMarkers2(dim);
274 void InsetMathWrapper::draw(PainterInfo & pi, int x, int y) const
276 value_->draw(pi, x, y);
277 //drawMarkers(pi, x, y);
281 ///////////////////////////////////////////////////////////////////////
283 class InsetNameWrapper : public InsetMathWrapper {
286 InsetNameWrapper(MathData const * value, MathMacroTemplate const & parent);
288 void metrics(MetricsInfo & mi, Dimension & dim) const;
290 void draw(PainterInfo &, int x, int y) const;
294 MathMacroTemplate const & parent_;
296 Inset * clone() const;
300 InsetNameWrapper::InsetNameWrapper(MathData const * value,
301 MathMacroTemplate const & parent)
302 : InsetMathWrapper(value), parent_(parent)
307 Inset * InsetNameWrapper::clone() const
309 return new InsetNameWrapper(*this);
313 void InsetNameWrapper::metrics(MetricsInfo & mi, Dimension & dim) const
315 InsetMathWrapper::metrics(mi, dim);
316 dim.wid += mathed_string_width(mi.base.font, from_ascii("\\"));
320 void InsetNameWrapper::draw(PainterInfo & pi, int x, int y) const
323 PainterInfo namepi = pi;
324 if (parent_.validMacro())
325 namepi.base.font.setColor(Color_latex);
327 namepi.base.font.setColor(Color_error);
330 pi.pain.text(x, y, from_ascii("\\"), namepi.base.font);
331 x += mathed_string_width(namepi.base.font, from_ascii("\\"));
334 InsetMathWrapper::draw(namepi, x, y);
338 ///////////////////////////////////////////////////////////////////////
341 MathMacroTemplate::MathMacroTemplate()
342 : InsetMathNest(3), numargs_(0), optionals_(0),
343 type_(MacroTypeNewcommand), lookOutdated_(true)
349 MathMacroTemplate::MathMacroTemplate(docstring const & name, int numargs,
350 int optionals, MacroType type,
351 vector<MathData> const & optionalValues,
352 MathData const & def, MathData const & display)
353 : InsetMathNest(optionals + 3), numargs_(numargs),
354 optionals_(optionals), optionalValues_(optionalValues),
355 type_(type), lookOutdated_(true)
360 lyxerr << "MathMacroTemplate::MathMacroTemplate: wrong # of arguments: "
363 asArray(name, cell(0));
364 optionalValues_.resize(9);
365 for (int i = 0; i < optionals_; ++i)
366 cell(optIdx(i)) = optionalValues_[i];
367 cell(defIdx()) = def;
368 cell(displayIdx()) = display;
374 MathMacroTemplate::MathMacroTemplate(docstring const & str)
375 : InsetMathNest(3), numargs_(0), optionals_(0),
376 type_(MacroTypeNewcommand), lookOutdated_(true)
381 mathed_parse_cell(ar, str);
382 if (ar.size() != 1 || !ar[0]->asMacroTemplate()) {
383 lyxerr << "Cannot read macro from '" << ar << "'" << endl;
384 asArray(from_ascii("invalidmacro"), cell(0));
385 // FIXME: The macro template does not make sense after this.
386 // The whole parsing should not be in a constructor which
387 // has no chance to report failure.
390 operator=( *(ar[0]->asMacroTemplate()) );
396 Inset * MathMacroTemplate::clone() const
398 return new MathMacroTemplate(*this);
402 docstring MathMacroTemplate::name() const
404 return asString(cell(0));
408 void MathMacroTemplate::updateToContext(MacroContext const & mc) const
410 redefinition_ = mc.get(name()) != 0;
414 void MathMacroTemplate::updateLook() const
416 lookOutdated_ = true;
420 void MathMacroTemplate::createLook() const
425 look_.push_back(MathAtom(new InsetLabelBox(_("Name"), *this, false)));
426 MathData & nameData = look_[look_.size() - 1].nucleus()->cell(0);
427 nameData.push_back(MathAtom(new InsetNameWrapper(&cell(0), *this)));
431 if (optionals_ > 0) {
432 look_.push_back(MathAtom(new InsetLabelBox(_("optional"), *this, false)));
433 MathData & optData = look_[look_.size() - 1].nucleus()->cell(0);
435 for (; i < optionals_; ++i) {
436 optData.push_back(MathAtom(new InsetMathChar('[')));
437 optData.push_back(MathAtom(new InsetMathWrapper(&cell(1 + i))));
438 optData.push_back(MathAtom(new InsetMathChar(']')));
443 for (; i < numargs_; ++i) {
445 arg.push_back(MathAtom(new MathMacroArgument(i + 1)));
446 look_.push_back(MathAtom(new InsetMathBrace(arg)));
450 look_.push_back(MathAtom(new InsetMathChar(':')));
451 look_.push_back(MathAtom(new InsetMathChar('=')));
454 look_.push_back(MathAtom(
455 new InsetLabelBox(MathAtom(
456 new InsetMathWrapper(&cell(defIdx()))), _("TeX"), *this, true)));
459 look_.push_back(MathAtom(
460 new DisplayLabelBox(MathAtom(
461 new InsetMathWrapper(&cell(displayIdx()))), _("LyX"), *this)));
465 void MathMacroTemplate::metrics(MetricsInfo & mi, Dimension & dim) const
467 FontSetChanger dummy1(mi.base, from_ascii("mathnormal"));
468 StyleChanger dummy2(mi.base, LM_ST_TEXT);
471 MacroData const * macro = 0;
473 macro = mi.macrocontext.get(name());
475 // updateToContext() - avoids another lookup
476 redefinition_ = macro != 0;
481 lookOutdated_ = false;
485 /// metrics for inset contents
489 // first phase, premetric:
491 look_.metrics(mi, dim);
492 labelBoxAscent_ = dim.asc;
493 labelBoxDescent_ = dim.des;
495 // second phase, main metric:
497 look_.metrics(mi, dim);
506 setDimCache(mi, dim);
510 void MathMacroTemplate::draw(PainterInfo & pi, int x, int y) const
512 FontSetChanger dummy1(pi.base, from_ascii("mathnormal"));
513 StyleChanger dummy2(pi.base, LM_ST_TEXT);
515 setPosCache(pi, x, y);
516 Dimension const dim = dimension(*pi.base.bv);
519 int const a = y - dim.asc + 1;
520 int const w = dim.wid - 2;
521 int const h = dim.height() - 2;
522 pi.pain.rectangle(x, a, w, h, Color_mathframe);
524 // just to be sure: set some dummy values for coord cache
525 for (idx_type i = 0; i < nargs(); ++i) {
526 cell(i).setXY(*pi.base.bv, x, y);
530 look_.draw(pi, x + 3, y);
534 void MathMacroTemplate::edit(Cursor & cur, bool left)
537 cur.updateFlags(Update::Force);
538 InsetMathNest::edit(cur, left);
542 Inset * MathMacroTemplate::editXY(Cursor & cur, int x, int y)
544 Inset * ret = InsetMathNest::editXY(cur, x, y);
545 /* if (!editing_ && editing(cur.bv())) {
547 cur.updateFlags(Update::Force);
553 bool MathMacroTemplate::notifyCursorLeaves(Cursor & cur)
556 cur.updateFlags(Update::Force);
557 return InsetMathNest::notifyCursorLeaves(cur);
561 void MathMacroTemplate::removeArguments(Cursor & cur, int from, int to) {
562 for (DocIterator it = doc_iterator_begin(*this); it; it.forwardChar()) {
565 if (it.nextInset()->lyxCode() != MATHMACROARG_CODE)
567 MathMacroArgument * arg = static_cast<MathMacroArgument*>(it.nextInset());
568 int n = arg->number() - 1;
569 if (from <= n && n <= to) {
570 int cellSlice = cur.find(it.cell());
571 if (cellSlice != -1 && cur[cellSlice].pos() > it.pos())
572 --cur[cellSlice].pos();
574 it.cell().erase(it.pos());
582 void MathMacroTemplate::shiftArguments(size_t from, int by) {
583 for (DocIterator it = doc_iterator_begin(*this); it; it.forwardChar()) {
586 if (it.nextInset()->lyxCode() != MATHMACROARG_CODE)
588 MathMacroArgument * arg = static_cast<MathMacroArgument*>(it.nextInset());
589 if (arg->number() >= from + 1)
590 arg->setNumber(arg->number() + by);
597 // FIXME: factorize those functions here with a functional style, maybe using Boost's function
600 void fixMacroInstancesAddRemove(Cursor const & from, docstring const & name, int n, bool insert) {
603 for (; dit; dit.forwardPos()) {
604 // only until a macro is redefined
605 if (dit.inset().lyxCode() == MATHMACRO_CODE) {
606 MathMacroTemplate const & macroTemplate
607 = static_cast<MathMacroTemplate const &>(dit.inset());
608 if (macroTemplate.name() == name)
612 // in front of macro instance?
613 Inset * inset = dit.nextInset();
615 InsetMath * insetMath = inset->asInsetMath();
617 MathMacro * macro = insetMath->asMacro();
618 if (macro && macro->name() == name && macro->folded()) {
619 // found macro instance
621 macro->insertArgument(n);
623 macro->removeArgument(n);
631 void fixMacroInstancesOptional(Cursor const & from, docstring const & name, int optionals) {
634 for (; dit; dit.forwardPos()) {
635 // only until a macro is redefined
636 if (dit.inset().lyxCode() == MATHMACRO_CODE) {
637 MathMacroTemplate const & macroTemplate
638 = static_cast<MathMacroTemplate const &>(dit.inset());
639 if (macroTemplate.name() == name)
643 // in front of macro instance?
644 Inset * inset = dit.nextInset();
646 InsetMath * insetMath = inset->asInsetMath();
648 MathMacro * macro = insetMath->asMacro();
649 if (macro && macro->name() == name && macro->folded()) {
650 // found macro instance
651 macro->setOptionals(optionals);
660 void fixMacroInstancesFunctional(Cursor const & from,
661 docstring const & name, F & fix) {
664 for (; dit; dit.forwardPos()) {
665 // only until a macro is redefined
666 if (dit.inset().lyxCode() == MATHMACRO_CODE) {
667 MathMacroTemplate const & macroTemplate
668 = static_cast<MathMacroTemplate const &>(dit.inset());
669 if (macroTemplate.name() == name)
673 // in front of macro instance?
674 Inset * inset = dit.nextInset();
676 InsetMath * insetMath = inset->asInsetMath();
678 MathMacro * macro = insetMath->asMacro();
679 if (macro && macro->name() == name && macro->folded())
687 void MathMacroTemplate::insertParameter(Cursor & cur, int pos, bool greedy)
689 if (pos <= numargs_ && pos >= optionals_ && numargs_ < 9) {
691 shiftArguments(pos, 1);
694 cell(defIdx()).push_back(MathAtom(new MathMacroArgument(pos + 1)));
695 if (!cell(displayIdx()).empty())
696 cell(displayIdx()).push_back(MathAtom(new MathMacroArgument(pos + 1)));
700 dit.leaveInset(*this);
701 // TODO: this was dit.forwardPosNoDescend before. Check that this is the same
702 dit.top().forwardPos();
704 // fix macro instances
705 fixMacroInstancesAddRemove(dit, name(), pos, true);
713 void MathMacroTemplate::removeParameter(Cursor & cur, int pos, bool greedy)
715 if (pos < numargs_ && pos >= 0) {
717 removeArguments(cur, pos, pos);
718 shiftArguments(pos + 1, -1);
720 // removed optional parameter?
721 if (pos < optionals_) {
723 optionalValues_[pos] = cell(optIdx(pos));
724 cells_.erase(cells_.begin() + optIdx(pos));
727 int macroSlice = cur.find(this);
728 if (macroSlice != -1) {
729 if (cur[macroSlice].idx() == optIdx(pos)) {
730 cur.cutOff(macroSlice);
731 cur[macroSlice].idx() = 1;
732 cur[macroSlice].pos() = 0;
733 } else if (cur[macroSlice].idx() > optIdx(pos))
734 --cur[macroSlice].idx();
739 // fix macro instances
740 //boost::function<void(MathMacro *)> fix = _1->insertArgument(n);
741 //fixMacroInstancesFunctional(dit, name(), fix);
743 dit.leaveInset(*this);
744 // TODO: this was dit.forwardPosNoDescend before. Check that this is the same
745 dit.top().forwardPos();
746 fixMacroInstancesAddRemove(dit, name(), pos, false);
754 void MathMacroTemplate::makeOptional(Cursor & cur) {
755 if (numargs_ > 0 && optionals_ < numargs_) {
757 cells_.insert(cells_.begin() + optIdx(optionals_ - 1), optionalValues_[optionals_ - 1]);
759 int macroSlice = cur.find(this);
760 if (macroSlice != -1 && cur[macroSlice].idx() >= optIdx(optionals_ - 1))
761 ++cur[macroSlice].idx();
763 // fix macro instances
765 dit.leaveInset(*this);
766 // TODO: this was dit.forwardPosNoDescend before. Check that this is the same
767 dit.top().forwardPos();
768 fixMacroInstancesOptional(dit, name(), optionals_);
775 void MathMacroTemplate::makeNonOptional(Cursor & cur) {
776 if (numargs_ > 0 && optionals_ > 0) {
779 // store default value for later if the use changes his mind
780 optionalValues_[optionals_] = cell(optIdx(optionals_));
781 cells_.erase(cells_.begin() + optIdx(optionals_));
784 int macroSlice = cur.find(this);
785 if (macroSlice != -1) {
786 if (cur[macroSlice].idx() > optIdx(optionals_))
787 --cur[macroSlice].idx();
788 else if (cur[macroSlice].idx() == optIdx(optionals_)) {
789 cur.cutOff(macroSlice);
790 cur[macroSlice].idx() = optIdx(optionals_);
791 cur[macroSlice].pos() = 0;
795 // fix macro instances
797 dit.leaveInset(*this);
798 // TODO: this was dit.forwardPosNoDescend before. Check that this is the same
799 dit.top().forwardPos();
800 fixMacroInstancesOptional(dit, name(), optionals_);
807 void MathMacroTemplate::doDispatch(Cursor & cur, FuncRequest & cmd)
809 string const arg = to_utf8(cmd.argument());
810 switch (cmd.action) {
812 case LFUN_MATH_MACRO_ADD_PARAM:
814 cur.recordUndoFullDocument();
815 size_t pos = numargs_;
817 pos = (size_t)convert<int>(arg) - 1; // it is checked for >=0 in getStatus
818 insertParameter(cur, pos);
823 case LFUN_MATH_MACRO_REMOVE_PARAM:
825 cur.recordUndoFullDocument();
826 size_t pos = numargs_ - 1;
828 pos = (size_t)convert<int>(arg) - 1; // it is checked for >=0 in getStatus
829 removeParameter(cur, pos);
833 case LFUN_MATH_MACRO_APPEND_GREEDY_PARAM:
835 cur.recordUndoFullDocument();
836 insertParameter(cur, numargs_, true);
840 case LFUN_MATH_MACRO_REMOVE_GREEDY_PARAM:
842 cur.recordUndoFullDocument();
843 removeParameter(cur, numargs_ - 1, true);
847 case LFUN_MATH_MACRO_MAKE_OPTIONAL:
848 cur.recordUndoFullDocument();
852 case LFUN_MATH_MACRO_MAKE_NONOPTIONAL:
853 cur.recordUndoFullDocument();
854 makeNonOptional(cur);
857 case LFUN_MATH_MACRO_ADD_OPTIONAL_PARAM:
859 cur.recordUndoFullDocument();
860 insertParameter(cur, optionals_);
865 case LFUN_MATH_MACRO_REMOVE_OPTIONAL_PARAM:
866 if (optionals_ > 0) {
867 cur.recordUndoFullDocument();
868 removeParameter(cur, optionals_ - 1);
871 case LFUN_MATH_MACRO_ADD_GREEDY_OPTIONAL_PARAM:
872 if (numargs_ == optionals_) {
873 cur.recordUndoFullDocument();
874 insertParameter(cur, 0, true);
880 InsetMathNest::doDispatch(cur, cmd);
886 bool MathMacroTemplate::getStatus(Cursor & /*cur*/, FuncRequest const & cmd,
887 FuncStatus & flag) const
890 string const arg = to_utf8(cmd.argument());
891 switch (cmd.action) {
892 case LFUN_MATH_MACRO_ADD_PARAM: {
893 int num = numargs_ + 1;
895 num = convert<int>(arg);
896 bool on = (num >= optionals_
897 && numargs_ < 9 && num <= numargs_ + 1);
902 case LFUN_MATH_MACRO_APPEND_GREEDY_PARAM:
903 flag.enabled(numargs_ < 9);
906 case LFUN_MATH_MACRO_REMOVE_PARAM: {
909 num = convert<int>(arg);
910 flag.enabled(num >= 1 && num <= numargs_);
914 case LFUN_MATH_MACRO_MAKE_OPTIONAL:
915 flag.enabled(numargs_ > 0
916 && optionals_ < numargs_
917 && type_ != MacroTypeDef);
920 case LFUN_MATH_MACRO_MAKE_NONOPTIONAL:
921 flag.enabled(optionals_ > 0
922 && type_ != MacroTypeDef);
925 case LFUN_MATH_MACRO_ADD_OPTIONAL_PARAM:
926 flag.enabled(numargs_ < 9);
929 case LFUN_MATH_MACRO_REMOVE_OPTIONAL_PARAM:
930 flag.enabled(optionals_ > 0);
933 case LFUN_MATH_MACRO_ADD_GREEDY_OPTIONAL_PARAM:
934 flag.enabled(numargs_ == 0
935 && type_ != MacroTypeDef);
938 case LFUN_IN_MATHMACROTEMPLATE:
950 void MathMacroTemplate::read(Buffer const &, Lexer & lex)
953 mathed_parse_cell(ar, lex.getStream());
954 if (ar.size() != 1 || !ar[0]->asMacroTemplate()) {
955 lyxerr << "Cannot read macro from '" << ar << "'" << endl;
956 lyxerr << "Read: " << to_utf8(asString(ar)) << endl;
959 operator=( *(ar[0]->asMacroTemplate()) );
965 void MathMacroTemplate::write(Buffer const &, ostream & os) const
967 odocstringstream oss;
968 WriteStream wi(oss, false, false);
969 oss << "FormulaMacro\n";
971 os << to_utf8(oss.str());
975 void MathMacroTemplate::write(WriteStream & os) const
981 void MathMacroTemplate::write(WriteStream & os, bool overwriteRedefinition) const
983 if (type_ == MacroTypeDef) {
984 os << "\\def\\" << name().c_str();
985 for (int i = 1; i <= numargs_; ++i)
988 // newcommand or renewcommand
989 if (redefinition_ && !overwriteRedefinition)
990 os << "\\renewcommand";
992 os << "\\newcommand";
993 os << "{\\" << name().c_str() << '}';
995 os << '[' << numargs_ << ']';
999 // in latex only one optional possible, simulate the others
1000 if (optionals_ >= 1) {
1001 docstring optValue = asString(cell(optIdx(0)));
1002 if (optValue.find(']') != docstring::npos)
1003 os << "[{" << cell(optIdx(0)) << "}]";
1005 os << "[" << cell(optIdx(0)) << "]";
1008 // in lyx we handle all optionals as real optionals
1009 for (int i = 0; i < optionals_; ++i) {
1010 docstring optValue = asString(cell(optIdx(i)));
1011 if (optValue.find(']') != docstring::npos)
1012 os << "[{" << cell(optIdx(i)) << "}]";
1014 os << "[" << cell(optIdx(i)) << "]";
1019 os << "{" << cell(defIdx()) << "}";
1022 // writing .tex. done.
1025 // writing .lyx, write special .tex export only if necessary
1026 if (!cell(displayIdx()).empty())
1027 os << "\n{" << cell(displayIdx()) << '}';
1032 int MathMacroTemplate::plaintext(Buffer const & buf, odocstream & os,
1033 OutputParams const &) const
1035 static docstring const str = '[' + buf.B_("math macro") + ']';
1042 bool MathMacroTemplate::validName() const
1044 docstring n = name();
1050 // converting back and force doesn't swallow anything?
1053 if (asString(ma) != n)
1056 // valid characters?
1057 for (size_t i = 0; i < n.size(); ++i) {
1058 if (!(n[i] >= 'a' && n[i] <= 'z') &&
1059 !(n[i] >= 'A' && n[i] <= 'Z'))
1067 bool MathMacroTemplate::validMacro() const
1073 bool MathMacroTemplate::fixNameAndCheckIfValid()
1075 // check all the characters/insets in the name cell
1077 MathData & data = cell(0);
1078 while (i < data.size()) {
1079 InsetMathChar const * cinset = data[i]->asCharInset();
1081 // valid character in [a-zA-Z]?
1082 char_type c = cinset->getChar();
1083 if ((c >= 'a' && c <= 'z')
1084 || (c >= 'A' && c <= 'Z')) {
1094 // now it should be valid if anything in the name survived
1095 return data.size() > 0;
1099 void MathMacroTemplate::getDefaults(vector<docstring> & defaults) const
1101 defaults.resize(numargs_);
1102 for (int i = 0; i < optionals_; ++i)
1103 defaults[i] = asString(cell(optIdx(i)));
1107 docstring MathMacroTemplate::definition() const
1109 return asString(cell(defIdx()));
1113 docstring MathMacroTemplate::displayDefinition() const
1115 return asString(cell(displayIdx()));
1119 size_t MathMacroTemplate::numArgs() const
1125 size_t MathMacroTemplate::numOptionals() const
1131 void MathMacroTemplate::infoize(odocstream & os) const
1133 os << "Math Macro: \\" << name();