]> git.lyx.org Git - features.git/commitdiff
Make math autocorrrect work with more than 2 chars
authorIsaac <isaacOscar@hotmail.com>
Tue, 9 Apr 2019 01:48:46 +0000 (13:48 +1200)
committerJean-Marc Lasgouttes <lasgouttes@lyx.org>
Thu, 18 Jun 2020 13:48:25 +0000 (15:48 +0200)
src/mathed/InsetMathNest.cpp
src/mathed/MathAutoCorrect.cpp
src/mathed/MathAutoCorrect.h

index 798ca8920b952ab656e01967817cafcc8b89069a..85e4dd7c70043d18d28b80b55618fb184efe5686 100644 (file)
@@ -1868,7 +1868,7 @@ bool InsetMathNest::interpretChar(Cursor & cur, char_type const c)
 
        // try auto-correction
        if (lyxrc.autocorrection_math && cur.autocorrect() && cur.pos() != 0
-                 && math_autocorrect(cur.prevAtom(), c))
+                 && math_autocorrect(cur, c))
                return true;
 
        // no special circumstances, so insert the character without any fuss
index fc4ad440d20919fd5576c3b6820a57710dd2d079..be5aeb42d0430c127179379784f8275431b83437 100644 (file)
@@ -10,6 +10,7 @@
 
 #include <config.h>
 
+#include "Cursor.h"
 #include "MathAutoCorrect.h"
 #include "MathData.h"
 #include "InsetMath.h"
@@ -38,18 +39,18 @@ public:
        /// \brief Correction
        Correction() : from2_(0) {}
        ///
-       bool correct(MathAtom & at, char_type c) const;
+       bool correct(Cursor & cur, char_type c) const;
        ///
        bool read(idocstream & is);
        ///
        void write(odocstream & os) const;
 private:
        ///
-       MathAtom from1_;
+       MathData from1_;
        ///
        char_type from2_;
        ///
-       MathAtom to_;
+       MathData to_;
 };
 
 
@@ -64,26 +65,35 @@ bool Correction::read(idocstream & is)
        MathData ar1, ar3;
        mathed_parse_cell(ar1, s1);
        mathed_parse_cell(ar3, s3);
-       if (ar1.size() != 1 || ar3.size() != 1)
-               return false;
-       from1_ = ar1.front();
+       from1_ = ar1;
        from2_ = s2[0];
-       to_    = ar3.front();
+       to_    = ar3;
        return true;
 }
 
 
-bool Correction::correct(MathAtom & at, char_type c) const
+bool Correction::correct(Cursor & cur, char_type c) const
 {
        //LYXERR(Debug::MATHED,
        //      "trying to correct ar: " << at << " from: '" << from1_ << '\'');
        if (from2_ != c)
                return false;
-       if (asString(at) != asString(from1_))
+       pos_type n = from1_.size();
+       if (cur.pos() < pos_type(from1_.size())) // not enough to match
                return false;
-       LYXERR(Debug::MATHED, "match found! subst in " << at
+       pos_type start = cur.pos() - from1_.size();
+
+       for (pos_type i = 0; i < n; i++)
+               if (asString(cur.cell()[start + i]) != asString(from1_[i]))
+                       return false;
+
+       LYXERR(Debug::MATHED, "match found! subst in " << cur.cell()
                << " from: '" << from1_ << "' to '" << to_ << '\'');
-       at = to_;
+
+       cur.cell().erase(cur.pos() - n, cur.pos());
+       cur.pos() -= n;
+
+       cur.insert(to_);
        return true;
 }
 
@@ -121,17 +131,17 @@ public:
        ///
        void insert(const Correction & corr) { data_.push_back(corr); }
        ///
-       bool correct(MathAtom & at, char_type c) const;
+       bool correct(Cursor & cur, char_type c) const;
 private:
        ///
        vector<Correction> data_;
 };
 
 
-bool Corrections::correct(MathAtom & at, char_type c) const
+bool Corrections::correct(Cursor & cur, char_type c) const
 {
        for (const_iterator it = data_.begin(); it != data_.end(); ++it)
-               if (it->correct(at, c))
+               if (it->correct(cur, c))
                        return true;
        return false;
 }
@@ -172,7 +182,7 @@ void initAutoCorrect()
 } // namespace
 
 
-bool math_autocorrect(MathAtom & at, char_type c)
+bool math_autocorrect(Cursor & cur, char_type c)
 {
        static bool initialized = false;
 
@@ -181,8 +191,6 @@ bool math_autocorrect(MathAtom & at, char_type c)
                initialized = true;
        }
 
-       return theCorrections.correct(at, c);
+       return theCorrections.correct(cur, c);
 }
-
-
 } // namespace lyx
index 679e83738e6c1cc52f54c04d92e5515413d78f24..a782d678eedcb8c0a4667149db6251a1511e6853 100644 (file)
 
 namespace lyx {
 
-class MathAtom;
+class Cursor;
 
 // make "corrections" according to file lib/autocorrect
-bool math_autocorrect(MathAtom & at, char_type c);
+bool math_autocorrect(Cursor & cur, char_type c);
 
 } // namespace lyx