]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/MathData.cpp
The previous commit was actually a fix for bug #9158.
[lyx.git] / src / mathed / MathData.cpp
index 2d5bcd02afc621b8690277738f92a82d22717e43..8ac4ac1273ac33ebd67ab933e32113722f3164d8 100644 (file)
@@ -38,7 +38,7 @@
 
 #include "support/gettext.h"
 #include "support/lassert.h"
-#include <boost/next_prior.hpp>
+#include "support/lyxalgo.h"
 
 #include <cstdlib>
 
@@ -48,7 +48,8 @@ namespace lyx {
 
 
 MathData::MathData(Buffer * buf, const_iterator from, const_iterator to)
-       : base_type(from, to), buffer_(buf)
+       : base_type(from, to), minasc_(0), mindes_(0), slevel_(0),
+         sshift_(0), kerning_(0), buffer_(buf)
 {}
 
 
@@ -265,7 +266,7 @@ void MathData::metrics(MetricsInfo & mi, Dimension & dim) const
        dim.asc = 0;
        dim.wid = 0;
        Dimension d;
-       CoordCacheBase<Inset> & coords = mi.base.bv->coordCache().insets();
+       CoordCache::Insets & coords = mi.base.bv->coordCache().insets();
        for (pos_type i = 0, n = size(); i != n; ++i) {
                MathAtom const & at = operator[](i);
                at->metrics(mi, d);
@@ -284,7 +285,7 @@ void MathData::metrics(MetricsInfo & mi, Dimension & dim) const
                        continue;
 
                FontInfo font = mi.base.font;
-               augmentFont(font, from_ascii("mathnormal"));
+               augmentFont(font, "mathnormal");
                dim.wid += mathed_string_width(font, completion);
        }
        // Cache the dimension.
@@ -317,7 +318,7 @@ void MathData::draw(PainterInfo & pi, int x, int y) const
        if (inlineCompletionPos.inMathed())
                inlineCompletionData = &inlineCompletionPos.cell();
 
-       CoordCacheBase<Inset> & coords = pi.base.bv->coordCache().insets();
+       CoordCache::Insets & coords = pi.base.bv->coordCache().insets();
        for (size_t i = 0, n = size(); i != n; ++i) {
                MathAtom const & at = operator[](i);
                coords.add(at.nucleus(), x, y);
@@ -333,7 +334,7 @@ void MathData::draw(PainterInfo & pi, int x, int y) const
                if (completion.length() == 0)
                        continue;
                FontInfo f = pi.base.font;
-               augmentFont(f, from_ascii("mathnormal"));
+               augmentFont(f, "mathnormal");
 
                // draw the unique and the non-unique completion part
                // Note: this is not time-critical as it is
@@ -405,9 +406,9 @@ void MathData::updateMacros(Cursor * cur, MacroContext const & mc,
        // go over the array and look for macros
        for (size_t i = 0; i < size(); ++i) {
                MathMacro * macroInset = operator[](i).nucleus()->asMacro();
-               if (!macroInset || macroInset->name_.empty()
-                               || macroInset->name_[0] == '^'
-                               || macroInset->name_[0] == '_'
+               if (!macroInset || macroInset->macroName().empty()
+                               || macroInset->macroName()[0] == '^'
+                               || macroInset->macroName()[0] == '_'
                                || (macroInset->name() == edited_name
                                    && macroInset->displayMode() ==
                                                MathMacro::DISPLAY_UNFOLDED))
@@ -432,13 +433,8 @@ void MathData::updateMacros(Cursor * cur, MacroContext const & mc,
                if (oldDisplayMode == MathMacro::DISPLAY_NORMAL
                    && (macroInset->arity() != macroNumArgs
                        || macroInset->optionals() != macroOptionals
-                       || newDisplayMode == MathMacro::DISPLAY_UNFOLDED)) {
-
+                       || newDisplayMode == MathMacro::DISPLAY_UNFOLDED))
                        detachMacroParameters(cur, i);
-                       // FIXME: proper anchor handling, this removes the selection
-                       if (cur)
-                               cur->clearSelection();
-               }
 
                // the macro could have been copied while resizing this
                macroInset = operator[](i).nucleus()->asMacro();
@@ -477,11 +473,8 @@ void MathData::updateMacros(Cursor * cur, MacroContext const & mc,
                        attachMacroParameters(cur, i, macroNumArgs, macroOptionals,
                                fromInitToNormalMode, interactive, appetite);
 
-                       if (cur) {
-                               // FIXME: proper anchor handling, this removes the selection
+                       if (cur)
                                cur->updateInsets(&cur->bottom().inset());
-                               cur->clearSelection();
-                       }
                }
 
                // Give macro the chance to adapt to new situation.
@@ -499,6 +492,8 @@ void MathData::updateMacros(Cursor * cur, MacroContext const & mc,
 void MathData::detachMacroParameters(DocIterator * cur, const size_type macroPos)
 {
        MathMacro * macroInset = operator[](macroPos).nucleus()->asMacro();
+       // We store this now, because the inset pointer will be invalidated in the scond loop below
+       size_t const optionals = macroInset->optionals();
 
        // detach all arguments
        vector<MathData> detachedArgs;
@@ -524,7 +519,7 @@ void MathData::detachMacroParameters(DocIterator * cur, const size_type macroPos
 
        // only [] after the last non-empty argument can be dropped later
        size_t lastNonEmptyOptional = 0;
-       for (size_t l = 0; l < detachedArgs.size() && l < macroInset->optionals(); ++l) {
+       for (size_t l = 0; l < detachedArgs.size() && l < optionals; ++l) {
                if (!detachedArgs[l].empty())
                        lastNonEmptyOptional = l;
        }
@@ -532,7 +527,10 @@ void MathData::detachMacroParameters(DocIterator * cur, const size_type macroPos
        // optional arguments to be put back?
        pos_type p = macroPos + 1;
        size_t j = 0;
-       for (; j < detachedArgs.size() && j < macroInset->optionals(); ++j) {
+       // We do not want to use macroInset below, the insert() call in
+       // the loop will invalidate it.
+       macroInset = 0;
+       for (; j < detachedArgs.size() && j < optionals; ++j) {
                // another non-empty parameter follows?
                bool canDropEmptyOptional = j >= lastNonEmptyOptional;
 
@@ -857,7 +855,7 @@ int MathData::pos2x(BufferView const * bv, size_type pos, int glue) const
 {
        int x = 0;
        size_type target = min(pos, size());
-       CoordCacheBase<Inset> const & coords = bv->coordCache().getInsets();
+       CoordCache::Insets const & coords = bv->coordCache().getInsets();
        for (size_type i = 0; i < target; ++i) {
                const_iterator it = begin() + i;
                if ((*it)->getChar() == ' ')
@@ -881,7 +879,7 @@ MathData::size_type MathData::x2pos(BufferView const * bv, int targetx, int glue
        const_iterator it = begin();
        int lastx = 0;
        int currx = 0;
-       CoordCacheBase<Inset> const & coords = bv->coordCache().getInsets();
+       CoordCache::Insets const & coords = bv->coordCache().getInsets();
        // find first position after targetx
        for (; currx < targetx && it != end(); ++it) {
                lastx = currx;
@@ -901,7 +899,7 @@ MathData::size_type MathData::x2pos(BufferView const * bv, int targetx, int glue
         * See bug 1918 for details.
         **/
        if (it != begin() && currx >= targetx
-           && ((*boost::prior(it))->asNestInset()
+           && ((*prev(it, 1))->asNestInset()
                || abs(lastx - targetx) < abs(currx - targetx))) {
                --it;
        }