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 MathMacro::MathMacro(Buffer * buf, docstring const & name)
138 : InsetMathNest(buf, 0), name_(name), displayMode_(DISPLAY_INIT),
139 expanded_(buf), attachedArgsNum_(0), optionals_(0), nextFoldMode_(true),
140 macroBackup_(buf), macro_(0), needsUpdate_(false),
141 isUpdating_(false), appetite_(9)
145 Inset * MathMacro::clone() const
147 MathMacro * copy = new MathMacro(*this);
148 copy->needsUpdate_ = true;
149 //copy->expanded_.cell(0).clear();
154 void MathMacro::normalize(NormalStream & os) const
156 os << "[macro " << name();
157 for (size_t i = 0; i < nargs(); ++i)
158 os << ' ' << cell(i);
163 docstring MathMacro::name() const
165 if (displayMode_ == DISPLAY_UNFOLDED)
166 return asString(cell(0));
172 void MathMacro::cursorPos(BufferView const & bv,
173 CursorSlice const & sl, bool boundary, int & x, int & y) const
175 // We may have 0 arguments, but InsetMathNest requires at least one.
177 InsetMathNest::cursorPos(bv, sl, boundary, x, y);
181 bool MathMacro::editMode(BufferView const * bv) const {
182 // find this in cursor trace
183 Cursor const & cur = bv->cursor();
184 for (size_t i = 0; i != cur.depth(); ++i)
185 if (&cur[i].inset() == this) {
186 // look if there is no other macro in edit mode above
188 for (; i != cur.depth(); ++i) {
189 InsetMath * im = cur[i].asInsetMath();
191 MathMacro const * macro = im->asMacro();
192 if (macro && macro->displayMode() == DISPLAY_NORMAL)
197 // ok, none found, I am the highest one
205 bool MathMacro::editMetrics(BufferView const * bv) const
211 void MathMacro::metrics(MetricsInfo & mi, Dimension & dim) const
213 // set edit mode for which we will have calculated metrics. But only
214 editing_[mi.base.bv] = editMode(mi.base.bv);
216 // calculate new metrics according to display mode
217 if (displayMode_ == DISPLAY_INIT || displayMode_ == DISPLAY_INTERACTIVE_INIT) {
218 mathed_string_dim(mi.base.font, from_ascii("\\") + name(), dim);
219 } else if (displayMode_ == DISPLAY_UNFOLDED) {
220 cell(0).metrics(mi, dim);
222 mathed_string_dim(mi.base.font, from_ascii("\\"), bsdim);
223 dim.wid += bsdim.width() + 1;
224 dim.asc = max(bsdim.ascent(), dim.ascent());
225 dim.des = max(bsdim.descent(), dim.descent());
227 } else if (lyxrc.macro_edit_style == LyXRC::MACRO_EDIT_LIST
228 && editing_[mi.base.bv]) {
229 // Macro will be edited in a old-style list mode here:
233 FontInfo labelFont = sane_font;
234 math_font_max_dim(labelFont, fontDim.asc, fontDim.des);
236 // get dimension of components of list view
238 nameDim.wid = mathed_string_width(mi.base.font, from_ascii("Macro \\") + name() + ": ");
239 nameDim.asc = fontDim.asc;
240 nameDim.des = fontDim.des;
243 argDim.wid = mathed_string_width(labelFont, from_ascii("#9: "));
244 argDim.asc = fontDim.asc;
245 argDim.des = fontDim.des;
248 definition_.metrics(mi, defDim);
251 dim.wid = nameDim.wid + defDim.wid;
252 dim.asc = max(nameDim.asc, defDim.asc);
253 dim.des = max(nameDim.des, defDim.des);
255 for (idx_type i = 0; i < nargs(); ++i) {
257 cell(i).metrics(mi, cdim);
258 dim.des += max(argDim.height(), cdim.height()) + 1;
259 dim.wid = max(dim.wid, argDim.wid + cdim.wid);
262 // make space for box and markers, 2 pixels
266 metricsMarkers2(dim);
270 // calculate metrics, hoping that all cells are seen
272 expanded_.cell(0).metrics(mi, dim);
274 // otherwise do a manual metrics call
275 CoordCache & coords = mi.base.bv->coordCache();
276 for (idx_type i = 0; i < nargs(); ++i) {
277 if (!coords.getArrays().hasDim(&cell(i))) {
279 cell(i).metrics(mi, tdim);
284 // calculate dimension with label while editing
285 if (lyxrc.macro_edit_style == LyXRC::MACRO_EDIT_INLINE_BOX
286 && editing_[mi.base.bv]) {
287 FontInfo font = mi.base.font;
288 augmentFont(font, from_ascii("lyxtex"));
290 mathed_string_dim(font, name(), namedim);
292 dim.wid += 2 + namedim.wid + 2 + 2;
293 dim.asc = max(dim.asc, namedim.asc) + 2;
294 dim.des = max(dim.des, namedim.des) + 2;
296 dim.wid = max(1 + namedim.wid + 1, 2 + dim.wid + 2);
297 dim.asc += 1 + namedim.height() + 1;
304 int MathMacro::kerning(BufferView const * bv) const {
305 if (displayMode_ == DISPLAY_NORMAL && !editing_[bv])
306 return expanded_.kerning(bv);
312 void MathMacro::updateMacro(MacroContext const & mc)
315 macro_ = mc.get(name());
316 if (macro_ && macroBackup_ != *macro_) {
317 macroBackup_ = *macro_;
326 class MathMacro::UpdateLocker
329 explicit UpdateLocker(MathMacro & mm) : mac(mm)
331 mac.isUpdating_ = true;
333 ~UpdateLocker() { mac.isUpdating_ = false; }
337 /** Avoid wrong usage of UpdateLocker.
338 To avoid wrong usage:
339 UpdateLocker(...); // wrong
340 UpdateLocker locker(...); // right
342 #define UpdateLocker(x) unnamed_UpdateLocker;
343 // Tip gotten from Bobby Schmidt's column in C/C++ Users Journal
346 void MathMacro::updateRepresentation(Cursor * cur, MacroContext const & mc,
349 // block recursive calls (bug 8999)
353 UpdateLocker locker(*this);
360 requires_ = macro_->requires();
363 // non-normal mode? We are done!
364 || (displayMode_ != DISPLAY_NORMAL))
367 needsUpdate_ = false;
369 // get default values of macro
370 vector<docstring> const & defaults = macro_->defaults();
372 // create MathMacroArgumentValue objects pointing to the cells of the macro
373 vector<MathData> values(nargs());
374 for (size_t i = 0; i < nargs(); ++i) {
375 ArgumentProxy * proxy;
376 if (i < defaults.size())
377 proxy = new ArgumentProxy(*this, i, defaults[i]);
379 proxy = new ArgumentProxy(*this, i);
380 values[i].insert(0, MathAtom(proxy));
382 // expanding macro with the values
383 // Only update the argument macros if anything was expanded, otherwise
384 // we would get an endless loop (bug 9140). UpdateLocker does not work
385 // in this case, since MacroData::expand() creates new MathMacro
386 // objects, so this would be a different recursion path than the one
387 // protected by UpdateLocker.
388 if (macro_->expand(values, expanded_.cell(0))) {
389 if (utype == OutputUpdate && !expanded_.cell(0).empty())
390 expanded_.cell(0).updateMacros(cur, mc, utype);
392 // get definition for list edit mode
393 docstring const & display = macro_->display();
394 asArray(display.empty() ? macro_->definition() : display, definition_);
398 void MathMacro::draw(PainterInfo & pi, int x, int y) const
400 Dimension const dim = dimension(*pi.base.bv);
402 setPosCache(pi, x, y);
406 if (displayMode_ == DISPLAY_INIT || displayMode_ == DISPLAY_INTERACTIVE_INIT) {
407 FontSetChanger dummy(pi.base, "lyxtex");
408 pi.pain.text(x, y, from_ascii("\\") + name(), pi.base.font);
409 } else if (displayMode_ == DISPLAY_UNFOLDED) {
410 FontSetChanger dummy(pi.base, "lyxtex");
411 pi.pain.text(x, y, from_ascii("\\"), pi.base.font);
412 x += mathed_string_width(pi.base.font, from_ascii("\\")) + 1;
413 cell(0).draw(pi, x, y);
414 drawMarkers(pi, expx, expy);
415 } else if (lyxrc.macro_edit_style == LyXRC::MACRO_EDIT_LIST
416 && editing_[pi.base.bv]) {
417 // Macro will be edited in a old-style list mode here:
419 CoordCache const & coords = pi.base.bv->coordCache();
420 FontInfo const & labelFont = sane_font;
422 // markers and box needs two pixels
425 // get maximal font height
427 math_font_max_dim(pi.base.font, fontDim.asc, fontDim.des);
430 docstring label = from_ascii("Macro \\") + name() + from_ascii(": ");
431 pi.pain.text(x, y, label, labelFont);
432 x += mathed_string_width(labelFont, label);
435 definition_.draw(pi, x, y);
436 Dimension const & defDim = coords.getArrays().dim(&definition_);
437 y += max(fontDim.des, defDim.des);
440 docstring str = from_ascii("#9");
441 int strw1 = mathed_string_width(labelFont, from_ascii("#9"));
442 int strw2 = mathed_string_width(labelFont, from_ascii(": "));
444 for (idx_type i = 0; i < nargs(); ++i) {
446 Dimension const & cdim = coords.getArrays().dim(&cell(i));
448 y += max(fontDim.asc, cdim.asc) + 1;
452 pi.pain.text(x, y, str, labelFont);
454 pi.pain.text(x, y, from_ascii(":"), labelFont);
458 cell(i).draw(pi, x, y);
461 y += max(fontDim.des, cdim.des);
464 pi.pain.rectangle(expx + 1, expy - dim.asc + 1, dim.wid - 3,
465 dim.height() - 2, Color_mathmacroframe);
466 drawMarkers2(pi, expx, expy);
468 bool drawBox = lyxrc.macro_edit_style == LyXRC::MACRO_EDIT_INLINE_BOX;
471 for (size_t i = 0; i < nargs(); ++i)
472 cell(i).setXY(*pi.base.bv, x, y);
474 if (drawBox && editing_[pi.base.bv]) {
475 // draw header and rectangle around
476 FontInfo font = pi.base.font;
477 augmentFont(font, from_ascii("lyxtex"));
478 font.setSize(FONT_SIZE_TINY);
479 font.setColor(Color_mathmacrolabel);
481 mathed_string_dim(font, name(), namedim);
483 pi.pain.fillRectangle(x, y - dim.asc, dim.wid, 1 + namedim.height() + 1, Color_mathmacrobg);
484 pi.pain.text(x + 1, y - dim.asc + namedim.asc + 2, name(), font);
485 expx += (dim.wid - expanded_.cell(0).dimension(*pi.base.bv).width()) / 2;
488 if (editing_[pi.base.bv]) {
489 pi.pain.enterMonochromeMode(Color_mathbg, Color_mathmacroblend);
490 expanded_.cell(0).draw(pi, expx, expy);
491 pi.pain.leaveMonochromeMode();
494 pi.pain.rectangle(x, y - dim.asc, dim.wid,
495 dim.height(), Color_mathmacroframe);
497 expanded_.cell(0).draw(pi, expx, expy);
500 drawMarkers(pi, x, y);
503 // edit mode changed?
504 if (editing_[pi.base.bv] != editMode(pi.base.bv))
505 pi.base.bv->cursor().screenUpdateFlags(Update::SinglePar);
509 void MathMacro::drawSelection(PainterInfo & pi, int x, int y) const
511 // We may have 0 arguments, but InsetMathNest requires at least one.
513 InsetMathNest::drawSelection(pi, x, y);
517 void MathMacro::setDisplayMode(MathMacro::DisplayMode mode, int appetite)
519 if (displayMode_ != mode) {
520 // transfer name if changing from or to DISPLAY_UNFOLDED
521 if (mode == DISPLAY_UNFOLDED) {
523 asArray(name_, cell(0));
524 } else if (displayMode_ == DISPLAY_UNFOLDED) {
525 name_ = asString(cell(0));
533 // the interactive init mode is non-greedy by default
535 appetite_ = (mode == DISPLAY_INTERACTIVE_INIT) ? 0 : 9;
537 appetite_ = size_t(appetite);
541 MathMacro::DisplayMode MathMacro::computeDisplayMode() const
543 if (nextFoldMode_ == true && macro_ && !macro_->locked())
544 return DISPLAY_NORMAL;
546 return DISPLAY_UNFOLDED;
550 bool MathMacro::validName() const
552 docstring n = name();
557 // converting back and force doesn't swallow anything?
560 if (asString(ma) != n)
564 for (size_t i = 0; i<n.size(); ++i) {
565 if (!(n[i] >= 'a' && n[i] <= 'z')
566 && !(n[i] >= 'A' && n[i] <= 'Z')
575 void MathMacro::validate(LaTeXFeatures & features) const
577 if (!requires_.empty())
578 features.require(requires_);
580 if (name() == "binom")
581 features.require("binom");
583 // validate the cells and the definition
584 if (displayMode() == DISPLAY_NORMAL) {
585 definition_.validate(features);
586 InsetMathNest::validate(features);
591 void MathMacro::edit(Cursor & cur, bool front, EntryDirection entry_from)
593 cur.screenUpdateFlags(Update::SinglePar);
594 InsetMathNest::edit(cur, front, entry_from);
598 Inset * MathMacro::editXY(Cursor & cur, int x, int y)
600 // We may have 0 arguments, but InsetMathNest requires at least one.
602 cur.screenUpdateFlags(Update::SinglePar);
603 return InsetMathNest::editXY(cur, x, y);
609 void MathMacro::removeArgument(Inset::pos_type pos) {
610 if (displayMode_ == DISPLAY_NORMAL) {
611 LASSERT(size_t(pos) < cells_.size(), return);
612 cells_.erase(cells_.begin() + pos);
613 if (size_t(pos) < attachedArgsNum_)
615 if (size_t(pos) < optionals_) {
624 void MathMacro::insertArgument(Inset::pos_type pos) {
625 if (displayMode_ == DISPLAY_NORMAL) {
626 LASSERT(size_t(pos) <= cells_.size(), return);
627 cells_.insert(cells_.begin() + pos, MathData());
628 if (size_t(pos) < attachedArgsNum_)
630 if (size_t(pos) < optionals_)
638 void MathMacro::detachArguments(vector<MathData> & args, bool strip)
640 LASSERT(displayMode_ == DISPLAY_NORMAL, return);
643 // strip off empty cells, but not more than arity-attachedArgsNum_
646 for (i = cells_.size(); i > attachedArgsNum_; --i)
647 if (!cell(i - 1).empty()) break;
651 attachedArgsNum_ = 0;
652 expanded_.cell(0) = MathData();
659 void MathMacro::attachArguments(vector<MathData> const & args, size_t arity, int optionals)
661 LASSERT(displayMode_ == DISPLAY_NORMAL, return);
663 attachedArgsNum_ = args.size();
664 cells_.resize(arity);
665 expanded_.cell(0) = MathData();
666 optionals_ = optionals;
672 bool MathMacro::idxFirst(Cursor & cur) const
674 cur.screenUpdateFlags(Update::SinglePar);
675 return InsetMathNest::idxFirst(cur);
679 bool MathMacro::idxLast(Cursor & cur) const
681 cur.screenUpdateFlags(Update::SinglePar);
682 return InsetMathNest::idxLast(cur);
686 bool MathMacro::notifyCursorLeaves(Cursor const & old, Cursor & cur)
688 if (displayMode_ == DISPLAY_UNFOLDED) {
689 docstring const & unfolded_name = name();
690 if (unfolded_name != name_) {
691 // The macro name was changed
692 Cursor inset_cursor = old;
693 int macroSlice = inset_cursor.find(this);
694 // returning true means the cursor is "now" invalid,
696 LASSERT(macroSlice != -1, return true);
697 inset_cursor.cutOff(macroSlice);
698 inset_cursor.recordUndoInset();
700 inset_cursor.cell().erase(inset_cursor.pos());
701 inset_cursor.cell().insert(inset_cursor.pos(),
702 createInsetMath(unfolded_name, cur.buffer()));
704 cur.screenUpdateFlags(cur.result().screenUpdate() | Update::SinglePar);
708 cur.screenUpdateFlags(Update::Force);
709 return InsetMathNest::notifyCursorLeaves(old, cur);
713 void MathMacro::fold(Cursor & cur)
715 if (!nextFoldMode_) {
716 nextFoldMode_ = true;
717 cur.screenUpdateFlags(Update::SinglePar);
722 void MathMacro::unfold(Cursor & cur)
725 nextFoldMode_ = false;
726 cur.screenUpdateFlags(Update::SinglePar);
731 bool MathMacro::folded() const
733 return nextFoldMode_;
737 void MathMacro::write(WriteStream & os) const
739 MathEnsurer ensurer(os, macro_ != 0, true);
742 if (displayMode_ != DISPLAY_NORMAL) {
743 os << "\\" << name();
744 if (name().size() != 1 || isAlphaASCII(name()[0]))
745 os.pendingSpace(true);
750 // we should be ok to continue even if this fails.
753 // Always protect macros in a fragile environment
757 os << "\\" << name();
760 // Optional arguments:
761 // First find last non-empty optional argument
762 idx_type emptyOptFrom = 0;
764 for (; i < cells_.size() && i < optionals_; ++i) {
765 if (!cell(i).empty())
766 emptyOptFrom = i + 1;
769 // print out optionals
770 for (i=0; i < cells_.size() && i < emptyOptFrom; ++i) {
772 os << "[" << cell(i) << "]";
775 // skip the tailing empty optionals
778 // Print remaining arguments
779 for (; i < cells_.size(); ++i) {
780 if (cell(i).size() == 1
781 && cell(i)[0].nucleus()->asCharInset()
782 && isASCII(cell(i)[0].nucleus()->asCharInset()->getChar())) {
787 os << "{" << cell(i) << "}";
791 // add space if there was no argument
793 os.pendingSpace(true);
797 void MathMacro::maple(MapleStream & os) const
799 lyx::maple(expanded_.cell(0), os);
803 void MathMacro::maxima(MaximaStream & os) const
805 lyx::maxima(expanded_.cell(0), os);
809 void MathMacro::mathematica(MathematicaStream & os) const
811 lyx::mathematica(expanded_.cell(0), os);
815 void MathMacro::mathmlize(MathStream & os) const
817 // macro_ is 0 if this is an unknown macro
818 LATTEST(macro_ || displayMode_ != DISPLAY_NORMAL);
820 docstring const xmlname = macro_->xmlname();
821 if (!xmlname.empty()) {
822 char const * type = macro_->MathMLtype();
823 os << '<' << type << "> " << xmlname << " /<"
828 MathData const & data = expanded_.cell(0);
830 // this means that we do not recognize the macro
831 throw MathExportException();
837 void MathMacro::htmlize(HtmlStream & os) const
839 // macro_ is 0 if this is an unknown macro
840 LATTEST(macro_ || displayMode_ != DISPLAY_NORMAL);
842 docstring const xmlname = macro_->xmlname();
843 if (!xmlname.empty()) {
844 os << ' ' << xmlname << ' ';
848 MathData const & data = expanded_.cell(0);
850 // this means that we do not recognize the macro
851 throw MathExportException();
857 void MathMacro::octave(OctaveStream & os) const
859 lyx::octave(expanded_.cell(0), os);
863 void MathMacro::infoize(odocstream & os) const
865 os << bformat(_("Macro: %1$s"), name());
869 void MathMacro::infoize2(odocstream & os) const
871 os << bformat(_("Macro: %1$s"), name());
875 bool MathMacro::completionSupported(Cursor const & cur) const
877 if (displayMode() != DISPLAY_UNFOLDED)
878 return InsetMathNest::completionSupported(cur);
880 return lyxrc.completion_popup_math
881 && displayMode() == DISPLAY_UNFOLDED
882 && cur.bv().cursor().pos() == int(name().size());
886 bool MathMacro::inlineCompletionSupported(Cursor const & cur) const
888 if (displayMode() != DISPLAY_UNFOLDED)
889 return InsetMathNest::inlineCompletionSupported(cur);
891 return lyxrc.completion_inline_math
892 && displayMode() == DISPLAY_UNFOLDED
893 && cur.bv().cursor().pos() == int(name().size());
897 bool MathMacro::automaticInlineCompletion() const
899 if (displayMode() != DISPLAY_UNFOLDED)
900 return InsetMathNest::automaticInlineCompletion();
902 return lyxrc.completion_inline_math;
906 bool MathMacro::automaticPopupCompletion() const
908 if (displayMode() != DISPLAY_UNFOLDED)
909 return InsetMathNest::automaticPopupCompletion();
911 return lyxrc.completion_popup_math;
915 CompletionList const *
916 MathMacro::createCompletionList(Cursor const & cur) const
918 if (displayMode() != DISPLAY_UNFOLDED)
919 return InsetMathNest::createCompletionList(cur);
921 return new MathCompletionList(cur.bv().cursor());
925 docstring MathMacro::completionPrefix(Cursor const & cur) const
927 if (displayMode() != DISPLAY_UNFOLDED)
928 return InsetMathNest::completionPrefix(cur);
930 if (!completionSupported(cur))
933 return "\\" + name();
937 bool MathMacro::insertCompletion(Cursor & cur, docstring const & s,
940 if (displayMode() != DISPLAY_UNFOLDED)
941 return InsetMathNest::insertCompletion(cur, s, finished);
943 if (!completionSupported(cur))
947 docstring newName = name() + s;
948 asArray(newName, cell(0));
949 cur.bv().cursor().pos() = name().size();
950 cur.screenUpdateFlags(Update::SinglePar);
954 cur.bv().cursor().pop();
955 ++cur.bv().cursor().pos();
956 cur.screenUpdateFlags(Update::SinglePar);
963 void MathMacro::completionPosAndDim(Cursor const & cur, int & x, int & y,
964 Dimension & dim) const
966 if (displayMode() != DISPLAY_UNFOLDED)
967 InsetMathNest::completionPosAndDim(cur, x, y, dim);
969 // get inset dimensions
970 dim = cur.bv().coordCache().insets().dim(this);
971 // FIXME: these 3 are no accurate, but should depend on the font.
972 // Now the popup jumps down if you enter a char with descent > 0.
978 = cur.bv().coordCache().insets().xy(this);