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 return new MathMacroTemplate(*this);
400 docstring MathMacroTemplate::name() const
402 return asString(cell(0));
406 void MathMacroTemplate::updateToContext(MacroContext const & mc) const
408 redefinition_ = mc.get(name()) != 0;
412 void MathMacroTemplate::updateLook() const
414 lookOutdated_ = true;
418 void MathMacroTemplate::createLook() const
423 look_.push_back(MathAtom(new InsetLabelBox(_("Name"), *this, false)));
424 MathData & nameData = look_[look_.size() - 1].nucleus()->cell(0);
425 nameData.push_back(MathAtom(new InsetNameWrapper(&cell(0), *this)));
429 if (optionals_ > 0) {
430 look_.push_back(MathAtom(new InsetLabelBox(_("optional"), *this, false)));
431 MathData & optData = look_[look_.size() - 1].nucleus()->cell(0);
433 for (; i < optionals_; ++i) {
434 optData.push_back(MathAtom(new InsetMathChar('[')));
435 optData.push_back(MathAtom(new InsetMathWrapper(&cell(1 + i))));
436 optData.push_back(MathAtom(new InsetMathChar(']')));
441 for (; i < numargs_; ++i) {
443 arg.push_back(MathAtom(new MathMacroArgument(i + 1)));
444 look_.push_back(MathAtom(new InsetMathBrace(arg)));
448 look_.push_back(MathAtom(new InsetMathChar(':')));
449 look_.push_back(MathAtom(new InsetMathChar('=')));
452 look_.push_back(MathAtom(
453 new InsetLabelBox(MathAtom(
454 new InsetMathWrapper(&cell(defIdx()))), _("TeX"), *this, true)));
457 look_.push_back(MathAtom(
458 new DisplayLabelBox(MathAtom(
459 new InsetMathWrapper(&cell(displayIdx()))), _("LyX"), *this)));
463 void MathMacroTemplate::metrics(MetricsInfo & mi, Dimension & dim) const
465 FontSetChanger dummy1(mi.base, from_ascii("mathnormal"));
466 StyleChanger dummy2(mi.base, LM_ST_TEXT);
469 MacroData const * macro = 0;
471 macro = mi.macrocontext.get(name());
473 // updateToContext() - avoids another lookup
474 redefinition_ = macro != 0;
479 lookOutdated_ = false;
483 /// metrics for inset contents
487 // first phase, premetric:
489 look_.metrics(mi, dim);
490 labelBoxAscent_ = dim.asc;
491 labelBoxDescent_ = dim.des;
493 // second phase, main metric:
495 look_.metrics(mi, dim);
504 setDimCache(mi, dim);
508 void MathMacroTemplate::draw(PainterInfo & pi, int x, int y) const
510 FontSetChanger dummy1(pi.base, from_ascii("mathnormal"));
511 StyleChanger dummy2(pi.base, LM_ST_TEXT);
513 setPosCache(pi, x, y);
514 Dimension const dim = dimension(*pi.base.bv);
517 int const a = y - dim.asc + 1;
518 int const w = dim.wid - 2;
519 int const h = dim.height() - 2;
520 pi.pain.rectangle(x, a, w, h, Color_mathframe);
522 // just to be sure: set some dummy values for coord cache
523 for (idx_type i = 0; i < nargs(); ++i) {
524 cell(i).setXY(*pi.base.bv, x, y);
528 look_.draw(pi, x + 3, y);
532 void MathMacroTemplate::edit(Cursor & cur, bool left)
535 cur.updateFlags(Update::Force);
536 InsetMathNest::edit(cur, left);
540 bool MathMacroTemplate::notifyCursorLeaves(Cursor & cur)
543 cur.updateFlags(Update::Force);
544 return InsetMathNest::notifyCursorLeaves(cur);
548 void MathMacroTemplate::removeArguments(Cursor & cur, int from, int to) {
549 for (DocIterator it = doc_iterator_begin(*this); it; it.forwardChar()) {
552 if (it.nextInset()->lyxCode() != MATHMACROARG_CODE)
554 MathMacroArgument * arg = static_cast<MathMacroArgument*>(it.nextInset());
555 int n = arg->number() - 1;
556 if (from <= n && n <= to) {
557 int cellSlice = cur.find(it.cell());
558 if (cellSlice != -1 && cur[cellSlice].pos() > it.pos())
559 --cur[cellSlice].pos();
561 it.cell().erase(it.pos());
569 void MathMacroTemplate::shiftArguments(size_t from, int by) {
570 for (DocIterator it = doc_iterator_begin(*this); it; it.forwardChar()) {
573 if (it.nextInset()->lyxCode() != MATHMACROARG_CODE)
575 MathMacroArgument * arg = static_cast<MathMacroArgument*>(it.nextInset());
576 if (arg->number() >= from + 1)
577 arg->setNumber(arg->number() + by);
584 // FIXME: factorize those functions here with a functional style, maybe using Boost's function
587 void fixMacroInstancesAddRemove(Cursor const & from, docstring const & name, int n, bool insert) {
590 for (; dit; dit.forwardPos()) {
591 // only until a macro is redefined
592 if (dit.inset().lyxCode() == MATHMACRO_CODE) {
593 MathMacroTemplate const & macroTemplate
594 = static_cast<MathMacroTemplate const &>(dit.inset());
595 if (macroTemplate.name() == name)
599 // in front of macro instance?
600 Inset * inset = dit.nextInset();
603 InsetMath * insetMath = inset->asInsetMath();
607 MathMacro * macro = insetMath->asMacro();
608 if (macro && macro->name() == name && macro->folded()) {
609 // found macro instance
611 macro->insertArgument(n);
613 macro->removeArgument(n);
619 void fixMacroInstancesOptional(Cursor const & from, docstring const & name, int optionals) {
622 for (; dit; dit.forwardPos()) {
623 // only until a macro is redefined
624 if (dit.inset().lyxCode() == MATHMACRO_CODE) {
625 MathMacroTemplate const & macroTemplate
626 = static_cast<MathMacroTemplate const &>(dit.inset());
627 if (macroTemplate.name() == name)
631 // in front of macro instance?
632 Inset * inset = dit.nextInset();
635 InsetMath * insetMath = inset->asInsetMath();
638 MathMacro * macro = insetMath->asMacro();
639 if (macro && macro->name() == name && macro->folded()) {
640 // found macro instance
641 macro->setOptionals(optionals);
648 void fixMacroInstancesFunctional(Cursor const & from,
649 docstring const & name, F & fix) {
652 for (; dit; dit.forwardPos()) {
653 // only until a macro is redefined
654 if (dit.inset().lyxCode() == MATHMACRO_CODE) {
655 MathMacroTemplate const & macroTemplate
656 = static_cast<MathMacroTemplate const &>(dit.inset());
657 if (macroTemplate.name() == name)
661 // in front of macro instance?
662 Inset * inset = dit.nextInset();
665 InsetMath * insetMath = inset->asInsetMath();
668 MathMacro * macro = insetMath->asMacro();
669 if (macro && macro->name() == name && macro->folded())
675 void MathMacroTemplate::insertParameter(Cursor & cur, int pos, bool greedy)
677 if (pos <= numargs_ && pos >= optionals_ && numargs_ < 9) {
679 shiftArguments(pos, 1);
682 cell(defIdx()).push_back(MathAtom(new MathMacroArgument(pos + 1)));
683 if (!cell(displayIdx()).empty())
684 cell(displayIdx()).push_back(MathAtom(new MathMacroArgument(pos + 1)));
688 dit.leaveInset(*this);
689 // TODO: this was dit.forwardPosNoDescend before. Check that this is the same
690 dit.top().forwardPos();
692 // fix macro instances
693 fixMacroInstancesAddRemove(dit, name(), pos, true);
701 void MathMacroTemplate::removeParameter(Cursor & cur, int pos, bool greedy)
703 if (pos < numargs_ && pos >= 0) {
705 removeArguments(cur, pos, pos);
706 shiftArguments(pos + 1, -1);
708 // removed optional parameter?
709 if (pos < optionals_) {
711 optionalValues_[pos] = cell(optIdx(pos));
712 cells_.erase(cells_.begin() + optIdx(pos));
715 int macroSlice = cur.find(this);
716 if (macroSlice != -1) {
717 if (cur[macroSlice].idx() == optIdx(pos)) {
718 cur.cutOff(macroSlice);
719 cur[macroSlice].idx() = 1;
720 cur[macroSlice].pos() = 0;
721 } else if (cur[macroSlice].idx() > optIdx(pos))
722 --cur[macroSlice].idx();
727 // fix macro instances
728 //boost::function<void(MathMacro *)> fix = _1->insertArgument(n);
729 //fixMacroInstancesFunctional(dit, name(), fix);
731 dit.leaveInset(*this);
732 // TODO: this was dit.forwardPosNoDescend before. Check that this is the same
733 dit.top().forwardPos();
734 fixMacroInstancesAddRemove(dit, name(), pos, false);
742 void MathMacroTemplate::makeOptional(Cursor & cur) {
743 if (numargs_ > 0 && optionals_ < numargs_) {
745 cells_.insert(cells_.begin() + optIdx(optionals_ - 1), optionalValues_[optionals_ - 1]);
747 int macroSlice = cur.find(this);
748 if (macroSlice != -1 && cur[macroSlice].idx() >= optIdx(optionals_ - 1))
749 ++cur[macroSlice].idx();
751 // fix macro instances
753 dit.leaveInset(*this);
754 // TODO: this was dit.forwardPosNoDescend before. Check that this is the same
755 dit.top().forwardPos();
756 fixMacroInstancesOptional(dit, name(), optionals_);
763 void MathMacroTemplate::makeNonOptional(Cursor & cur) {
764 if (numargs_ > 0 && optionals_ > 0) {
767 // store default value for later if the use changes his mind
768 optionalValues_[optionals_] = cell(optIdx(optionals_));
769 cells_.erase(cells_.begin() + optIdx(optionals_));
772 int macroSlice = cur.find(this);
773 if (macroSlice != -1) {
774 if (cur[macroSlice].idx() > optIdx(optionals_))
775 --cur[macroSlice].idx();
776 else if (cur[macroSlice].idx() == optIdx(optionals_)) {
777 cur.cutOff(macroSlice);
778 cur[macroSlice].idx() = optIdx(optionals_);
779 cur[macroSlice].pos() = 0;
783 // fix macro instances
785 dit.leaveInset(*this);
786 // TODO: this was dit.forwardPosNoDescend before. Check that this is the same
787 dit.top().forwardPos();
788 fixMacroInstancesOptional(dit, name(), optionals_);
795 void MathMacroTemplate::doDispatch(Cursor & cur, FuncRequest & cmd)
797 string const arg = to_utf8(cmd.argument());
798 switch (cmd.action) {
800 case LFUN_MATH_MACRO_ADD_PARAM:
802 cur.recordUndoFullDocument();
803 size_t pos = numargs_;
805 pos = (size_t)convert<int>(arg) - 1; // it is checked for >=0 in getStatus
806 insertParameter(cur, pos);
811 case LFUN_MATH_MACRO_REMOVE_PARAM:
813 cur.recordUndoFullDocument();
814 size_t pos = numargs_ - 1;
816 pos = (size_t)convert<int>(arg) - 1; // it is checked for >=0 in getStatus
817 removeParameter(cur, pos);
821 case LFUN_MATH_MACRO_APPEND_GREEDY_PARAM:
823 cur.recordUndoFullDocument();
824 insertParameter(cur, numargs_, true);
828 case LFUN_MATH_MACRO_REMOVE_GREEDY_PARAM:
830 cur.recordUndoFullDocument();
831 removeParameter(cur, numargs_ - 1, true);
835 case LFUN_MATH_MACRO_MAKE_OPTIONAL:
836 cur.recordUndoFullDocument();
840 case LFUN_MATH_MACRO_MAKE_NONOPTIONAL:
841 cur.recordUndoFullDocument();
842 makeNonOptional(cur);
845 case LFUN_MATH_MACRO_ADD_OPTIONAL_PARAM:
847 cur.recordUndoFullDocument();
848 insertParameter(cur, optionals_);
853 case LFUN_MATH_MACRO_REMOVE_OPTIONAL_PARAM:
854 if (optionals_ > 0) {
855 cur.recordUndoFullDocument();
856 removeParameter(cur, optionals_ - 1);
859 case LFUN_MATH_MACRO_ADD_GREEDY_OPTIONAL_PARAM:
860 if (numargs_ == optionals_) {
861 cur.recordUndoFullDocument();
862 insertParameter(cur, 0, true);
868 InsetMathNest::doDispatch(cur, cmd);
874 bool MathMacroTemplate::getStatus(Cursor & /*cur*/, FuncRequest const & cmd,
875 FuncStatus & flag) const
878 string const arg = to_utf8(cmd.argument());
879 switch (cmd.action) {
880 case LFUN_MATH_MACRO_ADD_PARAM: {
881 int num = numargs_ + 1;
883 num = convert<int>(arg);
884 bool on = (num >= optionals_
885 && numargs_ < 9 && num <= numargs_ + 1);
890 case LFUN_MATH_MACRO_APPEND_GREEDY_PARAM:
891 flag.enabled(numargs_ < 9);
894 case LFUN_MATH_MACRO_REMOVE_PARAM: {
897 num = convert<int>(arg);
898 flag.enabled(num >= 1 && num <= numargs_);
902 case LFUN_MATH_MACRO_MAKE_OPTIONAL:
903 flag.enabled(numargs_ > 0
904 && optionals_ < numargs_
905 && type_ != MacroTypeDef);
908 case LFUN_MATH_MACRO_MAKE_NONOPTIONAL:
909 flag.enabled(optionals_ > 0
910 && type_ != MacroTypeDef);
913 case LFUN_MATH_MACRO_ADD_OPTIONAL_PARAM:
914 flag.enabled(numargs_ < 9);
917 case LFUN_MATH_MACRO_REMOVE_OPTIONAL_PARAM:
918 flag.enabled(optionals_ > 0);
921 case LFUN_MATH_MACRO_ADD_GREEDY_OPTIONAL_PARAM:
922 flag.enabled(numargs_ == 0
923 && type_ != MacroTypeDef);
926 case LFUN_IN_MATHMACROTEMPLATE:
938 void MathMacroTemplate::read(Buffer const &, Lexer & lex)
941 mathed_parse_cell(ar, lex.getStream());
942 if (ar.size() != 1 || !ar[0]->asMacroTemplate()) {
943 lyxerr << "Cannot read macro from '" << ar << "'" << endl;
944 lyxerr << "Read: " << to_utf8(asString(ar)) << endl;
947 operator=( *(ar[0]->asMacroTemplate()) );
953 void MathMacroTemplate::write(Buffer const &, ostream & os) const
955 odocstringstream oss;
956 WriteStream wi(oss, false, false);
957 oss << "FormulaMacro\n";
959 os << to_utf8(oss.str());
963 void MathMacroTemplate::write(WriteStream & os) const
969 void MathMacroTemplate::write(WriteStream & os, bool overwriteRedefinition) const
971 // newcommand or renewcommand
972 if (os.latex() && optionals_ > 1)
973 os << "\\newlyxcommand";
976 os << "\\renewcommand";
978 os << "\\newcommand";
980 os << "{\\" << name().c_str() << '}';
982 os << '[' << numargs_ << ']';
985 for (int i = 0; i < optionals_; ++i) {
986 docstring optValue = asString(cell(optIdx(i)));
987 if (optValue.find(']') != docstring::npos)
988 os << "[{" << cell(optIdx(i)) << "}]";
990 os << "[" << cell(optIdx(i)) << "]";
993 os << "{" << cell(defIdx()) << "}";
996 // writing .tex. done.
999 // writing .lyx, write special .tex export only if necessary
1000 if (!cell(displayIdx()).empty())
1001 os << "\n{" << cell(displayIdx()) << '}';
1006 int MathMacroTemplate::plaintext(Buffer const & buf, odocstream & os,
1007 OutputParams const &) const
1009 static docstring const str = '[' + buf.B_("math macro") + ']';
1016 bool MathMacroTemplate::validName() const
1018 docstring n = name();
1024 // converting back and force doesn't swallow anything?
1027 if (asString(ma) != n)
1030 // valid characters?
1031 for (size_t i = 0; i < n.size(); ++i) {
1032 if (!(n[i] >= 'a' && n[i] <= 'z') &&
1033 !(n[i] >= 'A' && n[i] <= 'Z'))
1041 bool MathMacroTemplate::validMacro() const
1047 bool MathMacroTemplate::fixNameAndCheckIfValid()
1049 // check all the characters/insets in the name cell
1051 MathData & data = cell(0);
1052 while (i < data.size()) {
1053 InsetMathChar const * cinset = data[i]->asCharInset();
1055 // valid character in [a-zA-Z]?
1056 char_type c = cinset->getChar();
1057 if ((c >= 'a' && c <= 'z')
1058 || (c >= 'A' && c <= 'Z')) {
1068 // now it should be valid if anything in the name survived
1069 return data.size() > 0;
1073 void MathMacroTemplate::validate(LaTeXFeatures & features) const
1075 if (optionals_ > 1) {
1076 features.require("newlyxcommand");
1080 void MathMacroTemplate::getDefaults(vector<docstring> & defaults) const
1082 defaults.resize(numargs_);
1083 for (int i = 0; i < optionals_; ++i)
1084 defaults[i] = asString(cell(optIdx(i)));
1088 docstring MathMacroTemplate::definition() const
1090 return asString(cell(defIdx()));
1094 docstring MathMacroTemplate::displayDefinition() const
1096 return asString(cell(displayIdx()));
1100 size_t MathMacroTemplate::numArgs() const
1106 size_t MathMacroTemplate::numOptionals() const
1112 void MathMacroTemplate::infoize(odocstream & os) const
1114 os << "Math Macro: \\" << name();