+/// Private implementation of MathMacro
+class MathMacro::Private {
+public:
+ Private(Buffer * buf, docstring const & name)
+ : name_(name), displayMode_(DISPLAY_INIT),
+ expanded_(buf), definition_(buf), attachedArgsNum_(0),
+ optionals_(0), nextFoldMode_(true), macroBackup_(buf),
+ macro_(0), needsUpdate_(false), isUpdating_(false),
+ appetite_(9)
+ {
+ }
+ /// Update the pointers to our owner of all expanded macros.
+ /// This needs to be called every time a copy of the owner is created
+ /// (bug 9418).
+ void updateChildren(MathMacro * owner);
+ /// Recursively update the pointers of all expanded macros
+ /// appearing in the arguments of the current macro
+ void updateNestedChildren(MathMacro * owner, InsetMathNest * ni);
+ /// name of macro
+ docstring name_;
+ /// current display mode
+ DisplayMode displayMode_;
+ /// expanded macro with ArgumentProxies
+ MathData expanded_;
+ /// macro definition with #1,#2,.. insets
+ MathData definition_;
+ /// number of arguments that were really attached
+ size_t attachedArgsNum_;
+ /// optional argument attached? (only in DISPLAY_NORMAL mode)
+ size_t optionals_;
+ /// fold mode to be set in next metrics call?
+ bool nextFoldMode_;
+ /// if macro_ == true, then here is a copy of the macro
+ /// don't use it for locking
+ MacroData macroBackup_;
+ /// if macroNotFound_ == false, then here is a reference to the macro
+ /// this might invalidate after metrics was called
+ MacroData const * macro_;
+ ///
+ mutable std::map<BufferView const *, bool> editing_;
+ ///
+ std::string requires_;
+ /// update macro representation
+ bool needsUpdate_;
+ ///
+ bool isUpdating_;
+ /// maximal number of arguments the macro is greedy for
+ size_t appetite_;
+ /// Level of nesting in macros (including this one)
+ int nesting_;
+};
+
+
+void MathMacro::Private::updateChildren(MathMacro * owner)
+{
+ for (size_t i = 0; i < expanded_.size(); ++i) {
+ ArgumentProxy * p = dynamic_cast<ArgumentProxy *>(expanded_[i].nucleus());
+ if (p)
+ p->setOwner(owner);
+
+ InsetMathNest * ni = expanded_[i].nucleus()->asNestInset();
+ if (ni)
+ updateNestedChildren(owner, ni);
+ }
+
+ if (macro_) {
+ // The macro_ pointer is updated when MathData::metrics() is
+ // called. However, when instant preview is on or the macro is
+ // not on screen, MathData::metrics() is not called and we may
+ // have a dangling pointer. As a safety measure, when a macro
+ // is copied, always let macro_ point to the backup copy of the
+ // MacroData structure. This backup is updated every time the
+ // macro is changed, so it will not become stale.
+ macro_ = ¯oBackup_;
+ }
+}
+
+
+void MathMacro::Private::updateNestedChildren(MathMacro * owner, InsetMathNest * ni)
+{
+ for (size_t i = 0; i < ni->nargs(); ++i) {
+ MathData & ar = ni->cell(i);
+ for (size_t j = 0; j < ar.size(); ++j) {
+ ArgumentProxy * ap = dynamic_cast
+ <ArgumentProxy *>(ar[j].nucleus());
+ if (ap) {
+ MathMacro::Private * md = ap->owner()->d;
+ if (md->macro_)
+ md->macro_ = &md->macroBackup_;
+ ap->setOwner(owner);
+ }
+ InsetMathNest * imn = ar[j].nucleus()->asNestInset();
+ if (imn)
+ updateNestedChildren(owner, imn);
+ }
+ }
+}
+
+
+MathMacro::MathMacro(Buffer * buf, docstring const & name)
+ : InsetMathNest(buf, 0), d(new Private(buf, name))