#include "MathCompletionList.h"
#include "MathExtern.h"
#include "MathFactory.h"
+#include "MathRow.h"
#include "MathStream.h"
#include "MathSupport.h"
#include "support/gettext.h"
#include "support/lassert.h"
#include "support/lstrings.h"
-#include "support/RefChanger.h"
+#include "support/Changer.h"
#include "support/textutils.h"
#include <ostream>
///
InsetMathMacro const * owner() { return mathMacro_; }
///
- marker_type marker(BufferView const *) const override { return NO_MARKER; }
+ marker_type marker(BufferView const *) const override { return marker_type::NO_MARKER; }
///
InsetCode lyxCode() const override { return ARGUMENT_PROXY_CODE; }
/// The math data to use for display
// macro arguments are in macros
LATTEST(mathMacro_->nesting() > 0);
/// The macro nesting can change display of insets. Change it locally.
- Changer chg = make_change(mi.base.macro_nesting,
+ Changer chg = changeVar(mi.base.macro_nesting,
mathMacro_->nesting() == 1 ? 0 : mathMacro_->nesting());
MathRow::Element e_beg(mi, MathRow::BEGIN);
///
void mathematica(MathematicaStream & ms) const override { ms << mathMacro_->cell(idx_); }
///
- void mathmlize(MathStream & ms) const override { ms << mathMacro_->cell(idx_); }
+ void mathmlize(MathMLStream & ms) const override { ms << mathMacro_->cell(idx_); }
///
void htmlize(HtmlStream & ms) const override { ms << mathMacro_->cell(idx_); }
///
InsetMathMacro::InsetMathMacro(InsetMathMacro const & that)
: InsetMathNest(that), d(new Private(*that.d))
{
- setBuffer(*that.buffer_);
+ // FIXME This should not really be necessary, but when we are
+ // initializing the table of global macros, we create macros
+ // with no associated Buffer.
+ if (that.buffer_)
+ setBuffer(*that.buffer_);
d->updateChildren(this);
}
return InsetMath::addToMathRow(mrow, mi);
/// The macro nesting can change display of insets. Change it locally.
- Changer chg = make_change(mi.base.macro_nesting, d->nesting_);
+ Changer chg = changeVar(mi.base.macro_nesting, d->nesting_);
MathRow::Element e_beg(mi, MathRow::BEGIN);
e_beg.inset = this;
- e_beg.marker = (d->nesting_ == 1) ? marker(mi.base.bv) : NO_MARKER;
+ e_beg.marker = (d->nesting_ == 1) ? marker(mi.base.bv) : marker_type::NO_MARKER;
mrow.push_back(e_beg);
d->macro_->lock();
MathRow::Element e_end(mi, MathRow::END);
e_end.inset = this;
- e_end.marker = (d->nesting_ == 1) ? marker(mi.base.bv) : NO_MARKER;
+ e_end.marker = (d->nesting_ == 1) ? marker(mi.base.bv) : marker_type::NO_MARKER;
mrow.push_back(e_end);
return has_contents;
}
-Limits InsetMathMacro::defaultLimits() const
+Limits InsetMathMacro::defaultLimits(bool display) const
{
if (d->expanded_.empty())
return NO_LIMITS;
// Guess from the expanded macro
InsetMath const * in = d->expanded_.back().nucleus();
Limits const lim = in->limits() == AUTO_LIMITS
- ? in->defaultLimits() : in->limits();
+ ? in->defaultLimits(display) : in->limits();
LATTEST(lim != AUTO_LIMITS);
return lim;
}
bool InsetMathMacro::editMode(BufferView const * bv) const {
// find this in cursor trace
- Cursor const & cur = bv->cursor();
+ DocIterator const & cur =
+ // Do not move the reference while selecting with the mouse to avoid
+ // flicker due to changing metrics
+ bv->mouseSelecting() ? bv->cursor().realAnchor() : bv->cursor();
for (size_t i = 0; i != cur.depth(); ++i)
if (&cur[i].inset() == this) {
// look if there is no other macro in edit mode above
}
-InsetMath::marker_type InsetMathMacro::marker(BufferView const * bv) const
+marker_type InsetMathMacro::marker(BufferView const * bv) const
{
if (nargs() == 0)
- return NO_MARKER;
+ return marker_type::NO_MARKER;
switch (d->displayMode_) {
case DISPLAY_INIT:
case DISPLAY_INTERACTIVE_INIT:
- return NO_MARKER;
+ return marker_type::NO_MARKER;
case DISPLAY_UNFOLDED:
- return MARKER;
+ return marker_type::MARKER;
case DISPLAY_NORMAL:
switch (lyxrc.macro_edit_style) {
case LyXRC::MACRO_EDIT_INLINE:
- return MARKER2;
+ return marker_type::MARKER2;
case LyXRC::MACRO_EDIT_INLINE_BOX:
- return d->editing_[bv] ? BOX_MARKER : MARKER2;
+ return d->editing_[bv] ? marker_type::BOX_MARKER : marker_type::MARKER2;
case LyXRC::MACRO_EDIT_LIST:
- return MARKER2;
+ return marker_type::MARKER2;
}
}
// please gcc 4.6
- return NO_MARKER;
+ return marker_type::NO_MARKER;
}
void InsetMathMacro::metrics(MetricsInfo & mi, Dimension & dim) const
{
/// The macro nesting can change display of insets. Change it locally.
- Changer chg = make_change(mi.base.macro_nesting, d->nesting_);
+ Changer chg = changeVar(mi.base.macro_nesting, d->nesting_);
// set edit mode for which we will have calculated metrics. But only
d->editing_[mi.base.bv] = editMode(mi.base.bv);
features.require(data->required());
}
- if (name() == "binom")
- features.require("binom");
-
- // validate the cells and the definition
- if (displayMode() == DISPLAY_NORMAL) {
- // Don't update requirements if the macro comes from
- // the symbols file and has not been redefined.
- MathWordList const & words = mathedWordList();
- MathWordList::const_iterator it = words.find(name());
- MacroNameSet macros;
- buffer().listMacroNames(macros);
- if (it == words.end() || it->second.inset != "macro"
- || macros.find(name()) != macros.end()) {
- d->definition_.validate(features);
+ // Validate the cells and the definition.
+ // However, don't validate the definition if the macro is
+ // from the symbols file and has not been redefined, because
+ // in this case the definition is only used for screen display.
+ MathWordList const & words = mathedWordList();
+ MathWordList::const_iterator it = words.find(name());
+ MacroNameSet macros;
+ buffer().listMacroNames(macros);
+ if (it == words.end() || it->second.inset != "macro"
+ || macros.find(name()) != macros.end()) {
+ if (displayMode() == DISPLAY_NORMAL) {
+ d->definition_.validate(features);
+ } else if (displayMode() == DISPLAY_INIT) {
+ MathData ar(const_cast<Buffer *>(&buffer()));
+ MacroData const * data = buffer().getMacro(name());
+ if (data) {
+ asArray(data->definition(), ar);
+ ar.validate(features);
+ }
}
- InsetMathNest::validate(features);
}
+ InsetMathNest::validate(features);
}
}
-void InsetMathMacro::removeArgument(Inset::pos_type pos) {
+void InsetMathMacro::removeArgument(pos_type pos) {
if (d->displayMode_ == DISPLAY_NORMAL) {
LASSERT(size_t(pos) < cells_.size(), return);
cells_.erase(cells_.begin() + pos);
}
-void InsetMathMacro::insertArgument(Inset::pos_type pos) {
+void InsetMathMacro::insertArgument(pos_type pos) {
if (d->displayMode_ == DISPLAY_NORMAL) {
LASSERT(size_t(pos) <= cells_.size(), return);
cells_.insert(cells_.begin() + pos, MathData());
}
-void InsetMathMacro::write(WriteStream & os) const
+void InsetMathMacro::write(TeXMathStream & os) const
{
mode_type mode = currentMode();
MathEnsurer ensurer(os, mode == MATH_MODE, true, mode == TEXT_MODE);
}
// add space if there was no argument
- if (first)
+ // or add braces if we have optionals but none are present and [ follows
+ if (first) {
os.pendingSpace(true);
+ os.useBraces(d->optionals_ > 0);
+ }
// write \(no)limits modifiers if relevant
writeLimits(os);
}
-void InsetMathMacro::mathmlize(MathStream & ms) const
+void InsetMathMacro::mathmlize(MathMLStream & ms) const
{
// macro_ is 0 if this is an unknown macro
LATTEST(d->macro_ || d->displayMode_ != DISPLAY_NORMAL);
bool InsetMathMacro::completionSupported(Cursor const & cur) const
{
+ if (cur.buffer()->isReadonly())
+ return false;
+
if (displayMode() != DISPLAY_UNFOLDED)
return InsetMathNest::completionSupported(cur);
bool InsetMathMacro::inlineCompletionSupported(Cursor const & cur) const
{
+ if (cur.buffer()->isReadonly())
+ return false;
+
if (displayMode() != DISPLAY_UNFOLDED)
return InsetMathNest::inlineCompletionSupported(cur);
if (displayMode() != DISPLAY_UNFOLDED)
return InsetMathNest::completionPrefix(cur);
- if (!completionSupported(cur))
- return docstring();
-
return "\\" + name();
}
-bool InsetMathMacro::insertCompletion(Cursor & cur, docstring const & s,
- bool finished)
+bool InsetMathMacro::insertCompletion(Cursor & cur, docstring const & s, bool finished)
{
+ if (cur.buffer()->isReadonly())
+ return false;
+
if (displayMode() != DISPLAY_UNFOLDED)
return InsetMathNest::insertCompletion(cur, s, finished);
if (!completionSupported(cur))
return false;
+ // Contrary to Text, the whole inset should be recorded (#12581).
+ cur.recordUndoInset();
+
// append completion
docstring newName = name() + s;
asArray(newName, cell(0));