]> git.lyx.org Git - features.git/commitdiff
Remember the cursor before an undo group
authorJean-Marc Lasgouttes <lasgouttes@lyx.org>
Thu, 16 Jul 2015 12:13:13 +0000 (14:13 +0200)
committerJean-Marc Lasgouttes <lasgouttes@lyx.org>
Tue, 21 Jul 2015 09:57:50 +0000 (11:57 +0200)
With this patch, the cursor position before undo can be saved when
creating an undo group. Any such value will supercede parameters
passed to recordUndo. Only the first such cursor set by nested
beginUndoGoup takes effect.

Fixes bug #9663.

src/Cursor.cpp
src/Undo.cpp
src/Undo.h

index c89db5d2a2145f0124eeb0ec0de6aba833bf0ced..f6ca0fcf6e13d56b50dd4260f215cc06e26fdd88 100644 (file)
@@ -2482,7 +2482,7 @@ void Cursor::finishUndo() const
 
 void Cursor::beginUndoGroup() const
 {
-       buffer()->undo().beginUndoGroup();
+       buffer()->undo().beginUndoGroup(*this);
 }
 
 
index 772b9d4745126629e62bf064af10e980ae8aa5d1..7b924c7b49a6e73b0bbca83287daa9f60a538860 100644 (file)
@@ -235,6 +235,8 @@ struct Undo::Private
        size_t group_id_;
        /// Current group nesting nevel.
        size_t group_level_;
+       /// the position of cursor before the group was created
+       CursorData group_cur_before_;
 };
 
 
@@ -340,8 +342,9 @@ void Undo::Private::doRecordUndo(UndoKind kind,
 
        LYXERR(Debug::UNDO, "Create undo element of group " << group_id_);
        // create the position information of the Undo entry
-       UndoElement undo(kind, cur_before, cell, from, end, 0, 0,
-                        buffer_.isClean(), group_id_);
+       UndoElement undo(kind,
+             group_cur_before_.empty() ? cur_before : group_cur_before_,
+             cell, from, end, 0, 0, buffer_.isClean(), group_id_);
 
        // fill in the real data to be saved
        if (cell.inMathed()) {
@@ -390,7 +393,7 @@ void Undo::Private::recordUndo(UndoKind kind,
 
 
 void Undo::Private::doRecordUndoBufferParams(CursorData const & cur_before,
-                                                                          UndoElementStack & stack)
+                                             UndoElementStack & stack)
 {
        if (!group_level_) {
                LYXERR0("There is no group open (creating one)");
@@ -399,7 +402,8 @@ void Undo::Private::doRecordUndoBufferParams(CursorData const & cur_before,
 
        LYXERR(Debug::UNDO, "Create full buffer undo element of group " << group_id_);
        // create the position information of the Undo entry
-       UndoElement undo(cur_before, buffer_.params(), buffer_.isClean(),
+       UndoElement undo(group_cur_before_.empty() ? cur_before : group_cur_before_,
+                     buffer_.params(), buffer_.isClean(),
                                         group_id_);
 
        // push the undo entry to undo stack
@@ -555,6 +559,14 @@ void Undo::beginUndoGroup()
 }
 
 
+void Undo::beginUndoGroup(CursorData const & cur_before)
+{
+       beginUndoGroup();
+       if (d->group_cur_before_.empty())
+               d->group_cur_before_ = cur_before;
+}
+
+
 void Undo::endUndoGroup()
 {
        if (d->group_level_ == 0) {
@@ -564,16 +576,17 @@ void Undo::endUndoGroup()
        --d->group_level_;
        if (d->group_level_ == 0) {
                // real end of the group
+               d->group_cur_before_ = CursorData();
                LYXERR(Debug::UNDO, "-------End of group " << d->group_id_);
        }
 }
 
 
-void Undo::endUndoGroup(CursorData const & cur)
+void Undo::endUndoGroup(CursorData const & cur_after)
 {
        endUndoGroup();
        if (!d->undostack_.empty() && d->undostack_.top().cur_after.empty())
-               d->undostack_.top().cur_after = cur;
+               d->undostack_.top().cur_after = cur_after;
 }
 
 
index c2acf4af216b78060c7ed3893a965fbfecaa57aa..1bd6450d7c3a03749952c322961a87c6d51e1099 100644 (file)
@@ -80,12 +80,19 @@ public:
         *  a single step. This means you can add a group whenever you are not sure.
         */
        void beginUndoGroup();
-
+       /// open a new group as above and specify a cursor to set as cur_before
+       /// of the group's undo elements.
+       /**
+        * This cursor takes precedence over what is passed to recordUndo.
+        * In the case of nested groups, only the first cur_before is
+        * taken in account. The cursor is reset at the end of the
+        * top-level group.
+        */
+       void beginUndoGroup(CursorData const & cur_before);
        /// end the current undo group.
        void endUndoGroup();
-
        /// end the current undo group and set UndoElement::cur_after if necessary.
-       void endUndoGroup(CursorData const &);
+       void endUndoGroup(CursorData const & cur_after);
 
        /// The general case: record undo information for an arbitrary range.
        /**