]> git.lyx.org Git - features.git/commitdiff
Allow to undo partly math autocorrect
authorJean-Marc Lasgouttes <lasgouttes@lyx.org>
Mon, 15 Apr 2019 09:12:25 +0000 (11:12 +0200)
committerJean-Marc Lasgouttes <lasgouttes@lyx.org>
Sun, 2 Jun 2019 19:17:26 +0000 (21:17 +0200)
To this end, introduce Undo::splitUndoGroup, which ends currently
group and creates a new one with same nesting level.

src/Cursor.cpp
src/Cursor.h
src/Undo.cpp
src/Undo.h
src/mathed/MathAutoCorrect.cpp

index 35a34527c0c48a3dee29e10c479c343bed867f46..7623c5b0c71eb9b8e82b1396870c02901822c093 100644 (file)
@@ -609,6 +609,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);
index 38ddad3502c314f36410d66c76965e567d4ce467..3d3db5daa2b78bd2c481ddcbc6536caf6c58453d 100644 (file)
@@ -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;
index 5d613f2e0db4f6c84ac4502690c11a11c3b80db8..a9e0bba8e29a0c4c1ff0019b6fd1996270f85522 100644 (file)
@@ -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
index dd7035816d9287a2cbbdee858c9943e0aa3fe5d2..c2b9b5d074b4e6e8fbef33e40e5aac73e74df3b3 100644 (file)
@@ -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;
 
index be5aeb42d0430c127179379784f8275431b83437..77e5fefbd4e7b541d3bce77d6795ae6ffd0b325e 100644 (file)
@@ -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;