]> git.lyx.org Git - lyx.git/commitdiff
Try to make sure that math insets have a properly set buffer_ member
authorJean-Marc Lasgouttes <lasgouttes@lyx.org>
Fri, 14 Oct 2022 20:42:21 +0000 (22:42 +0200)
committerJean-Marc Lasgouttes <lasgouttes@lyx.org>
Mon, 22 Apr 2024 15:52:23 +0000 (17:52 +0200)
Set the buffer of contents that is added to a MathData object through
MathData::insert() (both versions)
MathData::push_back()
asArray()

Also in math macros, initialize look_ with the relevant buffer.

This reduces the number of insets hat do not have a proper buffer.

See #13050 for discussion of this issue.

src/mathed/InsetMathMacroTemplate.cpp
src/mathed/MathData.cpp
src/mathed/MathData.h
src/mathed/MathSupport.cpp

index 9fe10491e267b82ec487f2de254c3d5f5f2998c9..c28c0d100f680ef95abd9f48b06b517a01c8f9fe 100644 (file)
@@ -394,7 +394,7 @@ void InsetNameWrapper::draw(PainterInfo & pi, int x, int y) const
 
 
 InsetMathMacroTemplate::InsetMathMacroTemplate(Buffer * buf)
-       : InsetMathNest(buf, 3), numargs_(0), argsInLook_(0), optionals_(0),
+       : InsetMathNest(buf, 3), look_(buf), numargs_(0), argsInLook_(0), optionals_(0),
          type_(MacroTypeNewcommand), redefinition_(false), lookOutdated_(true),
          premetrics_(false), labelBoxAscent_(0), labelBoxDescent_(0)
 {
@@ -405,8 +405,8 @@ InsetMathMacroTemplate::InsetMathMacroTemplate(Buffer * buf)
 InsetMathMacroTemplate::InsetMathMacroTemplate(Buffer * buf, docstring const & name, int numargs,
        int optionals, MacroType type, vector<MathData> const & optionalValues,
        MathData const & def, MathData const & display)
-       : InsetMathNest(buf, optionals + 3), numargs_(numargs), argsInLook_(numargs),
-         optionals_(optionals), optionalValues_(optionalValues),
+       : InsetMathNest(buf, optionals + 3), look_(buf), numargs_(numargs),
+         argsInLook_(numargs), optionals_(optionals), optionalValues_(optionalValues),
          type_(type), redefinition_(false), lookOutdated_(true),
          premetrics_(false), labelBoxAscent_(0), labelBoxDescent_(0)
 {
@@ -536,6 +536,8 @@ void InsetMathMacroTemplate::createLook(int args) const
        look_.push_back(MathAtom(
                new InsetDisplayLabelBox(buffer_, MathAtom(
                        new InsetMathWrapper(&cell(displayIdx()))), _("LyX"), *this)));
+
+       look_.setContentsBuffer();
 }
 
 
index 24b4d86fb90649a006ddb2922924c8072919ec97..4c3393482885db3126bc76598eb1403a45a2bd76 100644 (file)
@@ -52,11 +52,18 @@ MathData::MathData(Buffer * buf, const_iterator from, const_iterator to)
 {}
 
 
+void MathData::setContentsBuffer()
+{
+       if (buffer_)
+               for (MathAtom & at : *this)
+                       at.nucleus()->setBuffer(*buffer_);
+}
+
+
 void MathData::setBuffer(Buffer & b)
 {
        buffer_ = &b;
-       for (MathAtom & at : *this)
-               at.nucleus()->setBuffer(b);
+       setContentsBuffer();
 }
 
 
@@ -78,6 +85,8 @@ void MathData::insert(size_type pos, MathAtom const & t)
 {
        LBUFERR(pos <= size());
        base_type::insert(begin() + pos, t);
+       if (buffer_)
+               operator[](pos)->setBuffer(*buffer_);
 }
 
 
@@ -85,6 +94,17 @@ void MathData::insert(size_type pos, MathData const & ar)
 {
        LBUFERR(pos <= size());
        base_type::insert(begin() + pos, ar.begin(), ar.end());
+       if (buffer_)
+               for (size_type i = 0 ; i < ar.size() ; ++i)
+                       operator[](pos + i)->setBuffer(*buffer_);
+}
+
+
+void MathData::push_back(MathAtom const & t)
+{
+       base_type::push_back(t);
+       if (buffer_)
+               back()->setBuffer(*buffer_);
 }
 
 
index 865742df4cb9603040cf5ec8456d3f9bdf54a585..c2d731efcb27e273de844df3adddd9737aeefd2a 100644 (file)
@@ -59,7 +59,6 @@ public:
        using base_type::clear;
        using base_type::begin;
        using base_type::end;
-       using base_type::push_back;
        using base_type::pop_back;
        using base_type::back;
        using base_type::front;
@@ -85,6 +84,8 @@ public:
        void insert(size_type pos, MathAtom const & at);
        /// inserts multiple atoms at position pos
        void insert(size_type pos, MathData const & ar);
+       /// inserts single atom at end
+       void push_back(MathAtom const & at);
 
        /// erase range from pos1 to pos2
        void erase(iterator pos1, iterator pos2);
@@ -187,8 +188,10 @@ public:
        void updateMacros(Cursor * cur, MacroContext const & mc, UpdateType, int nesting);
        ///
        void updateBuffer(ParIterator const &, UpdateType, bool const deleted = false);
-       ///
+       /// Change associated buffer for this object and its contents
        void setBuffer(Buffer & b);
+       /// Update assiociated buffer for the contents of the object
+       void setContentsBuffer();
 
 protected:
        /// cached values for super/subscript placement
index 585320f11aea3d640d2a9cb1e7e5fc92fb28f0db..6987decea8390d52d4d4ee2c8bacec5e6122fc8c 100644 (file)
@@ -1102,6 +1102,9 @@ void asArray(docstring const & str, MathData & ar, Parse::flags pf)
        bool macro = pf & Parse::MACRODEF;
        if ((str.size() == 1 && quiet) || (!mathed_parse_cell(ar, str, pf) && quiet && !macro))
                mathed_parse_cell(ar, str, pf | Parse::VERBATIM);
+
+       // set the buffer of the MathData contents
+       ar.setContentsBuffer();
 }