3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Alejandro Aguilar Sierra
8 * \author Stefan Schimanski
10 * Full author contact details are available in file CREDITS.
15 #include "MathMacro.h"
17 #include "InsetMathChar.h"
18 #include "MathCompletionList.h"
19 #include "MathExtern.h"
20 #include "MathFactory.h"
21 #include "MathStream.h"
22 #include "MathSupport.h"
25 #include "BufferView.h"
26 #include "CoordCache.h"
28 #include "FuncStatus.h"
29 #include "FuncRequest.h"
30 #include "LaTeXFeatures.h"
34 #include "frontends/Painter.h"
36 #include "support/debug.h"
37 #include "support/gettext.h"
38 #include "support/lassert.h"
39 #include "support/lstrings.h"
40 #include "support/textutils.h"
45 using namespace lyx::support;
51 /// A proxy for the macro values
52 class ArgumentProxy : public InsetMath {
55 ArgumentProxy(MathMacro & mathMacro, size_t idx)
56 : mathMacro_(mathMacro), idx_(idx) {}
58 ArgumentProxy(MathMacro & mathMacro, size_t idx, docstring const & def)
59 : mathMacro_(mathMacro), idx_(idx)
64 InsetCode lyxCode() const { return ARGUMENT_PROXY_CODE; }
66 void metrics(MetricsInfo & mi, Dimension & dim) const {
67 mathMacro_.macro()->unlock();
68 mathMacro_.cell(idx_).metrics(mi, dim);
70 if (!mathMacro_.editMetrics(mi.base.bv)
71 && mathMacro_.cell(idx_).empty())
72 def_.metrics(mi, dim);
74 mathMacro_.macro()->lock();
76 // write(), normalize(), infoize() and infoize2() are not needed since
77 // MathMacro uses the definition and not the expanded cells.
79 void maple(MapleStream & ms) const { ms << mathMacro_.cell(idx_); }
81 void maxima(MaximaStream & ms) const { ms << mathMacro_.cell(idx_); }
83 void mathematica(MathematicaStream & ms) const { ms << mathMacro_.cell(idx_); }
85 void mathmlize(MathStream & ms) const { ms << mathMacro_.cell(idx_); }
87 void htmlize(HtmlStream & ms) const { ms << mathMacro_.cell(idx_); }
89 void octave(OctaveStream & os) const { os << mathMacro_.cell(idx_); }
91 void draw(PainterInfo & pi, int x, int y) const {
92 if (mathMacro_.editMetrics(pi.base.bv)) {
93 // The only way a ArgumentProxy can appear is in a cell of the
94 // MathMacro. Moreover the cells are only drawn in the DISPLAY_FOLDED
95 // mode and then, if the macro is edited the monochrome
96 // mode is entered by the MathMacro before calling the cells' draw
97 // method. Then eventually this code is reached and the proxy leaves
98 // monochrome mode temporarely. Hence, if it is not in monochrome
99 // here (and the assert triggers in pain.leaveMonochromeMode())
101 pi.pain.leaveMonochromeMode();
102 mathMacro_.cell(idx_).draw(pi, x, y);
103 pi.pain.enterMonochromeMode(Color_mathbg, Color_mathmacroblend);
104 } else if (mathMacro_.cell(idx_).empty()) {
105 mathMacro_.cell(idx_).setXY(*pi.base.bv, x, y);
108 mathMacro_.cell(idx_).draw(pi, x, y);
111 size_t idx() const { return idx_; }
113 int kerning(BufferView const * bv) const
115 if (mathMacro_.editMetrics(bv)
116 || !mathMacro_.cell(idx_).empty())
117 return mathMacro_.cell(idx_).kerning(bv);
119 return def_.kerning(bv);
124 Inset * clone() const
126 return new ArgumentProxy(*this);
129 MathMacro & mathMacro_;
137 class MathMacro::Private {
139 Private(Buffer * buf, docstring const & name)
140 : name_(name), displayMode_(DISPLAY_INIT),
141 expanded_(buf), definition_(buf), attachedArgsNum_(0),
142 optionals_(0), nextFoldMode_(true), macroBackup_(buf),
143 macro_(0), needsUpdate_(false), isUpdating_(false),
149 /// current display mode
150 DisplayMode displayMode_;
151 /// expanded macro with ArgumentProxies
153 /// macro definition with #1,#2,.. insets
154 MathData definition_;
155 /// number of arguments that were really attached
156 size_t attachedArgsNum_;
157 /// optional argument attached? (only in DISPLAY_NORMAL mode)
159 /// fold mode to be set in next metrics call?
161 /// if macro_ == true, then here is a copy of the macro
162 /// don't use it for locking
163 MacroData macroBackup_;
164 /// if macroNotFound_ == false, then here is a reference to the macro
165 /// this might invalidate after metrics was called
166 MacroData const * macro_;
168 mutable std::map<BufferView const *, bool> editing_;
170 std::string requires_;
171 /// update macro representation
175 /// maximal number of arguments the macro is greedy for
180 MathMacro::MathMacro(Buffer * buf, docstring const & name)
181 : InsetMathNest(buf, 0), d(new Private(buf, name))
185 MathMacro::MathMacro(MathMacro const & that)
186 : InsetMathNest(that), d(new Private(*that.d))
191 MathMacro & MathMacro::operator=(MathMacro const & that)
195 InsetMathNest::operator=(that);
201 MathMacro::~MathMacro()
207 Inset * MathMacro::clone() const
209 MathMacro * copy = new MathMacro(*this);
210 copy->d->needsUpdate_ = true;
211 //copy->d->expanded_.clear();
216 void MathMacro::normalize(NormalStream & os) const
218 os << "[macro " << name();
219 for (size_t i = 0; i < nargs(); ++i)
220 os << ' ' << cell(i);
225 MathMacro::DisplayMode MathMacro::displayMode() const
227 return d->displayMode_;
231 bool MathMacro::extraBraces() const
233 return d->displayMode_ == DISPLAY_NORMAL && arity() > 0;
237 docstring MathMacro::name() const
239 if (d->displayMode_ == DISPLAY_UNFOLDED)
240 return asString(cell(0));
246 docstring MathMacro::macroName() const
252 void MathMacro::cursorPos(BufferView const & bv,
253 CursorSlice const & sl, bool boundary, int & x, int & y) const
255 // We may have 0 arguments, but InsetMathNest requires at least one.
257 InsetMathNest::cursorPos(bv, sl, boundary, x, y);
261 bool MathMacro::editMode(BufferView const * bv) const {
262 // find this in cursor trace
263 Cursor const & cur = bv->cursor();
264 for (size_t i = 0; i != cur.depth(); ++i)
265 if (&cur[i].inset() == this) {
266 // look if there is no other macro in edit mode above
268 for (; i != cur.depth(); ++i) {
269 InsetMath * im = cur[i].asInsetMath();
271 MathMacro const * macro = im->asMacro();
272 if (macro && macro->displayMode() == DISPLAY_NORMAL)
277 // ok, none found, I am the highest one
285 MacroData const * MathMacro::macro()
291 bool MathMacro::editMetrics(BufferView const * bv) const
293 return d->editing_[bv];
297 void MathMacro::metrics(MetricsInfo & mi, Dimension & dim) const
299 // set edit mode for which we will have calculated metrics. But only
300 d->editing_[mi.base.bv] = editMode(mi.base.bv);
302 // calculate new metrics according to display mode
303 if (d->displayMode_ == DISPLAY_INIT ||
304 d->displayMode_ == DISPLAY_INTERACTIVE_INIT) {
305 mathed_string_dim(mi.base.font, from_ascii("\\") + name(), dim);
306 } else if (d->displayMode_ == DISPLAY_UNFOLDED) {
307 cell(0).metrics(mi, dim);
309 mathed_string_dim(mi.base.font, from_ascii("\\"), bsdim);
310 dim.wid += bsdim.width() + 1;
311 dim.asc = max(bsdim.ascent(), dim.ascent());
312 dim.des = max(bsdim.descent(), dim.descent());
314 } else if (lyxrc.macro_edit_style == LyXRC::MACRO_EDIT_LIST
315 && d->editing_[mi.base.bv]) {
316 // Macro will be edited in a old-style list mode here:
320 FontInfo labelFont = sane_font;
321 math_font_max_dim(labelFont, fontDim.asc, fontDim.des);
323 // get dimension of components of list view
325 nameDim.wid = mathed_string_width(mi.base.font, from_ascii("Macro \\") + name() + ": ");
326 nameDim.asc = fontDim.asc;
327 nameDim.des = fontDim.des;
330 argDim.wid = mathed_string_width(labelFont, from_ascii("#9: "));
331 argDim.asc = fontDim.asc;
332 argDim.des = fontDim.des;
335 d->definition_.metrics(mi, defDim);
338 dim.wid = nameDim.wid + defDim.wid;
339 dim.asc = max(nameDim.asc, defDim.asc);
340 dim.des = max(nameDim.des, defDim.des);
342 for (idx_type i = 0; i < nargs(); ++i) {
344 cell(i).metrics(mi, cdim);
345 dim.des += max(argDim.height(), cdim.height()) + 1;
346 dim.wid = max(dim.wid, argDim.wid + cdim.wid);
349 // make space for box and markers, 2 pixels
353 metricsMarkers2(dim);
357 // calculate metrics, hoping that all cells are seen
359 d->expanded_.metrics(mi, dim);
361 // otherwise do a manual metrics call
362 CoordCache & coords = mi.base.bv->coordCache();
363 for (idx_type i = 0; i < nargs(); ++i) {
364 if (!coords.getArrays().hasDim(&cell(i))) {
366 cell(i).metrics(mi, tdim);
371 // calculate dimension with label while editing
372 if (lyxrc.macro_edit_style == LyXRC::MACRO_EDIT_INLINE_BOX
373 && d->editing_[mi.base.bv]) {
374 FontInfo font = mi.base.font;
375 augmentFont(font, from_ascii("lyxtex"));
377 mathed_string_dim(font, name(), namedim);
379 dim.wid += 2 + namedim.wid + 2 + 2;
380 dim.asc = max(dim.asc, namedim.asc) + 2;
381 dim.des = max(dim.des, namedim.des) + 2;
383 dim.wid = max(1 + namedim.wid + 1, 2 + dim.wid + 2);
384 dim.asc += 1 + namedim.height() + 1;
391 int MathMacro::kerning(BufferView const * bv) const {
392 if (d->displayMode_ == DISPLAY_NORMAL && !d->editing_[bv])
393 return d->expanded_.kerning(bv);
399 void MathMacro::updateMacro(MacroContext const & mc)
402 d->macro_ = mc.get(name());
403 if (d->macro_ && d->macroBackup_ != *d->macro_) {
404 d->macroBackup_ = *d->macro_;
405 d->needsUpdate_ = true;
413 class MathMacro::UpdateLocker
416 explicit UpdateLocker(MathMacro & mm) : mac(mm)
418 mac.d->isUpdating_ = true;
420 ~UpdateLocker() { mac.d->isUpdating_ = false; }
424 /** Avoid wrong usage of UpdateLocker.
425 To avoid wrong usage:
426 UpdateLocker(...); // wrong
427 UpdateLocker locker(...); // right
429 #define UpdateLocker(x) unnamed_UpdateLocker;
430 // Tip gotten from Bobby Schmidt's column in C/C++ Users Journal
433 void MathMacro::updateRepresentation(Cursor * cur, MacroContext const & mc,
436 // block recursive calls (bug 8999)
440 UpdateLocker locker(*this);
447 d->requires_ = d->macro_->requires();
450 // non-normal mode? We are done!
451 || (d->displayMode_ != DISPLAY_NORMAL))
454 d->needsUpdate_ = false;
456 // get default values of macro
457 vector<docstring> const & defaults = d->macro_->defaults();
459 // create MathMacroArgumentValue objects pointing to the cells of the macro
460 vector<MathData> values(nargs());
461 for (size_t i = 0; i < nargs(); ++i) {
462 ArgumentProxy * proxy;
463 if (i < defaults.size())
464 proxy = new ArgumentProxy(*this, i, defaults[i]);
466 proxy = new ArgumentProxy(*this, i);
467 values[i].insert(0, MathAtom(proxy));
469 // expanding macro with the values
470 // Only update the argument macros if anything was expanded, otherwise
471 // we would get an endless loop (bug 9140). UpdateLocker does not work
472 // in this case, since MacroData::expand() creates new MathMacro
473 // objects, so this would be a different recursion path than the one
474 // protected by UpdateLocker.
475 if (d->macro_->expand(values, d->expanded_)) {
476 if (utype == OutputUpdate && !d->expanded_.empty())
477 d->expanded_.updateMacros(cur, mc, utype);
479 // get definition for list edit mode
480 docstring const & display = d->macro_->display();
481 asArray(display.empty() ? d->macro_->definition() : display, d->definition_);
485 void MathMacro::draw(PainterInfo & pi, int x, int y) const
487 Dimension const dim = dimension(*pi.base.bv);
489 setPosCache(pi, x, y);
493 if (d->displayMode_ == DISPLAY_INIT || d->displayMode_ == DISPLAY_INTERACTIVE_INIT) {
494 FontSetChanger dummy(pi.base, "lyxtex");
495 pi.pain.text(x, y, from_ascii("\\") + name(), pi.base.font);
496 } else if (d->displayMode_ == DISPLAY_UNFOLDED) {
497 FontSetChanger dummy(pi.base, "lyxtex");
498 pi.pain.text(x, y, from_ascii("\\"), pi.base.font);
499 x += mathed_string_width(pi.base.font, from_ascii("\\")) + 1;
500 cell(0).draw(pi, x, y);
501 drawMarkers(pi, expx, expy);
502 } else if (lyxrc.macro_edit_style == LyXRC::MACRO_EDIT_LIST
503 && d->editing_[pi.base.bv]) {
504 // Macro will be edited in a old-style list mode here:
506 CoordCache const & coords = pi.base.bv->coordCache();
507 FontInfo const & labelFont = sane_font;
509 // markers and box needs two pixels
512 // get maximal font height
514 math_font_max_dim(pi.base.font, fontDim.asc, fontDim.des);
517 docstring label = from_ascii("Macro \\") + name() + from_ascii(": ");
518 pi.pain.text(x, y, label, labelFont);
519 x += mathed_string_width(labelFont, label);
522 d->definition_.draw(pi, x, y);
523 Dimension const & defDim = coords.getArrays().dim(&d->definition_);
524 y += max(fontDim.des, defDim.des);
527 docstring str = from_ascii("#9");
528 int strw1 = mathed_string_width(labelFont, from_ascii("#9"));
529 int strw2 = mathed_string_width(labelFont, from_ascii(": "));
531 for (idx_type i = 0; i < nargs(); ++i) {
533 Dimension const & cdim = coords.getArrays().dim(&cell(i));
535 y += max(fontDim.asc, cdim.asc) + 1;
539 pi.pain.text(x, y, str, labelFont);
541 pi.pain.text(x, y, from_ascii(":"), labelFont);
545 cell(i).draw(pi, x, y);
548 y += max(fontDim.des, cdim.des);
551 pi.pain.rectangle(expx + 1, expy - dim.asc + 1, dim.wid - 3,
552 dim.height() - 2, Color_mathmacroframe);
553 drawMarkers2(pi, expx, expy);
555 bool drawBox = lyxrc.macro_edit_style == LyXRC::MACRO_EDIT_INLINE_BOX;
558 for (size_t i = 0; i < nargs(); ++i)
559 cell(i).setXY(*pi.base.bv, x, y);
561 if (drawBox && d->editing_[pi.base.bv]) {
562 // draw header and rectangle around
563 FontInfo font = pi.base.font;
564 augmentFont(font, from_ascii("lyxtex"));
565 font.setSize(FONT_SIZE_TINY);
566 font.setColor(Color_mathmacrolabel);
568 mathed_string_dim(font, name(), namedim);
570 pi.pain.fillRectangle(x, y - dim.asc, dim.wid, 1 + namedim.height() + 1, Color_mathmacrobg);
571 pi.pain.text(x + 1, y - dim.asc + namedim.asc + 2, name(), font);
572 expx += (dim.wid - d->expanded_.dimension(*pi.base.bv).width()) / 2;
575 if (d->editing_[pi.base.bv]) {
576 pi.pain.enterMonochromeMode(Color_mathbg, Color_mathmacroblend);
577 d->expanded_.draw(pi, expx, expy);
578 pi.pain.leaveMonochromeMode();
581 pi.pain.rectangle(x, y - dim.asc, dim.wid,
582 dim.height(), Color_mathmacroframe);
584 d->expanded_.draw(pi, expx, expy);
587 drawMarkers(pi, x, y);
590 // edit mode changed?
591 if (d->editing_[pi.base.bv] != editMode(pi.base.bv))
592 pi.base.bv->cursor().screenUpdateFlags(Update::SinglePar);
596 void MathMacro::drawSelection(PainterInfo & pi, int x, int y) const
598 // We may have 0 arguments, but InsetMathNest requires at least one.
600 InsetMathNest::drawSelection(pi, x, y);
604 void MathMacro::setDisplayMode(MathMacro::DisplayMode mode, int appetite)
606 if (d->displayMode_ != mode) {
607 // transfer name if changing from or to DISPLAY_UNFOLDED
608 if (mode == DISPLAY_UNFOLDED) {
610 asArray(d->name_, cell(0));
611 } else if (d->displayMode_ == DISPLAY_UNFOLDED) {
612 d->name_ = asString(cell(0));
616 d->displayMode_ = mode;
617 d->needsUpdate_ = true;
620 // the interactive init mode is non-greedy by default
622 d->appetite_ = (mode == DISPLAY_INTERACTIVE_INIT) ? 0 : 9;
624 d->appetite_ = size_t(appetite);
628 MathMacro::DisplayMode MathMacro::computeDisplayMode() const
630 if (d->nextFoldMode_ == true && d->macro_ && !d->macro_->locked())
631 return DISPLAY_NORMAL;
633 return DISPLAY_UNFOLDED;
637 bool MathMacro::validName() const
639 docstring n = name();
644 // converting back and force doesn't swallow anything?
647 if (asString(ma) != n)
651 for (size_t i = 0; i<n.size(); ++i) {
652 if (!(n[i] >= 'a' && n[i] <= 'z')
653 && !(n[i] >= 'A' && n[i] <= 'Z')
662 size_t MathMacro::arity() const
664 if (d->displayMode_ == DISPLAY_NORMAL )
665 return cells_.size();
671 size_t MathMacro::optionals() const
673 return d->optionals_;
677 void MathMacro::setOptionals(int n)
679 if (n <= int(nargs()))
684 size_t MathMacro::appetite() const
690 void MathMacro::validate(LaTeXFeatures & features) const
692 if (!d->requires_.empty())
693 features.require(d->requires_);
695 if (name() == "binom")
696 features.require("binom");
698 // validate the cells and the definition
699 if (displayMode() == DISPLAY_NORMAL) {
700 d->definition_.validate(features);
701 InsetMathNest::validate(features);
706 void MathMacro::edit(Cursor & cur, bool front, EntryDirection entry_from)
708 cur.screenUpdateFlags(Update::SinglePar);
709 InsetMathNest::edit(cur, front, entry_from);
713 Inset * MathMacro::editXY(Cursor & cur, int x, int y)
715 // We may have 0 arguments, but InsetMathNest requires at least one.
717 cur.screenUpdateFlags(Update::SinglePar);
718 return InsetMathNest::editXY(cur, x, y);
724 void MathMacro::removeArgument(Inset::pos_type pos) {
725 if (d->displayMode_ == DISPLAY_NORMAL) {
726 LASSERT(size_t(pos) < cells_.size(), return);
727 cells_.erase(cells_.begin() + pos);
728 if (size_t(pos) < d->attachedArgsNum_)
729 --d->attachedArgsNum_;
730 if (size_t(pos) < d->optionals_) {
734 d->needsUpdate_ = true;
739 void MathMacro::insertArgument(Inset::pos_type pos) {
740 if (d->displayMode_ == DISPLAY_NORMAL) {
741 LASSERT(size_t(pos) <= cells_.size(), return);
742 cells_.insert(cells_.begin() + pos, MathData());
743 if (size_t(pos) < d->attachedArgsNum_)
744 ++d->attachedArgsNum_;
745 if (size_t(pos) < d->optionals_)
748 d->needsUpdate_ = true;
753 void MathMacro::detachArguments(vector<MathData> & args, bool strip)
755 LASSERT(d->displayMode_ == DISPLAY_NORMAL, return);
758 // strip off empty cells, but not more than arity-attachedArgsNum_
761 for (i = cells_.size(); i > d->attachedArgsNum_; --i)
762 if (!cell(i - 1).empty()) break;
766 d->attachedArgsNum_ = 0;
767 d->expanded_ = MathData();
770 d->needsUpdate_ = true;
774 void MathMacro::attachArguments(vector<MathData> const & args, size_t arity, int optionals)
776 LASSERT(d->displayMode_ == DISPLAY_NORMAL, return);
778 d->attachedArgsNum_ = args.size();
779 cells_.resize(arity);
780 d->expanded_ = MathData();
781 d->optionals_ = optionals;
783 d->needsUpdate_ = true;
787 bool MathMacro::idxFirst(Cursor & cur) const
789 cur.screenUpdateFlags(Update::SinglePar);
790 return InsetMathNest::idxFirst(cur);
794 bool MathMacro::idxLast(Cursor & cur) const
796 cur.screenUpdateFlags(Update::SinglePar);
797 return InsetMathNest::idxLast(cur);
801 bool MathMacro::notifyCursorLeaves(Cursor const & old, Cursor & cur)
803 if (d->displayMode_ == DISPLAY_UNFOLDED) {
804 docstring const & unfolded_name = name();
805 if (unfolded_name != d->name_) {
806 // The macro name was changed
807 Cursor inset_cursor = old;
808 int macroSlice = inset_cursor.find(this);
809 // returning true means the cursor is "now" invalid,
811 LASSERT(macroSlice != -1, return true);
812 inset_cursor.cutOff(macroSlice);
813 inset_cursor.recordUndoInset();
815 inset_cursor.cell().erase(inset_cursor.pos());
816 inset_cursor.cell().insert(inset_cursor.pos(),
817 createInsetMath(unfolded_name, cur.buffer()));
819 cur.screenUpdateFlags(cur.result().screenUpdate() | Update::SinglePar);
823 cur.screenUpdateFlags(Update::Force);
824 return InsetMathNest::notifyCursorLeaves(old, cur);
828 void MathMacro::fold(Cursor & cur)
830 if (!d->nextFoldMode_) {
831 d->nextFoldMode_ = true;
832 cur.screenUpdateFlags(Update::SinglePar);
837 void MathMacro::unfold(Cursor & cur)
839 if (d->nextFoldMode_) {
840 d->nextFoldMode_ = false;
841 cur.screenUpdateFlags(Update::SinglePar);
846 bool MathMacro::folded() const
848 return d->nextFoldMode_;
852 void MathMacro::write(WriteStream & os) const
854 MathEnsurer ensurer(os, d->macro_ != 0, true);
857 if (d->displayMode_ != DISPLAY_NORMAL) {
858 os << "\\" << name();
859 if (name().size() != 1 || isAlphaASCII(name()[0]))
860 os.pendingSpace(true);
865 // we should be ok to continue even if this fails.
868 // Always protect macros in a fragile environment
872 os << "\\" << name();
875 // Optional arguments:
876 // First find last non-empty optional argument
877 idx_type emptyOptFrom = 0;
879 for (; i < cells_.size() && i < d->optionals_; ++i) {
880 if (!cell(i).empty())
881 emptyOptFrom = i + 1;
884 // print out optionals
885 for (i=0; i < cells_.size() && i < emptyOptFrom; ++i) {
887 os << "[" << cell(i) << "]";
890 // skip the tailing empty optionals
893 // Print remaining arguments
894 for (; i < cells_.size(); ++i) {
895 if (cell(i).size() == 1
896 && cell(i)[0].nucleus()->asCharInset()
897 && isASCII(cell(i)[0].nucleus()->asCharInset()->getChar())) {
902 os << "{" << cell(i) << "}";
906 // add space if there was no argument
908 os.pendingSpace(true);
912 void MathMacro::maple(MapleStream & os) const
914 lyx::maple(d->expanded_, os);
918 void MathMacro::maxima(MaximaStream & os) const
920 lyx::maxima(d->expanded_, os);
924 void MathMacro::mathematica(MathematicaStream & os) const
926 lyx::mathematica(d->expanded_, os);
930 void MathMacro::mathmlize(MathStream & os) const
932 // macro_ is 0 if this is an unknown macro
933 LATTEST(d->macro_ || d->displayMode_ != DISPLAY_NORMAL);
935 docstring const xmlname = d->macro_->xmlname();
936 if (!xmlname.empty()) {
937 char const * type = d->macro_->MathMLtype();
938 os << '<' << type << "> " << xmlname << " /<"
943 if (d->expanded_.empty()) {
944 // this means that we do not recognize the macro
945 throw MathExportException();
951 void MathMacro::htmlize(HtmlStream & os) const
953 // macro_ is 0 if this is an unknown macro
954 LATTEST(d->macro_ || d->displayMode_ != DISPLAY_NORMAL);
956 docstring const xmlname = d->macro_->xmlname();
957 if (!xmlname.empty()) {
958 os << ' ' << xmlname << ' ';
962 if (d->expanded_.empty()) {
963 // this means that we do not recognize the macro
964 throw MathExportException();
970 void MathMacro::octave(OctaveStream & os) const
972 lyx::octave(d->expanded_, os);
976 void MathMacro::infoize(odocstream & os) const
978 os << bformat(_("Macro: %1$s"), name());
982 void MathMacro::infoize2(odocstream & os) const
984 os << bformat(_("Macro: %1$s"), name());
988 bool MathMacro::completionSupported(Cursor const & cur) const
990 if (displayMode() != DISPLAY_UNFOLDED)
991 return InsetMathNest::completionSupported(cur);
993 return lyxrc.completion_popup_math
994 && displayMode() == DISPLAY_UNFOLDED
995 && cur.bv().cursor().pos() == int(name().size());
999 bool MathMacro::inlineCompletionSupported(Cursor const & cur) const
1001 if (displayMode() != DISPLAY_UNFOLDED)
1002 return InsetMathNest::inlineCompletionSupported(cur);
1004 return lyxrc.completion_inline_math
1005 && displayMode() == DISPLAY_UNFOLDED
1006 && cur.bv().cursor().pos() == int(name().size());
1010 bool MathMacro::automaticInlineCompletion() const
1012 if (displayMode() != DISPLAY_UNFOLDED)
1013 return InsetMathNest::automaticInlineCompletion();
1015 return lyxrc.completion_inline_math;
1019 bool MathMacro::automaticPopupCompletion() const
1021 if (displayMode() != DISPLAY_UNFOLDED)
1022 return InsetMathNest::automaticPopupCompletion();
1024 return lyxrc.completion_popup_math;
1028 CompletionList const *
1029 MathMacro::createCompletionList(Cursor const & cur) const
1031 if (displayMode() != DISPLAY_UNFOLDED)
1032 return InsetMathNest::createCompletionList(cur);
1034 return new MathCompletionList(cur.bv().cursor());
1038 docstring MathMacro::completionPrefix(Cursor const & cur) const
1040 if (displayMode() != DISPLAY_UNFOLDED)
1041 return InsetMathNest::completionPrefix(cur);
1043 if (!completionSupported(cur))
1046 return "\\" + name();
1050 bool MathMacro::insertCompletion(Cursor & cur, docstring const & s,
1053 if (displayMode() != DISPLAY_UNFOLDED)
1054 return InsetMathNest::insertCompletion(cur, s, finished);
1056 if (!completionSupported(cur))
1059 // append completion
1060 docstring newName = name() + s;
1061 asArray(newName, cell(0));
1062 cur.bv().cursor().pos() = name().size();
1063 cur.screenUpdateFlags(Update::SinglePar);
1067 cur.bv().cursor().pop();
1068 ++cur.bv().cursor().pos();
1069 cur.screenUpdateFlags(Update::SinglePar);
1076 void MathMacro::completionPosAndDim(Cursor const & cur, int & x, int & y,
1077 Dimension & dim) const
1079 if (displayMode() != DISPLAY_UNFOLDED)
1080 InsetMathNest::completionPosAndDim(cur, x, y, dim);
1082 // get inset dimensions
1083 dim = cur.bv().coordCache().insets().dim(this);
1084 // FIXME: these 3 are no accurate, but should depend on the font.
1085 // Now the popup jumps down if you enter a char with descent > 0.
1091 = cur.bv().coordCache().insets().xy(this);