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 "LaTeXFeatures.h"
17 #include "InsetMathBrace.h"
18 #include "InsetMathChar.h"
19 #include "InsetMathSqrt.h"
20 #include "MathMacro.h"
21 #include "MathMacroArgument.h"
22 #include "MathStream.h"
23 #include "MathParser.h"
24 #include "MathSupport.h"
25 #include "MathMacroArgument.h"
28 #include "BufferView.h"
31 #include "support/debug.h"
32 #include "DispatchResult.h"
33 #include "FuncRequest.h"
34 #include "FuncStatus.h"
35 #include "support/gettext.h"
39 #include "frontends/FontMetrics.h"
40 #include "frontends/Painter.h"
42 #include "support/convert.h"
43 #include "support/docstream.h"
44 #include "support/lstrings.h"
46 #include "support/debug.h"
54 using support::bformat;
56 //////////////////////////////////////////////////////////////////////
58 class InsetLabelBox : public InsetMathNest {
61 InsetLabelBox(MathAtom const & atom, docstring label,
62 MathMacroTemplate const & parent, bool frame = false);
63 InsetLabelBox(docstring label, MathMacroTemplate const & parent,
66 void metrics(MetricsInfo & mi, Dimension & dim) const;
68 void draw(PainterInfo &, int x, int y) const;
72 MathMacroTemplate const & parent_;
74 Inset * clone() const;
76 docstring const label_;
82 InsetLabelBox::InsetLabelBox(MathAtom const & atom, docstring label,
83 MathMacroTemplate const & parent, bool frame)
84 : InsetMathNest(1), parent_(parent), label_(label), frame_(frame)
86 cell(0).insert(0, atom);
90 InsetLabelBox::InsetLabelBox(docstring label,
91 MathMacroTemplate const & parent, bool frame)
92 : InsetMathNest(1), parent_(parent), label_(label), frame_(frame)
97 Inset * InsetLabelBox::clone() const
99 return new InsetLabelBox(*this);
103 void InsetLabelBox::metrics(MetricsInfo & mi, Dimension & dim) const
106 cell(0).metrics(mi, dim);
115 // adjust to common height in main metrics phase
116 if (!parent_.premetrics()) {
117 dim.asc = max(dim.asc, parent_.commonLabelBoxAscent());
118 dim.des = max(dim.des, parent_.commonLabelBoxDescent());
122 if (parent_.editing(mi.base.bv) && label_.length() > 0) {
124 FontInfo font = sane_font;
125 font.setSize(FONT_SIZE_TINY);
126 font.setColor(Color_mathmacrolabel);
128 // make space for label and box
129 int lwid = mathed_string_width(font, label_);
132 math_font_max_dim(font, maxasc, maxdes);
134 dim.wid = max(dim.wid, lwid + 2);
136 // space for the label
137 if (!parent_.premetrics())
138 dim.des += maxasc + maxdes + 1;
143 void InsetLabelBox::draw(PainterInfo & pi, int x, int y) const
145 Dimension const dim = dimension(*pi.base.bv);
146 Dimension const cdim = cell(0).dimension(*pi.base.bv);
149 cell(0).draw(pi, x + (dim.wid - cdim.wid) / 2, y);
152 if (parent_.editing(pi.base.bv) && label_.length() > 0) {
154 FontInfo font = sane_font;
155 font.setSize(FONT_SIZE_TINY);
156 font.setColor(Color_mathmacrolabel);
158 // make space for label and box
159 int lwid = mathed_string_width(font, label_);
162 math_font_max_dim(font, maxasc, maxdes);
165 pi.pain.text(x + (dim.wid - lwid) / 2, y + dim.des - maxdes, label_, font);
167 pi.pain.text(x, y + dim.des - maxdes, label_, font);
171 int boxHeight = parent_.commonLabelBoxAscent() + parent_.commonLabelBoxDescent();
173 pi.pain.rectangle(x + 1, y - dim.ascent() + 1,
174 dim.wid - 2, boxHeight - 2,
180 //////////////////////////////////////////////////////////////////////
182 class DisplayLabelBox : public InsetLabelBox {
185 DisplayLabelBox(MathAtom const & atom, docstring label,
186 MathMacroTemplate const & parent);
189 void metrics(MetricsInfo & mi, Dimension & dim) const;
191 void draw(PainterInfo &, int x, int y) const;
195 Inset * clone() const;
199 DisplayLabelBox::DisplayLabelBox(MathAtom const & atom,
201 MathMacroTemplate const & parent)
202 : InsetLabelBox(atom, label, parent, true)
208 Inset * DisplayLabelBox::clone() const
210 return new DisplayLabelBox(*this);
214 void DisplayLabelBox::metrics(MetricsInfo & mi, Dimension & dim) const
216 InsetLabelBox::metrics(mi, dim);
217 if (!parent_.editing(mi.base.bv)
218 && parent_.cell(parent_.displayIdx()).empty()) {
226 void DisplayLabelBox::draw(PainterInfo & pi, int x, int y) const
228 if (parent_.editing(pi.base.bv)
229 || !parent_.cell(parent_.displayIdx()).empty()) {
230 InsetLabelBox::draw(pi, x, y);
232 bool enabled = pi.pain.isDrawingEnabled();
233 pi.pain.setDrawingEnabled(false);
234 InsetLabelBox::draw(pi, x, y);
235 pi.pain.setDrawingEnabled(enabled);
240 //////////////////////////////////////////////////////////////////////
242 class InsetMathWrapper : public InsetMath {
245 InsetMathWrapper(MathData const * value) : value_(value) {}
247 void metrics(MetricsInfo & mi, Dimension & dim) const;
249 void draw(PainterInfo &, int x, int y) const;
253 Inset * clone() const;
255 MathData const * value_;
259 Inset * InsetMathWrapper::clone() const
261 return new InsetMathWrapper(*this);
265 void InsetMathWrapper::metrics(MetricsInfo & mi, Dimension & dim) const
267 value_->metrics(mi, dim);
268 //metricsMarkers2(dim);
272 void InsetMathWrapper::draw(PainterInfo & pi, int x, int y) const
274 value_->draw(pi, x, y);
275 //drawMarkers(pi, x, y);
279 ///////////////////////////////////////////////////////////////////////
281 class InsetNameWrapper : public InsetMathWrapper {
284 InsetNameWrapper(MathData const * value, MathMacroTemplate const & parent);
286 void metrics(MetricsInfo & mi, Dimension & dim) const;
288 void draw(PainterInfo &, int x, int y) const;
292 MathMacroTemplate const & parent_;
294 Inset * clone() const;
298 InsetNameWrapper::InsetNameWrapper(MathData const * value,
299 MathMacroTemplate const & parent)
300 : InsetMathWrapper(value), parent_(parent)
305 Inset * InsetNameWrapper::clone() const
307 return new InsetNameWrapper(*this);
311 void InsetNameWrapper::metrics(MetricsInfo & mi, Dimension & dim) const
313 InsetMathWrapper::metrics(mi, dim);
314 dim.wid += mathed_string_width(mi.base.font, from_ascii("\\"));
318 void InsetNameWrapper::draw(PainterInfo & pi, int x, int y) const
321 PainterInfo namepi = pi;
322 if (parent_.validMacro())
323 namepi.base.font.setColor(Color_latex);
325 namepi.base.font.setColor(Color_error);
328 pi.pain.text(x, y, from_ascii("\\"), namepi.base.font);
329 x += mathed_string_width(namepi.base.font, from_ascii("\\"));
332 InsetMathWrapper::draw(namepi, x, y);
336 ///////////////////////////////////////////////////////////////////////
339 MathMacroTemplate::MathMacroTemplate()
340 : InsetMathNest(3), numargs_(0), optionals_(0),
341 type_(MacroTypeNewcommand), lookOutdated_(true)
347 MathMacroTemplate::MathMacroTemplate(docstring const & name, int numargs,
348 int optionals, MacroType type,
349 vector<MathData> const & optionalValues,
350 MathData const & def, MathData const & display)
351 : InsetMathNest(optionals + 3), numargs_(numargs),
352 optionals_(optionals), optionalValues_(optionalValues),
353 type_(type), lookOutdated_(true)
358 lyxerr << "MathMacroTemplate::MathMacroTemplate: wrong # of arguments: "
361 asArray(name, cell(0));
362 optionalValues_.resize(9);
363 for (int i = 0; i < optionals_; ++i)
364 cell(optIdx(i)) = optionalValues_[i];
365 cell(defIdx()) = def;
366 cell(displayIdx()) = display;
372 MathMacroTemplate::MathMacroTemplate(docstring const & str)
373 : InsetMathNest(3), numargs_(0), optionals_(0),
374 type_(MacroTypeNewcommand), lookOutdated_(true)
379 mathed_parse_cell(ar, str);
380 if (ar.size() != 1 || !ar[0]->asMacroTemplate()) {
381 lyxerr << "Cannot read macro from '" << ar << "'" << endl;
382 asArray(from_ascii("invalidmacro"), cell(0));
383 // FIXME: The macro template does not make sense after this.
384 // The whole parsing should not be in a constructor which
385 // has no chance to report failure.
388 operator=( *(ar[0]->asMacroTemplate()) );
394 Inset * MathMacroTemplate::clone() const
396 MathMacroTemplate * inset = new MathMacroTemplate(*this);
397 // the parent pointers of the proxy insets above will point to
398 // to the old template. Hence, the look must be updated.
404 docstring MathMacroTemplate::name() const
406 return asString(cell(0));
410 void MathMacroTemplate::updateToContext(MacroContext const & mc) const
412 redefinition_ = mc.get(name()) != 0;
416 void MathMacroTemplate::updateLook() const
418 lookOutdated_ = true;
422 void MathMacroTemplate::createLook() const
427 look_.push_back(MathAtom(new InsetLabelBox(_("Name"), *this, false)));
428 MathData & nameData = look_[look_.size() - 1].nucleus()->cell(0);
429 nameData.push_back(MathAtom(new InsetNameWrapper(&cell(0), *this)));
433 if (optionals_ > 0) {
434 look_.push_back(MathAtom(new InsetLabelBox(_("optional"), *this, false)));
435 MathData & optData = look_[look_.size() - 1].nucleus()->cell(0);
437 for (; i < optionals_; ++i) {
438 optData.push_back(MathAtom(new InsetMathChar('[')));
439 optData.push_back(MathAtom(new InsetMathWrapper(&cell(1 + i))));
440 optData.push_back(MathAtom(new InsetMathChar(']')));
445 for (; i < numargs_; ++i) {
447 arg.push_back(MathAtom(new MathMacroArgument(i + 1)));
448 look_.push_back(MathAtom(new InsetMathBrace(arg)));
452 look_.push_back(MathAtom(new InsetMathChar(':')));
453 look_.push_back(MathAtom(new InsetMathChar('=')));
456 look_.push_back(MathAtom(
457 new InsetLabelBox(MathAtom(
458 new InsetMathWrapper(&cell(defIdx()))), _("TeX"), *this, true)));
461 look_.push_back(MathAtom(
462 new DisplayLabelBox(MathAtom(
463 new InsetMathWrapper(&cell(displayIdx()))), _("LyX"), *this)));
467 void MathMacroTemplate::metrics(MetricsInfo & mi, Dimension & dim) const
469 FontSetChanger dummy1(mi.base, from_ascii("mathnormal"));
470 StyleChanger dummy2(mi.base, LM_ST_TEXT);
473 MacroData const * macro = 0;
475 macro = mi.macrocontext.get(name());
477 // updateToContext() - avoids another lookup
478 redefinition_ = macro != 0;
483 lookOutdated_ = false;
487 /// metrics for inset contents
491 // first phase, premetric:
493 look_.metrics(mi, dim);
494 labelBoxAscent_ = dim.asc;
495 labelBoxDescent_ = dim.des;
497 // second phase, main metric:
499 look_.metrics(mi, dim);
508 setDimCache(mi, dim);
512 void MathMacroTemplate::draw(PainterInfo & pi, int x, int y) const
514 FontSetChanger dummy1(pi.base, from_ascii("mathnormal"));
515 StyleChanger dummy2(pi.base, LM_ST_TEXT);
517 setPosCache(pi, x, y);
518 Dimension const dim = dimension(*pi.base.bv);
521 int const a = y - dim.asc + 1;
522 int const w = dim.wid - 2;
523 int const h = dim.height() - 2;
524 pi.pain.rectangle(x, a, w, h, Color_mathframe);
526 // just to be sure: set some dummy values for coord cache
527 for (idx_type i = 0; i < nargs(); ++i) {
528 cell(i).setXY(*pi.base.bv, x, y);
532 look_.draw(pi, x + 3, y);
536 void MathMacroTemplate::edit(Cursor & cur, bool left)
539 cur.updateFlags(Update::Force);
540 InsetMathNest::edit(cur, left);
544 bool MathMacroTemplate::notifyCursorLeaves(Cursor & cur)
547 cur.updateFlags(Update::Force);
548 return InsetMathNest::notifyCursorLeaves(cur);
552 void MathMacroTemplate::removeArguments(Cursor & cur, int from, int to) {
553 for (DocIterator it = doc_iterator_begin(*this); it; it.forwardChar()) {
556 if (it.nextInset()->lyxCode() != MATHMACROARG_CODE)
558 MathMacroArgument * arg = static_cast<MathMacroArgument*>(it.nextInset());
559 int n = arg->number() - 1;
560 if (from <= n && n <= to) {
561 int cellSlice = cur.find(it.cell());
562 if (cellSlice != -1 && cur[cellSlice].pos() > it.pos())
563 --cur[cellSlice].pos();
565 it.cell().erase(it.pos());
573 void MathMacroTemplate::shiftArguments(size_t from, int by) {
574 for (DocIterator it = doc_iterator_begin(*this); it; it.forwardChar()) {
577 if (it.nextInset()->lyxCode() != MATHMACROARG_CODE)
579 MathMacroArgument * arg = static_cast<MathMacroArgument*>(it.nextInset());
580 if (arg->number() >= from + 1)
581 arg->setNumber(arg->number() + by);
588 // FIXME: factorize those functions here with a functional style, maybe using Boost's function
591 void fixMacroInstancesAddRemove(Cursor const & from, docstring const & name, int n, bool insert) {
594 for (; dit; dit.forwardPos()) {
595 // only until a macro is redefined
596 if (dit.inset().lyxCode() == MATHMACRO_CODE) {
597 MathMacroTemplate const & macroTemplate
598 = static_cast<MathMacroTemplate const &>(dit.inset());
599 if (macroTemplate.name() == name)
603 // in front of macro instance?
604 Inset * inset = dit.nextInset();
607 InsetMath * insetMath = inset->asInsetMath();
611 MathMacro * macro = insetMath->asMacro();
612 if (macro && macro->name() == name && macro->folded()) {
613 // found macro instance
615 macro->insertArgument(n);
617 macro->removeArgument(n);
623 void fixMacroInstancesOptional(Cursor const & from, docstring const & name, int optionals) {
626 for (; dit; dit.forwardPos()) {
627 // only until a macro is redefined
628 if (dit.inset().lyxCode() == MATHMACRO_CODE) {
629 MathMacroTemplate const & macroTemplate
630 = static_cast<MathMacroTemplate const &>(dit.inset());
631 if (macroTemplate.name() == name)
635 // in front of macro instance?
636 Inset * inset = dit.nextInset();
639 InsetMath * insetMath = inset->asInsetMath();
642 MathMacro * macro = insetMath->asMacro();
643 if (macro && macro->name() == name && macro->folded()) {
644 // found macro instance
645 macro->setOptionals(optionals);
652 void fixMacroInstancesFunctional(Cursor const & from,
653 docstring const & name, F & fix) {
656 for (; dit; dit.forwardPos()) {
657 // only until a macro is redefined
658 if (dit.inset().lyxCode() == MATHMACRO_CODE) {
659 MathMacroTemplate const & macroTemplate
660 = static_cast<MathMacroTemplate const &>(dit.inset());
661 if (macroTemplate.name() == name)
665 // in front of macro instance?
666 Inset * inset = dit.nextInset();
669 InsetMath * insetMath = inset->asInsetMath();
672 MathMacro * macro = insetMath->asMacro();
673 if (macro && macro->name() == name && macro->folded())
679 void MathMacroTemplate::insertParameter(Cursor & cur, int pos, bool greedy)
681 if (pos <= numargs_ && pos >= optionals_ && numargs_ < 9) {
683 shiftArguments(pos, 1);
686 cell(defIdx()).push_back(MathAtom(new MathMacroArgument(pos + 1)));
687 if (!cell(displayIdx()).empty())
688 cell(displayIdx()).push_back(MathAtom(new MathMacroArgument(pos + 1)));
692 dit.leaveInset(*this);
693 // TODO: this was dit.forwardPosNoDescend before. Check that this is the same
694 dit.top().forwardPos();
696 // fix macro instances
697 fixMacroInstancesAddRemove(dit, name(), pos, true);
705 void MathMacroTemplate::removeParameter(Cursor & cur, int pos, bool greedy)
707 if (pos < numargs_ && pos >= 0) {
709 removeArguments(cur, pos, pos);
710 shiftArguments(pos + 1, -1);
712 // removed optional parameter?
713 if (pos < optionals_) {
715 optionalValues_[pos] = cell(optIdx(pos));
716 cells_.erase(cells_.begin() + optIdx(pos));
719 int macroSlice = cur.find(this);
720 if (macroSlice != -1) {
721 if (cur[macroSlice].idx() == optIdx(pos)) {
722 cur.cutOff(macroSlice);
723 cur[macroSlice].idx() = 1;
724 cur[macroSlice].pos() = 0;
725 } else if (cur[macroSlice].idx() > optIdx(pos))
726 --cur[macroSlice].idx();
731 // fix macro instances
732 //boost::function<void(MathMacro *)> fix = _1->insertArgument(n);
733 //fixMacroInstancesFunctional(dit, name(), fix);
735 dit.leaveInset(*this);
736 // TODO: this was dit.forwardPosNoDescend before. Check that this is the same
737 dit.top().forwardPos();
738 fixMacroInstancesAddRemove(dit, name(), pos, false);
746 void MathMacroTemplate::makeOptional(Cursor & cur) {
747 if (numargs_ > 0 && optionals_ < numargs_) {
749 cells_.insert(cells_.begin() + optIdx(optionals_ - 1), optionalValues_[optionals_ - 1]);
751 int macroSlice = cur.find(this);
752 if (macroSlice != -1 && cur[macroSlice].idx() >= optIdx(optionals_ - 1))
753 ++cur[macroSlice].idx();
755 // fix macro instances
757 dit.leaveInset(*this);
758 // TODO: this was dit.forwardPosNoDescend before. Check that this is the same
759 dit.top().forwardPos();
760 fixMacroInstancesOptional(dit, name(), optionals_);
767 void MathMacroTemplate::makeNonOptional(Cursor & cur) {
768 if (numargs_ > 0 && optionals_ > 0) {
771 // store default value for later if the use changes his mind
772 optionalValues_[optionals_] = cell(optIdx(optionals_));
773 cells_.erase(cells_.begin() + optIdx(optionals_));
776 int macroSlice = cur.find(this);
777 if (macroSlice != -1) {
778 if (cur[macroSlice].idx() > optIdx(optionals_))
779 --cur[macroSlice].idx();
780 else if (cur[macroSlice].idx() == optIdx(optionals_)) {
781 cur.cutOff(macroSlice);
782 cur[macroSlice].idx() = optIdx(optionals_);
783 cur[macroSlice].pos() = 0;
787 // fix macro instances
789 dit.leaveInset(*this);
790 // TODO: this was dit.forwardPosNoDescend before. Check that this is the same
791 dit.top().forwardPos();
792 fixMacroInstancesOptional(dit, name(), optionals_);
799 void MathMacroTemplate::doDispatch(Cursor & cur, FuncRequest & cmd)
801 string const arg = to_utf8(cmd.argument());
802 switch (cmd.action) {
804 case LFUN_MATH_MACRO_ADD_PARAM:
806 cur.recordUndoFullDocument();
807 size_t pos = numargs_;
809 pos = (size_t)convert<int>(arg) - 1; // it is checked for >=0 in getStatus
810 insertParameter(cur, pos);
815 case LFUN_MATH_MACRO_REMOVE_PARAM:
817 cur.recordUndoFullDocument();
818 size_t pos = numargs_ - 1;
820 pos = (size_t)convert<int>(arg) - 1; // it is checked for >=0 in getStatus
821 removeParameter(cur, pos);
825 case LFUN_MATH_MACRO_APPEND_GREEDY_PARAM:
827 cur.recordUndoFullDocument();
828 insertParameter(cur, numargs_, true);
832 case LFUN_MATH_MACRO_REMOVE_GREEDY_PARAM:
834 cur.recordUndoFullDocument();
835 removeParameter(cur, numargs_ - 1, true);
839 case LFUN_MATH_MACRO_MAKE_OPTIONAL:
840 cur.recordUndoFullDocument();
844 case LFUN_MATH_MACRO_MAKE_NONOPTIONAL:
845 cur.recordUndoFullDocument();
846 makeNonOptional(cur);
849 case LFUN_MATH_MACRO_ADD_OPTIONAL_PARAM:
851 cur.recordUndoFullDocument();
852 insertParameter(cur, optionals_);
857 case LFUN_MATH_MACRO_REMOVE_OPTIONAL_PARAM:
858 if (optionals_ > 0) {
859 cur.recordUndoFullDocument();
860 removeParameter(cur, optionals_ - 1);
863 case LFUN_MATH_MACRO_ADD_GREEDY_OPTIONAL_PARAM:
864 if (numargs_ == optionals_) {
865 cur.recordUndoFullDocument();
866 insertParameter(cur, 0, true);
872 InsetMathNest::doDispatch(cur, cmd);
878 bool MathMacroTemplate::getStatus(Cursor & /*cur*/, FuncRequest const & cmd,
879 FuncStatus & flag) const
882 string const arg = to_utf8(cmd.argument());
883 switch (cmd.action) {
884 case LFUN_MATH_MACRO_ADD_PARAM: {
885 int num = numargs_ + 1;
887 num = convert<int>(arg);
888 bool on = (num >= optionals_
889 && numargs_ < 9 && num <= numargs_ + 1);
894 case LFUN_MATH_MACRO_APPEND_GREEDY_PARAM:
895 flag.enabled(numargs_ < 9);
898 case LFUN_MATH_MACRO_REMOVE_PARAM: {
901 num = convert<int>(arg);
902 flag.enabled(num >= 1 && num <= numargs_);
906 case LFUN_MATH_MACRO_MAKE_OPTIONAL:
907 flag.enabled(numargs_ > 0
908 && optionals_ < numargs_
909 && type_ != MacroTypeDef);
912 case LFUN_MATH_MACRO_MAKE_NONOPTIONAL:
913 flag.enabled(optionals_ > 0
914 && type_ != MacroTypeDef);
917 case LFUN_MATH_MACRO_ADD_OPTIONAL_PARAM:
918 flag.enabled(numargs_ < 9);
921 case LFUN_MATH_MACRO_REMOVE_OPTIONAL_PARAM:
922 flag.enabled(optionals_ > 0);
925 case LFUN_MATH_MACRO_ADD_GREEDY_OPTIONAL_PARAM:
926 flag.enabled(numargs_ == 0
927 && type_ != MacroTypeDef);
930 case LFUN_IN_MATHMACROTEMPLATE:
942 void MathMacroTemplate::read(Buffer const &, Lexer & lex)
945 mathed_parse_cell(ar, lex.getStream());
946 if (ar.size() != 1 || !ar[0]->asMacroTemplate()) {
947 lyxerr << "Cannot read macro from '" << ar << "'" << endl;
948 lyxerr << "Read: " << to_utf8(asString(ar)) << endl;
951 operator=( *(ar[0]->asMacroTemplate()) );
957 void MathMacroTemplate::write(Buffer const &, ostream & os) const
959 odocstringstream oss;
960 WriteStream wi(oss, false, false);
961 oss << "FormulaMacro\n";
963 os << to_utf8(oss.str());
967 void MathMacroTemplate::write(WriteStream & os) const
973 void MathMacroTemplate::write(WriteStream & os, bool /*overwriteRedefinition*/) const
975 // newcommand or renewcommand
976 if (os.latex() && optionals_ > 1)
977 os << "\\newlyxcommand";
980 os << "\\renewcommand";
982 os << "\\newcommand";
984 os << "{\\" << name().c_str() << '}';
986 os << '[' << numargs_ << ']';
989 for (int i = 0; i < optionals_; ++i) {
990 docstring optValue = asString(cell(optIdx(i)));
991 if (optValue.find(']') != docstring::npos)
992 os << "[{" << cell(optIdx(i)) << "}]";
994 os << "[" << cell(optIdx(i)) << "]";
997 os << "{" << cell(defIdx()) << "}";
1000 // writing .tex. done.
1003 // writing .lyx, write special .tex export only if necessary
1004 if (!cell(displayIdx()).empty())
1005 os << "\n{" << cell(displayIdx()) << '}';
1010 int MathMacroTemplate::plaintext(Buffer const & buf, odocstream & os,
1011 OutputParams const &) const
1013 static docstring const str = '[' + buf.B_("math macro") + ']';
1020 bool MathMacroTemplate::validName() const
1022 docstring n = name();
1028 // converting back and force doesn't swallow anything?
1031 if (asString(ma) != n)
1034 // valid characters?
1035 for (size_t i = 0; i < n.size(); ++i) {
1036 if (!(n[i] >= 'a' && n[i] <= 'z') &&
1037 !(n[i] >= 'A' && n[i] <= 'Z'))
1045 bool MathMacroTemplate::validMacro() const
1051 bool MathMacroTemplate::fixNameAndCheckIfValid()
1053 // check all the characters/insets in the name cell
1055 MathData & data = cell(0);
1056 while (i < data.size()) {
1057 InsetMathChar const * cinset = data[i]->asCharInset();
1059 // valid character in [a-zA-Z]?
1060 char_type c = cinset->getChar();
1061 if ((c >= 'a' && c <= 'z')
1062 || (c >= 'A' && c <= 'Z')) {
1072 // now it should be valid if anything in the name survived
1073 return data.size() > 0;
1077 void MathMacroTemplate::validate(LaTeXFeatures & features) const
1079 if (optionals_ > 1) {
1080 features.require("newlyxcommand");
1084 void MathMacroTemplate::getDefaults(vector<docstring> & defaults) const
1086 defaults.resize(numargs_);
1087 for (int i = 0; i < optionals_; ++i)
1088 defaults[i] = asString(cell(optIdx(i)));
1092 docstring MathMacroTemplate::definition() const
1094 return asString(cell(defIdx()));
1098 docstring MathMacroTemplate::displayDefinition() const
1100 return asString(cell(displayIdx()));
1104 size_t MathMacroTemplate::numArgs() const
1110 size_t MathMacroTemplate::numOptionals() const
1116 void MathMacroTemplate::infoize(odocstream & os) const
1118 os << "Math Macro: \\" << name();