commit f7a11ca7115064710b98bedc64d724ca15362527
Author: Jean-Marc Lasgouttes <[email protected]>
Date:   Mon Apr 15 11:12:25 2019 +0200

    Allow to undo partly math autocorrect
    
    To this end, introduce Undo::splitUndoGroup, which ends currently
    group and creates a new one with same nesting level.
---
 src/Cursor.cpp                 |    6 ++++++
 src/Cursor.h                   |    2 ++
 src/Undo.cpp                   |   10 ++++++++++
 src/Undo.h                     |    2 ++
 src/mathed/MathAutoCorrect.cpp |   13 +++++++++++--
 5 files changed, 31 insertions(+), 2 deletions(-)

diff --git a/src/Cursor.cpp b/src/Cursor.cpp
index 56a9dd8..735ee34 100644
--- a/src/Cursor.cpp
+++ b/src/Cursor.cpp
@@ -607,6 +607,12 @@ void CursorData::endUndoGroup() const
 }
 
 
+void CursorData::splitUndoGroup() const
+{
+       buffer()->undo().splitUndoGroup(*this);
+}
+
+
 void CursorData::recordUndo(pit_type from, pit_type to) const
 {
        buffer()->undo().recordUndo(*this, from, to);
diff --git a/src/Cursor.h b/src/Cursor.h
index 38ddad3..3d3db5d 100644
--- a/src/Cursor.h
+++ b/src/Cursor.h
@@ -183,6 +183,8 @@ public:
        void beginUndoGroup() const;
        /// end the current undo group
        void endUndoGroup() const;
+       /// end abruptly the current group and create a new one wih the same 
nesting level
+       void splitUndoGroup() const;
 
        /// The general case: prepare undo for an arbitrary range.
        void recordUndo(pit_type from, pit_type to) const;
diff --git a/src/Undo.cpp b/src/Undo.cpp
index 5d613f2..a9e0bba 100644
--- a/src/Undo.cpp
+++ b/src/Undo.cpp
@@ -610,6 +610,16 @@ void Undo::endUndoGroup(CursorData const & cur_after)
 }
 
 
+void Undo::splitUndoGroup(CursorData const & cur)
+{
+       size_t const level = d->group_level_;
+       d->group_level_ = 1;
+       endUndoGroup(cur);
+       beginUndoGroup(cur);
+       d->group_level_ = level;
+}
+
+
 bool Undo::activeUndoGroup() const
 {
        return d->group_level_ > 0
diff --git a/src/Undo.h b/src/Undo.h
index dd70358..c2b9b5d 100644
--- a/src/Undo.h
+++ b/src/Undo.h
@@ -96,6 +96,8 @@ public:
        void endUndoGroup();
        /// end the current undo group and set UndoElement::cur_after if 
necessary.
        void endUndoGroup(CursorData const & cur_after);
+       /// end abruptly the current group and create a new one wih the same 
nesting level
+       void splitUndoGroup(CursorData const & cur);
        /// return true if an undo group is open and contains at least one 
element
        bool activeUndoGroup() const;
 
diff --git a/src/mathed/MathAutoCorrect.cpp b/src/mathed/MathAutoCorrect.cpp
index be5aeb4..77e5fef 100644
--- a/src/mathed/MathAutoCorrect.cpp
+++ b/src/mathed/MathAutoCorrect.cpp
@@ -90,8 +90,17 @@ bool Correction::correct(Cursor & cur, char_type c) const
        LYXERR(Debug::MATHED, "match found! subst in " << cur.cell()
                << " from: '" << from1_ << "' to '" << to_ << '\'');
 
-       cur.cell().erase(cur.pos() - n, cur.pos());
-       cur.pos() -= n;
+       /* To allow undoing the completion, we proceed in 4 steps
+        * - inset the raw character
+        * - split undo group so that we have two separate undo actions
+        * - record undo, delete the character we just entered and the from1_ 
part
+        * - finally, do the insertion of the correction.
+        */
+       cur.insert(c);
+       cur.splitUndoGroup();
+       cur.recordUndoSelection();
+       cur.cell().erase(cur.pos() - n - 1, cur.pos());
+       cur.pos() -= n + 1;
 
        cur.insert(to_);
        return true;

Reply via email to