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_;
79 mutable Dimension cellDim_;
83 InsetLabelBox::InsetLabelBox(MathAtom const & atom, docstring label,
84 MathMacroTemplate const & parent, bool frame)
85 : InsetMathNest(1), parent_(parent), label_(label), frame_(frame)
87 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);
116 // adjust to common height in main metrics phase
117 if (!parent_.premetrics()) {
118 dim.asc = max(dim.asc, parent_.commonLabelBoxAscent());
119 dim.des = max(dim.des, parent_.commonLabelBoxDescent());
123 if (label_.length() > 0) {
125 FontInfo font = sane_font;
126 font.setSize(FONT_SIZE_TINY);
127 font.setColor(Color_mathmacrolabel);
129 // make space for label and box
130 int lwid = mathed_string_width(font, label_);
133 math_font_max_dim(font, maxasc, maxdes);
135 dim.wid = max(dim.wid, lwid + 2);
137 // space for the label
138 if (!parent_.premetrics())
139 dim.des += maxasc + maxdes + 1;
142 setDimCache(mi, dim);
146 void InsetLabelBox::draw(PainterInfo & pi, int x, int y) const
148 Dimension const dim = dimension(*pi.base.bv);
151 cell(0).draw(pi, x + (dim.wid - cellDim_.wid) / 2, y);
154 if (label_.length() > 0) {
156 FontInfo font = sane_font;
157 font.setSize(FONT_SIZE_TINY);
158 font.setColor(Color_mathmacrolabel);
160 // make space for label and box
161 int lwid = mathed_string_width(font, label_);
164 math_font_max_dim(font, maxasc, maxdes);
167 pi.pain.text(x + (dim.wid - lwid) / 2, y + dim.des - maxdes, label_, font);
169 pi.pain.text(x, y + dim.des - maxdes, label_, font);
173 int boxHeight = parent_.commonLabelBoxAscent() + parent_.commonLabelBoxDescent();
175 pi.pain.rectangle(x + 1, y - dim.ascent() + 1,
176 dim.wid - 2, boxHeight - 2,
182 //////////////////////////////////////////////////////////////////////
184 class InsetMathWrapper : public InsetMath {
187 InsetMathWrapper(MathData const * value) : value_(value) {}
189 void metrics(MetricsInfo & mi, Dimension & dim) const;
191 void draw(PainterInfo &, int x, int y) const;
195 Inset * clone() const;
197 MathData const * value_;
201 Inset * InsetMathWrapper::clone() const
203 return new InsetMathWrapper(*this);
207 void InsetMathWrapper::metrics(MetricsInfo & mi, Dimension & dim) const
209 value_->metrics(mi, dim);
210 //metricsMarkers2(dim);
214 void InsetMathWrapper::draw(PainterInfo & pi, int x, int y) const
216 value_->draw(pi, x, y);
217 //drawMarkers(pi, x, y);
221 ///////////////////////////////////////////////////////////////////////
223 class InsetNameWrapper : public InsetMathWrapper {
226 InsetNameWrapper(MathData const * value, MathMacroTemplate const & parent);
228 void metrics(MetricsInfo & mi, Dimension & dim) const;
230 void draw(PainterInfo &, int x, int y) const;
234 MathMacroTemplate const & parent_;
236 Inset * clone() const;
240 InsetNameWrapper::InsetNameWrapper(MathData const * value,
241 MathMacroTemplate const & parent)
242 : InsetMathWrapper(value), parent_(parent)
247 Inset * InsetNameWrapper::clone() const
249 return new InsetNameWrapper(*this);
253 void InsetNameWrapper::metrics(MetricsInfo & mi, Dimension & dim) const
255 InsetMathWrapper::metrics(mi, dim);
256 dim.wid += mathed_string_width(mi.base.font, from_ascii("\\"));
260 void InsetNameWrapper::draw(PainterInfo & pi, int x, int y) const
263 PainterInfo namepi = pi;
264 if (parent_.validMacro())
265 namepi.base.font.setColor(Color_latex);
267 namepi.base.font.setColor(Color_error);
270 pi.pain.text(x, y, from_ascii("\\"), namepi.base.font);
271 x += mathed_string_width(namepi.base.font, from_ascii("\\"));
274 InsetMathWrapper::draw(namepi, x, y);
278 ///////////////////////////////////////////////////////////////////////
281 MathMacroTemplate::MathMacroTemplate()
282 : InsetMathNest(3), numargs_(0), optionals_(0),
283 type_(MacroTypeNewcommand), editing_(false), lookOutdated_(true)
289 MathMacroTemplate::MathMacroTemplate(docstring const & name, int numargs,
290 int optionals, MacroType type,
291 vector<MathData> const & optionalValues,
292 MathData const & def, MathData const & display)
293 : InsetMathNest(optionals + 3), numargs_(numargs),
294 optionals_(optionals), optionalValues_(optionalValues),
295 type_(type), editing_(false), lookOutdated_(true)
300 lyxerr << "MathMacroTemplate::MathMacroTemplate: wrong # of arguments: "
303 asArray(name, cell(0));
304 optionalValues_.resize(9);
305 for (int i = 0; i < optionals_; ++i)
306 cell(optIdx(i)) = optionalValues_[i];
307 cell(defIdx()) = def;
308 cell(displayIdx()) = display;
314 MathMacroTemplate::MathMacroTemplate(docstring const & str)
315 : InsetMathNest(3), numargs_(0), optionals_(0),
316 type_(MacroTypeNewcommand), editing_(false), lookOutdated_(true)
321 mathed_parse_cell(ar, str);
322 if (ar.size() != 1 || !ar[0]->asMacroTemplate()) {
323 lyxerr << "Cannot read macro from '" << ar << "'" << endl;
324 asArray(from_ascii("invalidmacro"), cell(0));
325 // FIXME: The macro template does not make sense after this.
326 // The whole parsing should not be in a constructor which
327 // has no chance to report failure.
330 operator=( *(ar[0]->asMacroTemplate()) );
336 Inset * MathMacroTemplate::clone() const
338 return new MathMacroTemplate(*this);
342 docstring MathMacroTemplate::name() const
344 return asString(cell(0));
348 void MathMacroTemplate::updateToContext(MacroContext const & mc) const
350 redefinition_ = mc.get(name()) != 0;
354 void MathMacroTemplate::updateLook() const
356 lookOutdated_ = true;
359 void MathMacroTemplate::createLook() const
364 look_.push_back(MathAtom(new InsetLabelBox(
365 editing_ ? _("name") : docstring(),
368 MathData & nameData = look_[look_.size() - 1].nucleus()->cell(0);
369 nameData.push_back(MathAtom(new InsetNameWrapper(&cell(0), *this)));
373 if (optionals_ > 0) {
374 look_.push_back(MathAtom(new InsetLabelBox(
375 editing_ ? _("optional") : docstring(),
378 MathData & optData = look_[look_.size() - 1].nucleus()->cell(0);
380 for (; i < optionals_; ++i) {
381 optData.push_back(MathAtom(new InsetMathChar('[')));
382 optData.push_back(MathAtom(new InsetMathWrapper(&cell(1 + i))));
383 optData.push_back(MathAtom(new InsetMathChar(']')));
388 for (; i < numargs_; ++i) {
390 arg.push_back(MathAtom(new MathMacroArgument(i + 1)));
391 look_.push_back(MathAtom(new InsetMathBrace(arg)));
395 look_.push_back(MathAtom(new InsetMathChar(':')));
396 look_.push_back(MathAtom(new InsetMathChar('=')));
400 MathAtom(new InsetLabelBox(
401 MathAtom(new InsetMathWrapper(&cell(defIdx()))),
402 editing_ ? from_ascii("TeX") : docstring(),
407 if (editing_ || !cell(displayIdx()).empty()) {
409 MathAtom(new InsetLabelBox(
410 MathAtom(new InsetMathWrapper(&cell(displayIdx()))),
411 editing_ ? from_ascii("LyX") : docstring(),
418 void MathMacroTemplate::metrics(MetricsInfo & mi, Dimension & dim) const
420 FontSetChanger dummy1(mi.base, from_ascii("mathnormal"));
421 StyleChanger dummy2(mi.base, LM_ST_TEXT);
424 MacroData const * macro = 0;
426 macro = mi.macrocontext.get(name());
428 // updateToContext() - avoids another lookup
429 redefinition_ = macro != 0;
433 bool realEditing = editing(mi.base.bv);
434 if (editing_ != realEditing) {
435 editing_ = realEditing;
436 lookOutdated_ = true;
441 lookOutdated_ = false;
445 /// metrics for inset contents
449 // first phase, premetric:
451 look_.metrics(mi, dim);
452 labelBoxAscent_ = dim.asc;
453 labelBoxDescent_ = dim.des;
455 // second phase, main metric:
457 look_.metrics(mi, dim);
459 // metrics for invisible display box
460 if (!editing_ && cell(displayIdx()).empty()) {
462 cell(displayIdx()).metrics(mi, ddim);
472 setDimCache(mi, dim);
476 void MathMacroTemplate::draw(PainterInfo & pi, int x, int y) const
478 FontSetChanger dummy1(pi.base, from_ascii("mathnormal"));
479 StyleChanger dummy2(pi.base, LM_ST_TEXT);
481 setPosCache(pi, x, y);
482 Dimension const dim = dimension(*pi.base.bv);
485 int const a = y - dim.asc + 1;
486 int const w = dim.wid - 2;
487 int const h = dim.height() - 2;
488 pi.pain.rectangle(x, a, w, h, Color_mathframe);
490 // just to be sure: set some dummy values for coord cache
491 for (idx_type i = 0; i < nargs(); ++i) {
492 cell(i).setXY(*pi.base.bv, x, y);
496 look_.draw(pi, x + 3, y);
500 void MathMacroTemplate::edit(Cursor & cur, bool left)
503 cur.updateFlags(Update::Force);
504 InsetMathNest::edit(cur, left);
508 Inset * MathMacroTemplate::editXY(Cursor & cur, int x, int y)
510 Inset * ret = InsetMathNest::editXY(cur, x, y);
511 /* if (!editing_ && editing(cur.bv())) {
513 cur.updateFlags(Update::Force);
519 bool MathMacroTemplate::notifyCursorLeaves(Cursor & cur)
522 cur.updateFlags(Update::Force);
523 return InsetMathNest::notifyCursorLeaves(cur);
527 void MathMacroTemplate::removeArguments(Cursor & cur, int from, int to) {
528 for (DocIterator it = doc_iterator_begin(*this); it; it.forwardChar()) {
531 if (it.nextInset()->lyxCode() != MATHMACROARG_CODE)
533 MathMacroArgument * arg = static_cast<MathMacroArgument*>(it.nextInset());
534 int n = arg->number() - 1;
535 if (from <= n && n <= to) {
536 int cellSlice = cur.find(it.cell());
537 if (cellSlice != -1 && cur[cellSlice].pos() > it.pos())
538 --cur[cellSlice].pos();
540 it.cell().erase(it.pos());
548 void MathMacroTemplate::shiftArguments(size_t from, int by) {
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 if (arg->number() >= from + 1)
556 arg->setNumber(arg->number() + by);
563 // FIXME: factorize those functions here with a functional style, maybe using Boost's function
566 void fixMacroInstancesAddRemove(Cursor const & from, docstring const & name, int n, bool insert) {
569 for (; dit; dit.forwardPos()) {
570 // only until a macro is redefined
571 if (dit.inset().lyxCode() == MATHMACRO_CODE) {
572 MathMacroTemplate const & macroTemplate
573 = static_cast<MathMacroTemplate const &>(dit.inset());
574 if (macroTemplate.name() == name)
578 // in front of macro instance?
579 Inset * inset = dit.nextInset();
581 InsetMath * insetMath = inset->asInsetMath();
583 MathMacro * macro = insetMath->asMacro();
584 if (macro && macro->name() == name && macro->folded()) {
585 // found macro instance
587 macro->insertArgument(n);
589 macro->removeArgument(n);
597 void fixMacroInstancesOptional(Cursor const & from, docstring const & name, int optionals) {
600 for (; dit; dit.forwardPos()) {
601 // only until a macro is redefined
602 if (dit.inset().lyxCode() == MATHMACRO_CODE) {
603 MathMacroTemplate const & macroTemplate
604 = static_cast<MathMacroTemplate const &>(dit.inset());
605 if (macroTemplate.name() == name)
609 // in front of macro instance?
610 Inset * inset = dit.nextInset();
612 InsetMath * insetMath = inset->asInsetMath();
614 MathMacro * macro = insetMath->asMacro();
615 if (macro && macro->name() == name && macro->folded()) {
616 // found macro instance
617 macro->setOptionals(optionals);
626 void fixMacroInstancesFunctional(Cursor const & from,
627 docstring const & name, F & fix) {
630 for (; dit; dit.forwardPos()) {
631 // only until a macro is redefined
632 if (dit.inset().lyxCode() == MATHMACRO_CODE) {
633 MathMacroTemplate const & macroTemplate
634 = static_cast<MathMacroTemplate const &>(dit.inset());
635 if (macroTemplate.name() == name)
639 // in front of macro instance?
640 Inset * inset = dit.nextInset();
642 InsetMath * insetMath = inset->asInsetMath();
644 MathMacro * macro = insetMath->asMacro();
645 if (macro && macro->name() == name && macro->folded())
653 void MathMacroTemplate::insertParameter(Cursor & cur, int pos, bool greedy)
655 if (pos <= numargs_ && pos >= optionals_ && numargs_ < 9) {
657 shiftArguments(pos, 1);
660 cell(defIdx()).push_back(MathAtom(new MathMacroArgument(pos + 1)));
661 if (!cell(displayIdx()).empty())
662 cell(displayIdx()).push_back(MathAtom(new MathMacroArgument(pos + 1)));
666 dit.leaveInset(*this);
667 // TODO: this was dit.forwardPosNoDescend before. Check that this is the same
668 dit.top().forwardPos();
670 // fix macro instances
671 fixMacroInstancesAddRemove(dit, name(), pos, true);
679 void MathMacroTemplate::removeParameter(Cursor & cur, int pos, bool greedy)
681 if (pos < numargs_ && pos >= 0) {
683 removeArguments(cur, pos, pos);
684 shiftArguments(pos + 1, -1);
686 // removed optional parameter?
687 if (pos < optionals_) {
689 optionalValues_[pos] = cell(optIdx(pos));
690 cells_.erase(cells_.begin() + optIdx(pos));
693 int macroSlice = cur.find(this);
694 if (macroSlice != -1) {
695 if (cur[macroSlice].idx() == optIdx(pos)) {
696 cur.cutOff(macroSlice);
697 cur[macroSlice].idx() = 1;
698 cur[macroSlice].pos() = 0;
699 } else if (cur[macroSlice].idx() > optIdx(pos))
700 --cur[macroSlice].idx();
705 // fix macro instances
706 //boost::function<void(MathMacro *)> fix = _1->insertArgument(n);
707 //fixMacroInstancesFunctional(dit, name(), fix);
709 dit.leaveInset(*this);
710 // TODO: this was dit.forwardPosNoDescend before. Check that this is the same
711 dit.top().forwardPos();
712 fixMacroInstancesAddRemove(dit, name(), pos, false);
720 void MathMacroTemplate::makeOptional(Cursor & cur) {
721 if (numargs_ > 0 && optionals_ < numargs_) {
723 cells_.insert(cells_.begin() + optIdx(optionals_ - 1), optionalValues_[optionals_ - 1]);
725 int macroSlice = cur.find(this);
726 if (macroSlice != -1 && cur[macroSlice].idx() >= optIdx(optionals_ - 1))
727 ++cur[macroSlice].idx();
729 // fix macro instances
731 dit.leaveInset(*this);
732 // TODO: this was dit.forwardPosNoDescend before. Check that this is the same
733 dit.top().forwardPos();
734 fixMacroInstancesOptional(dit, name(), optionals_);
741 void MathMacroTemplate::makeNonOptional(Cursor & cur) {
742 if (numargs_ > 0 && optionals_ > 0) {
745 // store default value for later if the use changes his mind
746 optionalValues_[optionals_] = cell(optIdx(optionals_));
747 cells_.erase(cells_.begin() + optIdx(optionals_));
750 int macroSlice = cur.find(this);
751 if (macroSlice != -1) {
752 if (cur[macroSlice].idx() > optIdx(optionals_))
753 --cur[macroSlice].idx();
754 else if (cur[macroSlice].idx() == optIdx(optionals_)) {
755 cur.cutOff(macroSlice);
756 cur[macroSlice].idx() = optIdx(optionals_);
757 cur[macroSlice].pos() = 0;
761 // fix macro instances
763 dit.leaveInset(*this);
764 // TODO: this was dit.forwardPosNoDescend before. Check that this is the same
765 dit.top().forwardPos();
766 fixMacroInstancesOptional(dit, name(), optionals_);
773 void MathMacroTemplate::doDispatch(Cursor & cur, FuncRequest & cmd)
775 string const arg = to_utf8(cmd.argument());
776 switch (cmd.action) {
778 case LFUN_MATH_MACRO_ADD_PARAM:
780 cur.recordUndoFullDocument();
781 size_t pos = numargs_;
783 pos = (size_t)convert<int>(arg) - 1; // it is checked for >=0 in getStatus
784 insertParameter(cur, pos);
789 case LFUN_MATH_MACRO_REMOVE_PARAM:
791 cur.recordUndoFullDocument();
792 size_t pos = numargs_ - 1;
794 pos = (size_t)convert<int>(arg) - 1; // it is checked for >=0 in getStatus
795 removeParameter(cur, pos);
799 case LFUN_MATH_MACRO_APPEND_GREEDY_PARAM:
801 cur.recordUndoFullDocument();
802 insertParameter(cur, numargs_, true);
806 case LFUN_MATH_MACRO_REMOVE_GREEDY_PARAM:
808 cur.recordUndoFullDocument();
809 removeParameter(cur, numargs_ - 1, true);
813 case LFUN_MATH_MACRO_MAKE_OPTIONAL:
814 cur.recordUndoFullDocument();
818 case LFUN_MATH_MACRO_MAKE_NONOPTIONAL:
819 cur.recordUndoFullDocument();
820 makeNonOptional(cur);
823 case LFUN_MATH_MACRO_ADD_OPTIONAL_PARAM:
825 cur.recordUndoFullDocument();
826 insertParameter(cur, optionals_);
831 case LFUN_MATH_MACRO_REMOVE_OPTIONAL_PARAM:
832 if (optionals_ > 0) {
833 cur.recordUndoFullDocument();
834 removeParameter(cur, optionals_ - 1);
837 case LFUN_MATH_MACRO_ADD_GREEDY_OPTIONAL_PARAM:
838 if (numargs_ == optionals_) {
839 cur.recordUndoFullDocument();
840 insertParameter(cur, 0, true);
846 InsetMathNest::doDispatch(cur, cmd);
852 bool MathMacroTemplate::getStatus(Cursor & /*cur*/, FuncRequest const & cmd,
853 FuncStatus & flag) const
856 string const arg = to_utf8(cmd.argument());
857 switch (cmd.action) {
858 case LFUN_MATH_MACRO_ADD_PARAM: {
859 int num = numargs_ + 1;
861 num = convert<int>(arg);
862 bool on = (num >= optionals_
863 && numargs_ < 9 && num <= numargs_ + 1);
868 case LFUN_MATH_MACRO_APPEND_GREEDY_PARAM:
869 flag.enabled(numargs_ < 9);
872 case LFUN_MATH_MACRO_REMOVE_PARAM: {
875 num = convert<int>(arg);
876 flag.enabled(num >= 1 && num <= numargs_);
880 case LFUN_MATH_MACRO_MAKE_OPTIONAL:
881 flag.enabled(numargs_ > 0
882 && optionals_ < numargs_
883 && type_ != MacroTypeDef);
886 case LFUN_MATH_MACRO_MAKE_NONOPTIONAL:
887 flag.enabled(optionals_ > 0
888 && type_ != MacroTypeDef);
891 case LFUN_MATH_MACRO_ADD_OPTIONAL_PARAM:
892 flag.enabled(numargs_ < 9);
895 case LFUN_MATH_MACRO_REMOVE_OPTIONAL_PARAM:
896 flag.enabled(optionals_ > 0);
899 case LFUN_MATH_MACRO_ADD_GREEDY_OPTIONAL_PARAM:
900 flag.enabled(numargs_ == 0
901 && type_ != MacroTypeDef);
904 case LFUN_IN_MATHMACROTEMPLATE:
916 void MathMacroTemplate::read(Buffer const &, Lexer & lex)
919 mathed_parse_cell(ar, lex.getStream());
920 if (ar.size() != 1 || !ar[0]->asMacroTemplate()) {
921 lyxerr << "Cannot read macro from '" << ar << "'" << endl;
922 lyxerr << "Read: " << to_utf8(asString(ar)) << endl;
925 operator=( *(ar[0]->asMacroTemplate()) );
931 void MathMacroTemplate::write(Buffer const &, ostream & os) const
933 odocstringstream oss;
934 WriteStream wi(oss, false, false);
935 oss << "FormulaMacro\n";
937 os << to_utf8(oss.str());
941 void MathMacroTemplate::write(WriteStream & os) const
947 void MathMacroTemplate::write(WriteStream & os, bool overwriteRedefinition) const
949 if (type_ == MacroTypeDef) {
950 os << "\\def\\" << name().c_str();
951 for (int i = 1; i <= numargs_; ++i)
954 // newcommand or renewcommand
955 if (redefinition_ && !overwriteRedefinition)
956 os << "\\renewcommand";
958 os << "\\newcommand";
959 os << "{\\" << name().c_str() << '}';
961 os << '[' << numargs_ << ']';
965 // in latex only one optional possible, simulate the others
966 if (optionals_ >= 1) {
967 docstring optValue = asString(cell(optIdx(0)));
968 if (optValue.find(']') != docstring::npos)
969 os << "[{" << cell(optIdx(0)) << "}]";
971 os << "[" << cell(optIdx(0)) << "]";
974 // in lyx we handle all optionals as real optionals
975 for (int i = 0; i < optionals_; ++i) {
976 docstring optValue = asString(cell(optIdx(i)));
977 if (optValue.find(']') != docstring::npos)
978 os << "[{" << cell(optIdx(i)) << "}]";
980 os << "[" << cell(optIdx(i)) << "]";
985 os << "{" << cell(defIdx()) << "}";
988 // writing .tex. done.
991 // writing .lyx, write special .tex export only if necessary
992 if (!cell(displayIdx()).empty())
993 os << "\n{" << cell(displayIdx()) << '}';
998 int MathMacroTemplate::plaintext(Buffer const & buf, odocstream & os,
999 OutputParams const &) const
1001 static docstring const str = '[' + buf.B_("math macro") + ']';
1008 bool MathMacroTemplate::validName() const
1010 docstring n = name();
1016 // converting back and force doesn't swallow anything?
1019 if (asString(ma) != n)
1022 // valid characters?
1023 for (size_t i = 0; i < n.size(); ++i) {
1024 if (!(n[i] >= 'a' && n[i] <= 'z') &&
1025 !(n[i] >= 'A' && n[i] <= 'Z'))
1033 bool MathMacroTemplate::validMacro() const
1039 bool MathMacroTemplate::fixNameAndCheckIfValid()
1041 // check all the characters/insets in the name cell
1043 MathData & data = cell(0);
1044 while (i < data.size()) {
1045 InsetMathChar const * cinset = data[i]->asCharInset();
1047 // valid character in [a-zA-Z]?
1048 char_type c = cinset->getChar();
1049 if ((c >= 'a' && c <= 'z')
1050 || (c >= 'A' && c <= 'Z')) {
1060 // now it should be valid if anything in the name survived
1061 return data.size() > 0;
1065 void MathMacroTemplate::getDefaults(vector<docstring> & defaults) const
1067 defaults.resize(numargs_);
1068 for (int i = 0; i < optionals_; ++i)
1069 defaults[i] = asString(cell(optIdx(i)));
1073 docstring MathMacroTemplate::definition() const
1075 return asString(cell(defIdx()));
1079 docstring MathMacroTemplate::displayDefinition() const
1081 return asString(cell(displayIdx()));
1085 size_t MathMacroTemplate::numArgs() const
1091 size_t MathMacroTemplate::numOptionals() const
1097 void MathMacroTemplate::infoize(odocstream & os) const
1099 os << "Math Macro: \\" << name();