X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Fmathed%2FMathMacro.cpp;h=f4150f30f84250533ff313105aa5ebfda07605cb;hb=ff85a4902ec4fff1b3bf0dbc9c9c53b3e87563cf;hp=da398683b029062d5eb66e79c58a5f3803de1c71;hpb=573500dd04f154f27318ac5ec469a337f97fe9f0;p=lyx.git diff --git a/src/mathed/MathMacro.cpp b/src/mathed/MathMacro.cpp index da398683b0..f4150f30f8 100644 --- a/src/mathed/MathMacro.cpp +++ b/src/mathed/MathMacro.cpp @@ -34,12 +34,15 @@ #include "frontends/Painter.h" #include "support/debug.h" +#include "support/gettext.h" #include "support/lassert.h" +#include "support/lstrings.h" #include "support/textutils.h" #include #include +using namespace lyx::support; using namespace std; namespace lyx { @@ -125,7 +128,8 @@ private: MathMacro::MathMacro(Buffer * buf, docstring const & name) : InsetMathNest(buf, 0), name_(name), displayMode_(DISPLAY_INIT), expanded_(buf), attachedArgsNum_(0), optionals_(0), nextFoldMode_(true), - macroBackup_(buf), macro_(0), needsUpdate_(false), appetite_(9) + macroBackup_(buf), macro_(0), needsUpdate_(false), + isUpdating_(false), appetite_(9) {} @@ -138,6 +142,15 @@ Inset * MathMacro::clone() const } +void MathMacro::normalize(NormalStream & os) const +{ + os << "[macro " << name(); + for (size_t i = 0; i < nargs(); ++i) + os << ' ' << cell(i); + os << ']'; +} + + docstring MathMacro::name() const { if (displayMode_ == DISPLAY_UNFOLDED) @@ -164,9 +177,12 @@ bool MathMacro::editMode(BufferView const * bv) const { // look if there is no other macro in edit mode above ++i; for (; i != cur.depth(); ++i) { - MathMacro const * macro = cur[i].asInsetMath()->asMacro(); - if (macro && macro->displayMode() == DISPLAY_NORMAL) - return false; + InsetMath * im = cur[i].asInsetMath(); + if (im) { + MathMacro const * macro = im->asMacro(); + if (macro && macro->displayMode() == DISPLAY_NORMAL) + return false; + } } // ok, none found, I am the highest one @@ -203,7 +219,7 @@ void MathMacro::metrics(MetricsInfo & mi, Dimension & dim) const && editing_[mi.base.bv]) { // Macro will be edited in a old-style list mode here: - LASSERT(macro_ != 0, /**/); + LBUFERR(macro_); Dimension fontDim; FontInfo labelFont = sane_font; math_font_max_dim(labelFont, fontDim.asc, fontDim.des); @@ -240,7 +256,7 @@ void MathMacro::metrics(MetricsInfo & mi, Dimension & dim) const dim.wid += 2; metricsMarkers2(dim); } else { - LASSERT(macro_ != 0, /**/); + LBUFERR(macro_); // calculate metrics, hoping that all cells are seen macro_->lock(); @@ -249,7 +265,7 @@ void MathMacro::metrics(MetricsInfo & mi, Dimension & dim) const // otherwise do a manual metrics call CoordCache & coords = mi.base.bv->coordCache(); for (idx_type i = 0; i < nargs(); ++i) { - if (!coords.getArrays().has(&cell(i))) { + if (!coords.getArrays().hasDim(&cell(i))) { Dimension tdim; cell(i).metrics(mi, tdim); } @@ -299,8 +315,27 @@ void MathMacro::updateMacro(MacroContext const & mc) } -void MathMacro::updateRepresentation() +class MathMacro::UpdateLocker +{ +public: + explicit UpdateLocker(MathMacro & mm) : mac(mm) + { + mac.isUpdating_ = true; + } + ~UpdateLocker() { mac.isUpdating_ = false; } +private: + MathMacro & mac; +}; + + +void MathMacro::updateRepresentation(Cursor * cur, MacroContext const & mc, + UpdateType utype) { + if (isUpdating_) + return; + + UpdateLocker locker(*this); + // known macro? if (macro_ == 0) return; @@ -330,6 +365,8 @@ void MathMacro::updateRepresentation() } // expanding macro with the values macro_->expand(values, expanded_.cell(0)); + if (utype == OutputUpdate && !expanded_.cell(0).empty()) + expanded_.cell(0).updateMacros(cur, mc, utype); // get definition for list edit mode docstring const & display = macro_->display(); asArray(display.empty() ? macro_->definition() : display, definition_); @@ -450,7 +487,7 @@ void MathMacro::draw(PainterInfo & pi, int x, int y) const void MathMacro::drawSelection(PainterInfo & pi, int x, int y) const { // We may have 0 arguments, but InsetMathNest requires at least one. - if (cells_.size() > 0) + if (!cells_.empty()) InsetMathNest::drawSelection(pi, x, y); } @@ -492,8 +529,7 @@ bool MathMacro::validName() const { docstring n = name(); - // empty name? - if (n.size() == 0) + if (n.empty()) return false; // converting back and force doesn't swallow anything? @@ -550,7 +586,7 @@ Inset * MathMacro::editXY(Cursor & cur, int x, int y) void MathMacro::removeArgument(Inset::pos_type pos) { if (displayMode_ == DISPLAY_NORMAL) { - LASSERT(size_t(pos) < cells_.size(), /**/); + LASSERT(size_t(pos) < cells_.size(), return); cells_.erase(cells_.begin() + pos); if (size_t(pos) < attachedArgsNum_) --attachedArgsNum_; @@ -565,7 +601,7 @@ void MathMacro::removeArgument(Inset::pos_type pos) { void MathMacro::insertArgument(Inset::pos_type pos) { if (displayMode_ == DISPLAY_NORMAL) { - LASSERT(size_t(pos) <= cells_.size(), /**/); + LASSERT(size_t(pos) <= cells_.size(), return); cells_.insert(cells_.begin() + pos, MathData()); if (size_t(pos) < attachedArgsNum_) ++attachedArgsNum_; @@ -579,7 +615,7 @@ void MathMacro::insertArgument(Inset::pos_type pos) { void MathMacro::detachArguments(vector & args, bool strip) { - LASSERT(displayMode_ == DISPLAY_NORMAL, /**/); + LASSERT(displayMode_ == DISPLAY_NORMAL, return); args = cells_; // strip off empty cells, but not more than arity-attachedArgsNum_ @@ -600,7 +636,7 @@ void MathMacro::detachArguments(vector & args, bool strip) void MathMacro::attachArguments(vector const & args, size_t arity, int optionals) { - LASSERT(displayMode_ == DISPLAY_NORMAL, /**/); + LASSERT(displayMode_ == DISPLAY_NORMAL, return); cells_ = args; attachedArgsNum_ = args.size(); cells_.resize(arity); @@ -633,13 +669,16 @@ bool MathMacro::notifyCursorLeaves(Cursor const & old, Cursor & cur) // The macro name was changed Cursor inset_cursor = old; int macroSlice = inset_cursor.find(this); - LASSERT(macroSlice != -1, /**/); + // returning true means the cursor is "now" invalid, + // which it was. + LASSERT(macroSlice != -1, return true); inset_cursor.cutOff(macroSlice); inset_cursor.recordUndoInset(); inset_cursor.pop(); inset_cursor.cell().erase(inset_cursor.pos()); inset_cursor.cell().insert(inset_cursor.pos(), createInsetMath(unfolded_name, cur.buffer())); + cur.resetAnchor(); cur.screenUpdateFlags(cur.result().screenUpdate() | Update::SinglePar); return true; } @@ -686,10 +725,11 @@ void MathMacro::write(WriteStream & os) const } // normal mode - LASSERT(macro_, /**/); + // we should be ok to continue even if this fails. + LATTEST(macro_); - // optional arguments make macros fragile - if (optionals_ > 0 && os.fragile()) + // Always protect macros in a fragile environment + if (os.fragile()) os << "\\protect"; os << "\\" << name(); @@ -717,7 +757,7 @@ void MathMacro::write(WriteStream & os) const for (; i < cells_.size(); ++i) { if (cell(i).size() == 1 && cell(i)[0].nucleus()->asCharInset() - && cell(i)[0].nucleus()->asCharInset()->getChar() < 0x80) { + && isASCII(cell(i)[0].nucleus()->asCharInset()->getChar())) { if (first) os << " "; os << cell(i); @@ -740,13 +780,23 @@ void MathMacro::maple(MapleStream & os) const void MathMacro::mathmlize(MathStream & os) const { - os << expanded_.cell(0); + MathData const & data = expanded_.cell(0); + if (data.empty()) { + // this means that we do not recognize the macro + throw MathExportException(); + } + os << data; } void MathMacro::htmlize(HtmlStream & os) const { - os << expanded_.cell(0); + MathData const & data = expanded_.cell(0); + if (data.empty()) { + // this means that we do not recognize the macro + throw MathExportException(); + } + os << data; } @@ -758,14 +808,13 @@ void MathMacro::octave(OctaveStream & os) const void MathMacro::infoize(odocstream & os) const { - os << "Macro: " << name(); + os << bformat(_("Macro: %1$s"), name()); } void MathMacro::infoize2(odocstream & os) const { - os << "Macro: " << name(); - + os << bformat(_("Macro: %1$s"), name()); }