commit a4676712837e10573f2f2ce2a95767832c81b4ea
Author: Guillaume Munch <g...@lyx.org>
Date:   Tue Jan 3 20:17:20 2017 +0100

    Define mathClass of MathMacro
    
    Enables intelligent splitting for math macros.
    
    Crucially improves the detection of math words for the new Ctrl-Arrow 
movement.
---
 src/mathed/MathData.cpp  |   14 ++++++++++++++
 src/mathed/MathData.h    |    2 ++
 src/mathed/MathMacro.cpp |   20 +++++++++++++++++---
 3 files changed, 33 insertions(+), 3 deletions(-)

diff --git a/src/mathed/MathData.cpp b/src/mathed/MathData.cpp
index 9acefe6..dcf9647 100644
--- a/src/mathed/MathData.cpp
+++ b/src/mathed/MathData.cpp
@@ -889,6 +889,20 @@ int MathData::yo(BufferView const & bv) const
 }
 
 
+MathClass MathData::mathClass() const
+{
+       MathClass res = MC_UNKNOWN;
+       for (MathAtom const & at : *this) {
+               MathClass mc = at->mathClass();
+               if (res == MC_UNKNOWN)
+                       res = mc;
+               else if (mc != MC_UNKNOWN && res != mc)
+                       return MC_ORD;
+       }
+       return res == MC_UNKNOWN ? MC_ORD : res;
+}
+
+
 ostream & operator<<(ostream & os, MathData const & ar)
 {
        odocstringstream oss;
diff --git a/src/mathed/MathData.h b/src/mathed/MathData.h
index 058d86e..a1ce7a6 100644
--- a/src/mathed/MathData.h
+++ b/src/mathed/MathData.h
@@ -137,6 +137,8 @@ public:
        void drawT(TextPainter & pi, int x, int y) const;
        /// mark cell for re-drawing
        void touch() const;
+       /// approximate the math class of the data
+       MathClass mathClass() const;
 
        /// access to cached x coordinate of last drawing
        int xo(BufferView const & bv) const;
diff --git a/src/mathed/MathMacro.cpp b/src/mathed/MathMacro.cpp
index 89f8200..64d2ee3 100644
--- a/src/mathed/MathMacro.cpp
+++ b/src/mathed/MathMacro.cpp
@@ -140,6 +140,15 @@ public:
        void htmlize(HtmlStream & ms) const { ms << mathMacro_->cell(idx_); }
        ///
        void octave(OctaveStream & os) const { os << mathMacro_->cell(idx_); }
+       ///
+       MathClass mathClass() const
+       {
+               return MC_UNKNOWN;
+               // This can be refined once the pointer issues are fixed. I did 
not
+               // notice any immediate crash with the following code, but it 
is risky
+               // nevertheless:
+               //return mathMacro_->cell(idx_).mathClass();
+       }
 
 private:
        ///
@@ -823,16 +832,21 @@ size_t MathMacro::appetite() const
 
 MathClass MathMacro::mathClass() const
 {
-       // this only affects intelligent splitting since global macros are 
always
-       // linearised.
+       // This can be just a heuristic, since it is only considered for display
+       // when the macro is not linearised. Therefore it affects:
+       // * The spacing of the inset while being edited,
+       // * Intelligent splitting
+       // * Cursor word movement (Ctrl-Arrow).
        if (MacroData const * m = macroBackup()) {
+               // If it is a global macro and is defined explicitly
                if (m->symbol()) {
                        MathClass mc = 
string_to_class(d->macroBackup_.symbol()->extra);
                        if (mc != MC_UNKNOWN)
                                return mc;
                }
        }
-       return MC_ORD;
+       // Otherwise guess from the expanded macro
+       return d->expanded_.mathClass();
 }
 
 

Reply via email to