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 bool MathMacroTemplate::notifyCursorLeaves(Cursor & cur)
545 cur.updateFlags(Update::Force);
546 return InsetMathNest::notifyCursorLeaves(cur);
550 void MathMacroTemplate::removeArguments(Cursor & cur, int from, int to) {
551 for (DocIterator it = doc_iterator_begin(*this); it; it.forwardChar()) {
554 if (it.nextInset()->lyxCode() != MATHMACROARG_CODE)
556 MathMacroArgument * arg = static_cast<MathMacroArgument*>(it.nextInset());
557 int n = arg->number() - 1;
558 if (from <= n && n <= to) {
559 int cellSlice = cur.find(it.cell());
560 if (cellSlice != -1 && cur[cellSlice].pos() > it.pos())
561 --cur[cellSlice].pos();
563 it.cell().erase(it.pos());
571 void MathMacroTemplate::shiftArguments(size_t from, int by) {
572 for (DocIterator it = doc_iterator_begin(*this); it; it.forwardChar()) {
575 if (it.nextInset()->lyxCode() != MATHMACROARG_CODE)
577 MathMacroArgument * arg = static_cast<MathMacroArgument*>(it.nextInset());
578 if (arg->number() >= from + 1)
579 arg->setNumber(arg->number() + by);
586 // FIXME: factorize those functions here with a functional style, maybe using Boost's function
589 void fixMacroInstancesAddRemove(Cursor const & from, docstring const & name, int n, bool insert) {
592 for (; dit; dit.forwardPos()) {
593 // only until a macro is redefined
594 if (dit.inset().lyxCode() == MATHMACRO_CODE) {
595 MathMacroTemplate const & macroTemplate
596 = static_cast<MathMacroTemplate const &>(dit.inset());
597 if (macroTemplate.name() == name)
601 // in front of macro instance?
602 Inset * inset = dit.nextInset();
604 InsetMath * insetMath = inset->asInsetMath();
606 MathMacro * macro = insetMath->asMacro();
607 if (macro && macro->name() == name && macro->folded()) {
608 // found macro instance
610 macro->insertArgument(n);
612 macro->removeArgument(n);
620 void fixMacroInstancesOptional(Cursor const & from, docstring const & name, int optionals) {
623 for (; dit; dit.forwardPos()) {
624 // only until a macro is redefined
625 if (dit.inset().lyxCode() == MATHMACRO_CODE) {
626 MathMacroTemplate const & macroTemplate
627 = static_cast<MathMacroTemplate const &>(dit.inset());
628 if (macroTemplate.name() == name)
632 // in front of macro instance?
633 Inset * inset = dit.nextInset();
635 InsetMath * insetMath = inset->asInsetMath();
637 MathMacro * macro = insetMath->asMacro();
638 if (macro && macro->name() == name && macro->folded()) {
639 // found macro instance
640 macro->setOptionals(optionals);
649 void fixMacroInstancesFunctional(Cursor const & from,
650 docstring const & name, F & fix) {
653 for (; dit; dit.forwardPos()) {
654 // only until a macro is redefined
655 if (dit.inset().lyxCode() == MATHMACRO_CODE) {
656 MathMacroTemplate const & macroTemplate
657 = static_cast<MathMacroTemplate const &>(dit.inset());
658 if (macroTemplate.name() == name)
662 // in front of macro instance?
663 Inset * inset = dit.nextInset();
665 InsetMath * insetMath = inset->asInsetMath();
667 MathMacro * macro = insetMath->asMacro();
668 if (macro && macro->name() == name && macro->folded())
676 void MathMacroTemplate::insertParameter(Cursor & cur, int pos, bool greedy)
678 if (pos <= numargs_ && pos >= optionals_ && numargs_ < 9) {
680 shiftArguments(pos, 1);
683 cell(defIdx()).push_back(MathAtom(new MathMacroArgument(pos + 1)));
684 if (!cell(displayIdx()).empty())
685 cell(displayIdx()).push_back(MathAtom(new MathMacroArgument(pos + 1)));
689 dit.leaveInset(*this);
690 // TODO: this was dit.forwardPosNoDescend before. Check that this is the same
691 dit.top().forwardPos();
693 // fix macro instances
694 fixMacroInstancesAddRemove(dit, name(), pos, true);
702 void MathMacroTemplate::removeParameter(Cursor & cur, int pos, bool greedy)
704 if (pos < numargs_ && pos >= 0) {
706 removeArguments(cur, pos, pos);
707 shiftArguments(pos + 1, -1);
709 // removed optional parameter?
710 if (pos < optionals_) {
712 optionalValues_[pos] = cell(optIdx(pos));
713 cells_.erase(cells_.begin() + optIdx(pos));
716 int macroSlice = cur.find(this);
717 if (macroSlice != -1) {
718 if (cur[macroSlice].idx() == optIdx(pos)) {
719 cur.cutOff(macroSlice);
720 cur[macroSlice].idx() = 1;
721 cur[macroSlice].pos() = 0;
722 } else if (cur[macroSlice].idx() > optIdx(pos))
723 --cur[macroSlice].idx();
728 // fix macro instances
729 //boost::function<void(MathMacro *)> fix = _1->insertArgument(n);
730 //fixMacroInstancesFunctional(dit, name(), fix);
732 dit.leaveInset(*this);
733 // TODO: this was dit.forwardPosNoDescend before. Check that this is the same
734 dit.top().forwardPos();
735 fixMacroInstancesAddRemove(dit, name(), pos, false);
743 void MathMacroTemplate::makeOptional(Cursor & cur) {
744 if (numargs_ > 0 && optionals_ < numargs_) {
746 cells_.insert(cells_.begin() + optIdx(optionals_ - 1), optionalValues_[optionals_ - 1]);
748 int macroSlice = cur.find(this);
749 if (macroSlice != -1 && cur[macroSlice].idx() >= optIdx(optionals_ - 1))
750 ++cur[macroSlice].idx();
752 // fix macro instances
754 dit.leaveInset(*this);
755 // TODO: this was dit.forwardPosNoDescend before. Check that this is the same
756 dit.top().forwardPos();
757 fixMacroInstancesOptional(dit, name(), optionals_);
764 void MathMacroTemplate::makeNonOptional(Cursor & cur) {
765 if (numargs_ > 0 && optionals_ > 0) {
768 // store default value for later if the use changes his mind
769 optionalValues_[optionals_] = cell(optIdx(optionals_));
770 cells_.erase(cells_.begin() + optIdx(optionals_));
773 int macroSlice = cur.find(this);
774 if (macroSlice != -1) {
775 if (cur[macroSlice].idx() > optIdx(optionals_))
776 --cur[macroSlice].idx();
777 else if (cur[macroSlice].idx() == optIdx(optionals_)) {
778 cur.cutOff(macroSlice);
779 cur[macroSlice].idx() = optIdx(optionals_);
780 cur[macroSlice].pos() = 0;
784 // fix macro instances
786 dit.leaveInset(*this);
787 // TODO: this was dit.forwardPosNoDescend before. Check that this is the same
788 dit.top().forwardPos();
789 fixMacroInstancesOptional(dit, name(), optionals_);
796 void MathMacroTemplate::doDispatch(Cursor & cur, FuncRequest & cmd)
798 string const arg = to_utf8(cmd.argument());
799 switch (cmd.action) {
801 case LFUN_MATH_MACRO_ADD_PARAM:
803 cur.recordUndoFullDocument();
804 size_t pos = numargs_;
806 pos = (size_t)convert<int>(arg) - 1; // it is checked for >=0 in getStatus
807 insertParameter(cur, pos);
812 case LFUN_MATH_MACRO_REMOVE_PARAM:
814 cur.recordUndoFullDocument();
815 size_t pos = numargs_ - 1;
817 pos = (size_t)convert<int>(arg) - 1; // it is checked for >=0 in getStatus
818 removeParameter(cur, pos);
822 case LFUN_MATH_MACRO_APPEND_GREEDY_PARAM:
824 cur.recordUndoFullDocument();
825 insertParameter(cur, numargs_, true);
829 case LFUN_MATH_MACRO_REMOVE_GREEDY_PARAM:
831 cur.recordUndoFullDocument();
832 removeParameter(cur, numargs_ - 1, true);
836 case LFUN_MATH_MACRO_MAKE_OPTIONAL:
837 cur.recordUndoFullDocument();
841 case LFUN_MATH_MACRO_MAKE_NONOPTIONAL:
842 cur.recordUndoFullDocument();
843 makeNonOptional(cur);
846 case LFUN_MATH_MACRO_ADD_OPTIONAL_PARAM:
848 cur.recordUndoFullDocument();
849 insertParameter(cur, optionals_);
854 case LFUN_MATH_MACRO_REMOVE_OPTIONAL_PARAM:
855 if (optionals_ > 0) {
856 cur.recordUndoFullDocument();
857 removeParameter(cur, optionals_ - 1);
860 case LFUN_MATH_MACRO_ADD_GREEDY_OPTIONAL_PARAM:
861 if (numargs_ == optionals_) {
862 cur.recordUndoFullDocument();
863 insertParameter(cur, 0, true);
869 InsetMathNest::doDispatch(cur, cmd);
875 bool MathMacroTemplate::getStatus(Cursor & /*cur*/, FuncRequest const & cmd,
876 FuncStatus & flag) const
879 string const arg = to_utf8(cmd.argument());
880 switch (cmd.action) {
881 case LFUN_MATH_MACRO_ADD_PARAM: {
882 int num = numargs_ + 1;
884 num = convert<int>(arg);
885 bool on = (num >= optionals_
886 && numargs_ < 9 && num <= numargs_ + 1);
891 case LFUN_MATH_MACRO_APPEND_GREEDY_PARAM:
892 flag.enabled(numargs_ < 9);
895 case LFUN_MATH_MACRO_REMOVE_PARAM: {
898 num = convert<int>(arg);
899 flag.enabled(num >= 1 && num <= numargs_);
903 case LFUN_MATH_MACRO_MAKE_OPTIONAL:
904 flag.enabled(numargs_ > 0
905 && optionals_ < numargs_
906 && type_ != MacroTypeDef);
909 case LFUN_MATH_MACRO_MAKE_NONOPTIONAL:
910 flag.enabled(optionals_ > 0
911 && type_ != MacroTypeDef);
914 case LFUN_MATH_MACRO_ADD_OPTIONAL_PARAM:
915 flag.enabled(numargs_ < 9);
918 case LFUN_MATH_MACRO_REMOVE_OPTIONAL_PARAM:
919 flag.enabled(optionals_ > 0);
922 case LFUN_MATH_MACRO_ADD_GREEDY_OPTIONAL_PARAM:
923 flag.enabled(numargs_ == 0
924 && type_ != MacroTypeDef);
927 case LFUN_IN_MATHMACROTEMPLATE:
939 void MathMacroTemplate::read(Buffer const &, Lexer & lex)
942 mathed_parse_cell(ar, lex.getStream());
943 if (ar.size() != 1 || !ar[0]->asMacroTemplate()) {
944 lyxerr << "Cannot read macro from '" << ar << "'" << endl;
945 lyxerr << "Read: " << to_utf8(asString(ar)) << endl;
948 operator=( *(ar[0]->asMacroTemplate()) );
954 void MathMacroTemplate::write(Buffer const &, ostream & os) const
956 odocstringstream oss;
957 WriteStream wi(oss, false, false);
958 oss << "FormulaMacro\n";
960 os << to_utf8(oss.str());
964 void MathMacroTemplate::write(WriteStream & os) const
970 void MathMacroTemplate::write(WriteStream & os, bool overwriteRedefinition) const
972 if (type_ == MacroTypeDef) {
973 os << "\\def\\" << name().c_str();
974 for (int i = 1; i <= numargs_; ++i)
977 // newcommand or renewcommand
978 if (redefinition_ && !overwriteRedefinition)
979 os << "\\renewcommand";
981 os << "\\newcommand";
982 os << "{\\" << name().c_str() << '}';
984 os << '[' << numargs_ << ']';
988 // in latex only one optional possible, simulate the others
989 if (optionals_ >= 1) {
990 docstring optValue = asString(cell(optIdx(0)));
991 if (optValue.find(']') != docstring::npos)
992 os << "[{" << cell(optIdx(0)) << "}]";
994 os << "[" << cell(optIdx(0)) << "]";
997 // in lyx we handle all optionals as real optionals
998 for (int i = 0; i < optionals_; ++i) {
999 docstring optValue = asString(cell(optIdx(i)));
1000 if (optValue.find(']') != docstring::npos)
1001 os << "[{" << cell(optIdx(i)) << "}]";
1003 os << "[" << cell(optIdx(i)) << "]";
1008 os << "{" << cell(defIdx()) << "}";
1011 // writing .tex. done.
1014 // writing .lyx, write special .tex export only if necessary
1015 if (!cell(displayIdx()).empty())
1016 os << "\n{" << cell(displayIdx()) << '}';
1021 int MathMacroTemplate::plaintext(Buffer const & buf, odocstream & os,
1022 OutputParams const &) const
1024 static docstring const str = '[' + buf.B_("math macro") + ']';
1031 bool MathMacroTemplate::validName() const
1033 docstring n = name();
1039 // converting back and force doesn't swallow anything?
1042 if (asString(ma) != n)
1045 // valid characters?
1046 for (size_t i = 0; i < n.size(); ++i) {
1047 if (!(n[i] >= 'a' && n[i] <= 'z') &&
1048 !(n[i] >= 'A' && n[i] <= 'Z'))
1056 bool MathMacroTemplate::validMacro() const
1062 bool MathMacroTemplate::fixNameAndCheckIfValid()
1064 // check all the characters/insets in the name cell
1066 MathData & data = cell(0);
1067 while (i < data.size()) {
1068 InsetMathChar const * cinset = data[i]->asCharInset();
1070 // valid character in [a-zA-Z]?
1071 char_type c = cinset->getChar();
1072 if ((c >= 'a' && c <= 'z')
1073 || (c >= 'A' && c <= 'Z')) {
1083 // now it should be valid if anything in the name survived
1084 return data.size() > 0;
1088 void MathMacroTemplate::getDefaults(vector<docstring> & defaults) const
1090 defaults.resize(numargs_);
1091 for (int i = 0; i < optionals_; ++i)
1092 defaults[i] = asString(cell(optIdx(i)));
1096 docstring MathMacroTemplate::definition() const
1098 return asString(cell(defIdx()));
1102 docstring MathMacroTemplate::displayDefinition() const
1104 return asString(cell(displayIdx()));
1108 size_t MathMacroTemplate::numArgs() const
1114 size_t MathMacroTemplate::numOptionals() const
1120 void MathMacroTemplate::infoize(odocstream & os) const
1122 os << "Math Macro: \\" << name();